]> icculus.org git repositories - dana/openbox.git/blob - openbox/client_menu.c
update the client menu when you do stuff without closing it.
[dana/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         break;
125     case CLIENT_RESTORE:
126         client_maximize(c, FALSE, 0);
127         break;
128     case CLIENT_MAXIMIZE:
129         client_maximize(c, TRUE, 0);
130         break;
131     case CLIENT_SHADE:
132         client_shade(c, !c->shaded);
133         break;
134     case CLIENT_DECORATE:
135         client_set_undecorated(c, !c->undecorated);
136         break;
137     case CLIENT_MOVE:
138         moveresize_start(c,0,0,0, prop_atoms.net_wm_moveresize_move_keyboard);
139         break;
140     case CLIENT_RESIZE:
141         moveresize_start(c,0,0,0,prop_atoms.net_wm_moveresize_size_keyboard);
142         break;
143     case CLIENT_CLOSE:
144         client_close(c);
145         break;
146     default:
147         g_assert_not_reached();
148     }
149
150     /* update the menu cuz stuff can have changed */
151     client_menu_update(f, NULL);
152     menu_frame_render(f);
153 }
154
155 static gboolean layer_menu_update(ObMenuFrame *frame, gpointer data)
156 {
157     ObMenu *menu = frame->menu;
158     GList *it;
159
160     if (frame->client == NULL || !client_normal(frame->client))
161         return FALSE; /* don't show the menu */
162
163     for (it = menu->entries; it; it = g_list_next(it)) {
164         ObMenuEntry *e = it->data;
165         gboolean *en = &e->data.normal.enabled; /* save some typing */
166         ObClient *c = frame->client;
167
168         if (e->type == OB_MENU_ENTRY_TYPE_NORMAL) {
169             switch (e->id) {
170             case LAYER_TOP:
171                 *en = !c->above;
172                 break;
173             case LAYER_NORMAL:
174                 *en = c->above || c->below;
175                 break;
176             case LAYER_BOTTOM:
177                 *en = !c->below;
178                 break;
179             default:
180                 *en = TRUE;
181             }
182         }
183     }
184     return TRUE; /* show the menu */
185 }
186
187 static void layer_menu_execute(ObMenuEntry *e, guint state, gpointer data,
188                                Time time)
189 {
190     GList *it;
191     ObMenuFrame *f;
192
193     /* find our frame */
194     for (it = menu_frame_visible; it; it = g_list_next(it)) {
195         f = it->data;
196         /* yay this is our menu frame */
197         if (f->menu == e->menu)
198             break;
199     }
200     g_assert(it);
201
202     switch (e->id) {
203     case LAYER_TOP:
204         client_set_layer(f->client, 1);
205         break;
206     case LAYER_NORMAL:
207         client_set_layer(f->client, 0);
208         break;
209     case LAYER_BOTTOM:
210         client_set_layer(f->client, -1);
211         break;
212     default:
213         g_assert_not_reached();
214     }
215
216     /* update the menu cuz stuff can have changed */
217     layer_menu_update(f, NULL);
218     menu_frame_render(f);
219 }
220
221 static gboolean send_to_menu_update(ObMenuFrame *frame, gpointer data)
222 {
223     ObMenu *menu = frame->menu;
224     guint i;
225     ObMenuEntry *e;
226
227     menu_clear_entries(menu);
228
229     if (frame->client == NULL || !client_normal(frame->client))
230         return FALSE; /* don't show the menu */
231
232     for (i = 0; i <= screen_num_desktops; ++i) {
233         const gchar *name;
234         guint desk;
235
236         if (i >= screen_num_desktops) {
237             menu_add_separator(menu, -1, NULL);
238
239             desk = DESKTOP_ALL;
240             name = _("All desktops");
241         } else {
242             desk = i;
243             name = screen_desktop_names[i];
244         }
245
246         e = menu_add_normal(menu, desk, name, NULL, FALSE);
247         e->id = desk;
248         if (desk == DESKTOP_ALL) {
249             e->data.normal.mask = ob_rr_theme->desk_mask;
250             e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
251             e->data.normal.mask_selected_color =
252                 ob_rr_theme->menu_selected_color;
253             e->data.normal.mask_disabled_color =
254                 ob_rr_theme->menu_disabled_color;
255             e->data.normal.mask_disabled_selected_color =
256                 ob_rr_theme->menu_disabled_selected_color;
257         }
258
259         if (frame->client->desktop == desk)
260             e->data.normal.enabled = FALSE;
261     }
262     return TRUE; /* show the menu */
263 }
264
265 static void send_to_menu_execute(ObMenuEntry *e, guint state, gpointer data,
266                                  Time time)
267 {
268     GList *it;
269     ObMenuFrame *f;
270
271     /* find our frame */
272     for (it = menu_frame_visible; it; it = g_list_next(it)) {
273         f = it->data;
274         /* yay this is our menu frame */
275         if (f->menu == e->menu)
276             break;
277     }
278     g_assert(it);
279
280     client_set_desktop(f->client, e->id, FALSE);
281     /* the client won't even be on the screen anymore, so hide the menu */
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     e = menu_add_normal(menu, CLIENT_SHADE, _("&Roll up/down"), NULL, TRUE);
402     e->data.normal.mask = ob_rr_theme->shade_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     menu_add_normal(menu, CLIENT_DECORATE, _("Un/&Decorate"), NULL, TRUE);
410
411     menu_add_separator(menu, -1, NULL);
412
413     menu_add_submenu(menu, CLIENT_SEND_TO, SEND_TO_MENU_NAME);
414
415     menu_add_submenu(menu, CLIENT_LAYER, LAYER_MENU_NAME);
416
417     menu_add_separator(menu, -1, NULL);
418
419     e = menu_add_normal(menu, CLIENT_CLOSE, _("&Close"), NULL, TRUE);
420     e->data.normal.mask = ob_rr_theme->close_mask;
421     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
422     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
423     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
424     e->data.normal.mask_disabled_selected_color =
425         ob_rr_theme->menu_disabled_selected_color;
426 }