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