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