chiark / gitweb /
Assume initial digits in a track name are a sort key even without the
[disorder] / disobedience / menu.c
... / ...
CommitLineData
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2006-2008 Richard Kettlewell
4 *
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.
9 *
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.
14 *
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
18 * USA
19 */
20/** @file disobedience/menu.c
21 * @brief Main menu
22 */
23
24#include "disobedience.h"
25
26static GtkWidget *selectall_widget;
27static GtkWidget *selectnone_widget;
28static GtkWidget *properties_widget;
29
30/** @brief Main menu widgets */
31GtkItemFactory *mainmenufactory;
32
33static void about_popup_got_version(void *v,
34 const char *err,
35 const char *value);
36
37/** @brief Called when the quit option is activated
38 *
39 * Just exits.
40 */
41static void quit_program(gpointer attribute((unused)) callback_data,
42 guint attribute((unused)) callback_action,
43 GtkWidget attribute((unused)) *menu_item) {
44 D(("quit_program"));
45 exit(0);
46}
47
48/** @brief Called when an edit menu item is selected
49 *
50 * Shared by several menu items; callback_action is expected to be the offset
51 * of the activate member of struct tabtype.
52 */
53static void menu_tab_action(gpointer attribute((unused)) callback_data,
54 guint callback_action,
55 GtkWidget attribute((unused)) *menu_item) {
56 GtkWidget *tab = gtk_notebook_get_nth_page
57 (GTK_NOTEBOOK(tabs), gtk_notebook_current_page(GTK_NOTEBOOK(tabs)));
58 const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
59
60 void (**activatep)(GtkMenuItem *, gpointer)
61 = (void *)((const char *)t + callback_action);
62 void (*activate)(GtkMenuItem *, gpointer) = *activatep;
63
64 if(activate)
65 activate(NULL, t->extra);
66}
67
68/** @brief Called when the login option is activated */
69static void login(gpointer attribute((unused)) callback_data,
70 guint attribute((unused)) callback_action,
71 GtkWidget attribute((unused)) *menu_item) {
72 login_box();
73}
74
75/** @brief Called when the login option is activated */
76static void users(gpointer attribute((unused)) callback_data,
77 guint attribute((unused)) callback_action,
78 GtkWidget attribute((unused)) *menu_item) {
79 manage_users();
80}
81
82#if 0
83/** @brief Called when the settings option is activated */
84static void settings(gpointer attribute((unused)) callback_data,
85 guint attribute((unused)) callback_action,
86 GtkWidget attribute((unused)) *menu_item) {
87 popup_settings();
88}
89#endif
90
91/** @brief Called when edit menu is shown
92 *
93 * Determines option sensitivity according to the current tab and adjusts the
94 * widgets accordingly. Knows about @ref DISORDER_CONNECTED so the callbacks
95 * need not.
96 */
97static void edit_menu_show(GtkWidget attribute((unused)) *widget,
98 gpointer attribute((unused)) user_data) {
99 if(tabs) {
100 GtkWidget *tab = gtk_notebook_get_nth_page
101 (GTK_NOTEBOOK(tabs),
102 gtk_notebook_current_page(GTK_NOTEBOOK(tabs)));
103 const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
104
105 assert(t != 0);
106 gtk_widget_set_sensitive(properties_widget,
107 (t->properties_sensitive
108 && t->properties_sensitive(t->extra)
109 && (disorder_eclient_state(client) & DISORDER_CONNECTED)));
110 gtk_widget_set_sensitive(selectall_widget,
111 t->selectall_sensitive
112 && t->selectall_sensitive(t->extra));
113 gtk_widget_set_sensitive(selectnone_widget,
114 t->selectnone_sensitive
115 && t->selectnone_sensitive(t->extra));
116 }
117}
118
119/** @brief Fetch version in order to display the about... popup */
120static void about_popup(gpointer attribute((unused)) callback_data,
121 guint attribute((unused)) callback_action,
122 GtkWidget attribute((unused)) *menu_item) {
123 D(("about_popup"));
124
125 gtk_label_set_text(GTK_LABEL(report_label), "getting server version");
126 disorder_eclient_version(client,
127 about_popup_got_version,
128 0);
129}
130
131static void manual_popup(gpointer attribute((unused)) callback_data,
132 guint attribute((unused)) callback_action,
133 GtkWidget attribute((unused)) *menu_item) {
134 D(("manual_popup"));
135
136 popup_help();
137}
138
139/** @brief Called when version arrives, displays about... popup */
140static void about_popup_got_version(void attribute((unused)) *v,
141 const char attribute((unused)) *err,
142 const char *value) {
143 GtkWidget *w;
144 char *server_version_string;
145 char *short_version_string;
146 GtkWidget *hbox, *vbox, *title;
147
148 if(!value)
149 value = "[error]";
150 byte_xasprintf(&server_version_string, "Server version %s", value);
151 byte_xasprintf(&short_version_string, "Disobedience %s",
152 disorder_short_version_string);
153 w = gtk_dialog_new_with_buttons("About Disobedience",
154 GTK_WINDOW(toplevel),
155 (GTK_DIALOG_MODAL
156 |GTK_DIALOG_DESTROY_WITH_PARENT),
157 GTK_STOCK_OK,
158 GTK_RESPONSE_ACCEPT,
159 (char *)NULL);
160 hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*padding*/);
161 vbox = gtk_vbox_new(FALSE/*homogeneous*/, 1/*padding*/);
162 gtk_box_pack_start(GTK_BOX(hbox),
163 gtk_image_new_from_pixbuf(find_image("duck.png")),
164 FALSE/*expand*/,
165 FALSE/*fill*/,
166 4/*padding*/);
167 gtk_box_pack_start(GTK_BOX(vbox),
168 gtk_label_new(short_version_string),
169 FALSE/*expand*/,
170 FALSE/*fill*/,
171 1/*padding*/);
172 gtk_box_pack_start(GTK_BOX(vbox),
173 gtk_label_new(server_version_string),
174 FALSE/*expand*/,
175 FALSE/*fill*/,
176 1/*padding*/);
177 gtk_box_pack_start(GTK_BOX(vbox),
178 gtk_label_new("\xC2\xA9 2004-2008 Richard Kettlewell"),
179 FALSE/*expand*/,
180 FALSE/*fill*/,
181 1/*padding*/);
182 gtk_box_pack_end(GTK_BOX(hbox),
183 vbox,
184 FALSE/*expand*/,
185 FALSE/*fill*/,
186 0/*padding*/);
187 title = gtk_label_new(0);
188 gtk_label_set_markup(GTK_LABEL(title),
189 "<span font_desc=\"Sans 36\">Disobedience</span>");
190 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(w)->vbox), title,
191 FALSE/*expand*/,
192 FALSE/*fill*/,
193 0/*padding*/);
194 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(w)->vbox), hbox,
195 FALSE/*expand*/,
196 FALSE/*fill*/,
197 0/*padding*/);
198 set_tool_colors(w);
199 gtk_widget_show_all(w);
200 gtk_dialog_run(GTK_DIALOG(w));
201 gtk_widget_destroy(w);
202}
203
204/** @brief Set 'Manage Users' menu item sensitivity */
205void users_set_sensitive(int sensitive) {
206 GtkWidget *w = gtk_item_factory_get_widget(mainmenufactory,
207 "<GdisorderMain>/Server/Manage users");
208 gtk_widget_set_sensitive(w, sensitive);
209}
210
211/** @brief Called when our rights change */
212static void menu_rights_changed(const char attribute((unused)) *event,
213 void attribute((unused)) *eventdata,
214 void attribute((unused)) *callbackdata) {
215 users_set_sensitive(!!(last_rights & RIGHT_ADMIN));
216}
217
218/** @brief Create the menu bar widget */
219GtkWidget *menubar(GtkWidget *w) {
220 GtkWidget *m;
221
222 static const GtkItemFactoryEntry entries[] = {
223 {
224 (char *)"/Server", /* path */
225 0, /* accelerator */
226 0, /* callback */
227 0, /* callback_action */
228 (char *)"<Branch>", /* item_type */
229 0 /* extra_data */
230 },
231 {
232 (char *)"/Server/Login", /* path */
233 (char *)"<CTRL>L", /* accelerator */
234 login, /* callback */
235 0, /* callback_action */
236 0, /* item_type */
237 0 /* extra_data */
238 },
239 {
240 (char *)"/Server/Manage users", /* path */
241 0, /* accelerator */
242 users, /* callback */
243 0, /* callback_action */
244 0, /* item_type */
245 0 /* extra_data */
246 },
247#if 0
248 {
249 (char *)"/Server/Settings", /* path */
250 0, /* accelerator */
251 settings, /* callback */
252 0, /* callback_action */
253 0, /* item_type */
254 0 /* extra_data */
255 },
256#endif
257 {
258 (char *)"/Server/Quit Disobedience", /* path */
259 (char *)"<CTRL>Q", /* accelerator */
260 quit_program, /* callback */
261 0, /* callback_action */
262 (char *)"<StockItem>", /* item_type */
263 GTK_STOCK_QUIT /* extra_data */
264 },
265
266 {
267 (char *)"/Edit", /* path */
268 0, /* accelerator */
269 0, /* callback */
270 0, /* callback_action */
271 (char *)"<Branch>", /* item_type */
272 0 /* extra_data */
273 },
274 {
275 (char *)"/Edit/Select all tracks", /* path */
276 0, /* accelerator */
277 menu_tab_action, /* callback */
278 offsetof(struct tabtype, selectall_activate), /* callback_action */
279 0, /* item_type */
280 0 /* extra_data */
281 },
282 {
283 (char *)"/Edit/Deselect all tracks", /* path */
284 0, /* accelerator */
285 menu_tab_action, /* callback */
286 offsetof(struct tabtype, selectnone_activate), /* callback_action */
287 0, /* item_type */
288 0 /* extra_data */
289 },
290 {
291 (char *)"/Edit/Track properties", /* path */
292 0, /* accelerator */
293 menu_tab_action, /* callback */
294 offsetof(struct tabtype, properties_activate), /* callback_action */
295 0, /* item_type */
296 0 /* extra_data */
297 },
298
299 {
300 (char *)"/Control", /* path */
301 0, /* accelerator */
302 0, /* callback */
303 0, /* callback_action */
304 (char *)"<Branch>", /* item_type */
305 0 /* extra_data */
306 },
307 {
308 (char *)"/Control/Scratch", /* path */
309 (char *)"<CTRL>S", /* accelerator */
310 0, /* callback */
311 0, /* callback_action */
312 0, /* item_type */
313 0 /* extra_data */
314 },
315 {
316 (char *)"/Control/Playing", /* path */
317 (char *)"<CTRL>P", /* accelerator */
318 0, /* callback */
319 0, /* callback_action */
320 (char *)"<CheckItem>", /* item_type */
321 0 /* extra_data */
322 },
323 {
324 (char *)"/Control/Random play", /* path */
325 (char *)"<CTRL>R", /* accelerator */
326 0, /* callback */
327 0, /* callback_action */
328 (char *)"<CheckItem>", /* item_type */
329 0 /* extra_data */
330 },
331 {
332 (char *)"/Control/Network player", /* path */
333 (char *)"<CTRL>N", /* accelerator */
334 0, /* callback */
335 0, /* callback_action */
336 (char *)"<CheckItem>", /* item_type */
337 0 /* extra_data */
338 },
339
340 {
341 (char *)"/Help", /* path */
342 0, /* accelerator */
343 0, /* callback */
344 0, /* callback_action */
345 (char *)"<Branch>", /* item_type */
346 0 /* extra_data */
347 },
348 {
349 (char *)"/Help/Manual page", /* path */
350 0, /* accelerator */
351 manual_popup, /* callback */
352 0, /* callback_action */
353 0, /* item_type */
354 0 /* extra_data */
355 },
356 {
357 (char *)"/Help/About DisOrder", /* path */
358 0, /* accelerator */
359 about_popup, /* callback */
360 0, /* callback_action */
361 (char *)"<StockItem>", /* item_type */
362 GTK_STOCK_ABOUT /* extra_data */
363 },
364 };
365
366 GtkAccelGroup *accel = gtk_accel_group_new();
367
368 D(("add_menubar"));
369 /* TODO: item factories are deprecated in favour of some XML thing */
370 mainmenufactory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<GdisorderMain>",
371 accel);
372 gtk_item_factory_create_items(mainmenufactory,
373 sizeof entries / sizeof *entries,
374 (GtkItemFactoryEntry *)entries,
375 0);
376 gtk_window_add_accel_group(GTK_WINDOW(w), accel);
377 selectall_widget = gtk_item_factory_get_widget(mainmenufactory,
378 "<GdisorderMain>/Edit/Select all tracks");
379 selectnone_widget = gtk_item_factory_get_widget(mainmenufactory,
380 "<GdisorderMain>/Edit/Deselect all tracks");
381 properties_widget = gtk_item_factory_get_widget(mainmenufactory,
382 "<GdisorderMain>/Edit/Track properties");
383 assert(selectall_widget != 0);
384 assert(selectnone_widget != 0);
385 assert(properties_widget != 0);
386
387
388 GtkWidget *edit_widget = gtk_item_factory_get_widget(mainmenufactory,
389 "<GdisorderMain>/Edit");
390 g_signal_connect(edit_widget, "show", G_CALLBACK(edit_menu_show), 0);
391
392 event_register("rights-changed", menu_rights_changed, 0);
393 users_set_sensitive(0);
394 m = gtk_item_factory_get_widget(mainmenufactory,
395 "<GdisorderMain>");
396 set_tool_colors(m);
397 return m;
398}
399
400/*
401Local Variables:
402c-basic-offset:2
403comment-column:40
404fill-column:79
405indent-tabs-mode:nil
406End:
407*/