Add "active" and "primary" options to the <monitor> placement option for per-app...
[openbox.git] / openbox / client_menu.c
blobc6cdd63557d56a2506fb789ce1e20538d12d8e42
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.
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 "frame.h"
28 #include "moveresize.h"
29 #include "event.h"
30 #include "gettext.h"
31 #include "obt/prop.h"
33 #include <glib.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"
39 enum {
40 LAYER_TOP = 1,
41 LAYER_NORMAL = 0,
42 LAYER_BOTTOM = -1
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
58 static void set_icon_color(ObMenuEntry *e)
60 e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
61 e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
62 e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
63 e->data.normal.mask_disabled_selected_color =
64 ob_rr_theme->menu_disabled_selected_color;
67 static gboolean client_menu_update(ObMenuFrame *frame, gpointer data)
69 ObMenu *menu = frame->menu;
70 GList *it;
72 if (frame->client == NULL || !client_normal(frame->client))
73 return FALSE; /* don't show the menu */
75 for (it = menu->entries; it; it = g_list_next(it)) {
76 ObMenuEntry *e = it->data;
77 gboolean *en = &e->data.normal.enabled; /* save some typing */
78 ObClient *c = frame->client;
80 if (e->type == OB_MENU_ENTRY_TYPE_NORMAL) {
81 switch (e->id) {
82 case CLIENT_ICONIFY:
83 *en = c->functions & OB_CLIENT_FUNC_ICONIFY;
84 break;
85 case CLIENT_RESTORE:
86 *en = c->max_horz || c->max_vert;
87 break;
88 case CLIENT_MAXIMIZE:
89 *en = ((c->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
90 (!c->max_horz || !c->max_vert));
91 break;
92 case CLIENT_SHADE:
93 *en = c->functions & OB_CLIENT_FUNC_SHADE;
94 break;
95 case CLIENT_MOVE:
96 *en = c->functions & OB_CLIENT_FUNC_MOVE;
97 break;
98 case CLIENT_RESIZE:
99 *en = c->functions & OB_CLIENT_FUNC_RESIZE;
100 break;
101 case CLIENT_CLOSE:
102 *en = c->functions & OB_CLIENT_FUNC_CLOSE;
103 break;
104 case CLIENT_DECORATE:
105 *en = c->functions & OB_CLIENT_FUNC_UNDECORATE;
106 break;
107 default:
108 *en = TRUE;
112 return TRUE; /* show the menu */
115 static void client_menu_execute(ObMenuEntry *e, ObMenuFrame *f,
116 ObClient *c, guint state, gpointer data)
118 gint x, y;
119 gulong ignore_start;
121 if (!c)
122 return;
124 if (!config_focus_under_mouse)
125 ignore_start = event_start_ignore_all_enters();
127 switch (e->id) {
128 case CLIENT_ICONIFY:
129 /* the client won't be on screen anymore so hide the menu */
130 menu_frame_hide_all();
131 f = NULL; /* and don't update */
133 client_iconify(c, TRUE, FALSE, FALSE);
134 break;
135 case CLIENT_RESTORE:
136 client_maximize(c, FALSE, 0);
137 break;
138 case CLIENT_MAXIMIZE:
139 client_maximize(c, TRUE, 0);
140 break;
141 case CLIENT_SHADE:
142 client_shade(c, !c->shaded);
143 break;
144 case CLIENT_DECORATE:
145 client_set_undecorated(c, !c->undecorated);
146 break;
147 case CLIENT_MOVE:
148 /* this needs to grab the keyboard so hide the menu */
149 menu_frame_hide_all();
150 f = NULL; /* and don't update */
152 screen_pointer_pos(&x, &y);
153 moveresize_start(c, x, y, 0,
154 OBT_PROP_ATOM(NET_WM_MOVERESIZE_MOVE_KEYBOARD));
155 break;
156 case CLIENT_RESIZE:
157 /* this needs to grab the keyboard so hide the menu */
158 menu_frame_hide_all();
159 f = NULL; /* and don't update */
161 screen_pointer_pos(&x, &y);
162 moveresize_start(c, x, y, 0,
163 OBT_PROP_ATOM(NET_WM_MOVERESIZE_SIZE_KEYBOARD));
164 break;
165 case CLIENT_CLOSE:
166 client_close(c);
167 break;
168 default:
169 g_assert_not_reached();
172 if (!config_focus_under_mouse)
173 event_end_ignore_all_enters(ignore_start);
175 /* update the menu cuz stuff can have changed */
176 if (f) {
177 client_menu_update(f, NULL);
178 menu_frame_render(f);
182 static gboolean layer_menu_update(ObMenuFrame *frame, gpointer data)
184 ObMenu *menu = frame->menu;
185 GList *it;
187 if (frame->client == NULL || !client_normal(frame->client))
188 return FALSE; /* don't show the menu */
190 for (it = menu->entries; it; it = g_list_next(it)) {
191 ObMenuEntry *e = it->data;
192 gboolean *en = &e->data.normal.enabled; /* save some typing */
193 ObClient *c = frame->client;
195 if (e->type == OB_MENU_ENTRY_TYPE_NORMAL) {
196 switch (e->id) {
197 case LAYER_TOP:
198 *en = !c->above && (c->functions & OB_CLIENT_FUNC_ABOVE);
199 break;
200 case LAYER_NORMAL:
201 *en = c->above || c->below;
202 break;
203 case LAYER_BOTTOM:
204 *en = !c->below && (c->functions & OB_CLIENT_FUNC_BELOW);
205 break;
206 default:
207 *en = TRUE;
211 return TRUE; /* show the menu */
214 static void layer_menu_execute(ObMenuEntry *e, ObMenuFrame *f,
215 ObClient *c, guint state, gpointer data)
217 gulong ignore_start;
219 g_assert(c);
221 if (!config_focus_under_mouse)
222 ignore_start = event_start_ignore_all_enters();
224 client_set_layer(c, e->id);
226 if (!config_focus_under_mouse)
227 event_end_ignore_all_enters(ignore_start);
229 /* update the menu cuz stuff can have changed */
230 if (f) {
231 layer_menu_update(f, NULL);
232 menu_frame_render(f);
236 static gboolean send_to_menu_update(ObMenuFrame *frame, gpointer data)
238 ObMenu *menu = frame->menu;
239 ObClient *c = frame->client;
240 guint i;
241 ObMenuEntry *e;
242 GList *it;
244 if (c == NULL || !client_normal(c))
245 return FALSE; /* don't show the menu */
247 if (!data)
248 menu_clear_entries(menu);
250 if (!menu->entries) {
251 for (i = 0; i <= screen_num_desktops; ++i) {
252 const gchar *name;
253 guint desk;
255 if (i == screen_num_desktops) {
256 menu_add_separator(menu, -1, NULL);
258 desk = DESKTOP_ALL;
259 name = _("All desktops");
260 } else {
261 desk = i;
262 name = screen_desktop_names[i];
265 e = menu_add_normal(menu, desk, name, NULL, FALSE);
266 e->id = desk;
270 for (it = menu->entries; it; it = g_list_next(it)) {
271 ObMenuEntry *e = it->data;
272 guint desk = e->id;
274 e->data.normal.enabled = c->desktop != desk;
276 if ((desk == DESKTOP_ALL && c->desktop != DESKTOP_ALL) ||
277 (c->desktop == DESKTOP_ALL && desk == screen_desktop))
279 e->data.normal.mask = ob_rr_theme->btn_desk->mask;
280 set_icon_color(e);
281 } else
282 e->data.normal.mask = NULL;
285 return TRUE; /* show the menu */
288 static void send_to_menu_execute(ObMenuEntry *e, ObMenuFrame *f,
289 ObClient *c, guint state, gpointer data)
291 g_assert(c);
293 client_set_desktop(c, e->id, FALSE, FALSE);
294 if (f && c->desktop != screen_desktop && c->desktop != DESKTOP_ALL)
295 /* the client won't even be on the screen anymore, so hide the menu */
296 menu_frame_hide_all();
297 else if (f) {
298 send_to_menu_update(f, (gpointer)1);
299 menu_frame_render(f);
303 static void client_menu_place(ObMenuFrame *frame, gint *x, gint *y,
304 gboolean mouse, gpointer data)
306 gint dx, dy;
308 if (!mouse && frame->client) {
309 *x = frame->client->frame->area.x;
311 /* try below the titlebar */
312 *y = frame->client->frame->area.y + frame->client->frame->size.top -
313 frame->client->frame->bwidth;
314 menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
315 if (dy != 0) {
316 /* try above the titlebar */
317 *y = frame->client->frame->area.y + frame->client->frame->bwidth -
318 frame->area.height;
319 menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
321 if (dy != 0) {
322 /* didnt fit either way, use move on screen's values */
323 *y = frame->client->frame->area.y + frame->client->frame->size.top;
324 menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
327 *x += dx;
328 *y += dy;
329 } else {
330 gint myx, myy;
332 myx = *x;
333 myy = *y;
335 /* try to the bottom right of the cursor */
336 menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
337 if (dx != 0 || dy != 0) {
338 /* try to the bottom left of the cursor */
339 myx = *x - frame->area.width;
340 myy = *y;
341 menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
343 if (dx != 0 || dy != 0) {
344 /* try to the top right of the cursor */
345 myx = *x;
346 myy = *y - frame->area.height;
347 menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
349 if (dx != 0 || dy != 0) {
350 /* try to the top left of the cursor */
351 myx = *x - frame->area.width;
352 myy = *y - frame->area.height;
353 menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
355 if (dx != 0 || dy != 0) {
356 /* if didnt fit on either side so just use what it says */
357 myx = *x;
358 myy = *y;
359 menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
361 *x = myx + dx;
362 *y = myy + dy;
366 void client_menu_startup(void)
368 ObMenu *menu;
369 ObMenuEntry *e;
371 menu = menu_new(LAYER_MENU_NAME, _("_Layer"), TRUE, NULL);
372 menu_show_all_shortcuts(menu, TRUE);
373 menu_set_update_func(menu, layer_menu_update);
374 menu_set_execute_func(menu, layer_menu_execute);
376 menu_add_normal(menu, LAYER_TOP, _("Always on _top"), NULL, TRUE);
377 menu_add_normal(menu, LAYER_NORMAL, _("_Normal"), NULL, TRUE);
378 menu_add_normal(menu, LAYER_BOTTOM, _("Always on _bottom"),NULL, TRUE);
380 menu = menu_new(SEND_TO_MENU_NAME, _("_Send to desktop"), TRUE, NULL);
381 menu_set_update_func(menu, send_to_menu_update);
382 menu_set_execute_func(menu, send_to_menu_execute);
384 menu = menu_new(CLIENT_MENU_NAME, _("Client menu"), TRUE, NULL);
385 menu_show_all_shortcuts(menu, TRUE);
386 menu_set_update_func(menu, client_menu_update);
387 menu_set_place_func(menu, client_menu_place);
388 menu_set_execute_func(menu, client_menu_execute);
390 menu_add_submenu(menu, CLIENT_SEND_TO, SEND_TO_MENU_NAME);
392 menu_add_submenu(menu, CLIENT_LAYER, LAYER_MENU_NAME);
394 e = menu_add_normal(menu, CLIENT_RESTORE, _("R_estore"), NULL, TRUE);
395 e->data.normal.mask = ob_rr_theme->btn_max->toggled_mask;
396 set_icon_color(e);
398 menu_add_normal(menu, CLIENT_MOVE, _("_Move"), NULL, TRUE);
400 menu_add_normal(menu, CLIENT_RESIZE, _("Resi_ze"), NULL, TRUE);
402 e = menu_add_normal(menu, CLIENT_ICONIFY, _("Ico_nify"), NULL, TRUE);
403 e->data.normal.mask = ob_rr_theme->btn_iconify->mask;
404 set_icon_color(e);
406 e = menu_add_normal(menu, CLIENT_MAXIMIZE, _("Ma_ximize"), NULL, TRUE);
407 e->data.normal.mask = ob_rr_theme->btn_max->mask;
408 set_icon_color(e);
410 e = menu_add_normal(menu, CLIENT_SHADE, _("_Roll up/down"), NULL, TRUE);
411 e->data.normal.mask = ob_rr_theme->btn_shade->mask;
412 set_icon_color(e);
414 menu_add_normal(menu, CLIENT_DECORATE, _("Un/_Decorate"), NULL, TRUE);
416 menu_add_separator(menu, -1, NULL);
418 e = menu_add_normal(menu, CLIENT_CLOSE, _("_Close"), NULL, TRUE);
419 e->data.normal.mask = ob_rr_theme->btn_close->mask;
420 set_icon_color(e);