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 struct nodevector children; /**< @brief vector of children */
84 void (*fill)(struct choosenode *); /**< @brief request child fill or 0 for leaf */
85 GtkWidget *container; /**< @brief the container for this row */
86 GtkWidget *hbox; /**< @brief the hbox for this row */
87 GtkWidget *arrow; /**< @brief arrow widget or 0 */
88 GtkWidget *label; /**< @brief text label for this node */
89 GtkWidget *marker; /**< @brief queued marker */
91 when_filled_callback *whenfilled; /**< @brief called when filled or 0 */
92 void *wfu; /**< @brief passed to @c whenfilled */
95 /** @brief One item in the popup menu */
96 struct choose_menuitem {
98 const char *name; /**< @brief name */
101 void (*activate)(GtkMenuItem *menuitem, gpointer user_data);
102 /**< @brief Called to activate the menu item.
104 * @p user_data is the choosenode the mouse pointer is over. */
106 gboolean (*sensitive)(struct choosenode *cn);
107 /* @brief Called to determine whether the menu item should be sensitive.
112 gulong handlerid; /**< @brief signal handler ID */
113 GtkWidget *w; /**< @brief menu item widget */
118 static GtkWidget *chooselayout;
119 static GtkWidget *searchentry; /**< @brief search terms */
120 static struct choosenode *root;
121 static struct choosenode *realroot;
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 */
132 /* Forward Declarations */
134 static void clear_children(struct choosenode *cn);
135 static struct choosenode *newnode(struct choosenode *parent,
140 void (*fill)(struct choosenode *));
141 static void fill_root_node(struct choosenode *cn);
142 static void fill_letter_node(struct choosenode *cn);
143 static void fill_directory_node(struct choosenode *cn);
144 static void got_files(void *v, int nvec, char **vec);
145 static void got_resolved_file(void *v, const char *track);
146 static void got_dirs(void *v, int nvec, char **vec);
148 static void expand_node(struct choosenode *cn);
149 static void contract_node(struct choosenode *cn);
150 static void updated_node(struct choosenode *cn, int redisplay);
152 static void display_selection(struct choosenode *cn);
153 static void clear_selection(struct choosenode *cn);
155 static void redisplay_tree(void);
156 static struct displaydata display_tree(struct choosenode *cn, int x, int y);
157 static void undisplay_tree(struct choosenode *cn);
158 static void initiate_search(void);
159 static void delete_widgets(struct choosenode *cn);
161 static void clicked_choosenode(GtkWidget attribute((unused)) *widget,
162 GdkEventButton *event,
165 static void activate_track_play(GtkMenuItem *menuitem, gpointer user_data);
166 static void activate_track_properties(GtkMenuItem *menuitem, gpointer user_data);
168 static gboolean sensitive_track_play(struct choosenode *cn);
169 static gboolean sensitive_track_properties(struct choosenode *cn);
171 static void activate_dir_play(GtkMenuItem *menuitem, gpointer user_data);
172 static void activate_dir_properties(GtkMenuItem *menuitem, gpointer user_data);
173 static void activate_dir_select(GtkMenuItem *menuitem, gpointer user_data);
175 static gboolean sensitive_dir_play(struct choosenode *cn);
176 static gboolean sensitive_dir_properties(struct choosenode *cn);
177 static gboolean sensitive_dir_select(struct choosenode *cn);
179 /** @brief Track menu items */
180 static struct choose_menuitem track_menuitems[] = {
181 { "Play track", activate_track_play, sensitive_track_play, 0, 0 },
182 { "Track properties", activate_track_properties, sensitive_track_properties, 0, 0 },
186 /** @brief Directory menu items */
187 static struct choose_menuitem dir_menuitems[] = {
188 { "Play all tracks", activate_dir_play, sensitive_dir_play, 0, 0 },
189 { "Track properties", activate_dir_properties, sensitive_dir_properties, 0, 0 },
190 { "Select all tracks", activate_dir_select, sensitive_dir_select, 0, 0 },
195 /* Maintaining the data structure ------------------------------------------ */
197 /** @brief Create a new node */
198 static struct choosenode *newnode(struct choosenode *parent,
203 void (*fill)(struct choosenode *)) {
204 struct choosenode *const n = xmalloc(sizeof *n);
206 D(("newnode %s %s", path, display));
207 if(flags & CN_EXPANDABLE)
213 n->display = display;
216 nodevector_init(&n->children);
219 nodevector_append(&parent->children, n);
223 /** @brief Called when a node has been filled
225 * Response for calling @c whenfilled.
227 static void filled(struct choosenode *cn) {
228 when_filled_callback *const whenfilled = cn->whenfilled;
232 whenfilled(cn, cn->wfu);
236 /** @brief Fill the root */
237 static void fill_root_node(struct choosenode *cn) {
240 struct callbackdata *cbd;
242 D(("fill_root_node"));
245 if(!cn->children.nvec) { /* Only need to do this once */
246 for(ch = 'A'; ch <= 'Z'; ++ch) {
247 byte_xasprintf(&name, "%c", ch);
248 newnode(cn, "<letter>", name, name, CN_EXPANDABLE, fill_letter_node);
250 newnode(cn, "<letter>", "*", "~", CN_EXPANDABLE, fill_letter_node);
255 /* More de-duping possible here */
256 if(cn->flags & CN_GETTING_ANY)
258 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
259 cbd = xmalloc(sizeof *cbd);
260 cbd->u.choosenode = cn;
261 disorder_eclient_dirs(client, got_dirs, "", 0, cbd);
262 cbd = xmalloc(sizeof *cbd);
263 cbd->u.choosenode = cn;
264 disorder_eclient_files(client, got_files, "", 0, cbd);
265 cn->flags |= CN_GETTING_FILES|CN_GETTING_DIRS;
269 /** @brief Delete all the widgets owned by @p cn */
270 static void delete_cn_widgets(struct choosenode *cn) {
273 gtk_widget_destroy(cn->arrow);
278 gtk_widget_destroy(cn->label);
283 gtk_widget_destroy(cn->marker);
288 gtk_widget_destroy(cn->hbox);
293 gtk_widget_destroy(cn->container);
298 /** @brief Recursively clear all the children of @p cn
300 * All the widgets at or below @p cn are deleted. All choosenodes below
301 * it are emptied. i.e. we prune the tree at @p cn.
303 static void clear_children(struct choosenode *cn) {
306 D(("clear_children %s", cn->path));
307 /* Recursively clear subtrees */
308 for(n = 0; n < cn->children.nvec; ++n) {
309 clear_children(cn->children.vec[n]);
310 delete_cn_widgets(cn->children.vec[n]);
312 cn->children.nvec = 0;
315 /** @brief Fill a letter node */
316 static void fill_letter_node(struct choosenode *cn) {
318 struct callbackdata *cbd;
320 D(("fill_letter_node %s", cn->display));
321 if(cn->flags & CN_GETTING_ANY)
323 switch(cn->display[0]) {
325 byte_xasprintf((char **)®exp, "^(the )?%c", tolower(cn->display[0]));
328 regexp = "^(?!the [^t])t";
335 /* TODO: de-dupe against fill_directory_node */
336 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
338 cbd = xmalloc(sizeof *cbd);
339 cbd->u.choosenode = cn;
340 disorder_eclient_dirs(client, got_dirs, "", regexp, cbd);
341 cbd = xmalloc(sizeof *cbd);
342 cbd->u.choosenode = cn;
343 disorder_eclient_files(client, got_files, "", regexp, cbd);
344 cn->flags |= CN_GETTING_FILES|CN_GETTING_DIRS;
347 /** @brief Called with a list of files just below some node */
348 static void got_files(void *v, int nvec, char **vec) {
349 struct callbackdata *cbd = v;
350 struct choosenode *cn = cbd->u.choosenode;
353 D(("got_files %d files for %s", nvec, cn->path));
354 /* Complicated by the need to resolve aliases. We can save a bit of effort
355 * by re-using cbd though. */
356 cn->flags &= ~CN_GETTING_FILES;
357 cn->flags |= CN_RESOLVING_FILES;
359 for(n = 0; n < nvec; ++n)
360 disorder_eclient_resolve(client, got_resolved_file, vec[n], cbd);
363 /** @brief Called with an alias resolved filename */
364 static void got_resolved_file(void *v, const char *track) {
365 struct callbackdata *cbd = v;
366 struct choosenode *cn = cbd->u.choosenode, *file_cn;
369 file_cn = newnode(cn, track,
370 trackname_transform("track", track, "display"),
371 trackname_transform("track", track, "sort"),
372 0/*flags*/, 0/*fill*/);
373 /* Only bother updating when we've got the lot */
374 if(--cn->pending == 0) {
375 cn->flags &= ~CN_RESOLVING_FILES;
377 if(!(cn->flags & CN_GETTING_ANY))
382 /** @brief Called with a list of directories just below some node */
383 static void got_dirs(void *v, int nvec, char **vec) {
384 struct callbackdata *cbd = v;
385 struct choosenode *cn = cbd->u.choosenode;
388 D(("got_dirs %d dirs for %s", nvec, cn->path));
389 /* TODO this depends on local configuration for trackname_transform().
390 * This will work, since the defaults are now built-in, but it'll be
391 * (potentially) different to the server's configured settings.
393 * Really we want a variant of files/dirs that produces both the
394 * raw filename and the transformed name for a chosen context.
396 for(n = 0; n < nvec; ++n)
398 trackname_transform("dir", vec[n], "display"),
399 trackname_transform("dir", vec[n], "sort"),
400 CN_EXPANDABLE, fill_directory_node);
402 cn->flags &= ~CN_GETTING_DIRS;
403 if(!(cn->flags & CN_GETTING_ANY))
407 /** @brief Fill a child node */
408 static void fill_directory_node(struct choosenode *cn) {
409 struct callbackdata *cbd;
411 D(("fill_directory_node %s", cn->path));
413 /* TODO: de-dupe against fill_letter_node */
414 if(cn->flags & CN_GETTING_ANY)
416 assert(report_label != 0);
417 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
419 cbd = xmalloc(sizeof *cbd);
420 cbd->u.choosenode = cn;
421 disorder_eclient_dirs(client, got_dirs, cn->path, 0, cbd);
422 cbd = xmalloc(sizeof *cbd);
423 cbd->u.choosenode = cn;
424 disorder_eclient_files(client, got_files, cn->path, 0, cbd);
425 cn->flags |= CN_GETTING_FILES|CN_GETTING_DIRS;
428 /** @brief Expand a node */
429 static void expand_node(struct choosenode *cn) {
430 D(("expand_node %s", cn->path));
431 assert(cn->flags & CN_EXPANDABLE);
432 /* If node is already expanded do nothing. */
433 if(cn->flags & CN_EXPANDED) return;
434 /* We mark the node as expanded and request that it fill itself. When it has
435 * completed it will called updated_node() and we can redraw at that
437 cn->flags |= CN_EXPANDED;
438 /* TODO: visual feedback */
442 /** @brief Contract a node */
443 static void contract_node(struct choosenode *cn) {
444 D(("contract_node %s", cn->path));
445 assert(cn->flags & CN_EXPANDABLE);
446 /* If node is already contracted do nothing. */
447 if(!(cn->flags & CN_EXPANDED)) return;
448 cn->flags &= ~CN_EXPANDED;
449 /* Clear selection below this node */
451 /* Zot children. We never used to do this but the result would be that over
452 * time you'd end up with the entire tree pulled into memory. If the server
453 * is over a slow network it will make interactivity slightly worse; if
454 * anyone complains we can make it an option. */
456 /* We can contract a node immediately. */
460 /** @brief qsort() callback for ordering choosenodes */
461 static int compare_choosenode(const void *av, const void *bv) {
462 const struct choosenode *const *aa = av, *const *bb = bv;
463 const struct choosenode *a = *aa, *b = *bb;
465 return compare_tracks(a->sort, b->sort,
466 a->display, b->display,
470 /** @brief Called when an expandable node is updated. */
471 static void updated_node(struct choosenode *cn, int redisplay) {
472 D(("updated_node %s", cn->path));
473 assert(cn->flags & CN_EXPANDABLE);
474 /* It might be that the node has been de-expanded since we requested the
475 * update. In that case we ignore this notification. */
476 if(!(cn->flags & CN_EXPANDED)) return;
478 qsort(cn->children.vec, cn->children.nvec, sizeof (struct choosenode *),
484 /* Searching --------------------------------------------------------------- */
486 /** @brief qsort() callback for ordering tracks */
487 static int compare_track_for_qsort(const void *a, const void *b) {
488 return compare_path(*(char **)a, *(char **)b);
491 /** @brief Return true iff @p file is a child of @p dir */
492 static int is_child(const char *dir, const char *file) {
493 const size_t dlen = strlen(dir);
495 return (!strncmp(file, dir, dlen)
497 && strchr(file + dlen + 1, '/') == 0);
500 /** @brief Return true iff @p file is a descendant of @p dir */
501 static int is_descendant(const char *dir, const char *file) {
502 const size_t dlen = strlen(dir);
504 return !strncmp(file, dir, dlen) && file[dlen] == '/';
507 /** @brief Called to fill a node in the search results tree */
508 static void fill_search_node(struct choosenode *cn) {
510 const size_t plen = strlen(cn->path);
512 char *dir, *last = 0;
514 D(("fill_search_node %s", cn->path));
515 /* We depend on the search results being sorted as by compare_path(). */
517 for(n = 0; n < nsearchresults; ++n) {
518 /* We only care about descendants of CN */
519 if(!is_descendant(cn->path, searchresults[n]))
521 s = strchr(searchresults[n] + plen + 1, '/');
523 /* We've identified a subdirectory of CN. */
524 dir = xstrndup(searchresults[n], s - searchresults[n]);
525 if(!last || strcmp(dir, last)) {
526 /* Not a duplicate */
529 trackname_transform("dir", dir, "display"),
530 trackname_transform("dir", dir, "sort"),
531 CN_EXPANDABLE, fill_search_node);
534 /* We've identified a file in CN */
535 newnode(cn, searchresults[n],
536 trackname_transform("track", searchresults[n], "display"),
537 trackname_transform("track", searchresults[n], "sort"),
538 0/*flags*/, 0/*fill*/);
544 /** @brief Called with a list of search results
546 * This is called from eclient with a (possibly empty) list of search results,
547 * and also from initiate_seatch with an always empty list to indicate that
548 * we're not searching for anything in particular. */
549 static void search_completed(void attribute((unused)) *v,
550 int nvec, char **vec) {
551 struct choosenode *cn;
555 search_in_flight = 0;
556 if(search_obsolete) {
557 /* This search has been obsoleted by user input since it started.
558 * Therefore we throw away the result and search again. */
563 /* We will replace the choose tree with a tree structured view of search
564 * results. First we must disabled the choose tree's widgets. */
565 delete_widgets(root);
566 /* Put the tracks into order, grouped by directory. They'll probably
567 * come back this way anyway in current versions of the server, but it's
568 * cheap not to rely on it (compared with the massive effort we expend
570 qsort(vec, nvec, sizeof(char *), compare_track_for_qsort);
572 nsearchresults = nvec;
573 cn = root = newnode(0/*parent*/, "", "Search results", "",
574 CN_EXPANDABLE|CN_EXPANDED, fill_search_node);
575 /* Construct the initial tree. We do this in a single pass and expand
576 * everything, so you can actually see your search results. */
577 for(n = 0; n < nsearchresults; ++n) {
578 /* Firstly we might need to go up a few directories to each an ancestor
580 while(!is_descendant(cn->path, searchresults[n])) {
581 /* We report the update on each node the last time we see it (With
582 * display=0, the main purpose of this is to get the order of the
583 * children right.) */
587 /* Secondly we might need to insert some new directories */
588 while(!is_child(cn->path, searchresults[n])) {
589 /* Figure out the subdirectory */
590 dir = xstrndup(searchresults[n],
591 strchr(searchresults[n] + strlen(cn->path) + 1,
592 '/') - searchresults[n]);
593 cn = newnode(cn, dir,
594 trackname_transform("dir", dir, "display"),
595 trackname_transform("dir", dir, "sort"),
596 CN_EXPANDABLE|CN_EXPANDED, fill_search_node);
598 /* Finally we can insert the track as a child of the current
600 newnode(cn, searchresults[n],
601 trackname_transform("track", searchresults[n], "display"),
602 trackname_transform("track", searchresults[n], "sort"),
603 0/*flags*/, 0/*fill*/);
606 /* Update all the nodes back up to the root */
610 /* Now it's worth displaying the tree */
612 } else if(root != realroot) {
613 delete_widgets(root);
620 /** @brief Initiate a search
622 * If a search is underway we set @ref search_obsolete and restart the search
623 * in search_completed() above.
625 static void initiate_search(void) {
628 /* Find out what the user is after */
629 terms = xstrdup(gtk_entry_get_text(GTK_ENTRY(searchentry)));
630 /* Strip leading and trailing space */
631 while(*terms == ' ') ++terms;
632 e = terms + strlen(terms);
633 while(e > terms && e[-1] == ' ') --e;
635 /* If a search is already underway then mark it as obsolete. We'll revisit
636 * when it returns. */
637 if(search_in_flight) {
642 /* There's still something left. Initiate the search. */
643 if(disorder_eclient_search(client, search_completed, terms, 0)) {
644 /* The search terms are bad! We treat this as if there were no search
645 * terms at all. Some kind of feedback would be handy. */
646 fprintf(stderr, "bad terms [%s]\n", terms); /* TODO */
647 search_completed(0, 0, 0);
649 search_in_flight = 1;
652 /* No search terms - we want to see all tracks */
653 search_completed(0, 0, 0);
657 /** @brief Called when the cancel search button is clicked */
658 static void clearsearch_clicked(GtkButton attribute((unused)) *button,
659 gpointer attribute((unused)) userdata) {
660 gtk_entry_set_text(GTK_ENTRY(searchentry), "");
663 /* Display functions ------------------------------------------------------- */
665 /** @brief Delete all the widgets in the tree */
666 static void delete_widgets(struct choosenode *cn) {
669 delete_cn_widgets(cn);
670 for(n = 0; n < cn->children.nvec; ++n)
671 delete_widgets(cn->children.vec[n]);
672 cn->flags &= ~(CN_DISPLAYED|CN_SELECTED);
676 /** @brief Update the display */
677 static void redisplay_tree(void) {
678 struct displaydata d;
679 guint oldwidth, oldheight;
681 D(("redisplay_tree"));
682 /* We'll count these up empirically each time */
685 /* Correct the layout and find out how much space it uses */
686 MTAG_PUSH("display_tree");
687 d = display_tree(root, 0, 0);
689 /* We must set the total size or scrolling will not work (it wouldn't be hard
690 * for GtkLayout to figure it out for itself but presumably you're supposed
691 * to be able to have widgets off the edge of the layuot.)
693 * There is a problem: if we shrink the size then the part of the screen that
694 * is outside the new size but inside the old one is not updated. I think
695 * this is arguably bug in GTK+ but it's easy to force a redraw if this
696 * region is nonempty.
698 gtk_layout_get_size(GTK_LAYOUT(chooselayout), &oldwidth, &oldheight);
699 if(oldwidth > d.width || oldheight > d.height)
700 gtk_widget_queue_draw(chooselayout);
701 gtk_layout_set_size(GTK_LAYOUT(chooselayout), d.width, d.height);
702 /* Notify the main menu of any recent changes */
706 /** @brief Recursive step for redisplay_tree()
708 * Makes sure all displayed widgets from CN down exist and are in their proper
709 * place and return the maximum space used.
711 static struct displaydata display_tree(struct choosenode *cn, int x, int y) {
714 struct displaydata d, cd;
717 D(("display_tree %s %d,%d", cn->path, x, y));
719 /* An expandable item contains an arrow and a text label. When you press the
720 * button it flips its expand state.
722 * A non-expandable item has just a text label and no arrow.
725 MTAG_PUSH("make_widgets_1");
726 /* Widgets need to be created */
728 cn->hbox = gtk_hbox_new(FALSE, 1);
729 if(cn->flags & CN_EXPANDABLE) {
731 cn->arrow = gtk_arrow_new(cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN
737 if((pb = find_image("notes.png"))) {
739 cn->marker = gtk_image_new_from_pixbuf(pb);
743 MTAG_PUSH("make_widgets_2");
745 cn->label = gtk_label_new(cn->display);
747 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->arrow);
748 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->label);
750 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->marker);
752 MTAG_PUSH("make_widgets_3");
754 cn->container = gtk_event_box_new();
755 gtk_container_add(GTK_CONTAINER(cn->container), cn->hbox);
756 g_signal_connect(cn->container, "button-release-event",
757 G_CALLBACK(clicked_choosenode), cn);
758 g_signal_connect(cn->container, "button-press-event",
759 G_CALLBACK(clicked_choosenode), cn);
760 g_object_ref(cn->container);
761 gtk_widget_set_name(cn->label, "choose");
762 gtk_widget_set_name(cn->container, "choose");
763 /* Show everything by default */
764 gtk_widget_show_all(cn->container);
767 assert(cn->container);
768 /* Make sure the icon is right */
769 if(cn->flags & CN_EXPANDABLE)
770 gtk_arrow_set(GTK_ARROW(cn->arrow),
771 cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN : GTK_ARROW_RIGHT,
774 /* Make sure the queued marker is right */
775 /* TODO: doesn't always work */
776 (queued(cn->path) ? gtk_widget_show : gtk_widget_hide)(cn->marker);
777 /* Put the widget in the right place */
778 if(cn->flags & CN_DISPLAYED)
779 gtk_layout_move(GTK_LAYOUT(chooselayout), cn->container, x, y);
781 gtk_layout_put(GTK_LAYOUT(chooselayout), cn->container, x, y);
782 cn->flags |= CN_DISPLAYED;
783 /* Now chooselayout has a ref to the container */
784 g_object_unref(cn->container);
786 /* Set the widget's selection status */
787 if(!(cn->flags & CN_EXPANDABLE))
788 display_selection(cn);
789 /* Find the size used so we can get vertical positioning right. */
790 gtk_widget_size_request(cn->container, &req);
791 d.width = x + req.width;
792 d.height = y + req.height;
793 if(cn->flags & CN_EXPANDED) {
794 /* We'll offset children by the size of the arrow whatever it might be. */
796 gtk_widget_size_request(cn->arrow, &req);
798 for(n = 0; n < cn->children.nvec; ++n) {
799 cd = display_tree(cn->children.vec[n], x + aw, d.height);
800 if(cd.width > d.width)
802 d.height = cd.height;
805 for(n = 0; n < cn->children.nvec; ++n)
806 undisplay_tree(cn->children.vec[n]);
808 if(!(cn->flags & CN_EXPANDABLE)) {
810 if(cn->flags & CN_SELECTED)
813 /* report back how much space we used */
814 D(("display_tree %s %d,%d total size %dx%d", cn->path, x, y,
819 /** @brief Remove widgets for newly hidden nodes */
820 static void undisplay_tree(struct choosenode *cn) {
823 D(("undisplay_tree %s", cn->path));
824 /* Remove this widget from the display */
825 if(cn->flags & CN_DISPLAYED) {
826 gtk_container_remove(GTK_CONTAINER(chooselayout), cn->container);
827 cn->flags ^= CN_DISPLAYED;
829 /* Remove children too */
830 for(n = 0; n < cn->children.nvec; ++n)
831 undisplay_tree(cn->children.vec[n]);
834 /* Selection --------------------------------------------------------------- */
836 /** @brief Mark the widget @p cn according to its selection state */
837 static void display_selection(struct choosenode *cn) {
838 /* Need foreground and background colors */
839 gtk_widget_set_state(cn->label, (cn->flags & CN_SELECTED
840 ? GTK_STATE_SELECTED : GTK_STATE_NORMAL));
841 gtk_widget_set_state(cn->container, (cn->flags & CN_SELECTED
842 ? GTK_STATE_SELECTED : GTK_STATE_NORMAL));
845 /** @brief Set the selection state of a widget
847 * Directories can never be selected, we just ignore attempts to do so. */
848 static void set_selection(struct choosenode *cn, int selected) {
849 unsigned f = selected ? CN_SELECTED : 0;
851 D(("set_selection %d %s", selected, cn->path));
852 if(!(cn->flags & CN_EXPANDABLE) && (cn->flags & CN_SELECTED) != f) {
853 cn->flags ^= CN_SELECTED;
854 /* Maintain selection count */
859 display_selection(cn);
860 /* Update main menu sensitivity */
865 /** @brief Recursively clear all selection bits from CN down */
866 static void clear_selection(struct choosenode *cn) {
869 set_selection(cn, 0);
870 for(n = 0; n < cn->children.nvec; ++n)
871 clear_selection(cn->children.vec[n]);
874 /* User actions ------------------------------------------------------------ */
876 /** @brief Clicked on something
878 * This implements playing, all the modifiers for selection, etc.
880 static void clicked_choosenode(GtkWidget attribute((unused)) *widget,
881 GdkEventButton *event,
882 gpointer user_data) {
883 struct choosenode *cn = user_data;
884 int ind, last_ind, n;
886 D(("clicked_choosenode %s", cn->path));
887 if(event->type == GDK_BUTTON_RELEASE
888 && event->button == 1) {
890 if(cn->flags & CN_EXPANDABLE) {
891 /* This is a directory. Flip its expansion status. */
892 if(cn->flags & CN_EXPANDED)
898 /* This is a file. Adjust selection status */
899 /* TODO the basic logic here is essentially the same as that in queue.c.
900 * Can we share code at all? */
901 switch(event->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) {
903 clear_selection(root);
904 set_selection(cn, 1);
907 case GDK_CONTROL_MASK:
908 set_selection(cn, !(cn->flags & CN_SELECTED));
912 case GDK_SHIFT_MASK|GDK_CONTROL_MASK:
913 if(last_click && last_click->parent == cn->parent) {
914 /* Figure out where the current and last clicks are in the list */
916 for(n = 0; n < cn->parent->children.nvec; ++n) {
917 if(cn->parent->children.vec[n] == cn)
919 if(cn->parent->children.vec[n] == last_click)
922 /* Test shouldn't ever fail, but still */
923 if(ind >= 0 && last_ind >= 0) {
924 if(!(event->state & GDK_CONTROL_MASK)) {
925 for(n = 0; n < cn->parent->children.nvec; ++n)
926 set_selection(cn->parent->children.vec[n], 0);
929 for(n = last_ind; n <= ind; ++n)
930 set_selection(cn->parent->children.vec[n], 1);
932 for(n = ind; n <= last_ind; ++n)
933 set_selection(cn->parent->children.vec[n], 1);
934 if(event->state & GDK_CONTROL_MASK)
938 /* TODO trying to select a range that doesn't share a single parent
939 * currently does not work, but it ought to. */
943 } else if(event->type == GDK_BUTTON_RELEASE
944 && event->button == 2) {
945 /* Middle click - play the pointed track */
946 if(!(cn->flags & CN_EXPANDABLE)) {
947 clear_selection(root);
948 set_selection(cn, 1);
949 gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
950 disorder_eclient_play(client, cn->path, 0, 0);
953 } else if(event->type == GDK_BUTTON_PRESS
954 && event->button == 3) {
955 struct choose_menuitem *const menuitems =
956 (cn->flags & CN_EXPANDABLE ? dir_menuitems : track_menuitems);
957 GtkWidget *const menu =
958 (cn->flags & CN_EXPANDABLE ? dir_menu : track_menu);
959 /* Right click. Pop up a menu. */
960 /* If the current file isn't selected, switch the selection to just that.
961 * (If we're looking at a directory then leave the selection alone.) */
962 if(!(cn->flags & CN_EXPANDABLE) && !(cn->flags & CN_SELECTED)) {
963 clear_selection(root);
964 set_selection(cn, 1);
967 /* Set the item sensitivity and callbacks */
968 for(n = 0; menuitems[n].name; ++n) {
969 if(menuitems[n].handlerid)
970 g_signal_handler_disconnect(menuitems[n].w,
971 menuitems[n].handlerid);
972 gtk_widget_set_sensitive(menuitems[n].w,
973 menuitems[n].sensitive(cn));
974 menuitems[n].handlerid = g_signal_connect
975 (menuitems[n].w, "activate", G_CALLBACK(menuitems[n].activate), cn);
977 /* Pop up the menu */
978 gtk_widget_show_all(menu);
979 gtk_menu_popup(GTK_MENU(menu), 0, 0, 0, 0,
980 event->button, event->time);
984 /** @brief Called BY GTK+ to tell us the search entry box has changed */
985 static void searchentry_changed(GtkEditable attribute((unused)) *editable,
986 gpointer attribute((unused)) user_data) {
990 /* Track menu items -------------------------------------------------------- */
992 /** @brief Recursive step for gather_selected() */
993 static void recurse_selected(struct choosenode *cn, struct vector *v) {
996 if(cn->flags & CN_EXPANDABLE) {
997 if(cn->flags & CN_EXPANDED)
998 for(n = 0; n < cn->children.nvec; ++n)
999 recurse_selected(cn->children.vec[n], v);
1001 if((cn->flags & CN_SELECTED) && cn->path)
1002 vector_append(v, (char *)cn->path);
1006 /*** @brief Get a list of all the selected tracks */
1007 static const char **gather_selected(int *ntracks) {
1011 recurse_selected(root, &v);
1012 vector_terminate(&v);
1013 if(ntracks) *ntracks = v.nvec;
1014 return (const char **)v.vec;
1017 /** @brief Called when the track menu's play option is activated */
1018 static void activate_track_play(GtkMenuItem attribute((unused)) *menuitem,
1019 gpointer attribute((unused)) user_data) {
1020 const char **tracks = gather_selected(0);
1023 gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
1024 for(n = 0; tracks[n]; ++n)
1025 disorder_eclient_play(client, tracks[n], 0, 0);
1028 /** @brief Called when the menu's properties option is activated */
1029 static void activate_track_properties(GtkMenuItem attribute((unused)) *menuitem,
1030 gpointer attribute((unused)) user_data) {
1032 const char **tracks = gather_selected(&ntracks);
1034 properties(ntracks, tracks);
1037 /** @brief Determine whether the menu's play option should be sensitive */
1038 static gboolean sensitive_track_play(struct choosenode attribute((unused)) *cn) {
1039 return (!!files_selected
1040 && (disorder_eclient_state(client) & DISORDER_CONNECTED));
1043 /** @brief Determine whether the menu's properties option should be sensitive */
1044 static gboolean sensitive_track_properties(struct choosenode attribute((unused)) *cn) {
1045 return !!files_selected && (disorder_eclient_state(client) & DISORDER_CONNECTED);
1048 /* Directory menu items ---------------------------------------------------- */
1050 /** @brief Return the file children of @p cn
1052 * The list is terminated by a null pointer.
1054 static const char **dir_files(struct choosenode *cn, int *nfiles) {
1055 const char **files = xcalloc(cn->children.nvec + 1, sizeof (char *));
1058 for(n = m = 0; n < cn->children.nvec; ++n)
1059 if(!(cn->children.vec[n]->flags & CN_EXPANDABLE))
1060 files[m++] = cn->children.vec[n]->path;
1062 if(nfiles) *nfiles = m;
1066 static void play_dir(struct choosenode *cn,
1067 void attribute((unused)) *wfu) {
1069 const char **tracks = dir_files(cn, &ntracks);
1071 gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
1072 for(n = 0; n < ntracks; ++n)
1073 disorder_eclient_play(client, tracks[n], 0, 0);
1076 static void properties_dir(struct choosenode *cn,
1077 void attribute((unused)) *wfu) {
1079 const char **tracks = dir_files(cn, &ntracks);
1081 properties(ntracks, tracks);
1084 static void select_dir(struct choosenode *cn,
1085 void attribute((unused)) *wfu) {
1088 clear_selection(root);
1089 for(n = 0; n < cn->children.nvec; ++n)
1090 set_selection(cn->children.vec[n], 1);
1093 /** @brief Ensure @p cn is expanded and then call @p callback */
1094 static void call_with_dir(struct choosenode *cn,
1095 when_filled_callback *whenfilled,
1097 if(!(cn->flags & CN_EXPANDABLE))
1098 return; /* something went wrong */
1099 if(cn->flags & CN_EXPANDED)
1100 /* @p cn is already open */
1101 whenfilled(cn, wfu);
1103 /* @p cn is not open, arrange for the callback to go off when it is
1105 cn->whenfilled = whenfilled;
1111 /** @brief Called when the directory menu's play option is activated */
1112 static void activate_dir_play(GtkMenuItem attribute((unused)) *menuitem,
1113 gpointer user_data) {
1114 struct choosenode *const cn = (struct choosenode *)user_data;
1116 call_with_dir(cn, play_dir, 0);
1119 /** @brief Called when the directory menu's properties option is activated */
1120 static void activate_dir_properties(GtkMenuItem attribute((unused)) *menuitem,
1121 gpointer user_data) {
1122 struct choosenode *const cn = (struct choosenode *)user_data;
1124 call_with_dir(cn, properties_dir, 0);
1127 /** @brief Called when the directory menu's select option is activated */
1128 static void activate_dir_select(GtkMenuItem attribute((unused)) *menuitem,
1129 gpointer user_data) {
1130 struct choosenode *const cn = (struct choosenode *)user_data;
1132 call_with_dir(cn, select_dir, 0);
1135 /** @brief Determine whether the directory menu's play option should be sensitive */
1136 static gboolean sensitive_dir_play(struct choosenode attribute((unused)) *cn) {
1137 return !!(disorder_eclient_state(client) & DISORDER_CONNECTED);
1140 /** @brief Determine whether the directory menu's properties option should be sensitive */
1141 static gboolean sensitive_dir_properties(struct choosenode attribute((unused)) *cn) {
1142 return !!(disorder_eclient_state(client) & DISORDER_CONNECTED);
1145 /** @brief Determine whether the directory menu's select option should be sensitive */
1146 static gboolean sensitive_dir_select(struct choosenode attribute((unused)) *cn) {
1152 /* Main menu plumbing ------------------------------------------------------ */
1154 /** @brief Determine whether the edit menu's properties option should be sensitive */
1155 static int choose_properties_sensitive(GtkWidget attribute((unused)) *w) {
1156 return !!files_selected && (disorder_eclient_state(client) & DISORDER_CONNECTED);
1159 /** @brief Determine whether the edit menu's select all option should be sensitive
1161 * TODO not implemented, see also choose_selectall_activate()
1163 static int choose_selectall_sensitive(GtkWidget attribute((unused)) *w) {
1167 /** @brief Called when the edit menu's properties option is activated */
1168 static void choose_properties_activate(GtkWidget attribute((unused)) *w) {
1169 activate_track_properties(0, 0);
1172 /** @brief Called when the edit menu's select all option is activated
1174 * TODO not implemented, see choose_selectall_sensitive() */
1175 static void choose_selectall_activate(GtkWidget attribute((unused)) *w) {
1178 /** @brief Main menu callbacks for Choose screen */
1179 static const struct tabtype tabtype_choose = {
1180 choose_properties_sensitive,
1181 choose_selectall_sensitive,
1182 choose_properties_activate,
1183 choose_selectall_activate,
1186 /* Public entry points ----------------------------------------------------- */
1188 /** @brief Create a track choice widget */
1189 GtkWidget *choose_widget(void) {
1191 GtkWidget *scrolled;
1192 GtkWidget *vbox, *hbox, *clearsearch;
1195 * +--vbox-------------------------------------------------------+
1196 * | +-hbox----------------------------------------------------+ |
1197 * | | searchentry | clearsearch | |
1198 * | +---------------------------------------------------------+ |
1199 * | +-scrolled------------------------------------------------+ |
1200 * | | +-chooselayout------------------------------------++--+ | |
1201 * | | | Tree structure is manually layed out in here ||^^| | |
1206 * | | +-------------------------------------------------++--+ | |
1207 * | | +-------------------------------------------------+ | |
1209 * | | +-------------------------------------------------+ | |
1210 * | +---------------------------------------------------------+ |
1211 * +-------------------------------------------------------------+
1214 /* Text entry box for search terms */
1216 searchentry = gtk_entry_new();
1217 g_signal_connect(searchentry, "changed", G_CALLBACK(searchentry_changed), 0);
1218 gtk_tooltips_set_tip(tips, searchentry, "Enter search terms here; search is automatic", "");
1220 /* Cancel button to clear the search */
1222 clearsearch = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
1223 g_signal_connect(G_OBJECT(clearsearch), "clicked",
1224 G_CALLBACK(clearsearch_clicked), 0);
1225 gtk_tooltips_set_tip(tips, clearsearch, "Clear search terms", "");
1228 /* hbox packs the search box and the cancel button together on a line */
1230 hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*spacing*/);
1231 gtk_box_pack_start(GTK_BOX(hbox), searchentry,
1232 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
1233 gtk_box_pack_end(GTK_BOX(hbox), clearsearch,
1234 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
1236 /* chooselayout contains the currently visible subset of the track
1239 chooselayout = gtk_layout_new(0, 0);
1240 root = newnode(0/*parent*/, "<root>", "All files", "",
1241 CN_EXPANDABLE, fill_root_node);
1243 expand_node(root); /* will call redisplay_tree */
1244 /* Create the popup menus */
1246 track_menu = gtk_menu_new();
1247 g_signal_connect(track_menu, "destroy", G_CALLBACK(gtk_widget_destroyed),
1249 for(n = 0; track_menuitems[n].name; ++n) {
1251 track_menuitems[n].w =
1252 gtk_menu_item_new_with_label(track_menuitems[n].name);
1253 gtk_menu_attach(GTK_MENU(track_menu), track_menuitems[n].w,
1257 dir_menu = gtk_menu_new();
1258 g_signal_connect(dir_menu, "destroy", G_CALLBACK(gtk_widget_destroyed),
1260 for(n = 0; dir_menuitems[n].name; ++n) {
1262 dir_menuitems[n].w =
1263 gtk_menu_item_new_with_label(dir_menuitems[n].name);
1264 gtk_menu_attach(GTK_MENU(dir_menu), dir_menuitems[n].w,
1267 /* The layout is scrollable */
1268 scrolled = scroll_widget(GTK_WIDGET(chooselayout), "choose");
1270 /* The scrollable layout and the search hbox go together in a vbox */
1272 vbox = gtk_vbox_new(FALSE/*homogenous*/, 1/*spacing*/);
1273 gtk_box_pack_start(GTK_BOX(vbox), hbox,
1274 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
1275 gtk_box_pack_end(GTK_BOX(vbox), scrolled,
1276 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
1278 g_object_set_data(G_OBJECT(vbox), "type", (void *)&tabtype_choose);
1282 /** @brief Called when something we care about here might have changed */
1283 void choose_update(void) {
1292 indent-tabs-mode:nil