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
21 #include "disobedience.h"
23 /* Choose track ------------------------------------------------------------ */
37 /* We don't use the built-in tree widgets because they require that you know
38 * the children of a node on demand, and we have to wait for the server to tell
46 guint width; /* total width required */
47 guint height; /* total height required */
50 /* instantiate the node vector type */
51 VECTOR_TYPE(nodevector, struct choosenode *, xrealloc)
54 struct choosenode *parent; /* parent node */
55 const char *path; /* full path or 0 */
56 const char *sort; /* sort key */
57 const char *display; /* display name */
58 int pending; /* pending resolve queries */
60 #define CN_EXPANDABLE 0x0001 /* node is expandable */
61 #define CN_EXPANDED 0x0002 /* node is expanded */
62 /* Expandable items are directories; non-expandable ones are files */
63 #define CN_DISPLAYED 0x0004 /* widget is displayed in layout */
64 #define CN_SELECTED 0x0008 /* node is selected */
65 struct nodevector children; /* vector of children */
66 void (*fill)(struct choosenode *); /* request child fill or 0 for leaf */
67 GtkWidget *container; /* the container for this row */
68 GtkWidget *hbox; /* the hbox for this row */
69 GtkWidget *arrow; /* arrow widget or 0 */
70 GtkWidget *label; /* text label for this node */
71 GtkWidget *marker; /* queued marker */
76 const char *name; /* name */
79 void (*activate)(GtkMenuItem *menuitem, gpointer user_data);
80 /* Called to activate the menu item. The user data is the choosenode the
83 gboolean (*sensitive)(struct choosenode *cn);
84 /* Called to determine whether the menu item should be sensitive. TODO */
87 gulong handlerid; /* signal handler ID */
88 GtkWidget *w; /* menu item widget */
93 static GtkWidget *chooselayout;
94 static GtkWidget *searchentry; /* search terms */
95 static struct choosenode *root;
96 static struct choosenode *realroot;
97 static GtkWidget *menu; /* our popup menu */
98 static struct choosenode *last_click; /* last clicked node for selection */
99 static int files_visible; /* total files visible */
100 static int files_selected; /* total files selected */
101 static int search_in_flight; /* a search is underway */
102 static int search_obsolete; /* the current search is void */
103 static char **searchresults; /* search results */
104 static int nsearchresults; /* number of results */
106 /* Forward Declarations */
108 static void clear_children(struct choosenode *cn);
109 static struct choosenode *newnode(struct choosenode *parent,
114 void (*fill)(struct choosenode *));
115 static void fill_root_node(struct choosenode *cn);
116 static void fill_letter_node(struct choosenode *cn);
117 static void fill_directory_node(struct choosenode *cn);
118 static void got_files(void *v, int nvec, char **vec);
119 static void got_resolved_file(void *v, const char *track);
120 static void got_dirs(void *v, int nvec, char **vec);
122 static void expand_node(struct choosenode *cn);
123 static void contract_node(struct choosenode *cn);
124 static void updated_node(struct choosenode *cn, int redisplay);
126 static void display_selection(struct choosenode *cn);
127 static void clear_selection(struct choosenode *cn);
129 static void redisplay_tree(void);
130 static struct displaydata display_tree(struct choosenode *cn, int x, int y);
131 static void undisplay_tree(struct choosenode *cn);
132 static void initiate_search(void);
133 static void delete_widgets(struct choosenode *cn);
135 static void clicked_choosenode(GtkWidget attribute((unused)) *widget,
136 GdkEventButton *event,
139 static void activate_play(GtkMenuItem *menuitem, gpointer user_data);
141 static void activate_remove(GtkMenuItem *menuitem, gpointer user_data);
143 static void activate_properties(GtkMenuItem *menuitem, gpointer user_data);
145 static gboolean sensitive_play(struct choosenode *cn);
147 static gboolean sensitive_remove(struct choosenode *cn);
149 static gboolean sensitive_properties(struct choosenode *cn);
151 static struct menuitem menuitems[] = {
152 { "Play track", activate_play, sensitive_play, 0, 0 },
154 /* Not implemented yet */
155 { "Remove", activate_remove, sensitive_remove, 0, 0 },
157 { "Track properties", activate_properties, sensitive_properties, 0, 0 },
160 #define NMENUITEMS (int)(sizeof menuitems / sizeof *menuitems)
162 /* Maintaining the data structure ------------------------------------------ */
164 /* Create a new node */
165 static struct choosenode *newnode(struct choosenode *parent,
170 void (*fill)(struct choosenode *)) {
171 struct choosenode *const n = xmalloc(sizeof *n);
173 D(("newnode %s %s", path, display));
174 if(flags & CN_EXPANDABLE)
180 n->display = display;
183 nodevector_init(&n->children);
186 nodevector_append(&parent->children, n);
191 static void fill_root_node(struct choosenode *cn) {
194 struct callbackdata *cbd;
196 D(("fill_root_node"));
199 if(!cn->children.nvec) { /* Only need to do this once */
200 for(ch = 'A'; ch <= 'Z'; ++ch) {
201 byte_xasprintf(&name, "%c", ch);
202 newnode(cn, "<letter>", name, name, CN_EXPANDABLE, fill_letter_node);
204 newnode(cn, "<letter>", "*", "~", CN_EXPANDABLE, fill_letter_node);
208 /* More de-duping possible here */
209 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
210 cbd = xmalloc(sizeof *cbd);
211 cbd->u.choosenode = cn;
212 disorder_eclient_dirs(client, got_dirs, "", 0, cbd);
213 cbd = xmalloc(sizeof *cbd);
214 cbd->u.choosenode = cn;
215 disorder_eclient_files(client, got_files, "", 0, cbd);
219 static void delete_cn_widgets(struct choosenode *cn) {
222 gtk_widget_destroy(cn->arrow);
227 gtk_widget_destroy(cn->label);
232 gtk_widget_destroy(cn->marker);
237 gtk_widget_destroy(cn->hbox);
242 gtk_widget_destroy(cn->container);
247 /* Clear all the children of CN */
248 static void clear_children(struct choosenode *cn) {
251 D(("clear_children %s", cn->path));
252 /* Recursively clear subtrees */
253 for(n = 0; n < cn->children.nvec; ++n) {
254 clear_children(cn->children.vec[n]);
255 delete_cn_widgets(cn->children.vec[n]);
257 cn->children.nvec = 0;
260 /* Fill a child node */
261 static void fill_letter_node(struct choosenode *cn) {
263 struct callbackdata *cbd;
265 D(("fill_letter_node %s", cn->display));
266 switch(cn->display[0]) {
268 byte_xasprintf((char **)®exp, "^(the )?%c", tolower(cn->display[0]));
271 regexp = "^(?!the [^t])t";
278 /* TODO: de-dupe against fill_directory_node */
279 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
281 cbd = xmalloc(sizeof *cbd);
282 cbd->u.choosenode = cn;
283 disorder_eclient_dirs(client, got_dirs, "", regexp, cbd);
284 cbd = xmalloc(sizeof *cbd);
285 cbd->u.choosenode = cn;
286 disorder_eclient_files(client, got_files, "", regexp, cbd);
289 /* Called with a list of files just below some node */
290 static void got_files(void *v, int nvec, char **vec) {
291 struct callbackdata *cbd = v;
292 struct choosenode *cn = cbd->u.choosenode;
295 D(("got_files %d files for %s", nvec, cn->path));
296 /* Complicated by the need to resolve aliases. We can save a bit of effort
297 * by re-using cbd though. */
299 for(n = 0; n < nvec; ++n)
300 disorder_eclient_resolve(client, got_resolved_file, vec[n], cbd);
303 static void got_resolved_file(void *v, const char *track) {
304 struct callbackdata *cbd = v;
305 struct choosenode *cn = cbd->u.choosenode, *file_cn;
307 file_cn = newnode(cn, track,
308 trackname_transform("track", track, "display"),
309 trackname_transform("track", track, "sort"),
310 0/*flags*/, 0/*fill*/);
311 /* Only bother updating when we've got the lot */
312 if(--cn->pending == 0)
316 /* Called with a list of directories just below some node */
317 static void got_dirs(void *v, int nvec, char **vec) {
318 struct callbackdata *cbd = v;
319 struct choosenode *cn = cbd->u.choosenode;
322 D(("got_dirs %d dirs for %s", nvec, cn->path));
323 for(n = 0; n < nvec; ++n)
325 trackname_transform("dir", vec[n], "display"),
326 trackname_transform("dir", vec[n], "sort"),
327 CN_EXPANDABLE, fill_directory_node);
331 /* Fill a child node */
332 static void fill_directory_node(struct choosenode *cn) {
333 struct callbackdata *cbd;
335 D(("fill_directory_node %s", cn->path));
337 /* TODO: de-dupe against fill_letter_node */
338 assert(report_label != 0);
339 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
341 cbd = xmalloc(sizeof *cbd);
342 cbd->u.choosenode = cn;
343 disorder_eclient_dirs(client, got_dirs, cn->path, 0, cbd);
344 cbd = xmalloc(sizeof *cbd);
345 cbd->u.choosenode = cn;
346 disorder_eclient_files(client, got_files, cn->path, 0, cbd);
350 static void expand_node(struct choosenode *cn) {
351 D(("expand_node %s", cn->path));
352 assert(cn->flags & CN_EXPANDABLE);
353 /* If node is already expanded do nothing. */
354 if(cn->flags & CN_EXPANDED) return;
355 /* We mark the node as expanded and request that it fill itself. When it has
356 * completed it will called updated_node() and we can redraw at that
358 cn->flags |= CN_EXPANDED;
359 /* TODO: visual feedback */
363 /* Contract a node */
364 static void contract_node(struct choosenode *cn) {
365 D(("contract_node %s", cn->path));
366 assert(cn->flags & CN_EXPANDABLE);
367 /* If node is already contracted do nothing. */
368 if(!(cn->flags & CN_EXPANDED)) return;
369 cn->flags &= ~CN_EXPANDED;
370 /* Clear selection below this node */
372 /* Zot children. We never used to do this but the result would be that over
373 * time you'd end up with the entire tree pulled into memory. If the server
374 * is over a slow network it will make interactivity slightly worse; if
375 * anyone complains we can make it an option. */
377 /* We can contract a node immediately. */
381 /* qsort callback for ordering choosenodes */
382 static int compare_choosenode(const void *av, const void *bv) {
383 const struct choosenode *const *aa = av, *const *bb = bv;
384 const struct choosenode *a = *aa, *b = *bb;
386 return compare_tracks(a->sort, b->sort,
387 a->display, b->display,
391 /* Called when an expandable node is updated. */
392 static void updated_node(struct choosenode *cn, int redisplay) {
393 D(("updated_node %s", cn->path));
394 assert(cn->flags & CN_EXPANDABLE);
395 /* It might be that the node has been de-expanded since we requested the
396 * update. In that case we ignore this notification. */
397 if(!(cn->flags & CN_EXPANDED)) return;
399 qsort(cn->children.vec, cn->children.nvec, sizeof (struct choosenode *),
405 /* Searching --------------------------------------------------------------- */
407 static int compare_track_for_qsort(const void *a, const void *b) {
408 return compare_path(*(char **)a, *(char **)b);
411 /* Return true iff FILE is a child of DIR */
412 static int is_child(const char *dir, const char *file) {
413 const size_t dlen = strlen(dir);
415 return (!strncmp(file, dir, dlen)
417 && strchr(file + dlen + 1, '/') == 0);
420 /* Return true iff FILE is a descendant of DIR */
421 static int is_descendant(const char *dir, const char *file) {
422 const size_t dlen = strlen(dir);
424 return !strncmp(file, dir, dlen) && file[dlen] == '/';
427 /* Called to fill a node in the search results tree */
428 static void fill_search_node(struct choosenode *cn) {
430 const size_t plen = strlen(cn->path);
432 char *dir, *last = 0;
434 D(("fill_search_node %s", cn->path));
435 /* We depend on the search results being sorted as by compare_path(). */
437 for(n = 0; n < nsearchresults; ++n) {
438 /* We only care about descendants of CN */
439 if(!is_descendant(cn->path, searchresults[n]))
441 s = strchr(searchresults[n] + plen + 1, '/');
443 /* We've identified a subdirectory of CN. */
444 dir = xstrndup(searchresults[n], s - searchresults[n]);
445 if(!last || strcmp(dir, last)) {
446 /* Not a duplicate */
449 trackname_transform("dir", dir, "display"),
450 trackname_transform("dir", dir, "sort"),
451 CN_EXPANDABLE, fill_search_node);
454 /* We've identified a file in CN */
455 newnode(cn, searchresults[n],
456 trackname_transform("track", searchresults[n], "display"),
457 trackname_transform("track", searchresults[n], "sort"),
458 0/*flags*/, 0/*fill*/);
464 /* This is called from eclient with a (possibly empty) list of search results,
465 * and also from initiate_seatch with an always empty list to indicate that
466 * we're not searching for anything in particular. */
467 static void search_completed(void attribute((unused)) *v,
468 int nvec, char **vec) {
469 struct choosenode *cn;
473 search_in_flight = 0;
474 if(search_obsolete) {
475 /* This search has been obsoleted by user input since it started.
476 * Therefore we throw away the result and search again. */
481 /* We will replace the choose tree with a tree structured view of search
482 * results. First we must disabled the choose tree's widgets. */
483 delete_widgets(root);
484 /* Put the tracks into order, grouped by directory. They'll probably
485 * come back this way anyway in current versions of the server, but it's
486 * cheap not to rely on it (compared with the massive effort we expend
488 qsort(vec, nvec, sizeof(char *), compare_track_for_qsort);
490 nsearchresults = nvec;
491 cn = root = newnode(0/*parent*/, "", "Search results", "",
492 CN_EXPANDABLE|CN_EXPANDED, fill_search_node);
493 /* Construct the initial tree. We do this in a single pass and expand
494 * everything, so you can actually see your search results. */
495 for(n = 0; n < nsearchresults; ++n) {
496 /* Firstly we might need to go up a few directories to each an ancestor
498 while(!is_descendant(cn->path, searchresults[n])) {
499 /* We report the update on each node the last time we see it (With
500 * display=0, the main purpose of this is to get the order of the
501 * children right.) */
505 /* Secondly we might need to insert some new directories */
506 while(!is_child(cn->path, searchresults[n])) {
507 /* Figure out the subdirectory */
508 dir = xstrndup(searchresults[n],
509 strchr(searchresults[n] + strlen(cn->path) + 1,
510 '/') - searchresults[n]);
511 cn = newnode(cn, dir,
512 trackname_transform("dir", dir, "display"),
513 trackname_transform("dir", dir, "sort"),
514 CN_EXPANDABLE|CN_EXPANDED, fill_search_node);
516 /* Finally we can insert the track as a child of the current
518 newnode(cn, searchresults[n],
519 trackname_transform("track", searchresults[n], "display"),
520 trackname_transform("track", searchresults[n], "sort"),
521 0/*flags*/, 0/*fill*/);
524 /* Update all the nodes back up to the root */
528 /* Now it's worth displaying the tree */
530 } else if(root != realroot) {
531 delete_widgets(root);
538 static void initiate_search(void) {
541 /* Find out what the user is after */
542 terms = xstrdup(gtk_entry_get_text(GTK_ENTRY(searchentry)));
543 /* Strip leading and trailing space */
544 while(*terms == ' ') ++terms;
545 e = terms + strlen(terms);
546 while(e > terms && e[-1] == ' ') --e;
548 /* If a search is already underway then mark it as obsolete. We'll revisit
549 * when it returns. */
550 if(search_in_flight) {
555 /* There's still something left. Initiate the search. */
556 if(disorder_eclient_search(client, search_completed, terms, 0)) {
557 /* The search terms are bad! We treat this as if there were no search
558 * terms at all. Some kind of feedback would be handy. */
559 fprintf(stderr, "bad terms [%s]\n", terms); /* TODO */
560 search_completed(0, 0, 0);
562 search_in_flight = 1;
565 /* No search terms - we want to see all tracks */
566 search_completed(0, 0, 0);
570 /* Called when the cancel search button is clicked */
571 static void clearsearch_clicked(GtkButton attribute((unused)) *button,
572 gpointer attribute((unused)) userdata) {
573 gtk_entry_set_text(GTK_ENTRY(searchentry), "");
576 /* Display functions ------------------------------------------------------- */
578 /* Delete all the widgets in the tree */
579 static void delete_widgets(struct choosenode *cn) {
582 delete_cn_widgets(cn);
583 for(n = 0; n < cn->children.nvec; ++n)
584 delete_widgets(cn->children.vec[n]);
585 cn->flags &= ~(CN_DISPLAYED|CN_SELECTED);
589 /* Update the display */
590 static void redisplay_tree(void) {
591 struct displaydata d;
592 guint oldwidth, oldheight;
594 D(("redisplay_tree"));
595 /* We'll count these up empirically each time */
598 /* Correct the layout and find out how much space it uses */
599 MTAG_PUSH("display_tree");
600 d = display_tree(root, 0, 0);
602 /* We must set the total size or scrolling will not work (it wouldn't be hard
603 * for GtkLayout to figure it out for itself but presumably you're supposed
604 * to be able to have widgets off the edge of the layuot.)
606 * There is a problem: if we shrink the size then the part of the screen that
607 * is outside the new size but inside the old one is not updated. I think
608 * this is arguably bug in GTK+ but it's easy to force a redraw if this
609 * region is nonempty.
611 gtk_layout_get_size(GTK_LAYOUT(chooselayout), &oldwidth, &oldheight);
612 if(oldwidth > d.width || oldheight > d.height)
613 gtk_widget_queue_draw(chooselayout);
614 gtk_layout_set_size(GTK_LAYOUT(chooselayout), d.width, d.height);
615 /* Notify the main menu of any recent changes */
619 /* Make sure all displayed widgets from CN down exist and are in their proper
620 * place and return the vertical space used. */
621 static struct displaydata display_tree(struct choosenode *cn, int x, int y) {
624 struct displaydata d, cd;
627 D(("display_tree %s %d,%d", cn->path, x, y));
629 /* An expandable item contains an arrow and a text label. When you press the
630 * button it flips its expand state.
632 * A non-expandable item has just a text label and no arrow.
635 MTAG_PUSH("make_widgets_1");
636 /* Widgets need to be created */
638 cn->hbox = gtk_hbox_new(FALSE, 1);
639 if(cn->flags & CN_EXPANDABLE) {
641 cn->arrow = gtk_arrow_new(cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN
647 if((pb = find_image("notes.png"))) {
649 cn->marker = gtk_image_new_from_pixbuf(pb);
653 MTAG_PUSH("make_widgets_2");
655 cn->label = gtk_label_new(cn->display);
657 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->arrow);
658 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->label);
660 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->marker);
662 MTAG_PUSH("make_widgets_3");
664 cn->container = gtk_event_box_new();
665 gtk_container_add(GTK_CONTAINER(cn->container), cn->hbox);
666 g_signal_connect(cn->container, "button-release-event",
667 G_CALLBACK(clicked_choosenode), cn);
668 g_signal_connect(cn->container, "button-press-event",
669 G_CALLBACK(clicked_choosenode), cn);
670 g_object_ref(cn->container);
671 gtk_widget_set_name(cn->label, "choose");
672 gtk_widget_set_name(cn->container, "choose");
673 /* Show everything by default */
674 gtk_widget_show_all(cn->container);
677 assert(cn->container);
678 /* Make sure the icon is right */
679 if(cn->flags & CN_EXPANDABLE)
680 gtk_arrow_set(GTK_ARROW(cn->arrow),
681 cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN : GTK_ARROW_RIGHT,
684 /* Make sure the queued marker is right */
685 /* TODO: doesn't always work */
686 (queued(cn->path) ? gtk_widget_show : gtk_widget_hide)(cn->marker);
687 /* Put the widget in the right place */
688 if(cn->flags & CN_DISPLAYED)
689 gtk_layout_move(GTK_LAYOUT(chooselayout), cn->container, x, y);
691 gtk_layout_put(GTK_LAYOUT(chooselayout), cn->container, x, y);
692 cn->flags |= CN_DISPLAYED;
693 /* Now chooselayout has a ref to the container */
694 g_object_unref(cn->container);
696 /* Set the widget's selection status */
697 if(!(cn->flags & CN_EXPANDABLE))
698 display_selection(cn);
699 /* Find the size used so we can get vertical positioning right. */
700 gtk_widget_size_request(cn->container, &req);
701 d.width = x + req.width;
702 d.height = y + req.height;
703 if(cn->flags & CN_EXPANDED) {
704 /* We'll offset children by the size of the arrow whatever it might be. */
706 gtk_widget_size_request(cn->arrow, &req);
708 for(n = 0; n < cn->children.nvec; ++n) {
709 cd = display_tree(cn->children.vec[n], x + aw, d.height);
710 if(cd.width > d.width)
712 d.height = cd.height;
715 for(n = 0; n < cn->children.nvec; ++n)
716 undisplay_tree(cn->children.vec[n]);
718 if(!(cn->flags & CN_EXPANDABLE)) {
720 if(cn->flags & CN_SELECTED)
723 /* report back how much space we used */
724 D(("display_tree %s %d,%d total size %dx%d", cn->path, x, y,
729 /* Remove widgets for newly hidden nodes */
730 static void undisplay_tree(struct choosenode *cn) {
733 D(("undisplay_tree %s", cn->path));
734 /* Remove this widget from the display */
735 if(cn->flags & CN_DISPLAYED) {
736 gtk_container_remove(GTK_CONTAINER(chooselayout), cn->container);
737 cn->flags ^= CN_DISPLAYED;
739 /* Remove children too */
740 for(n = 0; n < cn->children.nvec; ++n)
741 undisplay_tree(cn->children.vec[n]);
744 /* Selection --------------------------------------------------------------- */
746 static void display_selection(struct choosenode *cn) {
747 /* Need foreground and background colors */
748 gtk_widget_set_state(cn->label, (cn->flags & CN_SELECTED
749 ? GTK_STATE_SELECTED : GTK_STATE_NORMAL));
750 gtk_widget_set_state(cn->container, (cn->flags & CN_SELECTED
751 ? GTK_STATE_SELECTED : GTK_STATE_NORMAL));
754 /* Set the selection state of a widget. Directories can never be selected, we
755 * just ignore attempts to do so. */
756 static void set_selection(struct choosenode *cn, int selected) {
757 unsigned f = selected ? CN_SELECTED : 0;
759 D(("set_selection %d %s", selected, cn->path));
760 if(!(cn->flags & CN_EXPANDABLE) && (cn->flags & CN_SELECTED) != f) {
761 cn->flags ^= CN_SELECTED;
762 /* Maintain selection count */
767 display_selection(cn);
768 /* Update main menu sensitivity */
773 /* Recursively clear all selection bits from CN down */
774 static void clear_selection(struct choosenode *cn) {
777 set_selection(cn, 0);
778 for(n = 0; n < cn->children.nvec; ++n)
779 clear_selection(cn->children.vec[n]);
782 /* User actions ------------------------------------------------------------ */
784 /* Clicked on something */
785 static void clicked_choosenode(GtkWidget attribute((unused)) *widget,
786 GdkEventButton *event,
787 gpointer user_data) {
788 struct choosenode *cn = user_data;
789 int ind, last_ind, n;
791 D(("clicked_choosenode %s", cn->path));
792 if(event->type == GDK_BUTTON_RELEASE
793 && event->button == 1) {
795 if(cn->flags & CN_EXPANDABLE) {
796 /* This is a directory. Flip its expansion status. */
797 if(cn->flags & CN_EXPANDED)
803 /* This is a file. Adjust selection status */
804 /* TODO the basic logic here is essentially the same as that in queue.c.
805 * Can we share code at all? */
806 switch(event->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) {
808 clear_selection(root);
809 set_selection(cn, 1);
812 case GDK_CONTROL_MASK:
813 set_selection(cn, !(cn->flags & CN_SELECTED));
817 case GDK_SHIFT_MASK|GDK_CONTROL_MASK:
818 if(last_click && last_click->parent == cn->parent) {
819 /* Figure out where the current and last clicks are in the list */
821 for(n = 0; n < cn->parent->children.nvec; ++n) {
822 if(cn->parent->children.vec[n] == cn)
824 if(cn->parent->children.vec[n] == last_click)
827 /* Test shouldn't ever fail, but still */
828 if(ind >= 0 && last_ind >= 0) {
829 if(!(event->state & GDK_CONTROL_MASK)) {
830 for(n = 0; n < cn->parent->children.nvec; ++n)
831 set_selection(cn->parent->children.vec[n], 0);
834 for(n = last_ind; n <= ind; ++n)
835 set_selection(cn->parent->children.vec[n], 1);
837 for(n = ind; n <= last_ind; ++n)
838 set_selection(cn->parent->children.vec[n], 1);
839 if(event->state & GDK_CONTROL_MASK)
846 } else if(event->type == GDK_BUTTON_RELEASE
847 && event->button == 2) {
848 /* Middle click - play the pointed track */
849 if(!(cn->flags & CN_EXPANDABLE)) {
850 clear_selection(root);
851 set_selection(cn, 1);
852 gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
853 disorder_eclient_play(client, cn->path, 0, 0);
856 } else if(event->type == GDK_BUTTON_PRESS
857 && event->button == 3) {
858 /* Right click. Pop up a menu. */
859 /* If the current file isn't selected, switch the selection to just that.
860 * (If we're looking at a directory then leave the selection alone.) */
861 if(!(cn->flags & CN_EXPANDABLE) && !(cn->flags & CN_SELECTED)) {
862 clear_selection(root);
863 set_selection(cn, 1);
866 /* Set the item sensitivity and callbacks */
867 for(n = 0; n < NMENUITEMS; ++n) {
868 if(menuitems[n].handlerid)
869 g_signal_handler_disconnect(menuitems[n].w,
870 menuitems[n].handlerid);
871 gtk_widget_set_sensitive(menuitems[n].w,
872 menuitems[n].sensitive(cn));
873 menuitems[n].handlerid = g_signal_connect
874 (menuitems[n].w, "activate", G_CALLBACK(menuitems[n].activate), cn);
876 /* Pop up the menu */
877 gtk_widget_show_all(menu);
878 gtk_menu_popup(GTK_MENU(menu), 0, 0, 0, 0,
879 event->button, event->time);
883 static void searchentry_changed(GtkEditable attribute((unused)) *editable,
884 gpointer attribute((unused)) user_data) {
888 /* Menu items -------------------------------------------------------------- */
890 static void recurse_selected(struct choosenode *cn, struct vector *v) {
893 if(cn->flags & CN_EXPANDABLE) {
894 if(cn->flags & CN_EXPANDED)
895 for(n = 0; n < cn->children.nvec; ++n)
896 recurse_selected(cn->children.vec[n], v);
898 if((cn->flags & CN_SELECTED) && cn->path)
899 vector_append(v, (char *)cn->path);
903 static char **gather_selected(int *ntracks) {
907 recurse_selected(root, &v);
908 vector_terminate(&v);
909 if(ntracks) *ntracks = v.nvec;
913 static void activate_play(GtkMenuItem attribute((unused)) *menuitem,
914 gpointer attribute((unused)) user_data) {
915 char **tracks = gather_selected(0);
918 gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
919 for(n = 0; tracks[n]; ++n)
920 disorder_eclient_play(client, tracks[n], 0, 0);
924 static void activate_remove(GtkMenuItem attribute((unused)) *menuitem,
925 gpointer attribute((unused)) user_data) {
926 /* TODO remove all selected tracks */
930 static void activate_properties(GtkMenuItem attribute((unused)) *menuitem,
931 gpointer attribute((unused)) user_data) {
933 char **tracks = gather_selected(&ntracks);
935 properties(ntracks, tracks);
938 static gboolean sensitive_play(struct choosenode attribute((unused)) *cn) {
939 return !!files_selected;
943 static gboolean sensitive_remove(struct choosenode attribute((unused)) *cn) {
944 return FALSE; /* not implemented yet */
948 static gboolean sensitive_properties(struct choosenode attribute((unused)) *cn) {
949 return !!files_selected;
952 /* Main menu plumbing ------------------------------------------------------ */
954 static int choose_properties_sensitive(GtkWidget attribute((unused)) *w) {
955 return !!files_selected;
958 static int choose_selectall_sensitive(GtkWidget attribute((unused)) *w) {
959 return FALSE; /* TODO */
962 static void choose_properties_activate(GtkWidget attribute((unused)) *w) {
963 activate_properties(0, 0);
966 static void choose_selectall_activate(GtkWidget attribute((unused)) *w) {
970 static const struct tabtype tabtype_choose = {
971 choose_properties_sensitive,
972 choose_selectall_sensitive,
973 choose_properties_activate,
974 choose_selectall_activate,
977 /* Public entry points ----------------------------------------------------- */
979 /* Create a track choice widget */
980 GtkWidget *choose_widget(void) {
983 GtkWidget *vbox, *hbox, *clearsearch;
986 * +--vbox-------------------------------------------------------+
987 * | +-hbox----------------------------------------------------+ |
988 * | | searchentry | clearsearch | |
989 * | +---------------------------------------------------------+ |
990 * | +-scrolled------------------------------------------------+ |
991 * | | +-chooselayout------------------------------------++--+ | |
992 * | | | Tree structure is manually layed out in here ||^^| | |
997 * | | +-------------------------------------------------++--+ | |
998 * | | +-------------------------------------------------+ | |
1000 * | | +-------------------------------------------------+ | |
1001 * | +---------------------------------------------------------+ |
1002 * +-------------------------------------------------------------+
1005 /* Text entry box for search terms */
1007 searchentry = gtk_entry_new();
1008 g_signal_connect(searchentry, "changed", G_CALLBACK(searchentry_changed), 0);
1010 /* Cancel button to clear the search */
1012 clearsearch = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
1013 g_signal_connect(G_OBJECT(clearsearch), "clicked",
1014 G_CALLBACK(clearsearch_clicked), 0);
1016 /* hbox packs the search box and the cancel button together on a line */
1018 hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*spacing*/);
1019 gtk_box_pack_start(GTK_BOX(hbox), searchentry,
1020 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
1021 gtk_box_pack_end(GTK_BOX(hbox), clearsearch,
1022 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
1024 /* chooselayout contains the currently visible subset of the track
1027 chooselayout = gtk_layout_new(0, 0);
1028 root = newnode(0/*parent*/, "<root>", "All files", "",
1029 CN_EXPANDABLE, fill_root_node);
1031 expand_node(root); /* will call redisplay_tree */
1032 /* Create the popup menu */
1034 menu = gtk_menu_new();
1035 g_signal_connect(menu, "destroy", G_CALLBACK(gtk_widget_destroyed), &menu);
1036 for(n = 0; n < NMENUITEMS; ++n) {
1038 menuitems[n].w = gtk_menu_item_new_with_label(menuitems[n].name);
1039 gtk_menu_attach(GTK_MENU(menu), menuitems[n].w, 0, 1, n, n + 1);
1041 /* The layout is scrollable */
1042 scrolled = scroll_widget(GTK_WIDGET(chooselayout), "choose");
1044 /* The scrollable layout and the search hbox go together in a vbox */
1046 vbox = gtk_vbox_new(FALSE/*homogenous*/, 1/*spacing*/);
1047 gtk_box_pack_start(GTK_BOX(vbox), hbox,
1048 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
1049 gtk_box_pack_end(GTK_BOX(vbox), scrolled,
1050 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
1052 g_object_set_data(G_OBJECT(vbox), "type", (void *)&tabtype_choose);
1056 /* Called when something we care about here might have changed */
1057 void choose_update(void) {
1066 indent-tabs-mode:nil