chiark / gitweb /
Merge from trunk
[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 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 GtkWidget *selectall_widget;
25 static GtkWidget *selectnone_widget;
26 static GtkWidget *properties_widget;
27 #if PLAYLISTS
28 GtkWidget *playlists_widget;
29 GtkWidget *playlists_menu;
30 GtkWidget *editplaylists_widget;
31 #endif
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-2009 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 #if PLAYLISTS
302     {
303       (char *)"/Edit/Edit playlists",   /* path */
304       0,                                /* accelerator */
305       edit_playlists,                   /* callback */
306       0,                                /* callback_action */
307       0,                                /* item_type */
308       0                                 /* extra_data */
309     },
310 #endif
311     
312     
313     {
314       (char *)"/Control",               /* path */
315       0,                                /* accelerator */
316       0,                                /* callback */
317       0,                                /* callback_action */
318       (char *)"<Branch>",               /* item_type */
319       0                                 /* extra_data */
320     },
321     {
322       (char *)"/Control/Scratch",       /* path */
323       (char *)"<CTRL>S",                /* accelerator */
324       0,                                /* callback */
325       0,                                /* callback_action */
326       0,                                /* item_type */
327       0                                 /* extra_data */
328     },
329     {
330       (char *)"/Control/Playing",       /* path */
331       (char *)"<CTRL>P",                /* accelerator */
332       0,                                /* callback */
333       0,                                /* callback_action */
334       (char *)"<CheckItem>",            /* item_type */
335       0                                 /* extra_data */
336     },
337     {
338       (char *)"/Control/Random play",   /* path */
339       (char *)"<CTRL>R",                /* accelerator */
340       0,                                /* callback */
341       0,                                /* callback_action */
342       (char *)"<CheckItem>",            /* item_type */
343       0                                 /* extra_data */
344     },
345     {
346       (char *)"/Control/Network player", /* path */
347       (char *)"<CTRL>N",                /* accelerator */
348       0,                                /* callback */
349       0,                                /* callback_action */
350       (char *)"<CheckItem>",            /* item_type */
351       0                                 /* extra_data */
352     },
353 #if PLAYLISTS
354     {
355       (char *)"/Control/Activate playlist", /* path */
356       0,                                /* accelerator */
357       0,                                /* callback */
358       0,                                /* callback_action */
359       (char *)"<Branch>",               /* item_type */
360       0                                 /* extra_data */
361     },
362 #endif
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 #if PLAYLISTS
408   playlists_widget = gtk_item_factory_get_item(mainmenufactory,
409                                                "<GdisorderMain>/Control/Activate playlist");
410   playlists_menu = gtk_item_factory_get_widget(mainmenufactory,
411                                                "<GdisorderMain>/Control/Activate playlist");
412   editplaylists_widget = gtk_item_factory_get_widget(mainmenufactory,
413                                                      "<GdisorderMain>/Edit/Edit playlists");
414 #endif
415   assert(selectall_widget != 0);
416   assert(selectnone_widget != 0);
417   assert(properties_widget != 0);
418 #if PLAYLISTS
419   assert(playlists_widget != 0);
420   assert(playlists_menu != 0);
421   assert(editplaylists_widget != 0);
422 #endif
423
424   GtkWidget *edit_widget = gtk_item_factory_get_widget(mainmenufactory,
425                                                        "<GdisorderMain>/Edit");
426   g_signal_connect(edit_widget, "show", G_CALLBACK(edit_menu_show), 0);
427
428   event_register("rights-changed", menu_rights_changed, 0);
429   users_set_sensitive(0);
430   m = gtk_item_factory_get_widget(mainmenufactory,
431                                   "<GdisorderMain>");
432   set_tool_colors(m);
433   return m;
434 }
435
436 /*
437 Local Variables:
438 c-basic-offset:2
439 comment-column:40
440 fill-column:79
441 indent-tabs-mode:nil
442 End:
443 */