chiark / gitweb /
disobedience: more sensible naming in menu setup
[disorder] / disobedience / menu.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
3697ed99 3 * Copyright (C) 2006-2009, 2011, 2013 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
9 *
e7eb3a27
RK
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
2c348789
RK
18/** @file disobedience/menu.c
19 * @brief Main menu
20 */
460b9539 21
22#include "disobedience.h"
23
96295b25
RK
24static void toggled_minimode(GtkCheckMenuItem *item, gpointer userdata);
25
460b9539 26static GtkWidget *selectall_widget;
fb009628 27static GtkWidget *selectnone_widget;
460b9539 28static GtkWidget *properties_widget;
5b36cfe3 29GtkWidget *menu_playlists_widget;
f9b20469 30GtkWidget *playlists_menu;
5b36cfe3 31GtkWidget *menu_editplaylists_widget;
96295b25 32static GtkWidget *menu_minimode_widget;
460b9539 33
ac6bf2ba
RK
34/** @brief Main menu widgets */
35GtkItemFactory *mainmenufactory;
36
96295b25 37/** @brief Set for full mode, clear for mini mode */
01237e47 38int full_mode = 1;
96295b25 39
06bfbba4 40static void about_popup_got_version(void *v,
e2717131 41 const char *err,
06bfbba4 42 const char *value);
460b9539 43
2c348789
RK
44/** @brief Called when the quit option is activated
45 *
46 * Just exits.
47 */
460b9539 48static void quit_program(gpointer attribute((unused)) callback_data,
49 guint attribute((unused)) callback_action,
50 GtkWidget attribute((unused)) *menu_item) {
51 D(("quit_program"));
52 exit(0);
53}
54
d9a92920 55/** @brief Called when an edit menu item is selected
2c348789 56 *
d9a92920
RK
57 * Shared by several menu items; callback_action is expected to be the offset
58 * of the activate member of struct tabtype.
2c348789 59 */
d9a92920
RK
60static void menu_tab_action(gpointer attribute((unused)) callback_data,
61 guint callback_action,
460b9539 62 GtkWidget attribute((unused)) *menu_item) {
63 GtkWidget *tab = gtk_notebook_get_nth_page
64 (GTK_NOTEBOOK(tabs), gtk_notebook_current_page(GTK_NOTEBOOK(tabs)));
65 const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
66
d9a92920
RK
67 void (**activatep)(GtkMenuItem *, gpointer)
68 = (void *)((const char *)t + callback_action);
69 void (*activate)(GtkMenuItem *, gpointer) = *activatep;
70
71 if(activate)
72 activate(NULL, t->extra);
460b9539 73}
74
73f1b9f3
RK
75/** @brief Called when the login option is activated */
76static void login(gpointer attribute((unused)) callback_data,
77 guint attribute((unused)) callback_action,
78 GtkWidget attribute((unused)) *menu_item) {
79 login_box();
80}
81
ffc4dbaf
RK
82/** @brief Called when the login option is activated */
83static void users(gpointer attribute((unused)) callback_data,
84 guint attribute((unused)) callback_action,
85 GtkWidget attribute((unused)) *menu_item) {
86 manage_users();
87}
88
46fb1b05
RK
89#if 0
90/** @brief Called when the settings option is activated */
91static void settings(gpointer attribute((unused)) callback_data,
92 guint attribute((unused)) callback_action,
93 GtkWidget attribute((unused)) *menu_item) {
94 popup_settings();
95}
96#endif
97
229d327f 98/** @brief Called when edit menu is shown
2c348789
RK
99 *
100 * Determines option sensitivity according to the current tab and adjusts the
101 * widgets accordingly. Knows about @ref DISORDER_CONNECTED so the callbacks
102 * need not.
103 */
229d327f
RK
104static void edit_menu_show(GtkWidget attribute((unused)) *widget,
105 gpointer attribute((unused)) user_data) {
961d31f6
RK
106 if(tabs) {
107 GtkWidget *tab = gtk_notebook_get_nth_page
108 (GTK_NOTEBOOK(tabs),
229d327f 109 gtk_notebook_current_page(GTK_NOTEBOOK(tabs)));
961d31f6
RK
110 const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
111
112 assert(t != 0);
113 gtk_widget_set_sensitive(properties_widget,
ee7552f8
RK
114 (t->properties_sensitive
115 && t->properties_sensitive(t->extra)
961d31f6
RK
116 && (disorder_eclient_state(client) & DISORDER_CONNECTED)));
117 gtk_widget_set_sensitive(selectall_widget,
ee7552f8
RK
118 t->selectall_sensitive
119 && t->selectall_sensitive(t->extra));
961d31f6 120 gtk_widget_set_sensitive(selectnone_widget,
ee7552f8
RK
121 t->selectnone_sensitive
122 && t->selectnone_sensitive(t->extra));
961d31f6 123 }
460b9539 124}
fc36ecb7 125
2c348789 126/** @brief Fetch version in order to display the about... popup */
460b9539 127static void about_popup(gpointer attribute((unused)) callback_data,
128 guint attribute((unused)) callback_action,
129 GtkWidget attribute((unused)) *menu_item) {
130 D(("about_popup"));
131
132 gtk_label_set_text(GTK_LABEL(report_label), "getting server version");
133 disorder_eclient_version(client,
134 about_popup_got_version,
135 0);
136}
137
13affe66
RK
138static void manual_popup(gpointer attribute((unused)) callback_data,
139 guint attribute((unused)) callback_action,
140 GtkWidget attribute((unused)) *menu_item) {
141 D(("manual_popup"));
142
ad424400 143 popup_help(NULL);
13affe66
RK
144}
145
883a4e96 146/** @brief Called when version arrives, displays about... popup */
460b9539 147static void about_popup_got_version(void attribute((unused)) *v,
abf99697 148 const char attribute((unused)) *err,
460b9539 149 const char *value) {
150 GtkWidget *w;
151 char *server_version_string;
54cca6d6 152 char *short_version_string;
413b30a4 153 GtkWidget *hbox, *vbox, *title;
460b9539 154
06bfbba4
RK
155 if(!value)
156 value = "[error]";
3697ed99 157 byte_xasprintf(&server_version_string, "DisOrder server version %s", value);
54cca6d6
RK
158 byte_xasprintf(&short_version_string, "Disobedience %s",
159 disorder_short_version_string);
413b30a4 160 w = gtk_dialog_new_with_buttons("About Disobedience",
460b9539 161 GTK_WINDOW(toplevel),
162 (GTK_DIALOG_MODAL
163 |GTK_DIALOG_DESTROY_WITH_PARENT),
164 GTK_STOCK_OK,
165 GTK_RESPONSE_ACCEPT,
166 (char *)NULL);
413b30a4
RK
167 hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*padding*/);
168 vbox = gtk_vbox_new(FALSE/*homogeneous*/, 1/*padding*/);
169 gtk_box_pack_start(GTK_BOX(hbox),
170 gtk_image_new_from_pixbuf(find_image("duck.png")),
171 FALSE/*expand*/,
172 FALSE/*fill*/,
173 4/*padding*/);
174 gtk_box_pack_start(GTK_BOX(vbox),
54cca6d6 175 gtk_label_new(short_version_string),
413b30a4
RK
176 FALSE/*expand*/,
177 FALSE/*fill*/,
178 1/*padding*/);
179 gtk_box_pack_start(GTK_BOX(vbox),
180 gtk_label_new(server_version_string),
181 FALSE/*expand*/,
182 FALSE/*fill*/,
183 1/*padding*/);
184 gtk_box_pack_start(GTK_BOX(vbox),
3697ed99 185 gtk_label_new("\xC2\xA9 2003-2013 Richard Kettlewell et al"),
413b30a4
RK
186 FALSE/*expand*/,
187 FALSE/*fill*/,
188 1/*padding*/);
189 gtk_box_pack_end(GTK_BOX(hbox),
190 vbox,
191 FALSE/*expand*/,
192 FALSE/*fill*/,
193 0/*padding*/);
194 title = gtk_label_new(0);
195 gtk_label_set_markup(GTK_LABEL(title),
196 "<span font_desc=\"Sans 36\">Disobedience</span>");
197 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(w)->vbox), title,
198 FALSE/*expand*/,
199 FALSE/*fill*/,
200 0/*padding*/);
201 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(w)->vbox), hbox,
202 FALSE/*expand*/,
203 FALSE/*fill*/,
204 0/*padding*/);
76fc02c5 205 set_tool_colors(w);
460b9539 206 gtk_widget_show_all(w);
207 gtk_dialog_run(GTK_DIALOG(w));
208 gtk_widget_destroy(w);
209}
210
c73bd6c1
RK
211/** @brief Set 'Manage Users' menu item sensitivity */
212void users_set_sensitive(int sensitive) {
213 GtkWidget *w = gtk_item_factory_get_widget(mainmenufactory,
214 "<GdisorderMain>/Server/Manage users");
215 gtk_widget_set_sensitive(w, sensitive);
216}
217
73acd48d
RK
218static void menu_check_userman(void attribute((unused)) *v,
219 const char *err,
220 const char attribute((unused)) *value) {
221 if(err && !strncmp(err, "510", 3))
222 users_set_sensitive(FALSE);
223 else
224 users_set_sensitive(TRUE);
225}
226
6f09d54d
RK
227/** @brief Called when our rights change */
228static void menu_rights_changed(const char attribute((unused)) *event,
229 void attribute((unused)) *eventdata,
230 void attribute((unused)) *callbackdata) {
73acd48d
RK
231 if(last_rights & RIGHT_ADMIN)
232 disorder_eclient_userinfo(client, menu_check_userman, "", "email", 0);
233 else
234 users_set_sensitive(FALSE);
c73bd6c1
RK
235}
236
2c348789 237/** @brief Create the menu bar widget */
5602a92f 238GtkWidget *menubar(GtkWidget *window) {
d4ef4132
RK
239 GtkWidget *m;
240
460b9539 241 static const GtkItemFactoryEntry entries[] = {
2d504956 242 {
eb80584d 243 (char *)"/Server", /* path */
2d504956
RK
244 0, /* accelerator */
245 0, /* callback */
246 0, /* callback_action */
247 (char *)"<Branch>", /* item_type */
248 0 /* extra_data */
249 },
250 {
eb80584d 251 (char *)"/Server/Login", /* path */
2d504956
RK
252 (char *)"<CTRL>L", /* accelerator */
253 login, /* callback */
254 0, /* callback_action */
255 0, /* item_type */
256 0 /* extra_data */
257 },
ffc4dbaf 258 {
eb80584d 259 (char *)"/Server/Manage users", /* path */
ffc4dbaf
RK
260 0, /* accelerator */
261 users, /* callback */
262 0, /* callback_action */
263 0, /* item_type */
264 0 /* extra_data */
265 },
46fb1b05
RK
266#if 0
267 {
eb80584d 268 (char *)"/Server/Settings", /* path */
46fb1b05
RK
269 0, /* accelerator */
270 settings, /* callback */
271 0, /* callback_action */
272 0, /* item_type */
273 0 /* extra_data */
274 },
275#endif
2d504956 276 {
eb80584d 277 (char *)"/Server/Quit Disobedience", /* path */
2d504956
RK
278 (char *)"<CTRL>Q", /* accelerator */
279 quit_program, /* callback */
280 0, /* callback_action */
281 (char *)"<StockItem>", /* item_type */
282 GTK_STOCK_QUIT /* extra_data */
283 },
ac6bf2ba 284
2d504956
RK
285 {
286 (char *)"/Edit", /* path */
287 0, /* accelerator */
288 0, /* callback */
289 0, /* callback_action */
290 (char *)"<Branch>", /* item_type */
291 0 /* extra_data */
292 },
293 {
294 (char *)"/Edit/Select all tracks", /* path */
a99b7c50 295 (char *)"<CTRL>A", /* accelerator */
d9a92920
RK
296 menu_tab_action, /* callback */
297 offsetof(struct tabtype, selectall_activate), /* callback_action */
5170c61b
RK
298 (char *)"<StockItem>", /* item_type */
299 GTK_STOCK_SELECT_ALL, /* extra_data */
2d504956
RK
300 },
301 {
fb009628 302 (char *)"/Edit/Deselect all tracks", /* path */
a99b7c50 303 (char *)"<CTRL><SHIFT>A", /* accelerator */
d9a92920
RK
304 menu_tab_action, /* callback */
305 offsetof(struct tabtype, selectnone_activate), /* callback_action */
fb009628
RK
306 0, /* item_type */
307 0 /* extra_data */
308 },
309 {
2d504956
RK
310 (char *)"/Edit/Track properties", /* path */
311 0, /* accelerator */
d9a92920
RK
312 menu_tab_action, /* callback */
313 offsetof(struct tabtype, properties_activate), /* callback_action */
5170c61b
RK
314 (char *)"<StockItem>", /* item_type */
315 GTK_STOCK_PROPERTIES, /* extra_data */
2d504956 316 },
fc36ecb7
RK
317 {
318 (char *)"/Edit/Edit playlists", /* path */
319 0, /* accelerator */
f06a754c 320 playlist_window_create, /* callback */
fc36ecb7
RK
321 0, /* callback_action */
322 0, /* item_type */
323 0 /* extra_data */
324 },
325
2d504956
RK
326
327 {
328 (char *)"/Control", /* path */
329 0, /* accelerator */
330 0, /* callback */
331 0, /* callback_action */
332 (char *)"<Branch>", /* item_type */
333 0 /* extra_data */
334 },
335 {
336 (char *)"/Control/Scratch", /* path */
337 (char *)"<CTRL>S", /* accelerator */
338 0, /* callback */
339 0, /* callback_action */
5170c61b
RK
340 (char *)"<StockItem>", /* item_type */
341 GTK_STOCK_STOP, /* extra_data */
2d504956
RK
342 },
343 {
685bdfbd
RK
344 (char *)"/Control/Playing", /* path */
345 (char *)"<CTRL>P", /* accelerator */
346 0, /* callback */
347 0, /* callback_action */
348 (char *)"<CheckItem>", /* item_type */
349 0 /* extra_data */
350 },
351 {
2d504956
RK
352 (char *)"/Control/Random play", /* path */
353 (char *)"<CTRL>R", /* accelerator */
354 0, /* callback */
685bdfbd
RK
355 0, /* callback_action */
356 (char *)"<CheckItem>", /* item_type */
357 0 /* extra_data */
358 },
359 {
360 (char *)"/Control/Network player", /* path */
361 (char *)"<CTRL>N", /* accelerator */
362 0, /* callback */
2d504956
RK
363 0, /* callback_action */
364 (char *)"<CheckItem>", /* item_type */
365 0 /* extra_data */
366 },
96295b25
RK
367 {
368 (char *)"/Control/Compact mode", /* path */
369 (char *)"<CTRL>M", /* accelerator */
370 0, /* callback */
371 0, /* callback_action */
372 (char *)"<CheckItem>", /* item_type */
373 0 /* extra_data */
374 },
fc36ecb7 375 {
e5cce183
RK
376 (char *)"/Control/Global Preferences", /* path */
377 (char *)"<CTRL>G", /* accelerator */
378 popup_globals, /* callback */
7791f6cf
RK
379 0, /* callback_action */
380 0, /* item_type */
381 0 /* extra_data */
382 },
383 {
fc36ecb7
RK
384 (char *)"/Control/Activate playlist", /* path */
385 0, /* accelerator */
386 0, /* callback */
387 0, /* callback_action */
388 (char *)"<Branch>", /* item_type */
389 0 /* extra_data */
390 },
aeec47be 391
2d504956
RK
392 {
393 (char *)"/Help", /* path */
394 0, /* accelerator */
395 0, /* callback */
396 0, /* callback_action */
397 (char *)"<Branch>", /* item_type */
398 0 /* extra_data */
399 },
400 {
7e960883 401 (char *)"/Help/Manual", /* path */
13affe66
RK
402 0, /* accelerator */
403 manual_popup, /* callback */
404 0, /* callback_action */
5170c61b
RK
405 (char *)"<StockItem>", /* item_type */
406 GTK_STOCK_HELP, /* extra_data */
13affe66
RK
407 },
408 {
3697ed99 409 (char *)"/Help/About Disobedience", /* path */
2d504956
RK
410 0, /* accelerator */
411 about_popup, /* callback */
412 0, /* callback_action */
413 (char *)"<StockItem>", /* item_type */
414 GTK_STOCK_ABOUT /* extra_data */
415 },
460b9539 416 };
417
460b9539 418 GtkAccelGroup *accel = gtk_accel_group_new();
419
420 D(("add_menubar"));
421 /* TODO: item factories are deprecated in favour of some XML thing */
ac6bf2ba
RK
422 mainmenufactory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<GdisorderMain>",
423 accel);
424 gtk_item_factory_create_items(mainmenufactory,
460b9539 425 sizeof entries / sizeof *entries,
426 (GtkItemFactoryEntry *)entries,
427 0);
5602a92f 428 gtk_window_add_accel_group(GTK_WINDOW(window), accel);
ac6bf2ba 429 selectall_widget = gtk_item_factory_get_widget(mainmenufactory,
39fe1014 430 "<GdisorderMain>/Edit/Select all tracks");
fb009628
RK
431 selectnone_widget = gtk_item_factory_get_widget(mainmenufactory,
432 "<GdisorderMain>/Edit/Deselect all tracks");
ac6bf2ba 433 properties_widget = gtk_item_factory_get_widget(mainmenufactory,
39fe1014 434 "<GdisorderMain>/Edit/Track properties");
fdea9f40 435 menu_playlists_widget = gtk_item_factory_get_item(mainmenufactory,
fc36ecb7
RK
436 "<GdisorderMain>/Control/Activate playlist");
437 playlists_menu = gtk_item_factory_get_widget(mainmenufactory,
438 "<GdisorderMain>/Control/Activate playlist");
fdea9f40 439 menu_editplaylists_widget = gtk_item_factory_get_widget(mainmenufactory,
6acdbba4 440 "<GdisorderMain>/Edit/Edit playlists");
96295b25
RK
441 menu_minimode_widget = gtk_item_factory_get_widget(mainmenufactory,
442 "<GdisorderMain>/Control/Compact mode");
460b9539 443 assert(selectall_widget != 0);
fb009628 444 assert(selectnone_widget != 0);
460b9539 445 assert(properties_widget != 0);
5b36cfe3 446 assert(menu_playlists_widget != 0);
fc36ecb7 447 assert(playlists_menu != 0);
5b36cfe3 448 assert(menu_editplaylists_widget != 0);
229d327f 449
229d327f
RK
450 GtkWidget *edit_widget = gtk_item_factory_get_widget(mainmenufactory,
451 "<GdisorderMain>/Edit");
229d327f 452 g_signal_connect(edit_widget, "show", G_CALLBACK(edit_menu_show), 0);
fc36ecb7 453
6f09d54d
RK
454 event_register("rights-changed", menu_rights_changed, 0);
455 users_set_sensitive(0);
d4ef4132
RK
456 m = gtk_item_factory_get_widget(mainmenufactory,
457 "<GdisorderMain>");
458 set_tool_colors(m);
96295b25
RK
459 if(menu_minimode_widget)
460 g_signal_connect(G_OBJECT(menu_minimode_widget), "toggled",
461 G_CALLBACK(toggled_minimode), NULL);
d4ef4132 462 return m;
460b9539 463}
464
96295b25
RK
465static void toggled_minimode(GtkCheckMenuItem *item,
466 gpointer attribute((unused)) userdata) {
ecf9efdb
RK
467 int new_full_mode = !gtk_check_menu_item_get_active(item);
468 if(full_mode != new_full_mode) {
469 full_mode = new_full_mode;
470 event_raise("mini-mode-changed", NULL);
471 }
96295b25
RK
472}
473
460b9539 474/*
475Local Variables:
476c-basic-offset:2
477comment-column:40
478fill-column:79
479indent-tabs-mode:nil
480End:
481*/