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