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