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 search_in_flight; /**< @brief a search is underway */
128 static int search_obsolete; /**< @brief the current search is void */
129 static char **searchresults; /**< @brief search results */
130 static int nsearchresults; /**< @brief number of results */
131 static int nsearchvisible; /**< @brief number of search results visible */
132 static struct hash *searchhash; /**< @brief hash of search results */
134 /* Forward Declarations */
136 static void clear_children(struct choosenode *cn);
137 static struct choosenode *newnode(struct choosenode *parent,
142 void (*fill)(struct choosenode *));
143 static void fill_root_node(struct choosenode *cn);
144 static void fill_letter_node(struct choosenode *cn);
145 static void fill_directory_node(struct choosenode *cn);
146 static void got_files(void *v, int nvec, char **vec);
147 static void got_resolved_file(void *v, const char *track);
148 static void got_dirs(void *v, int nvec, char **vec);
150 static void expand_node(struct choosenode *cn, int contingent);
151 static void contract_node(struct choosenode *cn);
152 static void updated_node(struct choosenode *cn, int redisplay);
154 static void display_selection(struct choosenode *cn);
155 static void clear_selection(struct choosenode *cn);
157 static void redisplay_tree(void);
158 static struct displaydata display_tree(struct choosenode *cn, int x, int y);
159 static void undisplay_tree(struct choosenode *cn);
160 static void initiate_search(void);
161 static void delete_widgets(struct choosenode *cn);
162 static void expand_from(struct choosenode *cn);
164 static void clicked_choosenode(GtkWidget attribute((unused)) *widget,
165 GdkEventButton *event,
168 static void activate_track_play(GtkMenuItem *menuitem, gpointer user_data);
169 static void activate_track_properties(GtkMenuItem *menuitem, gpointer user_data);
171 static gboolean sensitive_track_play(struct choosenode *cn);
172 static gboolean sensitive_track_properties(struct choosenode *cn);
174 static void activate_dir_play(GtkMenuItem *menuitem, gpointer user_data);
175 static void activate_dir_properties(GtkMenuItem *menuitem, gpointer user_data);
176 static void activate_dir_select(GtkMenuItem *menuitem, gpointer user_data);
178 static gboolean sensitive_dir_play(struct choosenode *cn);
179 static gboolean sensitive_dir_properties(struct choosenode *cn);
180 static gboolean sensitive_dir_select(struct choosenode *cn);
182 /** @brief Track menu items */
183 static struct choose_menuitem track_menuitems[] = {
184 { "Play track", activate_track_play, sensitive_track_play, 0, 0 },
185 { "Track properties", activate_track_properties, sensitive_track_properties, 0, 0 },
189 /** @brief Directory menu items */
190 static struct choose_menuitem dir_menuitems[] = {
191 { "Play all tracks", activate_dir_play, sensitive_dir_play, 0, 0 },
192 { "Track properties", activate_dir_properties, sensitive_dir_properties, 0, 0 },
193 { "Select all tracks", activate_dir_select, sensitive_dir_select, 0, 0 },
197 /* Maintaining the data structure ------------------------------------------ */
199 static char *flags(const struct choosenode *cn) {
200 unsigned f = cn->flags, n;
203 static const char *bits[] = {
213 #define NBITS (sizeof bits / sizeof *bits)
217 dynstr_append(d, '0');
219 for(n = 0; n < NBITS; ++n) {
220 const unsigned bit = 1 << n;
223 dynstr_append(d, '|');
224 dynstr_append_string(d, bits[n]);
231 dynstr_append(d, '|');
232 sprintf(buf, "%#x", f);
233 dynstr_append_string(d, buf);
240 /** @brief Create a new node */
241 static struct choosenode *newnode(struct choosenode *parent,
246 void (*fill)(struct choosenode *)) {
247 struct choosenode *const n = xmalloc(sizeof *n);
249 D(("newnode %s %s", path, display));
250 if(flags & CN_EXPANDABLE)
256 n->display = display;
259 nodevector_init(&n->children);
262 nodevector_append(&parent->children, n);
266 /** @brief Called when a node has been filled
268 * Response for calling @c whenfilled.
270 static void filled(struct choosenode *cn) {
271 when_filled_callback *const whenfilled = cn->whenfilled;
275 whenfilled(cn, cn->wfu);
277 if(nsearchvisible < nsearchresults) {
278 /* There is still search expansion work to do */
279 D(("filled %s %d/%d", cn->path, nsearchvisible, nsearchresults));
284 /** @brief Fill the root */
285 static void fill_root_node(struct choosenode *cn) {
288 struct callbackdata *cbd;
290 D(("fill_root_node"));
293 if(!cn->children.nvec) { /* Only need to do this once */
294 for(ch = 'A'; ch <= 'Z'; ++ch) {
295 byte_xasprintf(&name, "%c", ch);
296 newnode(cn, "<letter>", name, name, CN_EXPANDABLE, fill_letter_node);
298 newnode(cn, "<letter>", "*", "~", CN_EXPANDABLE, fill_letter_node);
303 /* More de-duping possible here */
304 if(cn->flags & CN_GETTING_ANY)
306 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
307 cbd = xmalloc(sizeof *cbd);
308 cbd->u.choosenode = cn;
309 disorder_eclient_dirs(client, got_dirs, "", 0, cbd);
310 cbd = xmalloc(sizeof *cbd);
311 cbd->u.choosenode = cn;
312 disorder_eclient_files(client, got_files, "", 0, cbd);
313 cn->flags |= CN_GETTING_FILES|CN_GETTING_DIRS;
317 /** @brief Delete all the widgets owned by @p cn */
318 static void delete_cn_widgets(struct choosenode *cn) {
321 gtk_widget_destroy(cn->arrow);
326 gtk_widget_destroy(cn->label);
331 gtk_widget_destroy(cn->marker);
336 gtk_widget_destroy(cn->hbox);
341 gtk_widget_destroy(cn->container);
346 /** @brief Recursively clear all the children of @p cn
348 * All the widgets at or below @p cn are deleted. All choosenodes below
349 * it are emptied. i.e. we prune the tree at @p cn.
351 static void clear_children(struct choosenode *cn) {
354 D(("clear_children %s", cn->path));
355 /* Recursively clear subtrees */
356 for(n = 0; n < cn->children.nvec; ++n) {
357 clear_children(cn->children.vec[n]);
358 delete_cn_widgets(cn->children.vec[n]);
360 cn->children.nvec = 0;
363 /** @brief Fill a letter node */
364 static void fill_letter_node(struct choosenode *cn) {
366 struct callbackdata *cbd;
368 D(("fill_letter_node %s", cn->display));
369 if(cn->flags & CN_GETTING_ANY)
371 switch(cn->display[0]) {
373 byte_xasprintf((char **)®exp, "^(the )?%c", tolower(cn->display[0]));
376 regexp = "^(?!the [^t])t";
383 /* TODO: de-dupe against fill_directory_node */
384 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
386 cbd = xmalloc(sizeof *cbd);
387 cbd->u.choosenode = cn;
388 disorder_eclient_dirs(client, got_dirs, "", regexp, cbd);
389 cbd = xmalloc(sizeof *cbd);
390 cbd->u.choosenode = cn;
391 disorder_eclient_files(client, got_files, "", regexp, cbd);
392 cn->flags |= CN_GETTING_FILES|CN_GETTING_DIRS;
395 /** @brief Called with a list of files just below some node */
396 static void got_files(void *v, int nvec, char **vec) {
397 struct callbackdata *cbd = v;
398 struct choosenode *cn = cbd->u.choosenode;
401 D(("got_files %d files for %s %s", nvec, cn->path, flags(cn)));
402 /* Complicated by the need to resolve aliases. We can save a bit of effort
403 * by re-using cbd though. */
404 cn->flags &= ~CN_GETTING_FILES;
405 if((cn->pending = nvec)) {
406 cn->flags |= CN_RESOLVING_FILES;
407 for(n = 0; n < nvec; ++n)
408 disorder_eclient_resolve(client, got_resolved_file, vec[n], cbd);
410 /* If there are no files and the directories are all read by now, we're
412 if(!(cn->flags & CN_GETTING_ANY))
416 /** @brief Called with an alias resolved filename */
417 static void got_resolved_file(void *v, const char *track) {
418 struct callbackdata *cbd = v;
419 struct choosenode *cn = cbd->u.choosenode, *file_cn;
421 D(("resolved %s %s %d left", cn->path, flags(cn), cn->pending - 1));
423 file_cn = newnode(cn, track,
424 trackname_transform("track", track, "display"),
425 trackname_transform("track", track, "sort"),
426 0/*flags*/, 0/*fill*/);
427 /* Only bother updating when we've got the lot */
428 if(--cn->pending == 0) {
429 cn->flags &= ~CN_RESOLVING_FILES;
431 if(!(cn->flags & CN_GETTING_ANY))
436 /** @brief Called with a list of directories just below some node */
437 static void got_dirs(void *v, int nvec, char **vec) {
438 struct callbackdata *cbd = v;
439 struct choosenode *cn = cbd->u.choosenode;
442 D(("got_dirs %d dirs for %s %s", nvec, cn->path, flags(cn)));
443 /* TODO this depends on local configuration for trackname_transform().
444 * This will work, since the defaults are now built-in, but it'll be
445 * (potentially) different to the server's configured settings.
447 * Really we want a variant of files/dirs that produces both the
448 * raw filename and the transformed name for a chosen context.
450 for(n = 0; n < nvec; ++n)
452 trackname_transform("dir", vec[n], "display"),
453 trackname_transform("dir", vec[n], "sort"),
454 CN_EXPANDABLE, fill_directory_node);
456 cn->flags &= ~CN_GETTING_DIRS;
457 if(!(cn->flags & CN_GETTING_ANY))
461 /** @brief Fill a child node */
462 static void fill_directory_node(struct choosenode *cn) {
463 struct callbackdata *cbd;
465 D(("fill_directory_node %s", cn->path));
467 /* TODO: de-dupe against fill_letter_node */
468 if(cn->flags & CN_GETTING_ANY)
470 assert(report_label != 0);
471 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
473 cbd = xmalloc(sizeof *cbd);
474 cbd->u.choosenode = cn;
475 disorder_eclient_dirs(client, got_dirs, cn->path, 0, cbd);
476 cbd = xmalloc(sizeof *cbd);
477 cbd->u.choosenode = cn;
478 disorder_eclient_files(client, got_files, cn->path, 0, cbd);
479 cn->flags |= CN_GETTING_FILES|CN_GETTING_DIRS;
482 /** @brief Expand a node */
483 static void expand_node(struct choosenode *cn, int contingent) {
484 D(("expand_node %s %d %s", cn->path, contingent, flags(cn)));
485 assert(cn->flags & CN_EXPANDABLE);
486 /* If node is already expanded do nothing. */
487 if(cn->flags & CN_EXPANDED) return;
488 /* We mark the node as expanded and request that it fill itself. When it has
489 * completed it will called updated_node() and we can redraw at that
491 cn->flags |= CN_EXPANDED;
493 cn->flags |= CN_CONTINGENT;
495 cn->flags &= ~CN_CONTINGENT;
496 /* If this node is not contingently expanded, mark all its parents back to
497 * the root as not contingent either, so they won't be contracted when the
498 * search results change */
500 struct choosenode *cnp;
502 for(cnp = cn->parent; cnp; cnp = cnp->parent)
503 cnp->flags &= ~CN_CONTINGENT;
505 /* TODO: visual feedback */
509 /** @brief Make sure all the search results below @p cn are expanded
510 * @param cn Node to start at
512 static void expand_from(struct choosenode *cn) {
514 const size_t pathlen = strlen(cn->path);
516 if(nsearchvisible == nsearchresults)
519 /* Are any of the search tracks at/below this point? */
521 for(n = 0; n < nsearchresults; ++n)
522 if(strlen(searchresults[n]) >= pathlen
523 && !strncmp(cn->path, searchresults[n], pathlen)
524 && (searchresults[n][pathlen] == 0
525 || searchresults[n][pathlen] == '/'))
527 if(n >= nsearchresults)
528 /* This is neither a search result nor an ancestor directory of one */
531 D(("expand_from %d/%d visible %s",
532 nsearchvisible, nsearchresults, cn->path));
533 if(cn->flags & CN_EXPANDABLE) {
534 if(cn->flags & CN_EXPANDED)
535 /* This node is marked as expanded already. children.nvec might be 0,
536 * indicating that expansion is still underway. We should get another
537 * callback when it is expanded. */
538 for(n = 0; n < cn->children.nvec; ++n)
539 expand_from(cn->children.vec[n]);
541 /* This node is not expanded yet */
545 /* This is an actual search result */
549 /** @brief Contract all contingently expanded nodes below @p cn */
550 static void contract_contingent(struct choosenode *cn) {
553 if(cn->flags & CN_CONTINGENT)
556 for(n = 0; n < cn->children.nvec; ++n)
557 contract_contingent(cn->children.vec[n]);
560 /** @brief Contract a node */
561 static void contract_node(struct choosenode *cn) {
562 D(("contract_node %s", cn->path));
563 assert(cn->flags & CN_EXPANDABLE);
564 /* If node is already contracted do nothing. */
565 if(!(cn->flags & CN_EXPANDED)) return;
566 cn->flags &= ~(CN_EXPANDED|CN_CONTINGENT);
567 /* Clear selection below this node */
569 /* Zot children. We never used to do this but the result would be that over
570 * time you'd end up with the entire tree pulled into memory. If the server
571 * is over a slow network it will make interactivity slightly worse; if
572 * anyone complains we can make it an option. */
574 /* We can contract a node immediately. */
578 /** @brief qsort() callback for ordering choosenodes */
579 static int compare_choosenode(const void *av, const void *bv) {
580 const struct choosenode *const *aa = av, *const *bb = bv;
581 const struct choosenode *a = *aa, *b = *bb;
583 return compare_tracks(a->sort, b->sort,
584 a->display, b->display,
588 /** @brief Called when an expandable node is updated. */
589 static void updated_node(struct choosenode *cn, int redisplay) {
590 D(("updated_node %s", cn->path));
591 assert(cn->flags & CN_EXPANDABLE);
592 /* It might be that the node has been de-expanded since we requested the
593 * update. In that case we ignore this notification. */
594 if(!(cn->flags & CN_EXPANDED)) return;
596 qsort(cn->children.vec, cn->children.nvec, sizeof (struct choosenode *),
602 /* Searching --------------------------------------------------------------- */
604 /** @brief Return true if @p track is a search result */
605 static int is_search_result(const char *track) {
606 return searchhash && hash_find(searchhash, track);
609 /** @brief Called with a list of search results
611 * This is called from eclient with a (possibly empty) list of search results,
612 * and also from initiate_seatch with an always empty list to indicate that
613 * we're not searching for anything in particular. */
614 static void search_completed(void attribute((unused)) *v,
615 int nvec, char **vec) {
618 search_in_flight = 0;
619 /* Contract any choosenodes that were only expanded to show search
621 contract_contingent(root);
622 if(search_obsolete) {
623 /* This search has been obsoleted by user input since it started.
624 * Therefore we throw away the result and search again. */
628 /* Stash the search results */
630 nsearchresults = nvec;
632 /* Create a new search hash for fast identification of results */
633 searchhash = hash_new(1);
634 for(n = 0; n < nvec; ++n)
635 hash_add(searchhash, vec[n], "", HASH_INSERT_OR_REPLACE);
636 /* We don't yet know that the results are visible */
638 /* Initiate expansion */
641 searchhash = 0; /* for the gc */
646 /** @brief Initiate a search
648 * If a search is underway we set @ref search_obsolete and restart the search
649 * in search_completed() above.
651 static void initiate_search(void) {
654 /* Find out what the user is after */
655 terms = xstrdup(gtk_entry_get_text(GTK_ENTRY(searchentry)));
656 /* Strip leading and trailing space */
657 while(*terms == ' ') ++terms;
658 e = terms + strlen(terms);
659 while(e > terms && e[-1] == ' ') --e;
661 /* If a search is already underway then mark it as obsolete. We'll revisit
662 * when it returns. */
663 if(search_in_flight) {
668 /* There's still something left. Initiate the search. */
669 if(disorder_eclient_search(client, search_completed, terms, 0)) {
670 /* The search terms are bad! We treat this as if there were no search
671 * terms at all. Some kind of feedback would be handy. */
672 fprintf(stderr, "bad terms [%s]\n", terms); /* TODO */
673 search_completed(0, 0, 0);
675 search_in_flight = 1;
678 /* No search terms - we want to see all tracks */
679 search_completed(0, 0, 0);
683 /** @brief Called when the cancel search button is clicked */
684 static void clearsearch_clicked(GtkButton attribute((unused)) *button,
685 gpointer attribute((unused)) userdata) {
686 gtk_entry_set_text(GTK_ENTRY(searchentry), "");
689 /* Display functions ------------------------------------------------------- */
691 /** @brief Delete all the widgets in the tree */
692 static void delete_widgets(struct choosenode *cn) {
695 delete_cn_widgets(cn);
696 for(n = 0; n < cn->children.nvec; ++n)
697 delete_widgets(cn->children.vec[n]);
698 cn->flags &= ~(CN_DISPLAYED|CN_SELECTED);
702 /** @brief Update the display */
703 static void redisplay_tree(void) {
704 struct displaydata d;
705 guint oldwidth, oldheight;
707 D(("redisplay_tree"));
708 /* We'll count these up empirically each time */
711 /* Correct the layout and find out how much space it uses */
712 MTAG_PUSH("display_tree");
713 d = display_tree(root, 0, 0);
715 /* We must set the total size or scrolling will not work (it wouldn't be hard
716 * for GtkLayout to figure it out for itself but presumably you're supposed
717 * to be able to have widgets off the edge of the layuot.)
719 * There is a problem: if we shrink the size then the part of the screen that
720 * is outside the new size but inside the old one is not updated. I think
721 * this is arguably bug in GTK+ but it's easy to force a redraw if this
722 * region is nonempty.
724 gtk_layout_get_size(GTK_LAYOUT(chooselayout), &oldwidth, &oldheight);
725 if(oldwidth > d.width || oldheight > d.height)
726 gtk_widget_queue_draw(chooselayout);
727 gtk_layout_set_size(GTK_LAYOUT(chooselayout), d.width, d.height);
728 /* Notify the main menu of any recent changes */
732 /** @brief Recursive step for redisplay_tree()
734 * Makes sure all displayed widgets from CN down exist and are in their proper
735 * place and return the maximum space used.
737 static struct displaydata display_tree(struct choosenode *cn, int x, int y) {
740 struct displaydata d, cd;
744 D(("display_tree %s %d,%d", cn->path, x, y));
746 /* An expandable item contains an arrow and a text label. When you press the
747 * button it flips its expand state.
749 * A non-expandable item has just a text label and no arrow.
752 MTAG_PUSH("make_widgets_1");
753 /* Widgets need to be created */
755 cn->hbox = gtk_hbox_new(FALSE, 1);
756 if(cn->flags & CN_EXPANDABLE) {
758 cn->arrow = gtk_arrow_new(cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN
764 if((pb = find_image("notes.png"))) {
766 cn->marker = gtk_image_new_from_pixbuf(pb);
770 MTAG_PUSH("make_widgets_2");
772 cn->label = gtk_label_new(cn->display);
774 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->arrow);
775 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->label);
777 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->marker);
779 MTAG_PUSH("make_widgets_3");
781 cn->container = gtk_event_box_new();
782 gtk_container_add(GTK_CONTAINER(cn->container), cn->hbox);
783 g_signal_connect(cn->container, "button-release-event",
784 G_CALLBACK(clicked_choosenode), cn);
785 g_signal_connect(cn->container, "button-press-event",
786 G_CALLBACK(clicked_choosenode), cn);
787 g_object_ref(cn->container);
788 /* Show everything by default */
789 gtk_widget_show_all(cn->container);
792 assert(cn->container);
793 /* Make sure the widget name is right */
794 name = (cn->flags & CN_EXPANDABLE
796 : is_search_result(cn->path) ? "choose-search" : "choose");
797 gtk_widget_set_name(cn->label, name);
798 gtk_widget_set_name(cn->container, name);
799 /* Make sure the icon is right */
800 if(cn->flags & CN_EXPANDABLE)
801 gtk_arrow_set(GTK_ARROW(cn->arrow),
802 cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN : GTK_ARROW_RIGHT,
805 /* Make sure the queued marker is right */
806 /* TODO: doesn't always work */
807 (queued(cn->path) ? gtk_widget_show : gtk_widget_hide)(cn->marker);
808 /* Put the widget in the right place */
809 if(cn->flags & CN_DISPLAYED)
810 gtk_layout_move(GTK_LAYOUT(chooselayout), cn->container, x, y);
812 gtk_layout_put(GTK_LAYOUT(chooselayout), cn->container, x, y);
813 cn->flags |= CN_DISPLAYED;
814 /* Now chooselayout has a ref to the container */
815 g_object_unref(cn->container);
817 /* Set the widget's selection status */
818 if(!(cn->flags & CN_EXPANDABLE))
819 display_selection(cn);
820 /* Find the size used so we can get vertical positioning right. */
821 gtk_widget_size_request(cn->container, &req);
822 d.width = x + req.width;
823 d.height = y + req.height;
824 if(cn->flags & CN_EXPANDED) {
825 /* We'll offset children by the size of the arrow whatever it might be. */
827 gtk_widget_size_request(cn->arrow, &req);
829 for(n = 0; n < cn->children.nvec; ++n) {
830 cd = display_tree(cn->children.vec[n], x + aw, d.height);
831 if(cd.width > d.width)
833 d.height = cd.height;
836 for(n = 0; n < cn->children.nvec; ++n)
837 undisplay_tree(cn->children.vec[n]);
839 if(!(cn->flags & CN_EXPANDABLE)) {
841 if(cn->flags & CN_SELECTED)
844 /* report back how much space we used */
845 D(("display_tree %s %d,%d total size %dx%d", cn->path, x, y,
850 /** @brief Remove widgets for newly hidden nodes */
851 static void undisplay_tree(struct choosenode *cn) {
854 D(("undisplay_tree %s", cn->path));
855 /* Remove this widget from the display */
856 if(cn->flags & CN_DISPLAYED) {
857 gtk_container_remove(GTK_CONTAINER(chooselayout), cn->container);
858 cn->flags ^= CN_DISPLAYED;
860 /* Remove children too */
861 for(n = 0; n < cn->children.nvec; ++n)
862 undisplay_tree(cn->children.vec[n]);
865 /* Selection --------------------------------------------------------------- */
867 /** @brief Mark the widget @p cn according to its selection state */
868 static void display_selection(struct choosenode *cn) {
869 /* Need foreground and background colors */
870 gtk_widget_set_state(cn->label, (cn->flags & CN_SELECTED
871 ? GTK_STATE_SELECTED : GTK_STATE_NORMAL));
872 gtk_widget_set_state(cn->container, (cn->flags & CN_SELECTED
873 ? GTK_STATE_SELECTED : GTK_STATE_NORMAL));
876 /** @brief Set the selection state of a widget
878 * Directories can never be selected, we just ignore attempts to do so. */
879 static void set_selection(struct choosenode *cn, int selected) {
880 unsigned f = selected ? CN_SELECTED : 0;
882 D(("set_selection %d %s", selected, cn->path));
883 if(!(cn->flags & CN_EXPANDABLE) && (cn->flags & CN_SELECTED) != f) {
884 cn->flags ^= CN_SELECTED;
885 /* Maintain selection count */
890 display_selection(cn);
891 /* Update main menu sensitivity */
896 /** @brief Recursively clear all selection bits from CN down */
897 static void clear_selection(struct choosenode *cn) {
900 set_selection(cn, 0);
901 for(n = 0; n < cn->children.nvec; ++n)
902 clear_selection(cn->children.vec[n]);
905 /* User actions ------------------------------------------------------------ */
907 /** @brief Clicked on something
909 * This implements playing, all the modifiers for selection, etc.
911 static void clicked_choosenode(GtkWidget attribute((unused)) *widget,
912 GdkEventButton *event,
913 gpointer user_data) {
914 struct choosenode *cn = user_data;
915 int ind, last_ind, n;
917 D(("clicked_choosenode %s", cn->path));
918 if(event->type == GDK_BUTTON_RELEASE
919 && event->button == 1) {
921 if(cn->flags & CN_EXPANDABLE) {
922 /* This is a directory. Flip its expansion status. */
923 if(cn->flags & CN_EXPANDED)
926 expand_node(cn, 0/*!contingent*/);
929 /* This is a file. Adjust selection status */
930 /* TODO the basic logic here is essentially the same as that in queue.c.
931 * Can we share code at all? */
932 switch(event->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) {
934 clear_selection(root);
935 set_selection(cn, 1);
938 case GDK_CONTROL_MASK:
939 set_selection(cn, !(cn->flags & CN_SELECTED));
943 case GDK_SHIFT_MASK|GDK_CONTROL_MASK:
944 if(last_click && last_click->parent == cn->parent) {
945 /* Figure out where the current and last clicks are in the list */
947 for(n = 0; n < cn->parent->children.nvec; ++n) {
948 if(cn->parent->children.vec[n] == cn)
950 if(cn->parent->children.vec[n] == last_click)
953 /* Test shouldn't ever fail, but still */
954 if(ind >= 0 && last_ind >= 0) {
955 if(!(event->state & GDK_CONTROL_MASK)) {
956 for(n = 0; n < cn->parent->children.nvec; ++n)
957 set_selection(cn->parent->children.vec[n], 0);
960 for(n = last_ind; n <= ind; ++n)
961 set_selection(cn->parent->children.vec[n], 1);
963 for(n = ind; n <= last_ind; ++n)
964 set_selection(cn->parent->children.vec[n], 1);
965 if(event->state & GDK_CONTROL_MASK)
969 /* TODO trying to select a range that doesn't share a single parent
970 * currently does not work, but it ought to. */
974 } else if(event->type == GDK_BUTTON_RELEASE
975 && event->button == 2) {
976 /* Middle click - play the pointed track */
977 if(!(cn->flags & CN_EXPANDABLE)) {
978 clear_selection(root);
979 set_selection(cn, 1);
980 gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
981 disorder_eclient_play(client, cn->path, 0, 0);
984 } else if(event->type == GDK_BUTTON_PRESS
985 && event->button == 3) {
986 struct choose_menuitem *const menuitems =
987 (cn->flags & CN_EXPANDABLE ? dir_menuitems : track_menuitems);
988 GtkWidget *const menu =
989 (cn->flags & CN_EXPANDABLE ? dir_menu : track_menu);
990 /* Right click. Pop up a menu. */
991 /* If the current file isn't selected, switch the selection to just that.
992 * (If we're looking at a directory then leave the selection alone.) */
993 if(!(cn->flags & CN_EXPANDABLE) && !(cn->flags & CN_SELECTED)) {
994 clear_selection(root);
995 set_selection(cn, 1);
998 /* Set the item sensitivity and callbacks */
999 for(n = 0; menuitems[n].name; ++n) {
1000 if(menuitems[n].handlerid)
1001 g_signal_handler_disconnect(menuitems[n].w,
1002 menuitems[n].handlerid);
1003 gtk_widget_set_sensitive(menuitems[n].w,
1004 menuitems[n].sensitive(cn));
1005 menuitems[n].handlerid = g_signal_connect
1006 (menuitems[n].w, "activate", G_CALLBACK(menuitems[n].activate), cn);
1008 /* Pop up the menu */
1009 gtk_widget_show_all(menu);
1010 gtk_menu_popup(GTK_MENU(menu), 0, 0, 0, 0,
1011 event->button, event->time);
1015 /** @brief Called BY GTK+ to tell us the search entry box has changed */
1016 static void searchentry_changed(GtkEditable attribute((unused)) *editable,
1017 gpointer attribute((unused)) user_data) {
1021 /* Track menu items -------------------------------------------------------- */
1023 /** @brief Recursive step for gather_selected() */
1024 static void recurse_selected(struct choosenode *cn, struct vector *v) {
1027 if(cn->flags & CN_EXPANDABLE) {
1028 if(cn->flags & CN_EXPANDED)
1029 for(n = 0; n < cn->children.nvec; ++n)
1030 recurse_selected(cn->children.vec[n], v);
1032 if((cn->flags & CN_SELECTED) && cn->path)
1033 vector_append(v, (char *)cn->path);
1037 /*** @brief Get a list of all the selected tracks */
1038 static const char **gather_selected(int *ntracks) {
1042 recurse_selected(root, &v);
1043 vector_terminate(&v);
1044 if(ntracks) *ntracks = v.nvec;
1045 return (const char **)v.vec;
1048 /** @brief Called when the track menu's play option is activated */
1049 static void activate_track_play(GtkMenuItem attribute((unused)) *menuitem,
1050 gpointer attribute((unused)) user_data) {
1051 const char **tracks = gather_selected(0);
1054 gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
1055 for(n = 0; tracks[n]; ++n)
1056 disorder_eclient_play(client, tracks[n], 0, 0);
1059 /** @brief Called when the menu's properties option is activated */
1060 static void activate_track_properties(GtkMenuItem attribute((unused)) *menuitem,
1061 gpointer attribute((unused)) user_data) {
1063 const char **tracks = gather_selected(&ntracks);
1065 properties(ntracks, tracks);
1068 /** @brief Determine whether the menu's play option should be sensitive */
1069 static gboolean sensitive_track_play(struct choosenode attribute((unused)) *cn) {
1070 return (!!files_selected
1071 && (disorder_eclient_state(client) & DISORDER_CONNECTED));
1074 /** @brief Determine whether the menu's properties option should be sensitive */
1075 static gboolean sensitive_track_properties(struct choosenode attribute((unused)) *cn) {
1076 return !!files_selected && (disorder_eclient_state(client) & DISORDER_CONNECTED);
1079 /* Directory menu items ---------------------------------------------------- */
1081 /** @brief Return the file children of @p cn
1083 * The list is terminated by a null pointer.
1085 static const char **dir_files(struct choosenode *cn, int *nfiles) {
1086 const char **files = xcalloc(cn->children.nvec + 1, sizeof (char *));
1089 for(n = m = 0; n < cn->children.nvec; ++n)
1090 if(!(cn->children.vec[n]->flags & CN_EXPANDABLE))
1091 files[m++] = cn->children.vec[n]->path;
1093 if(nfiles) *nfiles = m;
1097 static void play_dir(struct choosenode *cn,
1098 void attribute((unused)) *wfu) {
1100 const char **tracks = dir_files(cn, &ntracks);
1102 gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
1103 for(n = 0; n < ntracks; ++n)
1104 disorder_eclient_play(client, tracks[n], 0, 0);
1107 static void properties_dir(struct choosenode *cn,
1108 void attribute((unused)) *wfu) {
1110 const char **tracks = dir_files(cn, &ntracks);
1112 properties(ntracks, tracks);
1115 static void select_dir(struct choosenode *cn,
1116 void attribute((unused)) *wfu) {
1119 clear_selection(root);
1120 for(n = 0; n < cn->children.nvec; ++n)
1121 set_selection(cn->children.vec[n], 1);
1124 /** @brief Ensure @p cn is expanded and then call @p callback */
1125 static void call_with_dir(struct choosenode *cn,
1126 when_filled_callback *whenfilled,
1128 if(!(cn->flags & CN_EXPANDABLE))
1129 return; /* something went wrong */
1130 if(cn->flags & CN_EXPANDED)
1131 /* @p cn is already open */
1132 whenfilled(cn, wfu);
1134 /* @p cn is not open, arrange for the callback to go off when it is
1136 cn->whenfilled = whenfilled;
1138 expand_node(cn, 0/*not contingnet upon search*/);
1142 /** @brief Called when the directory menu's play option is activated */
1143 static void activate_dir_play(GtkMenuItem attribute((unused)) *menuitem,
1144 gpointer user_data) {
1145 struct choosenode *const cn = (struct choosenode *)user_data;
1147 call_with_dir(cn, play_dir, 0);
1150 /** @brief Called when the directory menu's properties option is activated */
1151 static void activate_dir_properties(GtkMenuItem attribute((unused)) *menuitem,
1152 gpointer user_data) {
1153 struct choosenode *const cn = (struct choosenode *)user_data;
1155 call_with_dir(cn, properties_dir, 0);
1158 /** @brief Called when the directory menu's select option is activated */
1159 static void activate_dir_select(GtkMenuItem attribute((unused)) *menuitem,
1160 gpointer user_data) {
1161 struct choosenode *const cn = (struct choosenode *)user_data;
1163 call_with_dir(cn, select_dir, 0);
1166 /** @brief Determine whether the directory menu's play option should be sensitive */
1167 static gboolean sensitive_dir_play(struct choosenode attribute((unused)) *cn) {
1168 return !!(disorder_eclient_state(client) & DISORDER_CONNECTED);
1171 /** @brief Determine whether the directory menu's properties option should be sensitive */
1172 static gboolean sensitive_dir_properties(struct choosenode attribute((unused)) *cn) {
1173 return !!(disorder_eclient_state(client) & DISORDER_CONNECTED);
1176 /** @brief Determine whether the directory menu's select option should be sensitive */
1177 static gboolean sensitive_dir_select(struct choosenode attribute((unused)) *cn) {
1183 /* Main menu plumbing ------------------------------------------------------ */
1185 /** @brief Determine whether the edit menu's properties option should be sensitive */
1186 static int choose_properties_sensitive(GtkWidget attribute((unused)) *w) {
1187 return !!files_selected && (disorder_eclient_state(client) & DISORDER_CONNECTED);
1190 /** @brief Determine whether the edit menu's select all option should be sensitive
1192 * TODO not implemented, see also choose_selectall_activate()
1194 static int choose_selectall_sensitive(GtkWidget attribute((unused)) *w) {
1198 /** @brief Called when the edit menu's properties option is activated */
1199 static void choose_properties_activate(GtkWidget attribute((unused)) *w) {
1200 activate_track_properties(0, 0);
1203 /** @brief Called when the edit menu's select all option is activated
1205 * TODO not implemented, see choose_selectall_sensitive() */
1206 static void choose_selectall_activate(GtkWidget attribute((unused)) *w) {
1209 /** @brief Main menu callbacks for Choose screen */
1210 static const struct tabtype tabtype_choose = {
1211 choose_properties_sensitive,
1212 choose_selectall_sensitive,
1213 choose_properties_activate,
1214 choose_selectall_activate,
1217 /* Public entry points ----------------------------------------------------- */
1219 /** @brief Create a track choice widget */
1220 GtkWidget *choose_widget(void) {
1222 GtkWidget *scrolled;
1223 GtkWidget *vbox, *hbox, *clearsearch;
1226 * +--vbox-------------------------------------------------------+
1227 * | +-hbox----------------------------------------------------+ |
1228 * | | searchentry | clearsearch | |
1229 * | +---------------------------------------------------------+ |
1230 * | +-scrolled------------------------------------------------+ |
1231 * | | +-chooselayout------------------------------------++--+ | |
1232 * | | | Tree structure is manually layed out in here ||^^| | |
1237 * | | +-------------------------------------------------++--+ | |
1238 * | | +-------------------------------------------------+ | |
1240 * | | +-------------------------------------------------+ | |
1241 * | +---------------------------------------------------------+ |
1242 * +-------------------------------------------------------------+
1245 /* Text entry box for search terms */
1247 searchentry = gtk_entry_new();
1248 g_signal_connect(searchentry, "changed", G_CALLBACK(searchentry_changed), 0);
1249 gtk_tooltips_set_tip(tips, searchentry, "Enter search terms here; search is automatic", "");
1251 /* Cancel button to clear the search */
1253 clearsearch = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
1254 g_signal_connect(G_OBJECT(clearsearch), "clicked",
1255 G_CALLBACK(clearsearch_clicked), 0);
1256 gtk_tooltips_set_tip(tips, clearsearch, "Clear search terms", "");
1259 /* hbox packs the search box and the cancel button together on a line */
1261 hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*spacing*/);
1262 gtk_box_pack_start(GTK_BOX(hbox), searchentry,
1263 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
1264 gtk_box_pack_end(GTK_BOX(hbox), clearsearch,
1265 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
1267 /* chooselayout contains the currently visible subset of the track
1270 chooselayout = gtk_layout_new(0, 0);
1271 root = newnode(0/*parent*/, "<root>", "All files", "",
1272 CN_EXPANDABLE, fill_root_node);
1273 expand_node(root, 0); /* will call redisplay_tree */
1274 /* Create the popup menus */
1276 track_menu = gtk_menu_new();
1277 g_signal_connect(track_menu, "destroy", G_CALLBACK(gtk_widget_destroyed),
1279 for(n = 0; track_menuitems[n].name; ++n) {
1281 track_menuitems[n].w =
1282 gtk_menu_item_new_with_label(track_menuitems[n].name);
1283 gtk_menu_attach(GTK_MENU(track_menu), track_menuitems[n].w,
1287 dir_menu = gtk_menu_new();
1288 g_signal_connect(dir_menu, "destroy", G_CALLBACK(gtk_widget_destroyed),
1290 for(n = 0; dir_menuitems[n].name; ++n) {
1292 dir_menuitems[n].w =
1293 gtk_menu_item_new_with_label(dir_menuitems[n].name);
1294 gtk_menu_attach(GTK_MENU(dir_menu), dir_menuitems[n].w,
1297 /* The layout is scrollable */
1298 scrolled = scroll_widget(GTK_WIDGET(chooselayout), "choose");
1300 /* The scrollable layout and the search hbox go together in a vbox */
1302 vbox = gtk_vbox_new(FALSE/*homogenous*/, 1/*spacing*/);
1303 gtk_box_pack_start(GTK_BOX(vbox), hbox,
1304 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
1305 gtk_box_pack_end(GTK_BOX(vbox), scrolled,
1306 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
1308 g_object_set_data(G_OBJECT(vbox), "type", (void *)&tabtype_choose);
1312 /** @brief Called when something we care about here might have changed */
1313 void choose_update(void) {
1322 indent-tabs-mode:nil