2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
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.
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.
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/>.
18 /** @file disobedience/choose-menu.c
19 * @brief Popup menu for choose screen
21 #include "disobedience.h"
25 /** @brief Popup menu */
26 static GtkWidget *choose_menu;
28 /** @brief Path to directory pending a "select children" operation */
29 static GtkTreePath *choose_eventually_select_children;
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
36 * If @p callback returns nonzero, the walk stops immediately.
38 static int choose_visible_recurse(GtkTreeIter *parent,
39 int (*callback)(GtkTreeIter *it,
45 /* Skip placeholders */
46 if(choose_is_placeholder(parent))
48 const int isfile = choose_is_file(parent);
49 if(callback(parent, isfile, userdata))
52 return 0; /* Files never have children */
53 GtkTreePath *parent_path
54 = gtk_tree_model_get_path(GTK_TREE_MODEL(choose_store),
56 expanded = gtk_tree_view_row_expanded(GTK_TREE_VIEW(choose_view),
58 gtk_tree_path_free(parent_path);
61 /* See if parent is expanded */
63 /* Parent is expanded, visit all its children */
65 gboolean itv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store),
69 if(choose_visible_recurse(it, callback, userdata))
71 itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it);
77 /** @brief Should edit->select all be sensitive? No, for the choose tab. */
78 static int choose_selectall_sensitive(void attribute((unused)) *extra) {
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) {
87 /** @brief Should 'select none' be sensitive
89 * Yes if anything is selected.
91 static int choose_selectnone_sensitive(void attribute((unused)) *extra) {
92 return gtk_tree_selection_count_selected_rows(choose_selection) > 0;
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);
101 static void choose_play_sensitive_callback(GtkTreeModel attribute((unused)) *model,
102 GtkTreePath attribute((unused)) *path,
109 if(choose_is_dir(iter))
111 else if(choose_is_file(iter))
115 /** @brief Should 'play' be sensitive?
117 * Yes if tracks are selected and no directories are */
118 static int choose_play_sensitive(void attribute((unused)) *extra) {
121 gtk_tree_selection_selected_foreach(choose_selection,
122 choose_play_sensitive_callback,
127 static void choose_gather_selected_files_callback(GtkTreeModel attribute((unused)) *model,
128 GtkTreePath attribute((unused)) *path,
131 struct vector *v = data;
133 if(choose_is_file(iter))
134 vector_append(v, choose_get_track(iter));
137 static void choose_gather_selected_dirs_callback(GtkTreeModel attribute((unused)) *model,
138 GtkTreePath attribute((unused)) *path,
141 struct vector *v = data;
143 if(choose_is_dir(iter))
144 vector_append(v, choose_get_track(iter));
148 static void choose_play_activate(GtkMenuItem attribute((unused)) *item,
149 gpointer attribute((unused)) userdata) {
152 gtk_tree_selection_selected_foreach(choose_selection,
153 choose_gather_selected_files_callback,
155 for(int n = 0; n < v->nvec; ++n)
156 disorder_eclient_play(client, v->vec[n], choose_play_completed, 0);
159 static int choose_properties_sensitive(void *extra) {
160 return choose_play_sensitive(extra);
163 static void choose_properties_activate(GtkMenuItem attribute((unused)) *item,
164 gpointer attribute((unused)) userdata) {
167 gtk_tree_selection_selected_foreach(choose_selection,
168 choose_gather_selected_files_callback,
170 properties(v->nvec, (const char **)v->vec);
173 /** @brief Set sensitivity for select children
175 * Sensitive if we've selected exactly one directory.
177 static int choose_selectchildren_sensitive(void attribute((unused)) *extra) {
179 /* Only one thing should be selected */
180 if(gtk_tree_selection_count_selected_rows(choose_selection) != 1)
182 /* The selected thing should be a directory */
184 gtk_tree_selection_selected_foreach(choose_selection,
185 choose_gather_selected_dirs_callback,
190 /** @brief Actually select the children of path
192 * We deselect everything else, too.
194 static void choose_select_children(GtkTreePath *path) {
195 GtkTreeIter iter[1], child[1];
197 if(gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store), iter, path)) {
198 gtk_tree_selection_unselect_all(choose_selection);
200 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(choose_store), child,
203 if(choose_is_file(child))
204 gtk_tree_selection_select_iter(choose_selection, child);
209 /** @brief Called to expand the children of path/iter */
210 static void choose_selectchildren_callback(GtkTreeModel attribute((unused)) *model,
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);
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);
227 /** @brief Called when all pending track fetches are finished
229 * If there's a pending select-children operation, it can now be actioned
230 * (or might have gone stale).
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;
242 /** @brief Select all children
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.
248 * The way this is implented could cope with multiple directories but
249 * choose_selectchildren_sensitive() should stop this.
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,
259 /** @brief Pop-up menu for choose */
260 static struct menuitem choose_menuitems[] = {
263 choose_play_activate,
264 choose_play_sensitive,
270 choose_properties_activate,
271 choose_properties_sensitive,
277 choose_selectchildren_activate,
278 choose_selectchildren_sensitive,
283 "Deselect all tracks",
284 choose_selectnone_activate,
285 choose_selectnone_sensitive,
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,
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,