chiark / gitweb /
login details box for disobedience. a bit unfriendly but does work.
[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 static void about_popup_got_version(void *v, const char *value);
30
31 /** @brief Called when the quit option is activated
32  *
33  * Just exits.
34  */
35 static void quit_program(gpointer attribute((unused)) callback_data,
36                          guint attribute((unused)) callback_action,
37                          GtkWidget attribute((unused)) *menu_item) {
38   D(("quit_program"));
39   exit(0);
40 }
41
42 /* TODO can we have a single parameterized callback for all these */
43
44 /** @brief Called when the select all option is activated
45  *
46  * Calls the per-tab select all function.
47  */
48 static void select_all(gpointer attribute((unused)) callback_data,
49                        guint attribute((unused)) callback_action,
50                        GtkWidget attribute((unused)) *menu_item) {
51   GtkWidget *tab = gtk_notebook_get_nth_page
52     (GTK_NOTEBOOK(tabs), gtk_notebook_current_page(GTK_NOTEBOOK(tabs)));
53   const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
54
55   t->selectall_activate(tab);
56 }
57
58 /** @brief Called when the track properties option is activated
59  *
60  * Calls the per-tab properties function.
61  */
62 static void properties_item(gpointer attribute((unused)) callback_data,
63                             guint attribute((unused)) callback_action,
64                             GtkWidget attribute((unused)) *menu_item) {
65   GtkWidget *tab = gtk_notebook_get_nth_page
66     (GTK_NOTEBOOK(tabs), gtk_notebook_current_page(GTK_NOTEBOOK(tabs)));
67   const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
68
69   t->properties_activate(tab);
70 }
71
72 /** @brief Called when the login option is activated */
73 static void login(gpointer attribute((unused)) callback_data,
74                   guint attribute((unused)) callback_action,
75                   GtkWidget attribute((unused)) *menu_item) {
76   login_box();
77 }
78
79 /** @brief Update menu state
80  *
81  * Determines option sensitivity according to the current tab and adjusts the
82  * widgets accordingly.  Knows about @ref DISORDER_CONNECTED so the callbacks
83  * need not.
84  */
85 void menu_update(int page) {
86   GtkWidget *tab = gtk_notebook_get_nth_page
87     (GTK_NOTEBOOK(tabs),
88      page < 0 ? gtk_notebook_current_page(GTK_NOTEBOOK(tabs)) : page);
89   const struct tabtype *t = g_object_get_data(G_OBJECT(tab), "type");
90
91   assert(t != 0);
92   gtk_widget_set_sensitive(properties_widget,
93                            (t->properties_sensitive(tab)
94                             && (disorder_eclient_state(client) & DISORDER_CONNECTED)));
95   gtk_widget_set_sensitive(selectall_widget,
96                            t->selectall_sensitive(tab));
97 }
98    
99 /** @brief Fetch version in order to display the about... popup */
100 static void about_popup(gpointer attribute((unused)) callback_data,
101                         guint attribute((unused)) callback_action,
102                         GtkWidget attribute((unused)) *menu_item) {
103   D(("about_popup"));
104
105   gtk_label_set_text(GTK_LABEL(report_label), "getting server version");
106   disorder_eclient_version(client,
107                            about_popup_got_version,
108                            0);
109 }
110
111 /** @brief Callde when version arrives, displays about... popup */
112 static void about_popup_got_version(void attribute((unused)) *v,
113                                     const char *value) {
114   GtkWidget *w;
115   char *server_version_string;
116
117   byte_xasprintf(&server_version_string, "Server version %s", value);
118   w = gtk_dialog_new_with_buttons("About DisOrder",
119                                   GTK_WINDOW(toplevel),
120                                   (GTK_DIALOG_MODAL
121                                    |GTK_DIALOG_DESTROY_WITH_PARENT),
122                                   GTK_STOCK_OK,
123                                   GTK_RESPONSE_ACCEPT,
124                                   (char *)NULL);
125   gtk_container_add(GTK_CONTAINER(GTK_DIALOG(w)->vbox),
126                     gtk_label_new("DisOrder client " VERSION));
127   gtk_container_add(GTK_CONTAINER(GTK_DIALOG(w)->vbox),
128                     gtk_label_new(server_version_string));
129   gtk_container_add(GTK_CONTAINER(GTK_DIALOG(w)->vbox),
130                     gtk_label_new("(c) 2004-2007 Richard Kettlewell"));
131   gtk_widget_show_all(w);
132   gtk_dialog_run(GTK_DIALOG(w));
133   gtk_widget_destroy(w);
134 }
135
136 /** @brief Create the menu bar widget */
137 GtkWidget *menubar(GtkWidget *w) {
138   static const GtkItemFactoryEntry entries[] = {
139     { (char *)"/File", 0,  0, 0, (char *)"<Branch>", 0 },
140     { (char *)"/File/Login", (char *)"<CTRL>L", login, 0,
141       0, 0 },
142     { (char *)"/File/Quit Disobedience", (char *)"<CTRL>Q", quit_program, 0,
143       (char *)"<StockItem>", GTK_STOCK_QUIT },
144     { (char *)"/Edit", 0,  0, 0, (char *)"<Branch>", 0 },
145     { (char *)"/Edit/Select all tracks", (char *)"<CTRL>A", select_all, 0,
146       0, 0 },
147     { (char *)"/Edit/Track properties", 0, properties_item, 0,
148       0, 0 },
149     { (char *)"/Help", 0,  0, 0, (char *)"<Branch>", 0 },
150     { (char *)"/Help/About DisOrder", 0,  about_popup, 0,
151       (char *)"<StockItem>", GTK_STOCK_ABOUT },
152   };
153
154   GtkItemFactory *itemfactory;
155   GtkAccelGroup *accel = gtk_accel_group_new();
156
157   D(("add_menubar"));
158   /* TODO: item factories are deprecated in favour of some XML thing */
159   itemfactory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<GdisorderMain>",
160                                      accel);
161   gtk_item_factory_create_items(itemfactory,
162                                 sizeof entries / sizeof *entries,
163                                 (GtkItemFactoryEntry *)entries,
164                                 0);
165   gtk_window_add_accel_group(GTK_WINDOW(w), accel);
166   selectall_widget = gtk_item_factory_get_widget(itemfactory,
167                                                  "<GdisorderMain>/Edit/Select all tracks");
168   properties_widget = gtk_item_factory_get_widget(itemfactory,
169                                                   "<GdisorderMain>/Edit/Track properties");
170   assert(selectall_widget != 0);
171   assert(properties_widget != 0);
172   return gtk_item_factory_get_widget(itemfactory,
173                                      "<GdisorderMain>");
174   /* menu bar had better not expand vertically if the window is too big */
175 }
176
177 /*
178 Local Variables:
179 c-basic-offset:2
180 comment-column:40
181 fill-column:79
182 indent-tabs-mode:nil
183 End:
184 */