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