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