don't use it actually. it's confusing.
[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 "moveresize.h"
27 #include "prop.h"
28 #include "gettext.h"
29
30 #include <glib.h>
31
32 #define CLIENT_MENU_NAME  "client-menu"
33 #define SEND_TO_MENU_NAME "client-send-to-menu"
34 #define LAYER_MENU_NAME   "client-layer-menu"
35
36 enum {
37     LAYER_TOP,
38     LAYER_NORMAL,
39     LAYER_BOTTOM
40 };
41
42 enum {
43     CLIENT_SEND_TO,
44     CLIENT_LAYER,
45     CLIENT_ICONIFY,
46     CLIENT_RESTORE,
47     CLIENT_MAXIMIZE,
48     CLIENT_SHADE,
49     CLIENT_DECORATE,
50     CLIENT_MOVE,
51     CLIENT_RESIZE,
52     CLIENT_CLOSE
53 };
54
55 static gboolean client_menu_update(ObMenuFrame *frame, gpointer data)
56 {
57     ObMenu *menu = frame->menu;
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         ObMenuEntry *e = it->data;
65         gboolean *en = &e->data.normal.enabled; /* save some typing */
66         ObClient *c = frame->client;
67
68         if (e->type == OB_MENU_ENTRY_TYPE_NORMAL) {
69             switch (e->id) {
70             case CLIENT_ICONIFY:
71                 *en = c->functions & OB_CLIENT_FUNC_ICONIFY;
72                 break;
73             case CLIENT_RESTORE:
74                 *en = c->max_horz || c->max_vert;
75                 break;
76             case CLIENT_MAXIMIZE:
77                 *en = ((c->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
78                        (!c->max_horz || !c->max_vert));
79                 break;
80             case CLIENT_SHADE:
81                 *en = c->functions & OB_CLIENT_FUNC_SHADE;
82                 break;
83             case CLIENT_MOVE:
84                 *en = c->functions & OB_CLIENT_FUNC_MOVE;
85                 break;
86             case CLIENT_RESIZE:
87                 *en = c->functions & OB_CLIENT_FUNC_RESIZE;
88                 break;
89             case CLIENT_CLOSE:
90                 *en = c->functions & OB_CLIENT_FUNC_CLOSE;
91                 break;
92             case CLIENT_DECORATE:
93                 *en = client_normal(c);
94                 break;
95             default:
96                 *en = TRUE;
97             }
98         }
99     }
100     return TRUE; /* show the menu */
101 }
102
103 static void client_menu_execute(ObMenuEntry *e, ObMenuFrame *f,
104                                 ObClient *c, guint state, gpointer data,
105                                 Time time)
106 {
107     gint x, y;
108
109     g_assert(c);
110
111     switch (e->id) {
112     case CLIENT_ICONIFY:
113         /* the client won't be on screen anymore so hide the menu */
114         menu_frame_hide_all();
115         f = NULL; /* and don't update */
116
117         client_iconify(c, TRUE, FALSE, FALSE);
118         break;
119     case CLIENT_RESTORE:
120         client_maximize(c, FALSE, 0);
121         break;
122     case CLIENT_MAXIMIZE:
123         client_maximize(c, TRUE, 0);
124         break;
125     case CLIENT_SHADE:
126         client_shade(c, !c->shaded);
127         break;
128     case CLIENT_DECORATE:
129         client_set_undecorated(c, !c->undecorated);
130         break;
131     case CLIENT_MOVE:
132         /* this needs to grab the keyboard so hide the menu */
133         menu_frame_hide_all();
134         f = NULL; /* and don't update */
135
136         screen_pointer_pos(&x, &y);
137         moveresize_start(c, x, y, 0,
138                          prop_atoms.net_wm_moveresize_move_keyboard);
139         break;
140     case CLIENT_RESIZE:
141         /* this needs to grab the keyboard so hide the menu */
142         menu_frame_hide_all();
143         f = NULL; /* and don't update */
144
145         screen_pointer_pos(&x, &y);
146         moveresize_start(c, x, y, 0,
147                          prop_atoms.net_wm_moveresize_size_keyboard);
148         break;
149     case CLIENT_CLOSE:
150         client_close(c);
151         break;
152     default:
153         g_assert_not_reached();
154     }
155
156     /* update the menu cuz stuff can have changed */
157     if (f) {
158         client_menu_update(f, NULL);
159         menu_frame_render(f);
160     }
161 }
162
163 static gboolean layer_menu_update(ObMenuFrame *frame, gpointer data)
164 {
165     ObMenu *menu = frame->menu;
166     GList *it;
167
168     if (frame->client == NULL || !client_normal(frame->client))
169         return FALSE; /* don't show the menu */
170
171     for (it = menu->entries; it; it = g_list_next(it)) {
172         ObMenuEntry *e = it->data;
173         gboolean *en = &e->data.normal.enabled; /* save some typing */
174         ObClient *c = frame->client;
175
176         if (e->type == OB_MENU_ENTRY_TYPE_NORMAL) {
177             switch (e->id) {
178             case LAYER_TOP:
179                 *en = !c->above;
180                 break;
181             case LAYER_NORMAL:
182                 *en = c->above || c->below;
183                 break;
184             case LAYER_BOTTOM:
185                 *en = !c->below;
186                 break;
187             default:
188                 *en = TRUE;
189             }
190         }
191     }
192     return TRUE; /* show the menu */
193 }
194
195 static void layer_menu_execute(ObMenuEntry *e, ObMenuFrame *f,
196                                ObClient *c, guint state, gpointer data,
197                                Time time)
198 {
199     g_assert(c);
200
201     switch (e->id) {
202     case LAYER_TOP:
203         client_set_layer(c, 1);
204         break;
205     case LAYER_NORMAL:
206         client_set_layer(c, 0);
207         break;
208     case LAYER_BOTTOM:
209         client_set_layer(c, -1);
210         break;
211     default:
212         g_assert_not_reached();
213     }
214
215     /* update the menu cuz stuff can have changed */
216     if (f) {
217         layer_menu_update(f, NULL);
218         menu_frame_render(f);
219     }
220 }
221
222 static gboolean send_to_menu_update(ObMenuFrame *frame, gpointer data)
223 {
224     ObMenu *menu = frame->menu;
225     guint i;
226     ObMenuEntry *e;
227
228     menu_clear_entries(menu);
229
230     if (frame->client == NULL || !client_normal(frame->client))
231         return FALSE; /* don't show the menu */
232
233     for (i = 0; i <= screen_num_desktops; ++i) {
234         const gchar *name;
235         guint desk;
236
237         if (i >= screen_num_desktops) {
238             menu_add_separator(menu, -1, NULL);
239
240             desk = DESKTOP_ALL;
241             name = _("All desktops");
242         } else {
243             desk = i;
244             name = screen_desktop_names[i];
245         }
246
247         e = menu_add_normal(menu, desk, name, NULL, FALSE);
248         e->id = desk;
249         if (desk == DESKTOP_ALL) {
250             e->data.normal.mask = ob_rr_theme->desk_mask;
251             e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
252             e->data.normal.mask_selected_color =
253                 ob_rr_theme->menu_selected_color;
254             e->data.normal.mask_disabled_color =
255                 ob_rr_theme->menu_disabled_color;
256             e->data.normal.mask_disabled_selected_color =
257                 ob_rr_theme->menu_disabled_selected_color;
258         }
259
260         if (frame->client->desktop == desk)
261             e->data.normal.enabled = FALSE;
262     }
263     return TRUE; /* show the menu */
264 }
265
266 static void send_to_menu_execute(ObMenuEntry *e, ObMenuFrame *f,
267                                  ObClient *c, guint state, gpointer data,
268                                  Time time)
269 {
270     g_assert(c);
271
272     client_set_desktop(c, e->id, FALSE);
273     /* the client won't even be on the screen anymore, so hide the menu */
274     if (f)
275         menu_frame_hide_all();
276 }
277
278 static void client_menu_place(ObMenuFrame *frame, gint *x, gint *y,
279                               gint button, gpointer data)
280 {
281     gint dx, dy;
282
283     if (button == 0 && frame->client) {
284         *x = frame->client->frame->area.x;
285
286         /* try below the titlebar */
287         *y = frame->client->frame->area.y + frame->client->frame->size.top -
288             frame->client->frame->bwidth;
289         menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
290         if (dy != 0) {
291             /* try above the titlebar */
292             *y = frame->client->frame->area.y + frame->client->frame->bwidth -
293                 frame->area.height;
294             menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
295         }
296         if (dy != 0) {
297             /* didnt fit either way, use move on screen's values */
298             *y = frame->client->frame->area.y + frame->client->frame->size.top;
299             menu_frame_move_on_screen(frame, *x, *y, &dx, &dy);
300         }
301
302         *x += dx;
303         *y += dy;
304     } else {
305         gint myx, myy;
306
307         myx = *x;
308         myy = *y;
309
310         /* try to the bottom right of the cursor */
311         menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
312         if (dx != 0 || dy != 0) {
313             /* try to the bottom left of the cursor */
314             myx = *x - frame->area.width;
315             myy = *y;
316             menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
317         }
318         if (dx != 0 || dy != 0) {
319             /* try to the top right of the cursor */
320             myx = *x;
321             myy = *y - frame->area.height;
322             menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
323         }
324         if (dx != 0 || dy != 0) {
325             /* try to the top left of the cursor */
326             myx = *x - frame->area.width;
327             myy = *y - frame->area.height;
328             menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
329         }
330         if (dx != 0 || dy != 0) {
331             /* if didnt fit on either side so just use what it says */
332             myx = *x;
333             myy = *y;
334             menu_frame_move_on_screen(frame, myx, myy, &dx, &dy);
335         }
336         *x = myx + dx;
337         *y = myy + dy;
338     }
339 }
340
341 void client_menu_startup()
342 {
343     ObMenu *menu;
344     ObMenuEntry *e;
345
346     menu = menu_new(LAYER_MENU_NAME, _("&Layer"), TRUE, NULL);
347     menu_show_all_shortcuts(menu, TRUE);
348     menu_set_update_func(menu, layer_menu_update);
349     menu_set_execute_func(menu, layer_menu_execute);
350
351     menu_add_normal(menu, LAYER_TOP, _("Always on &top"), NULL, TRUE);
352     menu_add_normal(menu, LAYER_NORMAL, _("&Normal"), NULL, TRUE);
353     menu_add_normal(menu, LAYER_BOTTOM, _("Always on &bottom"),NULL, TRUE);
354
355
356     menu = menu_new(SEND_TO_MENU_NAME, _("&Send to desktop"), TRUE, NULL);
357     menu_set_update_func(menu, send_to_menu_update);
358     menu_set_execute_func(menu, send_to_menu_execute);
359
360     menu = menu_new(CLIENT_MENU_NAME, _("Client menu"), TRUE, NULL);
361     menu_show_all_shortcuts(menu, TRUE);
362     menu_set_update_func(menu, client_menu_update);
363     menu_set_place_func(menu, client_menu_place);
364     menu_set_execute_func(menu, client_menu_execute);
365
366     e = menu_add_normal(menu, CLIENT_RESTORE, _("R&estore"), NULL, TRUE);
367     e->data.normal.mask = ob_rr_theme->max_toggled_mask; 
368     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
369     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
370     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
371     e->data.normal.mask_disabled_selected_color =
372         ob_rr_theme->menu_disabled_selected_color;
373
374     menu_add_normal(menu, CLIENT_MOVE, _("&Move"), NULL, TRUE);
375
376     menu_add_normal(menu, CLIENT_RESIZE, _("Resi&ze"), NULL, TRUE);
377
378     e = menu_add_normal(menu, CLIENT_ICONIFY, _("Ico&nify"), NULL, TRUE);
379     e->data.normal.mask = ob_rr_theme->iconify_mask;
380     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
381     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
382     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
383     e->data.normal.mask_disabled_selected_color =
384         ob_rr_theme->menu_disabled_selected_color;
385
386     e = menu_add_normal(menu, CLIENT_MAXIMIZE, _("Ma&ximize"), NULL, TRUE);
387     e->data.normal.mask = ob_rr_theme->max_mask; 
388     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
389     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
390     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
391     e->data.normal.mask_disabled_selected_color =
392         ob_rr_theme->menu_disabled_selected_color;
393
394     menu_add_normal(menu, CLIENT_SHADE, _("&Roll up/down"), NULL, TRUE);
395
396     menu_add_normal(menu, CLIENT_DECORATE, _("Un/&Decorate"), NULL, TRUE);
397
398     menu_add_separator(menu, -1, NULL);
399
400     menu_add_submenu(menu, CLIENT_SEND_TO, SEND_TO_MENU_NAME);
401
402     menu_add_submenu(menu, CLIENT_LAYER, LAYER_MENU_NAME);
403
404     menu_add_separator(menu, -1, NULL);
405
406     e = menu_add_normal(menu, CLIENT_CLOSE, _("&Close"), NULL, TRUE);
407     e->data.normal.mask = ob_rr_theme->close_mask;
408     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
409     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
410     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
411     e->data.normal.mask_disabled_selected_color =
412         ob_rr_theme->menu_disabled_selected_color;
413 }