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