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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file disobedience/menu.c
22 #include "disobedience.h"
24 static GtkWidget *selectall_widget;
25 static GtkWidget *selectnone_widget;
26 static GtkWidget *properties_widget;
28 /** @brief Main menu widgets */
29 GtkItemFactory *mainmenufactory;
31 static void about_popup_got_version(void *v,
35 /** @brief Called when the quit option is activated
39 static void quit_program(gpointer attribute((unused)) callback_data,
40 guint attribute((unused)) callback_action,
41 GtkWidget attribute((unused)) *menu_item) {
46 /** @brief Called when an edit menu item is selected
48 * Shared by several menu items; callback_action is expected to be the offset
49 * of the activate member of struct tabtype.
51 static void menu_tab_action(gpointer attribute((unused)) callback_data,
52 guint callback_action,
53 GtkWidget attribute((unused)) *menu_item) {
54 GtkWidget *tab = gtk_notebook_get_nth_page
55 (GTK_NOTEBOOK(tabs), gtk_notebook_current_page(GTK_NOTEBOOK(tabs)));
56 const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
58 void (**activatep)(GtkMenuItem *, gpointer)
59 = (void *)((const char *)t + callback_action);
60 void (*activate)(GtkMenuItem *, gpointer) = *activatep;
63 activate(NULL, t->extra);
66 /** @brief Called when the login option is activated */
67 static void login(gpointer attribute((unused)) callback_data,
68 guint attribute((unused)) callback_action,
69 GtkWidget attribute((unused)) *menu_item) {
73 /** @brief Called when the login option is activated */
74 static void users(gpointer attribute((unused)) callback_data,
75 guint attribute((unused)) callback_action,
76 GtkWidget attribute((unused)) *menu_item) {
81 /** @brief Called when the settings option is activated */
82 static void settings(gpointer attribute((unused)) callback_data,
83 guint attribute((unused)) callback_action,
84 GtkWidget attribute((unused)) *menu_item) {
89 /** @brief Called when edit menu is shown
91 * Determines option sensitivity according to the current tab and adjusts the
92 * widgets accordingly. Knows about @ref DISORDER_CONNECTED so the callbacks
95 static void edit_menu_show(GtkWidget attribute((unused)) *widget,
96 gpointer attribute((unused)) user_data) {
98 GtkWidget *tab = gtk_notebook_get_nth_page
100 gtk_notebook_current_page(GTK_NOTEBOOK(tabs)));
101 const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
104 gtk_widget_set_sensitive(properties_widget,
105 (t->properties_sensitive
106 && t->properties_sensitive(t->extra)
107 && (disorder_eclient_state(client) & DISORDER_CONNECTED)));
108 gtk_widget_set_sensitive(selectall_widget,
109 t->selectall_sensitive
110 && t->selectall_sensitive(t->extra));
111 gtk_widget_set_sensitive(selectnone_widget,
112 t->selectnone_sensitive
113 && t->selectnone_sensitive(t->extra));
117 /** @brief Fetch version in order to display the about... popup */
118 static void about_popup(gpointer attribute((unused)) callback_data,
119 guint attribute((unused)) callback_action,
120 GtkWidget attribute((unused)) *menu_item) {
123 gtk_label_set_text(GTK_LABEL(report_label), "getting server version");
124 disorder_eclient_version(client,
125 about_popup_got_version,
129 static void manual_popup(gpointer attribute((unused)) callback_data,
130 guint attribute((unused)) callback_action,
131 GtkWidget attribute((unused)) *menu_item) {
137 /** @brief Called when version arrives, displays about... popup */
138 static void about_popup_got_version(void attribute((unused)) *v,
139 const char attribute((unused)) *err,
142 char *server_version_string;
143 char *short_version_string;
144 GtkWidget *hbox, *vbox, *title;
148 byte_xasprintf(&server_version_string, "Server version %s", value);
149 byte_xasprintf(&short_version_string, "Disobedience %s",
150 disorder_short_version_string);
151 w = gtk_dialog_new_with_buttons("About Disobedience",
152 GTK_WINDOW(toplevel),
154 |GTK_DIALOG_DESTROY_WITH_PARENT),
158 hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*padding*/);
159 vbox = gtk_vbox_new(FALSE/*homogeneous*/, 1/*padding*/);
160 gtk_box_pack_start(GTK_BOX(hbox),
161 gtk_image_new_from_pixbuf(find_image("duck.png")),
165 gtk_box_pack_start(GTK_BOX(vbox),
166 gtk_label_new(short_version_string),
170 gtk_box_pack_start(GTK_BOX(vbox),
171 gtk_label_new(server_version_string),
175 gtk_box_pack_start(GTK_BOX(vbox),
176 gtk_label_new("\xC2\xA9 2004-2008 Richard Kettlewell"),
180 gtk_box_pack_end(GTK_BOX(hbox),
185 title = gtk_label_new(0);
186 gtk_label_set_markup(GTK_LABEL(title),
187 "<span font_desc=\"Sans 36\">Disobedience</span>");
188 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(w)->vbox), title,
192 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(w)->vbox), hbox,
197 gtk_widget_show_all(w);
198 gtk_dialog_run(GTK_DIALOG(w));
199 gtk_widget_destroy(w);
202 /** @brief Set 'Manage Users' menu item sensitivity */
203 void users_set_sensitive(int sensitive) {
204 GtkWidget *w = gtk_item_factory_get_widget(mainmenufactory,
205 "<GdisorderMain>/Server/Manage users");
206 gtk_widget_set_sensitive(w, sensitive);
209 /** @brief Called when our rights change */
210 static void menu_rights_changed(const char attribute((unused)) *event,
211 void attribute((unused)) *eventdata,
212 void attribute((unused)) *callbackdata) {
213 users_set_sensitive(!!(last_rights & RIGHT_ADMIN));
216 /** @brief Create the menu bar widget */
217 GtkWidget *menubar(GtkWidget *w) {
220 static const GtkItemFactoryEntry entries[] = {
222 (char *)"/Server", /* path */
225 0, /* callback_action */
226 (char *)"<Branch>", /* item_type */
230 (char *)"/Server/Login", /* path */
231 (char *)"<CTRL>L", /* accelerator */
232 login, /* callback */
233 0, /* callback_action */
238 (char *)"/Server/Manage users", /* path */
240 users, /* callback */
241 0, /* callback_action */
247 (char *)"/Server/Settings", /* path */
249 settings, /* callback */
250 0, /* callback_action */
256 (char *)"/Server/Quit Disobedience", /* path */
257 (char *)"<CTRL>Q", /* accelerator */
258 quit_program, /* callback */
259 0, /* callback_action */
260 (char *)"<StockItem>", /* item_type */
261 GTK_STOCK_QUIT /* extra_data */
265 (char *)"/Edit", /* path */
268 0, /* callback_action */
269 (char *)"<Branch>", /* item_type */
273 (char *)"/Edit/Select all tracks", /* path */
275 menu_tab_action, /* callback */
276 offsetof(struct tabtype, selectall_activate), /* callback_action */
281 (char *)"/Edit/Deselect all tracks", /* path */
283 menu_tab_action, /* callback */
284 offsetof(struct tabtype, selectnone_activate), /* callback_action */
289 (char *)"/Edit/Track properties", /* path */
291 menu_tab_action, /* callback */
292 offsetof(struct tabtype, properties_activate), /* callback_action */
298 (char *)"/Control", /* path */
301 0, /* callback_action */
302 (char *)"<Branch>", /* item_type */
306 (char *)"/Control/Scratch", /* path */
307 (char *)"<CTRL>S", /* accelerator */
309 0, /* callback_action */
314 (char *)"/Control/Playing", /* path */
315 (char *)"<CTRL>P", /* accelerator */
317 0, /* callback_action */
318 (char *)"<CheckItem>", /* item_type */
322 (char *)"/Control/Random play", /* path */
323 (char *)"<CTRL>R", /* accelerator */
325 0, /* callback_action */
326 (char *)"<CheckItem>", /* item_type */
330 (char *)"/Control/Network player", /* path */
331 (char *)"<CTRL>N", /* accelerator */
333 0, /* callback_action */
334 (char *)"<CheckItem>", /* item_type */
339 (char *)"/Help", /* path */
342 0, /* callback_action */
343 (char *)"<Branch>", /* item_type */
347 (char *)"/Help/Manual page", /* path */
349 manual_popup, /* callback */
350 0, /* callback_action */
355 (char *)"/Help/About DisOrder", /* path */
357 about_popup, /* callback */
358 0, /* callback_action */
359 (char *)"<StockItem>", /* item_type */
360 GTK_STOCK_ABOUT /* extra_data */
364 GtkAccelGroup *accel = gtk_accel_group_new();
367 /* TODO: item factories are deprecated in favour of some XML thing */
368 mainmenufactory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<GdisorderMain>",
370 gtk_item_factory_create_items(mainmenufactory,
371 sizeof entries / sizeof *entries,
372 (GtkItemFactoryEntry *)entries,
374 gtk_window_add_accel_group(GTK_WINDOW(w), accel);
375 selectall_widget = gtk_item_factory_get_widget(mainmenufactory,
376 "<GdisorderMain>/Edit/Select all tracks");
377 selectnone_widget = gtk_item_factory_get_widget(mainmenufactory,
378 "<GdisorderMain>/Edit/Deselect all tracks");
379 properties_widget = gtk_item_factory_get_widget(mainmenufactory,
380 "<GdisorderMain>/Edit/Track properties");
381 assert(selectall_widget != 0);
382 assert(selectnone_widget != 0);
383 assert(properties_widget != 0);
386 GtkWidget *edit_widget = gtk_item_factory_get_widget(mainmenufactory,
387 "<GdisorderMain>/Edit");
388 g_signal_connect(edit_widget, "show", G_CALLBACK(edit_menu_show), 0);
390 event_register("rights-changed", menu_rights_changed, 0);
391 users_set_sensitive(0);
392 m = gtk_item_factory_get_widget(mainmenufactory,