2 * This file is part of DisOrder.
3 * Copyright (C) 2006-2008 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file disobedience/menu.c
24 #include "disobedience.h"
26 static GtkWidget *selectall_widget;
27 static GtkWidget *selectnone_widget;
28 static GtkWidget *properties_widget;
29 GtkWidget *playlists_widget;
30 GtkWidget *playlists_menu;
31 GtkWidget *editplaylists_widget;
33 /** @brief Main menu widgets */
34 GtkItemFactory *mainmenufactory;
36 static void about_popup_got_version(void *v,
40 /** @brief Called when the quit option is activated
44 static void quit_program(gpointer attribute((unused)) callback_data,
45 guint attribute((unused)) callback_action,
46 GtkWidget attribute((unused)) *menu_item) {
51 /** @brief Called when an edit menu item is selected
53 * Shared by several menu items; callback_action is expected to be the offset
54 * of the activate member of struct tabtype.
56 static void menu_tab_action(gpointer attribute((unused)) callback_data,
57 guint callback_action,
58 GtkWidget attribute((unused)) *menu_item) {
59 GtkWidget *tab = gtk_notebook_get_nth_page
60 (GTK_NOTEBOOK(tabs), gtk_notebook_current_page(GTK_NOTEBOOK(tabs)));
61 const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
63 void (**activatep)(GtkMenuItem *, gpointer)
64 = (void *)((const char *)t + callback_action);
65 void (*activate)(GtkMenuItem *, gpointer) = *activatep;
68 activate(NULL, t->extra);
71 /** @brief Called when the login option is activated */
72 static void login(gpointer attribute((unused)) callback_data,
73 guint attribute((unused)) callback_action,
74 GtkWidget attribute((unused)) *menu_item) {
78 /** @brief Called when the login option is activated */
79 static void users(gpointer attribute((unused)) callback_data,
80 guint attribute((unused)) callback_action,
81 GtkWidget attribute((unused)) *menu_item) {
86 /** @brief Called when the settings option is activated */
87 static void settings(gpointer attribute((unused)) callback_data,
88 guint attribute((unused)) callback_action,
89 GtkWidget attribute((unused)) *menu_item) {
94 /** @brief Called when edit menu is shown
96 * Determines option sensitivity according to the current tab and adjusts the
97 * widgets accordingly. Knows about @ref DISORDER_CONNECTED so the callbacks
100 static void edit_menu_show(GtkWidget attribute((unused)) *widget,
101 gpointer attribute((unused)) user_data) {
103 GtkWidget *tab = gtk_notebook_get_nth_page
105 gtk_notebook_current_page(GTK_NOTEBOOK(tabs)));
106 const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
109 gtk_widget_set_sensitive(properties_widget,
110 (t->properties_sensitive
111 && t->properties_sensitive(t->extra)
112 && (disorder_eclient_state(client) & DISORDER_CONNECTED)));
113 gtk_widget_set_sensitive(selectall_widget,
114 t->selectall_sensitive
115 && t->selectall_sensitive(t->extra));
116 gtk_widget_set_sensitive(selectnone_widget,
117 t->selectnone_sensitive
118 && t->selectnone_sensitive(t->extra));
122 /** @brief Fetch version in order to display the about... popup */
123 static void about_popup(gpointer attribute((unused)) callback_data,
124 guint attribute((unused)) callback_action,
125 GtkWidget attribute((unused)) *menu_item) {
128 gtk_label_set_text(GTK_LABEL(report_label), "getting server version");
129 disorder_eclient_version(client,
130 about_popup_got_version,
134 static void manual_popup(gpointer attribute((unused)) callback_data,
135 guint attribute((unused)) callback_action,
136 GtkWidget attribute((unused)) *menu_item) {
142 /** @brief Called when version arrives, displays about... popup */
143 static void about_popup_got_version(void attribute((unused)) *v,
144 const char attribute((unused)) *err,
147 char *server_version_string;
148 char *short_version_string;
149 GtkWidget *hbox, *vbox, *title;
153 byte_xasprintf(&server_version_string, "Server version %s", value);
154 byte_xasprintf(&short_version_string, "Disobedience %s",
155 disorder_short_version_string);
156 w = gtk_dialog_new_with_buttons("About Disobedience",
157 GTK_WINDOW(toplevel),
159 |GTK_DIALOG_DESTROY_WITH_PARENT),
163 hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*padding*/);
164 vbox = gtk_vbox_new(FALSE/*homogeneous*/, 1/*padding*/);
165 gtk_box_pack_start(GTK_BOX(hbox),
166 gtk_image_new_from_pixbuf(find_image("duck.png")),
170 gtk_box_pack_start(GTK_BOX(vbox),
171 gtk_label_new(short_version_string),
175 gtk_box_pack_start(GTK_BOX(vbox),
176 gtk_label_new(server_version_string),
180 gtk_box_pack_start(GTK_BOX(vbox),
181 gtk_label_new("\xC2\xA9 2004-2008 Richard Kettlewell"),
185 gtk_box_pack_end(GTK_BOX(hbox),
190 title = gtk_label_new(0);
191 gtk_label_set_markup(GTK_LABEL(title),
192 "<span font_desc=\"Sans 36\">Disobedience</span>");
193 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(w)->vbox), title,
197 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(w)->vbox), hbox,
202 gtk_widget_show_all(w);
203 gtk_dialog_run(GTK_DIALOG(w));
204 gtk_widget_destroy(w);
207 /** @brief Set 'Manage Users' menu item sensitivity */
208 void users_set_sensitive(int sensitive) {
209 GtkWidget *w = gtk_item_factory_get_widget(mainmenufactory,
210 "<GdisorderMain>/Server/Manage users");
211 gtk_widget_set_sensitive(w, sensitive);
214 /** @brief Called when our rights change */
215 static void menu_rights_changed(const char attribute((unused)) *event,
216 void attribute((unused)) *eventdata,
217 void attribute((unused)) *callbackdata) {
218 users_set_sensitive(!!(last_rights & RIGHT_ADMIN));
221 /** @brief Create the menu bar widget */
222 GtkWidget *menubar(GtkWidget *w) {
225 static const GtkItemFactoryEntry entries[] = {
227 (char *)"/Server", /* path */
230 0, /* callback_action */
231 (char *)"<Branch>", /* item_type */
235 (char *)"/Server/Login", /* path */
236 (char *)"<CTRL>L", /* accelerator */
237 login, /* callback */
238 0, /* callback_action */
243 (char *)"/Server/Manage users", /* path */
245 users, /* callback */
246 0, /* callback_action */
252 (char *)"/Server/Settings", /* path */
254 settings, /* callback */
255 0, /* callback_action */
261 (char *)"/Server/Quit Disobedience", /* path */
262 (char *)"<CTRL>Q", /* accelerator */
263 quit_program, /* callback */
264 0, /* callback_action */
265 (char *)"<StockItem>", /* item_type */
266 GTK_STOCK_QUIT /* extra_data */
270 (char *)"/Edit", /* path */
273 0, /* callback_action */
274 (char *)"<Branch>", /* item_type */
278 (char *)"/Edit/Select all tracks", /* path */
280 menu_tab_action, /* callback */
281 offsetof(struct tabtype, selectall_activate), /* callback_action */
286 (char *)"/Edit/Deselect all tracks", /* path */
288 menu_tab_action, /* callback */
289 offsetof(struct tabtype, selectnone_activate), /* callback_action */
294 (char *)"/Edit/Track properties", /* path */
296 menu_tab_action, /* callback */
297 offsetof(struct tabtype, properties_activate), /* callback_action */
302 (char *)"/Edit/Edit playlists", /* path */
304 edit_playlists, /* callback */
305 0, /* callback_action */
312 (char *)"/Control", /* path */
315 0, /* callback_action */
316 (char *)"<Branch>", /* item_type */
320 (char *)"/Control/Scratch", /* path */
321 (char *)"<CTRL>S", /* accelerator */
323 0, /* callback_action */
328 (char *)"/Control/Playing", /* path */
329 (char *)"<CTRL>P", /* accelerator */
331 0, /* callback_action */
332 (char *)"<CheckItem>", /* item_type */
336 (char *)"/Control/Random play", /* path */
337 (char *)"<CTRL>R", /* accelerator */
339 0, /* callback_action */
340 (char *)"<CheckItem>", /* item_type */
344 (char *)"/Control/Network player", /* path */
345 (char *)"<CTRL>N", /* accelerator */
347 0, /* callback_action */
348 (char *)"<CheckItem>", /* item_type */
352 (char *)"/Control/Activate playlist", /* path */
355 0, /* callback_action */
356 (char *)"<Branch>", /* item_type */
361 (char *)"/Help", /* path */
364 0, /* callback_action */
365 (char *)"<Branch>", /* item_type */
369 (char *)"/Help/Manual page", /* path */
371 manual_popup, /* callback */
372 0, /* callback_action */
377 (char *)"/Help/About DisOrder", /* path */
379 about_popup, /* callback */
380 0, /* callback_action */
381 (char *)"<StockItem>", /* item_type */
382 GTK_STOCK_ABOUT /* extra_data */
386 GtkAccelGroup *accel = gtk_accel_group_new();
389 /* TODO: item factories are deprecated in favour of some XML thing */
390 mainmenufactory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<GdisorderMain>",
392 gtk_item_factory_create_items(mainmenufactory,
393 sizeof entries / sizeof *entries,
394 (GtkItemFactoryEntry *)entries,
396 gtk_window_add_accel_group(GTK_WINDOW(w), accel);
397 selectall_widget = gtk_item_factory_get_widget(mainmenufactory,
398 "<GdisorderMain>/Edit/Select all tracks");
399 selectnone_widget = gtk_item_factory_get_widget(mainmenufactory,
400 "<GdisorderMain>/Edit/Deselect all tracks");
401 properties_widget = gtk_item_factory_get_widget(mainmenufactory,
402 "<GdisorderMain>/Edit/Track properties");
403 playlists_widget = gtk_item_factory_get_item(mainmenufactory,
404 "<GdisorderMain>/Control/Activate playlist");
405 playlists_menu = gtk_item_factory_get_widget(mainmenufactory,
406 "<GdisorderMain>/Control/Activate playlist");
407 editplaylists_widget = gtk_item_factory_get_widget(mainmenufactory,
408 "<GdisorderMain>/Edit/Edit playlists");
409 assert(selectall_widget != 0);
410 assert(selectnone_widget != 0);
411 assert(properties_widget != 0);
412 assert(playlists_widget != 0);
413 assert(playlists_menu != 0);
414 assert(editplaylists_widget != 0);
416 GtkWidget *edit_widget = gtk_item_factory_get_widget(mainmenufactory,
417 "<GdisorderMain>/Edit");
418 g_signal_connect(edit_widget, "show", G_CALLBACK(edit_menu_show), 0);
420 event_register("rights-changed", menu_rights_changed, 0);
421 users_set_sensitive(0);
422 m = gtk_item_factory_get_widget(mainmenufactory,