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