chiark / gitweb /
Move playlist menu code to playlists.c
[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
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 Create the menu bar widget */
221 GtkWidget *menubar(GtkWidget *w) {
222   GtkWidget *m;
223
224   static const GtkItemFactoryEntry entries[] = {
225     {
226       (char *)"/Server",                /* path */
227       0,                                /* accelerator */
228       0,                                /* callback */
229       0,                                /* callback_action */
230       (char *)"<Branch>",               /* item_type */
231       0                                 /* extra_data */
232     },
233     { 
234       (char *)"/Server/Login",          /* path */
235       (char *)"<CTRL>L",                /* accelerator */
236       login,                            /* callback */
237       0,                                /* callback_action */
238       0,                                /* item_type */
239       0                                 /* extra_data */
240     },
241     { 
242       (char *)"/Server/Manage users",   /* path */
243       0,                                /* accelerator */
244       users,                            /* callback */
245       0,                                /* callback_action */
246       0,                                /* item_type */
247       0                                 /* extra_data */
248     },
249 #if 0
250     {
251       (char *)"/Server/Settings",       /* path */
252       0,                                /* accelerator */
253       settings,                         /* callback */
254       0,                                /* callback_action */
255       0,                                /* item_type */
256       0                                 /* extra_data */
257     },
258 #endif
259     {
260       (char *)"/Server/Quit Disobedience", /* path */
261       (char *)"<CTRL>Q",                /* accelerator */
262       quit_program,                     /* callback */
263       0,                                /* callback_action */
264       (char *)"<StockItem>",            /* item_type */
265       GTK_STOCK_QUIT                    /* extra_data */
266     },
267     
268     {
269       (char *)"/Edit",                  /* path */
270       0,                                /* accelerator */
271       0,                                /* callback */
272       0,                                /* callback_action */
273       (char *)"<Branch>",               /* item_type */
274       0                                 /* extra_data */
275     },
276     {
277       (char *)"/Edit/Select all tracks", /* path */
278       0,                                /* accelerator */
279       menu_tab_action,                  /* callback */
280       offsetof(struct tabtype, selectall_activate), /* callback_action */
281       0,                                /* item_type */
282       0                                 /* extra_data */
283     },
284     {
285       (char *)"/Edit/Deselect all tracks", /* path */
286       0,                                /* accelerator */
287       menu_tab_action,                  /* callback */
288       offsetof(struct tabtype, selectnone_activate), /* callback_action */
289       0,                                /* item_type */
290       0                                 /* extra_data */
291     },
292     {
293       (char *)"/Edit/Track properties", /* path */
294       0,                                /* accelerator */
295       menu_tab_action,                  /* callback */
296       offsetof(struct tabtype, properties_activate), /* callback_action */
297       0,                                /* item_type */
298       0                                 /* extra_data */
299     },
300     {
301       (char *)"/Edit/Edit playlists",   /* path */
302       0,                                /* accelerator */
303       edit_playlists,                   /* callback */
304       0,                                /* callback_action */
305       0,                                /* item_type */
306       0                                 /* extra_data */
307     },
308     
309     
310     {
311       (char *)"/Control",               /* path */
312       0,                                /* accelerator */
313       0,                                /* callback */
314       0,                                /* callback_action */
315       (char *)"<Branch>",               /* item_type */
316       0                                 /* extra_data */
317     },
318     {
319       (char *)"/Control/Scratch",       /* path */
320       (char *)"<CTRL>S",                /* accelerator */
321       0,                                /* callback */
322       0,                                /* callback_action */
323       0,                                /* item_type */
324       0                                 /* extra_data */
325     },
326     {
327       (char *)"/Control/Playing",       /* path */
328       (char *)"<CTRL>P",                /* accelerator */
329       0,                                /* callback */
330       0,                                /* callback_action */
331       (char *)"<CheckItem>",            /* item_type */
332       0                                 /* extra_data */
333     },
334     {
335       (char *)"/Control/Random play",   /* path */
336       (char *)"<CTRL>R",                /* accelerator */
337       0,                                /* callback */
338       0,                                /* callback_action */
339       (char *)"<CheckItem>",            /* item_type */
340       0                                 /* extra_data */
341     },
342     {
343       (char *)"/Control/Network player", /* path */
344       (char *)"<CTRL>N",                /* accelerator */
345       0,                                /* callback */
346       0,                                /* callback_action */
347       (char *)"<CheckItem>",            /* item_type */
348       0                                 /* extra_data */
349     },
350     {
351       (char *)"/Control/Activate playlist", /* path */
352       0,                                /* accelerator */
353       0,                                /* callback */
354       0,                                /* callback_action */
355       (char *)"<Branch>",               /* item_type */
356       0                                 /* extra_data */
357     },
358     
359     {
360       (char *)"/Help",                  /* path */
361       0,                                /* accelerator */
362       0,                                /* callback */
363       0,                                /* callback_action */
364       (char *)"<Branch>",               /* item_type */
365       0                                 /* extra_data */
366     },
367     {
368       (char *)"/Help/Manual page",      /* path */
369       0,                                /* accelerator */
370       manual_popup,                     /* callback */
371       0,                                /* callback_action */
372       0,                                /* item_type */
373       0                                 /* extra_data */
374     },
375     {
376       (char *)"/Help/About DisOrder",   /* path */
377       0,                                /* accelerator */
378       about_popup,                      /* callback */
379       0,                                /* callback_action */
380       (char *)"<StockItem>",            /* item_type */
381       GTK_STOCK_ABOUT                   /* extra_data */
382     },
383   };
384
385   GtkAccelGroup *accel = gtk_accel_group_new();
386
387   D(("add_menubar"));
388   /* TODO: item factories are deprecated in favour of some XML thing */
389   mainmenufactory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<GdisorderMain>",
390                                          accel);
391   gtk_item_factory_create_items(mainmenufactory,
392                                 sizeof entries / sizeof *entries,
393                                 (GtkItemFactoryEntry *)entries,
394                                 0);
395   gtk_window_add_accel_group(GTK_WINDOW(w), accel);
396   selectall_widget = gtk_item_factory_get_widget(mainmenufactory,
397                                                  "<GdisorderMain>/Edit/Select all tracks");
398   selectnone_widget = gtk_item_factory_get_widget(mainmenufactory,
399                                                  "<GdisorderMain>/Edit/Deselect all tracks");
400   properties_widget = gtk_item_factory_get_widget(mainmenufactory,
401                                                   "<GdisorderMain>/Edit/Track properties");
402   playlists_widget = gtk_item_factory_get_item(mainmenufactory,
403                                                "<GdisorderMain>/Control/Activate playlist");
404   playlists_menu = gtk_item_factory_get_widget(mainmenufactory,
405                                                "<GdisorderMain>/Control/Activate playlist");
406   assert(selectall_widget != 0);
407   assert(selectnone_widget != 0);
408   assert(properties_widget != 0);
409   assert(playlists_widget != 0);
410   assert(playlists_menu != 0);
411
412   GtkWidget *edit_widget = gtk_item_factory_get_widget(mainmenufactory,
413                                                        "<GdisorderMain>/Edit");
414   g_signal_connect(edit_widget, "show", G_CALLBACK(edit_menu_show), 0);
415
416   event_register("rights-changed", menu_rights_changed, 0);
417   users_set_sensitive(0);
418   m = gtk_item_factory_get_widget(mainmenufactory,
419                                   "<GdisorderMain>");
420   set_tool_colors(m);
421   return m;
422 }
423
424 /*
425 Local Variables:
426 c-basic-offset:2
427 comment-column:40
428 fill-column:79
429 indent-tabs-mode:nil
430 End:
431 */