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