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 */
94 int ymin
; /**< @brief least Y value */
95 int ymax
; /**< @brief greatest Y value */
98 /** @brief One item in the popup menu */
99 struct choose_menuitem
{
101 const char *name
; /**< @brief name */
104 void (*activate
)(GtkMenuItem
*menuitem
, gpointer user_data
);
105 /**< @brief Called to activate the menu item.
107 * @p user_data is the choosenode the mouse pointer is over. */
109 gboolean (*sensitive
)(struct choosenode
*cn
);
110 /* @brief Called to determine whether the menu item should be sensitive.
115 gulong handlerid
; /**< @brief signal handler ID */
116 GtkWidget
*w
; /**< @brief menu item widget */
121 static GtkWidget
*chooselayout
;
122 static GtkAdjustment
*vadjust
;
123 static GtkWidget
*searchentry
; /**< @brief search terms */
124 static struct choosenode
*root
;
125 static GtkWidget
*track_menu
; /**< @brief track popup menu */
126 static GtkWidget
*dir_menu
; /**< @brief directory popup menu */
127 static struct choosenode
*last_click
; /**< @brief last clicked node for selection */
128 static int files_visible
; /**< @brief total files visible */
129 static int files_selected
; /**< @brief total files selected */
130 static int gets_in_flight
; /**< @brief total gets in flight */
131 static int search_in_flight
; /**< @brief a search is underway */
132 static int search_obsolete
; /**< @brief the current search is void */
133 static char **searchresults
; /**< @brief search results */
134 static int nsearchresults
; /**< @brief number of results */
135 static int nsearchvisible
; /**< @brief number of search results visible */
136 static struct hash
*searchhash
; /**< @brief hash of search results */
137 struct progress_window
*spw
; /**< @brief progress window */
139 /* Forward Declarations */
141 static void clear_children(struct choosenode
*cn
);
142 static struct choosenode
*newnode(struct choosenode
*parent
,
147 void (*fill
)(struct choosenode
*));
148 static void fill_root_node(struct choosenode
*cn
);
149 static void fill_directory_node(struct choosenode
*cn
);
150 static void got_files(void *v
, int nvec
, char **vec
);
151 static void got_resolved_file(void *v
, const char *track
);
152 static void got_dirs(void *v
, int nvec
, char **vec
);
154 static void expand_node(struct choosenode
*cn
, int contingent
);
155 static void contract_node(struct choosenode
*cn
);
156 static void updated_node(struct choosenode
*cn
, int redisplay
);
158 static void display_selection(struct choosenode
*cn
);
159 static void clear_selection(struct choosenode
*cn
);
161 static void redisplay_tree(void);
162 static struct displaydata
display_tree(struct choosenode
*cn
, int x
, int y
);
163 static void undisplay_tree(struct choosenode
*cn
);
164 static void initiate_search(void);
165 static void delete_widgets(struct choosenode
*cn
);
166 static void expand_from(struct choosenode
*cn
);
168 static void clicked_choosenode(GtkWidget
attribute((unused
)) *widget
,
169 GdkEventButton
*event
,
172 static void activate_track_play(GtkMenuItem
*menuitem
, gpointer user_data
);
173 static void activate_track_properties(GtkMenuItem
*menuitem
, gpointer user_data
);
175 static gboolean
sensitive_track_play(struct choosenode
*cn
);
176 static gboolean
sensitive_track_properties(struct choosenode
*cn
);
178 static void activate_dir_play(GtkMenuItem
*menuitem
, gpointer user_data
);
179 static void activate_dir_properties(GtkMenuItem
*menuitem
, gpointer user_data
);
180 static void activate_dir_select(GtkMenuItem
*menuitem
, gpointer user_data
);
182 static gboolean
sensitive_dir_play(struct choosenode
*cn
);
183 static gboolean
sensitive_dir_properties(struct choosenode
*cn
);
184 static gboolean
sensitive_dir_select(struct choosenode
*cn
);
186 /** @brief Track menu items */
187 static struct choose_menuitem track_menuitems
[] = {
188 { "Play track", activate_track_play
, sensitive_track_play
, 0, 0 },
189 { "Track properties", activate_track_properties
, sensitive_track_properties
, 0, 0 },
193 /** @brief Directory menu items */
194 static struct choose_menuitem dir_menuitems
[] = {
195 { "Play all tracks", activate_dir_play
, sensitive_dir_play
, 0, 0 },
196 { "Track properties", activate_dir_properties
, sensitive_dir_properties
, 0, 0 },
197 { "Select all tracks", activate_dir_select
, sensitive_dir_select
, 0, 0 },
201 /* Maintaining the data structure ------------------------------------------ */
203 static char *cnflags(const struct choosenode
*cn
) {
204 unsigned f
= cn
->flags
, n
;
207 static const char *bits
[] = {
217 #define NBITS (sizeof bits / sizeof *bits)
221 dynstr_append(d
, '0');
223 for(n
= 0; n
< NBITS
; ++n
) {
224 const unsigned bit
= 1 << n
;
227 dynstr_append(d
, '|');
228 dynstr_append_string(d
, bits
[n
]);
235 dynstr_append(d
, '|');
236 sprintf(buf
, "%#x", f
);
237 dynstr_append_string(d
, buf
);
244 /** @brief Create a new node */
245 static struct choosenode
*newnode(struct choosenode
*parent
,
250 void (*fill
)(struct choosenode
*)) {
251 struct choosenode
*const n
= xmalloc(sizeof *n
);
253 D(("newnode %s %s", path
, display
));
254 if(flags
& CN_EXPANDABLE
)
260 n
->display
= display
;
263 nodevector_init(&n
->children
);
266 nodevector_append(&parent
->children
, n
);
270 /** @brief Called when a node has been filled
272 * Response for calling @c whenfilled.
274 static void filled(struct choosenode
*cn
) {
275 when_filled_callback
*const whenfilled
= cn
->whenfilled
;
279 whenfilled(cn
, cn
->wfu
);
281 if(nsearchvisible
< nsearchresults
) {
282 /* There is still search expansion work to do */
283 D(("filled %s %d/%d", cn
->path
, nsearchvisible
, nsearchresults
));
286 if(gets_in_flight
== 0 && nsearchvisible
< nsearchresults
)
290 /** @brief Fill the root */
291 static void fill_root_node(struct choosenode
*cn
) {
292 struct callbackdata
*cbd
;
294 D(("fill_root_node"));
296 /* More de-duping possible here */
297 if(cn
->flags
& CN_GETTING_ANY
)
299 gtk_label_set_text(GTK_LABEL(report_label
), "getting files");
300 cbd
= xmalloc(sizeof *cbd
);
301 cbd
->u
.choosenode
= cn
;
302 disorder_eclient_dirs(client
, got_dirs
, "", 0, cbd
);
303 cbd
= xmalloc(sizeof *cbd
);
304 cbd
->u
.choosenode
= cn
;
305 disorder_eclient_files(client
, got_files
, "", 0, cbd
);
306 cn
->flags
|= CN_GETTING_FILES
|CN_GETTING_DIRS
;
310 /** @brief Delete all the widgets owned by @p cn */
311 static void delete_cn_widgets(struct choosenode
*cn
) {
314 gtk_widget_destroy(cn
->arrow
);
319 gtk_widget_destroy(cn
->label
);
324 gtk_widget_destroy(cn
->marker
);
329 gtk_widget_destroy(cn
->hbox
);
334 gtk_widget_destroy(cn
->container
);
339 /** @brief Recursively clear all the children of @p cn
341 * All the widgets at or below @p cn are deleted. All choosenodes below
342 * it are emptied. i.e. we prune the tree at @p cn.
344 static void clear_children(struct choosenode
*cn
) {
347 D(("clear_children %s", cn
->path
));
348 /* Recursively clear subtrees */
349 for(n
= 0; n
< cn
->children
.nvec
; ++n
) {
350 clear_children(cn
->children
.vec
[n
]);
351 delete_cn_widgets(cn
->children
.vec
[n
]);
353 cn
->children
.nvec
= 0;
356 /** @brief Called with a list of files just below some node */
357 static void got_files(void *v
, int nvec
, char **vec
) {
358 struct callbackdata
*cbd
= v
;
359 struct choosenode
*cn
= cbd
->u
.choosenode
;
362 D(("got_files %d files for %s %s", nvec
, cn
->path
, cnflags(cn
)));
363 /* Complicated by the need to resolve aliases. We can save a bit of effort
364 * by re-using cbd though. */
365 cn
->flags
&= ~CN_GETTING_FILES
;
367 if((cn
->pending
= nvec
)) {
368 cn
->flags
|= CN_RESOLVING_FILES
;
369 for(n
= 0; n
< nvec
; ++n
) {
370 disorder_eclient_resolve(client
, got_resolved_file
, vec
[n
], cbd
);
374 /* If there are no files and the directories are all read by now, we're
376 if(!(cn
->flags
& CN_GETTING_ANY
))
380 /** @brief Called with an alias resolved filename */
381 static void got_resolved_file(void *v
, const char *track
) {
382 struct callbackdata
*cbd
= v
;
383 struct choosenode
*cn
= cbd
->u
.choosenode
, *file_cn
;
385 D(("resolved %s %s %d left", cn
->path
, cnflags(cn
), cn
->pending
- 1));
387 file_cn
= newnode(cn
, track
,
388 trackname_transform("track", track
, "display"),
389 trackname_transform("track", track
, "sort"),
390 0/*flags*/, 0/*fill*/);
392 /* Only bother updating when we've got the lot */
393 if(--cn
->pending
== 0) {
394 cn
->flags
&= ~CN_RESOLVING_FILES
;
396 if(!(cn
->flags
& CN_GETTING_ANY
))
401 /** @brief Called with a list of directories just below some node */
402 static void got_dirs(void *v
, int nvec
, char **vec
) {
403 struct callbackdata
*cbd
= v
;
404 struct choosenode
*cn
= cbd
->u
.choosenode
;
407 D(("got_dirs %d dirs for %s %s", nvec
, cn
->path
, cnflags(cn
)));
408 /* TODO this depends on local configuration for trackname_transform().
409 * This will work, since the defaults are now built-in, but it'll be
410 * (potentially) different to the server's configured settings.
412 * Really we want a variant of files/dirs that produces both the
413 * raw filename and the transformed name for a chosen context.
416 for(n
= 0; n
< nvec
; ++n
)
418 trackname_transform("dir", vec
[n
], "display"),
419 trackname_transform("dir", vec
[n
], "sort"),
420 CN_EXPANDABLE
, fill_directory_node
);
422 cn
->flags
&= ~CN_GETTING_DIRS
;
423 if(!(cn
->flags
& CN_GETTING_ANY
))
427 /** @brief Fill a child node */
428 static void fill_directory_node(struct choosenode
*cn
) {
429 struct callbackdata
*cbd
;
431 D(("fill_directory_node %s", cn
->path
));
433 if(cn
->flags
& CN_GETTING_ANY
)
435 assert(report_label
!= 0);
436 gtk_label_set_text(GTK_LABEL(report_label
), "getting files");
438 cbd
= xmalloc(sizeof *cbd
);
439 cbd
->u
.choosenode
= cn
;
440 disorder_eclient_dirs(client
, got_dirs
, cn
->path
, 0, cbd
);
441 cbd
= xmalloc(sizeof *cbd
);
442 cbd
->u
.choosenode
= cn
;
443 disorder_eclient_files(client
, got_files
, cn
->path
, 0, cbd
);
444 cn
->flags
|= CN_GETTING_FILES
|CN_GETTING_DIRS
;
448 /** @brief Expand a node */
449 static void expand_node(struct choosenode
*cn
, int contingent
) {
450 D(("expand_node %s %d %s", cn
->path
, contingent
, cnflags(cn
)));
451 assert(cn
->flags
& CN_EXPANDABLE
);
452 /* If node is already expanded do nothing. */
453 if(cn
->flags
& CN_EXPANDED
) return;
454 /* We mark the node as expanded and request that it fill itself. When it has
455 * completed it will called updated_node() and we can redraw at that
457 cn
->flags
|= CN_EXPANDED
;
459 cn
->flags
|= CN_CONTINGENT
;
461 cn
->flags
&= ~CN_CONTINGENT
;
462 /* If this node is not contingently expanded, mark all its parents back to
463 * the root as not contingent either, so they won't be contracted when the
464 * search results change */
466 struct choosenode
*cnp
;
468 for(cnp
= cn
->parent
; cnp
; cnp
= cnp
->parent
)
469 cnp
->flags
&= ~CN_CONTINGENT
;
471 /* TODO: visual feedback */
475 /** @brief Make sure all the search results below @p cn are expanded
476 * @param cn Node to start at
478 static void expand_from(struct choosenode
*cn
) {
481 if(nsearchvisible
== nsearchresults
)
484 /* Are any of the search tracks at/below this point? */
485 if(!(cn
== root
|| hash_find(searchhash
, cn
->path
)))
487 D(("expand_from %d/%d visible %s",
488 nsearchvisible
, nsearchresults
, cn
->path
));
489 if(cn
->flags
& CN_EXPANDABLE
) {
490 if(cn
->flags
& CN_EXPANDED
)
491 /* This node is marked as expanded already. children.nvec might be 0,
492 * indicating that expansion is still underway. We should get another
493 * callback when it is expanded. */
494 for(n
= 0; n
< cn
->children
.nvec
&& gets_in_flight
< 10; ++n
)
495 expand_from(cn
->children
.vec
[n
]);
497 /* This node is not expanded yet */
501 /* This is an actual search result */
503 progress_window_progress(spw
, nsearchvisible
, nsearchresults
);
504 if(nsearchvisible
== nsearchresults
)
505 /* This is the last track to become visible, we'll make sure it's in
506 * range so that at least one is. */
507 gtk_adjustment_clamp_page(vadjust
, cn
->ymax
, cn
->ymin
);
511 /** @brief Contract all contingently expanded nodes below @p cn */
512 static void contract_contingent(struct choosenode
*cn
) {
515 if(cn
->flags
& CN_CONTINGENT
)
518 for(n
= 0; n
< cn
->children
.nvec
; ++n
)
519 contract_contingent(cn
->children
.vec
[n
]);
522 /** @brief Contract a node */
523 static void contract_node(struct choosenode
*cn
) {
524 D(("contract_node %s", cn
->path
));
525 assert(cn
->flags
& CN_EXPANDABLE
);
526 /* If node is already contracted do nothing. */
527 if(!(cn
->flags
& CN_EXPANDED
)) return;
528 cn
->flags
&= ~(CN_EXPANDED
|CN_CONTINGENT
);
529 /* Clear selection below this node */
531 /* Zot children. We never used to do this but the result would be that over
532 * time you'd end up with the entire tree pulled into memory. If the server
533 * is over a slow network it will make interactivity slightly worse; if
534 * anyone complains we can make it an option. */
536 /* We can contract a node immediately. */
540 /** @brief qsort() callback for ordering choosenodes */
541 static int compare_choosenode(const void *av
, const void *bv
) {
542 const struct choosenode
*const *aa
= av
, *const *bb
= bv
;
543 const struct choosenode
*a
= *aa
, *b
= *bb
;
545 return compare_tracks(a
->sort
, b
->sort
,
546 a
->display
, b
->display
,
550 /** @brief Called when an expandable node is updated. */
551 static void updated_node(struct choosenode
*cn
, int redisplay
) {
552 D(("updated_node %s", cn
->path
));
553 assert(cn
->flags
& CN_EXPANDABLE
);
554 /* It might be that the node has been de-expanded since we requested the
555 * update. In that case we ignore this notification. */
556 if(!(cn
->flags
& CN_EXPANDED
)) return;
558 qsort(cn
->children
.vec
, cn
->children
.nvec
, sizeof (struct choosenode
*),
564 /* Searching --------------------------------------------------------------- */
566 /** @brief Return true if @p track is a search result */
567 static int is_search_result(const char *track
) {
568 return searchhash
&& hash_find(searchhash
, track
);
571 /** @brief Called with a list of search results
573 * This is called from eclient with a (possibly empty) list of search results,
574 * and also from initiate_seatch with an always empty list to indicate that
575 * we're not searching for anything in particular. */
576 static void search_completed(void attribute((unused
)) *v
,
577 int nvec
, char **vec
) {
581 search_in_flight
= 0;
582 /* Contract any choosenodes that were only expanded to show search
584 contract_contingent(root
);
585 if(search_obsolete
) {
586 /* This search has been obsoleted by user input since it started.
587 * Therefore we throw away the result and search again. */
591 /* Stash the search results */
593 nsearchresults
= nvec
;
595 /* Create a new search hash for fast identification of results */
596 searchhash
= hash_new(1);
597 for(n
= 0; n
< nvec
; ++n
) {
598 /* The filename itself lives in the hash */
599 hash_add(searchhash
, vec
[n
], "", HASH_INSERT_OR_REPLACE
);
600 /* So do its ancestor directories */
601 for(s
= vec
[n
] + 1; *s
; ++s
) {
604 hash_add(searchhash
, vec
[n
], "", HASH_INSERT_OR_REPLACE
);
609 /* We don't yet know that the results are visible */
612 progress_window_progress(spw
, 0, 0);
615 if(nsearchresults
> 50)
616 spw
= progress_window_new("Fetching search results");
617 /* Initiate expansion */
620 searchhash
= 0; /* for the gc */
621 redisplay_tree(); /* remove search markers */
626 /** @brief Initiate a search
628 * If a search is underway we set @ref search_obsolete and restart the search
629 * in search_completed() above.
631 static void initiate_search(void) {
634 /* Find out what the user is after */
635 terms
= xstrdup(gtk_entry_get_text(GTK_ENTRY(searchentry
)));
636 /* Strip leading and trailing space */
637 while(*terms
== ' ') ++terms
;
638 e
= terms
+ strlen(terms
);
639 while(e
> terms
&& e
[-1] == ' ') --e
;
641 /* If a search is already underway then mark it as obsolete. We'll revisit
642 * when it returns. */
643 if(search_in_flight
) {
648 /* There's still something left. Initiate the search. */
649 if(disorder_eclient_search(client
, search_completed
, terms
, 0)) {
650 /* The search terms are bad! We treat this as if there were no search
651 * terms at all. Some kind of feedback would be handy. */
652 fprintf(stderr
, "bad terms [%s]\n", terms
); /* TODO */
653 search_completed(0, 0, 0);
655 search_in_flight
= 1;
658 /* No search terms - we want to see all tracks */
659 search_completed(0, 0, 0);
663 /** @brief Called when the cancel search button is clicked */
664 static void clearsearch_clicked(GtkButton
attribute((unused
)) *button
,
665 gpointer
attribute((unused
)) userdata
) {
666 gtk_entry_set_text(GTK_ENTRY(searchentry
), "");
669 /* Display functions ------------------------------------------------------- */
671 /** @brief Delete all the widgets in the tree */
672 static void delete_widgets(struct choosenode
*cn
) {
675 delete_cn_widgets(cn
);
676 for(n
= 0; n
< cn
->children
.nvec
; ++n
)
677 delete_widgets(cn
->children
.vec
[n
]);
678 cn
->flags
&= ~(CN_DISPLAYED
|CN_SELECTED
);
682 /** @brief Update the display */
683 static void redisplay_tree(void) {
684 struct displaydata d
;
685 guint oldwidth
, oldheight
;
687 D(("redisplay_tree"));
688 /* We'll count these up empirically each time */
691 /* Correct the layout and find out how much space it uses */
692 MTAG_PUSH("display_tree");
693 d
= display_tree(root
, 0, 0);
695 /* We must set the total size or scrolling will not work (it wouldn't be hard
696 * for GtkLayout to figure it out for itself but presumably you're supposed
697 * to be able to have widgets off the edge of the layuot.)
699 * There is a problem: if we shrink the size then the part of the screen that
700 * is outside the new size but inside the old one is not updated. I think
701 * this is arguably bug in GTK+ but it's easy to force a redraw if this
702 * region is nonempty.
704 gtk_layout_get_size(GTK_LAYOUT(chooselayout
), &oldwidth
, &oldheight
);
705 if(oldwidth
> d
.width
|| oldheight
> d
.height
)
706 gtk_widget_queue_draw(chooselayout
);
707 gtk_layout_set_size(GTK_LAYOUT(chooselayout
), d
.width
, d
.height
);
708 /* Notify the main menu of any recent changes */
712 /** @brief Recursive step for redisplay_tree()
714 * Makes sure all displayed widgets from CN down exist and are in their proper
715 * place and return the maximum space used.
717 static struct displaydata
display_tree(struct choosenode
*cn
, int x
, int y
) {
720 struct displaydata d
, cd
;
724 D(("display_tree %s %d,%d", cn
->path
, x
, y
));
726 /* An expandable item contains an arrow and a text label. When you press the
727 * button it flips its expand state.
729 * A non-expandable item has just a text label and no arrow.
732 MTAG_PUSH("make_widgets_1");
733 /* Widgets need to be created */
735 cn
->hbox
= gtk_hbox_new(FALSE
, 1);
736 if(cn
->flags
& CN_EXPANDABLE
) {
738 cn
->arrow
= gtk_arrow_new(cn
->flags
& CN_EXPANDED ? GTK_ARROW_DOWN
744 if((pb
= find_image("notes.png"))) {
746 cn
->marker
= gtk_image_new_from_pixbuf(pb
);
750 MTAG_PUSH("make_widgets_2");
752 cn
->label
= gtk_label_new(cn
->display
);
754 gtk_container_add(GTK_CONTAINER(cn
->hbox
), cn
->arrow
);
755 gtk_container_add(GTK_CONTAINER(cn
->hbox
), cn
->label
);
757 gtk_container_add(GTK_CONTAINER(cn
->hbox
), cn
->marker
);
759 MTAG_PUSH("make_widgets_3");
761 cn
->container
= gtk_event_box_new();
762 gtk_container_add(GTK_CONTAINER(cn
->container
), cn
->hbox
);
763 g_signal_connect(cn
->container
, "button-release-event",
764 G_CALLBACK(clicked_choosenode
), cn
);
765 g_signal_connect(cn
->container
, "button-press-event",
766 G_CALLBACK(clicked_choosenode
), cn
);
767 g_object_ref(cn
->container
);
768 /* Show everything by default */
769 gtk_widget_show_all(cn
->container
);
772 assert(cn
->container
);
773 /* Make sure the widget name is right */
774 name
= (cn
->flags
& CN_EXPANDABLE
776 : is_search_result(cn
->path
) ?
"choose-search" : "choose");
777 gtk_widget_set_name(cn
->label
, name
);
778 gtk_widget_set_name(cn
->container
, name
);
779 /* Make sure the icon is right */
780 if(cn
->flags
& CN_EXPANDABLE
)
781 gtk_arrow_set(GTK_ARROW(cn
->arrow
),
782 cn
->flags
& CN_EXPANDED ? GTK_ARROW_DOWN
: GTK_ARROW_RIGHT
,
785 /* Make sure the queued marker is right */
786 /* TODO: doesn't always work */
787 (queued(cn
->path
) ? gtk_widget_show
: gtk_widget_hide
)(cn
->marker
);
788 /* Put the widget in the right place */
789 if(cn
->flags
& CN_DISPLAYED
)
790 gtk_layout_move(GTK_LAYOUT(chooselayout
), cn
->container
, x
, y
);
792 gtk_layout_put(GTK_LAYOUT(chooselayout
), cn
->container
, x
, y
);
793 cn
->flags
|= CN_DISPLAYED
;
794 /* Now chooselayout has a ref to the container */
795 g_object_unref(cn
->container
);
797 /* Set the widget's selection status */
798 if(!(cn
->flags
& CN_EXPANDABLE
))
799 display_selection(cn
);
800 /* Find the size used so we can get vertical positioning right. */
801 gtk_widget_size_request(cn
->container
, &req
);
802 d
.width
= x
+ req
.width
;
803 d
.height
= y
+ req
.height
;
806 if(cn
->flags
& CN_EXPANDED
) {
807 /* We'll offset children by the size of the arrow whatever it might be. */
809 gtk_widget_size_request(cn
->arrow
, &req
);
811 for(n
= 0; n
< cn
->children
.nvec
; ++n
) {
812 cd
= display_tree(cn
->children
.vec
[n
], x
+ aw
, d
.height
);
813 if(cd
.width
> d
.width
)
815 d
.height
= cd
.height
;
818 for(n
= 0; n
< cn
->children
.nvec
; ++n
)
819 undisplay_tree(cn
->children
.vec
[n
]);
821 if(!(cn
->flags
& CN_EXPANDABLE
)) {
823 if(cn
->flags
& CN_SELECTED
)
826 /* report back how much space we used */
827 D(("display_tree %s %d,%d total size %dx%d", cn
->path
, x
, y
,
832 /** @brief Remove widgets for newly hidden nodes */
833 static void undisplay_tree(struct choosenode
*cn
) {
836 D(("undisplay_tree %s", cn
->path
));
837 /* Remove this widget from the display */
838 if(cn
->flags
& CN_DISPLAYED
) {
839 gtk_container_remove(GTK_CONTAINER(chooselayout
), cn
->container
);
840 cn
->flags
^= CN_DISPLAYED
;
842 /* Remove children too */
843 for(n
= 0; n
< cn
->children
.nvec
; ++n
)
844 undisplay_tree(cn
->children
.vec
[n
]);
847 /* Selection --------------------------------------------------------------- */
849 /** @brief Mark the widget @p cn according to its selection state */
850 static void display_selection(struct choosenode
*cn
) {
851 /* Need foreground and background colors */
852 gtk_widget_set_state(cn
->label
, (cn
->flags
& CN_SELECTED
853 ? GTK_STATE_SELECTED
: GTK_STATE_NORMAL
));
854 gtk_widget_set_state(cn
->container
, (cn
->flags
& CN_SELECTED
855 ? GTK_STATE_SELECTED
: GTK_STATE_NORMAL
));
858 /** @brief Set the selection state of a widget
860 * Directories can never be selected, we just ignore attempts to do so. */
861 static void set_selection(struct choosenode
*cn
, int selected
) {
862 unsigned f
= selected ? CN_SELECTED
: 0;
864 D(("set_selection %d %s", selected
, cn
->path
));
865 if(!(cn
->flags
& CN_EXPANDABLE
) && (cn
->flags
& CN_SELECTED
) != f
) {
866 cn
->flags
^= CN_SELECTED
;
867 /* Maintain selection count */
872 display_selection(cn
);
873 /* Update main menu sensitivity */
878 /** @brief Recursively clear all selection bits from CN down */
879 static void clear_selection(struct choosenode
*cn
) {
882 set_selection(cn
, 0);
883 for(n
= 0; n
< cn
->children
.nvec
; ++n
)
884 clear_selection(cn
->children
.vec
[n
]);
887 /* User actions ------------------------------------------------------------ */
889 /** @brief Clicked on something
891 * This implements playing, all the modifiers for selection, etc.
893 static void clicked_choosenode(GtkWidget
attribute((unused
)) *widget
,
894 GdkEventButton
*event
,
895 gpointer user_data
) {
896 struct choosenode
*cn
= user_data
;
897 int ind
, last_ind
, n
;
899 D(("clicked_choosenode %s", cn
->path
));
900 if(event
->type
== GDK_BUTTON_RELEASE
901 && event
->button
== 1) {
903 if(cn
->flags
& CN_EXPANDABLE
) {
904 /* This is a directory. Flip its expansion status. */
905 if(cn
->flags
& CN_EXPANDED
)
908 expand_node(cn
, 0/*!contingent*/);
911 /* This is a file. Adjust selection status */
912 /* TODO the basic logic here is essentially the same as that in queue.c.
913 * Can we share code at all? */
914 switch(event
->state
& (GDK_SHIFT_MASK
|GDK_CONTROL_MASK
)) {
916 clear_selection(root
);
917 set_selection(cn
, 1);
920 case GDK_CONTROL_MASK
:
921 set_selection(cn
, !(cn
->flags
& CN_SELECTED
));
925 case GDK_SHIFT_MASK
|GDK_CONTROL_MASK
:
926 if(last_click
&& last_click
->parent
== cn
->parent
) {
927 /* Figure out where the current and last clicks are in the list */
929 for(n
= 0; n
< cn
->parent
->children
.nvec
; ++n
) {
930 if(cn
->parent
->children
.vec
[n
] == cn
)
932 if(cn
->parent
->children
.vec
[n
] == last_click
)
935 /* Test shouldn't ever fail, but still */
936 if(ind
>= 0 && last_ind
>= 0) {
937 if(!(event
->state
& GDK_CONTROL_MASK
)) {
938 for(n
= 0; n
< cn
->parent
->children
.nvec
; ++n
)
939 set_selection(cn
->parent
->children
.vec
[n
], 0);
942 for(n
= last_ind
; n
<= ind
; ++n
)
943 set_selection(cn
->parent
->children
.vec
[n
], 1);
945 for(n
= ind
; n
<= last_ind
; ++n
)
946 set_selection(cn
->parent
->children
.vec
[n
], 1);
947 if(event
->state
& GDK_CONTROL_MASK
)
951 /* TODO trying to select a range that doesn't share a single parent
952 * currently does not work, but it ought to. */
956 } else if(event
->type
== GDK_BUTTON_RELEASE
957 && event
->button
== 2) {
958 /* Middle click - play the pointed track */
959 if(!(cn
->flags
& CN_EXPANDABLE
)) {
960 clear_selection(root
);
961 set_selection(cn
, 1);
962 gtk_label_set_text(GTK_LABEL(report_label
), "adding track to queue");
963 disorder_eclient_play(client
, cn
->path
, 0, 0);
966 } else if(event
->type
== GDK_BUTTON_PRESS
967 && event
->button
== 3) {
968 struct choose_menuitem
*const menuitems
=
969 (cn
->flags
& CN_EXPANDABLE ? dir_menuitems
: track_menuitems
);
970 GtkWidget
*const menu
=
971 (cn
->flags
& CN_EXPANDABLE ? dir_menu
: track_menu
);
972 /* Right click. Pop up a menu. */
973 /* If the current file isn't selected, switch the selection to just that.
974 * (If we're looking at a directory then leave the selection alone.) */
975 if(!(cn
->flags
& CN_EXPANDABLE
) && !(cn
->flags
& CN_SELECTED
)) {
976 clear_selection(root
);
977 set_selection(cn
, 1);
980 /* Set the item sensitivity and callbacks */
981 for(n
= 0; menuitems
[n
].name
; ++n
) {
982 if(menuitems
[n
].handlerid
)
983 g_signal_handler_disconnect(menuitems
[n
].w
,
984 menuitems
[n
].handlerid
);
985 gtk_widget_set_sensitive(menuitems
[n
].w
,
986 menuitems
[n
].sensitive(cn
));
987 menuitems
[n
].handlerid
= g_signal_connect
988 (menuitems
[n
].w
, "activate", G_CALLBACK(menuitems
[n
].activate
), cn
);
990 /* Pop up the menu */
991 gtk_widget_show_all(menu
);
992 gtk_menu_popup(GTK_MENU(menu
), 0, 0, 0, 0,
993 event
->button
, event
->time
);
997 /** @brief Called BY GTK+ to tell us the search entry box has changed */
998 static void searchentry_changed(GtkEditable
attribute((unused
)) *editable
,
999 gpointer
attribute((unused
)) user_data
) {
1003 /* Track menu items -------------------------------------------------------- */
1005 /** @brief Recursive step for gather_selected() */
1006 static void recurse_selected(struct choosenode
*cn
, struct vector
*v
) {
1009 if(cn
->flags
& CN_EXPANDABLE
) {
1010 if(cn
->flags
& CN_EXPANDED
)
1011 for(n
= 0; n
< cn
->children
.nvec
; ++n
)
1012 recurse_selected(cn
->children
.vec
[n
], v
);
1014 if((cn
->flags
& CN_SELECTED
) && cn
->path
)
1015 vector_append(v
, (char *)cn
->path
);
1019 /*** @brief Get a list of all the selected tracks */
1020 static const char **gather_selected(int *ntracks
) {
1024 recurse_selected(root
, &v
);
1025 vector_terminate(&v
);
1026 if(ntracks
) *ntracks
= v
.nvec
;
1027 return (const char **)v
.vec
;
1030 /** @brief Called when the track menu's play option is activated */
1031 static void activate_track_play(GtkMenuItem
attribute((unused
)) *menuitem
,
1032 gpointer
attribute((unused
)) user_data
) {
1033 const char **tracks
= gather_selected(0);
1036 gtk_label_set_text(GTK_LABEL(report_label
), "adding track to queue");
1037 for(n
= 0; tracks
[n
]; ++n
)
1038 disorder_eclient_play(client
, tracks
[n
], 0, 0);
1041 /** @brief Called when the menu's properties option is activated */
1042 static void activate_track_properties(GtkMenuItem
attribute((unused
)) *menuitem
,
1043 gpointer
attribute((unused
)) user_data
) {
1045 const char **tracks
= gather_selected(&ntracks
);
1047 properties(ntracks
, tracks
);
1050 /** @brief Determine whether the menu's play option should be sensitive */
1051 static gboolean
sensitive_track_play(struct choosenode
attribute((unused
)) *cn
) {
1052 return (!!files_selected
1053 && (disorder_eclient_state(client
) & DISORDER_CONNECTED
));
1056 /** @brief Determine whether the menu's properties option should be sensitive */
1057 static gboolean
sensitive_track_properties(struct choosenode
attribute((unused
)) *cn
) {
1058 return !!files_selected
&& (disorder_eclient_state(client
) & DISORDER_CONNECTED
);
1061 /* Directory menu items ---------------------------------------------------- */
1063 /** @brief Return the file children of @p cn
1065 * The list is terminated by a null pointer.
1067 static const char **dir_files(struct choosenode
*cn
, int *nfiles
) {
1068 const char **files
= xcalloc(cn
->children
.nvec
+ 1, sizeof (char *));
1071 for(n
= m
= 0; n
< cn
->children
.nvec
; ++n
)
1072 if(!(cn
->children
.vec
[n
]->flags
& CN_EXPANDABLE
))
1073 files
[m
++] = cn
->children
.vec
[n
]->path
;
1075 if(nfiles
) *nfiles
= m
;
1079 static void play_dir(struct choosenode
*cn
,
1080 void attribute((unused
)) *wfu
) {
1082 const char **tracks
= dir_files(cn
, &ntracks
);
1084 gtk_label_set_text(GTK_LABEL(report_label
), "adding track to queue");
1085 for(n
= 0; n
< ntracks
; ++n
)
1086 disorder_eclient_play(client
, tracks
[n
], 0, 0);
1089 static void properties_dir(struct choosenode
*cn
,
1090 void attribute((unused
)) *wfu
) {
1092 const char **tracks
= dir_files(cn
, &ntracks
);
1094 properties(ntracks
, tracks
);
1097 static void select_dir(struct choosenode
*cn
,
1098 void attribute((unused
)) *wfu
) {
1101 clear_selection(root
);
1102 for(n
= 0; n
< cn
->children
.nvec
; ++n
)
1103 set_selection(cn
->children
.vec
[n
], 1);
1106 /** @brief Ensure @p cn is expanded and then call @p callback */
1107 static void call_with_dir(struct choosenode
*cn
,
1108 when_filled_callback
*whenfilled
,
1110 if(!(cn
->flags
& CN_EXPANDABLE
))
1111 return; /* something went wrong */
1112 if(cn
->flags
& CN_EXPANDED
)
1113 /* @p cn is already open */
1114 whenfilled(cn
, wfu
);
1116 /* @p cn is not open, arrange for the callback to go off when it is
1118 cn
->whenfilled
= whenfilled
;
1120 expand_node(cn
, 0/*not contingnet upon search*/);
1124 /** @brief Called when the directory menu's play option is activated */
1125 static void activate_dir_play(GtkMenuItem
attribute((unused
)) *menuitem
,
1126 gpointer user_data
) {
1127 struct choosenode
*const cn
= (struct choosenode
*)user_data
;
1129 call_with_dir(cn
, play_dir
, 0);
1132 /** @brief Called when the directory menu's properties option is activated */
1133 static void activate_dir_properties(GtkMenuItem
attribute((unused
)) *menuitem
,
1134 gpointer user_data
) {
1135 struct choosenode
*const cn
= (struct choosenode
*)user_data
;
1137 call_with_dir(cn
, properties_dir
, 0);
1140 /** @brief Called when the directory menu's select option is activated */
1141 static void activate_dir_select(GtkMenuItem
attribute((unused
)) *menuitem
,
1142 gpointer user_data
) {
1143 struct choosenode
*const cn
= (struct choosenode
*)user_data
;
1145 call_with_dir(cn
, select_dir
, 0);
1148 /** @brief Determine whether the directory menu's play option should be sensitive */
1149 static gboolean
sensitive_dir_play(struct choosenode
attribute((unused
)) *cn
) {
1150 return !!(disorder_eclient_state(client
) & DISORDER_CONNECTED
);
1153 /** @brief Determine whether the directory menu's properties option should be sensitive */
1154 static gboolean
sensitive_dir_properties(struct choosenode
attribute((unused
)) *cn
) {
1155 return !!(disorder_eclient_state(client
) & DISORDER_CONNECTED
);
1158 /** @brief Determine whether the directory menu's select option should be sensitive */
1159 static gboolean
sensitive_dir_select(struct choosenode
attribute((unused
)) *cn
) {
1165 /* Main menu plumbing ------------------------------------------------------ */
1167 /** @brief Determine whether the edit menu's properties option should be sensitive */
1168 static int choose_properties_sensitive(GtkWidget
attribute((unused
)) *w
) {
1169 return !!files_selected
&& (disorder_eclient_state(client
) & DISORDER_CONNECTED
);
1172 /** @brief Determine whether the edit menu's select all option should be sensitive
1174 * TODO not implemented, see also choose_selectall_activate()
1176 static int choose_selectall_sensitive(GtkWidget
attribute((unused
)) *w
) {
1180 /** @brief Called when the edit menu's properties option is activated */
1181 static void choose_properties_activate(GtkWidget
attribute((unused
)) *w
) {
1182 activate_track_properties(0, 0);
1185 /** @brief Called when the edit menu's select all option is activated
1187 * TODO not implemented, see choose_selectall_sensitive() */
1188 static void choose_selectall_activate(GtkWidget
attribute((unused
)) *w
) {
1191 /** @brief Main menu callbacks for Choose screen */
1192 static const struct tabtype tabtype_choose
= {
1193 choose_properties_sensitive
,
1194 choose_selectall_sensitive
,
1195 choose_properties_activate
,
1196 choose_selectall_activate
,
1199 /* Public entry points ----------------------------------------------------- */
1201 /** @brief Create a track choice widget */
1202 GtkWidget
*choose_widget(void) {
1204 GtkWidget
*scrolled
;
1205 GtkWidget
*vbox
, *hbox
, *clearsearch
;
1208 * +--vbox-------------------------------------------------------+
1209 * | +-hbox----------------------------------------------------+ |
1210 * | | searchentry | clearsearch | |
1211 * | +---------------------------------------------------------+ |
1212 * | +-scrolled------------------------------------------------+ |
1213 * | | +-chooselayout------------------------------------++--+ | |
1214 * | | | Tree structure is manually layed out in here ||^^| | |
1219 * | | +-------------------------------------------------++--+ | |
1220 * | | +-------------------------------------------------+ | |
1222 * | | +-------------------------------------------------+ | |
1223 * | +---------------------------------------------------------+ |
1224 * +-------------------------------------------------------------+
1227 /* Text entry box for search terms */
1229 searchentry
= gtk_entry_new();
1230 g_signal_connect(searchentry
, "changed", G_CALLBACK(searchentry_changed
), 0);
1231 gtk_tooltips_set_tip(tips
, searchentry
, "Enter search terms here; search is automatic", "");
1233 /* Cancel button to clear the search */
1235 clearsearch
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
1236 g_signal_connect(G_OBJECT(clearsearch
), "clicked",
1237 G_CALLBACK(clearsearch_clicked
), 0);
1238 gtk_tooltips_set_tip(tips
, clearsearch
, "Clear search terms", "");
1241 /* hbox packs the search box and the cancel button together on a line */
1243 hbox
= gtk_hbox_new(FALSE
/*homogeneous*/, 1/*spacing*/);
1244 gtk_box_pack_start(GTK_BOX(hbox
), searchentry
,
1245 TRUE
/*expand*/, TRUE
/*fill*/, 0/*padding*/);
1246 gtk_box_pack_end(GTK_BOX(hbox
), clearsearch
,
1247 FALSE
/*expand*/, FALSE
/*fill*/, 0/*padding*/);
1249 /* chooselayout contains the currently visible subset of the track
1252 chooselayout
= gtk_layout_new(0, 0);
1253 root
= newnode(0/*parent*/, "<root>", "All files", "",
1254 CN_EXPANDABLE
, fill_root_node
);
1255 expand_node(root
, 0); /* will call redisplay_tree */
1256 /* Create the popup menus */
1258 track_menu
= gtk_menu_new();
1259 g_signal_connect(track_menu
, "destroy", G_CALLBACK(gtk_widget_destroyed
),
1261 for(n
= 0; track_menuitems
[n
].name
; ++n
) {
1263 track_menuitems
[n
].w
=
1264 gtk_menu_item_new_with_label(track_menuitems
[n
].name
);
1265 gtk_menu_attach(GTK_MENU(track_menu
), track_menuitems
[n
].w
,
1269 dir_menu
= gtk_menu_new();
1270 g_signal_connect(dir_menu
, "destroy", G_CALLBACK(gtk_widget_destroyed
),
1272 for(n
= 0; dir_menuitems
[n
].name
; ++n
) {
1274 dir_menuitems
[n
].w
=
1275 gtk_menu_item_new_with_label(dir_menuitems
[n
].name
);
1276 gtk_menu_attach(GTK_MENU(dir_menu
), dir_menuitems
[n
].w
,
1279 /* The layout is scrollable */
1280 scrolled
= scroll_widget(chooselayout
, "choose");
1281 vadjust
= gtk_layout_get_vadjustment(GTK_LAYOUT(chooselayout
));
1283 /* The scrollable layout and the search hbox go together in a vbox */
1285 vbox
= gtk_vbox_new(FALSE
/*homogenous*/, 1/*spacing*/);
1286 gtk_box_pack_start(GTK_BOX(vbox
), hbox
,
1287 FALSE
/*expand*/, FALSE
/*fill*/, 0/*padding*/);
1288 gtk_box_pack_end(GTK_BOX(vbox
), scrolled
,
1289 TRUE
/*expand*/, TRUE
/*fill*/, 0/*padding*/);
1291 g_object_set_data(G_OBJECT(vbox
), "type", (void *)&tabtype_choose
);
1295 /** @brief Called when something we care about here might have changed */
1296 void choose_update(void) {
1305 indent-tabs-mode:nil