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 | ||
e8a26cdd RK |
28 | /** @brief Recursion step for choose_get_visible() |
29 | * @param parent A visible node, or NULL for the root | |
a98fd571 RK |
30 | * @param callback Called for each visible node |
31 | * @param userdata Passed to @p callback | |
32 | * | |
33 | * If @p callback returns nonzero, the walk stops immediately. | |
e8a26cdd | 34 | */ |
a98fd571 RK |
35 | static int choose_visible_recurse(GtkTreeIter *parent, |
36 | int (*callback)(GtkTreeIter *it, | |
37 | int isfile, | |
38 | void *userdata), | |
39 | void *userdata) { | |
e8a26cdd RK |
40 | int expanded; |
41 | if(parent) { | |
a98fd571 RK |
42 | /* Skip placeholders */ |
43 | if(choose_is_placeholder(parent)) | |
44 | return 0; | |
45 | const int isfile = choose_is_file(parent); | |
46 | if(callback(parent, isfile, userdata)) | |
47 | return 1; | |
48 | if(isfile) | |
49 | return 0; /* Files never have children */ | |
e8a26cdd RK |
50 | GtkTreePath *parent_path |
51 | = gtk_tree_model_get_path(GTK_TREE_MODEL(choose_store), | |
52 | parent); | |
53 | expanded = gtk_tree_view_row_expanded(GTK_TREE_VIEW(choose_view), | |
54 | parent_path); | |
55 | gtk_tree_path_free(parent_path); | |
56 | } else | |
57 | expanded = 1; | |
58 | /* See if parent is expanded */ | |
59 | if(expanded) { | |
60 | /* Parent is expanded, visit all its children */ | |
61 | GtkTreeIter it[1]; | |
62 | gboolean itv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store), | |
63 | it, | |
64 | parent); | |
65 | while(itv) { | |
a98fd571 RK |
66 | if(choose_visible_recurse(it, callback, userdata)) |
67 | return TRUE; | |
e8a26cdd RK |
68 | itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it); |
69 | } | |
70 | } | |
a98fd571 | 71 | return 0; |
e8a26cdd RK |
72 | } |
73 | ||
a98fd571 RK |
74 | static void choose_visible_visit(int (*callback)(GtkTreeIter *it, |
75 | int isfile, | |
76 | void *userdata), | |
e8a26cdd RK |
77 | void *userdata) { |
78 | choose_visible_recurse(NULL, callback, userdata); | |
79 | } | |
80 | ||
a98fd571 RK |
81 | static int choose_selectall_sensitive_callback |
82 | (GtkTreeIter attribute((unused)) *it, | |
83 | int isfile, | |
e8a26cdd | 84 | void *userdata) { |
a98fd571 RK |
85 | if(isfile) { |
86 | *(int *)userdata = 1; | |
87 | return 1; | |
88 | } | |
89 | return 0; | |
e8a26cdd RK |
90 | } |
91 | ||
a98fd571 RK |
92 | /** @brief Should 'select all' be sensitive? |
93 | * | |
94 | * Yes if there are visible files. | |
95 | */ | |
6982880f | 96 | static int choose_selectall_sensitive(void attribute((unused)) *extra) { |
e8a26cdd RK |
97 | int files = 0; |
98 | choose_visible_visit(choose_selectall_sensitive_callback, &files); | |
99 | return files > 0; | |
6982880f | 100 | } |
e8a26cdd | 101 | |
a98fd571 | 102 | static int choose_selectall_activate_callback |
e8a26cdd | 103 | (GtkTreeIter *it, |
a98fd571 | 104 | int isfile, |
e8a26cdd | 105 | void attribute((unused)) *userdata) { |
a98fd571 | 106 | if(isfile) |
e8a26cdd RK |
107 | gtk_tree_selection_select_iter(choose_selection, it); |
108 | else | |
109 | gtk_tree_selection_unselect_iter(choose_selection, it); | |
a98fd571 | 110 | return 0; |
e8a26cdd RK |
111 | } |
112 | ||
a98fd571 RK |
113 | /** @brief Activate select all |
114 | * | |
115 | * Selects all files and deselects everything else. | |
116 | */ | |
6982880f RK |
117 | static void choose_selectall_activate(GtkMenuItem attribute((unused)) *item, |
118 | gpointer attribute((unused)) userdata) { | |
e8a26cdd | 119 | choose_visible_visit(choose_selectall_activate_callback, 0); |
6982880f | 120 | } |
a98fd571 RK |
121 | |
122 | /** @brief Should 'select none' be sensitive | |
123 | * | |
124 | * Yes if anything is selected. | |
125 | */ | |
6982880f RK |
126 | static int choose_selectnone_sensitive(void attribute((unused)) *extra) { |
127 | return gtk_tree_selection_count_selected_rows(choose_selection) > 0; | |
128 | } | |
a98fd571 RK |
129 | |
130 | /** @brief Activate select none */ | |
6982880f RK |
131 | static void choose_selectnone_activate(GtkMenuItem attribute((unused)) *item, |
132 | gpointer attribute((unused)) userdata) { | |
133 | gtk_tree_selection_unselect_all(choose_selection); | |
134 | } | |
a98fd571 RK |
135 | |
136 | static void choose_play_sensitive_callback(GtkTreeModel attribute((unused)) *model, | |
137 | GtkTreePath attribute((unused)) *path, | |
138 | GtkTreeIter *iter, | |
139 | gpointer data) { | |
140 | int *filesp = data; | |
141 | ||
142 | if(*filesp == -1) | |
143 | return; | |
144 | if(choose_is_dir(iter)) | |
145 | *filesp = -1; | |
146 | else if(choose_is_file(iter)) | |
147 | ++*filesp; | |
148 | } | |
149 | ||
150 | /** @brief Should 'play' be sensitive? | |
151 | * | |
152 | * Yes if tracks are selected and no directories are */ | |
6982880f | 153 | static int choose_play_sensitive(void attribute((unused)) *extra) { |
a98fd571 RK |
154 | int files = 0; |
155 | ||
156 | gtk_tree_selection_selected_foreach(choose_selection, | |
157 | choose_play_sensitive_callback, | |
158 | &files); | |
159 | return files > 0; | |
487f69b2 | 160 | } |
a98fd571 RK |
161 | |
162 | static void choose_gather_selected_files_callback(GtkTreeModel attribute((unused)) *model, | |
163 | GtkTreePath attribute((unused)) *path, | |
164 | GtkTreeIter *iter, | |
165 | gpointer data) { | |
166 | struct vector *v = data; | |
167 | ||
168 | if(choose_is_file(iter)) | |
169 | vector_append(v, choose_get_track(iter)); | |
170 | } | |
171 | ||
6982880f RK |
172 | |
173 | static void choose_play_activate(GtkMenuItem attribute((unused)) *item, | |
174 | gpointer attribute((unused)) userdata) { | |
a98fd571 RK |
175 | struct vector v[1]; |
176 | vector_init(v); | |
177 | gtk_tree_selection_selected_foreach(choose_selection, | |
178 | choose_gather_selected_files_callback, | |
179 | v); | |
180 | for(int n = 0; n < v->nvec; ++n) | |
181 | disorder_eclient_play(client, v->vec[n], choose_play_completed, 0); | |
6982880f RK |
182 | } |
183 | ||
487f69b2 RK |
184 | static int choose_properties_sensitive(void *extra) { |
185 | return choose_play_sensitive(extra); | |
6982880f RK |
186 | } |
187 | ||
188 | static void choose_properties_activate(GtkMenuItem attribute((unused)) *item, | |
189 | gpointer attribute((unused)) userdata) { | |
487f69b2 RK |
190 | struct vector v[1]; |
191 | vector_init(v); | |
a98fd571 RK |
192 | gtk_tree_selection_selected_foreach(choose_selection, |
193 | choose_gather_selected_files_callback, | |
194 | v); | |
487f69b2 | 195 | properties(v->nvec, (const char **)v->vec); |
6982880f RK |
196 | } |
197 | ||
198 | /** @brief Pop-up menu for choose */ | |
199 | static struct menuitem choose_menuitems[] = { | |
200 | { | |
201 | "Play track", | |
202 | choose_play_activate, | |
203 | choose_play_sensitive, | |
204 | 0, | |
205 | 0 | |
206 | }, | |
207 | { | |
208 | "Track properties", | |
209 | choose_properties_activate, | |
210 | choose_properties_sensitive, | |
211 | 0, | |
212 | 0 | |
213 | }, | |
214 | { | |
215 | "Select all tracks", | |
216 | choose_selectall_activate, | |
217 | choose_selectall_sensitive, | |
218 | 0, | |
219 | 0 | |
220 | }, | |
221 | { | |
222 | "Deselect all tracks", | |
223 | choose_selectnone_activate, | |
224 | choose_selectnone_sensitive, | |
225 | 0, | |
226 | 0 | |
227 | }, | |
228 | }; | |
229 | ||
230 | const struct tabtype choose_tabtype = { | |
231 | choose_properties_sensitive, | |
232 | choose_selectall_sensitive, | |
233 | choose_selectnone_sensitive, | |
234 | choose_properties_activate, | |
235 | choose_selectall_activate, | |
236 | choose_selectnone_activate, | |
237 | 0, | |
238 | 0 | |
239 | }; | |
240 | ||
241 | /** @brief Called when a mouse button is pressed or released */ | |
242 | gboolean choose_button_event(GtkWidget attribute((unused)) *widget, | |
243 | GdkEventButton *event, | |
244 | gpointer attribute((unused)) user_data) { | |
245 | if(event->type == GDK_BUTTON_RELEASE && event->button == 2) { | |
246 | /* Middle click release - play track */ | |
487f69b2 RK |
247 | ensure_selected(GTK_TREE_VIEW(choose_view), event); |
248 | choose_play_activate(NULL, NULL); | |
6982880f RK |
249 | } else if(event->type == GDK_BUTTON_PRESS && event->button == 3) { |
250 | /* Right click press - pop up the menu */ | |
251 | ensure_selected(GTK_TREE_VIEW(choose_view), event); | |
252 | popup(&choose_menu, event, | |
253 | choose_menuitems, sizeof choose_menuitems / sizeof *choose_menuitems, | |
254 | 0); | |
255 | return TRUE; | |
256 | } | |
257 | return FALSE; | |
258 | } | |
259 | ||
260 | /* | |
261 | Local Variables: | |
262 | c-basic-offset:2 | |
263 | comment-column:40 | |
264 | fill-column:79 | |
265 | indent-tabs-mode:nil | |
266 | End: | |
267 | */ |