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