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