f538eafd7f36ae263a81e2feecd8bba0f932073a
[mikachu/openbox.git] / openbox / client_menu.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    client_menu.c for the Openbox window manager
4    Copyright (c) 2003-2007   Dana Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "debug.h"
20 #include "menu.h"
21 #include "menuframe.h"
22 #include "screen.h"
23 #include "client.h"
24 #include "openbox.h"
25 #include "frame.h"
26 #include "moveresize.h"
27 #include "event.h"
28 #include "prop.h"
29 #include "gettext.h"
30
31 #include <glib.h>
32
33 #define CLIENT_MENU_NAME  "client-menu"
34 #define SEND_TO_MENU_NAME "client-send-to-menu"
35 #define LAYER_MENU_NAME   "client-layer-menu"
36
37 enum {
38     LAYER_TOP,
39     LAYER_NORMAL,
40     LAYER_BOTTOM
41 };
42
43 enum {
44     CLIENT_SEND_TO,
45     CLIENT_LAYER,
46     CLIENT_ICONIFY,
47     CLIENT_RESTORE,
48     CLIENT_MAXIMIZE,
49     CLIENT_SHADE,
50     CLIENT_DECORATE,
51     CLIENT_MOVE,
52     CLIENT_RESIZE,
53     CLIENT_CLOSE
54 };
55
56 static gboolean client_menu_update(ObMenuFrame *frame, gpointer data)
57 {
58     ObMenu *menu = frame->menu;
59     GList *it;
60
61     if (frame->client == NULL || !client_normal(frame->client))
62         return FALSE; /* don't show the menu */
63
64     for (it = menu->entries; it; it = g_list_next(it)) {
65         ObMenuEntry *e = it->data;
66         gboolean *en = &e->data.normal.enabled; /* save some typing */
67         ObClient *c = frame->client;
68
69         if (e->type == OB_MENU_ENTRY_TYPE_NORMAL) {
70             switch (e->id) {
71             case CLIENT_ICONIFY:
72                 *en = c->functions & OB_CLIENT_FUNC_ICONIFY;
73                 break;
74             case CLIENT_RESTORE:
75                 *en = c->max_horz || c->max_vert;
76                 break;
77             case CLIENT_MAXIMIZE:
78                 *en = ((c->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
79                        (!c->max_horz || !c->max_vert));
80                 break;
81             case CLIENT_SHADE:
82                 *en = c->functions & OB_CLIENT_FUNC_SHADE;
83                 break;
84             case CLIENT_MOVE:
85                 *en = c->functions & OB_CLIENT_FUNC_MOVE;
86                 break;
87             case CLIENT_RESIZE:
88                 *en = c->functions & OB_CLIENT_FUNC_RESIZE;
89                 break;
90             case CLIENT_CLOSE:
91                 *en = c->functions & OB_CLIENT_FUNC_CLOSE;
92                 break;
93             case CLIENT_DECORATE:
94                 *en = c->functions & OB_CLIENT_FUNC_UNDECORATE;
95                 break;
96             default:
97                 *en = TRUE;
98             }
99         }
100     }
101     return TRUE; /* show the menu */
102 }
103
104 static void client_menu_execute(ObMenuEntry *e, ObMenuFrame *f,
105                                 ObClient *c, guint state, gpointer data,
106                                 Time time)
107 {
108     gint x, y;
109
110     g_assert(c);
111
112     switch (e->id) {
113     case CLIENT_ICONIFY:
114         /* the client won't be on screen anymore so hide the menu */
115         menu_frame_hide_all();
116         f = NULL; /* and don't update */
117
118         client_iconify(c, TRUE, FALSE, FALSE);
119         break;
120     case CLIENT_RESTORE:
121         client_maximize(c, FALSE, 0);
122         break;
123     case CLIENT_MAXIMIZE:
124         client_maximize(c, TRUE, 0);
125         break;
126     case CLIENT_SHADE:
127         client_shade(c, !c->shaded);
128         break;
129     case CLIENT_DECORATE:
130         client_set_undecorated(c, !c->undecorated);
131         break;
132     case CLIENT_MOVE:
133         /* this needs to grab the keyboard so hide the menu */
134         menu_frame_hide_all();
135         f = NULL; /* and don't update */
136
137         screen_pointer_pos(&x, &y);
138         moveresize_start(c, x, y, 0,
139                          prop_atoms.net_wm_moveresize_move_keyboard);
140         break;
141     case CLIENT_RESIZE:
142         /* this needs to grab the keyboard so hide the menu */
143         menu_frame_hide_all();
144         f = NULL; /* and don't update */
145
146         screen_pointer_pos(&x, &y);
147         moveresize_start(c, x, y, 0,
148                          prop_atoms.net_wm_moveresize_size_keyboard);
149         break;
150     case CLIENT_CLOSE:
151         client_close(c);
152         break;
153     default:
154         g_assert_not_reached();
155     }
156
157     event_ignore_all_queued_enters();
158
159     /* update the menu cuz stuff can have changed */
160     if (f) {
161         client_menu_update(f, NULL);
162         menu_frame_render(f);
163     }
164 }
165
166 static gboolean layer_menu_update(ObMenuFrame *frame, gpointer data)
167 {
168     ObMenu *menu = frame->menu;
169     GList *it;
170
171     if (frame->client == NULL || !client_normal(frame->client))
172         return FALSE; /* don't show the menu */
173
174     for (it = menu->entries; it; it = g_list_next(it)) {
175         ObMenuEntry *e = it->data;
176         gboolean *en = &e->data.normal.enabled; /* save some typing */
177         ObClient *c = frame->client;
178
179         if (e->type == OB_MENU_ENTRY_TYPE_NORMAL) {
180             switch (e->id) {
181             case LAYER_TOP:
182                 *en = !c->above && (c->functions & OB_CLIENT_FUNC_ABOVE);
183                 break;
184             case LAYER_NORMAL:
185                 *en = c->above || c->below;
186                 break;
187             case LAYER_BOTTOM:
188                 *en = !c->below && (c->functions & OB_CLIENT_FUNC_BELOW);
189                 break;
190             default:
191                 *en = TRUE;
192             }
193         }
194     }
195     return TRUE; /* show the menu */
196 }
197
198 static void layer_menu_execute(ObMenuEntry *e, ObMenuFrame *f,
199                                ObClient *c, guint state, gpointer data,
200                                Time time)
201 {
202     g_assert(c);
203
204     switch (e->id) {
205     case LAYER_TOP:
206         client_set_layer(c, 1);
207         break;
208     case LAYER_NORMAL:
209         client_set_layer(c, 0);
210         break;
211     case LAYER_BOTTOM:
212         client_set_layer(c, -1);
213         break;
214     default:
215         g_assert_not_reached();
216     }
217
218     event_ignore_all_queued_enters();
219
220     /* update the menu cuz stuff can have changed */
221     if (f) {
222         layer_menu_update(f, NULL);
223         menu_frame_render(f);
224     }
225 }
226
227 static gboolean send_to_menu_update(ObMenuFrame *frame, gpointer data)
228 {
229     ObMenu *menu = frame->menu;
230     guint i;
231     ObMenuEntry *e;
232
233     menu_clear_entries(menu);
234
235     if (frame->client == NULL || !client_normal(frame->client))
236         return FALSE; /* don't show the menu */
237
238     for (i = 0; i <= screen_num_desktops; ++i) {
239         const gchar *name;
240         guint desk;
241
242         if (i >= screen_num_desktops) {
243             menu_add_separator(menu, -1, NULL);
244
245             desk = DESKTOP_ALL;
246             name = _("All desktops");
247         } else {
248             desk = i;
249             name = screen_desktop_names[i];
250         }
251
252         e = menu_add_normal(menu, desk, name, NULL, FALSE);
253         e->id = desk;
254         if (desk == DESKTOP_ALL) {
255             e->data.normal.mask = ob_rr_theme->desk_mask;
256             e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
257             e->data.normal.mask_selected_color =
258                 ob_rr_theme->menu_selected_color;
259             e->data.normal.mask_disabled_color =
260                 ob_rr_theme->menu_disabled_color;
261             e->data.normal.mask_disabled_selected_color =
262                 ob_rr_theme->menu_disabled_selected_color;
263         }
264
265         if (frame->client->desktop == desk)
266             e->data.normal.enabled = FALSE;
267     }
268     return TRUE; /* show the menu */
269 }
270
271 static void send_to_menu_execute(ObMenuEntry *e, ObMenuFrame *f,
272                                  ObClient *c, guint state, gpointer data,
273                                  Time time)
274 {
275     g_assert(c);
276
277     client_set_desktop(c, e->id, FALSE);
278     /* the client won't even be on the screen anymore, so hide the menu */
279     if (f)
280         menu_frame_hide_all();
281 }
282
283 static void client_menu_place(ObMenuFrame *frame, gint *x, gint *y,
284                               gint button, gpointer data)
285 {
286     gint dx, dy;
287
288     if (button == 0 && frame->client) {
289         *x = frame->client->frame->area.x;
290
291         /* try below the titlebar */
292         *y = frame->client->frame->area.y + frame->client->frame->size.top -
293             frame->client->frame->bwidth;
294         menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
295         if (dy != 0) {
296             /* try above the titlebar */
297             *y = frame->client->frame->area.y + frame->client->frame->bwidth -
298                 frame->area.height;
299             menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
300         }
301         if (dy != 0) {
302             /* didnt fit either way, use move on screen's values */
303             *y = frame->client->frame->area.y + frame->client->frame->size.top;
304             menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
305         }
306
307         *x += dx;
308         *y += dy;
309     } else {
310         gint myx, myy;
311
312         myx = *x;
313         myy = *y;
314
315         /* try to the bottom right of the cursor */
316         menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
317         if (dx != 0 || dy != 0) {
318             /* try to the bottom left of the cursor */
319             myx = *x - frame->area.width;
320             myy = *y;
321             menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
322         }
323         if (dx != 0 || dy != 0) {
324             /* try to the top right of the cursor */
325             myx = *x;
326             myy = *y - frame->area.height;
327             menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
328         }
329         if (dx != 0 || dy != 0) {
330             /* try to the top left of the cursor */
331             myx = *x - frame->area.width;
332             myy = *y - frame->area.height;
333             menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
334         }
335         if (dx != 0 || dy != 0) {
336             /* if didnt fit on either side so just use what it says */
337             myx = *x;
338             myy = *y;
339             menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
340         }
341         *x = myx + dx;
342         *y = myy + dy;
343     }
344 }
345
346 void client_menu_startup()
347 {
348     ObMenu *menu;
349     ObMenuEntry *e;
350
351     menu = menu_new(LAYER_MENU_NAME, _("&Layer"), TRUE, NULL);
352     menu_show_all_shortcuts(menu, TRUE);
353     menu_set_update_func(menu, layer_menu_update);
354     menu_set_execute_func(menu, layer_menu_execute);
355
356     menu_add_normal(menu, LAYER_TOP, _("Always on &top"), NULL, TRUE);
357     menu_add_normal(menu, LAYER_NORMAL, _("&Normal"), NULL, TRUE);
358     menu_add_normal(menu, LAYER_BOTTOM, _("Always on &bottom"),NULL, TRUE);
359
360
361     menu = menu_new(SEND_TO_MENU_NAME, _("&Send to desktop"), TRUE, NULL);
362     menu_set_update_func(menu, send_to_menu_update);
363     menu_set_execute_func(menu, send_to_menu_execute);
364
365     menu = menu_new(CLIENT_MENU_NAME, _("Client menu"), TRUE, NULL);
366     menu_show_all_shortcuts(menu, TRUE);
367     menu_set_update_func(menu, client_menu_update);
368     menu_set_place_func(menu, client_menu_place);
369     menu_set_execute_func(menu, client_menu_execute);
370
371     e = menu_add_normal(menu, CLIENT_RESTORE, _("R&estore"), NULL, TRUE);
372     e->data.normal.mask = ob_rr_theme->max_toggled_mask; 
373     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
374     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
375     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
376     e->data.normal.mask_disabled_selected_color =
377         ob_rr_theme->menu_disabled_selected_color;
378
379     menu_add_normal(menu, CLIENT_MOVE, _("&Move"), NULL, TRUE);
380
381     menu_add_normal(menu, CLIENT_RESIZE, _("Resi&ze"), NULL, TRUE);
382
383     e = menu_add_normal(menu, CLIENT_ICONIFY, _("Ico&nify"), NULL, TRUE);
384     e->data.normal.mask = ob_rr_theme->iconify_mask;
385     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
386     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
387     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
388     e->data.normal.mask_disabled_selected_color =
389         ob_rr_theme->menu_disabled_selected_color;
390
391     e = menu_add_normal(menu, CLIENT_MAXIMIZE, _("Ma&ximize"), NULL, TRUE);
392     e->data.normal.mask = ob_rr_theme->max_mask; 
393     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
394     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
395     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
396     e->data.normal.mask_disabled_selected_color =
397         ob_rr_theme->menu_disabled_selected_color;
398
399     menu_add_normal(menu, CLIENT_SHADE, _("&Roll up/down"), NULL, TRUE);
400
401     menu_add_normal(menu, CLIENT_DECORATE, _("Un/&Decorate"), NULL, TRUE);
402
403     menu_add_separator(menu, -1, NULL);
404
405     menu_add_submenu(menu, CLIENT_SEND_TO, SEND_TO_MENU_NAME);
406
407     menu_add_submenu(menu, CLIENT_LAYER, LAYER_MENU_NAME);
408
409     menu_add_separator(menu, -1, NULL);
410
411     e = menu_add_normal(menu, CLIENT_CLOSE, _("&Close"), NULL, TRUE);
412     e->data.normal.mask = ob_rr_theme->close_mask;
413     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
414     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
415     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
416     e->data.normal.mask_disabled_selected_color =
417         ob_rr_theme->menu_disabled_selected_color;
418 }