2 * This file is part of DisOrder
3 * Copyright (C) 2006, 2007 Richard Kettlewell
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.
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.
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
20 /** @file disobedience/choose.c
21 * @brief Hierarchical track selection and search
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
28 #include "disobedience.h"
30 /* Choose track ------------------------------------------------------------ */
48 /** @brief Accumulated information about the tree widget */
50 /** @brief Maximum width required */
52 /** @brief Maximum height required */
56 /* instantiate the node vector type */
58 VECTOR_TYPE(nodevector, struct choosenode *, xrealloc);
60 /** @brief Signature of function called when a choosenode is filled */
61 typedef void (when_filled_callback)(struct choosenode *cn,
64 /** @brief One node in the virtual filesystem */
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 */
72 #define CN_EXPANDABLE 0x0001 /**< @brief node is expandable */
73 #define CN_EXPANDED 0x0002 /**< @brief node is expanded
75 * Expandable items are directories;
76 * non-expandable ones are files. */
77 #define CN_DISPLAYED 0x0004 /**< @brief widget is displayed in layout */
78 #define CN_SELECTED 0x0008 /**< @brief node is selected */
79 #define CN_GETTING_FILES 0x0010 /**< @brief files inbound */
80 #define CN_RESOLVING_FILES 0x0020 /**< @brief resolved files inbound */
81 #define CN_GETTING_DIRS 0x0040 /**< @brief directories inbound */
82 #define CN_GETTING_ANY 0x0070 /**< @brief getting something */
83 #define CN_CONTINGENT 0x0080 /**< @brief expansion contingent on search */
84 struct nodevector children; /**< @brief vector of children */
85 void (*fill)(struct choosenode *); /**< @brief request child fill or 0 for leaf */
86 GtkWidget *container; /**< @brief the container for this row */
87 GtkWidget *hbox; /**< @brief the hbox for this row */
88 GtkWidget *arrow; /**< @brief arrow widget or 0 */
89 GtkWidget *label; /**< @brief text label for this node */
90 GtkWidget *marker; /**< @brief queued marker */
92 when_filled_callback *whenfilled; /**< @brief called when filled or 0 */
93 void *wfu; /**< @brief passed to @c whenfilled */
96 /** @brief One item in the popup menu */
97 struct choose_menuitem {
99 const char *name; /**< @brief name */
102 void (*activate)(GtkMenuItem *menuitem, gpointer user_data);
103 /**< @brief Called to activate the menu item.
105 * @p user_data is the choosenode the mouse pointer is over. */
107 gboolean (*sensitive)(struct choosenode *cn);
108 /* @brief Called to determine whether the menu item should be sensitive.
113 gulong handlerid; /**< @brief signal handler ID */
114 GtkWidget *w; /**< @brief menu item widget */
119 static GtkWidget *chooselayout;
120 static GtkWidget *searchentry; /**< @brief search terms */
121 static struct choosenode *root;
122 static GtkWidget *track_menu; /**< @brief track popup menu */
123 static GtkWidget *dir_menu; /**< @brief directory popup menu */
124 static struct choosenode *last_click; /**< @brief last clicked node for selection */
125 static int files_visible; /**< @brief total files visible */
126 static int files_selected; /**< @brief total files selected */
127 static int gets_in_flight; /**< @brief total gets in flight */
128 static int search_in_flight; /**< @brief a search is underway */
129 static int search_obsolete; /**< @brief the current search is void */
130 static char **searchresults; /**< @brief search results */
131 static int nsearchresults; /**< @brief number of results */
132 static int nsearchvisible; /**< @brief number of search results visible */
133 static struct hash *searchhash; /**< @brief hash of search results */
134 struct progress_window *spw; /**< @brief progress window */
136 /* Forward Declarations */
138 static void clear_children(struct choosenode *cn);
139 static struct choosenode *newnode(struct choosenode *parent,
144 void (*fill)(struct choosenode *));
145 static void fill_root_node(struct choosenode *cn);
146 static void fill_directory_node(struct choosenode *cn);
147 static void got_files(void *v, int nvec, char **vec);
148 static void got_resolved_file(void *v, const char *track);
149 static void got_dirs(void *v, int nvec, char **vec);
151 static void expand_node(struct choosenode *cn, int contingent);
152 static void contract_node(struct choosenode *cn);
153 static void updated_node(struct choosenode *cn, int redisplay);
155 static void display_selection(struct choosenode *cn);
156 static void clear_selection(struct choosenode *cn);
158 static void redisplay_tree(void);
159 static struct displaydata display_tree(struct choosenode *cn, int x, int y);
160 static void undisplay_tree(struct choosenode *cn);
161 static void initiate_search(void);
162 static void delete_widgets(struct choosenode *cn);
163 static void expand_from(struct choosenode *cn);
165 static void clicked_choosenode(GtkWidget attribute((unused)) *widget,
166 GdkEventButton *event,
169 static void activate_track_play(GtkMenuItem *menuitem, gpointer user_data);
170 static void activate_track_properties(GtkMenuItem *menuitem, gpointer user_data);
172 static gboolean sensitive_track_play(struct choosenode *cn);
173 static gboolean sensitive_track_properties(struct choosenode *cn);
175 static void activate_dir_play(GtkMenuItem *menuitem, gpointer user_data);
176 static void activate_dir_properties(GtkMenuItem *menuitem, gpointer user_data);
177 static void activate_dir_select(GtkMenuItem *menuitem, gpointer user_data);
179 static gboolean sensitive_dir_play(struct choosenode *cn);
180 static gboolean sensitive_dir_properties(struct choosenode *cn);
181 static gboolean sensitive_dir_select(struct choosenode *cn);
183 /** @brief Track menu items */
184 static struct choose_menuitem track_menuitems[] = {
185 { "Play track", activate_track_play, sensitive_track_play, 0, 0 },
186 { "Track properties", activate_track_properties, sensitive_track_properties, 0, 0 },
190 /** @brief Directory menu items */
191 static struct choose_menuitem dir_menuitems[] = {
192 { "Play all tracks", activate_dir_play, sensitive_dir_play, 0, 0 },
193 { "Track properties", activate_dir_properties, sensitive_dir_properties, 0, 0 },
194 { "Select all tracks", activate_dir_select, sensitive_dir_select, 0, 0 },
198 /* Maintaining the data structure ------------------------------------------ */
200 static char *flags(const struct choosenode *cn) {
201 unsigned f = cn->flags, n;
204 static const char *bits[] = {
214 #define NBITS (sizeof bits / sizeof *bits)
218 dynstr_append(d, '0');
220 for(n = 0; n < NBITS; ++n) {
221 const unsigned bit = 1 << n;
224 dynstr_append(d, '|');
225 dynstr_append_string(d, bits[n]);
232 dynstr_append(d, '|');
233 sprintf(buf, "%#x", f);
234 dynstr_append_string(d, buf);
241 /** @brief Create a new node */
242 static struct choosenode *newnode(struct choosenode *parent,
247 void (*fill)(struct choosenode *)) {
248 struct choosenode *const n = xmalloc(sizeof *n);
250 D(("newnode %s %s", path, display));
251 if(flags & CN_EXPANDABLE)
257 n->display = display;
260 nodevector_init(&n->children);
263 nodevector_append(&parent->children, n);
267 /** @brief Called when a node has been filled
269 * Response for calling @c whenfilled.
271 static void filled(struct choosenode *cn) {
272 when_filled_callback *const whenfilled = cn->whenfilled;
276 whenfilled(cn, cn->wfu);
278 if(nsearchvisible < nsearchresults) {
279 /* There is still search expansion work to do */
280 D(("filled %s %d/%d", cn->path, nsearchvisible, nsearchresults));
283 if(gets_in_flight == 0 && nsearchvisible < nsearchresults)
287 /** @brief Fill the root */
288 static void fill_root_node(struct choosenode *cn) {
289 struct callbackdata *cbd;
291 D(("fill_root_node"));
293 /* More de-duping possible here */
294 if(cn->flags & CN_GETTING_ANY)
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;
307 /** @brief Delete all the widgets owned by @p cn */
308 static void delete_cn_widgets(struct choosenode *cn) {
311 gtk_widget_destroy(cn->arrow);
316 gtk_widget_destroy(cn->label);
321 gtk_widget_destroy(cn->marker);
326 gtk_widget_destroy(cn->hbox);
331 gtk_widget_destroy(cn->container);
336 /** @brief Recursively clear all the children of @p cn
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.
341 static void clear_children(struct choosenode *cn) {
344 D(("clear_children %s", cn->path));
345 /* Recursively clear subtrees */
346 for(n = 0; n < cn->children.nvec; ++n) {
347 clear_children(cn->children.vec[n]);
348 delete_cn_widgets(cn->children.vec[n]);
350 cn->children.nvec = 0;
353 /** @brief Called with a list of files just below some node */
354 static void got_files(void *v, int nvec, char **vec) {
355 struct callbackdata *cbd = v;
356 struct choosenode *cn = cbd->u.choosenode;
359 D(("got_files %d files for %s %s", nvec, cn->path, flags(cn)));
360 /* Complicated by the need to resolve aliases. We can save a bit of effort
361 * by re-using cbd though. */
362 cn->flags &= ~CN_GETTING_FILES;
364 if((cn->pending = nvec)) {
365 cn->flags |= CN_RESOLVING_FILES;
366 for(n = 0; n < nvec; ++n) {
367 disorder_eclient_resolve(client, got_resolved_file, vec[n], cbd);
371 /* If there are no files and the directories are all read by now, we're
373 if(!(cn->flags & CN_GETTING_ANY))
377 /** @brief Called with an alias resolved filename */
378 static void got_resolved_file(void *v, const char *track) {
379 struct callbackdata *cbd = v;
380 struct choosenode *cn = cbd->u.choosenode, *file_cn;
382 D(("resolved %s %s %d left", cn->path, flags(cn), cn->pending - 1));
384 file_cn = newnode(cn, track,
385 trackname_transform("track", track, "display"),
386 trackname_transform("track", track, "sort"),
387 0/*flags*/, 0/*fill*/);
389 /* Only bother updating when we've got the lot */
390 if(--cn->pending == 0) {
391 cn->flags &= ~CN_RESOLVING_FILES;
393 if(!(cn->flags & CN_GETTING_ANY))
398 /** @brief Called with a list of directories just below some node */
399 static void got_dirs(void *v, int nvec, char **vec) {
400 struct callbackdata *cbd = v;
401 struct choosenode *cn = cbd->u.choosenode;
404 D(("got_dirs %d dirs for %s %s", nvec, cn->path, flags(cn)));
405 /* TODO this depends on local configuration for trackname_transform().
406 * This will work, since the defaults are now built-in, but it'll be
407 * (potentially) different to the server's configured settings.
409 * Really we want a variant of files/dirs that produces both the
410 * raw filename and the transformed name for a chosen context.
413 for(n = 0; n < nvec; ++n)
415 trackname_transform("dir", vec[n], "display"),
416 trackname_transform("dir", vec[n], "sort"),
417 CN_EXPANDABLE, fill_directory_node);
419 cn->flags &= ~CN_GETTING_DIRS;
420 if(!(cn->flags & CN_GETTING_ANY))
424 /** @brief Fill a child node */
425 static void fill_directory_node(struct choosenode *cn) {
426 struct callbackdata *cbd;
428 D(("fill_directory_node %s", cn->path));
430 if(cn->flags & CN_GETTING_ANY)
432 assert(report_label != 0);
433 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
435 cbd = xmalloc(sizeof *cbd);
436 cbd->u.choosenode = cn;
437 disorder_eclient_dirs(client, got_dirs, cn->path, 0, cbd);
438 cbd = xmalloc(sizeof *cbd);
439 cbd->u.choosenode = cn;
440 disorder_eclient_files(client, got_files, cn->path, 0, cbd);
441 cn->flags |= CN_GETTING_FILES|CN_GETTING_DIRS;
445 /** @brief Expand a node */
446 static void expand_node(struct choosenode *cn, int contingent) {
447 D(("expand_node %s %d %s", cn->path, contingent, flags(cn)));
448 assert(cn->flags & CN_EXPANDABLE);
449 /* If node is already expanded do nothing. */
450 if(cn->flags & CN_EXPANDED) return;
451 /* We mark the node as expanded and request that it fill itself. When it has
452 * completed it will called updated_node() and we can redraw at that
454 cn->flags |= CN_EXPANDED;
456 cn->flags |= CN_CONTINGENT;
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 */
463 struct choosenode *cnp;
465 for(cnp = cn->parent; cnp; cnp = cnp->parent)
466 cnp->flags &= ~CN_CONTINGENT;
468 /* TODO: visual feedback */
472 /** @brief Make sure all the search results below @p cn are expanded
473 * @param cn Node to start at
475 static void expand_from(struct choosenode *cn) {
478 if(nsearchvisible == nsearchresults)
481 /* Are any of the search tracks at/below this point? */
482 if(!(cn == root || hash_find(searchhash, cn->path)))
484 D(("expand_from %d/%d visible %s",
485 nsearchvisible, nsearchresults, cn->path));
486 if(cn->flags & CN_EXPANDABLE) {
487 if(cn->flags & CN_EXPANDED)
488 /* This node is marked as expanded already. children.nvec might be 0,
489 * indicating that expansion is still underway. We should get another
490 * callback when it is expanded. */
491 for(n = 0; n < cn->children.nvec && gets_in_flight < 10; ++n)
492 expand_from(cn->children.vec[n]);
494 /* This node is not expanded yet */
498 /* This is an actual search result */
500 progress_window_progress(spw, nsearchvisible, nsearchresults);
504 /** @brief Contract all contingently expanded nodes below @p cn */
505 static void contract_contingent(struct choosenode *cn) {
508 if(cn->flags & CN_CONTINGENT)
511 for(n = 0; n < cn->children.nvec; ++n)
512 contract_contingent(cn->children.vec[n]);
515 /** @brief Contract a node */
516 static void contract_node(struct choosenode *cn) {
517 D(("contract_node %s", cn->path));
518 assert(cn->flags & CN_EXPANDABLE);
519 /* If node is already contracted do nothing. */
520 if(!(cn->flags & CN_EXPANDED)) return;
521 cn->flags &= ~(CN_EXPANDED|CN_CONTINGENT);
522 /* Clear selection below this node */
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. */
529 /* We can contract a node immediately. */
533 /** @brief qsort() callback for ordering choosenodes */
534 static int compare_choosenode(const void *av, const void *bv) {
535 const struct choosenode *const *aa = av, *const *bb = bv;
536 const struct choosenode *a = *aa, *b = *bb;
538 return compare_tracks(a->sort, b->sort,
539 a->display, b->display,
543 /** @brief Called when an expandable node is updated. */
544 static void updated_node(struct choosenode *cn, int redisplay) {
545 D(("updated_node %s", cn->path));
546 assert(cn->flags & CN_EXPANDABLE);
547 /* It might be that the node has been de-expanded since we requested the
548 * update. In that case we ignore this notification. */
549 if(!(cn->flags & CN_EXPANDED)) return;
551 qsort(cn->children.vec, cn->children.nvec, sizeof (struct choosenode *),
557 /* Searching --------------------------------------------------------------- */
559 /** @brief Return true if @p track is a search result */
560 static int is_search_result(const char *track) {
561 return searchhash && hash_find(searchhash, track);
564 /** @brief Called with a list of search results
566 * This is called from eclient with a (possibly empty) list of search results,
567 * and also from initiate_seatch with an always empty list to indicate that
568 * we're not searching for anything in particular. */
569 static void search_completed(void attribute((unused)) *v,
570 int nvec, char **vec) {
574 search_in_flight = 0;
575 /* Contract any choosenodes that were only expanded to show search
577 contract_contingent(root);
578 if(search_obsolete) {
579 /* This search has been obsoleted by user input since it started.
580 * Therefore we throw away the result and search again. */
584 /* Stash the search results */
586 nsearchresults = nvec;
588 /* Create a new search hash for fast identification of results */
589 searchhash = hash_new(1);
590 for(n = 0; n < nvec; ++n) {
591 /* The filename itself lives in the hash */
592 hash_add(searchhash, vec[n], "", HASH_INSERT_OR_REPLACE);
593 /* So do its ancestor directories */
594 for(s = vec[n] + 1; *s; ++s) {
597 hash_add(searchhash, vec[n], "", HASH_INSERT_OR_REPLACE);
602 /* We don't yet know that the results are visible */
605 progress_window_progress(spw, 0, 0);
608 if(nsearchresults > 50)
609 spw = progress_window_new("Fetching search results");
610 /* Initiate expansion */
613 searchhash = 0; /* for the gc */
618 /** @brief Initiate a search
620 * If a search is underway we set @ref search_obsolete and restart the search
621 * in search_completed() above.
623 static void initiate_search(void) {
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;
633 /* If a search is already underway then mark it as obsolete. We'll revisit
634 * when it returns. */
635 if(search_in_flight) {
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);
647 search_in_flight = 1;
650 /* No search terms - we want to see all tracks */
651 search_completed(0, 0, 0);
655 /** @brief Called when the cancel search button is clicked */
656 static void clearsearch_clicked(GtkButton attribute((unused)) *button,
657 gpointer attribute((unused)) userdata) {
658 gtk_entry_set_text(GTK_ENTRY(searchentry), "");
661 /* Display functions ------------------------------------------------------- */
663 /** @brief Delete all the widgets in the tree */
664 static void delete_widgets(struct choosenode *cn) {
667 delete_cn_widgets(cn);
668 for(n = 0; n < cn->children.nvec; ++n)
669 delete_widgets(cn->children.vec[n]);
670 cn->flags &= ~(CN_DISPLAYED|CN_SELECTED);
674 /** @brief Update the display */
675 static void redisplay_tree(void) {
676 struct displaydata d;
677 guint oldwidth, oldheight;
679 D(("redisplay_tree"));
680 /* We'll count these up empirically each time */
683 /* Correct the layout and find out how much space it uses */
684 MTAG_PUSH("display_tree");
685 d = display_tree(root, 0, 0);
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.)
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.
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 */
704 /** @brief Recursive step for redisplay_tree()
706 * Makes sure all displayed widgets from CN down exist and are in their proper
707 * place and return the maximum space used.
709 static struct displaydata display_tree(struct choosenode *cn, int x, int y) {
712 struct displaydata d, cd;
716 D(("display_tree %s %d,%d", cn->path, x, y));
718 /* An expandable item contains an arrow and a text label. When you press the
719 * button it flips its expand state.
721 * A non-expandable item has just a text label and no arrow.
724 MTAG_PUSH("make_widgets_1");
725 /* Widgets need to be created */
727 cn->hbox = gtk_hbox_new(FALSE, 1);
728 if(cn->flags & CN_EXPANDABLE) {
730 cn->arrow = gtk_arrow_new(cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN
736 if((pb = find_image("notes.png"))) {
738 cn->marker = gtk_image_new_from_pixbuf(pb);
742 MTAG_PUSH("make_widgets_2");
744 cn->label = gtk_label_new(cn->display);
746 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->arrow);
747 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->label);
749 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->marker);
751 MTAG_PUSH("make_widgets_3");
753 cn->container = gtk_event_box_new();
754 gtk_container_add(GTK_CONTAINER(cn->container), cn->hbox);
755 g_signal_connect(cn->container, "button-release-event",
756 G_CALLBACK(clicked_choosenode), cn);
757 g_signal_connect(cn->container, "button-press-event",
758 G_CALLBACK(clicked_choosenode), cn);
759 g_object_ref(cn->container);
760 /* Show everything by default */
761 gtk_widget_show_all(cn->container);
764 assert(cn->container);
765 /* Make sure the widget name is right */
766 name = (cn->flags & CN_EXPANDABLE
768 : is_search_result(cn->path) ? "choose-search" : "choose");
769 gtk_widget_set_name(cn->label, name);
770 gtk_widget_set_name(cn->container, name);
771 /* Make sure the icon is right */
772 if(cn->flags & CN_EXPANDABLE)
773 gtk_arrow_set(GTK_ARROW(cn->arrow),
774 cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN : GTK_ARROW_RIGHT,
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);
784 gtk_layout_put(GTK_LAYOUT(chooselayout), cn->container, x, y);
785 cn->flags |= CN_DISPLAYED;
786 /* Now chooselayout has a ref to the container */
787 g_object_unref(cn->container);
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. */
799 gtk_widget_size_request(cn->arrow, &req);
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)
805 d.height = cd.height;
808 for(n = 0; n < cn->children.nvec; ++n)
809 undisplay_tree(cn->children.vec[n]);
811 if(!(cn->flags & CN_EXPANDABLE)) {
813 if(cn->flags & CN_SELECTED)
816 /* report back how much space we used */
817 D(("display_tree %s %d,%d total size %dx%d", cn->path, x, y,
822 /** @brief Remove widgets for newly hidden nodes */
823 static void undisplay_tree(struct choosenode *cn) {
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;
832 /* Remove children too */
833 for(n = 0; n < cn->children.nvec; ++n)
834 undisplay_tree(cn->children.vec[n]);
837 /* Selection --------------------------------------------------------------- */
839 /** @brief Mark the widget @p cn according to its selection state */
840 static void display_selection(struct choosenode *cn) {
841 /* Need foreground and background colors */
842 gtk_widget_set_state(cn->label, (cn->flags & CN_SELECTED
843 ? GTK_STATE_SELECTED : GTK_STATE_NORMAL));
844 gtk_widget_set_state(cn->container, (cn->flags & CN_SELECTED
845 ? GTK_STATE_SELECTED : GTK_STATE_NORMAL));
848 /** @brief Set the selection state of a widget
850 * Directories can never be selected, we just ignore attempts to do so. */
851 static void set_selection(struct choosenode *cn, int selected) {
852 unsigned f = selected ? CN_SELECTED : 0;
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 */
862 display_selection(cn);
863 /* Update main menu sensitivity */
868 /** @brief Recursively clear all selection bits from CN down */
869 static void clear_selection(struct choosenode *cn) {
872 set_selection(cn, 0);
873 for(n = 0; n < cn->children.nvec; ++n)
874 clear_selection(cn->children.vec[n]);
877 /* User actions ------------------------------------------------------------ */
879 /** @brief Clicked on something
881 * This implements playing, all the modifiers for selection, etc.
883 static void clicked_choosenode(GtkWidget attribute((unused)) *widget,
884 GdkEventButton *event,
885 gpointer user_data) {
886 struct choosenode *cn = user_data;
887 int ind, last_ind, n;
889 D(("clicked_choosenode %s", cn->path));
890 if(event->type == GDK_BUTTON_RELEASE
891 && event->button == 1) {
893 if(cn->flags & CN_EXPANDABLE) {
894 /* This is a directory. Flip its expansion status. */
895 if(cn->flags & CN_EXPANDED)
898 expand_node(cn, 0/*!contingent*/);
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)) {
906 clear_selection(root);
907 set_selection(cn, 1);
910 case GDK_CONTROL_MASK:
911 set_selection(cn, !(cn->flags & CN_SELECTED));
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 */
919 for(n = 0; n < cn->parent->children.nvec; ++n) {
920 if(cn->parent->children.vec[n] == cn)
922 if(cn->parent->children.vec[n] == last_click)
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);
932 for(n = last_ind; n <= ind; ++n)
933 set_selection(cn->parent->children.vec[n], 1);
935 for(n = ind; n <= last_ind; ++n)
936 set_selection(cn->parent->children.vec[n], 1);
937 if(event->state & GDK_CONTROL_MASK)
941 /* TODO trying to select a range that doesn't share a single parent
942 * currently does not work, but it ought to. */
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);
956 } else if(event->type == GDK_BUTTON_PRESS
957 && event->button == 3) {
958 struct choose_menuitem *const menuitems =
959 (cn->flags & CN_EXPANDABLE ? dir_menuitems : track_menuitems);
960 GtkWidget *const menu =
961 (cn->flags & CN_EXPANDABLE ? dir_menu : track_menu);
962 /* Right click. Pop up a menu. */
963 /* If the current file isn't selected, switch the selection to just that.
964 * (If we're looking at a directory then leave the selection alone.) */
965 if(!(cn->flags & CN_EXPANDABLE) && !(cn->flags & CN_SELECTED)) {
966 clear_selection(root);
967 set_selection(cn, 1);
970 /* Set the item sensitivity and callbacks */
971 for(n = 0; menuitems[n].name; ++n) {
972 if(menuitems[n].handlerid)
973 g_signal_handler_disconnect(menuitems[n].w,
974 menuitems[n].handlerid);
975 gtk_widget_set_sensitive(menuitems[n].w,
976 menuitems[n].sensitive(cn));
977 menuitems[n].handlerid = g_signal_connect
978 (menuitems[n].w, "activate", G_CALLBACK(menuitems[n].activate), cn);
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);
987 /** @brief Called BY GTK+ to tell us the search entry box has changed */
988 static void searchentry_changed(GtkEditable attribute((unused)) *editable,
989 gpointer attribute((unused)) user_data) {
993 /* Track menu items -------------------------------------------------------- */
995 /** @brief Recursive step for gather_selected() */
996 static void recurse_selected(struct choosenode *cn, struct vector *v) {
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);
1004 if((cn->flags & CN_SELECTED) && cn->path)
1005 vector_append(v, (char *)cn->path);
1009 /*** @brief Get a list of all the selected tracks */
1010 static const char **gather_selected(int *ntracks) {
1014 recurse_selected(root, &v);
1015 vector_terminate(&v);
1016 if(ntracks) *ntracks = v.nvec;
1017 return (const char **)v.vec;
1020 /** @brief Called when the track menu's play option is activated */
1021 static void activate_track_play(GtkMenuItem attribute((unused)) *menuitem,
1022 gpointer attribute((unused)) user_data) {
1023 const char **tracks = gather_selected(0);
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);
1031 /** @brief Called when the menu's properties option is activated */
1032 static void activate_track_properties(GtkMenuItem attribute((unused)) *menuitem,
1033 gpointer attribute((unused)) user_data) {
1035 const char **tracks = gather_selected(&ntracks);
1037 properties(ntracks, tracks);
1040 /** @brief Determine whether the menu's play option should be sensitive */
1041 static gboolean sensitive_track_play(struct choosenode attribute((unused)) *cn) {
1042 return (!!files_selected
1043 && (disorder_eclient_state(client) & DISORDER_CONNECTED));
1046 /** @brief Determine whether the menu's properties option should be sensitive */
1047 static gboolean sensitive_track_properties(struct choosenode attribute((unused)) *cn) {
1048 return !!files_selected && (disorder_eclient_state(client) & DISORDER_CONNECTED);
1051 /* Directory menu items ---------------------------------------------------- */
1053 /** @brief Return the file children of @p cn
1055 * The list is terminated by a null pointer.
1057 static const char **dir_files(struct choosenode *cn, int *nfiles) {
1058 const char **files = xcalloc(cn->children.nvec + 1, sizeof (char *));
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;
1065 if(nfiles) *nfiles = m;
1069 static void play_dir(struct choosenode *cn,
1070 void attribute((unused)) *wfu) {
1072 const char **tracks = dir_files(cn, &ntracks);
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);
1079 static void properties_dir(struct choosenode *cn,
1080 void attribute((unused)) *wfu) {
1082 const char **tracks = dir_files(cn, &ntracks);
1084 properties(ntracks, tracks);
1087 static void select_dir(struct choosenode *cn,
1088 void attribute((unused)) *wfu) {
1091 clear_selection(root);
1092 for(n = 0; n < cn->children.nvec; ++n)
1093 set_selection(cn->children.vec[n], 1);
1096 /** @brief Ensure @p cn is expanded and then call @p callback */
1097 static void call_with_dir(struct choosenode *cn,
1098 when_filled_callback *whenfilled,
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);
1106 /* @p cn is not open, arrange for the callback to go off when it is
1108 cn->whenfilled = whenfilled;
1110 expand_node(cn, 0/*not contingnet upon search*/);
1114 /** @brief Called when the directory menu's play option is activated */
1115 static void activate_dir_play(GtkMenuItem attribute((unused)) *menuitem,
1116 gpointer user_data) {
1117 struct choosenode *const cn = (struct choosenode *)user_data;
1119 call_with_dir(cn, play_dir, 0);
1122 /** @brief Called when the directory menu's properties option is activated */
1123 static void activate_dir_properties(GtkMenuItem attribute((unused)) *menuitem,
1124 gpointer user_data) {
1125 struct choosenode *const cn = (struct choosenode *)user_data;
1127 call_with_dir(cn, properties_dir, 0);
1130 /** @brief Called when the directory menu's select option is activated */
1131 static void activate_dir_select(GtkMenuItem attribute((unused)) *menuitem,
1132 gpointer user_data) {
1133 struct choosenode *const cn = (struct choosenode *)user_data;
1135 call_with_dir(cn, select_dir, 0);
1138 /** @brief Determine whether the directory menu's play option should be sensitive */
1139 static gboolean sensitive_dir_play(struct choosenode attribute((unused)) *cn) {
1140 return !!(disorder_eclient_state(client) & DISORDER_CONNECTED);
1143 /** @brief Determine whether the directory menu's properties option should be sensitive */
1144 static gboolean sensitive_dir_properties(struct choosenode attribute((unused)) *cn) {
1145 return !!(disorder_eclient_state(client) & DISORDER_CONNECTED);
1148 /** @brief Determine whether the directory menu's select option should be sensitive */
1149 static gboolean sensitive_dir_select(struct choosenode attribute((unused)) *cn) {
1155 /* Main menu plumbing ------------------------------------------------------ */
1157 /** @brief Determine whether the edit menu's properties option should be sensitive */
1158 static int choose_properties_sensitive(GtkWidget attribute((unused)) *w) {
1159 return !!files_selected && (disorder_eclient_state(client) & DISORDER_CONNECTED);
1162 /** @brief Determine whether the edit menu's select all option should be sensitive
1164 * TODO not implemented, see also choose_selectall_activate()
1166 static int choose_selectall_sensitive(GtkWidget attribute((unused)) *w) {
1170 /** @brief Called when the edit menu's properties option is activated */
1171 static void choose_properties_activate(GtkWidget attribute((unused)) *w) {
1172 activate_track_properties(0, 0);
1175 /** @brief Called when the edit menu's select all option is activated
1177 * TODO not implemented, see choose_selectall_sensitive() */
1178 static void choose_selectall_activate(GtkWidget attribute((unused)) *w) {
1181 /** @brief Main menu callbacks for Choose screen */
1182 static const struct tabtype tabtype_choose = {
1183 choose_properties_sensitive,
1184 choose_selectall_sensitive,
1185 choose_properties_activate,
1186 choose_selectall_activate,
1189 /* Public entry points ----------------------------------------------------- */
1191 /** @brief Create a track choice widget */
1192 GtkWidget *choose_widget(void) {
1194 GtkWidget *scrolled;
1195 GtkWidget *vbox, *hbox, *clearsearch;
1198 * +--vbox-------------------------------------------------------+
1199 * | +-hbox----------------------------------------------------+ |
1200 * | | searchentry | clearsearch | |
1201 * | +---------------------------------------------------------+ |
1202 * | +-scrolled------------------------------------------------+ |
1203 * | | +-chooselayout------------------------------------++--+ | |
1204 * | | | Tree structure is manually layed out in here ||^^| | |
1209 * | | +-------------------------------------------------++--+ | |
1210 * | | +-------------------------------------------------+ | |
1212 * | | +-------------------------------------------------+ | |
1213 * | +---------------------------------------------------------+ |
1214 * +-------------------------------------------------------------+
1217 /* Text entry box for search terms */
1219 searchentry = gtk_entry_new();
1220 g_signal_connect(searchentry, "changed", G_CALLBACK(searchentry_changed), 0);
1221 gtk_tooltips_set_tip(tips, searchentry, "Enter search terms here; search is automatic", "");
1223 /* Cancel button to clear the search */
1225 clearsearch = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
1226 g_signal_connect(G_OBJECT(clearsearch), "clicked",
1227 G_CALLBACK(clearsearch_clicked), 0);
1228 gtk_tooltips_set_tip(tips, clearsearch, "Clear search terms", "");
1231 /* hbox packs the search box and the cancel button together on a line */
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*/);
1239 /* chooselayout contains the currently visible subset of the track
1242 chooselayout = gtk_layout_new(0, 0);
1243 root = newnode(0/*parent*/, "<root>", "All files", "",
1244 CN_EXPANDABLE, fill_root_node);
1245 expand_node(root, 0); /* will call redisplay_tree */
1246 /* Create the popup menus */
1248 track_menu = gtk_menu_new();
1249 g_signal_connect(track_menu, "destroy", G_CALLBACK(gtk_widget_destroyed),
1251 for(n = 0; track_menuitems[n].name; ++n) {
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,
1259 dir_menu = gtk_menu_new();
1260 g_signal_connect(dir_menu, "destroy", G_CALLBACK(gtk_widget_destroyed),
1262 for(n = 0; dir_menuitems[n].name; ++n) {
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,
1269 /* The layout is scrollable */
1270 scrolled = scroll_widget(GTK_WIDGET(chooselayout), "choose");
1272 /* The scrollable layout and the search hbox go together in a vbox */
1274 vbox = gtk_vbox_new(FALSE/*homogenous*/, 1/*spacing*/);
1275 gtk_box_pack_start(GTK_BOX(vbox), hbox,
1276 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
1277 gtk_box_pack_end(GTK_BOX(vbox), scrolled,
1278 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
1280 g_object_set_data(G_OBJECT(vbox), "type", (void *)&tabtype_choose);
1284 /** @brief Called when something we care about here might have changed */
1285 void choose_update(void) {
1294 indent-tabs-mode:nil