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