chiark / gitweb /
931b1b7f5b291075f638878383311fb041b147e8
[disorder] / disobedience / choose.c
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2006, 2007 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 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  */
20 /** @file disobedience/choose.c
21  * @brief Hierarchical track selection and search
22  *
23  * We don't use the built-in tree widgets because they require that you know
24  * the children of a node on demand, and we have to wait for the server to tell
25  * us.
26  */
27
28 #include "disobedience.h"
29
30 /* Choose track ------------------------------------------------------------ */
31
32 WT(label);
33 WT(event_box);
34 WT(menu);
35 WT(menu_item);
36 WT(layout);
37 WT(vbox);
38 WT(arrow);
39 WT(hbox);
40 WT(button);
41 WT(image);
42 WT(entry);
43
44 /* Types */
45
46 struct choosenode;
47
48 /** @brief Accumulated information about the tree widget */
49 struct displaydata {
50   /** @brief Maximum width required */
51   guint width;
52   /** @brief Maximum height required */
53   guint height;
54 };
55
56 /* instantiate the node vector type */
57
58 VECTOR_TYPE(nodevector, struct choosenode *, xrealloc);
59
60 /** @brief Signature of function called when a choosenode is filled */
61 typedef void (when_filled_callback)(struct choosenode *cn,
62                                     void *wfu);
63
64 /** @brief One node in the virtual filesystem */
65 struct choosenode {
66   struct choosenode *parent;            /**< @brief parent node */
67   const char *path;                     /**< @brief full path or 0  */
68   const char *sort;                     /**< @brief sort key */
69   const char *display;                  /**< @brief display name */
70   int pending;                          /**< @brief pending resolve queries */
71   unsigned flags;
72 #define CN_EXPANDABLE 0x0001            /**< @brief node is expandable */
73 #define CN_EXPANDED 0x0002              /**< @brief node is expanded
74                                          *
75                                          * Expandable items are directories;
76                                          * non-expandable ones are files. */
77 #define CN_DISPLAYED 0x0004             /**< @brief widget is displayed in layout */
78 #define CN_SELECTED 0x0008              /**< @brief node is selected */
79 #define CN_GETTING_FILES 0x0010         /**< @brief files inbound */
80 #define CN_RESOLVING_FILES 0x0020       /**< @brief resolved files inbound */
81 #define CN_GETTING_DIRS 0x0040          /**< @brief directories inbound */
82 #define CN_GETTING_ANY 0x0070           /**< @brief getting something */
83 #define CN_CONTINGENT 0x0080            /**< @brief expansion contingent on search */
84   struct nodevector children;           /**< @brief vector of children */
85   void (*fill)(struct choosenode *);    /**< @brief request child fill or 0 for leaf */
86   GtkWidget *container;                 /**< @brief the container for this row */
87   GtkWidget *hbox;                      /**< @brief the hbox for this row */
88   GtkWidget *arrow;                     /**< @brief arrow widget or 0 */
89   GtkWidget *label;                     /**< @brief text label for this node */
90   GtkWidget *marker;                    /**< @brief queued marker */
91
92   when_filled_callback *whenfilled;     /**< @brief called when filled or 0 */
93   void *wfu;                            /**< @brief passed to @c whenfilled */
94 };
95
96 /** @brief One item in the popup menu */
97 struct choose_menuitem {
98   /* Parameters */
99   const char *name;                     /**< @brief name */
100
101   /* Callbacks */
102   void (*activate)(GtkMenuItem *menuitem, gpointer user_data);
103   /**< @brief Called to activate the menu item.
104    *
105    * @p user_data is the choosenode the mouse pointer is over. */
106
107   gboolean (*sensitive)(struct choosenode *cn);
108   /* @brief Called to determine whether the menu item should be sensitive.
109    *
110    * TODO? */
111
112   /* State */
113   gulong handlerid;                     /**< @brief signal handler ID */
114   GtkWidget *w;                         /**< @brief menu item widget */
115 };
116
117 /* Variables */
118
119 static GtkWidget *chooselayout;
120 static GtkWidget *searchentry;          /**< @brief search terms */
121 static struct choosenode *root;
122 static GtkWidget *track_menu;           /**< @brief track popup menu */
123 static GtkWidget *dir_menu;             /**< @brief directory popup menu */
124 static struct choosenode *last_click;   /**< @brief last clicked node for selection */
125 static int files_visible;               /**< @brief total files visible */
126 static int files_selected;              /**< @brief total files selected */
127 static int search_in_flight;            /**< @brief a search is underway */
128 static int search_obsolete;             /**< @brief the current search is void */
129 static char **searchresults;            /**< @brief search results */
130 static int nsearchresults;              /**< @brief number of results */
131 static int nsearchvisible;      /**< @brief number of search results visible */
132 static struct hash *searchhash;         /**< @brief hash of search results */
133
134 /* Forward Declarations */
135
136 static void clear_children(struct choosenode *cn);
137 static struct choosenode *newnode(struct choosenode *parent,
138                                   const char *path,
139                                   const char *display,
140                                   const char *sort,
141                                   unsigned flags,
142                                   void (*fill)(struct choosenode *));
143 static void fill_root_node(struct choosenode *cn);
144 static void fill_letter_node(struct choosenode *cn);
145 static void fill_directory_node(struct choosenode *cn);
146 static void got_files(void *v, int nvec, char **vec);
147 static void got_resolved_file(void *v, const char *track);
148 static void got_dirs(void *v, int nvec, char **vec);
149
150 static void expand_node(struct choosenode *cn, int contingent);
151 static void contract_node(struct choosenode *cn);
152 static void updated_node(struct choosenode *cn, int redisplay);
153
154 static void display_selection(struct choosenode *cn);
155 static void clear_selection(struct choosenode *cn);
156
157 static void redisplay_tree(void);
158 static struct displaydata display_tree(struct choosenode *cn, int x, int y);
159 static void undisplay_tree(struct choosenode *cn);
160 static void initiate_search(void);
161 static void delete_widgets(struct choosenode *cn);
162 static void expand_from(struct choosenode *cn);
163
164 static void clicked_choosenode(GtkWidget attribute((unused)) *widget,
165                                GdkEventButton *event,
166                                gpointer user_data);
167
168 static void activate_track_play(GtkMenuItem *menuitem, gpointer user_data);
169 static void activate_track_properties(GtkMenuItem *menuitem, gpointer user_data);
170
171 static gboolean sensitive_track_play(struct choosenode *cn);
172 static gboolean sensitive_track_properties(struct choosenode *cn);
173
174 static void activate_dir_play(GtkMenuItem *menuitem, gpointer user_data);
175 static void activate_dir_properties(GtkMenuItem *menuitem, gpointer user_data);
176 static void activate_dir_select(GtkMenuItem *menuitem, gpointer user_data);
177
178 static gboolean sensitive_dir_play(struct choosenode *cn);
179 static gboolean sensitive_dir_properties(struct choosenode *cn);
180 static gboolean sensitive_dir_select(struct choosenode *cn);
181
182 /** @brief Track menu items */
183 static struct choose_menuitem track_menuitems[] = {
184   { "Play track", activate_track_play, sensitive_track_play, 0, 0 },
185   { "Track properties", activate_track_properties, sensitive_track_properties, 0, 0 },
186   { 0, 0, 0, 0, 0 }
187 };
188
189 /** @brief Directory menu items */
190 static struct choose_menuitem dir_menuitems[] = {
191   { "Play all tracks", activate_dir_play, sensitive_dir_play, 0, 0 },
192   { "Track properties", activate_dir_properties, sensitive_dir_properties, 0, 0 },
193   { "Select all tracks", activate_dir_select, sensitive_dir_select, 0, 0 },
194   { 0, 0, 0, 0, 0 }
195 };
196
197 /* Maintaining the data structure ------------------------------------------ */
198
199 static char *flags(const struct choosenode *cn) {
200   unsigned f = cn->flags, n;
201   struct dynstr d[1];
202   
203   static const char *bits[] = {
204     "expandable",
205     "expanded",
206     "displayed",
207     "selected",
208     "getting_files",
209     "resolving_files",
210     "getting_dirs",
211     "contingent"
212   };
213 #define NBITS (sizeof bits / sizeof *bits)
214
215   dynstr_init(d);
216   if(!f)
217     dynstr_append(d, '0');
218   else {
219     for(n = 0; n < NBITS; ++n) {
220       const unsigned bit = 1 << n;
221       if(f & bit) {
222         if(d->nvec)
223           dynstr_append(d, '|');
224         dynstr_append_string(d, bits[n]);
225         f ^= bit;
226       }
227     }
228     if(f) {
229       char buf[32];
230       if(d->nvec)
231         dynstr_append(d, '|');
232       sprintf(buf, "%#x", f);
233       dynstr_append_string(d, buf);
234     }
235   }
236   dynstr_terminate(d);
237   return d->vec;
238 }
239
240 /** @brief Create a new node */
241 static struct choosenode *newnode(struct choosenode *parent,
242                                   const char *path,
243                                   const char *display,
244                                   const char *sort,
245                                   unsigned flags,
246                                   void (*fill)(struct choosenode *)) {
247   struct choosenode *const n = xmalloc(sizeof *n);
248
249   D(("newnode %s %s", path, display));
250   if(flags & CN_EXPANDABLE)
251     assert(fill);
252   else
253     assert(!fill);
254   n->parent = parent;
255   n->path = path;
256   n->display = display;
257   n->sort = sort;
258   n->flags = flags;
259   nodevector_init(&n->children);
260   n->fill = fill;
261   if(parent)
262     nodevector_append(&parent->children, n);
263   return n;
264 }
265
266 /** @brief Called when a node has been filled
267  *
268  * Response for calling @c whenfilled.
269  */
270 static void filled(struct choosenode *cn) {
271   when_filled_callback *const whenfilled = cn->whenfilled;
272
273   if(whenfilled) {
274     cn->whenfilled = 0;
275     whenfilled(cn, cn->wfu);
276   }
277   if(nsearchvisible < nsearchresults) {
278     /* There is still search expansion work to do */
279     D(("filled %s %d/%d", cn->path, nsearchvisible, nsearchresults));
280     expand_from(cn);
281   }
282 }
283
284 /** @brief Fill the root */
285 static void fill_root_node(struct choosenode *cn) {
286   int ch;
287   char *name;
288   struct callbackdata *cbd;
289
290   D(("fill_root_node"));
291   clear_children(cn);
292   if(choosealpha) {
293     if(!cn->children.nvec) {              /* Only need to do this once */
294       for(ch = 'A'; ch <= 'Z'; ++ch) {
295         byte_xasprintf(&name, "%c", ch);
296         newnode(cn, "<letter>", name, name, CN_EXPANDABLE, fill_letter_node);
297       }
298       newnode(cn, "<letter>", "*", "~", CN_EXPANDABLE, fill_letter_node);
299     }
300     updated_node(cn, 1);
301     filled(cn);
302   } else {
303     /* More de-duping possible here */
304     if(cn->flags & CN_GETTING_ANY)
305       return;
306     gtk_label_set_text(GTK_LABEL(report_label), "getting files");
307     cbd = xmalloc(sizeof *cbd);
308     cbd->u.choosenode = cn;
309     disorder_eclient_dirs(client, got_dirs, "", 0, cbd);
310     cbd = xmalloc(sizeof *cbd);
311     cbd->u.choosenode = cn;
312     disorder_eclient_files(client, got_files, "", 0, cbd);
313     cn->flags |= CN_GETTING_FILES|CN_GETTING_DIRS;
314   }
315 }
316
317 /** @brief Delete all the widgets owned by @p cn */
318 static void delete_cn_widgets(struct choosenode *cn) {
319   if(cn->arrow) {
320     DW(arrow);
321     gtk_widget_destroy(cn->arrow);
322     cn->arrow = 0;
323   }
324   if(cn->label) {
325     DW(label);
326     gtk_widget_destroy(cn->label);
327     cn->label = 0;
328   }
329   if(cn->marker) {
330     DW(image);
331     gtk_widget_destroy(cn->marker);
332     cn->marker = 0;
333   }
334   if(cn->hbox) {
335     DW(hbox);
336     gtk_widget_destroy(cn->hbox);
337     cn->hbox = 0;
338   }
339   if(cn->container) {
340     DW(event_box);
341     gtk_widget_destroy(cn->container);
342     cn->container = 0;
343   }
344 }
345
346 /** @brief Recursively clear all the children of @p cn
347  *
348  * All the widgets at or below @p cn are deleted.  All choosenodes below
349  * it are emptied. i.e. we prune the tree at @p cn.
350  */
351 static void clear_children(struct choosenode *cn) {
352   int n;
353
354   D(("clear_children %s", cn->path));
355   /* Recursively clear subtrees */
356   for(n = 0; n < cn->children.nvec; ++n) {
357     clear_children(cn->children.vec[n]);
358     delete_cn_widgets(cn->children.vec[n]);
359   }
360   cn->children.nvec = 0;
361 }
362
363 /** @brief Fill a letter node */
364 static void fill_letter_node(struct choosenode *cn) {
365   const char *regexp;
366   struct callbackdata *cbd;
367
368   D(("fill_letter_node %s", cn->display));
369   if(cn->flags & CN_GETTING_ANY)
370     return;
371   switch(cn->display[0]) {
372   default:
373     byte_xasprintf((char **)&regexp, "^(the )?%c", tolower(cn->display[0]));
374     break;
375   case 'T':
376     regexp = "^(?!the [^t])t";
377     break;
378   case '*':
379     regexp = "^[^a-z]";
380     break;
381   }
382   /* TODO: caching */
383   /* TODO: de-dupe against fill_directory_node */
384   gtk_label_set_text(GTK_LABEL(report_label), "getting files");
385   clear_children(cn);
386   cbd = xmalloc(sizeof *cbd);
387   cbd->u.choosenode = cn;
388   disorder_eclient_dirs(client, got_dirs, "", regexp, cbd);
389   cbd = xmalloc(sizeof *cbd);
390   cbd->u.choosenode = cn;
391   disorder_eclient_files(client, got_files, "", regexp, cbd);
392   cn->flags |= CN_GETTING_FILES|CN_GETTING_DIRS;
393 }
394
395 /** @brief Called with a list of files just below some node */
396 static void got_files(void *v, int nvec, char **vec) {
397   struct callbackdata *cbd = v;
398   struct choosenode *cn = cbd->u.choosenode;
399   int n;
400
401   D(("got_files %d files for %s %s", nvec, cn->path, flags(cn)));
402   /* Complicated by the need to resolve aliases.  We can save a bit of effort
403    * by re-using cbd though. */
404   cn->flags &= ~CN_GETTING_FILES;
405   if((cn->pending = nvec)) {
406     cn->flags |= CN_RESOLVING_FILES;
407     for(n = 0; n < nvec; ++n)
408       disorder_eclient_resolve(client, got_resolved_file, vec[n], cbd);
409   }
410   /* If there are no files and the directories are all read by now, we're
411    * done */
412   if(!(cn->flags & CN_GETTING_ANY))
413     filled(cn);
414 }
415
416 /** @brief Called with an alias resolved filename */
417 static void got_resolved_file(void *v, const char *track) {
418   struct callbackdata *cbd = v;
419   struct choosenode *cn = cbd->u.choosenode, *file_cn;
420
421   D(("resolved %s %s %d left", cn->path, flags(cn), cn->pending - 1));
422   /* TODO as below */
423   file_cn = newnode(cn, track,
424                     trackname_transform("track", track, "display"),
425                     trackname_transform("track", track, "sort"),
426                     0/*flags*/, 0/*fill*/);
427   /* Only bother updating when we've got the lot */
428   if(--cn->pending == 0) {
429     cn->flags &= ~CN_RESOLVING_FILES;
430     updated_node(cn, 1);
431     if(!(cn->flags & CN_GETTING_ANY))
432       filled(cn);
433   }
434 }
435
436 /** @brief Called with a list of directories just below some node */
437 static void got_dirs(void *v, int nvec, char **vec) {
438   struct callbackdata *cbd = v;
439   struct choosenode *cn = cbd->u.choosenode;
440   int n;
441
442   D(("got_dirs %d dirs for %s %s", nvec, cn->path, flags(cn)));
443   /* TODO this depends on local configuration for trackname_transform().
444    * This will work, since the defaults are now built-in, but it'll be
445    * (potentially) different to the server's configured settings.
446    *
447    * Really we want a variant of files/dirs that produces both the
448    * raw filename and the transformed name for a chosen context.
449    */
450   for(n = 0; n < nvec; ++n)
451     newnode(cn, vec[n],
452             trackname_transform("dir", vec[n], "display"),
453             trackname_transform("dir", vec[n], "sort"),
454             CN_EXPANDABLE, fill_directory_node);
455   updated_node(cn, 1);
456   cn->flags &= ~CN_GETTING_DIRS;
457   if(!(cn->flags & CN_GETTING_ANY))
458     filled(cn);
459 }
460   
461 /** @brief Fill a child node */
462 static void fill_directory_node(struct choosenode *cn) {
463   struct callbackdata *cbd;
464
465   D(("fill_directory_node %s", cn->path));
466   /* TODO: caching */
467   /* TODO: de-dupe against fill_letter_node */
468   if(cn->flags & CN_GETTING_ANY)
469     return;
470   assert(report_label != 0);
471   gtk_label_set_text(GTK_LABEL(report_label), "getting files");
472   clear_children(cn);
473   cbd = xmalloc(sizeof *cbd);
474   cbd->u.choosenode = cn;
475   disorder_eclient_dirs(client, got_dirs, cn->path, 0, cbd);
476   cbd = xmalloc(sizeof *cbd);
477   cbd->u.choosenode = cn;
478   disorder_eclient_files(client, got_files, cn->path, 0, cbd);
479   cn->flags |= CN_GETTING_FILES|CN_GETTING_DIRS;
480 }
481
482 /** @brief Expand a node */
483 static void expand_node(struct choosenode *cn, int contingent) {
484   D(("expand_node %s %d %s", cn->path, contingent, flags(cn)));
485   assert(cn->flags & CN_EXPANDABLE);
486   /* If node is already expanded do nothing. */
487   if(cn->flags & CN_EXPANDED) return;
488   /* We mark the node as expanded and request that it fill itself.  When it has
489    * completed it will called updated_node() and we can redraw at that
490    * point. */
491   cn->flags |= CN_EXPANDED;
492   if(contingent)
493     cn->flags |= CN_CONTINGENT;
494   else
495     cn->flags &= ~CN_CONTINGENT;
496   /* If this node is not contingently expanded, mark all its parents back to
497    * the root as not contingent either, so they won't be contracted when the
498    * search results change */
499   if(!contingent) {
500     struct choosenode *cnp;
501
502     for(cnp = cn->parent; cnp; cnp = cnp->parent)
503       cnp->flags &= ~CN_CONTINGENT;
504   }
505   /* TODO: visual feedback */
506   cn->fill(cn);
507 }
508
509 /** @brief Make sure all the search results below @p cn are expanded
510  * @param cn Node to start at
511  */
512 static void expand_from(struct choosenode *cn) {
513   int n;
514   const size_t pathlen = strlen(cn->path);
515
516   if(nsearchvisible == nsearchresults)
517     /* We're done */
518     return;
519   /* Are any of the search tracks at/below this point? */
520   if(cn != root) {
521     for(n = 0; n < nsearchresults; ++n)
522       if(strlen(searchresults[n]) >= pathlen
523          && !strncmp(cn->path, searchresults[n], pathlen)
524          && (searchresults[n][pathlen] == 0
525              || searchresults[n][pathlen] == '/'))
526         break;
527     if(n >= nsearchresults)
528       /* This is neither a search result nor an ancestor directory of one */
529       return;
530   }
531   D(("expand_from %d/%d visible %s", 
532      nsearchvisible, nsearchresults, cn->path));
533   if(cn->flags & CN_EXPANDABLE) {
534     if(cn->flags & CN_EXPANDED)
535       /* This node is marked as expanded already.  children.nvec might be 0,
536        * indicating that expansion is still underway.  We should get another
537        * callback when it is expanded. */
538       for(n = 0; n < cn->children.nvec; ++n)
539         expand_from(cn->children.vec[n]);
540     else {
541       /* This node is not expanded yet */
542       expand_node(cn, 1);
543     }
544   } else
545     /* This is an actual search result */
546     ++nsearchvisible;
547 }
548
549 /** @brief Contract all contingently expanded nodes below @p cn */
550 static void contract_contingent(struct choosenode *cn) {
551   int n;
552
553   if(cn->flags & CN_CONTINGENT)
554     contract_node(cn);
555   else
556     for(n = 0; n < cn->children.nvec; ++n)
557       contract_contingent(cn->children.vec[n]);
558 }
559
560 /** @brief Contract a node */
561 static void contract_node(struct choosenode *cn) {
562   D(("contract_node %s", cn->path));
563   assert(cn->flags & CN_EXPANDABLE);
564   /* If node is already contracted do nothing. */
565   if(!(cn->flags & CN_EXPANDED)) return;
566   cn->flags &= ~(CN_EXPANDED|CN_CONTINGENT);
567   /* Clear selection below this node */
568   clear_selection(cn);
569   /* Zot children.  We never used to do this but the result would be that over
570    * time you'd end up with the entire tree pulled into memory.  If the server
571    * is over a slow network it will make interactivity slightly worse; if
572    * anyone complains we can make it an option. */
573   clear_children(cn);
574   /* We can contract a node immediately. */
575   redisplay_tree();
576 }
577
578 /** @brief qsort() callback for ordering choosenodes */
579 static int compare_choosenode(const void *av, const void *bv) {
580   const struct choosenode *const *aa = av, *const *bb = bv;
581   const struct choosenode *a = *aa, *b = *bb;
582
583   return compare_tracks(a->sort, b->sort,
584                         a->display, b->display,
585                         a->path, b->path);
586 }
587
588 /** @brief Called when an expandable node is updated.   */
589 static void updated_node(struct choosenode *cn, int redisplay) {
590   D(("updated_node %s", cn->path));
591   assert(cn->flags & CN_EXPANDABLE);
592   /* It might be that the node has been de-expanded since we requested the
593    * update.  In that case we ignore this notification. */
594   if(!(cn->flags & CN_EXPANDED)) return;
595   /* Sort children */
596   qsort(cn->children.vec, cn->children.nvec, sizeof (struct choosenode *),
597         compare_choosenode);
598   if(redisplay)
599     redisplay_tree();
600 }
601
602 /* Searching --------------------------------------------------------------- */
603
604 /** @brief Return true if @p track is a search result */
605 static int is_search_result(const char *track) {
606   return searchhash && hash_find(searchhash, track);
607 }
608
609 /** @brief Called with a list of search results
610  *
611  * This is called from eclient with a (possibly empty) list of search results,
612  * and also from initiate_seatch with an always empty list to indicate that
613  * we're not searching for anything in particular. */
614 static void search_completed(void attribute((unused)) *v,
615                              int nvec, char **vec) {
616   int n;
617
618   search_in_flight = 0;
619   /* Contract any choosenodes that were only expanded to show search
620    * results */
621   contract_contingent(root);
622   if(search_obsolete) {
623     /* This search has been obsoleted by user input since it started.
624      * Therefore we throw away the result and search again. */
625     search_obsolete = 0;
626     initiate_search();
627   } else {
628     /* Stash the search results */
629     searchresults = vec;
630     nsearchresults = nvec;
631     if(nvec) {
632       /* Create a new search hash for fast identification of results */
633       searchhash = hash_new(1);
634       for(n = 0; n < nvec; ++n)
635         hash_add(searchhash, vec[n], "", HASH_INSERT_OR_REPLACE);
636       /* We don't yet know that the results are visible */
637       nsearchvisible = 0;
638       /* Initiate expansion */
639       expand_from(root);
640     } else {
641       searchhash = 0;                   /* for the gc */
642     }
643   }
644 }
645
646 /** @brief Initiate a search 
647  *
648  * If a search is underway we set @ref search_obsolete and restart the search
649  * in search_completed() above.
650  */
651 static void initiate_search(void) {
652   char *terms, *e;
653
654   /* Find out what the user is after */
655   terms = xstrdup(gtk_entry_get_text(GTK_ENTRY(searchentry)));
656   /* Strip leading and trailing space */
657   while(*terms == ' ') ++terms;
658   e = terms + strlen(terms);
659   while(e > terms && e[-1] == ' ') --e;
660   *e = 0;
661   /* If a search is already underway then mark it as obsolete.  We'll revisit
662    * when it returns. */
663   if(search_in_flight) {
664     search_obsolete = 1;
665     return;
666   }
667   if(*terms) {
668     /* There's still something left.  Initiate the search. */
669     if(disorder_eclient_search(client, search_completed, terms, 0)) {
670       /* The search terms are bad!  We treat this as if there were no search
671        * terms at all.  Some kind of feedback would be handy. */
672       fprintf(stderr, "bad terms [%s]\n", terms); /* TODO */
673       search_completed(0, 0, 0);
674     } else {
675       search_in_flight = 1;
676     }
677   } else {
678     /* No search terms - we want to see all tracks */
679     search_completed(0, 0, 0);
680   }
681 }
682
683 /** @brief Called when the cancel search button is clicked */
684 static void clearsearch_clicked(GtkButton attribute((unused)) *button,
685                                 gpointer attribute((unused)) userdata) {
686   gtk_entry_set_text(GTK_ENTRY(searchentry), "");
687 }
688
689 /* Display functions ------------------------------------------------------- */
690
691 /** @brief Delete all the widgets in the tree */
692 static void delete_widgets(struct choosenode *cn) {
693   int n;
694
695   delete_cn_widgets(cn);
696   for(n = 0; n < cn->children.nvec; ++n)
697     delete_widgets(cn->children.vec[n]);
698   cn->flags &= ~(CN_DISPLAYED|CN_SELECTED);
699   files_selected = 0;
700 }
701
702 /** @brief Update the display */
703 static void redisplay_tree(void) {
704   struct displaydata d;
705   guint oldwidth, oldheight;
706
707   D(("redisplay_tree"));
708   /* We'll count these up empirically each time */
709   files_selected = 0;
710   files_visible = 0;
711   /* Correct the layout and find out how much space it uses */
712   MTAG_PUSH("display_tree");
713   d = display_tree(root, 0, 0);
714   MTAG_POP();
715   /* We must set the total size or scrolling will not work (it wouldn't be hard
716    * for GtkLayout to figure it out for itself but presumably you're supposed
717    * to be able to have widgets off the edge of the layuot.)
718    *
719    * There is a problem: if we shrink the size then the part of the screen that
720    * is outside the new size but inside the old one is not updated.  I think
721    * this is arguably bug in GTK+ but it's easy to force a redraw if this
722    * region is nonempty.
723    */
724   gtk_layout_get_size(GTK_LAYOUT(chooselayout), &oldwidth, &oldheight);
725   if(oldwidth > d.width || oldheight > d.height)
726     gtk_widget_queue_draw(chooselayout);
727   gtk_layout_set_size(GTK_LAYOUT(chooselayout), d.width, d.height);
728   /* Notify the main menu of any recent changes */
729   menu_update(-1);
730 }
731
732 /** @brief Recursive step for redisplay_tree()
733  *
734  * Makes sure all displayed widgets from CN down exist and are in their proper
735  * place and return the maximum space used.
736  */
737 static struct displaydata display_tree(struct choosenode *cn, int x, int y) {
738   int n, aw;
739   GtkRequisition req;
740   struct displaydata d, cd;
741   GdkPixbuf *pb;
742   const char *name;
743   
744   D(("display_tree %s %d,%d", cn->path, x, y));
745
746   /* An expandable item contains an arrow and a text label.  When you press the
747    * button it flips its expand state.
748    *
749    * A non-expandable item has just a text label and no arrow.
750    */
751   if(!cn->container) {
752     MTAG_PUSH("make_widgets_1");
753     /* Widgets need to be created */
754     NW(hbox);
755     cn->hbox = gtk_hbox_new(FALSE, 1);
756     if(cn->flags & CN_EXPANDABLE) {
757       NW(arrow);
758       cn->arrow = gtk_arrow_new(cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN
759                                                         : GTK_ARROW_RIGHT,
760                                 GTK_SHADOW_NONE);
761       cn->marker = 0;
762     } else {
763       cn->arrow = 0;
764       if((pb = find_image("notes.png"))) {
765         NW(image);
766         cn->marker = gtk_image_new_from_pixbuf(pb);
767       }
768     }
769     MTAG_POP();
770     MTAG_PUSH("make_widgets_2");
771     NW(label);
772     cn->label = gtk_label_new(cn->display);
773     if(cn->arrow)
774       gtk_container_add(GTK_CONTAINER(cn->hbox), cn->arrow);
775     gtk_container_add(GTK_CONTAINER(cn->hbox), cn->label);
776     if(cn->marker)
777       gtk_container_add(GTK_CONTAINER(cn->hbox), cn->marker);
778     MTAG_POP();
779     MTAG_PUSH("make_widgets_3");
780     NW(event_box);
781     cn->container = gtk_event_box_new();
782     gtk_container_add(GTK_CONTAINER(cn->container), cn->hbox);
783     g_signal_connect(cn->container, "button-release-event", 
784                      G_CALLBACK(clicked_choosenode), cn);
785     g_signal_connect(cn->container, "button-press-event", 
786                      G_CALLBACK(clicked_choosenode), cn);
787     g_object_ref(cn->container);
788     /* Show everything by default */
789     gtk_widget_show_all(cn->container);
790     MTAG_POP();
791   }
792   assert(cn->container);
793   /* Make sure the widget name is right */
794   name = (cn->flags & CN_EXPANDABLE
795           ? "choose-dir"
796           : is_search_result(cn->path) ? "choose-search" : "choose");
797   gtk_widget_set_name(cn->label, name);
798   gtk_widget_set_name(cn->container, name);
799   /* Make sure the icon is right */
800   if(cn->flags & CN_EXPANDABLE)
801     gtk_arrow_set(GTK_ARROW(cn->arrow),
802                   cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN : GTK_ARROW_RIGHT,
803                   GTK_SHADOW_NONE);
804   else if(cn->marker)
805     /* Make sure the queued marker is right */
806     /* TODO: doesn't always work */
807     (queued(cn->path) ? gtk_widget_show : gtk_widget_hide)(cn->marker);
808   /* Put the widget in the right place */
809   if(cn->flags & CN_DISPLAYED)
810     gtk_layout_move(GTK_LAYOUT(chooselayout), cn->container, x, y);
811   else {
812     gtk_layout_put(GTK_LAYOUT(chooselayout), cn->container, x, y);
813     cn->flags |= CN_DISPLAYED;
814     /* Now chooselayout has a ref to the container */
815     g_object_unref(cn->container);
816   }
817   /* Set the widget's selection status */
818   if(!(cn->flags & CN_EXPANDABLE))
819     display_selection(cn);
820   /* Find the size used so we can get vertical positioning right. */
821   gtk_widget_size_request(cn->container, &req);
822   d.width = x + req.width;
823   d.height = y + req.height;
824   if(cn->flags & CN_EXPANDED) {
825     /* We'll offset children by the size of the arrow whatever it might be. */
826     assert(cn->arrow);
827     gtk_widget_size_request(cn->arrow, &req);
828     aw = req.width;
829     for(n = 0; n < cn->children.nvec; ++n) {
830       cd = display_tree(cn->children.vec[n], x + aw, d.height);
831       if(cd.width > d.width)
832         d.width = cd.width;
833       d.height = cd.height;
834     }
835   } else {
836     for(n = 0; n < cn->children.nvec; ++n)
837       undisplay_tree(cn->children.vec[n]);
838   }
839   if(!(cn->flags & CN_EXPANDABLE)) {
840     ++files_visible;
841     if(cn->flags & CN_SELECTED)
842       ++files_selected;
843   }
844   /* report back how much space we used */
845   D(("display_tree %s %d,%d total size %dx%d", cn->path, x, y,
846      d.width, d.height));
847   return d;
848 }
849
850 /** @brief Remove widgets for newly hidden nodes */
851 static void undisplay_tree(struct choosenode *cn) {
852   int n;
853
854   D(("undisplay_tree %s", cn->path));
855   /* Remove this widget from the display */
856   if(cn->flags & CN_DISPLAYED) {
857     gtk_container_remove(GTK_CONTAINER(chooselayout), cn->container);
858     cn->flags ^= CN_DISPLAYED;
859   }
860   /* Remove children too */
861   for(n = 0; n < cn->children.nvec; ++n)
862     undisplay_tree(cn->children.vec[n]);
863 }
864
865 /* Selection --------------------------------------------------------------- */
866
867 /** @brief Mark the widget @p cn according to its selection state */
868 static void display_selection(struct choosenode *cn) {
869   /* Need foreground and background colors */
870   gtk_widget_set_state(cn->label, (cn->flags & CN_SELECTED
871                                    ? GTK_STATE_SELECTED : GTK_STATE_NORMAL));
872   gtk_widget_set_state(cn->container, (cn->flags & CN_SELECTED
873                                        ? GTK_STATE_SELECTED : GTK_STATE_NORMAL));
874 }
875
876 /** @brief Set the selection state of a widget
877  *
878  * Directories can never be selected, we just ignore attempts to do so. */
879 static void set_selection(struct choosenode *cn, int selected) {
880   unsigned f = selected ? CN_SELECTED : 0;
881
882   D(("set_selection %d %s", selected, cn->path));
883   if(!(cn->flags & CN_EXPANDABLE) && (cn->flags & CN_SELECTED) != f) {
884     cn->flags ^= CN_SELECTED;
885     /* Maintain selection count */
886     if(selected)
887       ++files_selected;
888     else
889       --files_selected;
890     display_selection(cn);
891     /* Update main menu sensitivity */
892     menu_update(-1);
893   }
894 }
895
896 /** @brief Recursively clear all selection bits from CN down */
897 static void clear_selection(struct choosenode *cn) {
898   int n;
899
900   set_selection(cn, 0);
901   for(n = 0; n < cn->children.nvec; ++n)
902     clear_selection(cn->children.vec[n]);
903 }
904
905 /* User actions ------------------------------------------------------------ */
906
907 /** @brief Clicked on something
908  *
909  * This implements playing, all the modifiers for selection, etc.
910  */
911 static void clicked_choosenode(GtkWidget attribute((unused)) *widget,
912                                GdkEventButton *event,
913                                gpointer user_data) {
914   struct choosenode *cn = user_data;
915   int ind, last_ind, n;
916
917   D(("clicked_choosenode %s", cn->path));
918   if(event->type == GDK_BUTTON_RELEASE
919      && event->button == 1) {
920     /* Left click */
921     if(cn->flags & CN_EXPANDABLE) {
922       /* This is a directory.  Flip its expansion status. */
923       if(cn->flags & CN_EXPANDED)
924         contract_node(cn);
925       else
926         expand_node(cn, 0/*!contingent*/);
927       last_click = 0;
928     } else {
929       /* This is a file.  Adjust selection status */
930       /* TODO the basic logic here is essentially the same as that in queue.c.
931        * Can we share code at all? */
932       switch(event->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) {
933       case 0:
934         clear_selection(root);
935         set_selection(cn, 1);
936         last_click = cn;
937         break;
938       case GDK_CONTROL_MASK:
939         set_selection(cn, !(cn->flags & CN_SELECTED));
940         last_click = cn;
941         break;
942       case GDK_SHIFT_MASK:
943       case GDK_SHIFT_MASK|GDK_CONTROL_MASK:
944         if(last_click && last_click->parent == cn->parent) {
945           /* Figure out where the current and last clicks are in the list */
946           ind = last_ind = -1;
947           for(n = 0; n < cn->parent->children.nvec; ++n) {
948             if(cn->parent->children.vec[n] == cn)
949               ind = n;
950             if(cn->parent->children.vec[n] == last_click)
951               last_ind = n;
952           }
953           /* Test shouldn't ever fail, but still */
954           if(ind >= 0 && last_ind >= 0) {
955             if(!(event->state & GDK_CONTROL_MASK)) {
956               for(n = 0; n < cn->parent->children.nvec; ++n)
957                 set_selection(cn->parent->children.vec[n], 0);
958             }
959             if(ind > last_ind)
960               for(n = last_ind; n <= ind; ++n)
961                 set_selection(cn->parent->children.vec[n], 1);
962             else
963               for(n = ind; n <= last_ind; ++n)
964                 set_selection(cn->parent->children.vec[n], 1);
965             if(event->state & GDK_CONTROL_MASK)
966               last_click = cn;
967           }
968         }
969         /* TODO trying to select a range that doesn't share a single parent
970          * currently does not work, but it ought to. */
971         break;
972       }
973     }
974   } else if(event->type == GDK_BUTTON_RELEASE
975      && event->button == 2) {
976     /* Middle click - play the pointed track */
977     if(!(cn->flags & CN_EXPANDABLE)) {
978       clear_selection(root);
979       set_selection(cn, 1);
980       gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
981       disorder_eclient_play(client, cn->path, 0, 0);
982       last_click = 0;
983     }
984   } else if(event->type == GDK_BUTTON_PRESS
985      && event->button == 3) {
986     struct choose_menuitem *const menuitems =
987       (cn->flags & CN_EXPANDABLE ? dir_menuitems : track_menuitems);
988     GtkWidget *const menu =
989       (cn->flags & CN_EXPANDABLE ? dir_menu : track_menu);
990     /* Right click.  Pop up a menu. */
991     /* If the current file isn't selected, switch the selection to just that.
992      * (If we're looking at a directory then leave the selection alone.) */
993     if(!(cn->flags & CN_EXPANDABLE) && !(cn->flags & CN_SELECTED)) {
994       clear_selection(root);
995       set_selection(cn, 1);
996       last_click = cn;
997     }
998     /* Set the item sensitivity and callbacks */
999     for(n = 0; menuitems[n].name; ++n) {
1000       if(menuitems[n].handlerid)
1001         g_signal_handler_disconnect(menuitems[n].w,
1002                                     menuitems[n].handlerid);
1003       gtk_widget_set_sensitive(menuitems[n].w,
1004                                menuitems[n].sensitive(cn));
1005       menuitems[n].handlerid = g_signal_connect
1006         (menuitems[n].w, "activate", G_CALLBACK(menuitems[n].activate), cn);
1007     }
1008     /* Pop up the menu */
1009     gtk_widget_show_all(menu);
1010     gtk_menu_popup(GTK_MENU(menu), 0, 0, 0, 0,
1011                    event->button, event->time);
1012   }
1013 }
1014
1015 /** @brief Called BY GTK+ to tell us the search entry box has changed */
1016 static void searchentry_changed(GtkEditable attribute((unused)) *editable,
1017                                 gpointer attribute((unused)) user_data) {
1018   initiate_search();
1019 }
1020
1021 /* Track menu items -------------------------------------------------------- */
1022
1023 /** @brief Recursive step for gather_selected() */
1024 static void recurse_selected(struct choosenode *cn, struct vector *v) {
1025   int n;
1026
1027   if(cn->flags & CN_EXPANDABLE) {
1028     if(cn->flags & CN_EXPANDED)
1029       for(n = 0; n < cn->children.nvec; ++n)
1030         recurse_selected(cn->children.vec[n], v);
1031   } else {
1032     if((cn->flags & CN_SELECTED) && cn->path)
1033       vector_append(v, (char *)cn->path);
1034   }
1035 }
1036
1037 /*** @brief Get a list of all the selected tracks */
1038 static const char **gather_selected(int *ntracks) {
1039   struct vector v;
1040
1041   vector_init(&v);
1042   recurse_selected(root, &v);
1043   vector_terminate(&v);
1044   if(ntracks) *ntracks = v.nvec;
1045   return (const char **)v.vec;
1046 }
1047
1048 /** @brief Called when the track menu's play option is activated */
1049 static void activate_track_play(GtkMenuItem attribute((unused)) *menuitem,
1050                                 gpointer attribute((unused)) user_data) {
1051   const char **tracks = gather_selected(0);
1052   int n;
1053   
1054   gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
1055   for(n = 0; tracks[n]; ++n)
1056     disorder_eclient_play(client, tracks[n], 0, 0);
1057 }
1058
1059 /** @brief Called when the menu's properties option is activated */
1060 static void activate_track_properties(GtkMenuItem attribute((unused)) *menuitem,
1061                                       gpointer attribute((unused)) user_data) {
1062   int ntracks;
1063   const char **tracks = gather_selected(&ntracks);
1064
1065   properties(ntracks, tracks);
1066 }
1067
1068 /** @brief Determine whether the menu's play option should be sensitive */
1069 static gboolean sensitive_track_play(struct choosenode attribute((unused)) *cn) {
1070   return (!!files_selected
1071           && (disorder_eclient_state(client) & DISORDER_CONNECTED));
1072 }
1073
1074 /** @brief Determine whether the menu's properties option should be sensitive */
1075 static gboolean sensitive_track_properties(struct choosenode attribute((unused)) *cn) {
1076   return !!files_selected && (disorder_eclient_state(client) & DISORDER_CONNECTED);
1077 }
1078
1079 /* Directory menu items ---------------------------------------------------- */
1080
1081 /** @brief Return the file children of @p cn
1082  *
1083  * The list is terminated by a null pointer.
1084  */
1085 static const char **dir_files(struct choosenode *cn, int *nfiles) {
1086   const char **files = xcalloc(cn->children.nvec + 1, sizeof (char *));
1087   int n, m;
1088
1089   for(n = m = 0; n < cn->children.nvec; ++n) 
1090     if(!(cn->children.vec[n]->flags & CN_EXPANDABLE))
1091       files[m++] = cn->children.vec[n]->path;
1092   files[m] = 0;
1093   if(nfiles) *nfiles = m;
1094   return files;
1095 }
1096
1097 static void play_dir(struct choosenode *cn,
1098                      void attribute((unused)) *wfu) {
1099   int ntracks, n;
1100   const char **tracks = dir_files(cn, &ntracks);
1101   
1102   gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
1103   for(n = 0; n < ntracks; ++n)
1104     disorder_eclient_play(client, tracks[n], 0, 0);
1105 }
1106
1107 static void properties_dir(struct choosenode *cn,
1108                            void attribute((unused)) *wfu) {
1109   int ntracks;
1110   const char **tracks = dir_files(cn, &ntracks);
1111   
1112   properties(ntracks, tracks);
1113 }
1114
1115 static void select_dir(struct choosenode *cn,
1116                        void attribute((unused)) *wfu) {
1117   int n;
1118
1119   clear_selection(root);
1120   for(n = 0; n < cn->children.nvec; ++n) 
1121     set_selection(cn->children.vec[n], 1);
1122 }
1123
1124 /** @brief Ensure @p cn is expanded and then call @p callback */
1125 static void call_with_dir(struct choosenode *cn,
1126                           when_filled_callback *whenfilled,
1127                           void *wfu) {
1128   if(!(cn->flags & CN_EXPANDABLE))
1129     return;                             /* something went wrong */
1130   if(cn->flags & CN_EXPANDED)
1131     /* @p cn is already open */
1132     whenfilled(cn, wfu);
1133   else {
1134     /* @p cn is not open, arrange for the callback to go off when it is
1135      * opened */
1136     cn->whenfilled = whenfilled;
1137     cn->wfu = wfu;
1138     expand_node(cn, 0/*not contingnet upon search*/);
1139   }
1140 }
1141
1142 /** @brief Called when the directory menu's play option is activated */
1143 static void activate_dir_play(GtkMenuItem attribute((unused)) *menuitem,
1144                               gpointer user_data) {
1145   struct choosenode *const cn = (struct choosenode *)user_data;
1146
1147   call_with_dir(cn, play_dir, 0);
1148 }
1149
1150 /** @brief Called when the directory menu's properties option is activated */
1151 static void activate_dir_properties(GtkMenuItem attribute((unused)) *menuitem,
1152                                     gpointer user_data) {
1153   struct choosenode *const cn = (struct choosenode *)user_data;
1154
1155   call_with_dir(cn, properties_dir, 0);
1156 }
1157
1158 /** @brief Called when the directory menu's select option is activated */
1159 static void activate_dir_select(GtkMenuItem attribute((unused)) *menuitem,
1160                                 gpointer user_data) {
1161   struct choosenode *const cn = (struct choosenode *)user_data;
1162
1163   call_with_dir(cn, select_dir,  0);
1164 }
1165
1166 /** @brief Determine whether the directory menu's play option should be sensitive */
1167 static gboolean sensitive_dir_play(struct choosenode attribute((unused)) *cn) {
1168   return !!(disorder_eclient_state(client) & DISORDER_CONNECTED);
1169 }
1170
1171 /** @brief Determine whether the directory menu's properties option should be sensitive */
1172 static gboolean sensitive_dir_properties(struct choosenode attribute((unused)) *cn) {
1173   return !!(disorder_eclient_state(client) & DISORDER_CONNECTED);
1174 }
1175
1176 /** @brief Determine whether the directory menu's select option should be sensitive */
1177 static gboolean sensitive_dir_select(struct choosenode attribute((unused)) *cn) {
1178   return TRUE;
1179 }
1180
1181
1182
1183 /* Main menu plumbing ------------------------------------------------------ */
1184
1185 /** @brief Determine whether the edit menu's properties option should be sensitive */
1186 static int choose_properties_sensitive(GtkWidget attribute((unused)) *w) {
1187   return !!files_selected && (disorder_eclient_state(client) & DISORDER_CONNECTED);
1188 }
1189
1190 /** @brief Determine whether the edit menu's select all option should be sensitive
1191  *
1192  * TODO not implemented,  see also choose_selectall_activate()
1193  */
1194 static int choose_selectall_sensitive(GtkWidget attribute((unused)) *w) {
1195   return FALSE;
1196 }
1197
1198 /** @brief Called when the edit menu's properties option is activated */
1199 static void choose_properties_activate(GtkWidget attribute((unused)) *w) {
1200   activate_track_properties(0, 0);
1201 }
1202
1203 /** @brief Called when the edit menu's select all option is activated
1204  *
1205  * TODO not implemented, see choose_selectall_sensitive() */
1206 static void choose_selectall_activate(GtkWidget attribute((unused)) *w) {
1207 }
1208
1209 /** @brief Main menu callbacks for Choose screen */
1210 static const struct tabtype tabtype_choose = {
1211   choose_properties_sensitive,
1212   choose_selectall_sensitive,
1213   choose_properties_activate,
1214   choose_selectall_activate,
1215 };
1216
1217 /* Public entry points ----------------------------------------------------- */
1218
1219 /** @brief Create a track choice widget */
1220 GtkWidget *choose_widget(void) {
1221   int n;
1222   GtkWidget *scrolled;
1223   GtkWidget *vbox, *hbox, *clearsearch;
1224
1225   /*
1226    *   +--vbox-------------------------------------------------------+
1227    *   | +-hbox----------------------------------------------------+ |
1228    *   | | searchentry                               | clearsearch | |
1229    *   | +---------------------------------------------------------+ |
1230    *   | +-scrolled------------------------------------------------+ |
1231    *   | | +-chooselayout------------------------------------++--+ | |
1232    *   | | | Tree structure is manually layed out in here    ||^^| | |
1233    *   | | |                                                 ||  | | |
1234    *   | | |                                                 ||  | | |
1235    *   | | |                                                 ||  | | |
1236    *   | | |                                                 ||vv| | |
1237    *   | | +-------------------------------------------------++--+ | |
1238    *   | | +-------------------------------------------------+     | |
1239    *   | | |<                                               >|     | |
1240    *   | | +-------------------------------------------------+     | |
1241    *   | +---------------------------------------------------------+ |
1242    *   +-------------------------------------------------------------+
1243    */
1244   
1245   /* Text entry box for search terms */
1246   NW(entry);
1247   searchentry = gtk_entry_new();
1248   g_signal_connect(searchentry, "changed", G_CALLBACK(searchentry_changed), 0);
1249   gtk_tooltips_set_tip(tips, searchentry, "Enter search terms here; search is automatic", "");
1250
1251   /* Cancel button to clear the search */
1252   NW(button);
1253   clearsearch = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
1254   g_signal_connect(G_OBJECT(clearsearch), "clicked",
1255                    G_CALLBACK(clearsearch_clicked), 0);
1256   gtk_tooltips_set_tip(tips, clearsearch, "Clear search terms", "");
1257
1258
1259   /* hbox packs the search box and the cancel button together on a line */
1260   NW(hbox);
1261   hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*spacing*/);
1262   gtk_box_pack_start(GTK_BOX(hbox), searchentry,
1263                      TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
1264   gtk_box_pack_end(GTK_BOX(hbox), clearsearch,
1265                    FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
1266   
1267   /* chooselayout contains the currently visible subset of the track
1268    * namespace */
1269   NW(layout);
1270   chooselayout = gtk_layout_new(0, 0);
1271   root = newnode(0/*parent*/, "<root>", "All files", "",
1272                  CN_EXPANDABLE, fill_root_node);
1273   expand_node(root, 0);                 /* will call redisplay_tree */
1274   /* Create the popup menus */
1275   NW(menu);
1276   track_menu = gtk_menu_new();
1277   g_signal_connect(track_menu, "destroy", G_CALLBACK(gtk_widget_destroyed),
1278                    &track_menu);
1279   for(n = 0; track_menuitems[n].name; ++n) {
1280     NW(menu_item);
1281     track_menuitems[n].w = 
1282       gtk_menu_item_new_with_label(track_menuitems[n].name);
1283     gtk_menu_attach(GTK_MENU(track_menu), track_menuitems[n].w,
1284                     0, 1, n, n + 1);
1285   }
1286   NW(menu);
1287   dir_menu = gtk_menu_new();
1288   g_signal_connect(dir_menu, "destroy", G_CALLBACK(gtk_widget_destroyed),
1289                    &dir_menu);
1290   for(n = 0; dir_menuitems[n].name; ++n) {
1291     NW(menu_item);
1292     dir_menuitems[n].w = 
1293       gtk_menu_item_new_with_label(dir_menuitems[n].name);
1294     gtk_menu_attach(GTK_MENU(dir_menu), dir_menuitems[n].w,
1295                     0, 1, n, n + 1);
1296   }
1297   /* The layout is scrollable */
1298   scrolled = scroll_widget(GTK_WIDGET(chooselayout), "choose");
1299
1300   /* The scrollable layout and the search hbox go together in a vbox */
1301   NW(vbox);
1302   vbox = gtk_vbox_new(FALSE/*homogenous*/, 1/*spacing*/);
1303   gtk_box_pack_start(GTK_BOX(vbox), hbox,
1304                      FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
1305   gtk_box_pack_end(GTK_BOX(vbox), scrolled,
1306                    TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
1307
1308   g_object_set_data(G_OBJECT(vbox), "type", (void *)&tabtype_choose);
1309   return vbox;
1310 }
1311
1312 /** @brief Called when something we care about here might have changed */
1313 void choose_update(void) {
1314   redisplay_tree();
1315 }
1316
1317 /*
1318 Local Variables:
1319 c-basic-offset:2
1320 comment-column:40
1321 fill-column:79
1322 indent-tabs-mode:nil
1323 End:
1324 */