Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder | |
ebcfbfef | 3 | * Copyright (C) 2008 Richard Kettlewell |
460b9539 | 4 | * |
e7eb3a27 | 5 | * This program is free software: you can redistribute it and/or modify |
460b9539 | 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 |
460b9539 | 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 | * | |
460b9539 | 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/>. |
460b9539 | 17 | */ |
b9bdafc7 RK |
18 | /** @file disobedience/choose.c |
19 | * @brief Hierarchical track selection and search | |
20 | * | |
ebcfbfef RK |
21 | * We now use an ordinary GtkTreeStore/GtkTreeView. |
22 | * | |
ebcfbfef RK |
23 | * We don't want to pull the entire tree in memory, but we want directories to |
24 | * show up as having children. Therefore we give directories a placeholder | |
a98fd571 RK |
25 | * child and replace their children when they are opened. Placeholders have |
26 | * TRACK_COLUMN="" and ISFILE_COLUMN=FALSE (so that they don't get check boxes, | |
27 | * lengths, etc). | |
ebcfbfef | 28 | * |
ebcfbfef | 29 | * TODO: |
d9a92920 | 30 | * - sweep up contracted nodes, replacing their content with a placeholder |
b9bdafc7 | 31 | */ |
460b9539 | 32 | |
33 | #include "disobedience.h" | |
6982880f | 34 | #include "choose.h" |
ff18efce | 35 | #include "multidrag.h" |
a6712ea8 | 36 | #include "queue-generic.h" |
a59663d4 | 37 | #include <gdk/gdkkeysyms.h> |
16e145a5 | 38 | |
ff18efce | 39 | /** @brief Drag types */ |
a6712ea8 | 40 | const GtkTargetEntry choose_targets[] = { |
ff18efce | 41 | { |
a6712ea8 | 42 | PLAYABLE_TRACKS, /* drag type */ |
ff18efce | 43 | GTK_TARGET_SAME_APP|GTK_TARGET_OTHER_WIDGET, /* copying between widgets */ |
a6712ea8 | 44 | PLAYABLE_TRACKS_ID /* ID value */ |
ff18efce | 45 | }, |
a6712ea8 RK |
46 | { |
47 | .target = NULL | |
48 | } | |
ff18efce RK |
49 | }; |
50 | ||
ebcfbfef | 51 | /** @brief The current selection tree */ |
6982880f | 52 | GtkTreeStore *choose_store; |
16e145a5 | 53 | |
ebcfbfef | 54 | /** @brief The view onto the selection tree */ |
6982880f | 55 | GtkWidget *choose_view; |
16e145a5 | 56 | |
ebcfbfef | 57 | /** @brief The selection tree's selection */ |
6982880f | 58 | GtkTreeSelection *choose_selection; |
460b9539 | 59 | |
2a9a65e4 RK |
60 | /** @brief Count of file listing operations in flight */ |
61 | static int choose_list_in_flight; | |
62 | ||
bd802f22 RK |
63 | /** @brief If nonzero autocollapse column won't be set */ |
64 | static int choose_suppress_set_autocollapse; | |
65 | ||
a98fd571 RK |
66 | static char *choose_get_string(GtkTreeIter *iter, int column) { |
67 | gchar *gs; | |
68 | gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter, | |
69 | column, &gs, | |
70 | -1); | |
71 | char *s = xstrdup(gs); | |
72 | g_free(gs); | |
73 | return s; | |
6a06a735 RK |
74 | } |
75 | ||
a98fd571 RK |
76 | char *choose_get_track(GtkTreeIter *iter) { |
77 | char *s = choose_get_string(iter, TRACK_COLUMN); | |
78 | return *s ? s : 0; /* Placeholder -> NULL */ | |
79 | } | |
80 | ||
81 | char *choose_get_sort(GtkTreeIter *iter) { | |
82 | return choose_get_string(iter, SORT_COLUMN); | |
83 | } | |
84 | ||
659201af RK |
85 | char *choose_get_display(GtkTreeIter *iter) { |
86 | return choose_get_string(iter, NAME_COLUMN); | |
87 | } | |
88 | ||
a98fd571 RK |
89 | int choose_is_file(GtkTreeIter *iter) { |
90 | gboolean isfile; | |
91 | gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter, | |
92 | ISFILE_COLUMN, &isfile, | |
93 | -1); | |
94 | return isfile; | |
95 | } | |
96 | ||
97 | int choose_is_dir(GtkTreeIter *iter) { | |
98 | gboolean isfile; | |
99 | gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter, | |
100 | ISFILE_COLUMN, &isfile, | |
101 | -1); | |
102 | if(isfile) | |
103 | return FALSE; | |
104 | return !choose_is_placeholder(iter); | |
105 | } | |
106 | ||
107 | int choose_is_placeholder(GtkTreeIter *iter) { | |
108 | return choose_get_string(iter, TRACK_COLUMN)[0] == 0; | |
e8a26cdd RK |
109 | } |
110 | ||
bd802f22 RK |
111 | int choose_can_autocollapse(GtkTreeIter *iter) { |
112 | gboolean autocollapse; | |
113 | gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter, | |
114 | AUTOCOLLAPSE_COLUMN, &autocollapse, | |
115 | -1); | |
116 | return autocollapse; | |
117 | } | |
118 | ||
ebcfbfef | 119 | /** @brief Remove node @p it and all its children |
59cf25c4 | 120 | * @param it Iterator, updated to point to next |
ebcfbfef | 121 | * @return True if iterator remains valid |
659201af RK |
122 | * |
123 | * TODO is this necessary? gtk_tree_store_remove() does not document what | |
124 | * happens to children. | |
16e145a5 | 125 | */ |
ebcfbfef RK |
126 | static gboolean choose_remove_node(GtkTreeIter *it) { |
127 | GtkTreeIter child[1]; | |
128 | gboolean childv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store), | |
129 | child, | |
130 | it); | |
131 | while(childv) | |
132 | childv = choose_remove_node(child); | |
ebcfbfef RK |
133 | return gtk_tree_store_remove(choose_store, it); |
134 | } | |
135 | ||
ad47bd4c RK |
136 | /** @brief Update length and state fields */ |
137 | static gboolean choose_set_state_callback(GtkTreeModel attribute((unused)) *model, | |
138 | GtkTreePath attribute((unused)) *path, | |
139 | GtkTreeIter *it, | |
140 | gpointer attribute((unused)) data) { | |
a98fd571 RK |
141 | if(choose_is_file(it)) { |
142 | const char *track = choose_get_track(it); | |
143 | const long l = namepart_length(track); | |
ad47bd4c RK |
144 | char length[64]; |
145 | if(l > 0) | |
146 | byte_snprintf(length, sizeof length, "%ld:%02ld", l / 60, l % 60); | |
147 | else | |
148 | length[0] = 0; | |
149 | gtk_tree_store_set(choose_store, it, | |
150 | LENGTH_COLUMN, length, | |
a98fd571 | 151 | STATE_COLUMN, queued(track), |
ad47bd4c | 152 | -1); |
cfa78eaa RK |
153 | if(choose_is_search_result(track)) |
154 | gtk_tree_store_set(choose_store, it, | |
b96f65cf RK |
155 | BG_COLUMN, SEARCH_RESULT_BG, |
156 | FG_COLUMN, SEARCH_RESULT_FG, | |
cfa78eaa RK |
157 | -1); |
158 | else | |
159 | gtk_tree_store_set(choose_store, it, | |
160 | BG_COLUMN, (char *)0, | |
161 | FG_COLUMN, (char *)0, | |
162 | -1); | |
ad47bd4c RK |
163 | } |
164 | return FALSE; /* continue walking */ | |
165 | } | |
166 | ||
167 | /** @brief Called when the queue or playing track change */ | |
168 | static void choose_set_state(const char attribute((unused)) *event, | |
169 | void attribute((unused)) *eventdata, | |
170 | void attribute((unused)) *callbackdata) { | |
171 | gtk_tree_model_foreach(GTK_TREE_MODEL(choose_store), | |
172 | choose_set_state_callback, | |
173 | NULL); | |
174 | } | |
175 | ||
ebcfbfef RK |
176 | /** @brief (Re-)populate a node |
177 | * @param parent_ref Node to populate or NULL to fill root | |
178 | * @param nvec Number of children to add | |
179 | * @param vec Children | |
59cf25c4 | 180 | * @param isfile 1 if children are files, 0 if directories |
b9bdafc7 | 181 | * |
ebcfbfef RK |
182 | * Adjusts the set of files (or directories) below @p parent_ref to match those |
183 | * listed in @p nvec and @p vec. | |
fcc8b9f7 | 184 | * |
59cf25c4 | 185 | * @p parent_ref will be destroyed. |
fcc8b9f7 | 186 | */ |
ebcfbfef RK |
187 | static void choose_populate(GtkTreeRowReference *parent_ref, |
188 | int nvec, char **vec, | |
a98fd571 | 189 | int isfile) { |
2fadbbc6 | 190 | const char *type = isfile ? "track" : "dir"; |
d848c36f RK |
191 | //fprintf(stderr, "%d new children of type %s\n", nvec, type); |
192 | if(!nvec) | |
193 | goto skip; | |
ebcfbfef RK |
194 | /* Compute parent_* */ |
195 | GtkTreeIter pit[1], *parent_it; | |
196 | GtkTreePath *parent_path; | |
197 | if(parent_ref) { | |
198 | parent_path = gtk_tree_row_reference_get_path(parent_ref); | |
199 | parent_it = pit; | |
200 | gboolean pitv = gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store), | |
201 | pit, parent_path); | |
202 | assert(pitv); | |
203 | /*fprintf(stderr, "choose_populate %s: parent path is [%s]\n", | |
2fadbbc6 | 204 | type, |
ebcfbfef | 205 | gtk_tree_path_to_string(parent_path));*/ |
460b9539 | 206 | } else { |
ebcfbfef RK |
207 | parent_path = 0; |
208 | parent_it = 0; | |
209 | /*fprintf(stderr, "choose_populate %s: populating the root\n", | |
2fadbbc6 | 210 | type);*/ |
ebcfbfef | 211 | } |
659201af RK |
212 | /* Both td[] and the current node set are sorted so we can do a single linear |
213 | * pass to insert new nodes and remove unwanted ones. The total performance | |
214 | * may be worse than linear depending on the performance of GTK+'s insert and | |
215 | * delete operations. */ | |
216 | //fprintf(stderr, "sorting tracks\n"); | |
2fadbbc6 | 217 | struct tracksort_data *td = tracksort_init(nvec, vec, type); |
ebcfbfef RK |
218 | GtkTreeIter it[1]; |
219 | gboolean itv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store), | |
220 | it, | |
221 | parent_it); | |
659201af RK |
222 | int inserted = 0, deleted_placeholder = 0; |
223 | //fprintf(stderr, "inserting tracks type=%s\n", type); | |
224 | while(nvec > 0 || itv) { | |
225 | /*fprintf(stderr, "td[] = %s, it=%s [%s]\n", | |
226 | nvec > 0 ? td->track : "(none)", | |
227 | itv ? choose_get_track(it) : "(!itv)", | |
228 | itv ? (choose_is_file(it) ? "file" : "dir") : "");*/ | |
229 | enum { INSERT, DELETE, SKIP_TREE, SKIP_BOTH } action; | |
230 | const char *track = itv ? choose_get_track(it) : 0; | |
231 | if(itv && !track) { | |
232 | //fprintf(stderr, " placeholder\n"); | |
233 | action = DELETE; | |
234 | ++deleted_placeholder; | |
235 | } else if(nvec > 0 && itv) { | |
236 | /* There's both a tree row and a td[] entry */ | |
237 | const int cmp = compare_tracks(td->sort, choose_get_sort(it), | |
238 | td->display, choose_get_display(it), | |
239 | td->track, track); | |
240 | //fprintf(stderr, " cmp=%d\n", cmp); | |
241 | if(cmp < 0) | |
242 | /* td < it, so we insert td before it */ | |
243 | action = INSERT; | |
244 | else if(cmp > 0) { | |
245 | /* td > it, so we must either delete it (if the same type) or skip it */ | |
246 | if(choose_is_file(it) == isfile) | |
247 | action = DELETE; | |
248 | else | |
249 | action = SKIP_TREE; | |
250 | } else | |
251 | /* td = it, so we step past both */ | |
252 | action = SKIP_BOTH; | |
253 | } else if(nvec > 0) { | |
254 | /* We've reached the end of the tree rows, but new have tracks left in | |
255 | * td[] */ | |
256 | //fprintf(stderr, " inserting\n"); | |
257 | action = INSERT; | |
6a06a735 | 258 | } else { |
659201af RK |
259 | /* We've reached the end of the new tracks from td[], but there are |
260 | * further tracks in the tree */ | |
261 | //fprintf(stderr, " deleting\n"); | |
d62c03cd RK |
262 | if(choose_is_file(it) == isfile) |
263 | action = DELETE; | |
264 | else | |
265 | action = SKIP_TREE; | |
460b9539 | 266 | } |
659201af RK |
267 | |
268 | switch(action) { | |
269 | case INSERT: { | |
270 | //fprintf(stderr, " INSERT %s\n", td->track); | |
271 | /* Insert a new row from td[] before it, or at the end if it is no longer | |
272 | * valid */ | |
273 | GtkTreeIter child[1]; | |
274 | gtk_tree_store_insert_before(choose_store, | |
275 | child, /* new row */ | |
276 | parent_it, /* parent */ | |
277 | itv ? it : NULL); /* successor */ | |
278 | gtk_tree_store_set(choose_store, child, | |
279 | NAME_COLUMN, td->display, | |
a98fd571 | 280 | ISFILE_COLUMN, isfile, |
659201af RK |
281 | TRACK_COLUMN, td->track, |
282 | SORT_COLUMN, td->sort, | |
bd802f22 | 283 | AUTOCOLLAPSE_COLUMN, FALSE, |
ebcfbfef | 284 | -1); |
ad47bd4c RK |
285 | /* Update length and state; we expect this to kick off length lookups |
286 | * rather than necessarily get the right value the first time round. */ | |
659201af | 287 | choose_set_state_callback(0, 0, child, 0); |
ebcfbfef RK |
288 | /* If we inserted a directory, insert a placeholder too, so it appears to |
289 | * have children; it will be deleted when we expand the directory. */ | |
a98fd571 | 290 | if(!isfile) { |
ebcfbfef RK |
291 | //fprintf(stderr, " inserting a placeholder\n"); |
292 | GtkTreeIter placeholder[1]; | |
293 | ||
659201af | 294 | gtk_tree_store_append(choose_store, placeholder, child); |
ebcfbfef RK |
295 | gtk_tree_store_set(choose_store, placeholder, |
296 | NAME_COLUMN, "Waddling...", | |
a98fd571 RK |
297 | TRACK_COLUMN, "", |
298 | ISFILE_COLUMN, FALSE, | |
ebcfbfef | 299 | -1); |
e9b70a84 | 300 | } |
659201af RK |
301 | ++inserted; |
302 | ++td; | |
303 | --nvec; | |
304 | break; | |
305 | } | |
306 | case SKIP_BOTH: | |
307 | //fprintf(stderr, " SKIP_BOTH\n"); | |
308 | ++td; | |
309 | --nvec; | |
310 | /* fall thru */ | |
311 | case SKIP_TREE: | |
312 | //fprintf(stderr, " SKIP_TREE\n"); | |
313 | itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it); | |
314 | break; | |
315 | case DELETE: | |
316 | //fprintf(stderr, " DELETE\n"); | |
317 | itv = choose_remove_node(it); | |
318 | break; | |
460b9539 | 319 | } |
460b9539 | 320 | } |
659201af RK |
321 | /*fprintf(stderr, "inserted=%d deleted_placeholder=%d\n\n", |
322 | inserted, deleted_placeholder);*/ | |
ebcfbfef RK |
323 | if(parent_ref) { |
324 | /* If we deleted a placeholder then we must re-expand the row */ | |
bd802f22 RK |
325 | if(deleted_placeholder) { |
326 | ++choose_suppress_set_autocollapse; | |
659201af | 327 | gtk_tree_view_expand_row(GTK_TREE_VIEW(choose_view), parent_path, FALSE); |
bd802f22 RK |
328 | --choose_suppress_set_autocollapse; |
329 | } | |
ebcfbfef RK |
330 | gtk_tree_row_reference_free(parent_ref); |
331 | gtk_tree_path_free(parent_path); | |
460b9539 | 332 | } |
cab9a17c | 333 | skip: |
2a9a65e4 RK |
334 | /* We only notify others that we've inserted tracks when there are no more |
335 | * insertions pending, so that they don't have to keep track of how many | |
336 | * requests they've made. */ | |
2a9a65e4 RK |
337 | if(--choose_list_in_flight == 0) { |
338 | /* Notify interested parties that we inserted some tracks, AFTER making | |
339 | * sure that the row is properly expanded */ | |
cab9a17c RK |
340 | //fprintf(stderr, "raising choose-more-tracks\n"); |
341 | event_raise("choose-more-tracks", 0); | |
2a9a65e4 | 342 | } |
d848c36f | 343 | //fprintf(stderr, "choose_list_in_flight -> %d-\n", choose_list_in_flight); |
460b9539 | 344 | } |
345 | ||
ebcfbfef | 346 | static void choose_dirs_completed(void *v, |
abf99697 | 347 | const char *err, |
ebcfbfef | 348 | int nvec, char **vec) { |
abf99697 RK |
349 | if(err) { |
350 | popup_protocol_error(0, err); | |
ebcfbfef | 351 | return; |
460b9539 | 352 | } |
a98fd571 | 353 | choose_populate(v, nvec, vec, 0/*!isfile*/); |
460b9539 | 354 | } |
355 | ||
ebcfbfef | 356 | static void choose_files_completed(void *v, |
abf99697 | 357 | const char *err, |
ebcfbfef | 358 | int nvec, char **vec) { |
abf99697 RK |
359 | if(err) { |
360 | popup_protocol_error(0, err); | |
ebcfbfef | 361 | return; |
460b9539 | 362 | } |
a98fd571 | 363 | choose_populate(v, nvec, vec, 1/*isfile*/); |
460b9539 | 364 | } |
365 | ||
a3602333 | 366 | void choose_play_completed(void attribute((unused)) *v, |
abf99697 RK |
367 | const char *err) { |
368 | if(err) | |
369 | popup_protocol_error(0, err); | |
a3602333 RK |
370 | } |
371 | ||
372 | static void choose_state_toggled | |
373 | (GtkCellRendererToggle attribute((unused)) *cell_renderer, | |
374 | gchar *path_str, | |
375 | gpointer attribute((unused)) user_data) { | |
376 | GtkTreeIter it[1]; | |
377 | /* Identify the track */ | |
378 | gboolean itv = | |
379 | gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(choose_store), | |
380 | it, | |
381 | path_str); | |
382 | if(!itv) | |
383 | return; | |
a98fd571 | 384 | if(!choose_is_file(it)) |
a3602333 | 385 | return; |
a98fd571 RK |
386 | const char *track = choose_get_track(it); |
387 | if(queued(track)) | |
a3602333 | 388 | return; |
a98fd571 | 389 | disorder_eclient_play(client, track, choose_play_completed, 0); |
a3602333 RK |
390 | |
391 | } | |
392 | ||
00e0c652 RK |
393 | /** @brief (Re-)get the children of @p path |
394 | * @param path Path to target row | |
395 | * @param iter Iterator pointing at target row | |
396 | * | |
397 | * Called from choose_row_expanded() to make sure that the contents are present | |
398 | * and from choose_refill_callback() to (re-)synchronize. | |
399 | */ | |
400 | static void choose_refill_row(GtkTreePath *path, | |
401 | GtkTreeIter *iter) { | |
a98fd571 | 402 | const char *track = choose_get_track(iter); |
ebcfbfef | 403 | disorder_eclient_files(client, choose_files_completed, |
a98fd571 | 404 | track, |
ebcfbfef RK |
405 | NULL, |
406 | gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store), | |
407 | path)); | |
408 | disorder_eclient_dirs(client, choose_dirs_completed, | |
a98fd571 | 409 | track, |
ebcfbfef RK |
410 | NULL, |
411 | gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store), | |
412 | path)); | |
413 | /* The row references are destroyed in the _completed handlers. */ | |
2a9a65e4 | 414 | choose_list_in_flight += 2; |
00e0c652 RK |
415 | } |
416 | ||
417 | static void choose_row_expanded(GtkTreeView attribute((unused)) *treeview, | |
418 | GtkTreeIter *iter, | |
419 | GtkTreePath *path, | |
420 | gpointer attribute((unused)) user_data) { | |
421 | /*fprintf(stderr, "row-expanded path=[%s]\n\n", | |
422 | gtk_tree_path_to_string(path));*/ | |
423 | /* We update a node's contents whenever it is expanded, even if it was | |
424 | * already populated; the effect is that contracting and expanding a node | |
425 | * suffices to update it to the latest state on the server. */ | |
426 | choose_refill_row(path, iter); | |
bd802f22 RK |
427 | if(!choose_suppress_set_autocollapse) { |
428 | if(choose_auto_expanding) { | |
429 | /* This was an automatic expansion; mark it the row for auto-collapse. */ | |
430 | gtk_tree_store_set(choose_store, iter, | |
431 | AUTOCOLLAPSE_COLUMN, TRUE, | |
432 | -1); | |
433 | /*fprintf(stderr, "enable auto-collapse for %s\n", | |
434 | gtk_tree_path_to_string(path));*/ | |
435 | } else { | |
436 | /* This was a manual expansion. Inhibit automatic collapse on this row | |
437 | * and all its ancestors. */ | |
438 | gboolean itv; | |
439 | do { | |
440 | gtk_tree_store_set(choose_store, iter, | |
441 | AUTOCOLLAPSE_COLUMN, FALSE, | |
442 | -1); | |
443 | /*fprintf(stderr, "suppress auto-collapse for %s\n", | |
444 | gtk_tree_model_get_string_from_iter(GTK_TREE_MODEL(choose_store), | |
445 | iter));*/ | |
446 | GtkTreeIter child = *iter; | |
447 | itv = gtk_tree_model_iter_parent(GTK_TREE_MODEL(choose_store), | |
448 | iter, | |
449 | &child); | |
450 | } while(itv); | |
451 | /* The effect of this is that if you expand a row that's actually a | |
452 | * sibling of the real target of the auto-expansion, it stays expanded | |
453 | * when you clear a search. That's find and good, but it _still_ stays | |
454 | * expanded if you expand it and then collapse it. | |
455 | * | |
456 | * An alternative policy would be to only auto-collapse rows that don't | |
457 | * have any expanded children (apart from ones also subject to | |
458 | * auto-collapse). I'm not sure what the most usable policy is. | |
459 | */ | |
460 | } | |
461 | } | |
462 | } | |
463 | ||
464 | static void choose_auto_collapse_callback(GtkTreeView *tree_view, | |
465 | GtkTreePath *path, | |
466 | gpointer attribute((unused)) user_data) { | |
467 | GtkTreeIter it[1]; | |
468 | ||
469 | gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store), it, path); | |
470 | if(choose_can_autocollapse(it)) { | |
471 | /*fprintf(stderr, "collapse %s\n", | |
472 | gtk_tree_path_to_string(path));*/ | |
473 | gtk_tree_store_set(choose_store, it, | |
474 | AUTOCOLLAPSE_COLUMN, FALSE, | |
475 | -1); | |
476 | gtk_tree_view_collapse_row(tree_view, path); | |
477 | } | |
478 | } | |
479 | ||
480 | /** @brief Perform automatic collapse after a search is cleared */ | |
481 | void choose_auto_collapse(void) { | |
482 | gtk_tree_view_map_expanded_rows(GTK_TREE_VIEW(choose_view), | |
483 | choose_auto_collapse_callback, | |
484 | 0); | |
ebcfbfef RK |
485 | } |
486 | ||
00e0c652 RK |
487 | /** @brief Called from choose_refill() with each expanded row */ |
488 | static void choose_refill_callback(GtkTreeView attribute((unused)) *tree_view, | |
489 | GtkTreePath *path, | |
490 | gpointer attribute((unused)) user_data) { | |
491 | GtkTreeIter it[1]; | |
492 | ||
493 | gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store), it, path); | |
494 | choose_refill_row(path, it); | |
495 | } | |
496 | ||
497 | /** @brief Synchronize all visible data with the server | |
498 | * | |
499 | * Called at startup, when a rescan completes, and via periodic_slow(). | |
500 | */ | |
501 | static void choose_refill(const char attribute((unused)) *event, | |
502 | void attribute((unused)) *eventdata, | |
503 | void attribute((unused)) *callbackdata) { | |
504 | //fprintf(stderr, "choose_refill\n"); | |
505 | /* Update the root */ | |
506 | disorder_eclient_files(client, choose_files_completed, "", NULL, NULL); | |
507 | disorder_eclient_dirs(client, choose_dirs_completed, "", NULL, NULL); | |
508 | choose_list_in_flight += 2; | |
509 | /* Update all expanded rows */ | |
510 | gtk_tree_view_map_expanded_rows(GTK_TREE_VIEW(choose_view), | |
511 | choose_refill_callback, | |
512 | 0); | |
513 | //fprintf(stderr, "choose_list_in_flight -> %d+\n", choose_list_in_flight); | |
514 | } | |
515 | ||
a59663d4 | 516 | /** @brief Called for key-*-event on the main view |
a59663d4 RK |
517 | */ |
518 | static gboolean choose_key_event(GtkWidget attribute((unused)) *widget, | |
519 | GdkEventKey *event, | |
c10dd9af | 520 | gpointer user_data) { |
a59663d4 RK |
521 | /*fprintf(stderr, "choose_key_event type=%d state=%#x keyval=%#x\n", |
522 | event->type, event->state, event->keyval);*/ | |
523 | switch(event->keyval) { | |
524 | case GDK_Page_Up: | |
525 | case GDK_Page_Down: | |
526 | case GDK_Up: | |
527 | case GDK_Down: | |
528 | case GDK_Home: | |
529 | case GDK_End: | |
530 | return FALSE; /* We'll take these */ | |
531 | case 'f': case 'F': | |
532 | /* ^F is expected to start a search. We implement this by focusing the | |
533 | * search entry box. */ | |
534 | if((event->state & ~(GDK_LOCK_MASK|GDK_SHIFT_MASK)) == GDK_CONTROL_MASK | |
535 | && event->type == GDK_KEY_PRESS) { | |
4d6a5c4e RK |
536 | choose_search_new(); |
537 | return TRUE; /* Handled it */ | |
a59663d4 RK |
538 | } |
539 | break; | |
540 | case 'g': case 'G': | |
541 | /* ^G is expected to go the next match. We simulate a click on the 'next' | |
542 | * button. */ | |
543 | if((event->state & ~(GDK_LOCK_MASK|GDK_SHIFT_MASK)) == GDK_CONTROL_MASK | |
544 | && event->type == GDK_KEY_PRESS) { | |
545 | choose_next_clicked(0, 0); | |
4d6a5c4e | 546 | return TRUE; /* Handled it */ |
a59663d4 RK |
547 | } |
548 | break; | |
549 | } | |
c10dd9af | 550 | /* Anything not handled we redirected to the search entry field */ |
a59663d4 | 551 | gtk_widget_event(user_data, (GdkEvent *)event); |
4d6a5c4e | 552 | return TRUE; /* Handled it */ |
a59663d4 RK |
553 | } |
554 | ||
ff18efce RK |
555 | static gboolean choose_multidrag_predicate(GtkTreePath attribute((unused)) *path, |
556 | GtkTreeIter *iter) { | |
557 | return choose_is_file(iter); | |
558 | } | |
559 | ||
560 | ||
561 | /** @brief Callback to add selected tracks to the selection data | |
562 | * | |
563 | * Called from choose_drag_data_get(). | |
564 | */ | |
565 | static void choose_drag_data_get_collect(GtkTreeModel attribute((unused)) *model, | |
566 | GtkTreePath attribute((unused)) *path, | |
567 | GtkTreeIter *iter, | |
568 | gpointer data) { | |
569 | struct dynstr *const result = data; | |
570 | ||
571 | if(choose_is_file(iter)) { /* no diretories */ | |
572 | dynstr_append_string(result, ""); /* no ID */ | |
573 | dynstr_append(result, '\n'); | |
574 | dynstr_append_string(result, choose_get_track(iter)); | |
575 | dynstr_append(result, '\n'); | |
576 | } | |
577 | } | |
578 | ||
579 | /** @brief Called to extract the dragged data from the choose view | |
580 | * @param w Source widget (the tree view) | |
581 | * @param dc Drag context | |
582 | * @param data Where to put the answer | |
583 | * @param info_ Target @c info parameter | |
584 | * @param time_ Time data requested (for some reason not a @c time_t) | |
585 | * @param user_data The queuelike | |
586 | * | |
587 | * Closely analogous to ql_drag_data_get(), and uses the same data format. | |
588 | * IDs are sent as empty strings. | |
589 | */ | |
590 | static void choose_drag_data_get(GtkWidget *w, | |
591 | GdkDragContext attribute((unused)) *dc, | |
592 | GtkSelectionData *data, | |
593 | guint attribute((unused)) info_, | |
594 | guint attribute((unused)) time_, | |
595 | gpointer attribute((unused)) user_data) { | |
596 | struct dynstr result[1]; | |
597 | GtkTreeSelection *sel; | |
598 | ||
599 | sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(w)); | |
600 | dynstr_init(result); | |
601 | gtk_tree_selection_selected_foreach(sel, | |
602 | choose_drag_data_get_collect, | |
603 | result); | |
604 | gtk_selection_data_set(data, | |
605 | GDK_TARGET_STRING, | |
606 | 8, (guchar *)result->vec, result->nvec); | |
607 | } | |
608 | ||
ebcfbfef | 609 | /** @brief Create the choose tab */ |
460b9539 | 610 | GtkWidget *choose_widget(void) { |
ebcfbfef | 611 | /* Create the tree store. */ |
a98fd571 | 612 | choose_store = gtk_tree_store_new(CHOOSE_COLUMNS, |
ad47bd4c RK |
613 | G_TYPE_BOOLEAN, |
614 | G_TYPE_STRING, | |
ebcfbfef | 615 | G_TYPE_STRING, |
a3602333 | 616 | G_TYPE_BOOLEAN, |
a98fd571 | 617 | G_TYPE_STRING, |
cfa78eaa RK |
618 | G_TYPE_STRING, |
619 | G_TYPE_STRING, | |
bd802f22 RK |
620 | G_TYPE_STRING, |
621 | G_TYPE_BOOLEAN); | |
ebcfbfef RK |
622 | |
623 | /* Create the view */ | |
624 | choose_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(choose_store)); | |
ad47bd4c | 625 | gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(choose_view), TRUE); |
d9a92920 | 626 | /* Suppress built-in typeahead find, we do our own search support. */ |
26108f95 | 627 | gtk_tree_view_set_enable_search(GTK_TREE_VIEW(choose_view), FALSE); |
ebcfbfef RK |
628 | |
629 | /* Create cell renderers and columns */ | |
ad47bd4c RK |
630 | /* TODO use a table */ |
631 | { | |
e9a72659 | 632 | GtkCellRenderer *r = gtk_cell_renderer_toggle_new(); |
ad47bd4c | 633 | GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes |
e9a72659 | 634 | ("Queued", |
ad47bd4c | 635 | r, |
e9a72659 RK |
636 | "active", STATE_COLUMN, |
637 | "visible", ISFILE_COLUMN, | |
ad47bd4c RK |
638 | (char *)0); |
639 | gtk_tree_view_column_set_resizable(c, TRUE); | |
640 | gtk_tree_view_column_set_reorderable(c, TRUE); | |
ad47bd4c | 641 | gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c); |
e9a72659 RK |
642 | g_signal_connect(r, "toggled", |
643 | G_CALLBACK(choose_state_toggled), 0); | |
ad47bd4c RK |
644 | } |
645 | { | |
646 | GtkCellRenderer *r = gtk_cell_renderer_text_new(); | |
647 | GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes | |
648 | ("Length", | |
649 | r, | |
650 | "text", LENGTH_COLUMN, | |
651 | (char *)0); | |
652 | gtk_tree_view_column_set_resizable(c, TRUE); | |
653 | gtk_tree_view_column_set_reorderable(c, TRUE); | |
a3602333 | 654 | g_object_set(r, "xalign", (gfloat)1.0, (char *)0); |
ad47bd4c RK |
655 | gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c); |
656 | } | |
657 | { | |
e9a72659 | 658 | GtkCellRenderer *r = gtk_cell_renderer_text_new(); |
ad47bd4c | 659 | GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes |
e9a72659 | 660 | ("Track", |
ad47bd4c | 661 | r, |
e9a72659 RK |
662 | "text", NAME_COLUMN, |
663 | "background", BG_COLUMN, | |
664 | "foreground", FG_COLUMN, | |
ad47bd4c RK |
665 | (char *)0); |
666 | gtk_tree_view_column_set_resizable(c, TRUE); | |
667 | gtk_tree_view_column_set_reorderable(c, TRUE); | |
e9a72659 | 668 | g_object_set(c, "expand", TRUE, (char *)0); |
ad47bd4c | 669 | gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c); |
e9a72659 | 670 | gtk_tree_view_set_expander_column(GTK_TREE_VIEW(choose_view), c); |
ad47bd4c | 671 | } |
460b9539 | 672 | |
ebcfbfef RK |
673 | /* The selection should support multiple things being selected */ |
674 | choose_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(choose_view)); | |
675 | gtk_tree_selection_set_mode(choose_selection, GTK_SELECTION_MULTIPLE); | |
676 | ||
677 | /* Catch button presses */ | |
6982880f RK |
678 | g_signal_connect(choose_view, "button-press-event", |
679 | G_CALLBACK(choose_button_event), 0); | |
680 | g_signal_connect(choose_view, "button-release-event", | |
681 | G_CALLBACK(choose_button_event), 0); | |
ebcfbfef RK |
682 | /* Catch row expansions so we can fill in placeholders */ |
683 | g_signal_connect(choose_view, "row-expanded", | |
684 | G_CALLBACK(choose_row_expanded), 0); | |
ad47bd4c RK |
685 | |
686 | event_register("queue-list-changed", choose_set_state, 0); | |
687 | event_register("playing-track-changed", choose_set_state, 0); | |
cfa78eaa | 688 | event_register("search-results-changed", choose_set_state, 0); |
3efa1d16 | 689 | event_register("lookups-completed", choose_set_state, 0); |
a0e78d96 | 690 | event_register("choose-more-tracks", choose_menu_moretracks, 0); |
3efa1d16 | 691 | |
00e0c652 RK |
692 | /* After a rescan we update the choose tree. We get a rescan-complete |
693 | * automatically at startup and upon connection too. */ | |
694 | event_register("rescan-complete", choose_refill, 0); | |
cfa78eaa | 695 | |
ebcfbfef RK |
696 | /* Make the widget scrollable */ |
697 | GtkWidget *scrolled = scroll_widget(choose_view); | |
cfa78eaa RK |
698 | |
699 | /* Pack vertically with the search widget */ | |
700 | GtkWidget *vbox = gtk_vbox_new(FALSE/*homogenous*/, 1/*spacing*/); | |
701 | gtk_box_pack_start(GTK_BOX(vbox), scrolled, | |
702 | TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/); | |
703 | gtk_box_pack_end(GTK_BOX(vbox), choose_search_widget(), | |
704 | FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/); | |
705 | ||
706 | g_object_set_data(G_OBJECT(vbox), "type", (void *)&choose_tabtype); | |
a59663d4 RK |
707 | |
708 | /* Redirect keyboard activity to the search widget */ | |
709 | g_signal_connect(choose_view, "key-press-event", | |
710 | G_CALLBACK(choose_key_event), choose_search_entry); | |
711 | g_signal_connect(choose_view, "key-release-event", | |
712 | G_CALLBACK(choose_key_event), choose_search_entry); | |
713 | ||
ff18efce RK |
714 | /* Enable dragging of tracks out */ |
715 | gtk_drag_source_set(choose_view, | |
716 | GDK_BUTTON1_MASK, | |
717 | choose_targets, | |
a6712ea8 | 718 | 1, |
ff18efce RK |
719 | GDK_ACTION_COPY); |
720 | g_signal_connect(choose_view, "drag-data-get", | |
721 | G_CALLBACK(choose_drag_data_get), NULL); | |
722 | make_treeview_multidrag(choose_view, | |
723 | choose_multidrag_predicate); | |
724 | ||
cfa78eaa | 725 | return vbox; |
460b9539 | 726 | } |
727 | ||
728 | /* | |
729 | Local Variables: | |
730 | c-basic-offset:2 | |
731 | comment-column:40 | |
732 | fill-column:79 | |
733 | indent-tabs-mode:nil | |
734 | End: | |
735 | */ |