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