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