chiark / gitweb /
disobedience: add a disobedience.desktop file
[disorder] / disobedience / choose.c
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2008 Richard Kettlewell
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 3 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,
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  * 
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/>.
17  */
18 /** @file disobedience/choose.c
19  * @brief Hierarchical track selection and search
20  *
21  * We now use an ordinary GtkTreeStore/GtkTreeView.
22  *
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,
27  * lengths, etc).
28  *
29  * TODO:
30  * - sweep up contracted nodes, replacing their content with a placeholder
31  */
32
33 #include "disobedience.h"
34 #include "choose.h"
35 #include "multidrag.h"
36 #include "queue-generic.h"
37 #include <gdk/gdkkeysyms.h>
38
39 /** @brief Drag types */
40 const GtkTargetEntry choose_targets[] = {
41   {
42     PLAYABLE_TRACKS,                             /* drag type */
43     GTK_TARGET_SAME_APP|GTK_TARGET_OTHER_WIDGET, /* copying between widgets */
44     PLAYABLE_TRACKS_ID                           /* ID value */
45   },
46   {
47     .target = NULL
48   }
49 };
50
51 /** @brief The current selection tree */
52 GtkTreeStore *choose_store;
53
54 /** @brief The view onto the selection tree */
55 GtkWidget *choose_view;
56
57 /** @brief The selection tree's selection */
58 GtkTreeSelection *choose_selection;
59
60 /** @brief Count of file listing operations in flight */
61 static int choose_list_in_flight;
62
63 /** @brief If nonzero autocollapse column won't be set */
64 static int choose_suppress_set_autocollapse;
65
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;
74 }
75
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
85 char *choose_get_display(GtkTreeIter *iter) {
86   return choose_get_string(iter, NAME_COLUMN);
87 }
88
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;
109 }
110
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
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
122  *
123  * TODO is this necessary?  gtk_tree_store_remove() does not document what
124  * happens to children.
125  */
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);
133   return gtk_tree_store_remove(choose_store, it);
134 }
135
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) {
141   if(choose_is_file(it)) {
142     const char *track = choose_get_track(it);
143     const long l = namepart_length(track);
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,
151                        STATE_COLUMN, queued(track),
152                        -1);
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,
157                          -1);
158     else
159       gtk_tree_store_set(choose_store, it,
160                          BG_COLUMN, (char *)0,
161                          FG_COLUMN, (char *)0,
162                          -1);
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
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
181  *
182  * Adjusts the set of files (or directories) below @p parent_ref to match those
183  * listed in @p nvec and @p vec.
184  *
185  * @p parent_ref will be destroyed.
186  */
187 static void choose_populate(GtkTreeRowReference *parent_ref,
188                             int nvec, char **vec,
189                             int isfile) {
190   const char *type = isfile ? "track" : "dir";
191   //fprintf(stderr, "%d new children of type %s\n", nvec, type);
192   if(!nvec)
193     goto skip;
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",
204             type,
205             gtk_tree_path_to_string(parent_path));*/
206   } else {
207     parent_path = 0;
208     parent_it = 0;
209     /*fprintf(stderr, "choose_populate %s: populating the root\n",
210             type);*/
211   }
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);
218   GtkTreeIter it[1];
219   gboolean itv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store),
220                                               it,
221                                               parent_it);
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;
258     } else {
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)
263         action = DELETE;
264       else
265         action = SKIP_TREE;
266     }
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,
280                          ISFILE_COLUMN, isfile,
281                          TRACK_COLUMN, td->track,
282                          SORT_COLUMN, td->sort,
283                          AUTOCOLLAPSE_COLUMN, FALSE,
284                          -1);
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. */
290       if(!isfile) {
291         //fprintf(stderr, "  inserting a placeholder\n");
292         GtkTreeIter placeholder[1];
293
294         gtk_tree_store_append(choose_store, placeholder, child);
295         gtk_tree_store_set(choose_store, placeholder,
296                            NAME_COLUMN, "Waddling...",
297                            TRACK_COLUMN, "",
298                            ISFILE_COLUMN, FALSE,
299                            -1);
300       }
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;
319     }
320   }
321   /*fprintf(stderr, "inserted=%d deleted_placeholder=%d\n\n",
322           inserted, deleted_placeholder);*/
323   if(parent_ref) {
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;
329     }
330     gtk_tree_row_reference_free(parent_ref);
331     gtk_tree_path_free(parent_path);
332   }
333 skip:
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);
342   }
343   //fprintf(stderr, "choose_list_in_flight -> %d-\n", choose_list_in_flight);
344 }
345
346 static void choose_dirs_completed(void *v,
347                                   const char *err,
348                                   int nvec, char **vec) {
349   if(err) {
350     popup_protocol_error(0, err);
351     return;
352   }
353   choose_populate(v, nvec, vec, 0/*!isfile*/);
354 }
355
356 static void choose_files_completed(void *v,
357                                    const char *err,
358                                    int nvec, char **vec) {
359   if(err) {
360     popup_protocol_error(0, err);
361     return;
362   }
363   choose_populate(v, nvec, vec, 1/*isfile*/);
364 }
365
366 void choose_play_completed(void attribute((unused)) *v,
367                            const char *err,
368                            const char attribute((unused)) *id) {
369   if(err)
370     popup_protocol_error(0, err);
371 }
372
373 static void choose_state_toggled
374     (GtkCellRendererToggle attribute((unused)) *cell_renderer,
375      gchar *path_str,
376      gpointer attribute((unused)) user_data) {
377   GtkTreeIter it[1];
378   /* Identify the track */
379   gboolean itv =
380     gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(choose_store),
381                                         it,
382                                         path_str);
383   if(!itv)
384     return;
385   if(!choose_is_file(it))
386     return;
387   const char *track = choose_get_track(it);
388   if(queued(track))
389     return;
390   disorder_eclient_play(client, choose_play_completed, track, 0);
391   
392 }
393
394 /** @brief (Re-)get the children of @p path
395  * @param path Path to target row
396  * @param iter Iterator pointing at target row
397  *
398  * Called from choose_row_expanded() to make sure that the contents are present
399  * and from choose_refill_callback() to (re-)synchronize.
400  */
401 static void choose_refill_row(GtkTreePath *path,
402                               GtkTreeIter *iter) {
403   const char *track = choose_get_track(iter);
404   disorder_eclient_files(client, choose_files_completed,
405                          track,
406                          NULL,
407                          gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store),
408                                                     path));
409   disorder_eclient_dirs(client, choose_dirs_completed,
410                         track,
411                         NULL,
412                         gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store),
413                                                    path));
414   /* The row references are destroyed in the _completed handlers. */
415   choose_list_in_flight += 2;
416 }
417
418 static void choose_row_expanded(GtkTreeView attribute((unused)) *treeview,
419                                 GtkTreeIter *iter,
420                                 GtkTreePath *path,
421                                 gpointer attribute((unused)) user_data) {
422   /*fprintf(stderr, "row-expanded path=[%s]\n\n",
423           gtk_tree_path_to_string(path));*/
424   /* We update a node's contents whenever it is expanded, even if it was
425    * already populated; the effect is that contracting and expanding a node
426    * suffices to update it to the latest state on the server. */
427   choose_refill_row(path, iter);
428   if(!choose_suppress_set_autocollapse) {
429     if(choose_auto_expanding) {
430       /* This was an automatic expansion; mark it the row for auto-collapse. */
431       gtk_tree_store_set(choose_store, iter,
432                          AUTOCOLLAPSE_COLUMN, TRUE,
433                          -1);
434       /*fprintf(stderr, "enable auto-collapse for %s\n",
435               gtk_tree_path_to_string(path));*/
436     } else {
437       /* This was a manual expansion.  Inhibit automatic collapse on this row
438        * and all its ancestors.  */
439       gboolean itv;
440       do {
441         gtk_tree_store_set(choose_store, iter,
442                            AUTOCOLLAPSE_COLUMN, FALSE,
443                            -1);
444         /*fprintf(stderr, "suppress auto-collapse for %s\n",
445                 gtk_tree_model_get_string_from_iter(GTK_TREE_MODEL(choose_store),
446                                                     iter));*/
447         GtkTreeIter child = *iter;
448         itv = gtk_tree_model_iter_parent(GTK_TREE_MODEL(choose_store),
449                                          iter,
450                                          &child);
451       } while(itv);
452       /* The effect of this is that if you expand a row that's actually a
453        * sibling of the real target of the auto-expansion, it stays expanded
454        * when you clear a search.  That's find and good, but it _still_ stays
455        * expanded if you expand it and then collapse it.
456        *
457        * An alternative policy would be to only auto-collapse rows that don't
458        * have any expanded children (apart from ones also subject to
459        * auto-collapse).  I'm not sure what the most usable policy is.
460        */
461     }
462   }
463 }
464
465 static void choose_auto_collapse_callback(GtkTreeView *tree_view,
466                                           GtkTreePath *path,
467                                           gpointer attribute((unused)) user_data) {
468   GtkTreeIter it[1];
469
470   gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store), it, path);
471   if(choose_can_autocollapse(it)) {
472     /*fprintf(stderr, "collapse %s\n",
473             gtk_tree_path_to_string(path));*/
474     gtk_tree_store_set(choose_store, it,
475                        AUTOCOLLAPSE_COLUMN, FALSE,
476                        -1);
477     gtk_tree_view_collapse_row(tree_view, path);
478   }
479 }
480
481 /** @brief Perform automatic collapse after a search is cleared */
482 void choose_auto_collapse(void) {
483   gtk_tree_view_map_expanded_rows(GTK_TREE_VIEW(choose_view),
484                                   choose_auto_collapse_callback,
485                                   0);
486 }
487
488 /** @brief Called from choose_refill() with each expanded row */
489 static void choose_refill_callback(GtkTreeView attribute((unused)) *tree_view,
490                                    GtkTreePath *path,
491                                    gpointer attribute((unused)) user_data) {
492   GtkTreeIter it[1];
493
494   gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store), it, path);
495   choose_refill_row(path, it);
496 }
497
498 /** @brief Synchronize all visible data with the server
499  *
500  * Called at startup, when a rescan completes, and via periodic_slow().
501  */
502 static void choose_refill(const char attribute((unused)) *event,
503                           void attribute((unused)) *eventdata,
504                           void attribute((unused)) *callbackdata) {
505   //fprintf(stderr, "choose_refill\n");
506   /* Update the root */
507   disorder_eclient_files(client, choose_files_completed, "", NULL, NULL); 
508   disorder_eclient_dirs(client, choose_dirs_completed, "", NULL, NULL); 
509   choose_list_in_flight += 2;
510   /* Update all expanded rows */
511   gtk_tree_view_map_expanded_rows(GTK_TREE_VIEW(choose_view),
512                                   choose_refill_callback,
513                                   0);
514   //fprintf(stderr, "choose_list_in_flight -> %d+\n", choose_list_in_flight);
515 }
516
517 /** @brief Called for key-*-event on the main view
518  */
519 static gboolean choose_key_event(GtkWidget attribute((unused)) *widget,
520                                  GdkEventKey *event,
521                                  gpointer user_data) {
522   /*fprintf(stderr, "choose_key_event type=%d state=%#x keyval=%#x\n",
523           event->type, event->state, event->keyval);*/
524   switch(event->keyval) {
525   case GDK_Page_Up:
526   case GDK_Page_Down:
527   case GDK_Up:
528   case GDK_Down:
529   case GDK_Home:
530   case GDK_End:
531     return FALSE;                       /* We'll take these */
532   case 'f': case 'F':
533     /* ^F is expected to start a search.  We implement this by focusing the
534      * search entry box. */
535     if((event->state & ~(GDK_LOCK_MASK|GDK_SHIFT_MASK)) == GDK_CONTROL_MASK
536        && event->type == GDK_KEY_PRESS) {
537       choose_search_new();
538       return TRUE;                      /* Handled it */
539     }
540     break;
541   case 'g': case 'G':
542     /* ^G is expected to go the next match.  We simulate a click on the 'next'
543      * button. */
544     if((event->state & ~(GDK_LOCK_MASK|GDK_SHIFT_MASK)) == GDK_CONTROL_MASK
545        && event->type == GDK_KEY_PRESS) {
546       choose_next_clicked(0, 0);
547       return TRUE;                      /* Handled it */
548     }
549     break;
550   }
551   /* Anything not handled we redirected to the search entry field */
552   gtk_widget_event(user_data, (GdkEvent *)event);
553   return TRUE;                          /* Handled it */
554 }
555
556 static gboolean choose_multidrag_predicate(GtkTreePath attribute((unused)) *path,
557                                            GtkTreeIter *iter) {
558   return choose_is_file(iter);
559 }
560
561
562 /** @brief Callback to add selected tracks to the selection data
563  *
564  * Called from choose_drag_data_get().
565  */
566 static void choose_drag_data_get_collect(GtkTreeModel attribute((unused)) *model,
567                                          GtkTreePath attribute((unused)) *path,
568                                          GtkTreeIter *iter,
569                                          gpointer data) {
570   struct dynstr *const result = data;
571
572   if(choose_is_file(iter)) {            /* no diretories */
573     dynstr_append_string(result, "");   /* no ID */
574     dynstr_append(result, '\n');
575     dynstr_append_string(result, choose_get_track(iter));
576     dynstr_append(result, '\n');
577   }
578 }
579
580 /** @brief Called to extract the dragged data from the choose view
581  * @param w Source widget (the tree view)
582  * @param dc Drag context
583  * @param data Where to put the answer
584  * @param info_ Target @c info parameter
585  * @param time_ Time data requested (for some reason not a @c time_t)
586  * @param user_data The queuelike
587  *
588  * Closely analogous to ql_drag_data_get(), and uses the same data format.
589  * IDs are sent as empty strings.
590  */
591 static void choose_drag_data_get(GtkWidget *w,
592                                  GdkDragContext attribute((unused)) *dc,
593                                  GtkSelectionData *data,
594                                  guint attribute((unused)) info_,
595                                  guint attribute((unused)) time_,
596                                  gpointer attribute((unused)) user_data) {
597   struct dynstr result[1];
598   GtkTreeSelection *sel;
599
600   sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
601   dynstr_init(result);
602   gtk_tree_selection_selected_foreach(sel,
603                                       choose_drag_data_get_collect,
604                                       result);
605   gtk_selection_data_set(data,
606                          GDK_TARGET_STRING,
607                          8, (guchar *)result->vec, result->nvec);
608 }
609
610 /** @brief Create the choose tab */
611 GtkWidget *choose_widget(void) {
612   /* Create the tree store. */
613   choose_store = gtk_tree_store_new(CHOOSE_COLUMNS,
614                                     G_TYPE_BOOLEAN,
615                                     G_TYPE_STRING,
616                                     G_TYPE_STRING,
617                                     G_TYPE_BOOLEAN,
618                                     G_TYPE_STRING,
619                                     G_TYPE_STRING,
620                                     G_TYPE_STRING,
621                                     G_TYPE_STRING,
622                                     G_TYPE_BOOLEAN);
623
624   /* Create the view */
625   choose_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(choose_store));
626   gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(choose_view), TRUE);
627   /* Suppress built-in typeahead find, we do our own search support. */
628   gtk_tree_view_set_enable_search(GTK_TREE_VIEW(choose_view), FALSE);
629
630   /* Create cell renderers and columns */
631   /* TODO use a table */
632   {
633     GtkCellRenderer *r = gtk_cell_renderer_toggle_new();
634     GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
635       ("Queued",
636        r,
637        "active", STATE_COLUMN,
638        "visible", ISFILE_COLUMN,
639        (char *)0);
640     gtk_tree_view_column_set_resizable(c, TRUE);
641     gtk_tree_view_column_set_reorderable(c, TRUE);
642     gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c);
643     g_signal_connect(r, "toggled",
644                      G_CALLBACK(choose_state_toggled), 0);
645   }
646   {
647     GtkCellRenderer *r = gtk_cell_renderer_text_new();
648     GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
649       ("Length",
650        r,
651        "text", LENGTH_COLUMN,
652        (char *)0);
653     gtk_tree_view_column_set_resizable(c, TRUE);
654     gtk_tree_view_column_set_reorderable(c, TRUE);
655     g_object_set(r, "xalign", (gfloat)1.0, (char *)0);
656     gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c);
657   }
658   {
659     GtkCellRenderer *r = gtk_cell_renderer_text_new();
660     GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes
661       ("Track",
662        r,
663        "text", NAME_COLUMN,
664        "background", BG_COLUMN,
665        "foreground", FG_COLUMN,
666        (char *)0);
667     gtk_tree_view_column_set_resizable(c, TRUE);
668     gtk_tree_view_column_set_reorderable(c, TRUE);
669     g_object_set(c, "expand", TRUE, (char *)0);
670     gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c);
671     gtk_tree_view_set_expander_column(GTK_TREE_VIEW(choose_view), c);
672   }
673   
674   /* The selection should support multiple things being selected */
675   choose_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(choose_view));
676   gtk_tree_selection_set_mode(choose_selection, GTK_SELECTION_MULTIPLE);
677
678   /* Catch button presses */
679   g_signal_connect(choose_view, "button-press-event",
680                    G_CALLBACK(choose_button_event), 0);
681   g_signal_connect(choose_view, "button-release-event",
682                    G_CALLBACK(choose_button_event), 0);
683   /* Catch row expansions so we can fill in placeholders */
684   g_signal_connect(choose_view, "row-expanded",
685                    G_CALLBACK(choose_row_expanded), 0);
686
687   event_register("queue-list-changed", choose_set_state, 0);
688   event_register("playing-track-changed", choose_set_state, 0);
689   event_register("search-results-changed", choose_set_state, 0);
690   event_register("lookups-completed", choose_set_state, 0);
691   event_register("choose-more-tracks", choose_menu_moretracks, 0);
692
693   /* After a rescan we update the choose tree.  We get a rescan-complete
694    * automatically at startup and upon connection too. */
695   event_register("rescan-complete", choose_refill, 0);
696
697   /* Make the widget scrollable */
698   GtkWidget *scrolled = scroll_widget(choose_view);
699
700   /* Pack vertically with the search widget */
701   GtkWidget *vbox = gtk_vbox_new(FALSE/*homogenous*/, 1/*spacing*/);
702   gtk_box_pack_start(GTK_BOX(vbox), scrolled,
703                      TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
704   gtk_box_pack_end(GTK_BOX(vbox), choose_search_widget(),
705                    FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
706   
707   g_object_set_data(G_OBJECT(vbox), "type", (void *)&choose_tabtype);
708
709   /* Redirect keyboard activity to the search widget */
710   g_signal_connect(choose_view, "key-press-event",
711                    G_CALLBACK(choose_key_event), choose_search_entry);
712   g_signal_connect(choose_view, "key-release-event",
713                    G_CALLBACK(choose_key_event), choose_search_entry);
714
715   /* Enable dragging of tracks out */
716   gtk_drag_source_set(choose_view,
717                       GDK_BUTTON1_MASK,
718                       choose_targets,
719                       1,
720                       GDK_ACTION_COPY);
721   g_signal_connect(choose_view, "drag-data-get",
722                    G_CALLBACK(choose_drag_data_get), NULL);
723   make_treeview_multidrag(choose_view,
724                           choose_multidrag_predicate);
725
726   return vbox;
727 }
728
729 /*
730 Local Variables:
731 c-basic-offset:2
732 comment-column:40
733 fill-column:79
734 indent-tabs-mode:nil
735 End:
736 */