]> Dogcows Code - chaz/openbox/blob - openbox/client_menu.c
initalize static stuff.
[chaz/openbox] / 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 Time time)
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 prop_atoms.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 prop_atoms.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 Time time)
207 {
208 gulong ignore_start;
209
210 g_assert(c);
211
212 if (!config_focus_under_mouse)
213 ignore_start = event_start_ignore_all_enters();
214
215 switch (e->id) {
216 case LAYER_TOP:
217 client_set_layer(c, 1);
218 break;
219 case LAYER_NORMAL:
220 client_set_layer(c, 0);
221 break;
222 case LAYER_BOTTOM:
223 client_set_layer(c, -1);
224 break;
225 default:
226 g_assert_not_reached();
227 }
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 guint i;
243 ObMenuEntry *e;
244
245 menu_clear_entries(menu);
246
247 if (frame->client == NULL || !client_normal(frame->client))
248 return FALSE; /* don't show the menu */
249
250 for (i = 0; i <= screen_num_desktops; ++i) {
251 const gchar *name;
252 guint desk;
253
254 if (i >= screen_num_desktops) {
255 menu_add_separator(menu, -1, NULL);
256
257 desk = DESKTOP_ALL;
258 name = _("All desktops");
259 } else {
260 desk = i;
261 name = screen_desktop_names[i];
262 }
263
264 e = menu_add_normal(menu, desk, name, NULL, FALSE);
265 e->id = desk;
266 if (desk == DESKTOP_ALL) {
267 e->data.normal.mask = ob_rr_theme->desk_mask;
268 e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
269 e->data.normal.mask_selected_color =
270 ob_rr_theme->menu_selected_color;
271 e->data.normal.mask_disabled_color =
272 ob_rr_theme->menu_disabled_color;
273 e->data.normal.mask_disabled_selected_color =
274 ob_rr_theme->menu_disabled_selected_color;
275 }
276
277 if (frame->client->desktop == desk)
278 e->data.normal.enabled = FALSE;
279 }
280 return TRUE; /* show the menu */
281 }
282
283 static void send_to_menu_execute(ObMenuEntry *e, ObMenuFrame *f,
284 ObClient *c, guint state, gpointer data,
285 Time time)
286 {
287 g_assert(c);
288
289 client_set_desktop(c, e->id, FALSE);
290 /* the client won't even be on the screen anymore, so hide the menu */
291 if (f)
292 menu_frame_hide_all();
293 }
294
295 static void client_menu_place(ObMenuFrame *frame, gint *x, gint *y,
296 gint button, gpointer data)
297 {
298 gint dx, dy;
299
300 if (button == 0 && frame->client) {
301 *x = frame->client->frame->area.x;
302
303 /* try below the titlebar */
304 *y = frame->client->frame->area.y + frame->client->frame->size.top -
305 frame->client->frame->bwidth;
306 menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
307 if (dy != 0) {
308 /* try above the titlebar */
309 *y = frame->client->frame->area.y + frame->client->frame->bwidth -
310 frame->area.height;
311 menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
312 }
313 if (dy != 0) {
314 /* didnt fit either way, use move on screen's values */
315 *y = frame->client->frame->area.y + frame->client->frame->size.top;
316 menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
317 }
318
319 *x += dx;
320 *y += dy;
321 } else {
322 gint myx, myy;
323
324 myx = *x;
325 myy = *y;
326
327 /* try to the bottom right of the cursor */
328 menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
329 if (dx != 0 || dy != 0) {
330 /* try to the bottom left of the cursor */
331 myx = *x - frame->area.width;
332 myy = *y;
333 menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
334 }
335 if (dx != 0 || dy != 0) {
336 /* try to the top right of the cursor */
337 myx = *x;
338 myy = *y - frame->area.height;
339 menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
340 }
341 if (dx != 0 || dy != 0) {
342 /* try to the top left of the cursor */
343 myx = *x - frame->area.width;
344 myy = *y - frame->area.height;
345 menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
346 }
347 if (dx != 0 || dy != 0) {
348 /* if didnt fit on either side so just use what it says */
349 myx = *x;
350 myy = *y;
351 menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
352 }
353 *x = myx + dx;
354 *y = myy + dy;
355 }
356 }
357
358 void client_menu_startup()
359 {
360 ObMenu *menu;
361 ObMenuEntry *e;
362
363 menu = menu_new(LAYER_MENU_NAME, _("&Layer"), TRUE, NULL);
364 menu_show_all_shortcuts(menu, TRUE);
365 menu_set_update_func(menu, layer_menu_update);
366 menu_set_execute_func(menu, layer_menu_execute);
367
368 menu_add_normal(menu, LAYER_TOP, _("Always on &top"), NULL, TRUE);
369 menu_add_normal(menu, LAYER_NORMAL, _("&Normal"), NULL, TRUE);
370 menu_add_normal(menu, LAYER_BOTTOM, _("Always on &bottom"),NULL, TRUE);
371
372
373 menu = menu_new(SEND_TO_MENU_NAME, _("&Send to desktop"), TRUE, NULL);
374 menu_set_update_func(menu, send_to_menu_update);
375 menu_set_execute_func(menu, send_to_menu_execute);
376
377 menu = menu_new(CLIENT_MENU_NAME, _("Client menu"), TRUE, NULL);
378 menu_show_all_shortcuts(menu, TRUE);
379 menu_set_update_func(menu, client_menu_update);
380 menu_set_place_func(menu, client_menu_place);
381 menu_set_execute_func(menu, client_menu_execute);
382
383 e = menu_add_normal(menu, CLIENT_RESTORE, _("R&estore"), NULL, TRUE);
384 e->data.normal.mask = ob_rr_theme->max_toggled_mask;
385 e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
386 e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
387 e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
388 e->data.normal.mask_disabled_selected_color =
389 ob_rr_theme->menu_disabled_selected_color;
390
391 menu_add_normal(menu, CLIENT_MOVE, _("&Move"), NULL, TRUE);
392
393 menu_add_normal(menu, CLIENT_RESIZE, _("Resi&ze"), NULL, TRUE);
394
395 e = menu_add_normal(menu, CLIENT_ICONIFY, _("Ico&nify"), NULL, TRUE);
396 e->data.normal.mask = ob_rr_theme->iconify_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_MAXIMIZE, _("Ma&ximize"), NULL, TRUE);
404 e->data.normal.mask = ob_rr_theme->max_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_SHADE, _("&Roll up/down"), NULL, TRUE);
412
413 menu_add_normal(menu, CLIENT_DECORATE, _("Un/&Decorate"), NULL, TRUE);
414
415 menu_add_separator(menu, -1, NULL);
416
417 menu_add_submenu(menu, CLIENT_SEND_TO, SEND_TO_MENU_NAME);
418
419 menu_add_submenu(menu, CLIENT_LAYER, LAYER_MENU_NAME);
420
421 menu_add_separator(menu, -1, NULL);
422
423 e = menu_add_normal(menu, CLIENT_CLOSE, _("&Close"), NULL, TRUE);
424 e->data.normal.mask = ob_rr_theme->close_mask;
425 e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
426 e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
427 e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
428 e->data.normal.mask_disabled_selected_color =
429 ob_rr_theme->menu_disabled_selected_color;
430 }
This page took 0.057012 seconds and 4 git commands to generate.