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 static void choose_playchildren_callback(GtkTreeModel *model,
29 static void choose_playchildren_received(void *v,
31 int nvec, char **vec);
32 static void choose_playchildren_played(void *v, const char *err);
34 /** @brief Popup menu */
35 static GtkWidget *choose_menu;
37 /** @brief Path to directory pending a "select children" operation */
38 static GtkTreePath *choose_eventually_select_children;
40 /** @brief Should edit->select all be sensitive? No, for the choose tab. */
41 static int choose_selectall_sensitive(void attribute((unused)) *extra) {
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) {
50 /** @brief Should 'select none' be sensitive
52 * Yes if anything is selected.
54 static int choose_selectnone_sensitive(void attribute((unused)) *extra) {
55 return gtk_tree_selection_count_selected_rows(choose_selection) > 0;
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);
64 static void choose_play_sensitive_callback(GtkTreeModel attribute((unused)) *model,
65 GtkTreePath attribute((unused)) *path,
72 if(choose_is_dir(iter))
74 else if(choose_is_file(iter))
78 /** @brief Should 'play' be sensitive?
80 * Yes if tracks are selected and no directories are */
81 static int choose_play_sensitive(void attribute((unused)) *extra) {
84 gtk_tree_selection_selected_foreach(choose_selection,
85 choose_play_sensitive_callback,
90 static void choose_gather_selected_files_callback(GtkTreeModel attribute((unused)) *model,
91 GtkTreePath attribute((unused)) *path,
94 struct vector *v = data;
96 if(choose_is_file(iter))
97 vector_append(v, choose_get_track(iter));
100 static void choose_gather_selected_dirs_callback(GtkTreeModel attribute((unused)) *model,
101 GtkTreePath attribute((unused)) *path,
104 struct vector *v = data;
106 if(choose_is_dir(iter))
107 vector_append(v, choose_get_track(iter));
111 static void choose_play_activate(GtkMenuItem attribute((unused)) *item,
112 gpointer attribute((unused)) userdata) {
115 gtk_tree_selection_selected_foreach(choose_selection,
116 choose_gather_selected_files_callback,
118 for(int n = 0; n < v->nvec; ++n)
119 disorder_eclient_play(client, v->vec[n], choose_play_completed, 0);
122 static int choose_properties_sensitive(void *extra) {
123 return choose_play_sensitive(extra);
126 static void choose_properties_activate(GtkMenuItem attribute((unused)) *item,
127 gpointer attribute((unused)) userdata) {
130 gtk_tree_selection_selected_foreach(choose_selection,
131 choose_gather_selected_files_callback,
133 properties(v->nvec, (const char **)v->vec, toplevel);
136 /** @brief Set sensitivity for select children
138 * Sensitive if we've selected exactly one directory.
140 static int choose_selectchildren_sensitive(void attribute((unused)) *extra) {
142 /* Only one thing should be selected */
143 if(gtk_tree_selection_count_selected_rows(choose_selection) != 1)
145 /* The selected thing should be a directory */
147 gtk_tree_selection_selected_foreach(choose_selection,
148 choose_gather_selected_dirs_callback,
153 /** @brief Actually select the children of path
155 * We deselect everything else, too.
157 static void choose_select_children(GtkTreePath *path) {
158 GtkTreeIter iter[1], child[1];
160 if(gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store), iter, path)) {
161 gtk_tree_selection_unselect_all(choose_selection);
163 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(choose_store), child,
166 if(choose_is_file(child))
167 gtk_tree_selection_select_iter(choose_selection, child);
172 /** @brief Called to expand the children of path/iter */
173 static void choose_selectchildren_callback(GtkTreeModel attribute((unused)) *model,
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);
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);
190 /** @brief Called when all pending track fetches are finished
192 * If there's a pending select-children operation, it can now be actioned
193 * (or might have gone stale).
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;
205 /** @brief Select all children
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.
211 * The way this is implented could cope with multiple directories but
212 * choose_selectchildren_sensitive() should stop this.
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,
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,
232 static void choose_playchildren_callback(GtkTreeModel attribute((unused)) *model,
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),
241 /* Expand the node */
242 gtk_tree_view_expand_row(GTK_TREE_VIEW(choose_view), path, FALSE);
245 static void choose_playchildren_received(void attribute((unused)) *v,
247 int nvec, char **vec) {
249 popup_protocol_error(0, err);
252 for(int n = 0; n < nvec; ++n)
253 disorder_eclient_play(client, vec[n], choose_playchildren_played, NULL);
256 static void choose_playchildren_played(void attribute((unused)) *v,
259 popup_protocol_error(0, err);
264 /** @brief Pop-up menu for choose */
265 static struct menuitem choose_menuitems[] = {
268 choose_play_activate,
269 choose_play_sensitive,
275 choose_properties_activate,
276 choose_properties_sensitive,
282 choose_selectchildren_activate,
283 choose_selectchildren_sensitive,
289 choose_playchildren_activate,
290 choose_selectchildren_sensitive, /* re-use */
295 "Deselect all tracks",
296 choose_selectnone_activate,
297 choose_selectnone_sensitive,
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,
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,