chiark / gitweb /
improve new disobedience search code. --choosealpha is dead now
[disorder] / disobedience / choose.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
eb525fcd 3 * Copyright (C) 2006, 2007 Richard Kettlewell
460b9539 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 */
b9bdafc7
RK
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 */
460b9539 27
28#include "disobedience.h"
29
30/* Choose track ------------------------------------------------------------ */
31
e9b70a84 32WT(label);
33WT(event_box);
34WT(menu);
35WT(menu_item);
36WT(layout);
37WT(vbox);
38WT(arrow);
39WT(hbox);
40WT(button);
41WT(image);
42WT(entry);
43
460b9539 44/* Types */
45
46struct choosenode;
47
b9bdafc7 48/** @brief Accumulated information about the tree widget */
460b9539 49struct displaydata {
b9bdafc7
RK
50 /** @brief Maximum width required */
51 guint width;
52 /** @brief Maximum height required */
53 guint height;
460b9539 54};
55
56/* instantiate the node vector type */
b8956e9e
RK
57
58VECTOR_TYPE(nodevector, struct choosenode *, xrealloc);
460b9539 59
16e145a5
RK
60/** @brief Signature of function called when a choosenode is filled */
61typedef void (when_filled_callback)(struct choosenode *cn,
62 void *wfu);
63
b9bdafc7 64/** @brief One node in the virtual filesystem */
460b9539 65struct choosenode {
b9bdafc7
RK
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 */
460b9539 71 unsigned flags;
b9bdafc7
RK
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 */
16e145a5
RK
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 */
6a06a735 83#define CN_CONTINGENT 0x0080 /**< @brief expansion contingent on search */
b9bdafc7
RK
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 */
16e145a5
RK
91
92 when_filled_callback *whenfilled; /**< @brief called when filled or 0 */
93 void *wfu; /**< @brief passed to @c whenfilled */
460b9539 94};
95
b9bdafc7 96/** @brief One item in the popup menu */
16e145a5 97struct choose_menuitem {
460b9539 98 /* Parameters */
b9bdafc7 99 const char *name; /**< @brief name */
460b9539 100
101 /* Callbacks */
102 void (*activate)(GtkMenuItem *menuitem, gpointer user_data);
b9bdafc7
RK
103 /**< @brief Called to activate the menu item.
104 *
105 * @p user_data is the choosenode the mouse pointer is over. */
460b9539 106
107 gboolean (*sensitive)(struct choosenode *cn);
b9bdafc7
RK
108 /* @brief Called to determine whether the menu item should be sensitive.
109 *
110 * TODO? */
460b9539 111
112 /* State */
b9bdafc7
RK
113 gulong handlerid; /**< @brief signal handler ID */
114 GtkWidget *w; /**< @brief menu item widget */
460b9539 115};
116
117/* Variables */
118
119static GtkWidget *chooselayout;
b9bdafc7 120static GtkWidget *searchentry; /**< @brief search terms */
460b9539 121static struct choosenode *root;
16e145a5
RK
122static GtkWidget *track_menu; /**< @brief track popup menu */
123static GtkWidget *dir_menu; /**< @brief directory popup menu */
b9bdafc7
RK
124static struct choosenode *last_click; /**< @brief last clicked node for selection */
125static int files_visible; /**< @brief total files visible */
126static int files_selected; /**< @brief total files selected */
c3df9503 127static int gets_in_flight; /**< @brief total gets in flight */
b9bdafc7
RK
128static int search_in_flight; /**< @brief a search is underway */
129static int search_obsolete; /**< @brief the current search is void */
130static char **searchresults; /**< @brief search results */
131static int nsearchresults; /**< @brief number of results */
6a06a735
RK
132static int nsearchvisible; /**< @brief number of search results visible */
133static struct hash *searchhash; /**< @brief hash of search results */
c3df9503 134struct progress_window *spw; /**< @brief progress window */
460b9539 135
136/* Forward Declarations */
137
77d95881 138static void clear_children(struct choosenode *cn);
460b9539 139static 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 *));
145static void fill_root_node(struct choosenode *cn);
460b9539 146static void fill_directory_node(struct choosenode *cn);
147static void got_files(void *v, int nvec, char **vec);
148static void got_resolved_file(void *v, const char *track);
149static void got_dirs(void *v, int nvec, char **vec);
150
6a06a735 151static void expand_node(struct choosenode *cn, int contingent);
460b9539 152static void contract_node(struct choosenode *cn);
153static void updated_node(struct choosenode *cn, int redisplay);
154
155static void display_selection(struct choosenode *cn);
156static void clear_selection(struct choosenode *cn);
157
158static void redisplay_tree(void);
159static struct displaydata display_tree(struct choosenode *cn, int x, int y);
160static void undisplay_tree(struct choosenode *cn);
161static void initiate_search(void);
162static void delete_widgets(struct choosenode *cn);
6a06a735 163static void expand_from(struct choosenode *cn);
460b9539 164
165static void clicked_choosenode(GtkWidget attribute((unused)) *widget,
166 GdkEventButton *event,
167 gpointer user_data);
168
16e145a5
RK
169static void activate_track_play(GtkMenuItem *menuitem, gpointer user_data);
170static void activate_track_properties(GtkMenuItem *menuitem, gpointer user_data);
171
172static gboolean sensitive_track_play(struct choosenode *cn);
173static gboolean sensitive_track_properties(struct choosenode *cn);
174
175static void activate_dir_play(GtkMenuItem *menuitem, gpointer user_data);
176static void activate_dir_properties(GtkMenuItem *menuitem, gpointer user_data);
177static void activate_dir_select(GtkMenuItem *menuitem, gpointer user_data);
178
179static gboolean sensitive_dir_play(struct choosenode *cn);
180static gboolean sensitive_dir_properties(struct choosenode *cn);
181static gboolean sensitive_dir_select(struct choosenode *cn);
182
183/** @brief Track menu items */
184static 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 */
191static 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 }
460b9539 196};
197
460b9539 198/* Maintaining the data structure ------------------------------------------ */
199
6a06a735
RK
200static 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
b9bdafc7 241/** @brief Create a new node */
460b9539 242static 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
16e145a5
RK
267/** @brief Called when a node has been filled
268 *
269 * Response for calling @c whenfilled.
270 */
271static 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 }
6a06a735
RK
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 }
c3df9503
RK
283 if(gets_in_flight == 0 && nsearchvisible < nsearchresults)
284 expand_from(root);
16e145a5
RK
285}
286
b9bdafc7 287/** @brief Fill the root */
460b9539 288static void fill_root_node(struct choosenode *cn) {
460b9539 289 struct callbackdata *cbd;
290
291 D(("fill_root_node"));
77d95881 292 clear_children(cn);
c3df9503
RK
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;
460b9539 305}
306
b9bdafc7 307/** @brief Delete all the widgets owned by @p cn */
e9b70a84 308static 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
b9bdafc7
RK
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 */
460b9539 341static 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]);
e9b70a84 348 delete_cn_widgets(cn->children.vec[n]);
460b9539 349 }
350 cn->children.nvec = 0;
351}
352
b9bdafc7 353/** @brief Called with a list of files just below some node */
460b9539 354static 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
6a06a735 359 D(("got_files %d files for %s %s", nvec, cn->path, flags(cn)));
460b9539 360 /* Complicated by the need to resolve aliases. We can save a bit of effort
361 * by re-using cbd though. */
16e145a5 362 cn->flags &= ~CN_GETTING_FILES;
c3df9503 363 --gets_in_flight;
5459347d
RK
364 if((cn->pending = nvec)) {
365 cn->flags |= CN_RESOLVING_FILES;
c3df9503 366 for(n = 0; n < nvec; ++n) {
5459347d 367 disorder_eclient_resolve(client, got_resolved_file, vec[n], cbd);
c3df9503
RK
368 ++gets_in_flight;
369 }
5459347d 370 }
6a06a735
RK
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);
460b9539 375}
376
b9bdafc7 377/** @brief Called with an alias resolved filename */
460b9539 378static void got_resolved_file(void *v, const char *track) {
379 struct callbackdata *cbd = v;
380 struct choosenode *cn = cbd->u.choosenode, *file_cn;
381
6a06a735 382 D(("resolved %s %s %d left", cn->path, flags(cn), cn->pending - 1));
b9bdafc7 383 /* TODO as below */
460b9539 384 file_cn = newnode(cn, track,
385 trackname_transform("track", track, "display"),
386 trackname_transform("track", track, "sort"),
387 0/*flags*/, 0/*fill*/);
c3df9503 388 --gets_in_flight;
460b9539 389 /* Only bother updating when we've got the lot */
16e145a5
RK
390 if(--cn->pending == 0) {
391 cn->flags &= ~CN_RESOLVING_FILES;
460b9539 392 updated_node(cn, 1);
16e145a5
RK
393 if(!(cn->flags & CN_GETTING_ANY))
394 filled(cn);
395 }
460b9539 396}
397
b9bdafc7 398/** @brief Called with a list of directories just below some node */
460b9539 399static 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
6a06a735 404 D(("got_dirs %d dirs for %s %s", nvec, cn->path, flags(cn)));
b9bdafc7
RK
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 */
c3df9503 412 --gets_in_flight;
460b9539 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);
16e145a5
RK
419 cn->flags &= ~CN_GETTING_DIRS;
420 if(!(cn->flags & CN_GETTING_ANY))
421 filled(cn);
460b9539 422}
423
b9bdafc7 424/** @brief Fill a child node */
460b9539 425static void fill_directory_node(struct choosenode *cn) {
426 struct callbackdata *cbd;
427
428 D(("fill_directory_node %s", cn->path));
429 /* TODO: caching */
16e145a5
RK
430 if(cn->flags & CN_GETTING_ANY)
431 return;
460b9539 432 assert(report_label != 0);
433 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
1d0c28c7 434 clear_children(cn);
460b9539 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);
16e145a5 441 cn->flags |= CN_GETTING_FILES|CN_GETTING_DIRS;
c3df9503 442 gets_in_flight += 2;
460b9539 443}
444
b9bdafc7 445/** @brief Expand a node */
6a06a735
RK
446static void expand_node(struct choosenode *cn, int contingent) {
447 D(("expand_node %s %d %s", cn->path, contingent, flags(cn)));
460b9539 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;
6a06a735
RK
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 }
460b9539 468 /* TODO: visual feedback */
469 cn->fill(cn);
470}
471
6a06a735
RK
472/** @brief Make sure all the search results below @p cn are expanded
473 * @param cn Node to start at
474 */
475static void expand_from(struct choosenode *cn) {
476 int n;
6a06a735
RK
477
478 if(nsearchvisible == nsearchresults)
479 /* We're done */
480 return;
481 /* Are any of the search tracks at/below this point? */
c3df9503
RK
482 if(!(cn == root || hash_find(searchhash, cn->path)))
483 return;
6a06a735
RK
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. */
c3df9503 491 for(n = 0; n < cn->children.nvec && gets_in_flight < 10; ++n)
6a06a735
RK
492 expand_from(cn->children.vec[n]);
493 else {
494 /* This node is not expanded yet */
495 expand_node(cn, 1);
496 }
c3df9503 497 } else {
6a06a735
RK
498 /* This is an actual search result */
499 ++nsearchvisible;
c3df9503
RK
500 progress_window_progress(spw, nsearchvisible, nsearchresults);
501 }
6a06a735
RK
502}
503
504/** @brief Contract all contingently expanded nodes below @p cn */
505static 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
b9bdafc7 515/** @brief Contract a node */
460b9539 516static 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;
6a06a735 521 cn->flags &= ~(CN_EXPANDED|CN_CONTINGENT);
460b9539 522 /* Clear selection below this node */
523 clear_selection(cn);
e9b70a84 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);
460b9539 529 /* We can contract a node immediately. */
530 redisplay_tree();
531}
532
b9bdafc7 533/** @brief qsort() callback for ordering choosenodes */
460b9539 534static 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
b9bdafc7 543/** @brief Called when an expandable node is updated. */
460b9539 544static 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
6a06a735
RK
559/** @brief Return true if @p track is a search result */
560static int is_search_result(const char *track) {
561 return searchhash && hash_find(searchhash, track);
460b9539 562}
563
b9bdafc7
RK
564/** @brief Called with a list of search results
565 *
566 * This is called from eclient with a (possibly empty) list of search results,
460b9539 567 * and also from initiate_seatch with an always empty list to indicate that
568 * we're not searching for anything in particular. */
569static void search_completed(void attribute((unused)) *v,
570 int nvec, char **vec) {
460b9539 571 int n;
c3df9503 572 char *s;
460b9539 573
574 search_in_flight = 0;
6a06a735
RK
575 /* Contract any choosenodes that were only expanded to show search
576 * results */
577 contract_contingent(root);
460b9539 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 {
6a06a735
RK
584 /* Stash the search results */
585 searchresults = vec;
586 nsearchresults = nvec;
460b9539 587 if(nvec) {
6a06a735
RK
588 /* Create a new search hash for fast identification of results */
589 searchhash = hash_new(1);
c3df9503
RK
590 for(n = 0; n < nvec; ++n) {
591 /* The filename itself lives in the hash */
6a06a735 592 hash_add(searchhash, vec[n], "", HASH_INSERT_OR_REPLACE);
c3df9503
RK
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 }
6a06a735
RK
602 /* We don't yet know that the results are visible */
603 nsearchvisible = 0;
c3df9503
RK
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");
6a06a735
RK
610 /* Initiate expansion */
611 expand_from(root);
612 } else {
613 searchhash = 0; /* for the gc */
460b9539 614 }
615 }
616}
617
b9bdafc7
RK
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 */
460b9539 623static 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
b9bdafc7 655/** @brief Called when the cancel search button is clicked */
460b9539 656static 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
b9bdafc7 663/** @brief Delete all the widgets in the tree */
460b9539 664static void delete_widgets(struct choosenode *cn) {
665 int n;
666
e9b70a84 667 delete_cn_widgets(cn);
460b9539 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
b9bdafc7 674/** @brief Update the display */
460b9539 675static 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 */
e1d4a32b 684 MTAG_PUSH("display_tree");
460b9539 685 d = display_tree(root, 0, 0);
e1d4a32b 686 MTAG_POP();
460b9539 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
b9bdafc7
RK
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 */
460b9539 709static 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;
6a06a735 714 const char *name;
460b9539 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) {
521b8841 724 MTAG_PUSH("make_widgets_1");
460b9539 725 /* Widgets need to be created */
e9b70a84 726 NW(hbox);
460b9539 727 cn->hbox = gtk_hbox_new(FALSE, 1);
728 if(cn->flags & CN_EXPANDABLE) {
e9b70a84 729 NW(arrow);
460b9539 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;
e9b70a84 736 if((pb = find_image("notes.png"))) {
737 NW(image);
460b9539 738 cn->marker = gtk_image_new_from_pixbuf(pb);
e9b70a84 739 }
460b9539 740 }
521b8841 741 MTAG_POP();
742 MTAG_PUSH("make_widgets_2");
e9b70a84 743 NW(label);
460b9539 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);
521b8841 750 MTAG_POP();
751 MTAG_PUSH("make_widgets_3");
e9b70a84 752 NW(event_box);
460b9539 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);
460b9539 760 /* Show everything by default */
761 gtk_widget_show_all(cn->container);
e1d4a32b 762 MTAG_POP();
460b9539 763 }
764 assert(cn->container);
6a06a735
RK
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);
460b9539 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;
521b8841 786 /* Now chooselayout has a ref to the container */
787 g_object_unref(cn->container);
460b9539 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
b9bdafc7 822/** @brief Remove widgets for newly hidden nodes */
460b9539 823static 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
b9bdafc7 839/** @brief Mark the widget @p cn according to its selection state */
460b9539 840static 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
b9bdafc7
RK
848/** @brief Set the selection state of a widget
849 *
850 * Directories can never be selected, we just ignore attempts to do so. */
460b9539 851static 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
b9bdafc7 868/** @brief Recursively clear all selection bits from CN down */
460b9539 869static 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
b9bdafc7
RK
879/** @brief Clicked on something
880 *
881 * This implements playing, all the modifiers for selection, etc.
882 */
460b9539 883static 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
6a06a735 898 expand_node(cn, 0/*!contingent*/);
460b9539 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 }
b9bdafc7
RK
941 /* TODO trying to select a range that doesn't share a single parent
942 * currently does not work, but it ought to. */
460b9539 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) {
16e145a5
RK
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);
460b9539 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 */
16e145a5 971 for(n = 0; menuitems[n].name; ++n) {
460b9539 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
b9bdafc7 987/** @brief Called BY GTK+ to tell us the search entry box has changed */
460b9539 988static void searchentry_changed(GtkEditable attribute((unused)) *editable,
989 gpointer attribute((unused)) user_data) {
990 initiate_search();
991}
992
16e145a5 993/* Track menu items -------------------------------------------------------- */
460b9539 994
b9bdafc7 995/** @brief Recursive step for gather_selected() */
460b9539 996static 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
b9bdafc7 1009/*** @brief Get a list of all the selected tracks */
16e145a5 1010static const char **gather_selected(int *ntracks) {
460b9539 1011 struct vector v;
1012
1013 vector_init(&v);
1014 recurse_selected(root, &v);
1015 vector_terminate(&v);
1016 if(ntracks) *ntracks = v.nvec;
16e145a5 1017 return (const char **)v.vec;
460b9539 1018}
1019
16e145a5
RK
1020/** @brief Called when the track menu's play option is activated */
1021static void activate_track_play(GtkMenuItem attribute((unused)) *menuitem,
1022 gpointer attribute((unused)) user_data) {
1023 const char **tracks = gather_selected(0);
460b9539 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
b9bdafc7 1031/** @brief Called when the menu's properties option is activated */
16e145a5
RK
1032static void activate_track_properties(GtkMenuItem attribute((unused)) *menuitem,
1033 gpointer attribute((unused)) user_data) {
460b9539 1034 int ntracks;
16e145a5 1035 const char **tracks = gather_selected(&ntracks);
460b9539 1036
1037 properties(ntracks, tracks);
1038}
1039
b9bdafc7 1040/** @brief Determine whether the menu's play option should be sensitive */
16e145a5 1041static gboolean sensitive_track_play(struct choosenode attribute((unused)) *cn) {
8f763f1b
RK
1042 return (!!files_selected
1043 && (disorder_eclient_state(client) & DISORDER_CONNECTED));
460b9539 1044}
1045
b9bdafc7 1046/** @brief Determine whether the menu's properties option should be sensitive */
16e145a5 1047static gboolean sensitive_track_properties(struct choosenode attribute((unused)) *cn) {
8f763f1b 1048 return !!files_selected && (disorder_eclient_state(client) & DISORDER_CONNECTED);
460b9539 1049}
1050
16e145a5
RK
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 */
1057static 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
1069static 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
1079static 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
1087static 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 */
1097static 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;
6a06a735 1110 expand_node(cn, 0/*not contingnet upon search*/);
16e145a5
RK
1111 }
1112}
1113
1114/** @brief Called when the directory menu's play option is activated */
1115static 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 */
1123static 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 */
1131static 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 */
1139static 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 */
1144static 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 */
1149static gboolean sensitive_dir_select(struct choosenode attribute((unused)) *cn) {
1150 return TRUE;
1151}
1152
1153
1154
460b9539 1155/* Main menu plumbing ------------------------------------------------------ */
1156
b9bdafc7 1157/** @brief Determine whether the edit menu's properties option should be sensitive */
460b9539 1158static int choose_properties_sensitive(GtkWidget attribute((unused)) *w) {
8f763f1b 1159 return !!files_selected && (disorder_eclient_state(client) & DISORDER_CONNECTED);
460b9539 1160}
1161
b9bdafc7
RK
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 */
460b9539 1166static int choose_selectall_sensitive(GtkWidget attribute((unused)) *w) {
b9bdafc7 1167 return FALSE;
460b9539 1168}
1169
b9bdafc7 1170/** @brief Called when the edit menu's properties option is activated */
460b9539 1171static void choose_properties_activate(GtkWidget attribute((unused)) *w) {
16e145a5 1172 activate_track_properties(0, 0);
460b9539 1173}
1174
b9bdafc7
RK
1175/** @brief Called when the edit menu's select all option is activated
1176 *
1177 * TODO not implemented, see choose_selectall_sensitive() */
460b9539 1178static void choose_selectall_activate(GtkWidget attribute((unused)) *w) {
460b9539 1179}
1180
b9bdafc7 1181/** @brief Main menu callbacks for Choose screen */
460b9539 1182static 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
b9bdafc7 1191/** @brief Create a track choice widget */
460b9539 1192GtkWidget *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 */
e9b70a84 1218 NW(entry);
460b9539 1219 searchentry = gtk_entry_new();
1220 g_signal_connect(searchentry, "changed", G_CALLBACK(searchentry_changed), 0);
a8c9507f 1221 gtk_tooltips_set_tip(tips, searchentry, "Enter search terms here; search is automatic", "");
460b9539 1222
1223 /* Cancel button to clear the search */
e9b70a84 1224 NW(button);
460b9539 1225 clearsearch = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
1226 g_signal_connect(G_OBJECT(clearsearch), "clicked",
1227 G_CALLBACK(clearsearch_clicked), 0);
a8c9507f
RK
1228 gtk_tooltips_set_tip(tips, clearsearch, "Clear search terms", "");
1229
460b9539 1230
1231 /* hbox packs the search box and the cancel button together on a line */
e9b70a84 1232 NW(hbox);
460b9539 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 */
e9b70a84 1241 NW(layout);
460b9539 1242 chooselayout = gtk_layout_new(0, 0);
1243 root = newnode(0/*parent*/, "<root>", "All files", "",
1244 CN_EXPANDABLE, fill_root_node);
6a06a735 1245 expand_node(root, 0); /* will call redisplay_tree */
16e145a5
RK
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 }
e9b70a84 1258 NW(menu);
16e145a5
RK
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) {
e9b70a84 1263 NW(menu_item);
16e145a5
RK
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);
460b9539 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 */
e9b70a84 1273 NW(vbox);
460b9539 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
b9bdafc7 1284/** @brief Called when something we care about here might have changed */
460b9539 1285void choose_update(void) {
1286 redisplay_tree();
1287}
1288
1289/*
1290Local Variables:
1291c-basic-offset:2
1292comment-column:40
1293fill-column:79
1294indent-tabs-mode:nil
1295End:
1296*/