]> icculus.org git repositories - dana/openbox.git/blob - openbox/client_menu.c
typos
[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_MAXIMIZE,
45     CLIENT_RAISE,
46     CLIENT_LOWER,
47     CLIENT_SHADE,
48     CLIENT_DECORATE,
49     CLIENT_MOVE,
50     CLIENT_RESIZE,
51     CLIENT_CLOSE
52 };
53
54 static void client_update(ObMenuFrame *frame, gpointer data)
55 {
56     ObMenu *menu = frame->menu;
57     ObMenuEntry *e;
58     GList *it;
59
60     for (it = menu->entries; it; it = g_list_next(it)) {
61         e = it->data;
62         if (e->type == OB_MENU_ENTRY_TYPE_NORMAL)
63             e->data.normal.enabled = !!frame->client;
64     }
65
66     if (!frame->client)
67         return;
68
69     e = menu_find_entry_id(menu, CLIENT_ICONIFY);
70     e->data.normal.enabled = frame->client->functions & OB_CLIENT_FUNC_ICONIFY;
71
72     e = menu_find_entry_id(menu, CLIENT_MAXIMIZE);
73     menu_entry_set_label(e,
74                          (frame->client->max_vert || frame->client->max_horz ?
75                           _("Restor&e") : _("Maximiz&e")), TRUE);
76     e->data.normal.enabled =frame->client->functions & OB_CLIENT_FUNC_MAXIMIZE;
77
78     e = menu_find_entry_id(menu, CLIENT_SHADE);
79     menu_entry_set_label(e, (frame->client->shaded ?
80                              _("&Roll down") : _("&Roll up")), TRUE);
81     e->data.normal.enabled = frame->client->functions & OB_CLIENT_FUNC_SHADE;
82
83     e = menu_find_entry_id(menu, CLIENT_MOVE);
84     e->data.normal.enabled = frame->client->functions & OB_CLIENT_FUNC_MOVE;
85
86     e = menu_find_entry_id(menu, CLIENT_RESIZE);
87     e->data.normal.enabled = frame->client->functions & OB_CLIENT_FUNC_RESIZE;
88
89     e = menu_find_entry_id(menu, CLIENT_CLOSE);
90     e->data.normal.enabled = frame->client->functions & OB_CLIENT_FUNC_CLOSE;
91
92     e = menu_find_entry_id(menu, CLIENT_DECORATE);
93     e->data.normal.enabled = client_normal(frame->client);
94 }
95
96 static void layer_update(ObMenuFrame *frame, gpointer data)
97 {
98     ObMenu *menu = frame->menu;
99     ObMenuEntry *e;
100     GList *it;
101
102     for (it = menu->entries; it; it = g_list_next(it)) {
103         e = it->data;
104         if (e->type == OB_MENU_ENTRY_TYPE_NORMAL)
105             e->data.normal.enabled = !!frame->client;
106     }
107
108     if (!frame->client)
109         return;
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 }
120
121 static void send_to_update(ObMenuFrame *frame, gpointer data)
122 {
123     ObMenu *menu = frame->menu;
124     guint i;
125     GSList *acts;
126     ObAction *act;
127     ObMenuEntry *e;;
128
129     menu_clear_entries(menu);
130
131     if (!frame->client)
132         return;
133
134     for (i = 0; i <= screen_num_desktops; ++i) {
135         const gchar *name;
136         guint desk;
137
138         if (i >= screen_num_desktops) {
139             menu_add_separator(menu, -1, NULL);
140
141             desk = DESKTOP_ALL;
142             name = _("All desktops");
143         } else {
144             desk = i;
145             name = screen_desktop_names[i];
146         }
147
148         act = action_from_string("SendToDesktop",
149                                  OB_USER_ACTION_MENU_SELECTION);
150         act->data.sendto.desk = desk;
151         act->data.sendto.follow = FALSE;
152         acts = g_slist_prepend(NULL, act);
153         e = menu_add_normal(menu, desk, name, acts, FALSE);
154
155         if (frame->client->desktop == desk)
156             e->data.normal.enabled = FALSE;
157     }
158 }
159
160 static void client_menu_place(ObMenuFrame *frame, gint *x, gint *y,
161                               gint button, gpointer data)
162 {
163     gint dx, dy;
164
165     if (button == 0 && frame->client) {
166         *x = frame->client->frame->area.x;
167
168         /* try below the titlebar */
169         *y = frame->client->frame->area.y + frame->client->frame->size.top -
170             frame->client->frame->bwidth;
171         menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
172         if (dy != 0) {
173             /* try above the titlebar */
174             *y = frame->client->frame->area.y + frame->client->frame->bwidth -
175                 frame->area.height;
176             menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
177         }
178         if (dy != 0) {
179             /* didnt fit either way, use move on screen's values */
180             *y = frame->client->frame->area.y + frame->client->frame->size.top;
181             menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
182         }
183
184         *x += dx;
185         *y += dy;
186     } else {
187         gint myx, myy;
188
189         myx = *x;
190         myy = *y;
191
192         /* try to the bottom right of the cursor */
193         menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
194         if (dx != 0 || dy != 0) {
195             /* try to the bottom left of the cursor */
196             myx = *x - frame->area.width;
197             myy = *y;
198             menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
199         }
200         if (dx != 0 || dy != 0) {
201             /* try to the top right of the cursor */
202             myx = *x;
203             myy = *y - frame->area.height;
204             menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
205         }
206         if (dx != 0 || dy != 0) {
207             /* try to the top left of the cursor */
208             myx = *x - frame->area.width;
209             myy = *y - frame->area.height;
210             menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
211         }
212         if (dx != 0 || dy != 0) {
213             /* if didnt fit on either side so just use what it says */
214             myx = *x;
215             myy = *y;
216             menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
217         }
218         *x = myx + dx;
219         *y = myy + dy;
220     }
221 }
222
223 void client_menu_startup()
224 {
225     GSList *acts;
226     ObMenu *menu;
227     ObMenuEntry *e;
228
229     menu = menu_new(LAYER_MENU_NAME, _("&Layer"), TRUE, NULL);
230     menu_show_all_shortcuts(menu, TRUE);
231     menu_set_update_func(menu, layer_update);
232
233     acts = g_slist_prepend(NULL, action_from_string
234                            ("SendToTopLayer", OB_USER_ACTION_MENU_SELECTION));
235     menu_add_normal(menu, LAYER_TOP, _("Always on &top"), acts, TRUE);
236
237     acts = g_slist_prepend(NULL, action_from_string
238                            ("SendToNormalLayer",
239                             OB_USER_ACTION_MENU_SELECTION));
240     menu_add_normal(menu, LAYER_NORMAL, _("&Normal"), acts, TRUE);
241
242     acts = g_slist_prepend(NULL, action_from_string
243                            ("SendToBottomLayer",
244                             OB_USER_ACTION_MENU_SELECTION));
245     menu_add_normal(menu, LAYER_BOTTOM, _("Always on &bottom"),acts, TRUE);
246
247
248     menu = menu_new(SEND_TO_MENU_NAME, _("&Send to desktop"), TRUE, NULL);
249     menu_set_update_func(menu, send_to_update);
250
251
252     menu = menu_new(CLIENT_MENU_NAME, _("Client menu"), TRUE, NULL);
253     menu_show_all_shortcuts(menu, TRUE);
254     menu_set_update_func(menu, client_update);
255     menu_set_place_func(menu, client_menu_place);
256
257     menu_add_submenu(menu, CLIENT_SEND_TO, SEND_TO_MENU_NAME);
258
259     menu_add_submenu(menu, CLIENT_LAYER, LAYER_MENU_NAME);
260
261     acts = g_slist_prepend(NULL, action_from_string
262                            ("Iconify", OB_USER_ACTION_MENU_SELECTION));
263     e = menu_add_normal(menu, CLIENT_ICONIFY, _("Ico&nify"), acts, TRUE);
264     e->data.normal.mask = ob_rr_theme->iconify_mask;
265     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
266     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
267     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
268
269     acts = g_slist_prepend(NULL, action_from_string
270                            ("ToggleMaximizeFull",
271                             OB_USER_ACTION_MENU_SELECTION));
272     e = menu_add_normal(menu, CLIENT_MAXIMIZE, "MAXIMIZE", acts, TRUE);
273     e->data.normal.mask = ob_rr_theme->max_mask; 
274     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
275     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
276     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
277
278     acts = g_slist_prepend(NULL, action_from_string
279                            ("Raise", OB_USER_ACTION_MENU_SELECTION));
280     menu_add_normal(menu, CLIENT_RAISE, _("Raise to &top"), acts, TRUE);
281
282     acts = g_slist_prepend(NULL, action_from_string
283                            ("Lower", OB_USER_ACTION_MENU_SELECTION));
284     menu_add_normal(menu, CLIENT_LOWER, _("Lower to &bottom"),acts, TRUE);
285
286     acts = g_slist_prepend(NULL, action_from_string
287                            ("ToggleShade", OB_USER_ACTION_MENU_SELECTION));
288     e = menu_add_normal(menu, CLIENT_SHADE, "SHADE", acts, TRUE);
289     e->data.normal.mask = ob_rr_theme->shade_mask;
290     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
291     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
292     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
293
294     acts = g_slist_prepend(NULL, action_from_string
295                            ("ToggleDecorations",
296                             OB_USER_ACTION_MENU_SELECTION));
297     menu_add_normal(menu, CLIENT_DECORATE, _("&Decorate"), acts, TRUE);
298
299     menu_add_separator(menu, -1, NULL);
300
301     acts = g_slist_prepend(NULL, action_from_string
302                            ("Move", OB_USER_ACTION_MENU_SELECTION));
303     menu_add_normal(menu, CLIENT_MOVE, _("&Move"), acts, TRUE);
304
305     acts = g_slist_prepend(NULL, action_from_string
306                            ("Resize", OB_USER_ACTION_MENU_SELECTION));
307     menu_add_normal(menu, CLIENT_RESIZE, _("Resi&ze"), acts, TRUE);
308
309     menu_add_separator(menu, -1, NULL);
310
311     acts = g_slist_prepend(NULL, action_from_string
312                            ("Close", OB_USER_ACTION_MENU_SELECTION));
313     e = menu_add_normal(menu, CLIENT_CLOSE, _("&Close"), acts, TRUE);
314     e->data.normal.mask = ob_rr_theme->close_mask;
315     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
316     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
317     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
318 }