2 * This file is part of DisOrder
3 * Copyright (C) 2006, 2007 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file disobedience/choose.c
21 * @brief Hierarchical track selection and search
23 * We don't use the built-in tree widgets because they require that you know
24 * the children of a node on demand, and we have to wait for the server to tell
28 #include "disobedience.h"
30 /* Choose track ------------------------------------------------------------ */
48 /** @brief Accumulated information about the tree widget */
50 /** @brief Maximum width required */
52 /** @brief Maximum height required */
56 /* instantiate the node vector type */
58 VECTOR_TYPE(nodevector
, struct choosenode
*, xrealloc
);
60 /** @brief Signature of function called when a choosenode is filled */
61 typedef void (when_filled_callback
)(struct choosenode
*cn
,
64 /** @brief One node in the virtual filesystem */
66 struct choosenode
*parent
; /**< @brief parent node */
67 const char *path
; /**< @brief full path or 0 */
68 const char *sort
; /**< @brief sort key */
69 const char *display
; /**< @brief display name */
70 int pending
; /**< @brief pending resolve queries */
72 #define CN_EXPANDABLE 0x0001 /**< @brief node is expandable */
73 #define CN_EXPANDED 0x0002 /**< @brief node is expanded
75 * Expandable items are directories;
76 * non-expandable ones are files. */
77 #define CN_DISPLAYED 0x0004 /**< @brief widget is displayed in layout */
78 #define CN_SELECTED 0x0008 /**< @brief node is selected */
79 #define CN_GETTING_FILES 0x0010 /**< @brief files inbound */
80 #define CN_RESOLVING_FILES 0x0020 /**< @brief resolved files inbound */
81 #define CN_GETTING_DIRS 0x0040 /**< @brief directories inbound */
82 #define CN_GETTING_ANY 0x0070 /**< @brief getting something */
83 #define CN_CONTINGENT 0x0080 /**< @brief expansion contingent on search */
84 struct nodevector children
; /**< @brief vector of children */
85 void (*fill
)(struct choosenode
*); /**< @brief request child fill or 0 for leaf */
86 GtkWidget
*container
; /**< @brief the container for this row */
87 GtkWidget
*hbox
; /**< @brief the hbox for this row */
88 GtkWidget
*arrow
; /**< @brief arrow widget or 0 */
89 GtkWidget
*label
; /**< @brief text label for this node */
90 GtkWidget
*marker
; /**< @brief queued marker */
92 when_filled_callback
*whenfilled
; /**< @brief called when filled or 0 */
93 void *wfu
; /**< @brief passed to @c whenfilled */
96 /** @brief One item in the popup menu */
97 struct choose_menuitem
{
99 const char *name
; /**< @brief name */
102 void (*activate
)(GtkMenuItem
*menuitem
, gpointer user_data
);
103 /**< @brief Called to activate the menu item.
105 * @p user_data is the choosenode the mouse pointer is over. */
107 gboolean (*sensitive
)(struct choosenode
*cn
);
108 /* @brief Called to determine whether the menu item should be sensitive.
113 gulong handlerid
; /**< @brief signal handler ID */
114 GtkWidget
*w
; /**< @brief menu item widget */
119 static GtkWidget
*chooselayout
;
120 static GtkWidget
*searchentry
; /**< @brief search terms */
121 static struct choosenode
*root
;
122 static GtkWidget
*track_menu
; /**< @brief track popup menu */
123 static GtkWidget
*dir_menu
; /**< @brief directory popup menu */
124 static struct choosenode
*last_click
; /**< @brief last clicked node for selection */
125 static int files_visible
; /**< @brief total files visible */
126 static int files_selected
; /**< @brief total files selected */
127 static int gets_in_flight
; /**< @brief total gets in flight */
128 static int search_in_flight
; /**< @brief a search is underway */
129 static int search_obsolete
; /**< @brief the current search is void */
130 static char **searchresults
; /**< @brief search results */
131 static int nsearchresults
; /**< @brief number of results */
132 static int nsearchvisible
; /**< @brief number of search results visible */
133 static struct hash
*searchhash
; /**< @brief hash of search results */
134 struct progress_window
*spw
; /**< @brief progress window */
136 /* Forward Declarations */
138 static void clear_children(struct choosenode
*cn
);
139 static struct choosenode
*newnode(struct choosenode
*parent
,
144 void (*fill
)(struct choosenode
*));
145 static void fill_root_node(struct choosenode
*cn
);
146 static void fill_directory_node(struct choosenode
*cn
);
147 static void got_files(void *v
, int nvec
, char **vec
);
148 static void got_resolved_file(void *v
, const char *track
);
149 static void got_dirs(void *v
, int nvec
, char **vec
);
151 static void expand_node(struct choosenode
*cn
, int contingent
);
152 static void contract_node(struct choosenode
*cn
);
153 static void updated_node(struct choosenode
*cn
, int redisplay
);
155 static void display_selection(struct choosenode
*cn
);
156 static void clear_selection(struct choosenode
*cn
);
158 static void redisplay_tree(void);
159 static struct displaydata
display_tree(struct choosenode
*cn
, int x
, int y
);
160 static void undisplay_tree(struct choosenode
*cn
);
161 static void initiate_search(void);
162 static void delete_widgets(struct choosenode
*cn
);
163 static void expand_from(struct choosenode
*cn
);
165 static void clicked_choosenode(GtkWidget
attribute((unused
)) *widget
,
166 GdkEventButton
*event
,
169 static void activate_track_play(GtkMenuItem
*menuitem
, gpointer user_data
);
170 static void activate_track_properties(GtkMenuItem
*menuitem
, gpointer user_data
);
172 static gboolean
sensitive_track_play(struct choosenode
*cn
);
173 static gboolean
sensitive_track_properties(struct choosenode
*cn
);
175 static void activate_dir_play(GtkMenuItem
*menuitem
, gpointer user_data
);
176 static void activate_dir_properties(GtkMenuItem
*menuitem
, gpointer user_data
);
177 static void activate_dir_select(GtkMenuItem
*menuitem
, gpointer user_data
);
179 static gboolean
sensitive_dir_play(struct choosenode
*cn
);
180 static gboolean
sensitive_dir_properties(struct choosenode
*cn
);
181 static gboolean
sensitive_dir_select(struct choosenode
*cn
);
183 /** @brief Track menu items */
184 static struct choose_menuitem track_menuitems
[] = {
185 { "Play track", activate_track_play
, sensitive_track_play
, 0, 0 },
186 { "Track properties", activate_track_properties
, sensitive_track_properties
, 0, 0 },
190 /** @brief Directory menu items */
191 static struct choose_menuitem dir_menuitems
[] = {
192 { "Play all tracks", activate_dir_play
, sensitive_dir_play
, 0, 0 },
193 { "Track properties", activate_dir_properties
, sensitive_dir_properties
, 0, 0 },
194 { "Select all tracks", activate_dir_select
, sensitive_dir_select
, 0, 0 },
198 /* Maintaining the data structure ------------------------------------------ */
200 static char *flags(const struct choosenode
*cn
) {
201 unsigned f
= cn
->flags
, n
;
204 static const char *bits
[] = {
214 #define NBITS (sizeof bits / sizeof *bits)
218 dynstr_append(d
, '0');
220 for(n
= 0; n
< NBITS
; ++n
) {
221 const unsigned bit
= 1 << n
;
224 dynstr_append(d
, '|');
225 dynstr_append_string(d
, bits
[n
]);
232 dynstr_append(d
, '|');
233 sprintf(buf
, "%#x", f
);
234 dynstr_append_string(d
, buf
);
241 /** @brief Create a new node */
242 static struct choosenode
*newnode(struct choosenode
*parent
,
247 void (*fill
)(struct choosenode
*)) {
248 struct choosenode
*const n
= xmalloc(sizeof *n
);
250 D(("newnode %s %s", path
, display
));
251 if(flags
& CN_EXPANDABLE
)
257 n
->display
= display
;
260 nodevector_init(&n
->children
);
263 nodevector_append(&parent
->children
, n
);
267 /** @brief Called when a node has been filled
269 * Response for calling @c whenfilled.
271 static void filled(struct choosenode
*cn
) {
272 when_filled_callback
*const whenfilled
= cn
->whenfilled
;
276 whenfilled(cn
, cn
->wfu
);
278 if(nsearchvisible
< nsearchresults
) {
279 /* There is still search expansion work to do */
280 D(("filled %s %d/%d", cn
->path
, nsearchvisible
, nsearchresults
));
283 if(gets_in_flight
== 0 && nsearchvisible
< nsearchresults
)
287 /** @brief Fill the root */
288 static void fill_root_node(struct choosenode
*cn
) {
289 struct callbackdata
*cbd
;
291 D(("fill_root_node"));
293 /* More de-duping possible here */
294 if(cn
->flags
& CN_GETTING_ANY
)
296 gtk_label_set_text(GTK_LABEL(report_label
), "getting files");
297 cbd
= xmalloc(sizeof *cbd
);
298 cbd
->u
.choosenode
= cn
;
299 disorder_eclient_dirs(client
, got_dirs
, "", 0, cbd
);
300 cbd
= xmalloc(sizeof *cbd
);
301 cbd
->u
.choosenode
= cn
;
302 disorder_eclient_files(client
, got_files
, "", 0, cbd
);
303 cn
->flags
|= CN_GETTING_FILES
|CN_GETTING_DIRS
;
307 /** @brief Delete all the widgets owned by @p cn */
308 static void delete_cn_widgets(struct choosenode
*cn
) {
311 gtk_widget_destroy(cn
->arrow
);
316 gtk_widget_destroy(cn
->label
);
321 gtk_widget_destroy(cn
->marker
);
326 gtk_widget_destroy(cn
->hbox
);
331 gtk_widget_destroy(cn
->container
);
336 /** @brief Recursively clear all the children of @p cn
338 * All the widgets at or below @p cn are deleted. All choosenodes below
339 * it are emptied. i.e. we prune the tree at @p cn.
341 static void clear_children(struct choosenode
*cn
) {
344 D(("clear_children %s", cn
->path
));
345 /* Recursively clear subtrees */
346 for(n
= 0; n
< cn
->children
.nvec
; ++n
) {
347 clear_children(cn
->children
.vec
[n
]);
348 delete_cn_widgets(cn
->children
.vec
[n
]);
350 cn
->children
.nvec
= 0;
353 /** @brief Called with a list of files just below some node */
354 static void got_files(void *v
, int nvec
, char **vec
) {
355 struct callbackdata
*cbd
= v
;
356 struct choosenode
*cn
= cbd
->u
.choosenode
;
359 D(("got_files %d files for %s %s", nvec
, cn
->path
, flags(cn
)));
360 /* Complicated by the need to resolve aliases. We can save a bit of effort
361 * by re-using cbd though. */
362 cn
->flags
&= ~CN_GETTING_FILES
;
364 if((cn
->pending
= nvec
)) {
365 cn
->flags
|= CN_RESOLVING_FILES
;
366 for(n
= 0; n
< nvec
; ++n
) {
367 disorder_eclient_resolve(client
, got_resolved_file
, vec
[n
], cbd
);
371 /* If there are no files and the directories are all read by now, we're
373 if(!(cn
->flags
& CN_GETTING_ANY
))
377 /** @brief Called with an alias resolved filename */
378 static void got_resolved_file(void *v
, const char *track
) {
379 struct callbackdata
*cbd
= v
;
380 struct choosenode
*cn
= cbd
->u
.choosenode
, *file_cn
;
382 D(("resolved %s %s %d left", cn
->path
, flags(cn
), cn
->pending
- 1));
384 file_cn
= newnode(cn
, track
,
385 trackname_transform("track", track
, "display"),
386 trackname_transform("track", track
, "sort"),
387 0/*flags*/, 0/*fill*/);
389 /* Only bother updating when we've got the lot */
390 if(--cn
->pending
== 0) {
391 cn
->flags
&= ~CN_RESOLVING_FILES
;
393 if(!(cn
->flags
& CN_GETTING_ANY
))
398 /** @brief Called with a list of directories just below some node */
399 static void got_dirs(void *v
, int nvec
, char **vec
) {
400 struct callbackdata
*cbd
= v
;
401 struct choosenode
*cn
= cbd
->u
.choosenode
;
404 D(("got_dirs %d dirs for %s %s", nvec
, cn
->path
, flags(cn
)));
405 /* TODO this depends on local configuration for trackname_transform().
406 * This will work, since the defaults are now built-in, but it'll be
407 * (potentially) different to the server's configured settings.
409 * Really we want a variant of files/dirs that produces both the
410 * raw filename and the transformed name for a chosen context.
413 for(n
= 0; n
< nvec
; ++n
)
415 trackname_transform("dir", vec
[n
], "display"),
416 trackname_transform("dir", vec
[n
], "sort"),
417 CN_EXPANDABLE
, fill_directory_node
);
419 cn
->flags
&= ~CN_GETTING_DIRS
;
420 if(!(cn
->flags
& CN_GETTING_ANY
))
424 /** @brief Fill a child node */
425 static void fill_directory_node(struct choosenode
*cn
) {
426 struct callbackdata
*cbd
;
428 D(("fill_directory_node %s", cn
->path
));
430 if(cn
->flags
& CN_GETTING_ANY
)
432 assert(report_label
!= 0);
433 gtk_label_set_text(GTK_LABEL(report_label
), "getting files");
435 cbd
= xmalloc(sizeof *cbd
);
436 cbd
->u
.choosenode
= cn
;
437 disorder_eclient_dirs(client
, got_dirs
, cn
->path
, 0, cbd
);
438 cbd
= xmalloc(sizeof *cbd
);
439 cbd
->u
.choosenode
= cn
;
440 disorder_eclient_files(client
, got_files
, cn
->path
, 0, cbd
);
441 cn
->flags
|= CN_GETTING_FILES
|CN_GETTING_DIRS
;
445 /** @brief Expand a node */
446 static void expand_node(struct choosenode
*cn
, int contingent
) {
447 D(("expand_node %s %d %s", cn
->path
, contingent
, flags(cn
)));
448 assert(cn
->flags
& CN_EXPANDABLE
);
449 /* If node is already expanded do nothing. */
450 if(cn
->flags
& CN_EXPANDED
) return;
451 /* We mark the node as expanded and request that it fill itself. When it has
452 * completed it will called updated_node() and we can redraw at that
454 cn
->flags
|= CN_EXPANDED
;
456 cn
->flags
|= CN_CONTINGENT
;
458 cn
->flags
&= ~CN_CONTINGENT
;
459 /* If this node is not contingently expanded, mark all its parents back to
460 * the root as not contingent either, so they won't be contracted when the
461 * search results change */
463 struct choosenode
*cnp
;
465 for(cnp
= cn
->parent
; cnp
; cnp
= cnp
->parent
)
466 cnp
->flags
&= ~CN_CONTINGENT
;
468 /* TODO: visual feedback */
472 /** @brief Make sure all the search results below @p cn are expanded
473 * @param cn Node to start at
475 static void expand_from(struct choosenode
*cn
) {
478 if(nsearchvisible
== nsearchresults
)
481 /* Are any of the search tracks at/below this point? */
482 if(!(cn
== root
|| hash_find(searchhash
, cn
->path
)))
484 D(("expand_from %d/%d visible %s",
485 nsearchvisible
, nsearchresults
, cn
->path
));
486 if(cn
->flags
& CN_EXPANDABLE
) {
487 if(cn
->flags
& CN_EXPANDED
)
488 /* This node is marked as expanded already. children.nvec might be 0,
489 * indicating that expansion is still underway. We should get another
490 * callback when it is expanded. */
491 for(n
= 0; n
< cn
->children
.nvec
&& gets_in_flight
< 10; ++n
)
492 expand_from(cn
->children
.vec
[n
]);
494 /* This node is not expanded yet */
498 /* This is an actual search result */
500 progress_window_progress(spw
, nsearchvisible
, nsearchresults
);
504 /** @brief Contract all contingently expanded nodes below @p cn */
505 static void contract_contingent(struct choosenode
*cn
) {
508 if(cn
->flags
& CN_CONTINGENT
)
511 for(n
= 0; n
< cn
->children
.nvec
; ++n
)
512 contract_contingent(cn
->children
.vec
[n
]);
515 /** @brief Contract a node */
516 static void contract_node(struct choosenode
*cn
) {
517 D(("contract_node %s", cn
->path
));
518 assert(cn
->flags
& CN_EXPANDABLE
);
519 /* If node is already contracted do nothing. */
520 if(!(cn
->flags
& CN_EXPANDED
)) return;
521 cn
->flags
&= ~(CN_EXPANDED
|CN_CONTINGENT
);
522 /* Clear selection below this node */
524 /* Zot children. We never used to do this but the result would be that over
525 * time you'd end up with the entire tree pulled into memory. If the server
526 * is over a slow network it will make interactivity slightly worse; if
527 * anyone complains we can make it an option. */
529 /* We can contract a node immediately. */
533 /** @brief qsort() callback for ordering choosenodes */
534 static int compare_choosenode(const void *av
, const void *bv
) {
535 const struct choosenode
*const *aa
= av
, *const *bb
= bv
;
536 const struct choosenode
*a
= *aa
, *b
= *bb
;
538 return compare_tracks(a
->sort
, b
->sort
,
539 a
->display
, b
->display
,
543 /** @brief Called when an expandable node is updated. */
544 static void updated_node(struct choosenode
*cn
, int redisplay
) {
545 D(("updated_node %s", cn
->path
));
546 assert(cn
->flags
& CN_EXPANDABLE
);
547 /* It might be that the node has been de-expanded since we requested the
548 * update. In that case we ignore this notification. */
549 if(!(cn
->flags
& CN_EXPANDED
)) return;
551 qsort(cn
->children
.vec
, cn
->children
.nvec
, sizeof (struct choosenode
*),
557 /* Searching --------------------------------------------------------------- */
559 /** @brief Return true if @p track is a search result */
560 static int is_search_result(const char *track
) {
561 return searchhash
&& hash_find(searchhash
, track
);
564 /** @brief Called with a list of search results
566 * This is called from eclient with a (possibly empty) list of search results,
567 * and also from initiate_seatch with an always empty list to indicate that
568 * we're not searching for anything in particular. */
569 static void search_completed(void attribute((unused
)) *v
,
570 int nvec
, char **vec
) {
574 search_in_flight
= 0;
575 /* Contract any choosenodes that were only expanded to show search
577 contract_contingent(root
);
578 if(search_obsolete
) {
579 /* This search has been obsoleted by user input since it started.
580 * Therefore we throw away the result and search again. */
584 /* Stash the search results */
586 nsearchresults
= nvec
;
588 /* Create a new search hash for fast identification of results */
589 searchhash
= hash_new(1);
590 for(n
= 0; n
< nvec
; ++n
) {
591 /* The filename itself lives in the hash */
592 hash_add(searchhash
, vec
[n
], "", HASH_INSERT_OR_REPLACE
);
593 /* So do its ancestor directories */
594 for(s
= vec
[n
] + 1; *s
; ++s
) {
597 hash_add(searchhash
, vec
[n
], "", HASH_INSERT_OR_REPLACE
);
602 /* We don't yet know that the results are visible */
605 progress_window_progress(spw
, 0, 0);
608 if(nsearchresults
> 50)
609 spw
= progress_window_new("Fetching search results");
610 /* Initiate expansion */
613 searchhash
= 0; /* for the gc */
614 redisplay_tree(); /* remove search markers */
619 /** @brief Initiate a search
621 * If a search is underway we set @ref search_obsolete and restart the search
622 * in search_completed() above.
624 static void initiate_search(void) {
627 /* Find out what the user is after */
628 terms
= xstrdup(gtk_entry_get_text(GTK_ENTRY(searchentry
)));
629 /* Strip leading and trailing space */
630 while(*terms
== ' ') ++terms
;
631 e
= terms
+ strlen(terms
);
632 while(e
> terms
&& e
[-1] == ' ') --e
;
634 /* If a search is already underway then mark it as obsolete. We'll revisit
635 * when it returns. */
636 if(search_in_flight
) {
641 /* There's still something left. Initiate the search. */
642 if(disorder_eclient_search(client
, search_completed
, terms
, 0)) {
643 /* The search terms are bad! We treat this as if there were no search
644 * terms at all. Some kind of feedback would be handy. */
645 fprintf(stderr
, "bad terms [%s]\n", terms
); /* TODO */
646 search_completed(0, 0, 0);
648 search_in_flight
= 1;
651 /* No search terms - we want to see all tracks */
652 search_completed(0, 0, 0);
656 /** @brief Called when the cancel search button is clicked */
657 static void clearsearch_clicked(GtkButton
attribute((unused
)) *button
,
658 gpointer
attribute((unused
)) userdata
) {
659 gtk_entry_set_text(GTK_ENTRY(searchentry
), "");
662 /* Display functions ------------------------------------------------------- */
664 /** @brief Delete all the widgets in the tree */
665 static void delete_widgets(struct choosenode
*cn
) {
668 delete_cn_widgets(cn
);
669 for(n
= 0; n
< cn
->children
.nvec
; ++n
)
670 delete_widgets(cn
->children
.vec
[n
]);
671 cn
->flags
&= ~(CN_DISPLAYED
|CN_SELECTED
);
675 /** @brief Update the display */
676 static void redisplay_tree(void) {
677 struct displaydata d
;
678 guint oldwidth
, oldheight
;
680 D(("redisplay_tree"));
681 /* We'll count these up empirically each time */
684 /* Correct the layout and find out how much space it uses */
685 MTAG_PUSH("display_tree");
686 d
= display_tree(root
, 0, 0);
688 /* We must set the total size or scrolling will not work (it wouldn't be hard
689 * for GtkLayout to figure it out for itself but presumably you're supposed
690 * to be able to have widgets off the edge of the layuot.)
692 * There is a problem: if we shrink the size then the part of the screen that
693 * is outside the new size but inside the old one is not updated. I think
694 * this is arguably bug in GTK+ but it's easy to force a redraw if this
695 * region is nonempty.
697 gtk_layout_get_size(GTK_LAYOUT(chooselayout
), &oldwidth
, &oldheight
);
698 if(oldwidth
> d
.width
|| oldheight
> d
.height
)
699 gtk_widget_queue_draw(chooselayout
);
700 gtk_layout_set_size(GTK_LAYOUT(chooselayout
), d
.width
, d
.height
);
701 /* Notify the main menu of any recent changes */
705 /** @brief Recursive step for redisplay_tree()
707 * Makes sure all displayed widgets from CN down exist and are in their proper
708 * place and return the maximum space used.
710 static struct displaydata
display_tree(struct choosenode
*cn
, int x
, int y
) {
713 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 /* Show everything by default */
762 gtk_widget_show_all(cn
->container
);
765 assert(cn
->container
);
766 /* Make sure the widget name is right */
767 name
= (cn
->flags
& CN_EXPANDABLE
769 : is_search_result(cn
->path
) ?
"choose-search" : "choose");
770 gtk_widget_set_name(cn
->label
, name
);
771 gtk_widget_set_name(cn
->container
, name
);
772 /* Make sure the icon is right */
773 if(cn
->flags
& CN_EXPANDABLE
)
774 gtk_arrow_set(GTK_ARROW(cn
->arrow
),
775 cn
->flags
& CN_EXPANDED ? GTK_ARROW_DOWN
: GTK_ARROW_RIGHT
,
778 /* Make sure the queued marker is right */
779 /* TODO: doesn't always work */
780 (queued(cn
->path
) ? gtk_widget_show
: gtk_widget_hide
)(cn
->marker
);
781 /* Put the widget in the right place */
782 if(cn
->flags
& CN_DISPLAYED
)
783 gtk_layout_move(GTK_LAYOUT(chooselayout
), cn
->container
, x
, y
);
785 gtk_layout_put(GTK_LAYOUT(chooselayout
), cn
->container
, x
, y
);
786 cn
->flags
|= CN_DISPLAYED
;
787 /* Now chooselayout has a ref to the container */
788 g_object_unref(cn
->container
);
790 /* Set the widget's selection status */
791 if(!(cn
->flags
& CN_EXPANDABLE
))
792 display_selection(cn
);
793 /* Find the size used so we can get vertical positioning right. */
794 gtk_widget_size_request(cn
->container
, &req
);
795 d
.width
= x
+ req
.width
;
796 d
.height
= y
+ req
.height
;
797 if(cn
->flags
& CN_EXPANDED
) {
798 /* We'll offset children by the size of the arrow whatever it might be. */
800 gtk_widget_size_request(cn
->arrow
, &req
);
802 for(n
= 0; n
< cn
->children
.nvec
; ++n
) {
803 cd
= display_tree(cn
->children
.vec
[n
], x
+ aw
, d
.height
);
804 if(cd
.width
> d
.width
)
806 d
.height
= cd
.height
;
809 for(n
= 0; n
< cn
->children
.nvec
; ++n
)
810 undisplay_tree(cn
->children
.vec
[n
]);
812 if(!(cn
->flags
& CN_EXPANDABLE
)) {
814 if(cn
->flags
& CN_SELECTED
)
817 /* report back how much space we used */
818 D(("display_tree %s %d,%d total size %dx%d", cn
->path
, x
, y
,
823 /** @brief Remove widgets for newly hidden nodes */
824 static void undisplay_tree(struct choosenode
*cn
) {
827 D(("undisplay_tree %s", cn
->path
));
828 /* Remove this widget from the display */
829 if(cn
->flags
& CN_DISPLAYED
) {
830 gtk_container_remove(GTK_CONTAINER(chooselayout
), cn
->container
);
831 cn
->flags
^= CN_DISPLAYED
;
833 /* Remove children too */
834 for(n
= 0; n
< cn
->children
.nvec
; ++n
)
835 undisplay_tree(cn
->children
.vec
[n
]);
838 /* Selection --------------------------------------------------------------- */
840 /** @brief Mark the widget @p cn according to its selection state */
841 static void display_selection(struct choosenode
*cn
) {
842 /* Need foreground and background colors */
843 gtk_widget_set_state(cn
->label
, (cn
->flags
& CN_SELECTED
844 ? GTK_STATE_SELECTED
: GTK_STATE_NORMAL
));
845 gtk_widget_set_state(cn
->container
, (cn
->flags
& CN_SELECTED
846 ? GTK_STATE_SELECTED
: GTK_STATE_NORMAL
));
849 /** @brief Set the selection state of a widget
851 * Directories can never be selected, we just ignore attempts to do so. */
852 static void set_selection(struct choosenode
*cn
, int selected
) {
853 unsigned f
= selected ? CN_SELECTED
: 0;
855 D(("set_selection %d %s", selected
, cn
->path
));
856 if(!(cn
->flags
& CN_EXPANDABLE
) && (cn
->flags
& CN_SELECTED
) != f
) {
857 cn
->flags
^= CN_SELECTED
;
858 /* Maintain selection count */
863 display_selection(cn
);
864 /* Update main menu sensitivity */
869 /** @brief Recursively clear all selection bits from CN down */
870 static void clear_selection(struct choosenode
*cn
) {
873 set_selection(cn
, 0);
874 for(n
= 0; n
< cn
->children
.nvec
; ++n
)
875 clear_selection(cn
->children
.vec
[n
]);
878 /* User actions ------------------------------------------------------------ */
880 /** @brief Clicked on something
882 * This implements playing, all the modifiers for selection, etc.
884 static void clicked_choosenode(GtkWidget
attribute((unused
)) *widget
,
885 GdkEventButton
*event
,
886 gpointer user_data
) {
887 struct choosenode
*cn
= user_data
;
888 int ind
, last_ind
, n
;
890 D(("clicked_choosenode %s", cn
->path
));
891 if(event
->type
== GDK_BUTTON_RELEASE
892 && event
->button
== 1) {
894 if(cn
->flags
& CN_EXPANDABLE
) {
895 /* This is a directory. Flip its expansion status. */
896 if(cn
->flags
& CN_EXPANDED
)
899 expand_node(cn
, 0/*!contingent*/);
902 /* This is a file. Adjust selection status */
903 /* TODO the basic logic here is essentially the same as that in queue.c.
904 * Can we share code at all? */
905 switch(event
->state
& (GDK_SHIFT_MASK
|GDK_CONTROL_MASK
)) {
907 clear_selection(root
);
908 set_selection(cn
, 1);
911 case GDK_CONTROL_MASK
:
912 set_selection(cn
, !(cn
->flags
& CN_SELECTED
));
916 case GDK_SHIFT_MASK
|GDK_CONTROL_MASK
:
917 if(last_click
&& last_click
->parent
== cn
->parent
) {
918 /* Figure out where the current and last clicks are in the list */
920 for(n
= 0; n
< cn
->parent
->children
.nvec
; ++n
) {
921 if(cn
->parent
->children
.vec
[n
] == cn
)
923 if(cn
->parent
->children
.vec
[n
] == last_click
)
926 /* Test shouldn't ever fail, but still */
927 if(ind
>= 0 && last_ind
>= 0) {
928 if(!(event
->state
& GDK_CONTROL_MASK
)) {
929 for(n
= 0; n
< cn
->parent
->children
.nvec
; ++n
)
930 set_selection(cn
->parent
->children
.vec
[n
], 0);
933 for(n
= last_ind
; n
<= ind
; ++n
)
934 set_selection(cn
->parent
->children
.vec
[n
], 1);
936 for(n
= ind
; n
<= last_ind
; ++n
)
937 set_selection(cn
->parent
->children
.vec
[n
], 1);
938 if(event
->state
& GDK_CONTROL_MASK
)
942 /* TODO trying to select a range that doesn't share a single parent
943 * currently does not work, but it ought to. */
947 } else if(event
->type
== GDK_BUTTON_RELEASE
948 && event
->button
== 2) {
949 /* Middle click - play the pointed track */
950 if(!(cn
->flags
& CN_EXPANDABLE
)) {
951 clear_selection(root
);
952 set_selection(cn
, 1);
953 gtk_label_set_text(GTK_LABEL(report_label
), "adding track to queue");
954 disorder_eclient_play(client
, cn
->path
, 0, 0);
957 } else if(event
->type
== GDK_BUTTON_PRESS
958 && event
->button
== 3) {
959 struct choose_menuitem
*const menuitems
=
960 (cn
->flags
& CN_EXPANDABLE ? dir_menuitems
: track_menuitems
);
961 GtkWidget
*const menu
=
962 (cn
->flags
& CN_EXPANDABLE ? dir_menu
: track_menu
);
963 /* Right click. Pop up a menu. */
964 /* If the current file isn't selected, switch the selection to just that.
965 * (If we're looking at a directory then leave the selection alone.) */
966 if(!(cn
->flags
& CN_EXPANDABLE
) && !(cn
->flags
& CN_SELECTED
)) {
967 clear_selection(root
);
968 set_selection(cn
, 1);
971 /* Set the item sensitivity and callbacks */
972 for(n
= 0; menuitems
[n
].name
; ++n
) {
973 if(menuitems
[n
].handlerid
)
974 g_signal_handler_disconnect(menuitems
[n
].w
,
975 menuitems
[n
].handlerid
);
976 gtk_widget_set_sensitive(menuitems
[n
].w
,
977 menuitems
[n
].sensitive(cn
));
978 menuitems
[n
].handlerid
= g_signal_connect
979 (menuitems
[n
].w
, "activate", G_CALLBACK(menuitems
[n
].activate
), cn
);
981 /* Pop up the menu */
982 gtk_widget_show_all(menu
);
983 gtk_menu_popup(GTK_MENU(menu
), 0, 0, 0, 0,
984 event
->button
, event
->time
);
988 /** @brief Called BY GTK+ to tell us the search entry box has changed */
989 static void searchentry_changed(GtkEditable
attribute((unused
)) *editable
,
990 gpointer
attribute((unused
)) user_data
) {
994 /* Track menu items -------------------------------------------------------- */
996 /** @brief Recursive step for gather_selected() */
997 static void recurse_selected(struct choosenode
*cn
, struct vector
*v
) {
1000 if(cn
->flags
& CN_EXPANDABLE
) {
1001 if(cn
->flags
& CN_EXPANDED
)
1002 for(n
= 0; n
< cn
->children
.nvec
; ++n
)
1003 recurse_selected(cn
->children
.vec
[n
], v
);
1005 if((cn
->flags
& CN_SELECTED
) && cn
->path
)
1006 vector_append(v
, (char *)cn
->path
);
1010 /*** @brief Get a list of all the selected tracks */
1011 static const char **gather_selected(int *ntracks
) {
1015 recurse_selected(root
, &v
);
1016 vector_terminate(&v
);
1017 if(ntracks
) *ntracks
= v
.nvec
;
1018 return (const char **)v
.vec
;
1021 /** @brief Called when the track menu's play option is activated */
1022 static void activate_track_play(GtkMenuItem
attribute((unused
)) *menuitem
,
1023 gpointer
attribute((unused
)) user_data
) {
1024 const char **tracks
= gather_selected(0);
1027 gtk_label_set_text(GTK_LABEL(report_label
), "adding track to queue");
1028 for(n
= 0; tracks
[n
]; ++n
)
1029 disorder_eclient_play(client
, tracks
[n
], 0, 0);
1032 /** @brief Called when the menu's properties option is activated */
1033 static void activate_track_properties(GtkMenuItem
attribute((unused
)) *menuitem
,
1034 gpointer
attribute((unused
)) user_data
) {
1036 const char **tracks
= gather_selected(&ntracks
);
1038 properties(ntracks
, tracks
);
1041 /** @brief Determine whether the menu's play option should be sensitive */
1042 static gboolean
sensitive_track_play(struct choosenode
attribute((unused
)) *cn
) {
1043 return (!!files_selected
1044 && (disorder_eclient_state(client
) & DISORDER_CONNECTED
));
1047 /** @brief Determine whether the menu's properties option should be sensitive */
1048 static gboolean
sensitive_track_properties(struct choosenode
attribute((unused
)) *cn
) {
1049 return !!files_selected
&& (disorder_eclient_state(client
) & DISORDER_CONNECTED
);
1052 /* Directory menu items ---------------------------------------------------- */
1054 /** @brief Return the file children of @p cn
1056 * The list is terminated by a null pointer.
1058 static const char **dir_files(struct choosenode
*cn
, int *nfiles
) {
1059 const char **files
= xcalloc(cn
->children
.nvec
+ 1, sizeof (char *));
1062 for(n
= m
= 0; n
< cn
->children
.nvec
; ++n
)
1063 if(!(cn
->children
.vec
[n
]->flags
& CN_EXPANDABLE
))
1064 files
[m
++] = cn
->children
.vec
[n
]->path
;
1066 if(nfiles
) *nfiles
= m
;
1070 static void play_dir(struct choosenode
*cn
,
1071 void attribute((unused
)) *wfu
) {
1073 const char **tracks
= dir_files(cn
, &ntracks
);
1075 gtk_label_set_text(GTK_LABEL(report_label
), "adding track to queue");
1076 for(n
= 0; n
< ntracks
; ++n
)
1077 disorder_eclient_play(client
, tracks
[n
], 0, 0);
1080 static void properties_dir(struct choosenode
*cn
,
1081 void attribute((unused
)) *wfu
) {
1083 const char **tracks
= dir_files(cn
, &ntracks
);
1085 properties(ntracks
, tracks
);
1088 static void select_dir(struct choosenode
*cn
,
1089 void attribute((unused
)) *wfu
) {
1092 clear_selection(root
);
1093 for(n
= 0; n
< cn
->children
.nvec
; ++n
)
1094 set_selection(cn
->children
.vec
[n
], 1);
1097 /** @brief Ensure @p cn is expanded and then call @p callback */
1098 static void call_with_dir(struct choosenode
*cn
,
1099 when_filled_callback
*whenfilled
,
1101 if(!(cn
->flags
& CN_EXPANDABLE
))
1102 return; /* something went wrong */
1103 if(cn
->flags
& CN_EXPANDED
)
1104 /* @p cn is already open */
1105 whenfilled(cn
, wfu
);
1107 /* @p cn is not open, arrange for the callback to go off when it is
1109 cn
->whenfilled
= whenfilled
;
1111 expand_node(cn
, 0/*not contingnet upon search*/);
1115 /** @brief Called when the directory menu's play option is activated */
1116 static void activate_dir_play(GtkMenuItem
attribute((unused
)) *menuitem
,
1117 gpointer user_data
) {
1118 struct choosenode
*const cn
= (struct choosenode
*)user_data
;
1120 call_with_dir(cn
, play_dir
, 0);
1123 /** @brief Called when the directory menu's properties option is activated */
1124 static void activate_dir_properties(GtkMenuItem
attribute((unused
)) *menuitem
,
1125 gpointer user_data
) {
1126 struct choosenode
*const cn
= (struct choosenode
*)user_data
;
1128 call_with_dir(cn
, properties_dir
, 0);
1131 /** @brief Called when the directory menu's select option is activated */
1132 static void activate_dir_select(GtkMenuItem
attribute((unused
)) *menuitem
,
1133 gpointer user_data
) {
1134 struct choosenode
*const cn
= (struct choosenode
*)user_data
;
1136 call_with_dir(cn
, select_dir
, 0);
1139 /** @brief Determine whether the directory menu's play option should be sensitive */
1140 static gboolean
sensitive_dir_play(struct choosenode
attribute((unused
)) *cn
) {
1141 return !!(disorder_eclient_state(client
) & DISORDER_CONNECTED
);
1144 /** @brief Determine whether the directory menu's properties option should be sensitive */
1145 static gboolean
sensitive_dir_properties(struct choosenode
attribute((unused
)) *cn
) {
1146 return !!(disorder_eclient_state(client
) & DISORDER_CONNECTED
);
1149 /** @brief Determine whether the directory menu's select option should be sensitive */
1150 static gboolean
sensitive_dir_select(struct choosenode
attribute((unused
)) *cn
) {
1156 /* Main menu plumbing ------------------------------------------------------ */
1158 /** @brief Determine whether the edit menu's properties option should be sensitive */
1159 static int choose_properties_sensitive(GtkWidget
attribute((unused
)) *w
) {
1160 return !!files_selected
&& (disorder_eclient_state(client
) & DISORDER_CONNECTED
);
1163 /** @brief Determine whether the edit menu's select all option should be sensitive
1165 * TODO not implemented, see also choose_selectall_activate()
1167 static int choose_selectall_sensitive(GtkWidget
attribute((unused
)) *w
) {
1171 /** @brief Called when the edit menu's properties option is activated */
1172 static void choose_properties_activate(GtkWidget
attribute((unused
)) *w
) {
1173 activate_track_properties(0, 0);
1176 /** @brief Called when the edit menu's select all option is activated
1178 * TODO not implemented, see choose_selectall_sensitive() */
1179 static void choose_selectall_activate(GtkWidget
attribute((unused
)) *w
) {
1182 /** @brief Main menu callbacks for Choose screen */
1183 static const struct tabtype tabtype_choose
= {
1184 choose_properties_sensitive
,
1185 choose_selectall_sensitive
,
1186 choose_properties_activate
,
1187 choose_selectall_activate
,
1190 /* Public entry points ----------------------------------------------------- */
1192 /** @brief Create a track choice widget */
1193 GtkWidget
*choose_widget(void) {
1195 GtkWidget
*scrolled
;
1196 GtkWidget
*vbox
, *hbox
, *clearsearch
;
1199 * +--vbox-------------------------------------------------------+
1200 * | +-hbox----------------------------------------------------+ |
1201 * | | searchentry | clearsearch | |
1202 * | +---------------------------------------------------------+ |
1203 * | +-scrolled------------------------------------------------+ |
1204 * | | +-chooselayout------------------------------------++--+ | |
1205 * | | | Tree structure is manually layed out in here ||^^| | |
1210 * | | +-------------------------------------------------++--+ | |
1211 * | | +-------------------------------------------------+ | |
1213 * | | +-------------------------------------------------+ | |
1214 * | +---------------------------------------------------------+ |
1215 * +-------------------------------------------------------------+
1218 /* Text entry box for search terms */
1220 searchentry
= gtk_entry_new();
1221 g_signal_connect(searchentry
, "changed", G_CALLBACK(searchentry_changed
), 0);
1222 gtk_tooltips_set_tip(tips
, searchentry
, "Enter search terms here; search is automatic", "");
1224 /* Cancel button to clear the search */
1226 clearsearch
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
1227 g_signal_connect(G_OBJECT(clearsearch
), "clicked",
1228 G_CALLBACK(clearsearch_clicked
), 0);
1229 gtk_tooltips_set_tip(tips
, clearsearch
, "Clear search terms", "");
1232 /* hbox packs the search box and the cancel button together on a line */
1234 hbox
= gtk_hbox_new(FALSE
/*homogeneous*/, 1/*spacing*/);
1235 gtk_box_pack_start(GTK_BOX(hbox
), searchentry
,
1236 TRUE
/*expand*/, TRUE
/*fill*/, 0/*padding*/);
1237 gtk_box_pack_end(GTK_BOX(hbox
), clearsearch
,
1238 FALSE
/*expand*/, FALSE
/*fill*/, 0/*padding*/);
1240 /* chooselayout contains the currently visible subset of the track
1243 chooselayout
= gtk_layout_new(0, 0);
1244 root
= newnode(0/*parent*/, "<root>", "All files", "",
1245 CN_EXPANDABLE
, fill_root_node
);
1246 expand_node(root
, 0); /* will call redisplay_tree */
1247 /* Create the popup menus */
1249 track_menu
= gtk_menu_new();
1250 g_signal_connect(track_menu
, "destroy", G_CALLBACK(gtk_widget_destroyed
),
1252 for(n
= 0; track_menuitems
[n
].name
; ++n
) {
1254 track_menuitems
[n
].w
=
1255 gtk_menu_item_new_with_label(track_menuitems
[n
].name
);
1256 gtk_menu_attach(GTK_MENU(track_menu
), track_menuitems
[n
].w
,
1260 dir_menu
= gtk_menu_new();
1261 g_signal_connect(dir_menu
, "destroy", G_CALLBACK(gtk_widget_destroyed
),
1263 for(n
= 0; dir_menuitems
[n
].name
; ++n
) {
1265 dir_menuitems
[n
].w
=
1266 gtk_menu_item_new_with_label(dir_menuitems
[n
].name
);
1267 gtk_menu_attach(GTK_MENU(dir_menu
), dir_menuitems
[n
].w
,
1270 /* The layout is scrollable */
1271 scrolled
= scroll_widget(GTK_WIDGET(chooselayout
), "choose");
1273 /* The scrollable layout and the search hbox go together in a vbox */
1275 vbox
= gtk_vbox_new(FALSE
/*homogenous*/, 1/*spacing*/);
1276 gtk_box_pack_start(GTK_BOX(vbox
), hbox
,
1277 FALSE
/*expand*/, FALSE
/*fill*/, 0/*padding*/);
1278 gtk_box_pack_end(GTK_BOX(vbox
), scrolled
,
1279 TRUE
/*expand*/, TRUE
/*fill*/, 0/*padding*/);
1281 g_object_set_data(G_OBJECT(vbox
), "type", (void *)&tabtype_choose
);
1285 /** @brief Called when something we care about here might have changed */
1286 void choose_update(void) {
1295 indent-tabs-mode:nil