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