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