chiark / gitweb /
More event_*
[disorder] / disobedience / menu.c
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
26 static GtkWidget *selectall_widget;
27 static GtkWidget *selectnone_widget;
28 static GtkWidget *properties_widget;
29
30 /** @brief Main menu widgets */
31 GtkItemFactory *mainmenufactory;
32
33 static void about_popup_got_version(void *v,
34                                     const char *error,
35                                     const char *value);
36
37 /** @brief Called when the quit option is activated
38  *
39  * Just exits.
40  */
41 static 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 */
49
50 /** @brief Called when the select all option is activated
51  *
52  * Calls the per-tab select all function.
53  */
54 static 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
64 /** @brief Called when the select none option is activated
65  *
66  * Calls the per-tab select none function.
67  */
68 static 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
78 /** @brief Called when the track properties option is activated
79  *
80  * Calls the per-tab properties function.
81  */
82 static 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
92 /** @brief Called when the login option is activated */
93 static void login(gpointer attribute((unused)) callback_data,
94                   guint attribute((unused)) callback_action,
95                   GtkWidget attribute((unused)) *menu_item) {
96   login_box();
97 }
98
99 /** @brief Called when the login option is activated */
100 static void users(gpointer attribute((unused)) callback_data,
101                   guint attribute((unused)) callback_action,
102                   GtkWidget attribute((unused)) *menu_item) {
103   manage_users();
104 }
105
106 #if 0
107 /** @brief Called when the settings option is activated */
108 static 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
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  */
121 void menu_update(int page) {
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   }
137 }
138    
139 /** @brief Fetch version in order to display the about... popup */
140 static 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
151 static 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
159 /** @brief Called when version arrives, displays about... popup */
160 static void about_popup_got_version(void attribute((unused)) *v,
161                                     const char attribute((unused)) *error,
162                                     const char *value) {
163   GtkWidget *w;
164   char *server_version_string;
165   char *short_version_string;
166   GtkWidget *hbox, *vbox, *title;
167
168   if(!value)
169     value = "[error]";
170   byte_xasprintf(&server_version_string, "Server version %s", value);
171   byte_xasprintf(&short_version_string, "Disobedience %s",
172                  disorder_short_version_string);
173   w = gtk_dialog_new_with_buttons("About Disobedience",
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);
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),
188                      gtk_label_new(short_version_string),
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),
198                      gtk_label_new("\xC2\xA9 2004-2008 Richard Kettlewell"),
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*/);
218   set_tool_colors(w);
219   gtk_widget_show_all(w);
220   gtk_dialog_run(GTK_DIALOG(w));
221   gtk_widget_destroy(w);
222 }
223
224 /** @brief Set 'Manage Users' menu item sensitivity */
225 void 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 */
232 static void menu_got_rights(void attribute((unused)) *v,
233                             const char *error,
234                             const char *value) {
235   rights_type r;
236
237   if(error) {
238     popup_protocol_error(0, error);
239     r = 0;
240   } else {
241     if(parse_rights(value, &r, 0))
242       r = 0;
243   }
244   users_set_sensitive(!!(r & RIGHT_ADMIN));
245 }
246
247 /** @brief Called when we need to reset state */
248 static void menu_reset(void) {
249   users_set_sensitive(0);               /* until we know better */
250   disorder_eclient_userinfo(client, menu_got_rights, config->username, "rights",
251                             0);
252 }
253
254 /** @brief Create the menu bar widget */
255 GtkWidget *menubar(GtkWidget *w) {
256   GtkWidget *m;
257
258   static const GtkItemFactoryEntry entries[] = {
259     {
260       (char *)"/Server",                /* path */
261       0,                                /* accelerator */
262       0,                                /* callback */
263       0,                                /* callback_action */
264       (char *)"<Branch>",               /* item_type */
265       0                                 /* extra_data */
266     },
267     { 
268       (char *)"/Server/Login",          /* path */
269       (char *)"<CTRL>L",                /* accelerator */
270       login,                            /* callback */
271       0,                                /* callback_action */
272       0,                                /* item_type */
273       0                                 /* extra_data */
274     },
275     { 
276       (char *)"/Server/Manage users",   /* path */
277       0,                                /* accelerator */
278       users,                            /* callback */
279       0,                                /* callback_action */
280       0,                                /* item_type */
281       0                                 /* extra_data */
282     },
283 #if 0
284     {
285       (char *)"/Server/Settings",       /* path */
286       0,                                /* accelerator */
287       settings,                         /* callback */
288       0,                                /* callback_action */
289       0,                                /* item_type */
290       0                                 /* extra_data */
291     },
292 #endif
293     {
294       (char *)"/Server/Quit Disobedience", /* path */
295       (char *)"<CTRL>Q",                /* accelerator */
296       quit_program,                     /* callback */
297       0,                                /* callback_action */
298       (char *)"<StockItem>",            /* item_type */
299       GTK_STOCK_QUIT                    /* extra_data */
300     },
301     
302     {
303       (char *)"/Edit",                  /* path */
304       0,                                /* accelerator */
305       0,                                /* callback */
306       0,                                /* callback_action */
307       (char *)"<Branch>",               /* item_type */
308       0                                 /* extra_data */
309     },
310     {
311       (char *)"/Edit/Select all tracks", /* path */
312       (char *)"<CTRL>A",                /* accelerator */
313       select_all,                       /* callback */
314       0,                                /* callback_action */
315       0,                                /* item_type */
316       0                                 /* extra_data */
317     },
318     {
319       (char *)"/Edit/Deselect all tracks", /* path */
320       (char *)"<CTRL><SHIFT>A",         /* accelerator */
321       select_none,                      /* callback */
322       0,                                /* callback_action */
323       0,                                /* item_type */
324       0                                 /* extra_data */
325     },
326     {
327       (char *)"/Edit/Track properties", /* path */
328       0,                                /* accelerator */
329       properties_item,                  /* callback */
330       0,                                /* callback_action */
331       0,                                /* item_type */
332       0                                 /* extra_data */
333     },
334     
335     {
336       (char *)"/Control",               /* path */
337       0,                                /* accelerator */
338       0,                                /* callback */
339       0,                                /* callback_action */
340       (char *)"<Branch>",               /* item_type */
341       0                                 /* extra_data */
342     },
343     {
344       (char *)"/Control/Scratch",       /* path */
345       (char *)"<CTRL>S",                /* accelerator */
346       0,                                /* callback */
347       0,                                /* callback_action */
348       0,                                /* item_type */
349       0                                 /* extra_data */
350     },
351     {
352       (char *)"/Control/Playing",       /* path */
353       (char *)"<CTRL>P",                /* accelerator */
354       0,                                /* callback */
355       0,                                /* callback_action */
356       (char *)"<CheckItem>",            /* item_type */
357       0                                 /* extra_data */
358     },
359     {
360       (char *)"/Control/Random play",   /* path */
361       (char *)"<CTRL>R",                /* accelerator */
362       0,                                /* callback */
363       0,                                /* callback_action */
364       (char *)"<CheckItem>",            /* item_type */
365       0                                 /* extra_data */
366     },
367     {
368       (char *)"/Control/Network player", /* path */
369       (char *)"<CTRL>N",                /* accelerator */
370       0,                                /* callback */
371       0,                                /* callback_action */
372       (char *)"<CheckItem>",            /* item_type */
373       0                                 /* extra_data */
374     },
375     
376     {
377       (char *)"/Help",                  /* path */
378       0,                                /* accelerator */
379       0,                                /* callback */
380       0,                                /* callback_action */
381       (char *)"<Branch>",               /* item_type */
382       0                                 /* extra_data */
383     },
384     {
385       (char *)"/Help/Manual page",      /* path */
386       0,                                /* accelerator */
387       manual_popup,                     /* callback */
388       0,                                /* callback_action */
389       0,                                /* item_type */
390       0                                 /* extra_data */
391     },
392     {
393       (char *)"/Help/About DisOrder",   /* path */
394       0,                                /* accelerator */
395       about_popup,                      /* callback */
396       0,                                /* callback_action */
397       (char *)"<StockItem>",            /* item_type */
398       GTK_STOCK_ABOUT                   /* extra_data */
399     },
400   };
401
402   GtkAccelGroup *accel = gtk_accel_group_new();
403
404   D(("add_menubar"));
405   /* TODO: item factories are deprecated in favour of some XML thing */
406   mainmenufactory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<GdisorderMain>",
407                                          accel);
408   gtk_item_factory_create_items(mainmenufactory,
409                                 sizeof entries / sizeof *entries,
410                                 (GtkItemFactoryEntry *)entries,
411                                 0);
412   gtk_window_add_accel_group(GTK_WINDOW(w), accel);
413   selectall_widget = gtk_item_factory_get_widget(mainmenufactory,
414                                                  "<GdisorderMain>/Edit/Select all tracks");
415   selectnone_widget = gtk_item_factory_get_widget(mainmenufactory,
416                                                  "<GdisorderMain>/Edit/Deselect all tracks");
417   properties_widget = gtk_item_factory_get_widget(mainmenufactory,
418                                                   "<GdisorderMain>/Edit/Track properties");
419   assert(selectall_widget != 0);
420   assert(selectnone_widget != 0);
421   assert(properties_widget != 0);
422   register_reset(menu_reset);
423   menu_reset();
424   m = gtk_item_factory_get_widget(mainmenufactory,
425                                   "<GdisorderMain>");
426   set_tool_colors(m);
427   return m;
428 }
429
430 /*
431 Local Variables:
432 c-basic-offset:2
433 comment-column:40
434 fill-column:79
435 indent-tabs-mode:nil
436 End:
437 */