chiark / gitweb /
3ebba2f8022a9b539c18af59a1abb34afeb1b737
[disorder] / disobedience / menu.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2006, 2007 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 *properties_widget;
28
29 /** @brief Main menu widgets */
30 GtkItemFactory *mainmenufactory;
31
32 static void about_popup_got_version(void *v, const char *value);
33
34 /** @brief Called when the quit option is activated
35  *
36  * Just exits.
37  */
38 static void quit_program(gpointer attribute((unused)) callback_data,
39                          guint attribute((unused)) callback_action,
40                          GtkWidget attribute((unused)) *menu_item) {
41   D(("quit_program"));
42   exit(0);
43 }
44
45 /* TODO can we have a single parameterized callback for all these */
46
47 /** @brief Called when the select all option is activated
48  *
49  * Calls the per-tab select all function.
50  */
51 static void select_all(gpointer attribute((unused)) callback_data,
52                        guint attribute((unused)) 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   t->selectall_activate(tab);
59 }
60
61 /** @brief Called when the track properties option is activated
62  *
63  * Calls the per-tab properties function.
64  */
65 static void properties_item(gpointer attribute((unused)) callback_data,
66                             guint attribute((unused)) callback_action,
67                             GtkWidget attribute((unused)) *menu_item) {
68   GtkWidget *tab = gtk_notebook_get_nth_page
69     (GTK_NOTEBOOK(tabs), gtk_notebook_current_page(GTK_NOTEBOOK(tabs)));
70   const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
71
72   t->properties_activate(tab);
73 }
74
75 /** @brief Called when the login option is activated */
76 static void login(gpointer attribute((unused)) callback_data,
77                   guint attribute((unused)) callback_action,
78                   GtkWidget attribute((unused)) *menu_item) {
79   login_box();
80 }
81
82 /** @brief Update menu state
83  *
84  * Determines option sensitivity according to the current tab and adjusts the
85  * widgets accordingly.  Knows about @ref DISORDER_CONNECTED so the callbacks
86  * need not.
87  */
88 void menu_update(int page) {
89   GtkWidget *tab = gtk_notebook_get_nth_page
90     (GTK_NOTEBOOK(tabs),
91      page < 0 ? gtk_notebook_current_page(GTK_NOTEBOOK(tabs)) : page);
92   const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
93
94   assert(t != 0);
95   gtk_widget_set_sensitive(properties_widget,
96                            (t->properties_sensitive(tab)
97                             && (disorder_eclient_state(client) & DISORDER_CONNECTED)));
98   gtk_widget_set_sensitive(selectall_widget,
99                            t->selectall_sensitive(tab));
100 }
101    
102 /** @brief Fetch version in order to display the about... popup */
103 static void about_popup(gpointer attribute((unused)) callback_data,
104                         guint attribute((unused)) callback_action,
105                         GtkWidget attribute((unused)) *menu_item) {
106   D(("about_popup"));
107
108   gtk_label_set_text(GTK_LABEL(report_label), "getting server version");
109   disorder_eclient_version(client,
110                            about_popup_got_version,
111                            0);
112 }
113
114 static void manual_popup(gpointer attribute((unused)) callback_data,
115                        guint attribute((unused)) callback_action,
116                        GtkWidget attribute((unused)) *menu_item) {
117   D(("manual_popup"));
118
119   popup_help();
120 }
121
122 /** @brief Callde when version arrives, displays about... popup */
123 static void about_popup_got_version(void attribute((unused)) *v,
124                                     const char *value) {
125   GtkWidget *w;
126   char *server_version_string;
127
128   byte_xasprintf(&server_version_string, "Server version %s", value);
129   w = gtk_dialog_new_with_buttons("About DisOrder",
130                                   GTK_WINDOW(toplevel),
131                                   (GTK_DIALOG_MODAL
132                                    |GTK_DIALOG_DESTROY_WITH_PARENT),
133                                   GTK_STOCK_OK,
134                                   GTK_RESPONSE_ACCEPT,
135                                   (char *)NULL);
136   gtk_container_add(GTK_CONTAINER(GTK_DIALOG(w)->vbox),
137                     gtk_label_new("DisOrder client " VERSION));
138   gtk_container_add(GTK_CONTAINER(GTK_DIALOG(w)->vbox),
139                     gtk_label_new(server_version_string));
140   gtk_container_add(GTK_CONTAINER(GTK_DIALOG(w)->vbox),
141                     gtk_label_new("(c) 2004-2007 Richard Kettlewell"));
142   set_tool_colors(w);
143   gtk_widget_show_all(w);
144   gtk_dialog_run(GTK_DIALOG(w));
145   gtk_widget_destroy(w);
146 }
147
148 /** @brief Create the menu bar widget */
149 GtkWidget *menubar(GtkWidget *w) {
150   GtkWidget *m;
151
152   static const GtkItemFactoryEntry entries[] = {
153     {
154       (char *)"/File",                  /* path */
155       0,                                /* accelerator */
156       0,                                /* callback */
157       0,                                /* callback_action */
158       (char *)"<Branch>",               /* item_type */
159       0                                 /* extra_data */
160     },
161     { 
162       (char *)"/File/Login",            /* path */
163       (char *)"<CTRL>L",                /* accelerator */
164       login,                            /* callback */
165       0,                                /* callback_action */
166       0,                                /* item_type */
167       0                                 /* extra_data */
168     },
169     {
170       (char *)"/File/Quit Disobedience", /* path */
171       (char *)"<CTRL>Q",                /* accelerator */
172       quit_program,                     /* callback */
173       0,                                /* callback_action */
174       (char *)"<StockItem>",            /* item_type */
175       GTK_STOCK_QUIT                    /* extra_data */
176     },
177     
178     {
179       (char *)"/Edit",                  /* path */
180       0,                                /* accelerator */
181       0,                                /* callback */
182       0,                                /* callback_action */
183       (char *)"<Branch>",               /* item_type */
184       0                                 /* extra_data */
185     },
186     {
187       (char *)"/Edit/Select all tracks", /* path */
188       (char *)"<CTRL>A",                /* accelerator */
189       select_all,                       /* callback */
190       0,                                /* callback_action */
191       0,                                /* item_type */
192       0                                 /* extra_data */
193     },
194     {
195       (char *)"/Edit/Track properties", /* path */
196       0,                                /* accelerator */
197       properties_item,                  /* callback */
198       0,                                /* callback_action */
199       0,                                /* item_type */
200       0                                 /* extra_data */
201     },
202     
203     {
204       (char *)"/Control",               /* path */
205       0,                                /* accelerator */
206       0,                                /* callback */
207       0,                                /* callback_action */
208       (char *)"<Branch>",               /* item_type */
209       0                                 /* extra_data */
210     },
211     {
212       (char *)"/Control/Scratch",       /* path */
213       (char *)"<CTRL>S",                /* accelerator */
214       0,                                /* callback */
215       0,                                /* callback_action */
216       0,                                /* item_type */
217       0                                 /* extra_data */
218     },
219     {
220       (char *)"/Control/Playing",       /* path */
221       (char *)"<CTRL>P",                /* accelerator */
222       0,                                /* callback */
223       0,                                /* callback_action */
224       (char *)"<CheckItem>",            /* item_type */
225       0                                 /* extra_data */
226     },
227     {
228       (char *)"/Control/Random play",   /* path */
229       (char *)"<CTRL>R",                /* accelerator */
230       0,                                /* callback */
231       0,                                /* callback_action */
232       (char *)"<CheckItem>",            /* item_type */
233       0                                 /* extra_data */
234     },
235     {
236       (char *)"/Control/Network player", /* path */
237       (char *)"<CTRL>N",                /* accelerator */
238       0,                                /* callback */
239       0,                                /* callback_action */
240       (char *)"<CheckItem>",            /* item_type */
241       0                                 /* extra_data */
242     },
243     
244     {
245       (char *)"/Help",                  /* path */
246       0,                                /* accelerator */
247       0,                                /* callback */
248       0,                                /* callback_action */
249       (char *)"<Branch>",               /* item_type */
250       0                                 /* extra_data */
251     },
252     {
253       (char *)"/Help/Manual page",      /* path */
254       0,                                /* accelerator */
255       manual_popup,                     /* callback */
256       0,                                /* callback_action */
257       0,                                /* item_type */
258       0                                 /* extra_data */
259     },
260     {
261       (char *)"/Help/About DisOrder",   /* path */
262       0,                                /* accelerator */
263       about_popup,                      /* callback */
264       0,                                /* callback_action */
265       (char *)"<StockItem>",            /* item_type */
266       GTK_STOCK_ABOUT                   /* extra_data */
267     },
268   };
269
270   GtkAccelGroup *accel = gtk_accel_group_new();
271
272   D(("add_menubar"));
273   /* TODO: item factories are deprecated in favour of some XML thing */
274   mainmenufactory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<GdisorderMain>",
275                                          accel);
276   gtk_item_factory_create_items(mainmenufactory,
277                                 sizeof entries / sizeof *entries,
278                                 (GtkItemFactoryEntry *)entries,
279                                 0);
280   gtk_window_add_accel_group(GTK_WINDOW(w), accel);
281   selectall_widget = gtk_item_factory_get_widget(mainmenufactory,
282                                                  "<GdisorderMain>/Edit/Select all tracks");
283   properties_widget = gtk_item_factory_get_widget(mainmenufactory,
284                                                   "<GdisorderMain>/Edit/Track properties");
285   assert(selectall_widget != 0);
286   assert(properties_widget != 0);
287   m = gtk_item_factory_get_widget(mainmenufactory,
288                                   "<GdisorderMain>");
289   set_tool_colors(m);
290   return m;
291 }
292
293 /*
294 Local Variables:
295 c-basic-offset:2
296 comment-column:40
297 fill-column:79
298 indent-tabs-mode:nil
299 End:
300 */