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