kill enter events without using pointer grabs
[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 = client_normal(c);
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         event_ignore_queued_enters();
123         break;
124     case CLIENT_MAXIMIZE:
125         client_maximize(c, TRUE, 0);
126         event_ignore_queued_enters();
127         break;
128     case CLIENT_SHADE:
129         client_shade(c, !c->shaded);
130         event_ignore_queued_enters();
131         break;
132     case CLIENT_DECORATE:
133         client_set_undecorated(c, !c->undecorated);
134         event_ignore_queued_enters();
135         break;
136     case CLIENT_MOVE:
137         /* this needs to grab the keyboard so hide the menu */
138         menu_frame_hide_all();
139         f = NULL; /* and don't update */
140
141         screen_pointer_pos(&x, &y);
142         moveresize_start(c, x, y, 0,
143                          prop_atoms.net_wm_moveresize_move_keyboard);
144         break;
145     case CLIENT_RESIZE:
146         /* this needs to grab the keyboard so hide the menu */
147         menu_frame_hide_all();
148         f = NULL; /* and don't update */
149
150         screen_pointer_pos(&x, &y);
151         moveresize_start(c, x, y, 0,
152                          prop_atoms.net_wm_moveresize_size_keyboard);
153         break;
154     case CLIENT_CLOSE:
155         client_close(c);
156         break;
157     default:
158         g_assert_not_reached();
159     }
160
161     /* update the menu cuz stuff can have changed */
162     if (f) {
163         client_menu_update(f, NULL);
164         menu_frame_render(f);
165     }
166 }
167
168 static gboolean layer_menu_update(ObMenuFrame *frame, gpointer data)
169 {
170     ObMenu *menu = frame->menu;
171     GList *it;
172
173     if (frame->client == NULL || !client_normal(frame->client))
174         return FALSE; /* don't show the menu */
175
176     for (it = menu->entries; it; it = g_list_next(it)) {
177         ObMenuEntry *e = it->data;
178         gboolean *en = &e->data.normal.enabled; /* save some typing */
179         ObClient *c = frame->client;
180
181         if (e->type == OB_MENU_ENTRY_TYPE_NORMAL) {
182             switch (e->id) {
183             case LAYER_TOP:
184                 *en = !c->above && (c->functions & OB_CLIENT_FUNC_ABOVE);
185                 break;
186             case LAYER_NORMAL:
187                 *en = c->above || c->below;
188                 break;
189             case LAYER_BOTTOM:
190                 *en = !c->below && (c->functions & OB_CLIENT_FUNC_BELOW);
191                 break;
192             default:
193                 *en = TRUE;
194             }
195         }
196     }
197     return TRUE; /* show the menu */
198 }
199
200 static void layer_menu_execute(ObMenuEntry *e, ObMenuFrame *f,
201                                ObClient *c, guint state, gpointer data,
202                                Time time)
203 {
204     g_assert(c);
205
206     switch (e->id) {
207     case LAYER_TOP:
208         client_set_layer(c, 1);
209         break;
210     case LAYER_NORMAL:
211         client_set_layer(c, 0);
212         break;
213     case LAYER_BOTTOM:
214         client_set_layer(c, -1);
215         break;
216     default:
217         g_assert_not_reached();
218     }
219
220     event_ignore_queued_enters();
221
222     /* update the menu cuz stuff can have changed */
223     if (f) {
224         layer_menu_update(f, NULL);
225         menu_frame_render(f);
226     }
227 }
228
229 static gboolean send_to_menu_update(ObMenuFrame *frame, gpointer data)
230 {
231     ObMenu *menu = frame->menu;
232     guint i;
233     ObMenuEntry *e;
234
235     menu_clear_entries(menu);
236
237     if (frame->client == NULL || !client_normal(frame->client))
238         return FALSE; /* don't show the menu */
239
240     for (i = 0; i <= screen_num_desktops; ++i) {
241         const gchar *name;
242         guint desk;
243
244         if (i >= screen_num_desktops) {
245             menu_add_separator(menu, -1, NULL);
246
247             desk = DESKTOP_ALL;
248             name = _("All desktops");
249         } else {
250             desk = i;
251             name = screen_desktop_names[i];
252         }
253
254         e = menu_add_normal(menu, desk, name, NULL, FALSE);
255         e->id = desk;
256         if (desk == DESKTOP_ALL) {
257             e->data.normal.mask = ob_rr_theme->desk_mask;
258             e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
259             e->data.normal.mask_selected_color =
260                 ob_rr_theme->menu_selected_color;
261             e->data.normal.mask_disabled_color =
262                 ob_rr_theme->menu_disabled_color;
263             e->data.normal.mask_disabled_selected_color =
264                 ob_rr_theme->menu_disabled_selected_color;
265         }
266
267         if (frame->client->desktop == desk)
268             e->data.normal.enabled = FALSE;
269     }
270     return TRUE; /* show the menu */
271 }
272
273 static void send_to_menu_execute(ObMenuEntry *e, ObMenuFrame *f,
274                                  ObClient *c, guint state, gpointer data,
275                                  Time time)
276 {
277     g_assert(c);
278
279     client_set_desktop(c, e->id, FALSE);
280     /* the client won't even be on the screen anymore, so hide the menu */
281     if (f)
282         menu_frame_hide_all();
283 }
284
285 static void client_menu_place(ObMenuFrame *frame, gint *x, gint *y,
286                               gint button, gpointer data)
287 {
288     gint dx, dy;
289
290     if (button == 0 && frame->client) {
291         *x = frame->client->frame->area.x;
292
293         /* try below the titlebar */
294         *y = frame->client->frame->area.y + frame->client->frame->size.top -
295             frame->client->frame->bwidth;
296         menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
297         if (dy != 0) {
298             /* try above the titlebar */
299             *y = frame->client->frame->area.y + frame->client->frame->bwidth -
300                 frame->area.height;
301             menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
302         }
303         if (dy != 0) {
304             /* didnt fit either way, use move on screen's values */
305             *y = frame->client->frame->area.y + frame->client->frame->size.top;
306             menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
307         }
308
309         *x += dx;
310         *y += dy;
311     } else {
312         gint myx, myy;
313
314         myx = *x;
315         myy = *y;
316
317         /* try to the bottom right of the cursor */
318         menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
319         if (dx != 0 || dy != 0) {
320             /* try to the bottom left of the cursor */
321             myx = *x - frame->area.width;
322             myy = *y;
323             menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
324         }
325         if (dx != 0 || dy != 0) {
326             /* try to the top right of the cursor */
327             myx = *x;
328             myy = *y - frame->area.height;
329             menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
330         }
331         if (dx != 0 || dy != 0) {
332             /* try to the top left of the cursor */
333             myx = *x - frame->area.width;
334             myy = *y - frame->area.height;
335             menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
336         }
337         if (dx != 0 || dy != 0) {
338             /* if didnt fit on either side so just use what it says */
339             myx = *x;
340             myy = *y;
341             menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
342         }
343         *x = myx + dx;
344         *y = myy + dy;
345     }
346 }
347
348 void client_menu_startup()
349 {
350     ObMenu *menu;
351     ObMenuEntry *e;
352
353     menu = menu_new(LAYER_MENU_NAME, _("&Layer"), TRUE, NULL);
354     menu_show_all_shortcuts(menu, TRUE);
355     menu_set_update_func(menu, layer_menu_update);
356     menu_set_execute_func(menu, layer_menu_execute);
357
358     menu_add_normal(menu, LAYER_TOP, _("Always on &top"), NULL, TRUE);
359     menu_add_normal(menu, LAYER_NORMAL, _("&Normal"), NULL, TRUE);
360     menu_add_normal(menu, LAYER_BOTTOM, _("Always on &bottom"),NULL, TRUE);
361
362
363     menu = menu_new(SEND_TO_MENU_NAME, _("&Send to desktop"), TRUE, NULL);
364     menu_set_update_func(menu, send_to_menu_update);
365     menu_set_execute_func(menu, send_to_menu_execute);
366
367     menu = menu_new(CLIENT_MENU_NAME, _("Client menu"), TRUE, NULL);
368     menu_show_all_shortcuts(menu, TRUE);
369     menu_set_update_func(menu, client_menu_update);
370     menu_set_place_func(menu, client_menu_place);
371     menu_set_execute_func(menu, client_menu_execute);
372
373     e = menu_add_normal(menu, CLIENT_RESTORE, _("R&estore"), NULL, TRUE);
374     e->data.normal.mask = ob_rr_theme->max_toggled_mask; 
375     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
376     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
377     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
378     e->data.normal.mask_disabled_selected_color =
379         ob_rr_theme->menu_disabled_selected_color;
380
381     menu_add_normal(menu, CLIENT_MOVE, _("&Move"), NULL, TRUE);
382
383     menu_add_normal(menu, CLIENT_RESIZE, _("Resi&ze"), NULL, TRUE);
384
385     e = menu_add_normal(menu, CLIENT_ICONIFY, _("Ico&nify"), NULL, TRUE);
386     e->data.normal.mask = ob_rr_theme->iconify_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     e = menu_add_normal(menu, CLIENT_MAXIMIZE, _("Ma&ximize"), NULL, TRUE);
394     e->data.normal.mask = ob_rr_theme->max_mask; 
395     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
396     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
397     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
398     e->data.normal.mask_disabled_selected_color =
399         ob_rr_theme->menu_disabled_selected_color;
400
401     menu_add_normal(menu, CLIENT_SHADE, _("&Roll up/down"), NULL, TRUE);
402
403     menu_add_normal(menu, CLIENT_DECORATE, _("Un/&Decorate"), NULL, TRUE);
404
405     menu_add_separator(menu, -1, NULL);
406
407     menu_add_submenu(menu, CLIENT_SEND_TO, SEND_TO_MENU_NAME);
408
409     menu_add_submenu(menu, CLIENT_LAYER, LAYER_MENU_NAME);
410
411     menu_add_separator(menu, -1, NULL);
412
413     e = menu_add_normal(menu, CLIENT_CLOSE, _("&Close"), NULL, TRUE);
414     e->data.normal.mask = ob_rr_theme->close_mask;
415     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
416     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
417     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
418     e->data.normal.mask_disabled_selected_color =
419         ob_rr_theme->menu_disabled_selected_color;
420 }