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