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