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