]> icculus.org git repositories - dana/openbox.git/blob - openbox/client_menu.c
add a notifier for clients changing desktops. use it to update the send-to menu if...
[dana/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 "screen.h"
23 #include "client.h"
24 #include "openbox.h"
25 #include "frame.h"
26 #include "gettext.h"
27
28 #include <glib.h>
29
30 #define CLIENT_MENU_NAME  "client-menu"
31 #define SEND_TO_MENU_NAME "client-send-to-menu"
32 #define LAYER_MENU_NAME   "client-layer-menu"
33
34 enum {
35     LAYER_TOP,
36     LAYER_NORMAL,
37     LAYER_BOTTOM
38 };
39
40 enum {
41     CLIENT_SEND_TO,
42     CLIENT_LAYER,
43     CLIENT_ICONIFY,
44     CLIENT_RESTORE,
45     CLIENT_MAXIMIZE,
46     CLIENT_SHADE,
47     CLIENT_DECORATE,
48     CLIENT_MOVE,
49     CLIENT_RESIZE,
50     CLIENT_CLOSE
51 };
52
53 static gboolean client_update(ObMenuFrame *frame, gpointer data)
54 {
55     ObMenu *menu = frame->menu;
56     ObMenuEntry *e;
57     GList *it;
58
59     if (frame->client == NULL || !client_normal(frame->client))
60         return FALSE; /* don't show the menu */
61
62     for (it = menu->entries; it; it = g_list_next(it)) {
63         e = it->data;
64         if (e->type == OB_MENU_ENTRY_TYPE_NORMAL)
65             e->data.normal.enabled = TRUE;
66     }
67
68     e = menu_find_entry_id(menu, CLIENT_ICONIFY);
69     e->data.normal.enabled = frame->client->functions & OB_CLIENT_FUNC_ICONIFY;
70
71     e = menu_find_entry_id(menu, CLIENT_RESTORE);
72     e->data.normal.enabled =frame->client->max_horz || frame->client->max_vert;
73
74     e = menu_find_entry_id(menu, CLIENT_MAXIMIZE);
75     e->data.normal.enabled =
76         (frame->client->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
77         !frame->client->max_horz && !frame->client->max_vert;
78
79     e = menu_find_entry_id(menu, CLIENT_SHADE);
80     e->data.normal.enabled = frame->client->functions & OB_CLIENT_FUNC_SHADE;
81
82     e = menu_find_entry_id(menu, CLIENT_MOVE);
83     e->data.normal.enabled = frame->client->functions & OB_CLIENT_FUNC_MOVE;
84
85     e = menu_find_entry_id(menu, CLIENT_RESIZE);
86     e->data.normal.enabled = frame->client->functions & OB_CLIENT_FUNC_RESIZE;
87
88     e = menu_find_entry_id(menu, CLIENT_CLOSE);
89     e->data.normal.enabled = frame->client->functions & OB_CLIENT_FUNC_CLOSE;
90
91     e = menu_find_entry_id(menu, CLIENT_DECORATE);
92     e->data.normal.enabled = client_normal(frame->client);
93     return TRUE; /* show the menu */
94 }
95
96 static gboolean layer_update(ObMenuFrame *frame, gpointer data)
97 {
98     ObMenu *menu = frame->menu;
99     ObMenuEntry *e;
100     GList *it;
101
102     if (frame->client == NULL || !client_normal(frame->client))
103         return FALSE; /* don't show the menu */
104
105     for (it = menu->entries; it; it = g_list_next(it)) {
106         e = it->data;
107         if (e->type == OB_MENU_ENTRY_TYPE_NORMAL)
108             e->data.normal.enabled = TRUE;
109     }
110
111     e = menu_find_entry_id(menu, LAYER_TOP);
112     e->data.normal.enabled = !frame->client->above;
113
114     e = menu_find_entry_id(menu, LAYER_NORMAL);
115     e->data.normal.enabled = (frame->client->above || frame->client->below);
116
117     e = menu_find_entry_id(menu, LAYER_BOTTOM);
118     e->data.normal.enabled = !frame->client->below;
119     return TRUE; /* show the menu */
120 }
121
122 static gboolean send_to_update(ObMenuFrame *frame, gpointer data)
123 {
124     ObMenu *menu = frame->menu;
125     guint i;
126     GSList *acts;
127     ObAction *act;
128     ObMenuEntry *e;
129
130     menu_clear_entries(menu);
131
132     if (frame->client == NULL || !client_normal(frame->client))
133         return FALSE; /* don't show the menu */
134
135     for (i = 0; i <= screen_num_desktops; ++i) {
136         const gchar *name;
137         guint desk;
138
139         if (i >= screen_num_desktops) {
140             menu_add_separator(menu, -1, NULL);
141
142             desk = DESKTOP_ALL;
143             name = _("All desktops");
144         } else {
145             desk = i;
146             name = screen_desktop_names[i];
147         }
148
149         act = action_from_string("SendToDesktop",
150                                  OB_USER_ACTION_MENU_SELECTION);
151         act->data.sendto.desk = desk;
152         act->data.sendto.follow = FALSE;
153         acts = g_slist_prepend(NULL, act);
154         e = menu_add_normal(menu, desk, name, acts, FALSE);
155         if (desk == DESKTOP_ALL) {
156             e->data.normal.mask = ob_rr_theme->desk_mask;
157             e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
158             e->data.normal.mask_selected_color =
159                 ob_rr_theme->menu_selected_color;
160             e->data.normal.mask_disabled_color =
161                 ob_rr_theme->menu_disabled_color;
162             e->data.normal.mask_disabled_selected_color =
163                 ob_rr_theme->menu_disabled_selected_color;
164         }
165
166         if (frame->client->desktop == desk)
167             e->data.normal.enabled = FALSE;
168     }
169     return TRUE; /* show the menu */
170 }
171
172 static void desktop_change_callback(ObClient *c, gpointer data)
173 {
174     ObMenuFrame *frame = data;
175     if (c == frame->client) {
176         /* adding/removing entries while it's shown is not fun, so just hide
177          the menu and reshow it */
178         if (frame->parent) {
179             ObMenuEntryFrame *me = frame->parent_entry;
180             ObMenuFrame *parent = frame->parent;
181             menu_frame_select(parent, NULL, TRUE);
182             menu_frame_select(parent, me, TRUE);
183         } else
184             menu_frame_hide(frame);
185     }
186 }
187
188 static void show_callback(ObMenuFrame *frame, gpointer data)
189 {
190     client_add_desktop_notify(desktop_change_callback, frame);
191 }
192
193 static void hide_callback(ObMenuFrame *frame, gpointer data)
194 {
195     client_remove_desktop_notify(desktop_change_callback);
196 }
197
198 static void client_menu_place(ObMenuFrame *frame, gint *x, gint *y,
199                               gint button, gpointer data)
200 {
201     gint dx, dy;
202
203     if (button == 0 && frame->client) {
204         *x = frame->client->frame->area.x;
205
206         /* try below the titlebar */
207         *y = frame->client->frame->area.y + frame->client->frame->size.top -
208             frame->client->frame->bwidth;
209         menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
210         if (dy != 0) {
211             /* try above the titlebar */
212             *y = frame->client->frame->area.y + frame->client->frame->bwidth -
213                 frame->area.height;
214             menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
215         }
216         if (dy != 0) {
217             /* didnt fit either way, use move on screen's values */
218             *y = frame->client->frame->area.y + frame->client->frame->size.top;
219             menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
220         }
221
222         *x += dx;
223         *y += dy;
224     } else {
225         gint myx, myy;
226
227         myx = *x;
228         myy = *y;
229
230         /* try to the bottom right of the cursor */
231         menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
232         if (dx != 0 || dy != 0) {
233             /* try to the bottom left of the cursor */
234             myx = *x - frame->area.width;
235             myy = *y;
236             menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
237         }
238         if (dx != 0 || dy != 0) {
239             /* try to the top right of the cursor */
240             myx = *x;
241             myy = *y - frame->area.height;
242             menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
243         }
244         if (dx != 0 || dy != 0) {
245             /* try to the top left of the cursor */
246             myx = *x - frame->area.width;
247             myy = *y - frame->area.height;
248             menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
249         }
250         if (dx != 0 || dy != 0) {
251             /* if didnt fit on either side so just use what it says */
252             myx = *x;
253             myy = *y;
254             menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
255         }
256         *x = myx + dx;
257         *y = myy + dy;
258     }
259 }
260
261 void client_menu_startup()
262 {
263     GSList *acts;
264     ObMenu *menu;
265     ObMenuEntry *e;
266
267     menu = menu_new(LAYER_MENU_NAME, _("&Layer"), TRUE, NULL);
268     menu_show_all_shortcuts(menu, TRUE);
269     menu_set_update_func(menu, layer_update);
270
271     acts = g_slist_prepend(NULL, action_from_string
272                            ("SendToTopLayer", OB_USER_ACTION_MENU_SELECTION));
273     menu_add_normal(menu, LAYER_TOP, _("Always on &top"), acts, TRUE);
274
275     acts = g_slist_prepend(NULL, action_from_string
276                            ("SendToNormalLayer",
277                             OB_USER_ACTION_MENU_SELECTION));
278     menu_add_normal(menu, LAYER_NORMAL, _("&Normal"), acts, TRUE);
279
280     acts = g_slist_prepend(NULL, action_from_string
281                            ("SendToBottomLayer",
282                             OB_USER_ACTION_MENU_SELECTION));
283     menu_add_normal(menu, LAYER_BOTTOM, _("Always on &bottom"),acts, TRUE);
284
285
286     menu = menu_new(SEND_TO_MENU_NAME, _("&Send to desktop"), TRUE, NULL);
287     menu_set_update_func(menu, send_to_update);
288     menu_set_show_func(menu, show_callback);
289     menu_set_hide_func(menu, hide_callback);
290
291
292     menu = menu_new(CLIENT_MENU_NAME, _("Client menu"), TRUE, NULL);
293     menu_show_all_shortcuts(menu, TRUE);
294     menu_set_update_func(menu, client_update);
295     menu_set_place_func(menu, client_menu_place);
296
297     acts = g_slist_prepend(NULL, action_from_string
298                            ("ToggleMaximizeFull",
299                             OB_USER_ACTION_MENU_SELECTION));
300     e = menu_add_normal(menu, CLIENT_RESTORE, _("R&estore"), acts, TRUE);
301     e->data.normal.mask = ob_rr_theme->max_toggled_mask; 
302     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
303     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
304     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
305     e->data.normal.mask_disabled_selected_color =
306         ob_rr_theme->menu_disabled_selected_color;
307
308     acts = g_slist_prepend(NULL, action_from_string
309                            ("Move", OB_USER_ACTION_MENU_SELECTION));
310     menu_add_normal(menu, CLIENT_MOVE, _("&Move"), acts, TRUE);
311
312     acts = g_slist_prepend(NULL, action_from_string
313                            ("Resize", OB_USER_ACTION_MENU_SELECTION));
314     menu_add_normal(menu, CLIENT_RESIZE, _("Resi&ze"), acts, TRUE);
315
316     acts = g_slist_prepend(NULL, action_from_string
317                            ("Iconify", OB_USER_ACTION_MENU_SELECTION));
318     e = menu_add_normal(menu, CLIENT_ICONIFY, _("Ico&nify"), acts, TRUE);
319     e->data.normal.mask = ob_rr_theme->iconify_mask;
320     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
321     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
322     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
323     e->data.normal.mask_disabled_selected_color =
324         ob_rr_theme->menu_disabled_selected_color;
325
326     acts = g_slist_prepend(NULL, action_from_string
327                            ("ToggleMaximizeFull",
328                             OB_USER_ACTION_MENU_SELECTION));
329     e = menu_add_normal(menu, CLIENT_MAXIMIZE, _("Ma&ximize"), acts, TRUE);
330     e->data.normal.mask = ob_rr_theme->max_mask; 
331     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
332     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
333     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
334     e->data.normal.mask_disabled_selected_color =
335         ob_rr_theme->menu_disabled_selected_color;
336
337     acts = g_slist_prepend(NULL, action_from_string
338                            ("ToggleShade", OB_USER_ACTION_MENU_SELECTION));
339     e = menu_add_normal(menu, CLIENT_SHADE, _("&Roll up/down"), acts, TRUE);
340     e->data.normal.mask = ob_rr_theme->shade_mask;
341     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
342     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
343     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
344     e->data.normal.mask_disabled_selected_color =
345         ob_rr_theme->menu_disabled_selected_color;
346
347     acts = g_slist_prepend(NULL, action_from_string
348                            ("ToggleDecorations",
349                             OB_USER_ACTION_MENU_SELECTION));
350     menu_add_normal(menu, CLIENT_DECORATE, _("Un/&Decorate"), acts, TRUE);
351
352     menu_add_separator(menu, -1, NULL);
353
354     menu_add_submenu(menu, CLIENT_SEND_TO, SEND_TO_MENU_NAME);
355
356     menu_add_submenu(menu, CLIENT_LAYER, LAYER_MENU_NAME);
357
358     menu_add_separator(menu, -1, NULL);
359
360     acts = g_slist_prepend(NULL, action_from_string
361                            ("Close", OB_USER_ACTION_MENU_SELECTION));
362     e = menu_add_normal(menu, CLIENT_CLOSE, _("&Close"), acts, TRUE);
363     e->data.normal.mask = ob_rr_theme->close_mask;
364     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
365     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
366     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
367     e->data.normal.mask_disabled_selected_color =
368         ob_rr_theme->menu_disabled_selected_color;
369 }