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