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