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"
25 #include "client_menu.h"
28 #include "moveresize.h"
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"
58 static gboolean client_menu_update(ObMenuFrame *frame, gpointer data)
60 ObMenu *menu = frame->menu;
63 if (frame->client == NULL || !client_normal(frame->client))
64 return FALSE; /* don't show the menu */
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;
71 if (e->type == OB_MENU_ENTRY_TYPE_NORMAL) {
74 *en = c->functions & OB_CLIENT_FUNC_ICONIFY;
77 *en = c->max_horz || c->max_vert;
80 *en = ((c->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
81 (!c->max_horz || !c->max_vert));
84 *en = c->functions & OB_CLIENT_FUNC_SHADE;
87 *en = c->functions & OB_CLIENT_FUNC_MOVE;
90 *en = c->functions & OB_CLIENT_FUNC_RESIZE;
93 *en = c->functions & OB_CLIENT_FUNC_CLOSE;
96 *en = c->functions & OB_CLIENT_FUNC_UNDECORATE;
103 return TRUE; /* show the menu */
106 static void client_menu_execute(ObMenuEntry *e, ObMenuFrame *f,
107 ObClient *c, guint state, gpointer data)
114 if (!config_focus_under_mouse)
115 ignore_start = event_start_ignore_all_enters();
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 */
123 client_iconify(c, TRUE, FALSE, FALSE);
126 client_maximize(c, FALSE, 0);
128 case CLIENT_MAXIMIZE:
129 client_maximize(c, TRUE, 0);
132 client_shade(c, !c->shaded);
134 case CLIENT_DECORATE:
135 client_set_undecorated(c, !c->undecorated);
138 /* this needs to grab the keyboard so hide the menu */
139 menu_frame_hide_all();
140 f = NULL; /* and don't update */
142 screen_pointer_pos(&x, &y);
143 moveresize_start(c, x, y, 0,
144 prop_atoms.net_wm_moveresize_move_keyboard);
147 /* this needs to grab the keyboard so hide the menu */
148 menu_frame_hide_all();
149 f = NULL; /* and don't update */
151 screen_pointer_pos(&x, &y);
152 moveresize_start(c, x, y, 0,
153 prop_atoms.net_wm_moveresize_size_keyboard);
159 g_assert_not_reached();
162 if (!config_focus_under_mouse)
163 event_end_ignore_all_enters(ignore_start);
165 /* update the menu cuz stuff can have changed */
167 client_menu_update(f, NULL);
168 menu_frame_render(f);
172 static gboolean layer_menu_update(ObMenuFrame *frame, gpointer data)
174 ObMenu *menu = frame->menu;
177 if (frame->client == NULL || !client_normal(frame->client))
178 return FALSE; /* don't show the menu */
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;
185 if (e->type == OB_MENU_ENTRY_TYPE_NORMAL) {
188 *en = !c->above && (c->functions & OB_CLIENT_FUNC_ABOVE);
191 *en = c->above || c->below;
194 *en = !c->below && (c->functions & OB_CLIENT_FUNC_BELOW);
201 return TRUE; /* show the menu */
204 static void layer_menu_execute(ObMenuEntry *e, ObMenuFrame *f,
205 ObClient *c, guint state, gpointer data)
211 if (!config_focus_under_mouse)
212 ignore_start = event_start_ignore_all_enters();
216 client_set_layer(c, 1);
219 client_set_layer(c, 0);
222 client_set_layer(c, -1);
225 g_assert_not_reached();
228 if (!config_focus_under_mouse)
229 event_end_ignore_all_enters(ignore_start);
231 /* update the menu cuz stuff can have changed */
233 layer_menu_update(f, NULL);
234 menu_frame_render(f);
238 static gboolean send_to_menu_update(ObMenuFrame *frame, gpointer data)
240 ObMenu *menu = frame->menu;
244 menu_clear_entries(menu);
246 if (frame->client == NULL || !client_normal(frame->client))
247 return FALSE; /* don't show the menu */
249 for (i = 0; i <= screen_num_desktops; ++i) {
253 if (i >= screen_num_desktops) {
254 menu_add_separator(menu, -1, NULL);
257 name = _("All desktops");
260 name = screen_desktop_names[i];
263 e = menu_add_normal(menu, desk, name, NULL, FALSE);
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;
276 if (frame->client->desktop == desk)
277 e->data.normal.enabled = FALSE;
279 return TRUE; /* show the menu */
282 static void send_to_menu_execute(ObMenuEntry *e, ObMenuFrame *f,
283 ObClient *c, guint state, gpointer data)
287 client_set_desktop(c, e->id, FALSE, FALSE);
288 /* the client won't even be on the screen anymore, so hide the menu */
290 menu_frame_hide_all();
293 static void client_menu_place(ObMenuFrame *frame, gint *x, gint *y,
294 gboolean mouse, gpointer data)
298 if (!mouse && frame->client) {
299 *x = frame->client->frame->area.x;
301 /* try below the titlebar */
302 *y = frame->client->frame->area.y + frame->client->frame->size.top -
303 frame->client->frame->bwidth;
304 menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
306 /* try above the titlebar */
307 *y = frame->client->frame->area.y + frame->client->frame->bwidth -
309 menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
312 /* didnt fit either way, use move on screen's values */
313 *y = frame->client->frame->area.y + frame->client->frame->size.top;
314 menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
325 /* try to the bottom right of the cursor */
326 menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
327 if (dx != 0 || dy != 0) {
328 /* try to the bottom left of the cursor */
329 myx = *x - frame->area.width;
331 menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
333 if (dx != 0 || dy != 0) {
334 /* try to the top right of the cursor */
336 myy = *y - frame->area.height;
337 menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
339 if (dx != 0 || dy != 0) {
340 /* try to the top left of the cursor */
341 myx = *x - frame->area.width;
342 myy = *y - frame->area.height;
343 menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
345 if (dx != 0 || dy != 0) {
346 /* if didnt fit on either side so just use what it says */
349 menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
356 void client_menu_startup(void)
361 menu = menu_new(LAYER_MENU_NAME, _("_Layer"), TRUE, NULL);
362 menu_show_all_shortcuts(menu, TRUE);
363 menu_set_update_func(menu, layer_menu_update);
364 menu_set_execute_func(menu, layer_menu_execute);
366 menu_add_normal(menu, LAYER_TOP, _("Always on _top"), NULL, TRUE);
367 menu_add_normal(menu, LAYER_NORMAL, _("_Normal"), NULL, TRUE);
368 menu_add_normal(menu, LAYER_BOTTOM, _("Always on _bottom"),NULL, TRUE);
371 menu = menu_new(SEND_TO_MENU_NAME, _("_Send to desktop"), TRUE, NULL);
372 menu_set_update_func(menu, send_to_menu_update);
373 menu_set_execute_func(menu, send_to_menu_execute);
375 menu = menu_new(CLIENT_MENU_NAME, _("Client menu"), TRUE, NULL);
376 menu_show_all_shortcuts(menu, TRUE);
377 menu_set_update_func(menu, client_menu_update);
378 menu_set_place_func(menu, client_menu_place);
379 menu_set_execute_func(menu, client_menu_execute);
381 menu_add_submenu(menu, CLIENT_SEND_TO, SEND_TO_MENU_NAME);
383 menu_add_submenu(menu, CLIENT_LAYER, LAYER_MENU_NAME);
385 e = menu_add_normal(menu, CLIENT_RESTORE, _("R_estore"), NULL, TRUE);
386 e->data.normal.mask = ob_rr_theme->max_toggled_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;
393 menu_add_normal(menu, CLIENT_MOVE, _("_Move"), NULL, TRUE);
395 menu_add_normal(menu, CLIENT_RESIZE, _("Resi_ze"), NULL, TRUE);
397 e = menu_add_normal(menu, CLIENT_ICONIFY, _("Ico_nify"), NULL, TRUE);
398 e->data.normal.mask = ob_rr_theme->iconify_mask;
399 e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
400 e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
401 e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
402 e->data.normal.mask_disabled_selected_color =
403 ob_rr_theme->menu_disabled_selected_color;
405 e = menu_add_normal(menu, CLIENT_MAXIMIZE, _("Ma_ximize"), NULL, TRUE);
406 e->data.normal.mask = ob_rr_theme->max_mask;
407 e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
408 e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
409 e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
410 e->data.normal.mask_disabled_selected_color =
411 ob_rr_theme->menu_disabled_selected_color;
413 menu_add_normal(menu, CLIENT_SHADE, _("_Roll up/down"), NULL, TRUE);
415 menu_add_normal(menu, CLIENT_DECORATE, _("Un/_Decorate"), NULL, TRUE);
417 menu_add_separator(menu, -1, NULL);
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;