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