chiark / gitweb /
8e43d178c0e47dd19902af797af515c207664c54
[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     }
615   }
616 }
617
618 /** @brief Initiate a search 
619  *
620  * If a search is underway we set @ref search_obsolete and restart the search
621  * in search_completed() above.
622  */
623 static void initiate_search(void) {
624   char *terms, *e;
625
626   /* Find out what the user is after */
627   terms = xstrdup(gtk_entry_get_text(GTK_ENTRY(searchentry)));
628   /* Strip leading and trailing space */
629   while(*terms == ' ') ++terms;
630   e = terms + strlen(terms);
631   while(e > terms && e[-1] == ' ') --e;
632   *e = 0;
633   /* If a search is already underway then mark it as obsolete.  We'll revisit
634    * when it returns. */
635   if(search_in_flight) {
636     search_obsolete = 1;
637     return;
638   }
639   if(*terms) {
640     /* There's still something left.  Initiate the search. */
641     if(disorder_eclient_search(client, search_completed, terms, 0)) {
642       /* The search terms are bad!  We treat this as if there were no search
643        * terms at all.  Some kind of feedback would be handy. */
644       fprintf(stderr, "bad terms [%s]\n", terms); /* TODO */
645       search_completed(0, 0, 0);
646     } else {
647       search_in_flight = 1;
648     }
649   } else {
650     /* No search terms - we want to see all tracks */
651     search_completed(0, 0, 0);
652   }
653 }
654
655 /** @brief Called when the cancel search button is clicked */
656 static void clearsearch_clicked(GtkButton attribute((unused)) *button,
657                                 gpointer attribute((unused)) userdata) {
658   gtk_entry_set_text(GTK_ENTRY(searchentry), "");
659 }
660
661 /* Display functions ------------------------------------------------------- */
662
663 /** @brief Delete all the widgets in the tree */
664 static void delete_widgets(struct choosenode *cn) {
665   int n;
666
667   delete_cn_widgets(cn);
668   for(n = 0; n < cn->children.nvec; ++n)
669     delete_widgets(cn->children.vec[n]);
670   cn->flags &= ~(CN_DISPLAYED|CN_SELECTED);
671   files_selected = 0;
672 }
673
674 /** @brief Update the display */
675 static void redisplay_tree(void) {
676   struct displaydata d;
677   guint oldwidth, oldheight;
678
679   D(("redisplay_tree"));
680   /* We'll count these up empirically each time */
681   files_selected = 0;
682   files_visible = 0;
683   /* Correct the layout and find out how much space it uses */
684   MTAG_PUSH("display_tree");
685   d = display_tree(root, 0, 0);
686   MTAG_POP();
687   /* We must set the total size or scrolling will not work (it wouldn't be hard
688    * for GtkLayout to figure it out for itself but presumably you're supposed
689    * to be able to have widgets off the edge of the layuot.)
690    *
691    * There is a problem: if we shrink the size then the part of the screen that
692    * is outside the new size but inside the old one is not updated.  I think
693    * this is arguably bug in GTK+ but it's easy to force a redraw if this
694    * region is nonempty.
695    */
696   gtk_layout_get_size(GTK_LAYOUT(chooselayout), &oldwidth, &oldheight);
697   if(oldwidth > d.width || oldheight > d.height)
698     gtk_widget_queue_draw(chooselayout);
699   gtk_layout_set_size(GTK_LAYOUT(chooselayout), d.width, d.height);
700   /* Notify the main menu of any recent changes */
701   menu_update(-1);
702 }
703
704 /** @brief Recursive step for redisplay_tree()
705  *
706  * Makes sure all displayed widgets from CN down exist and are in their proper
707  * place and return the maximum space used.
708  */
709 static struct displaydata display_tree(struct choosenode *cn, int x, int y) {
710   int n, aw;
711   GtkRequisition req;
712   struct displaydata d, cd;
713   GdkPixbuf *pb;
714   const char *name;
715   
716   D(("display_tree %s %d,%d", cn->path, x, y));
717
718   /* An expandable item contains an arrow and a text label.  When you press the
719    * button it flips its expand state.
720    *
721    * A non-expandable item has just a text label and no arrow.
722    */
723   if(!cn->container) {
724     MTAG_PUSH("make_widgets_1");
725     /* Widgets need to be created */
726     NW(hbox);
727     cn->hbox = gtk_hbox_new(FALSE, 1);
728     if(cn->flags & CN_EXPANDABLE) {
729       NW(arrow);
730       cn->arrow = gtk_arrow_new(cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN
731                                                         : GTK_ARROW_RIGHT,
732                                 GTK_SHADOW_NONE);
733       cn->marker = 0;
734     } else {
735       cn->arrow = 0;
736       if((pb = find_image("notes.png"))) {
737         NW(image);
738         cn->marker = gtk_image_new_from_pixbuf(pb);
739       }
740     }
741     MTAG_POP();
742     MTAG_PUSH("make_widgets_2");
743     NW(label);
744     cn->label = gtk_label_new(cn->display);
745     if(cn->arrow)
746       gtk_container_add(GTK_CONTAINER(cn->hbox), cn->arrow);
747     gtk_container_add(GTK_CONTAINER(cn->hbox), cn->label);
748     if(cn->marker)
749       gtk_container_add(GTK_CONTAINER(cn->hbox), cn->marker);
750     MTAG_POP();
751     MTAG_PUSH("make_widgets_3");
752     NW(event_box);
753     cn->container = gtk_event_box_new();
754     gtk_container_add(GTK_CONTAINER(cn->container), cn->hbox);
755     g_signal_connect(cn->container, "button-release-event", 
756                      G_CALLBACK(clicked_choosenode), cn);
757     g_signal_connect(cn->container, "button-press-event", 
758                      G_CALLBACK(clicked_choosenode), cn);
759     g_object_ref(cn->container);
760     /* Show everything by default */
761     gtk_widget_show_all(cn->container);
762     MTAG_POP();
763   }
764   assert(cn->container);
765   /* Make sure the widget name is right */
766   name = (cn->flags & CN_EXPANDABLE
767           ? "choose-dir"
768           : is_search_result(cn->path) ? "choose-search" : "choose");
769   gtk_widget_set_name(cn->label, name);
770   gtk_widget_set_name(cn->container, name);
771   /* Make sure the icon is right */
772   if(cn->flags & CN_EXPANDABLE)
773     gtk_arrow_set(GTK_ARROW(cn->arrow),
774                   cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN : GTK_ARROW_RIGHT,
775                   GTK_SHADOW_NONE);
776   else if(cn->marker)
777     /* Make sure the queued marker is right */
778     /* TODO: doesn't always work */
779     (queued(cn->path) ? gtk_widget_show : gtk_widget_hide)(cn->marker);
780   /* Put the widget in the right place */
781   if(cn->flags & CN_DISPLAYED)
782     gtk_layout_move(GTK_LAYOUT(chooselayout), cn->container, x, y);
783   else {
784     gtk_layout_put(GTK_LAYOUT(chooselayout), cn->container, x, y);
785     cn->flags |= CN_DISPLAYED;
786     /* Now chooselayout has a ref to the container */
787     g_object_unref(cn->container);
788   }
789   /* Set the widget's selection status */
790   if(!(cn->flags & CN_EXPANDABLE))
791     display_selection(cn);
792   /* Find the size used so we can get vertical positioning right. */
793   gtk_widget_size_request(cn->container, &req);
794   d.width = x + req.width;
795   d.height = y + req.height;
796   if(cn->flags & CN_EXPANDED) {
797     /* We'll offset children by the size of the arrow whatever it might be. */
798     assert(cn->arrow);
799     gtk_widget_size_request(cn->arrow, &req);
800     aw = req.width;
801     for(n = 0; n < cn->children.nvec; ++n) {
802       cd = display_tree(cn->children.vec[n], x + aw, d.height);
803       if(cd.width > d.width)
804         d.width = cd.width;
805       d.height = cd.height;
806     }
807   } else {
808     for(n = 0; n < cn->children.nvec; ++n)
809       undisplay_tree(cn->children.vec[n]);
810   }
811   if(!(cn->flags & CN_EXPANDABLE)) {
812     ++files_visible;
813     if(cn->flags & CN_SELECTED)
814       ++files_selected;
815   }
816   /* report back how much space we used */
817   D(("display_tree %s %d,%d total size %dx%d", cn->path, x, y,
818      d.width, d.height));
819   return d;
820 }
821
822 /** @brief Remove widgets for newly hidden nodes */
823 static void undisplay_tree(struct choosenode *cn) {
824   int n;
825
826   D(("undisplay_tree %s", cn->path));
827   /* Remove this widget from the display */
828   if(cn->flags & CN_DISPLAYED) {
829     gtk_container_remove(GTK_CONTAINER(chooselayout), cn->container);
830     cn->flags ^= CN_DISPLAYED;
831   }
832   /* Remove children too */
833   for(n = 0; n < cn->children.nvec; ++n)
834     undisplay_tree(cn->children.vec[n]);
835 }
836
837 /* Selection --------------------------------------------------------------- */
838
839 /** @brief Mark the widget @p cn according to its selection state */
840 static void display_selection(struct choosenode *cn) {
841   /* Need foreground and background colors */
842   gtk_widget_set_state(cn->label, (cn->flags & CN_SELECTED
843                                    ? GTK_STATE_SELECTED : GTK_STATE_NORMAL));
844   gtk_widget_set_state(cn->container, (cn->flags & CN_SELECTED
845                                        ? GTK_STATE_SELECTED : GTK_STATE_NORMAL));
846 }
847
848 /** @brief Set the selection state of a widget
849  *
850  * Directories can never be selected, we just ignore attempts to do so. */
851 static void set_selection(struct choosenode *cn, int selected) {
852   unsigned f = selected ? CN_SELECTED : 0;
853
854   D(("set_selection %d %s", selected, cn->path));
855   if(!(cn->flags & CN_EXPANDABLE) && (cn->flags & CN_SELECTED) != f) {
856     cn->flags ^= CN_SELECTED;
857     /* Maintain selection count */
858     if(selected)
859       ++files_selected;
860     else
861       --files_selected;
862     display_selection(cn);
863     /* Update main menu sensitivity */
864     menu_update(-1);
865   }
866 }
867
868 /** @brief Recursively clear all selection bits from CN down */
869 static void clear_selection(struct choosenode *cn) {
870   int n;
871
872   set_selection(cn, 0);
873   for(n = 0; n < cn->children.nvec; ++n)
874     clear_selection(cn->children.vec[n]);
875 }
876
877 /* User actions ------------------------------------------------------------ */
878
879 /** @brief Clicked on something
880  *
881  * This implements playing, all the modifiers for selection, etc.
882  */
883 static void clicked_choosenode(GtkWidget attribute((unused)) *widget,
884                                GdkEventButton *event,
885                                gpointer user_data) {
886   struct choosenode *cn = user_data;
887   int ind, last_ind, n;
888
889   D(("clicked_choosenode %s", cn->path));
890   if(event->type == GDK_BUTTON_RELEASE
891      && event->button == 1) {
892     /* Left click */
893     if(cn->flags & CN_EXPANDABLE) {
894       /* This is a directory.  Flip its expansion status. */
895       if(cn->flags & CN_EXPANDED)
896         contract_node(cn);
897       else
898         expand_node(cn, 0/*!contingent*/);
899       last_click = 0;
900     } else {
901       /* This is a file.  Adjust selection status */
902       /* TODO the basic logic here is essentially the same as that in queue.c.
903        * Can we share code at all? */
904       switch(event->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) {
905       case 0:
906         clear_selection(root);
907         set_selection(cn, 1);
908         last_click = cn;
909         break;
910       case GDK_CONTROL_MASK:
911         set_selection(cn, !(cn->flags & CN_SELECTED));
912         last_click = cn;
913         break;
914       case GDK_SHIFT_MASK:
915       case GDK_SHIFT_MASK|GDK_CONTROL_MASK:
916         if(last_click && last_click->parent == cn->parent) {
917           /* Figure out where the current and last clicks are in the list */
918           ind = last_ind = -1;
919           for(n = 0; n < cn->parent->children.nvec; ++n) {
920             if(cn->parent->children.vec[n] == cn)
921               ind = n;
922             if(cn->parent->children.vec[n] == last_click)
923               last_ind = n;
924           }
925           /* Test shouldn't ever fail, but still */
926           if(ind >= 0 && last_ind >= 0) {
927             if(!(event->state & GDK_CONTROL_MASK)) {
928               for(n = 0; n < cn->parent->children.nvec; ++n)
929                 set_selection(cn->parent->children.vec[n], 0);
930             }
931             if(ind > last_ind)
932               for(n = last_ind; n <= ind; ++n)
933                 set_selection(cn->parent->children.vec[n], 1);
934             else
935               for(n = ind; n <= last_ind; ++n)
936                 set_selection(cn->parent->children.vec[n], 1);
937             if(event->state & GDK_CONTROL_MASK)
938               last_click = cn;
939           }
940         }
941         /* TODO trying to select a range that doesn't share a single parent
942          * currently does not work, but it ought to. */
943         break;
944       }
945     }
946   } else if(event->type == GDK_BUTTON_RELEASE
947      && event->button == 2) {
948     /* Middle click - play the pointed track */
949     if(!(cn->flags & CN_EXPANDABLE)) {
950       clear_selection(root);
951       set_selection(cn, 1);
952       gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
953       disorder_eclient_play(client, cn->path, 0, 0);
954       last_click = 0;
955     }
956   } else if(event->type == GDK_BUTTON_PRESS
957      && event->button == 3) {
958     struct choose_menuitem *const menuitems =
959       (cn->flags & CN_EXPANDABLE ? dir_menuitems : track_menuitems);
960     GtkWidget *const menu =
961       (cn->flags & CN_EXPANDABLE ? dir_menu : track_menu);
962     /* Right click.  Pop up a menu. */
963     /* If the current file isn't selected, switch the selection to just that.
964      * (If we're looking at a directory then leave the selection alone.) */
965     if(!(cn->flags & CN_EXPANDABLE) && !(cn->flags & CN_SELECTED)) {
966       clear_selection(root);
967       set_selection(cn, 1);
968       last_click = cn;
969     }
970     /* Set the item sensitivity and callbacks */
971     for(n = 0; menuitems[n].name; ++n) {
972       if(menuitems[n].handlerid)
973         g_signal_handler_disconnect(menuitems[n].w,
974                                     menuitems[n].handlerid);
975       gtk_widget_set_sensitive(menuitems[n].w,
976                                menuitems[n].sensitive(cn));
977       menuitems[n].handlerid = g_signal_connect
978         (menuitems[n].w, "activate", G_CALLBACK(menuitems[n].activate), cn);
979     }
980     /* Pop up the menu */
981     gtk_widget_show_all(menu);
982     gtk_menu_popup(GTK_MENU(menu), 0, 0, 0, 0,
983                    event->button, event->time);
984   }
985 }
986
987 /** @brief Called BY GTK+ to tell us the search entry box has changed */
988 static void searchentry_changed(GtkEditable attribute((unused)) *editable,
989                                 gpointer attribute((unused)) user_data) {
990   initiate_search();
991 }
992
993 /* Track menu items -------------------------------------------------------- */
994
995 /** @brief Recursive step for gather_selected() */
996 static void recurse_selected(struct choosenode *cn, struct vector *v) {
997   int n;
998
999   if(cn->flags & CN_EXPANDABLE) {
1000     if(cn->flags & CN_EXPANDED)
1001       for(n = 0; n < cn->children.nvec; ++n)
1002         recurse_selected(cn->children.vec[n], v);
1003   } else {
1004     if((cn->flags & CN_SELECTED) && cn->path)
1005       vector_append(v, (char *)cn->path);
1006   }
1007 }
1008
1009 /*** @brief Get a list of all the selected tracks */
1010 static const char **gather_selected(int *ntracks) {
1011   struct vector v;
1012
1013   vector_init(&v);
1014   recurse_selected(root, &v);
1015   vector_terminate(&v);
1016   if(ntracks) *ntracks = v.nvec;
1017   return (const char **)v.vec;
1018 }
1019
1020 /** @brief Called when the track menu's play option is activated */
1021 static void activate_track_play(GtkMenuItem attribute((unused)) *menuitem,
1022                                 gpointer attribute((unused)) user_data) {
1023   const char **tracks = gather_selected(0);
1024   int n;
1025   
1026   gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
1027   for(n = 0; tracks[n]; ++n)
1028     disorder_eclient_play(client, tracks[n], 0, 0);
1029 }
1030
1031 /** @brief Called when the menu's properties option is activated */
1032 static void activate_track_properties(GtkMenuItem attribute((unused)) *menuitem,
1033                                       gpointer attribute((unused)) user_data) {
1034   int ntracks;
1035   const char **tracks = gather_selected(&ntracks);
1036
1037   properties(ntracks, tracks);
1038 }
1039
1040 /** @brief Determine whether the menu's play option should be sensitive */
1041 static gboolean sensitive_track_play(struct choosenode attribute((unused)) *cn) {
1042   return (!!files_selected
1043           && (disorder_eclient_state(client) & DISORDER_CONNECTED));
1044 }
1045
1046 /** @brief Determine whether the menu's properties option should be sensitive */
1047 static gboolean sensitive_track_properties(struct choosenode attribute((unused)) *cn) {
1048   return !!files_selected && (disorder_eclient_state(client) & DISORDER_CONNECTED);
1049 }
1050
1051 /* Directory menu items ---------------------------------------------------- */
1052
1053 /** @brief Return the file children of @p cn
1054  *
1055  * The list is terminated by a null pointer.
1056  */
1057 static const char **dir_files(struct choosenode *cn, int *nfiles) {
1058   const char **files = xcalloc(cn->children.nvec + 1, sizeof (char *));
1059   int n, m;
1060
1061   for(n = m = 0; n < cn->children.nvec; ++n) 
1062     if(!(cn->children.vec[n]->flags & CN_EXPANDABLE))
1063       files[m++] = cn->children.vec[n]->path;
1064   files[m] = 0;
1065   if(nfiles) *nfiles = m;
1066   return files;
1067 }
1068
1069 static void play_dir(struct choosenode *cn,
1070                      void attribute((unused)) *wfu) {
1071   int ntracks, n;
1072   const char **tracks = dir_files(cn, &ntracks);
1073   
1074   gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
1075   for(n = 0; n < ntracks; ++n)
1076     disorder_eclient_play(client, tracks[n], 0, 0);
1077 }
1078
1079 static void properties_dir(struct choosenode *cn,
1080                            void attribute((unused)) *wfu) {
1081   int ntracks;
1082   const char **tracks = dir_files(cn, &ntracks);
1083   
1084   properties(ntracks, tracks);
1085 }
1086
1087 static void select_dir(struct choosenode *cn,
1088                        void attribute((unused)) *wfu) {
1089   int n;
1090
1091   clear_selection(root);
1092   for(n = 0; n < cn->children.nvec; ++n) 
1093     set_selection(cn->children.vec[n], 1);
1094 }
1095
1096 /** @brief Ensure @p cn is expanded and then call @p callback */
1097 static void call_with_dir(struct choosenode *cn,
1098                           when_filled_callback *whenfilled,
1099                           void *wfu) {
1100   if(!(cn->flags & CN_EXPANDABLE))
1101     return;                             /* something went wrong */
1102   if(cn->flags & CN_EXPANDED)
1103     /* @p cn is already open */
1104     whenfilled(cn, wfu);
1105   else {
1106     /* @p cn is not open, arrange for the callback to go off when it is
1107      * opened */
1108     cn->whenfilled = whenfilled;
1109     cn->wfu = wfu;
1110     expand_node(cn, 0/*not contingnet upon search*/);
1111   }
1112 }
1113
1114 /** @brief Called when the directory menu's play option is activated */
1115 static void activate_dir_play(GtkMenuItem attribute((unused)) *menuitem,
1116                               gpointer user_data) {
1117   struct choosenode *const cn = (struct choosenode *)user_data;
1118
1119   call_with_dir(cn, play_dir, 0);
1120 }
1121
1122 /** @brief Called when the directory menu's properties option is activated */
1123 static void activate_dir_properties(GtkMenuItem attribute((unused)) *menuitem,
1124                                     gpointer user_data) {
1125   struct choosenode *const cn = (struct choosenode *)user_data;
1126
1127   call_with_dir(cn, properties_dir, 0);
1128 }
1129
1130 /** @brief Called when the directory menu's select option is activated */
1131 static void activate_dir_select(GtkMenuItem attribute((unused)) *menuitem,
1132                                 gpointer user_data) {
1133   struct choosenode *const cn = (struct choosenode *)user_data;
1134
1135   call_with_dir(cn, select_dir,  0);
1136 }
1137
1138 /** @brief Determine whether the directory menu's play option should be sensitive */
1139 static gboolean sensitive_dir_play(struct choosenode attribute((unused)) *cn) {
1140   return !!(disorder_eclient_state(client) & DISORDER_CONNECTED);
1141 }
1142
1143 /** @brief Determine whether the directory menu's properties option should be sensitive */
1144 static gboolean sensitive_dir_properties(struct choosenode attribute((unused)) *cn) {
1145   return !!(disorder_eclient_state(client) & DISORDER_CONNECTED);
1146 }
1147
1148 /** @brief Determine whether the directory menu's select option should be sensitive */
1149 static gboolean sensitive_dir_select(struct choosenode attribute((unused)) *cn) {
1150   return TRUE;
1151 }
1152
1153
1154
1155 /* Main menu plumbing ------------------------------------------------------ */
1156
1157 /** @brief Determine whether the edit menu's properties option should be sensitive */
1158 static int choose_properties_sensitive(GtkWidget attribute((unused)) *w) {
1159   return !!files_selected && (disorder_eclient_state(client) & DISORDER_CONNECTED);
1160 }
1161
1162 /** @brief Determine whether the edit menu's select all option should be sensitive
1163  *
1164  * TODO not implemented,  see also choose_selectall_activate()
1165  */
1166 static int choose_selectall_sensitive(GtkWidget attribute((unused)) *w) {
1167   return FALSE;
1168 }
1169
1170 /** @brief Called when the edit menu's properties option is activated */
1171 static void choose_properties_activate(GtkWidget attribute((unused)) *w) {
1172   activate_track_properties(0, 0);
1173 }
1174
1175 /** @brief Called when the edit menu's select all option is activated
1176  *
1177  * TODO not implemented, see choose_selectall_sensitive() */
1178 static void choose_selectall_activate(GtkWidget attribute((unused)) *w) {
1179 }
1180
1181 /** @brief Main menu callbacks for Choose screen */
1182 static const struct tabtype tabtype_choose = {
1183   choose_properties_sensitive,
1184   choose_selectall_sensitive,
1185   choose_properties_activate,
1186   choose_selectall_activate,
1187 };
1188
1189 /* Public entry points ----------------------------------------------------- */
1190
1191 /** @brief Create a track choice widget */
1192 GtkWidget *choose_widget(void) {
1193   int n;
1194   GtkWidget *scrolled;
1195   GtkWidget *vbox, *hbox, *clearsearch;
1196
1197   /*
1198    *   +--vbox-------------------------------------------------------+
1199    *   | +-hbox----------------------------------------------------+ |
1200    *   | | searchentry                               | clearsearch | |
1201    *   | +---------------------------------------------------------+ |
1202    *   | +-scrolled------------------------------------------------+ |
1203    *   | | +-chooselayout------------------------------------++--+ | |
1204    *   | | | Tree structure is manually layed out in here    ||^^| | |
1205    *   | | |                                                 ||  | | |
1206    *   | | |                                                 ||  | | |
1207    *   | | |                                                 ||  | | |
1208    *   | | |                                                 ||vv| | |
1209    *   | | +-------------------------------------------------++--+ | |
1210    *   | | +-------------------------------------------------+     | |
1211    *   | | |<                                               >|     | |
1212    *   | | +-------------------------------------------------+     | |
1213    *   | +---------------------------------------------------------+ |
1214    *   +-------------------------------------------------------------+
1215    */
1216   
1217   /* Text entry box for search terms */
1218   NW(entry);
1219   searchentry = gtk_entry_new();
1220   g_signal_connect(searchentry, "changed", G_CALLBACK(searchentry_changed), 0);
1221   gtk_tooltips_set_tip(tips, searchentry, "Enter search terms here; search is automatic", "");
1222
1223   /* Cancel button to clear the search */
1224   NW(button);
1225   clearsearch = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
1226   g_signal_connect(G_OBJECT(clearsearch), "clicked",
1227                    G_CALLBACK(clearsearch_clicked), 0);
1228   gtk_tooltips_set_tip(tips, clearsearch, "Clear search terms", "");
1229
1230
1231   /* hbox packs the search box and the cancel button together on a line */
1232   NW(hbox);
1233   hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*spacing*/);
1234   gtk_box_pack_start(GTK_BOX(hbox), searchentry,
1235                      TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
1236   gtk_box_pack_end(GTK_BOX(hbox), clearsearch,
1237                    FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
1238   
1239   /* chooselayout contains the currently visible subset of the track
1240    * namespace */
1241   NW(layout);
1242   chooselayout = gtk_layout_new(0, 0);
1243   root = newnode(0/*parent*/, "<root>", "All files", "",
1244                  CN_EXPANDABLE, fill_root_node);
1245   expand_node(root, 0);                 /* will call redisplay_tree */
1246   /* Create the popup menus */
1247   NW(menu);
1248   track_menu = gtk_menu_new();
1249   g_signal_connect(track_menu, "destroy", G_CALLBACK(gtk_widget_destroyed),
1250                    &track_menu);
1251   for(n = 0; track_menuitems[n].name; ++n) {
1252     NW(menu_item);
1253     track_menuitems[n].w = 
1254       gtk_menu_item_new_with_label(track_menuitems[n].name);
1255     gtk_menu_attach(GTK_MENU(track_menu), track_menuitems[n].w,
1256                     0, 1, n, n + 1);
1257   }
1258   NW(menu);
1259   dir_menu = gtk_menu_new();
1260   g_signal_connect(dir_menu, "destroy", G_CALLBACK(gtk_widget_destroyed),
1261                    &dir_menu);
1262   for(n = 0; dir_menuitems[n].name; ++n) {
1263     NW(menu_item);
1264     dir_menuitems[n].w = 
1265       gtk_menu_item_new_with_label(dir_menuitems[n].name);
1266     gtk_menu_attach(GTK_MENU(dir_menu), dir_menuitems[n].w,
1267                     0, 1, n, n + 1);
1268   }
1269   /* The layout is scrollable */
1270   scrolled = scroll_widget(GTK_WIDGET(chooselayout), "choose");
1271
1272   /* The scrollable layout and the search hbox go together in a vbox */
1273   NW(vbox);
1274   vbox = gtk_vbox_new(FALSE/*homogenous*/, 1/*spacing*/);
1275   gtk_box_pack_start(GTK_BOX(vbox), hbox,
1276                      FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
1277   gtk_box_pack_end(GTK_BOX(vbox), scrolled,
1278                    TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
1279
1280   g_object_set_data(G_OBJECT(vbox), "type", (void *)&tabtype_choose);
1281   return vbox;
1282 }
1283
1284 /** @brief Called when something we care about here might have changed */
1285 void choose_update(void) {
1286   redisplay_tree();
1287 }
1288
1289 /*
1290 Local Variables:
1291 c-basic-offset:2
1292 comment-column:40
1293 fill-column:79
1294 indent-tabs-mode:nil
1295 End:
1296 */