chiark / gitweb /
Add documentation and administrivia about `disorder-gstdecode'.
[disorder] / disobedience / choose-menu.c
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 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/choose-menu.c
19  * @brief Popup menu for choose screen
20  */
21 #include "disobedience.h"
22 #include "popup.h"
23 #include "choose.h"
24
25 static void choose_playchildren_callback(GtkTreeModel *model,
26                                          GtkTreePath *path,
27                                          GtkTreeIter *iter,
28                                          gpointer data);
29 static void choose_playchildren_received(void *v,
30                                          const char *err,
31                                          int nvec, char **vec);
32 static void choose_playchildren_played(void *v,
33                                        const char *err,
34                                        const char *id);
35
36 /** @brief Popup menu */
37 static GtkWidget *choose_menu;
38
39 /** @brief Path to directory pending a "select children" operation */
40 static GtkTreePath *choose_eventually_select_children;
41
42 /** @brief Should edit->select all be sensitive?  No, for the choose tab. */
43 static int choose_selectall_sensitive(void attribute((unused)) *extra) {
44   return FALSE;
45 }
46
47 /** @brief Activate edit->select all (which should do nothing) */
48 static void choose_selectall_activate(GtkMenuItem attribute((unused)) *item,
49                                       gpointer attribute((unused)) userdata) {
50 }
51
52 /** @brief Should 'select none' be sensitive
53  *
54  * Yes if anything is selected.
55  */
56 static int choose_selectnone_sensitive(void attribute((unused)) *extra) {
57   return gtk_tree_selection_count_selected_rows(choose_selection) > 0;
58 }
59
60 /** @brief Activate select none */
61 static void choose_selectnone_activate(GtkMenuItem attribute((unused)) *item,
62                                        gpointer attribute((unused)) userdata) {
63   gtk_tree_selection_unselect_all(choose_selection);
64 }
65
66 static void choose_play_sensitive_callback(GtkTreeModel attribute((unused)) *model,
67                                            GtkTreePath attribute((unused)) *path,
68                                            GtkTreeIter *iter,
69                                            gpointer data) {
70   int *filesp = data;
71
72   if(*filesp == -1)
73     return;
74   if(choose_is_dir(iter))
75     *filesp = -1;
76   else if(choose_is_file(iter))
77     ++*filesp;
78 }
79
80 /** @brief Should 'play' be sensitive?
81  *
82  * Yes if tracks are selected and no directories are */
83 static int choose_play_sensitive(void attribute((unused)) *extra) {
84   int files = 0;
85   
86   gtk_tree_selection_selected_foreach(choose_selection,
87                                       choose_play_sensitive_callback,
88                                       &files);
89   return files > 0;
90 }
91
92 static void choose_gather_selected_files_callback(GtkTreeModel attribute((unused)) *model,
93                                                   GtkTreePath attribute((unused)) *path,
94                                                   GtkTreeIter *iter,
95                                                   gpointer data) {
96   struct vector *v = data;
97
98   if(choose_is_file(iter))
99     vector_append(v, choose_get_track(iter));
100 }
101
102 static void choose_gather_selected_dirs_callback(GtkTreeModel attribute((unused)) *model,
103                                                  GtkTreePath attribute((unused)) *path,
104                                                  GtkTreeIter *iter,
105                                                  gpointer data) {
106   struct vector *v = data;
107
108   if(choose_is_dir(iter))
109     vector_append(v, choose_get_track(iter));
110 }
111
112   
113 static void choose_play_activate(GtkMenuItem attribute((unused)) *item,
114                                  gpointer attribute((unused)) userdata) {
115   struct vector v[1];
116   vector_init(v);
117   gtk_tree_selection_selected_foreach(choose_selection,
118                                       choose_gather_selected_files_callback,
119                                       v);
120   for(int n = 0; n < v->nvec; ++n)
121     disorder_eclient_play(client, choose_play_completed, v->vec[n], 0);
122 }
123   
124 static int choose_properties_sensitive(void *extra) {
125   return choose_play_sensitive(extra);
126 }
127   
128 static void choose_properties_activate(GtkMenuItem attribute((unused)) *item,
129                                        gpointer attribute((unused)) userdata) {
130   struct vector v[1];
131   vector_init(v);
132   gtk_tree_selection_selected_foreach(choose_selection,
133                                       choose_gather_selected_files_callback,
134                                       v);
135   properties(v->nvec, (const char **)v->vec, toplevel);
136 }
137
138 /** @brief Set sensitivity for select children
139  *
140  * Sensitive if we've selected exactly one directory.
141  */
142 static int choose_selectchildren_sensitive(void attribute((unused)) *extra) {
143   struct vector v[1];
144   /* Only one thing should be selected */
145   if(gtk_tree_selection_count_selected_rows(choose_selection) != 1)
146     return FALSE;
147   /* The selected thing should be a directory */
148   vector_init(v);
149   gtk_tree_selection_selected_foreach(choose_selection,
150                                       choose_gather_selected_dirs_callback,
151                                       v);
152   return v->nvec == 1;
153 }
154
155 /** @brief Actually select the children of path
156  *
157  * We deselect everything else, too.
158  */
159 static void choose_select_children(GtkTreePath *path) {
160   GtkTreeIter iter[1], child[1];
161   
162   if(gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store), iter, path)) {
163     gtk_tree_selection_unselect_all(choose_selection);
164     for(int n = 0;
165         gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(choose_store), child,
166                                       iter, n);
167         ++n) {
168       if(choose_is_file(child))
169         gtk_tree_selection_select_iter(choose_selection, child);
170     }
171   }
172 }
173
174 /** @brief Called to expand the children of path/iter */
175 static void choose_selectchildren_callback(GtkTreeModel attribute((unused)) *model,
176                                            GtkTreePath *path,
177                                            GtkTreeIter attribute((unused)) *iter,
178                                            gpointer attribute((unused)) data) {
179   if(gtk_tree_view_row_expanded(GTK_TREE_VIEW(choose_view), path)) {
180     /* Directory is already expanded */
181     choose_select_children(path);
182   } else {
183     /* Directory is not expanded, so expand it */
184     gtk_tree_view_expand_row(GTK_TREE_VIEW(choose_view), path, FALSE/*!expand_all*/);
185     /* Select its children when it's done */
186     if(choose_eventually_select_children)
187       gtk_tree_path_free(choose_eventually_select_children);
188     choose_eventually_select_children = gtk_tree_path_copy(path);
189   }
190 }
191
192 /** @brief Called when all pending track fetches are finished
193  *
194  * If there's a pending select-children operation, it can now be actioned
195  * (or might have gone stale).
196  */
197 void choose_menu_moretracks(const char attribute((unused)) *event,
198                             void attribute((unused)) *eventdata,
199                             void attribute((unused)) *callbackdata) {
200   if(choose_eventually_select_children) {
201     choose_select_children(choose_eventually_select_children);
202     gtk_tree_path_free(choose_eventually_select_children);
203     choose_eventually_select_children = 0;
204   }
205 }
206
207 /** @brief Select all children
208  *
209  * Easy enough if the directory is already expanded, we can just select its
210  * children.  However if it is not then we must expand it and _when this has
211  * completed_ select its children.
212  *
213  * The way this is implented could cope with multiple directories but
214  * choose_selectchildren_sensitive() should stop this.
215  */
216 static void choose_selectchildren_activate
217     (GtkMenuItem attribute((unused)) *item,
218      gpointer attribute((unused)) userdata) {
219   gtk_tree_selection_selected_foreach(choose_selection,
220                                       choose_selectchildren_callback,
221                                       0);
222 }
223
224 /** @brief Play all children */
225 static void choose_playchildren_activate
226     (GtkMenuItem attribute((unused)) *item,
227      gpointer attribute((unused)) userdata) {
228   /* Only one thing is selected */
229   gtk_tree_selection_selected_foreach(choose_selection,
230                                       choose_playchildren_callback,
231                                       0);
232 }
233
234 static void choose_playchildren_callback(GtkTreeModel attribute((unused)) *model,
235                                          GtkTreePath *path,
236                                          GtkTreeIter *iter,
237                                          gpointer attribute((unused)) data) {
238   /* Find the children and play them */
239   disorder_eclient_files(client, choose_playchildren_received,
240                          choose_get_track(iter),
241                          NULL/*re*/,
242                          NULL);
243   /* Expand the node */
244   gtk_tree_view_expand_row(GTK_TREE_VIEW(choose_view), path, FALSE);
245 }
246
247 static void choose_playchildren_received(void attribute((unused)) *v,
248                                          const char *err,
249                                          int nvec, char **vec) {
250   if(err) {
251     popup_protocol_error(0, err);
252     return;
253   }
254   for(int n = 0; n < nvec; ++n)
255     disorder_eclient_play(client, choose_playchildren_played, vec[n], NULL);
256 }
257
258 static void choose_playchildren_played(void attribute((unused)) *v,
259                                        const char *err,
260                                        const char attribute((unused)) *id) {
261   if(err) {
262     popup_protocol_error(0, err);
263     return;
264   }
265 }
266
267 /** @brief Pop-up menu for choose */
268 static struct menuitem choose_menuitems[] = {
269   {
270     "Play track",
271     GTK_STOCK_MEDIA_PLAY,
272     choose_play_activate,
273     choose_play_sensitive,
274     0,
275     0
276   },
277   {
278     "Track properties",
279     GTK_STOCK_PROPERTIES,
280     choose_properties_activate,
281     choose_properties_sensitive,
282     0,
283     0
284   },
285   {
286     "Select children",
287     NULL,
288     choose_selectchildren_activate,
289     choose_selectchildren_sensitive,
290     0,
291     0
292   },
293   {
294     "Play children",
295     NULL,
296     choose_playchildren_activate,
297     choose_selectchildren_sensitive,    /* re-use */
298     0,
299     0
300   },
301   {
302     "Deselect all tracks",
303     NULL,
304     choose_selectnone_activate,
305     choose_selectnone_sensitive,
306     0,
307     0
308   },
309 };
310
311 const struct tabtype choose_tabtype = {
312   choose_properties_sensitive,
313   choose_selectall_sensitive,
314   choose_selectnone_sensitive,
315   choose_properties_activate,
316   choose_selectall_activate,
317   choose_selectnone_activate,
318   0,
319   0
320 };
321
322 /** @brief Called when a mouse button is pressed or released */
323 gboolean choose_button_event(GtkWidget attribute((unused)) *widget,
324                              GdkEventButton *event,
325                              gpointer attribute((unused)) user_data) {
326   if(event->type == GDK_BUTTON_RELEASE && event->button == 2) {
327     /* Middle click release - play track */
328     ensure_selected(GTK_TREE_VIEW(choose_view), event);
329     choose_play_activate(NULL, NULL);
330   } else if(event->type == GDK_BUTTON_PRESS && event->button == 3) {
331     /* Right click press - pop up the menu */
332     ensure_selected(GTK_TREE_VIEW(choose_view), event);
333     popup(&choose_menu, event,
334           choose_menuitems, sizeof choose_menuitems / sizeof *choose_menuitems,
335           0);
336     return TRUE;
337   }
338   return FALSE;
339 }
340
341 /*
342 Local Variables:
343 c-basic-offset:2
344 comment-column:40
345 fill-column:79
346 indent-tabs-mode:nil
347 End:
348 */