the menu's labels are allocated
[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        Ben 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     frame->show_title = FALSE;
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 = !!frame->client;
66     }
67
68     if (!frame->client)
69         return;
70
71     e = menu_find_entry_id(menu, CLIENT_ICONIFY);
72     e->data.normal.enabled = frame->client->functions & OB_CLIENT_FUNC_ICONIFY;
73
74     e = menu_find_entry_id(menu, CLIENT_MAXIMIZE);
75     g_free(e->data.normal.label);
76     e->data.normal.label =
77         g_strdup(frame->client->max_vert || frame->client->max_horz ?
78                  _("Restore") : _("Maximize"));
79     e->data.normal.enabled =frame->client->functions & OB_CLIENT_FUNC_MAXIMIZE;
80
81     e = menu_find_entry_id(menu, CLIENT_SHADE);
82     g_free(e->data.normal.label);
83     e->data.normal.label = g_strdup(frame->client->shaded ?
84                                     _("Roll down") : _("Roll up"));
85     e->data.normal.enabled = frame->client->functions & OB_CLIENT_FUNC_SHADE;
86
87     e = menu_find_entry_id(menu, CLIENT_MOVE);
88     e->data.normal.enabled = frame->client->functions & OB_CLIENT_FUNC_MOVE;
89
90     e = menu_find_entry_id(menu, CLIENT_RESIZE);
91     e->data.normal.enabled = frame->client->functions & OB_CLIENT_FUNC_RESIZE;
92
93     e = menu_find_entry_id(menu, CLIENT_CLOSE);
94     e->data.normal.enabled = frame->client->functions & OB_CLIENT_FUNC_CLOSE;
95
96     e = menu_find_entry_id(menu, CLIENT_DECORATE);
97     e->data.normal.enabled = client_normal(frame->client);
98 }
99
100 static void layer_update(ObMenuFrame *frame, gpointer data)
101 {
102     ObMenu *menu = frame->menu;
103     ObMenuEntry *e;
104     GList *it;
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 = !!frame->client;
110     }
111
112     if (!frame->client)
113         return;
114
115     e = menu_find_entry_id(menu, LAYER_TOP);
116     e->data.normal.enabled = !frame->client->above;
117
118     e = menu_find_entry_id(menu, LAYER_NORMAL);
119     e->data.normal.enabled = (frame->client->above || frame->client->below);
120
121     e = menu_find_entry_id(menu, LAYER_BOTTOM);
122     e->data.normal.enabled = !frame->client->below;
123 }
124
125 static void send_to_update(ObMenuFrame *frame, gpointer data)
126 {
127     ObMenu *menu = frame->menu;
128     guint i;
129     GSList *acts;
130     ObAction *act;
131
132     menu_clear_entries(menu);
133
134     if (!frame->client)
135         return;
136
137     for (i = 0; i <= screen_num_desktops; ++i) {
138         gchar *name;
139         guint desk;
140
141         if (i >= screen_num_desktops) {
142             menu_add_separator(menu, -1);
143
144             desk = DESKTOP_ALL;
145             name = _("All desktops");
146         } else {
147             desk = i;
148             name = screen_desktop_names[i];
149         }
150
151         act = action_from_string("SendToDesktop",
152                                  OB_USER_ACTION_MENU_SELECTION);
153         act->data.sendto.desk = desk;
154         act->data.sendto.follow = FALSE;
155         acts = g_slist_prepend(NULL, act);
156         menu_add_normal(menu, desk, name, acts);
157
158         if (frame->client->desktop == desk) {
159             ObMenuEntry *e = menu_find_entry_id(menu, desk);
160             g_assert(e);
161             e->data.normal.enabled = FALSE;
162         }
163     }
164 }
165
166 void client_menu_startup()
167 {
168     GSList *acts;
169     ObMenu *menu;
170     ObMenuEntry *e;
171
172     menu = menu_new(LAYER_MENU_NAME, _("Layer"), NULL);
173     menu_set_update_func(menu, layer_update);
174
175     acts = g_slist_prepend(NULL, action_from_string
176                            ("SendToTopLayer", OB_USER_ACTION_MENU_SELECTION));
177     menu_add_normal(menu, LAYER_TOP, _("Always on top"), acts);
178
179     acts = g_slist_prepend(NULL, action_from_string
180                            ("SendToNormalLayer",
181                             OB_USER_ACTION_MENU_SELECTION));
182     menu_add_normal(menu, LAYER_NORMAL, _("Normal"), acts);
183
184     acts = g_slist_prepend(NULL, action_from_string
185                            ("SendToBottomLayer",
186                             OB_USER_ACTION_MENU_SELECTION));
187     menu_add_normal(menu, LAYER_BOTTOM, _("Always on bottom"),acts);
188
189
190     menu = menu_new(SEND_TO_MENU_NAME, _("Send to desktop"), NULL);
191     menu_set_update_func(menu, send_to_update);
192
193
194     menu = menu_new(CLIENT_MENU_NAME, _("Client menu"), NULL);
195     menu_set_update_func(menu, client_update);
196
197     e = menu_add_submenu(menu, CLIENT_SEND_TO, SEND_TO_MENU_NAME);
198     e->data.normal.mask = ob_rr_theme->desk_mask;
199     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
200     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
201     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
202
203     menu_add_submenu(menu, CLIENT_LAYER, LAYER_MENU_NAME);
204
205     acts = g_slist_prepend(NULL, action_from_string
206                            ("Iconify", OB_USER_ACTION_MENU_SELECTION));
207     e = menu_add_normal(menu, CLIENT_ICONIFY, _("Iconify"), acts);
208     e->data.normal.mask = ob_rr_theme->iconify_mask;
209     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
210     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
211     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
212
213     acts = g_slist_prepend(NULL, action_from_string
214                            ("ToggleMaximizeFull",
215                             OB_USER_ACTION_MENU_SELECTION));
216     e = menu_add_normal(menu, CLIENT_MAXIMIZE, "MAXIMIZE", acts);
217     e->data.normal.mask = ob_rr_theme->max_mask; 
218     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
219     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
220     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
221
222     acts = g_slist_prepend(NULL, action_from_string
223                            ("Raise", OB_USER_ACTION_MENU_SELECTION));
224     menu_add_normal(menu, CLIENT_RAISE, _("Raise to top"), acts);
225
226     acts = g_slist_prepend(NULL, action_from_string
227                            ("Lower", OB_USER_ACTION_MENU_SELECTION));
228     menu_add_normal(menu, CLIENT_LOWER, _("Lower to bottom"),acts);
229
230     acts = g_slist_prepend(NULL, action_from_string
231                            ("ToggleShade", OB_USER_ACTION_MENU_SELECTION));
232     e = menu_add_normal(menu, CLIENT_SHADE, "SHADE", acts);
233     e->data.normal.mask = ob_rr_theme->shade_mask;
234     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
235     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
236     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
237
238     acts = g_slist_prepend(NULL, action_from_string
239                            ("ToggleDecorations",
240                             OB_USER_ACTION_MENU_SELECTION));
241     menu_add_normal(menu, CLIENT_DECORATE, _("Decorate"), acts);
242
243     menu_add_separator(menu, -1);
244
245     acts = g_slist_prepend(NULL, action_from_string
246                            ("Move", OB_USER_ACTION_MENU_SELECTION));
247     menu_add_normal(menu, CLIENT_MOVE, _("Move"), acts);
248
249     acts = g_slist_prepend(NULL, action_from_string
250                            ("Resize", OB_USER_ACTION_MENU_SELECTION));
251     menu_add_normal(menu, CLIENT_RESIZE, _("Resize"), acts);
252
253     menu_add_separator(menu, -1);
254
255     acts = g_slist_prepend(NULL, action_from_string
256                            ("Close", OB_USER_ACTION_MENU_SELECTION));
257     e = menu_add_normal(menu, CLIENT_CLOSE, _("Close"), acts);
258     e->data.normal.mask = ob_rr_theme->close_mask;
259     e->data.normal.mask_normal_color = ob_rr_theme->menu_color;
260     e->data.normal.mask_disabled_color = ob_rr_theme->menu_disabled_color;
261     e->data.normal.mask_selected_color = ob_rr_theme->menu_selected_color;
262 }