3 * This file is part of DisOrder
4 * Copyright (C) 2006-2008 Richard Kettlewell
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.
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.
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
21 /** @file disobedience/choose.c
22 * @brief Hierarchical track selection and search
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
29 #include "disobedience.h"
32 /* Choose track ------------------------------------------------------------ */
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;
48 #define BEGIN(WHAT) do { \
49 struct timeval started##WHAT, finished##WHAT; \
50 xgettimeofday(&started##WHAT, 0)
53 xgettimeofday(&finished##WHAT, 0); \
54 times.WHAT = tvadd(times.WHAT, tvsub(finished##WHAT, started##WHAT)); \
57 #define INIT() memset(×, 0, sizeof times)
59 #define REPORT() do { \
60 fprintf(stderr, "total=%g\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))); \
90 #define BEGIN(WHAT) do {
91 #define END(WHAT) } while(0)
92 #define INIT() ((void)0)
93 #define REPORT() ((void)0)
100 /** @brief Accumulated information about the tree widget */
102 /** @brief Maximum width required */
104 /** @brief Maximum height required */
108 /* instantiate the node vector type */
110 VECTOR_TYPE(nodevector, struct choosenode *, xrealloc);
112 /** @brief Signature of function called when a choosenode is filled */
113 typedef void (when_filled_callback)(struct choosenode *cn,
116 /** @brief One node in the virtual filesystem */
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 */
124 #define CN_EXPANDABLE 0x0001 /**< @brief node is expandable */
125 #define CN_EXPANDED 0x0002 /**< @brief node is expanded
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 */
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 */
150 /** @brief One item in the popup menu */
151 struct choose_menuitem {
153 const char *name; /**< @brief name */
156 void (*activate)(GtkMenuItem *menuitem, gpointer user_data);
157 /**< @brief Called to activate the menu item.
159 * @p user_data is the choosenode the mouse pointer is over. */
161 gboolean (*sensitive)(struct choosenode *cn);
162 /* @brief Called to determine whether the menu item should be sensitive.
167 gulong handlerid; /**< @brief signal handler ID */
168 GtkWidget *w; /**< @brief menu item widget */
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 */
195 /* Forward Declarations */
197 static void clear_children(struct choosenode *cn);
198 static struct choosenode *newnode(struct choosenode *parent,
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);
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,
215 static void display_selection(struct choosenode *cn);
216 static void clear_selection(struct choosenode *cn);
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);
226 static void clicked_choosenode(GtkWidget attribute((unused)) *widget,
227 GdkEventButton *event,
230 static void activate_track_play(GtkMenuItem *menuitem, gpointer user_data);
231 static void activate_track_properties(GtkMenuItem *menuitem, gpointer user_data);
233 static gboolean sensitive_track_play(struct choosenode *cn);
234 static gboolean sensitive_track_properties(struct choosenode *cn);
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);
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);
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 },
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 },
259 /* Maintaining the data structure ------------------------------------------ */
261 static char *cnflags(const struct choosenode *cn) {
262 unsigned f = cn->flags, n;
265 static const char *bits[] = {
275 #define NBITS (sizeof bits / sizeof *bits)
279 dynstr_append(d, '0');
281 for(n = 0; n < NBITS; ++n) {
282 const unsigned bit = 1 << n;
285 dynstr_append(d, '|');
286 dynstr_append_string(d, bits[n]);
293 dynstr_append(d, '|');
294 sprintf(buf, "%#x", f);
295 dynstr_append_string(d, buf);
302 /** @brief Create a new node */
303 static struct choosenode *newnode(struct choosenode *parent,
308 void (*fill)(struct choosenode *)) {
309 struct choosenode *const n = xmalloc(sizeof *n);
311 D(("newnode %s %s", path, display));
312 if(flags & CN_EXPANDABLE)
318 n->display = display;
321 nodevector_init(&n->children);
324 nodevector_append(&parent->children, n);
328 /** @brief Called when a node has been filled
330 * Response for calling @c whenfilled.
332 static void filled(struct choosenode *cn) {
333 when_filled_callback *const whenfilled = cn->whenfilled;
337 whenfilled(cn, cn->wfu);
339 if(nsearchvisible < nsearchresults) {
340 /* There is still search expansion work to do */
341 D(("filled %s %d/%d", cn->path, nsearchvisible, nsearchresults));
344 if(gets_in_flight == 0 && nsearchvisible < nsearchresults)
348 /** @brief Fill the root */
349 static void fill_root_node(struct choosenode *cn) {
350 D(("fill_root_node"));
352 /* More de-duping possible here */
353 if(cn->flags & CN_GETTING_ANY)
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;
362 /** @brief Delete all the widgets owned by @p cn */
363 static void delete_cn_widgets(struct choosenode *cn) {
365 gtk_widget_destroy(cn->arrow);
369 gtk_widget_destroy(cn->label);
373 gtk_widget_destroy(cn->marker);
377 gtk_widget_destroy(cn->hbox);
381 gtk_widget_destroy(cn->container);
386 /** @brief Recursively clear all the children of @p cn
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.
391 static void clear_children(struct choosenode *cn) {
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]);
400 cn->children.nvec = 0;
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;
409 popup_protocol_error(0, error);
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;
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);
424 /* If there are no files and the directories are all read by now, we're
426 if(!(cn->flags & CN_GETTING_ANY))
429 redisplay_tree("got_files");
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;
437 popup_protocol_error(0, error);
439 D(("resolved %s %s %d left", cn->path, cnflags(cn), cn->pending - 1));
441 file_cn = newnode(cn, track,
442 trackname_transform("track", track, "display"),
443 trackname_transform("track", track, "sort"),
444 0/*flags*/, 0/*fill*/);
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))
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;
462 popup_protocol_error(0, error);
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.
471 * Really we want a variant of files/dirs that produces both the
472 * raw filename and the transformed name for a chosen context.
475 for(n = 0; n < nvec; ++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))
486 /** @brief Fill a child node */
487 static void fill_directory_node(struct choosenode *cn) {
488 D(("fill_directory_node %s", cn->path));
490 if(cn->flags & CN_GETTING_ANY)
492 assert(report_label != 0);
493 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
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;
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
510 cn->flags |= CN_EXPANDED;
512 cn->flags |= CN_CONTINGENT;
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 */
519 struct choosenode *cnp;
521 for(cnp = cn->parent; cnp; cnp = cnp->parent)
522 cnp->flags &= ~CN_CONTINGENT;
524 /* TODO: visual feedback */
528 /** @brief Make sure all the search results below @p cn are expanded
529 * @param cn Node to start at
531 static void expand_from(struct choosenode *cn) {
534 if(nsearchvisible == nsearchresults)
537 /* Are any of the search tracks at/below this point? */
538 if(!(cn == root || hash_find(searchhash, cn->path)))
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]);
550 /* This node is not expanded yet */
554 /* This is an actual search result */
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");
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);
569 /** @brief Contract all contingently expanded nodes below @p cn */
570 static void contract_contingent(struct choosenode *cn) {
573 if(cn->flags & CN_CONTINGENT)
576 for(n = 0; n < cn->children.nvec; ++n)
577 contract_contingent(cn->children.vec[n]);
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 */
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. */
594 /* We can contract a node immediately. */
595 redisplay_tree("contract_node");
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;
603 return compare_tracks(a->sort, b->sort,
604 a->display, b->display,
608 /** @brief Called when an expandable node is updated. */
609 static void updated_node(struct choosenode *cn, int redisplay,
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;
617 qsort(cn->children.vec, cn->children.nvec, sizeof (struct choosenode *),
622 snprintf(whywhy, sizeof whywhy, "updated_node %s", why);
623 redisplay_tree(whywhy);
627 /* Searching --------------------------------------------------------------- */
629 /** @brief Return true if @p track is a search result
631 * In particular the return value is one more than the index of the track
634 static int is_search_result(const char *track) {
637 if(searchhash && (r = hash_find(searchhash, track)))
638 return 1 + *(int *)r;
643 /** @brief Return the first search result at or below @p cn */
644 static struct choosenode *first_search_result(struct choosenode *cn) {
646 struct choosenode *r;
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])))
652 } else if(is_search_result(cn->path))
657 /** @brief Called with a list of search results
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,
664 int nvec, char **vec) {
669 popup_protocol_error(0, error);
673 search_in_flight = 0;
674 /* Contract any choosenodes that were only expanded to show search
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. */
685 /* Stash the search results */
687 nsearchresults = 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;
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) {
701 hash_add(searchhash, vec[n], &minus_1, HASH_INSERT_OR_REPLACE);
706 /* We don't yet know that the results are visible */
709 progress_window_progress(spw, 0, 0);
712 if(nsearchresults > 50)
713 spw = progress_window_new("Fetching search results");
714 /* Initiate expansion */
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 */
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);
730 /** @brief Initiate a search
732 * If a search is underway we set @ref search_obsolete and restart the search
733 * in search_completed() above.
735 static void initiate_search(void) {
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;
745 /* If a search is already underway then mark it as obsolete. We'll revisit
746 * when it returns. */
747 if(search_in_flight) {
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);
759 search_in_flight = 1;
762 /* No search terms - we want to see all tracks */
763 search_completed(0, 0, 0, 0);
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);
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
781 const gdouble bottom = gtk_adjustment_get_value(vadjust) + vadjust->page_size;
782 const struct choosenode *candidate = 0;
784 for(n = 0; n < nsearchresults; ++n) {
785 const struct choosenode *const cn = searchnodes[n];
790 || cn->ymax < candidate->ymax))
794 gtk_adjustment_clamp_page(vadjust, candidate->ymin, candidate->ymax);
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
803 const gdouble top = gtk_adjustment_get_value(vadjust);
804 const struct choosenode *candidate = 0;
806 for(n = 0; n < nsearchresults; ++n) {
807 const struct choosenode *const cn = searchnodes[n];
812 || cn->ymax > candidate->ymax))
816 gtk_adjustment_clamp_page(vadjust, candidate->ymin, candidate->ymax);
819 /* Display functions ------------------------------------------------------- */
821 /** @brief Delete all the widgets in the tree */
822 static void delete_widgets(struct choosenode *cn) {
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);
832 /** @brief Update the display */
833 static void redisplay_tree(const char *why) {
834 struct displaydata d;
835 guint oldwidth, oldheight;
837 D(("redisplay_tree %s", why));
838 if(suppress_redisplay) {
839 /*fprintf(stderr, "redisplay_tree %s suppressed\n", why);*/
843 /*fprintf(stderr, "redisplay_tree %s suppressed (gets_in_flight)\n", why);*/
848 /*fprintf(stderr, "redisplay_tree %s *** NOT SUPPRESSED ***\n", why);*/
849 /* We'll count these up empirically each time */
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);
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.)
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.
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);
872 /* Notify the main menu of any recent changes */
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
885 * Makes sure all displayed widgets from CN down exist and are in their proper
886 * place and return the maximum space used.
888 static struct displaydata display_tree(struct choosenode *cn, int x, int y) {
891 struct displaydata d, cd;
893 const int search_result = is_search_result(cn->path);
895 D(("display_tree %s %d,%d", cn->path, x, y));
897 /* An expandable item contains an arrow and a text label. When you press the
898 * button it flips its expand state.
900 * A non-expandable item has just a text label and no arrow.
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
913 if((pb = find_image("notes.png"))) {
914 cn->marker = gtk_image_new_from_pixbuf(pb);
917 cn->label = gtk_label_new(cn->display);
919 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->arrow);
920 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->label);
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);
934 assert(cn->container);
938 gtk_widget_set_style(cn->container, search_style);
939 gtk_widget_set_style(cn->label, search_style);
941 gtk_widget_set_style(cn->container, layout_style);
942 gtk_widget_set_style(cn->label, layout_style);
945 /* Make sure the icon is right */
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,
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);
956 /* Put the widget in the right place */
958 if(cn->flags & CN_DISPLAYED)
959 gtk_layout_move(GTK_LAYOUT(chooselayout), cn->container, x, y);
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);
967 /* Set the widget's selection status */
969 if(!(cn->flags & CN_EXPANDABLE))
970 display_selection(cn);
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;
978 if(cn->flags & CN_EXPANDED) {
979 /* We'll offset children by the size of the arrow whatever it might be. */
981 gtk_widget_size_request(cn->arrow, &req);
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)
987 d.height = cd.height;
991 for(n = 0; n < cn->children.nvec; ++n)
992 undisplay_tree(cn->children.vec[n]);
995 if(!(cn->flags & CN_EXPANDABLE)) {
997 if(cn->flags & CN_SELECTED)
1000 /* update the search results array */
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));
1009 /** @brief Remove widgets for newly hidden nodes */
1010 static void undisplay_tree(struct choosenode *cn) {
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;
1019 /* Remove children too */
1020 for(n = 0; n < cn->children.nvec; ++n)
1021 undisplay_tree(cn->children.vec[n]);
1024 /* Selection --------------------------------------------------------------- */
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));
1035 /** @brief Set the selection state of a widget
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;
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 */
1049 display_selection(cn);
1050 /* Update main menu sensitivity */
1055 /** @brief Recursively clear all selection bits from CN down */
1056 static void clear_selection(struct choosenode *cn) {
1059 set_selection(cn, 0);
1060 for(n = 0; n < cn->children.nvec; ++n)
1061 clear_selection(cn->children.vec[n]);
1064 /* User actions ------------------------------------------------------------ */
1066 /** @brief Called when disorder_eclient_play completes */
1067 void play_completed(void attribute((unused)) *v,
1068 const char *error) {
1070 popup_protocol_error(0, error);
1073 /** @brief Clicked on something
1075 * This implements playing, all the modifiers for selection, etc.
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;
1083 D(("clicked_choosenode %s", cn->path));
1084 if(event->type == GDK_BUTTON_RELEASE
1085 && event->button == 1) {
1087 if(cn->flags & CN_EXPANDABLE) {
1088 /* This is a directory. Flip its expansion status. */
1089 if(cn->flags & CN_EXPANDED)
1092 expand_node(cn, 0/*!contingent*/);
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)) {
1100 clear_selection(root);
1101 set_selection(cn, 1);
1104 case GDK_CONTROL_MASK:
1105 set_selection(cn, !(cn->flags & CN_SELECTED));
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)
1116 if(cn->parent->children.vec[n] == last_click)
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);
1126 for(n = last_ind; n <= ind; ++n)
1127 set_selection(cn->parent->children.vec[n], 1);
1129 for(n = ind; n <= last_ind; ++n)
1130 set_selection(cn->parent->children.vec[n], 1);
1131 if(event->state & GDK_CONTROL_MASK)
1135 /* TODO trying to select a range that doesn't share a single parent
1136 * currently does not work, but it ought to. */
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);
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);
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);
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);
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) {
1188 /* Track menu items -------------------------------------------------------- */
1190 /** @brief Recursive step for gather_selected() */
1191 static void recurse_selected(struct choosenode *cn, struct vector *v) {
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);
1199 if((cn->flags & CN_SELECTED) && cn->path)
1200 vector_append(v, (char *)cn->path);
1204 /*** @brief Get a list of all the selected tracks */
1205 static const char **gather_selected(int *ntracks) {
1209 recurse_selected(root, &v);
1210 vector_terminate(&v);
1211 if(ntracks) *ntracks = v.nvec;
1212 return (const char **)v.vec;
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);
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);
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) {
1230 const char **tracks = gather_selected(&ntracks);
1232 properties(ntracks, tracks);
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));
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));
1249 /* Directory menu items ---------------------------------------------------- */
1251 /** @brief Return the file children of @p cn
1253 * The list is terminated by a null pointer.
1255 static const char **dir_files(struct choosenode *cn, int *nfiles) {
1256 const char **files = xcalloc(cn->children.nvec + 1, sizeof (char *));
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;
1263 if(nfiles) *nfiles = m;
1267 static void play_dir(struct choosenode *cn,
1268 void attribute((unused)) *wfu) {
1270 const char **tracks = dir_files(cn, &ntracks);
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);
1277 static void properties_dir(struct choosenode *cn,
1278 void attribute((unused)) *wfu) {
1280 const char **tracks = dir_files(cn, &ntracks);
1282 properties(ntracks, tracks);
1285 static void select_dir(struct choosenode *cn,
1286 void attribute((unused)) *wfu) {
1289 clear_selection(root);
1290 for(n = 0; n < cn->children.nvec; ++n)
1291 set_selection(cn->children.vec[n], 1);
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,
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);
1304 /* @p cn is not open, arrange for the callback to go off when it is
1306 cn->whenfilled = whenfilled;
1308 expand_node(cn, 0/*not contingnet upon search*/);
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;
1317 call_with_dir(cn, play_dir, 0);
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;
1325 call_with_dir(cn, properties_dir, 0);
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;
1333 call_with_dir(cn, select_dir, 0);
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);
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);
1346 /** @brief Determine whether the directory menu's select option should be sensitive */
1347 static gboolean sensitive_dir_select(struct choosenode attribute((unused)) *cn) {
1351 /* Main menu plumbing ------------------------------------------------------ */
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);
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);
1363 /** @brief Called when the choose tab is selected */
1364 static void choose_tab_selected(void) {
1365 gtk_widget_grab_focus(searchentry);
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,
1380 /* Public entry points ----------------------------------------------------- */
1382 /** @brief Called when we have just logged in
1384 * Entirely resets the choose tab.
1386 static void choose_logged_in(const char attribute((unused)) *event,
1387 void attribute((unused)) *eventdata,
1388 void attribute((unused)) *callbackdata) {
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 */
1396 /** @brief Create a track choice widget */
1397 GtkWidget *choose_widget(void) {
1399 GtkWidget *scrolled;
1400 GtkWidget *vbox, *hbox, *clearsearch;
1403 * +--vbox-------------------------------------------------------+
1404 * | +-hbox----------------------------------------------------+ |
1405 * | | searchentry | clearsearch | |
1406 * | +---------------------------------------------------------+ |
1407 * | +-scrolled------------------------------------------------+ |
1408 * | | +-chooselayout------------------------------------++--+ | |
1409 * | | | Tree structure is manually layed out in here ||^^| | |
1414 * | | +-------------------------------------------------++--+ | |
1415 * | | +-------------------------------------------------+ | |
1417 * | | +-------------------------------------------------+ | |
1418 * | +---------------------------------------------------------+ |
1419 * +-------------------------------------------------------------+
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", "");
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", "");
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);
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*/);
1459 /* chooselayout contains the currently visible subset of the track
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),
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,
1475 dir_menu = gtk_menu_new();
1476 g_signal_connect(dir_menu, "destroy", G_CALLBACK(gtk_widget_destroyed),
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,
1484 /* The layout is scrollable */
1485 scrolled = scroll_widget(chooselayout);
1486 vadjust = gtk_layout_get_vadjustment(GTK_LAYOUT(chooselayout));
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*/);
1495 g_object_set_data(G_OBJECT(vbox), "type", (void *)&tabtype_choose);
1499 /** @brief Called when something we care about here might have changed */
1500 void choose_update(void) {
1501 redisplay_tree("choose_update");
1509 indent-tabs-mode:nil