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