chiark / gitweb /
plugins/tracklength-gstreamer.c: Rewrite to use `GstDiscoverer'.
[disorder] / disobedience / choose-menu.c
CommitLineData
6982880f
RK
1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
6982880f 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
6982880f
RK
8 * (at your option) any later version.
9 *
e7eb3a27
RK
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 *
6982880f 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
6982880f 17 */
132a5a4a
RK
18/** @file disobedience/choose-menu.c
19 * @brief Popup menu for choose screen
20 */
6982880f
RK
21#include "disobedience.h"
22#include "popup.h"
23#include "choose.h"
24
a49ece1c
RK
25static void choose_playchildren_callback(GtkTreeModel *model,
26 GtkTreePath *path,
27 GtkTreeIter *iter,
28 gpointer data);
29static void choose_playchildren_received(void *v,
30 const char *err,
31 int nvec, char **vec);
ad131c25
RK
32static void choose_playchildren_played(void *v,
33 const char *err,
34 const char *id);
a49ece1c 35
6982880f
RK
36/** @brief Popup menu */
37static GtkWidget *choose_menu;
38
a0e78d96
RK
39/** @brief Path to directory pending a "select children" operation */
40static GtkTreePath *choose_eventually_select_children;
41
a0e78d96 42/** @brief Should edit->select all be sensitive? No, for the choose tab. */
6982880f 43static int choose_selectall_sensitive(void attribute((unused)) *extra) {
a0e78d96 44 return FALSE;
e8a26cdd
RK
45}
46
a0e78d96 47/** @brief Activate edit->select all (which should do nothing) */
6982880f
RK
48static void choose_selectall_activate(GtkMenuItem attribute((unused)) *item,
49 gpointer attribute((unused)) userdata) {
6982880f 50}
a98fd571
RK
51
52/** @brief Should 'select none' be sensitive
53 *
54 * Yes if anything is selected.
55 */
6982880f
RK
56static int choose_selectnone_sensitive(void attribute((unused)) *extra) {
57 return gtk_tree_selection_count_selected_rows(choose_selection) > 0;
58}
a98fd571
RK
59
60/** @brief Activate select none */
6982880f
RK
61static void choose_selectnone_activate(GtkMenuItem attribute((unused)) *item,
62 gpointer attribute((unused)) userdata) {
63 gtk_tree_selection_unselect_all(choose_selection);
64}
a98fd571
RK
65
66static void choose_play_sensitive_callback(GtkTreeModel attribute((unused)) *model,
67 GtkTreePath attribute((unused)) *path,
68 GtkTreeIter *iter,
69 gpointer data) {
70 int *filesp = data;
71
72 if(*filesp == -1)
73 return;
74 if(choose_is_dir(iter))
75 *filesp = -1;
76 else if(choose_is_file(iter))
77 ++*filesp;
78}
79
80/** @brief Should 'play' be sensitive?
81 *
82 * Yes if tracks are selected and no directories are */
6982880f 83static int choose_play_sensitive(void attribute((unused)) *extra) {
a98fd571
RK
84 int files = 0;
85
86 gtk_tree_selection_selected_foreach(choose_selection,
87 choose_play_sensitive_callback,
88 &files);
89 return files > 0;
487f69b2 90}
a98fd571
RK
91
92static void choose_gather_selected_files_callback(GtkTreeModel attribute((unused)) *model,
93 GtkTreePath attribute((unused)) *path,
94 GtkTreeIter *iter,
95 gpointer data) {
96 struct vector *v = data;
97
98 if(choose_is_file(iter))
99 vector_append(v, choose_get_track(iter));
100}
101
a0e78d96
RK
102static void choose_gather_selected_dirs_callback(GtkTreeModel attribute((unused)) *model,
103 GtkTreePath attribute((unused)) *path,
104 GtkTreeIter *iter,
105 gpointer data) {
106 struct vector *v = data;
107
108 if(choose_is_dir(iter))
109 vector_append(v, choose_get_track(iter));
110}
111
6982880f
RK
112
113static void choose_play_activate(GtkMenuItem attribute((unused)) *item,
114 gpointer attribute((unused)) userdata) {
a98fd571
RK
115 struct vector v[1];
116 vector_init(v);
117 gtk_tree_selection_selected_foreach(choose_selection,
118 choose_gather_selected_files_callback,
119 v);
120 for(int n = 0; n < v->nvec; ++n)
ad131c25 121 disorder_eclient_play(client, choose_play_completed, v->vec[n], 0);
6982880f
RK
122}
123
487f69b2
RK
124static int choose_properties_sensitive(void *extra) {
125 return choose_play_sensitive(extra);
6982880f
RK
126}
127
128static void choose_properties_activate(GtkMenuItem attribute((unused)) *item,
129 gpointer attribute((unused)) userdata) {
487f69b2
RK
130 struct vector v[1];
131 vector_init(v);
a98fd571
RK
132 gtk_tree_selection_selected_foreach(choose_selection,
133 choose_gather_selected_files_callback,
134 v);
d8b71e03 135 properties(v->nvec, (const char **)v->vec, toplevel);
6982880f
RK
136}
137
a0e78d96
RK
138/** @brief Set sensitivity for select children
139 *
140 * Sensitive if we've selected exactly one directory.
141 */
142static int choose_selectchildren_sensitive(void attribute((unused)) *extra) {
143 struct vector v[1];
144 /* Only one thing should be selected */
145 if(gtk_tree_selection_count_selected_rows(choose_selection) != 1)
146 return FALSE;
147 /* The selected thing should be a directory */
148 vector_init(v);
149 gtk_tree_selection_selected_foreach(choose_selection,
150 choose_gather_selected_dirs_callback,
151 v);
152 return v->nvec == 1;
153}
154
155/** @brief Actually select the children of path
156 *
157 * We deselect everything else, too.
158 */
159static void choose_select_children(GtkTreePath *path) {
160 GtkTreeIter iter[1], child[1];
161
162 if(gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store), iter, path)) {
163 gtk_tree_selection_unselect_all(choose_selection);
164 for(int n = 0;
165 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(choose_store), child,
166 iter, n);
167 ++n) {
168 if(choose_is_file(child))
169 gtk_tree_selection_select_iter(choose_selection, child);
170 }
171 }
172}
173
174/** @brief Called to expand the children of path/iter */
175static void choose_selectchildren_callback(GtkTreeModel attribute((unused)) *model,
176 GtkTreePath *path,
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);
182 } else {
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);
189 }
190}
191
192/** @brief Called when all pending track fetches are finished
193 *
194 * If there's a pending select-children operation, it can now be actioned
195 * (or might have gone stale).
196 */
197void 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;
204 }
205}
206
207/** @brief Select all children
208 *
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.
212 *
213 * The way this is implented could cope with multiple directories but
214 * choose_selectchildren_sensitive() should stop this.
215 */
216static 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,
221 0);
222}
223
a49ece1c
RK
224/** @brief Play all children */
225static 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,
231 0);
232}
233
234static void choose_playchildren_callback(GtkTreeModel attribute((unused)) *model,
235 GtkTreePath *path,
236 GtkTreeIter *iter,
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),
241 NULL/*re*/,
242 NULL);
243 /* Expand the node */
244 gtk_tree_view_expand_row(GTK_TREE_VIEW(choose_view), path, FALSE);
245}
246
247static void choose_playchildren_received(void attribute((unused)) *v,
248 const char *err,
249 int nvec, char **vec) {
250 if(err) {
251 popup_protocol_error(0, err);
252 return;
253 }
254 for(int n = 0; n < nvec; ++n)
ad131c25 255 disorder_eclient_play(client, choose_playchildren_played, vec[n], NULL);
a49ece1c
RK
256}
257
258static void choose_playchildren_played(void attribute((unused)) *v,
ad131c25
RK
259 const char *err,
260 const char attribute((unused)) *id) {
a49ece1c
RK
261 if(err) {
262 popup_protocol_error(0, err);
263 return;
264 }
265}
266
6982880f
RK
267/** @brief Pop-up menu for choose */
268static struct menuitem choose_menuitems[] = {
269 {
270 "Play track",
ba6f3dd4 271 GTK_STOCK_MEDIA_PLAY,
6982880f
RK
272 choose_play_activate,
273 choose_play_sensitive,
274 0,
275 0
276 },
277 {
278 "Track properties",
ba6f3dd4 279 GTK_STOCK_PROPERTIES,
6982880f
RK
280 choose_properties_activate,
281 choose_properties_sensitive,
282 0,
283 0
284 },
285 {
a0e78d96 286 "Select children",
ba6f3dd4 287 NULL,
a0e78d96
RK
288 choose_selectchildren_activate,
289 choose_selectchildren_sensitive,
6982880f
RK
290 0,
291 0
292 },
a49ece1c
RK
293 {
294 "Play children",
ba6f3dd4 295 NULL,
a49ece1c
RK
296 choose_playchildren_activate,
297 choose_selectchildren_sensitive, /* re-use */
298 0,
299 0
300 },
6982880f
RK
301 {
302 "Deselect all tracks",
ba6f3dd4 303 NULL,
6982880f
RK
304 choose_selectnone_activate,
305 choose_selectnone_sensitive,
306 0,
307 0
308 },
309};
310
311const 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,
318 0,
319 0
320};
321
322/** @brief Called when a mouse button is pressed or released */
323gboolean 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 */
487f69b2
RK
328 ensure_selected(GTK_TREE_VIEW(choose_view), event);
329 choose_play_activate(NULL, NULL);
6982880f
RK
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,
335 0);
336 return TRUE;
337 }
338 return FALSE;
339}
340
341/*
342Local Variables:
343c-basic-offset:2
344comment-column:40
345fill-column:79
346indent-tabs-mode:nil
347End:
348*/