1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 client_menu.c for the Openbox window manager
4 Copyright (c) 2003-2007 Dana Jansens
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.
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.
16 See the COPYING file for a copy of the GNU General Public License.
21 #include "menuframe.h"
26 #include "moveresize.h"
33 #define CLIENT_MENU_NAME "client-menu"
34 #define SEND_TO_MENU_NAME "client-send-to-menu"
35 #define LAYER_MENU_NAME "client-layer-menu"
56 static gboolean client_menu_update(ObMenuFrame *frame, gpointer data)
58 ObMenu *menu = frame->menu;
61 if (frame->client == NULL || !client_normal(frame->client))
62 return FALSE; /* don't show the menu */
64 for (it = menu->entries; it; it = g_list_next(it)) {
65 ObMenuEntry *e = it->data;
66 gboolean *en = &e->data.normal.enabled; /* save some typing */
67 ObClient *c = frame->client;
69 if (e->type == OB_MENU_ENTRY_TYPE_NORMAL) {
72 *en = c->functions & OB_CLIENT_FUNC_ICONIFY;
75 *en = c->max_horz || c->max_vert;
78 *en = ((c->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
79 (!c->max_horz || !c->max_vert));
82 *en = c->functions & OB_CLIENT_FUNC_SHADE;
85 *en = c->functions & OB_CLIENT_FUNC_MOVE;
88 *en = c->functions & OB_CLIENT_FUNC_RESIZE;
91 *en = c->functions & OB_CLIENT_FUNC_CLOSE;
94 *en = c->functions & OB_CLIENT_FUNC_UNDECORATE;
101 return TRUE; /* show the menu */
104 static void client_menu_execute(ObMenuEntry *e, ObMenuFrame *f,
105 ObClient *c, guint state, gpointer data,
114 /* the client won't be on screen anymore so hide the menu */
115 menu_frame_hide_all();
116 f = NULL; /* and don't update */
118 client_iconify(c, TRUE, FALSE, FALSE);
121 client_maximize(c, FALSE, 0);
123 case CLIENT_MAXIMIZE:
124 client_maximize(c, TRUE, 0);
127 client_shade(c, !c->shaded);
129 case CLIENT_DECORATE:
130 client_set_undecorated(c, !c->undecorated);
133 /* this needs to grab the keyboard so hide the menu */
134 menu_frame_hide_all();
135 f = NULL; /* and don't update */
137 screen_pointer_pos(&x, &y);
138 moveresize_start(c, x, y, 0,
139 prop_atoms.net_wm_moveresize_move_keyboard);
142 /* this needs to grab the keyboard so hide the menu */
143 menu_frame_hide_all();
144 f = NULL; /* and don't update */
146 screen_pointer_pos(&x, &y);
147 moveresize_start(c, x, y, 0,
148 prop_atoms.net_wm_moveresize_size_keyboard);
154 g_assert_not_reached();
157 /* we have to ignore all queued enters because the menu has a grab, and the
158 so the cursor isnt considered inside the client's window */
159 event_ignore_all_queued_enters();
161 /* update the menu cuz stuff can have changed */
163 client_menu_update(f, NULL);
164 menu_frame_render(f);
168 static gboolean layer_menu_update(ObMenuFrame *frame, gpointer data)
170 ObMenu *menu = frame->menu;
173 if (frame->client == NULL || !client_normal(frame->client))
174 return FALSE; /* don't show the menu */
176 for (it = menu->entries; it; it = g_list_next(it)) {
177 ObMenuEntry *e = it->data;
178 gboolean *en = &e->data.normal.enabled; /* save some typing */
179 ObClient *c = frame->client;
181 if (e->type == OB_MENU_ENTRY_TYPE_NORMAL) {
184 *en = !c->above && (c->functions & OB_CLIENT_FUNC_ABOVE);
187 *en = c->above || c->below;
190 *en = !c->below && (c->functions & OB_CLIENT_FUNC_BELOW);
197 return TRUE; /* show the menu */
200 static void layer_menu_execute(ObMenuEntry *e, ObMenuFrame *f,
201 ObClient *c, guint state, gpointer data,
208 client_set_layer(c, 1);
211 client_set_layer(c, 0);
214 client_set_layer(c, -1);
217 g_assert_not_reached();
220 /* we have to ignore all queued enters because the menu has a grab, and the
221 so the cursor isnt considered inside the client's window */
222 event_ignore_all_queued_enters();
224 /* update the menu cuz stuff can have changed */
226 layer_menu_update(f, NULL);
227 menu_frame_render(f);
231 static gboolean send_to_menu_update(ObMenuFrame *frame, gpointer data)
233 ObMenu *menu = frame->menu;
237 menu_clear_entries(menu);
239 if (frame->client == NULL || !client_normal(frame->client))
240 return FALSE; /* don't show the menu */
242 for (i = 0; i <= screen_num_desktops; ++i) {
246 if (i >= screen_num_desktops) {
247 menu_add_separator(menu, -1, NULL);
250 name = _("All desktops");
253 name = screen_desktop_names[i];
256 e = menu_add_normal(menu, desk, name, NULL, FALSE);
258 if (desk == DESKTOP_ALL) {
259 e->data.normal.mask = ob_rr_theme->desk_mask;
260 e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
261 e->data.normal.mask_selected_color =
262 ob_rr_theme->menu_selected_color;
263 e->data.normal.mask_disabled_color =
264 ob_rr_theme->menu_disabled_color;
265 e->data.normal.mask_disabled_selected_color =
266 ob_rr_theme->menu_disabled_selected_color;
269 if (frame->client->desktop == desk)
270 e->data.normal.enabled = FALSE;
272 return TRUE; /* show the menu */
275 static void send_to_menu_execute(ObMenuEntry *e, ObMenuFrame *f,
276 ObClient *c, guint state, gpointer data,
281 client_set_desktop(c, e->id, FALSE);
282 /* the client won't even be on the screen anymore, so hide the menu */
284 menu_frame_hide_all();
287 static void client_menu_place(ObMenuFrame *frame, gint *x, gint *y,
288 gint button, gpointer data)
292 if (button == 0 && frame->client) {
293 *x = frame->client->frame->area.x;
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);
300 /* try above the titlebar */
301 *y = frame->client->frame->area.y + frame->client->frame->bwidth -
303 menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
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);
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;
325 menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
327 if (dx != 0 || dy != 0) {
328 /* try to the top right of the cursor */
330 myy = *y - frame->area.height;
331 menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
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);
339 if (dx != 0 || dy != 0) {
340 /* if didnt fit on either side so just use what it says */
343 menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
350 void client_menu_startup()
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);
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);
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);
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);
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;
383 menu_add_normal(menu, CLIENT_MOVE, _("&Move"), NULL, TRUE);
385 menu_add_normal(menu, CLIENT_RESIZE, _("Resi&ze"), NULL, TRUE);
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;
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;
403 menu_add_normal(menu, CLIENT_SHADE, _("&Roll up/down"), NULL, TRUE);
405 menu_add_normal(menu, CLIENT_DECORATE, _("Un/&Decorate"), NULL, TRUE);
407 menu_add_separator(menu, -1, NULL);
409 menu_add_submenu(menu, CLIENT_SEND_TO, SEND_TO_MENU_NAME);
411 menu_add_submenu(menu, CLIENT_LAYER, LAYER_MENU_NAME);
413 menu_add_separator(menu, -1, NULL);
415 e = menu_add_normal(menu, CLIENT_CLOSE, _("&Close"), NULL, TRUE);
416 e->data.normal.mask = ob_rr_theme->close_mask;
417 e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
418 e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
419 e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
420 e->data.normal.mask_disabled_selected_color =
421 ob_rr_theme->menu_disabled_selected_color;