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,
36 /** @brief Popup menu */
37 static GtkWidget *choose_menu;
39 /** @brief Path to directory pending a "select children" operation */
40 static GtkTreePath *choose_eventually_select_children;
42 /** @brief Should edit->select all be sensitive? No, for the choose tab. */
43 static int choose_selectall_sensitive(void attribute((unused)) *extra) {
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) {
52 /** @brief Should 'select none' be sensitive
54 * Yes if anything is selected.
56 static int choose_selectnone_sensitive(void attribute((unused)) *extra) {
57 return gtk_tree_selection_count_selected_rows(choose_selection) > 0;
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);
66 static void choose_play_sensitive_callback(GtkTreeModel attribute((unused)) *model,
67 GtkTreePath attribute((unused)) *path,
74 if(choose_is_dir(iter))
76 else if(choose_is_file(iter))
80 /** @brief Should 'play' be sensitive?
82 * Yes if tracks are selected and no directories are */
83 static int choose_play_sensitive(void attribute((unused)) *extra) {
86 gtk_tree_selection_selected_foreach(choose_selection,
87 choose_play_sensitive_callback,
92 static void choose_gather_selected_files_callback(GtkTreeModel attribute((unused)) *model,
93 GtkTreePath attribute((unused)) *path,
96 struct vector *v = data;
98 if(choose_is_file(iter))
99 vector_append(v, choose_get_track(iter));
102 static void choose_gather_selected_dirs_callback(GtkTreeModel attribute((unused)) *model,
103 GtkTreePath attribute((unused)) *path,
106 struct vector *v = data;
108 if(choose_is_dir(iter))
109 vector_append(v, choose_get_track(iter));
113 static void choose_play_activate(GtkMenuItem attribute((unused)) *item,
114 gpointer attribute((unused)) userdata) {
117 gtk_tree_selection_selected_foreach(choose_selection,
118 choose_gather_selected_files_callback,
120 for(int n = 0; n < v->nvec; ++n)
121 disorder_eclient_play(client, choose_play_completed, v->vec[n], 0);
124 static int choose_properties_sensitive(void *extra) {
125 return choose_play_sensitive(extra);
128 static void choose_properties_activate(GtkMenuItem attribute((unused)) *item,
129 gpointer attribute((unused)) userdata) {
132 gtk_tree_selection_selected_foreach(choose_selection,
133 choose_gather_selected_files_callback,
135 properties(v->nvec, (const char **)v->vec, toplevel);
138 /** @brief Set sensitivity for select children
140 * Sensitive if we've selected exactly one directory.
142 static int choose_selectchildren_sensitive(void attribute((unused)) *extra) {
144 /* Only one thing should be selected */
145 if(gtk_tree_selection_count_selected_rows(choose_selection) != 1)
147 /* The selected thing should be a directory */
149 gtk_tree_selection_selected_foreach(choose_selection,
150 choose_gather_selected_dirs_callback,
155 /** @brief Actually select the children of path
157 * We deselect everything else, too.
159 static void choose_select_children(GtkTreePath *path) {
160 GtkTreeIter iter[1], child[1];
162 if(gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store), iter, path)) {
163 gtk_tree_selection_unselect_all(choose_selection);
165 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(choose_store), child,
168 if(choose_is_file(child))
169 gtk_tree_selection_select_iter(choose_selection, child);
174 /** @brief Called to expand the children of path/iter */
175 static void choose_selectchildren_callback(GtkTreeModel attribute((unused)) *model,
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);
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);
192 /** @brief Called when all pending track fetches are finished
194 * If there's a pending select-children operation, it can now be actioned
195 * (or might have gone stale).
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;
207 /** @brief Select all children
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.
213 * The way this is implented could cope with multiple directories but
214 * choose_selectchildren_sensitive() should stop this.
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,
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,
234 static void choose_playchildren_callback(GtkTreeModel attribute((unused)) *model,
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),
243 /* Expand the node */
244 gtk_tree_view_expand_row(GTK_TREE_VIEW(choose_view), path, FALSE);
247 static void choose_playchildren_received(void attribute((unused)) *v,
249 int nvec, char **vec) {
251 popup_protocol_error(0, err);
254 for(int n = 0; n < nvec; ++n)
255 disorder_eclient_play(client, choose_playchildren_played, vec[n], NULL);
258 static void choose_playchildren_played(void attribute((unused)) *v,
260 const char attribute((unused)) *id) {
262 popup_protocol_error(0, err);
267 /** @brief Pop-up menu for choose */
268 static struct menuitem choose_menuitems[] = {
271 GTK_STOCK_MEDIA_PLAY,
272 choose_play_activate,
273 choose_play_sensitive,
279 GTK_STOCK_PROPERTIES,
280 choose_properties_activate,
281 choose_properties_sensitive,
288 choose_selectchildren_activate,
289 choose_selectchildren_sensitive,
296 choose_playchildren_activate,
297 choose_selectchildren_sensitive, /* re-use */
302 "Deselect all tracks",
304 choose_selectnone_activate,
305 choose_selectnone_sensitive,
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,
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,