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 static GtkWidget *playlists_widget;
30 static GtkWidget *playlists_menu;
32 /** @brief Main menu widgets */
33 GtkItemFactory *mainmenufactory;
35 static void about_popup_got_version(void *v,
39 /** @brief Called when the quit option is activated
43 static void quit_program(gpointer attribute((unused)) callback_data,
44 guint attribute((unused)) callback_action,
45 GtkWidget attribute((unused)) *menu_item) {
50 /** @brief Called when an edit menu item is selected
52 * Shared by several menu items; callback_action is expected to be the offset
53 * of the activate member of struct tabtype.
55 static void menu_tab_action(gpointer attribute((unused)) callback_data,
56 guint callback_action,
57 GtkWidget attribute((unused)) *menu_item) {
58 GtkWidget *tab = gtk_notebook_get_nth_page
59 (GTK_NOTEBOOK(tabs), gtk_notebook_current_page(GTK_NOTEBOOK(tabs)));
60 const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
62 void (**activatep)(GtkMenuItem *, gpointer)
63 = (void *)((const char *)t + callback_action);
64 void (*activate)(GtkMenuItem *, gpointer) = *activatep;
67 activate(NULL, t->extra);
70 /** @brief Called when the login option is activated */
71 static void login(gpointer attribute((unused)) callback_data,
72 guint attribute((unused)) callback_action,
73 GtkWidget attribute((unused)) *menu_item) {
77 /** @brief Called when the login option is activated */
78 static void users(gpointer attribute((unused)) callback_data,
79 guint attribute((unused)) callback_action,
80 GtkWidget attribute((unused)) *menu_item) {
85 /** @brief Called when the settings option is activated */
86 static void settings(gpointer attribute((unused)) callback_data,
87 guint attribute((unused)) callback_action,
88 GtkWidget attribute((unused)) *menu_item) {
93 /** @brief Called when edit menu is shown
95 * Determines option sensitivity according to the current tab and adjusts the
96 * widgets accordingly. Knows about @ref DISORDER_CONNECTED so the callbacks
99 static void edit_menu_show(GtkWidget attribute((unused)) *widget,
100 gpointer attribute((unused)) user_data) {
102 GtkWidget *tab = gtk_notebook_get_nth_page
104 gtk_notebook_current_page(GTK_NOTEBOOK(tabs)));
105 const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
108 gtk_widget_set_sensitive(properties_widget,
109 (t->properties_sensitive
110 && t->properties_sensitive(t->extra)
111 && (disorder_eclient_state(client) & DISORDER_CONNECTED)));
112 gtk_widget_set_sensitive(selectall_widget,
113 t->selectall_sensitive
114 && t->selectall_sensitive(t->extra));
115 gtk_widget_set_sensitive(selectnone_widget,
116 t->selectnone_sensitive
117 && t->selectnone_sensitive(t->extra));
121 /** @brief Fetch version in order to display the about... popup */
122 static void about_popup(gpointer attribute((unused)) callback_data,
123 guint attribute((unused)) callback_action,
124 GtkWidget attribute((unused)) *menu_item) {
127 gtk_label_set_text(GTK_LABEL(report_label), "getting server version");
128 disorder_eclient_version(client,
129 about_popup_got_version,
133 static void manual_popup(gpointer attribute((unused)) callback_data,
134 guint attribute((unused)) callback_action,
135 GtkWidget attribute((unused)) *menu_item) {
141 /** @brief Called when version arrives, displays about... popup */
142 static void about_popup_got_version(void attribute((unused)) *v,
143 const char attribute((unused)) *err,
146 char *server_version_string;
147 char *short_version_string;
148 GtkWidget *hbox, *vbox, *title;
152 byte_xasprintf(&server_version_string, "Server version %s", value);
153 byte_xasprintf(&short_version_string, "Disobedience %s",
154 disorder_short_version_string);
155 w = gtk_dialog_new_with_buttons("About Disobedience",
156 GTK_WINDOW(toplevel),
158 |GTK_DIALOG_DESTROY_WITH_PARENT),
162 hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*padding*/);
163 vbox = gtk_vbox_new(FALSE/*homogeneous*/, 1/*padding*/);
164 gtk_box_pack_start(GTK_BOX(hbox),
165 gtk_image_new_from_pixbuf(find_image("duck.png")),
169 gtk_box_pack_start(GTK_BOX(vbox),
170 gtk_label_new(short_version_string),
174 gtk_box_pack_start(GTK_BOX(vbox),
175 gtk_label_new(server_version_string),
179 gtk_box_pack_start(GTK_BOX(vbox),
180 gtk_label_new("\xC2\xA9 2004-2008 Richard Kettlewell"),
184 gtk_box_pack_end(GTK_BOX(hbox),
189 title = gtk_label_new(0);
190 gtk_label_set_markup(GTK_LABEL(title),
191 "<span font_desc=\"Sans 36\">Disobedience</span>");
192 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(w)->vbox), title,
196 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(w)->vbox), hbox,
201 gtk_widget_show_all(w);
202 gtk_dialog_run(GTK_DIALOG(w));
203 gtk_widget_destroy(w);
206 /** @brief Set 'Manage Users' menu item sensitivity */
207 void users_set_sensitive(int sensitive) {
208 GtkWidget *w = gtk_item_factory_get_widget(mainmenufactory,
209 "<GdisorderMain>/Server/Manage users");
210 gtk_widget_set_sensitive(w, sensitive);
213 /** @brief Called when our rights change */
214 static void menu_rights_changed(const char attribute((unused)) *event,
215 void attribute((unused)) *eventdata,
216 void attribute((unused)) *callbackdata) {
217 users_set_sensitive(!!(last_rights & RIGHT_ADMIN));
220 /** @brief Called to activate a playlist */
221 static void menu_activate_playlist(GtkMenuItem *menuitem,
222 gpointer attribute((unused)) user_data) {
223 GtkLabel *label = GTK_LABEL(GTK_BIN(menuitem)->child);
224 const char *playlist = gtk_label_get_text(label);
226 fprintf(stderr, "activate playlist %s\n", playlist); /* TODO */
229 /** @brief Called when the playlists change */
230 static void menu_playlists_changed(const char attribute((unused)) *event,
231 void attribute((unused)) *eventdata,
232 void attribute((unused)) *callbackdata) {
233 GtkMenuShell *menu = GTK_MENU_SHELL(playlists_menu);
234 /* TODO: we could be more sophisticated and only insert/remove widgets as
235 * needed. For now that's too much effort. */
236 while(menu->children)
237 gtk_container_remove(GTK_CONTAINER(menu), GTK_WIDGET(menu->children->data));
238 /* NB nplaylists can be -1 as well as 0 */
239 for(int n = 0; n < nplaylists; ++n) {
240 GtkWidget *w = gtk_menu_item_new_with_label(playlists[n]);
241 g_signal_connect(w, "activate", G_CALLBACK(menu_activate_playlist), 0);
243 gtk_menu_shell_append(menu, w);
245 gtk_widget_set_sensitive(playlists_widget,
249 static void edit_playlists(gpointer attribute((unused)) callback_data,
250 guint attribute((unused)) callback_action,
251 GtkWidget attribute((unused)) *menu_item) {
252 fprintf(stderr, "edit playlists\n"); /* TODO */
255 /** @brief Create the menu bar widget */
256 GtkWidget *menubar(GtkWidget *w) {
259 static const GtkItemFactoryEntry entries[] = {
261 (char *)"/Server", /* path */
264 0, /* callback_action */
265 (char *)"<Branch>", /* item_type */
269 (char *)"/Server/Login", /* path */
270 (char *)"<CTRL>L", /* accelerator */
271 login, /* callback */
272 0, /* callback_action */
277 (char *)"/Server/Manage users", /* path */
279 users, /* callback */
280 0, /* callback_action */
286 (char *)"/Server/Settings", /* path */
288 settings, /* callback */
289 0, /* callback_action */
295 (char *)"/Server/Quit Disobedience", /* path */
296 (char *)"<CTRL>Q", /* accelerator */
297 quit_program, /* callback */
298 0, /* callback_action */
299 (char *)"<StockItem>", /* item_type */
300 GTK_STOCK_QUIT /* extra_data */
304 (char *)"/Edit", /* path */
307 0, /* callback_action */
308 (char *)"<Branch>", /* item_type */
312 (char *)"/Edit/Select all tracks", /* path */
314 menu_tab_action, /* callback */
315 offsetof(struct tabtype, selectall_activate), /* callback_action */
320 (char *)"/Edit/Deselect all tracks", /* path */
322 menu_tab_action, /* callback */
323 offsetof(struct tabtype, selectnone_activate), /* callback_action */
328 (char *)"/Edit/Track properties", /* path */
330 menu_tab_action, /* callback */
331 offsetof(struct tabtype, properties_activate), /* callback_action */
336 (char *)"/Edit/Edit playlists", /* path */
338 edit_playlists, /* callback */
339 0, /* callback_action */
346 (char *)"/Control", /* path */
349 0, /* callback_action */
350 (char *)"<Branch>", /* item_type */
354 (char *)"/Control/Scratch", /* path */
355 (char *)"<CTRL>S", /* accelerator */
357 0, /* callback_action */
362 (char *)"/Control/Playing", /* path */
363 (char *)"<CTRL>P", /* accelerator */
365 0, /* callback_action */
366 (char *)"<CheckItem>", /* item_type */
370 (char *)"/Control/Random play", /* path */
371 (char *)"<CTRL>R", /* accelerator */
373 0, /* callback_action */
374 (char *)"<CheckItem>", /* item_type */
378 (char *)"/Control/Network player", /* path */
379 (char *)"<CTRL>N", /* accelerator */
381 0, /* callback_action */
382 (char *)"<CheckItem>", /* item_type */
386 (char *)"/Control/Activate playlist", /* path */
389 0, /* callback_action */
390 (char *)"<Branch>", /* item_type */
395 (char *)"/Help", /* path */
398 0, /* callback_action */
399 (char *)"<Branch>", /* item_type */
403 (char *)"/Help/Manual page", /* path */
405 manual_popup, /* callback */
406 0, /* callback_action */
411 (char *)"/Help/About DisOrder", /* path */
413 about_popup, /* callback */
414 0, /* callback_action */
415 (char *)"<StockItem>", /* item_type */
416 GTK_STOCK_ABOUT /* extra_data */
420 GtkAccelGroup *accel = gtk_accel_group_new();
423 /* TODO: item factories are deprecated in favour of some XML thing */
424 mainmenufactory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<GdisorderMain>",
426 gtk_item_factory_create_items(mainmenufactory,
427 sizeof entries / sizeof *entries,
428 (GtkItemFactoryEntry *)entries,
430 gtk_window_add_accel_group(GTK_WINDOW(w), accel);
431 selectall_widget = gtk_item_factory_get_widget(mainmenufactory,
432 "<GdisorderMain>/Edit/Select all tracks");
433 selectnone_widget = gtk_item_factory_get_widget(mainmenufactory,
434 "<GdisorderMain>/Edit/Deselect all tracks");
435 properties_widget = gtk_item_factory_get_widget(mainmenufactory,
436 "<GdisorderMain>/Edit/Track properties");
437 playlists_widget = gtk_item_factory_get_item(mainmenufactory,
438 "<GdisorderMain>/Control/Activate playlist");
439 playlists_menu = gtk_item_factory_get_widget(mainmenufactory,
440 "<GdisorderMain>/Control/Activate playlist");
441 assert(selectall_widget != 0);
442 assert(selectnone_widget != 0);
443 assert(properties_widget != 0);
444 assert(playlists_widget != 0);
445 assert(playlists_menu != 0);
447 GtkWidget *edit_widget = gtk_item_factory_get_widget(mainmenufactory,
448 "<GdisorderMain>/Edit");
449 g_signal_connect(edit_widget, "show", G_CALLBACK(edit_menu_show), 0);
451 event_register("rights-changed", menu_rights_changed, 0);
452 event_register("playlists-updated", menu_playlists_changed, 0);
453 users_set_sensitive(0);
454 m = gtk_item_factory_get_widget(mainmenufactory,