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