Commit | Line | Data |
---|---|---|
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 | ||
25 | /** @brief Popup menu */ | |
26 | static GtkWidget *choose_menu; | |
27 | ||
a0e78d96 RK |
28 | /** @brief Path to directory pending a "select children" operation */ |
29 | static GtkTreePath *choose_eventually_select_children; | |
30 | ||
e8a26cdd RK |
31 | /** @brief Recursion step for choose_get_visible() |
32 | * @param parent A visible node, or NULL for the root | |
a98fd571 RK |
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. | |
e8a26cdd | 37 | */ |
a98fd571 RK |
38 | static int choose_visible_recurse(GtkTreeIter *parent, |
39 | int (*callback)(GtkTreeIter *it, | |
40 | int isfile, | |
41 | void *userdata), | |
42 | void *userdata) { | |
e8a26cdd RK |
43 | int expanded; |
44 | if(parent) { | |
a98fd571 RK |
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 */ | |
e8a26cdd RK |
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) { | |
a98fd571 RK |
69 | if(choose_visible_recurse(it, callback, userdata)) |
70 | return TRUE; | |
e8a26cdd RK |
71 | itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it); |
72 | } | |
73 | } | |
a98fd571 | 74 | return 0; |
e8a26cdd RK |
75 | } |
76 | ||
a0e78d96 | 77 | /** @brief Should edit->select all be sensitive? No, for the choose tab. */ |
6982880f | 78 | static int choose_selectall_sensitive(void attribute((unused)) *extra) { |
a0e78d96 | 79 | return FALSE; |
e8a26cdd RK |
80 | } |
81 | ||
a0e78d96 | 82 | /** @brief Activate edit->select all (which should do nothing) */ |
6982880f RK |
83 | static void choose_selectall_activate(GtkMenuItem attribute((unused)) *item, |
84 | gpointer attribute((unused)) userdata) { | |
6982880f | 85 | } |
a98fd571 RK |
86 | |
87 | /** @brief Should 'select none' be sensitive | |
88 | * | |
89 | * Yes if anything is selected. | |
90 | */ | |
6982880f RK |
91 | static int choose_selectnone_sensitive(void attribute((unused)) *extra) { |
92 | return gtk_tree_selection_count_selected_rows(choose_selection) > 0; | |
93 | } | |
a98fd571 RK |
94 | |
95 | /** @brief Activate select none */ | |
6982880f RK |
96 | static void choose_selectnone_activate(GtkMenuItem attribute((unused)) *item, |
97 | gpointer attribute((unused)) userdata) { | |
98 | gtk_tree_selection_unselect_all(choose_selection); | |
99 | } | |
a98fd571 RK |
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 */ | |
6982880f | 118 | static int choose_play_sensitive(void attribute((unused)) *extra) { |
a98fd571 RK |
119 | int files = 0; |
120 | ||
121 | gtk_tree_selection_selected_foreach(choose_selection, | |
122 | choose_play_sensitive_callback, | |
123 | &files); | |
124 | return files > 0; | |
487f69b2 | 125 | } |
a98fd571 RK |
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 | ||
a0e78d96 RK |
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 | ||
6982880f RK |
147 | |
148 | static void choose_play_activate(GtkMenuItem attribute((unused)) *item, | |
149 | gpointer attribute((unused)) userdata) { | |
a98fd571 RK |
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); | |
6982880f RK |
157 | } |
158 | ||
487f69b2 RK |
159 | static int choose_properties_sensitive(void *extra) { |
160 | return choose_play_sensitive(extra); | |
6982880f RK |
161 | } |
162 | ||
163 | static void choose_properties_activate(GtkMenuItem attribute((unused)) *item, | |
164 | gpointer attribute((unused)) userdata) { | |
487f69b2 RK |
165 | struct vector v[1]; |
166 | vector_init(v); | |
a98fd571 RK |
167 | gtk_tree_selection_selected_foreach(choose_selection, |
168 | choose_gather_selected_files_callback, | |
169 | v); | |
487f69b2 | 170 | properties(v->nvec, (const char **)v->vec); |
6982880f RK |
171 | } |
172 | ||
a0e78d96 RK |
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 | ||
6982880f RK |
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 | { | |
a0e78d96 RK |
276 | "Select children", |
277 | choose_selectchildren_activate, | |
278 | choose_selectchildren_sensitive, | |
6982880f RK |
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 */ | |
487f69b2 RK |
308 | ensure_selected(GTK_TREE_VIEW(choose_view), event); |
309 | choose_play_activate(NULL, NULL); | |
6982880f RK |
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 | */ |