2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file disobedience/choose.c
19 * @brief Hierarchical track selection and search
21 * We now use an ordinary GtkTreeStore/GtkTreeView.
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
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,
30 * - sweep up contracted nodes, replacing their content with a placeholder
33 #include "disobedience.h"
35 #include "multidrag.h"
36 #include "queue-generic.h"
37 #include <gdk/gdkkeysyms.h>
39 /** @brief Drag types */
40 const GtkTargetEntry choose_targets[] = {
42 PLAYABLE_TRACKS, /* drag type */
43 GTK_TARGET_SAME_APP|GTK_TARGET_OTHER_WIDGET, /* copying between widgets */
44 PLAYABLE_TRACKS_ID /* ID value */
51 /** @brief The current selection tree */
52 GtkTreeStore *choose_store;
54 /** @brief The view onto the selection tree */
55 GtkWidget *choose_view;
57 /** @brief The selection tree's selection */
58 GtkTreeSelection *choose_selection;
60 /** @brief Count of file listing operations in flight */
61 static int choose_list_in_flight;
63 /** @brief If nonzero autocollapse column won't be set */
64 static int choose_suppress_set_autocollapse;
66 static char *choose_get_string(GtkTreeIter *iter, int column) {
68 gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter,
71 char *s = xstrdup(gs);
76 char *choose_get_track(GtkTreeIter *iter) {
77 char *s = choose_get_string(iter, TRACK_COLUMN);
78 return *s ? s : 0; /* Placeholder -> NULL */
81 char *choose_get_sort(GtkTreeIter *iter) {
82 return choose_get_string(iter, SORT_COLUMN);
85 char *choose_get_display(GtkTreeIter *iter) {
86 return choose_get_string(iter, NAME_COLUMN);
89 int choose_is_file(GtkTreeIter *iter) {
91 gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter,
92 ISFILE_COLUMN, &isfile,
97 int choose_is_dir(GtkTreeIter *iter) {
99 gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter,
100 ISFILE_COLUMN, &isfile,
104 return !choose_is_placeholder(iter);
107 int choose_is_placeholder(GtkTreeIter *iter) {
108 return choose_get_string(iter, TRACK_COLUMN)[0] == 0;
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,
119 /** @brief Remove node @p it and all its children
120 * @param it Iterator, updated to point to next
121 * @return True if iterator remains valid
123 * TODO is this necessary? gtk_tree_store_remove() does not document what
124 * happens to children.
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),
132 childv = choose_remove_node(child);
133 return gtk_tree_store_remove(choose_store, it);
136 /** @brief Update length and state fields */
137 static gboolean choose_set_state_callback(GtkTreeModel attribute((unused)) *model,
138 GtkTreePath attribute((unused)) *path,
140 gpointer attribute((unused)) data) {
141 if(choose_is_file(it)) {
142 const char *track = choose_get_track(it);
143 const long l = namepart_length(track);
146 byte_snprintf(length, sizeof length, "%ld:%02ld", l / 60, l % 60);
149 gtk_tree_store_set(choose_store, it,
150 LENGTH_COLUMN, length,
151 STATE_COLUMN, queued(track),
153 if(choose_is_search_result(track))
154 gtk_tree_store_set(choose_store, it,
155 BG_COLUMN, SEARCH_RESULT_BG,
156 FG_COLUMN, SEARCH_RESULT_FG,
159 gtk_tree_store_set(choose_store, it,
160 BG_COLUMN, (char *)0,
161 FG_COLUMN, (char *)0,
164 return FALSE; /* continue walking */
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,
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
180 * @param isfile 1 if children are files, 0 if directories
182 * Adjusts the set of files (or directories) below @p parent_ref to match those
183 * listed in @p nvec and @p vec.
185 * @p parent_ref will be destroyed.
187 static void choose_populate(GtkTreeRowReference *parent_ref,
188 int nvec, char **vec,
190 const char *type = isfile ? "track" : "dir";
191 //fprintf(stderr, "%d new children of type %s\n", nvec, type);
194 /* Compute parent_* */
195 GtkTreeIter pit[1], *parent_it;
196 GtkTreePath *parent_path;
198 parent_path = gtk_tree_row_reference_get_path(parent_ref);
200 gboolean pitv = gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store),
203 /*fprintf(stderr, "choose_populate %s: parent path is [%s]\n",
205 gtk_tree_path_to_string(parent_path));*/
209 /*fprintf(stderr, "choose_populate %s: populating the root\n",
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");
217 struct tracksort_data *td = tracksort_init(nvec, vec, type);
219 gboolean itv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store),
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;
232 //fprintf(stderr, " placeholder\n");
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),
240 //fprintf(stderr, " cmp=%d\n", cmp);
242 /* td < it, so we insert td before it */
245 /* td > it, so we must either delete it (if the same type) or skip it */
246 if(choose_is_file(it) == isfile)
251 /* td = it, so we step past both */
253 } else if(nvec > 0) {
254 /* We've reached the end of the tree rows, but new have tracks left in
256 //fprintf(stderr, " inserting\n");
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");
262 if(choose_is_file(it) == isfile)
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
273 GtkTreeIter child[1];
274 gtk_tree_store_insert_before(choose_store,
276 parent_it, /* parent */
277 itv ? it : NULL); /* successor */
278 gtk_tree_store_set(choose_store, child,
279 NAME_COLUMN, td->display,
280 ISFILE_COLUMN, isfile,
281 TRACK_COLUMN, td->track,
282 SORT_COLUMN, td->sort,
283 AUTOCOLLAPSE_COLUMN, FALSE,
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. */
287 choose_set_state_callback(0, 0, child, 0);
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. */
291 //fprintf(stderr, " inserting a placeholder\n");
292 GtkTreeIter placeholder[1];
294 gtk_tree_store_append(choose_store, placeholder, child);
295 gtk_tree_store_set(choose_store, placeholder,
296 NAME_COLUMN, "Waddling...",
298 ISFILE_COLUMN, FALSE,
307 //fprintf(stderr, " SKIP_BOTH\n");
312 //fprintf(stderr, " SKIP_TREE\n");
313 itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it);
316 //fprintf(stderr, " DELETE\n");
317 itv = choose_remove_node(it);
321 /*fprintf(stderr, "inserted=%d deleted_placeholder=%d\n\n",
322 inserted, deleted_placeholder);*/
324 /* If we deleted a placeholder then we must re-expand the row */
325 if(deleted_placeholder) {
326 ++choose_suppress_set_autocollapse;
327 gtk_tree_view_expand_row(GTK_TREE_VIEW(choose_view), parent_path, FALSE);
328 --choose_suppress_set_autocollapse;
330 gtk_tree_row_reference_free(parent_ref);
331 gtk_tree_path_free(parent_path);
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. */
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 */
340 //fprintf(stderr, "raising choose-more-tracks\n");
341 event_raise("choose-more-tracks", 0);
343 //fprintf(stderr, "choose_list_in_flight -> %d-\n", choose_list_in_flight);
346 static void choose_dirs_completed(void *v,
348 int nvec, char **vec) {
350 popup_protocol_error(0, err);
353 choose_populate(v, nvec, vec, 0/*!isfile*/);
356 static void choose_files_completed(void *v,
358 int nvec, char **vec) {
360 popup_protocol_error(0, err);
363 choose_populate(v, nvec, vec, 1/*isfile*/);
366 void choose_play_completed(void attribute((unused)) *v,
369 popup_protocol_error(0, err);
372 static void choose_state_toggled
373 (GtkCellRendererToggle attribute((unused)) *cell_renderer,
375 gpointer attribute((unused)) user_data) {
377 /* Identify the track */
379 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(choose_store),
384 if(!choose_is_file(it))
386 const char *track = choose_get_track(it);
389 disorder_eclient_play(client, track, choose_play_completed, 0);
393 /** @brief (Re-)get the children of @p path
394 * @param path Path to target row
395 * @param iter Iterator pointing at target row
397 * Called from choose_row_expanded() to make sure that the contents are present
398 * and from choose_refill_callback() to (re-)synchronize.
400 static void choose_refill_row(GtkTreePath *path,
402 const char *track = choose_get_track(iter);
403 disorder_eclient_files(client, choose_files_completed,
406 gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store),
408 disorder_eclient_dirs(client, choose_dirs_completed,
411 gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store),
413 /* The row references are destroyed in the _completed handlers. */
414 choose_list_in_flight += 2;
417 static void choose_row_expanded(GtkTreeView attribute((unused)) *treeview,
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);
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,
433 /*fprintf(stderr, "enable auto-collapse for %s\n",
434 gtk_tree_path_to_string(path));*/
436 /* This was a manual expansion. Inhibit automatic collapse on this row
437 * and all its ancestors. */
440 gtk_tree_store_set(choose_store, iter,
441 AUTOCOLLAPSE_COLUMN, FALSE,
443 /*fprintf(stderr, "suppress auto-collapse for %s\n",
444 gtk_tree_model_get_string_from_iter(GTK_TREE_MODEL(choose_store),
446 GtkTreeIter child = *iter;
447 itv = gtk_tree_model_iter_parent(GTK_TREE_MODEL(choose_store),
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.
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.
464 static void choose_auto_collapse_callback(GtkTreeView *tree_view,
466 gpointer attribute((unused)) user_data) {
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,
476 gtk_tree_view_collapse_row(tree_view, path);
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,
487 /** @brief Called from choose_refill() with each expanded row */
488 static void choose_refill_callback(GtkTreeView attribute((unused)) *tree_view,
490 gpointer attribute((unused)) user_data) {
493 gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store), it, path);
494 choose_refill_row(path, it);
497 /** @brief Synchronize all visible data with the server
499 * Called at startup, when a rescan completes, and via periodic_slow().
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,
513 //fprintf(stderr, "choose_list_in_flight -> %d+\n", choose_list_in_flight);
516 /** @brief Called for key-*-event on the main view
518 static gboolean choose_key_event(GtkWidget attribute((unused)) *widget,
520 gpointer user_data) {
521 /*fprintf(stderr, "choose_key_event type=%d state=%#x keyval=%#x\n",
522 event->type, event->state, event->keyval);*/
523 switch(event->keyval) {
530 return FALSE; /* We'll take these */
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) {
537 return TRUE; /* Handled it */
541 /* ^G is expected to go the next match. We simulate a click on the 'next'
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);
546 return TRUE; /* Handled it */
550 /* Anything not handled we redirected to the search entry field */
551 gtk_widget_event(user_data, (GdkEvent *)event);
552 return TRUE; /* Handled it */
555 static gboolean choose_multidrag_predicate(GtkTreePath attribute((unused)) *path,
557 return choose_is_file(iter);
561 /** @brief Callback to add selected tracks to the selection data
563 * Called from choose_drag_data_get().
565 static void choose_drag_data_get_collect(GtkTreeModel attribute((unused)) *model,
566 GtkTreePath attribute((unused)) *path,
569 struct dynstr *const result = data;
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');
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
587 * Closely analogous to ql_drag_data_get(), and uses the same data format.
588 * IDs are sent as empty strings.
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;
599 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
601 gtk_tree_selection_selected_foreach(sel,
602 choose_drag_data_get_collect,
604 gtk_selection_data_set(data,
606 8, (guchar *)result->vec, result->nvec);
609 /** @brief Create the choose tab */
610 GtkWidget *choose_widget(void) {
611 /* Create the tree store. */
612 choose_store = gtk_tree_store_new(CHOOSE_COLUMNS,
623 /* Create the view */
624 choose_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(choose_store));
625 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(choose_view), TRUE);
626 /* Suppress built-in typeahead find, we do our own search support. */
627 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(choose_view), FALSE);
629 /* Create cell renderers and columns */
630 /* TODO use a table */
632 GtkCellRenderer *r = gtk_cell_renderer_toggle_new();
633 GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
636 "active", STATE_COLUMN,
637 "visible", ISFILE_COLUMN,
639 gtk_tree_view_column_set_resizable(c, TRUE);
640 gtk_tree_view_column_set_reorderable(c, TRUE);
641 gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c);
642 g_signal_connect(r, "toggled",
643 G_CALLBACK(choose_state_toggled), 0);
646 GtkCellRenderer *r = gtk_cell_renderer_text_new();
647 GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
650 "text", LENGTH_COLUMN,
652 gtk_tree_view_column_set_resizable(c, TRUE);
653 gtk_tree_view_column_set_reorderable(c, TRUE);
654 g_object_set(r, "xalign", (gfloat)1.0, (char *)0);
655 gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c);
658 GtkCellRenderer *r = gtk_cell_renderer_text_new();
659 GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
663 "background", BG_COLUMN,
664 "foreground", FG_COLUMN,
666 gtk_tree_view_column_set_resizable(c, TRUE);
667 gtk_tree_view_column_set_reorderable(c, TRUE);
668 g_object_set(c, "expand", TRUE, (char *)0);
669 gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c);
670 gtk_tree_view_set_expander_column(GTK_TREE_VIEW(choose_view), c);
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);
677 /* Catch button presses */
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);
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);
686 event_register("queue-list-changed", choose_set_state, 0);
687 event_register("playing-track-changed", choose_set_state, 0);
688 event_register("search-results-changed", choose_set_state, 0);
689 event_register("lookups-completed", choose_set_state, 0);
690 event_register("choose-more-tracks", choose_menu_moretracks, 0);
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);
696 /* Make the widget scrollable */
697 GtkWidget *scrolled = scroll_widget(choose_view);
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*/);
706 g_object_set_data(G_OBJECT(vbox), "type", (void *)&choose_tabtype);
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);
714 /* Enable dragging of tracks out */
715 gtk_drag_source_set(choose_view,
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);