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