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