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 One node in the virtual filesystem */
62 struct choosenode
*parent
; /**< @brief parent node */
63 const char *path
; /**< @brief full path or 0 */
64 const char *sort
; /**< @brief sort key */
65 const char *display
; /**< @brief display name */
66 int pending
; /**< @brief pending resolve queries */
68 #define CN_EXPANDABLE 0x0001 /**< @brief node is expandable */
69 #define CN_EXPANDED 0x0002 /**< @brief node is expanded
71 * Expandable items are directories;
72 * non-expandable ones are files. */
73 #define CN_DISPLAYED 0x0004 /**< @brief widget is displayed in layout */
74 #define CN_SELECTED 0x0008 /**< @brief node is selected */
75 struct nodevector children
; /**< @brief vector of children */
76 void (*fill
)(struct choosenode
*); /**< @brief request child fill or 0 for leaf */
77 GtkWidget
*container
; /**< @brief the container for this row */
78 GtkWidget
*hbox
; /**< @brief the hbox for this row */
79 GtkWidget
*arrow
; /**< @brief arrow widget or 0 */
80 GtkWidget
*label
; /**< @brief text label for this node */
81 GtkWidget
*marker
; /**< @brief queued marker */
84 /** @brief One item in the popup menu */
87 const char *name
; /**< @brief name */
90 void (*activate
)(GtkMenuItem
*menuitem
, gpointer user_data
);
91 /**< @brief Called to activate the menu item.
93 * @p user_data is the choosenode the mouse pointer is over. */
95 gboolean (*sensitive
)(struct choosenode
*cn
);
96 /* @brief Called to determine whether the menu item should be sensitive.
101 gulong handlerid
; /**< @brief signal handler ID */
102 GtkWidget
*w
; /**< @brief menu item widget */
107 static GtkWidget
*chooselayout
;
108 static GtkWidget
*searchentry
; /**< @brief search terms */
109 static struct choosenode
*root
;
110 static struct choosenode
*realroot
;
111 static GtkWidget
*menu
; /**< @brief our popup menu */
112 static struct choosenode
*last_click
; /**< @brief last clicked node for selection */
113 static int files_visible
; /**< @brief total files visible */
114 static int files_selected
; /**< @brief total files selected */
115 static int search_in_flight
; /**< @brief a search is underway */
116 static int search_obsolete
; /**< @brief the current search is void */
117 static char **searchresults
; /**< @brief search results */
118 static int nsearchresults
; /**< @brief number of results */
120 /* Forward Declarations */
122 static void clear_children(struct choosenode
*cn
);
123 static struct choosenode
*newnode(struct choosenode
*parent
,
128 void (*fill
)(struct choosenode
*));
129 static void fill_root_node(struct choosenode
*cn
);
130 static void fill_letter_node(struct choosenode
*cn
);
131 static void fill_directory_node(struct choosenode
*cn
);
132 static void got_files(void *v
, int nvec
, char **vec
);
133 static void got_resolved_file(void *v
, const char *track
);
134 static void got_dirs(void *v
, int nvec
, char **vec
);
136 static void expand_node(struct choosenode
*cn
);
137 static void contract_node(struct choosenode
*cn
);
138 static void updated_node(struct choosenode
*cn
, int redisplay
);
140 static void display_selection(struct choosenode
*cn
);
141 static void clear_selection(struct choosenode
*cn
);
143 static void redisplay_tree(void);
144 static struct displaydata
display_tree(struct choosenode
*cn
, int x
, int y
);
145 static void undisplay_tree(struct choosenode
*cn
);
146 static void initiate_search(void);
147 static void delete_widgets(struct choosenode
*cn
);
149 static void clicked_choosenode(GtkWidget
attribute((unused
)) *widget
,
150 GdkEventButton
*event
,
153 static void activate_play(GtkMenuItem
*menuitem
, gpointer user_data
);
155 static void activate_remove(GtkMenuItem
*menuitem
, gpointer user_data
);
157 static void activate_properties(GtkMenuItem
*menuitem
, gpointer user_data
);
159 static gboolean
sensitive_play(struct choosenode
*cn
);
161 static gboolean
sensitive_remove(struct choosenode
*cn
);
163 static gboolean
sensitive_properties(struct choosenode
*cn
);
165 /** @brief Menu items */
166 static struct menuitem menuitems
[] = {
167 { "Play track", activate_play
, sensitive_play
, 0, 0 },
169 /* Not implemented yet */
170 { "Remove", activate_remove
, sensitive_remove
, 0, 0 },
172 { "Track properties", activate_properties
, sensitive_properties
, 0, 0 },
175 /** @brief Count of menu items */
176 #define NMENUITEMS (int)(sizeof menuitems / sizeof *menuitems)
178 /* Maintaining the data structure ------------------------------------------ */
180 /** @brief Create a new node */
181 static struct choosenode
*newnode(struct choosenode
*parent
,
186 void (*fill
)(struct choosenode
*)) {
187 struct choosenode
*const n
= xmalloc(sizeof *n
);
189 D(("newnode %s %s", path
, display
));
190 if(flags
& CN_EXPANDABLE
)
196 n
->display
= display
;
199 nodevector_init(&n
->children
);
202 nodevector_append(&parent
->children
, n
);
206 /** @brief Fill the root */
207 static void fill_root_node(struct choosenode
*cn
) {
210 struct callbackdata
*cbd
;
212 D(("fill_root_node"));
215 if(!cn
->children
.nvec
) { /* Only need to do this once */
216 for(ch
= 'A'; ch
<= 'Z'; ++ch
) {
217 byte_xasprintf(&name
, "%c", ch
);
218 newnode(cn
, "<letter>", name
, name
, CN_EXPANDABLE
, fill_letter_node
);
220 newnode(cn
, "<letter>", "*", "~", CN_EXPANDABLE
, fill_letter_node
);
224 /* More de-duping possible here */
225 gtk_label_set_text(GTK_LABEL(report_label
), "getting files");
226 cbd
= xmalloc(sizeof *cbd
);
227 cbd
->u
.choosenode
= cn
;
228 disorder_eclient_dirs(client
, got_dirs
, "", 0, cbd
);
229 cbd
= xmalloc(sizeof *cbd
);
230 cbd
->u
.choosenode
= cn
;
231 disorder_eclient_files(client
, got_files
, "", 0, cbd
);
235 /** @brief Delete all the widgets owned by @p cn */
236 static void delete_cn_widgets(struct choosenode
*cn
) {
239 gtk_widget_destroy(cn
->arrow
);
244 gtk_widget_destroy(cn
->label
);
249 gtk_widget_destroy(cn
->marker
);
254 gtk_widget_destroy(cn
->hbox
);
259 gtk_widget_destroy(cn
->container
);
264 /** @brief Recursively clear all the children of @p cn
266 * All the widgets at or below @p cn are deleted. All choosenodes below
267 * it are emptied. i.e. we prune the tree at @p cn.
269 static void clear_children(struct choosenode
*cn
) {
272 D(("clear_children %s", cn
->path
));
273 /* Recursively clear subtrees */
274 for(n
= 0; n
< cn
->children
.nvec
; ++n
) {
275 clear_children(cn
->children
.vec
[n
]);
276 delete_cn_widgets(cn
->children
.vec
[n
]);
278 cn
->children
.nvec
= 0;
281 /** @brief Fill a letter node */
282 static void fill_letter_node(struct choosenode
*cn
) {
284 struct callbackdata
*cbd
;
286 D(("fill_letter_node %s", cn
->display
));
287 switch(cn
->display
[0]) {
289 byte_xasprintf((char **)®exp
, "^(the )?%c", tolower(cn
->display
[0]));
292 regexp
= "^(?!the [^t])t";
299 /* TODO: de-dupe against fill_directory_node */
300 gtk_label_set_text(GTK_LABEL(report_label
), "getting files");
302 cbd
= xmalloc(sizeof *cbd
);
303 cbd
->u
.choosenode
= cn
;
304 disorder_eclient_dirs(client
, got_dirs
, "", regexp
, cbd
);
305 cbd
= xmalloc(sizeof *cbd
);
306 cbd
->u
.choosenode
= cn
;
307 disorder_eclient_files(client
, got_files
, "", regexp
, cbd
);
310 /** @brief Called with a list of files just below some node */
311 static void got_files(void *v
, int nvec
, char **vec
) {
312 struct callbackdata
*cbd
= v
;
313 struct choosenode
*cn
= cbd
->u
.choosenode
;
316 D(("got_files %d files for %s", nvec
, cn
->path
));
317 /* Complicated by the need to resolve aliases. We can save a bit of effort
318 * by re-using cbd though. */
320 for(n
= 0; n
< nvec
; ++n
)
321 disorder_eclient_resolve(client
, got_resolved_file
, vec
[n
], cbd
);
324 /** @brief Called with an alias resolved filename */
325 static void got_resolved_file(void *v
, const char *track
) {
326 struct callbackdata
*cbd
= v
;
327 struct choosenode
*cn
= cbd
->u
.choosenode
, *file_cn
;
330 file_cn
= newnode(cn
, track
,
331 trackname_transform("track", track
, "display"),
332 trackname_transform("track", track
, "sort"),
333 0/*flags*/, 0/*fill*/);
334 /* Only bother updating when we've got the lot */
335 if(--cn
->pending
== 0)
339 /** @brief Called with a list of directories just below some node */
340 static void got_dirs(void *v
, int nvec
, char **vec
) {
341 struct callbackdata
*cbd
= v
;
342 struct choosenode
*cn
= cbd
->u
.choosenode
;
345 D(("got_dirs %d dirs for %s", nvec
, cn
->path
));
346 /* TODO this depends on local configuration for trackname_transform().
347 * This will work, since the defaults are now built-in, but it'll be
348 * (potentially) different to the server's configured settings.
350 * Really we want a variant of files/dirs that produces both the
351 * raw filename and the transformed name for a chosen context.
353 for(n
= 0; n
< nvec
; ++n
)
355 trackname_transform("dir", vec
[n
], "display"),
356 trackname_transform("dir", vec
[n
], "sort"),
357 CN_EXPANDABLE
, fill_directory_node
);
361 /** @brief Fill a child node */
362 static void fill_directory_node(struct choosenode
*cn
) {
363 struct callbackdata
*cbd
;
365 D(("fill_directory_node %s", cn
->path
));
367 /* TODO: de-dupe against fill_letter_node */
368 assert(report_label
!= 0);
369 gtk_label_set_text(GTK_LABEL(report_label
), "getting files");
371 cbd
= xmalloc(sizeof *cbd
);
372 cbd
->u
.choosenode
= cn
;
373 disorder_eclient_dirs(client
, got_dirs
, cn
->path
, 0, cbd
);
374 cbd
= xmalloc(sizeof *cbd
);
375 cbd
->u
.choosenode
= cn
;
376 disorder_eclient_files(client
, got_files
, cn
->path
, 0, cbd
);
379 /** @brief Expand a node */
380 static void expand_node(struct choosenode
*cn
) {
381 D(("expand_node %s", cn
->path
));
382 assert(cn
->flags
& CN_EXPANDABLE
);
383 /* If node is already expanded do nothing. */
384 if(cn
->flags
& CN_EXPANDED
) return;
385 /* We mark the node as expanded and request that it fill itself. When it has
386 * completed it will called updated_node() and we can redraw at that
388 cn
->flags
|= CN_EXPANDED
;
389 /* TODO: visual feedback */
393 /** @brief Contract a node */
394 static void contract_node(struct choosenode
*cn
) {
395 D(("contract_node %s", cn
->path
));
396 assert(cn
->flags
& CN_EXPANDABLE
);
397 /* If node is already contracted do nothing. */
398 if(!(cn
->flags
& CN_EXPANDED
)) return;
399 cn
->flags
&= ~CN_EXPANDED
;
400 /* Clear selection below this node */
402 /* Zot children. We never used to do this but the result would be that over
403 * time you'd end up with the entire tree pulled into memory. If the server
404 * is over a slow network it will make interactivity slightly worse; if
405 * anyone complains we can make it an option. */
407 /* We can contract a node immediately. */
411 /** @brief qsort() callback for ordering choosenodes */
412 static int compare_choosenode(const void *av
, const void *bv
) {
413 const struct choosenode
*const *aa
= av
, *const *bb
= bv
;
414 const struct choosenode
*a
= *aa
, *b
= *bb
;
416 return compare_tracks(a
->sort
, b
->sort
,
417 a
->display
, b
->display
,
421 /** @brief Called when an expandable node is updated. */
422 static void updated_node(struct choosenode
*cn
, int redisplay
) {
423 D(("updated_node %s", cn
->path
));
424 assert(cn
->flags
& CN_EXPANDABLE
);
425 /* It might be that the node has been de-expanded since we requested the
426 * update. In that case we ignore this notification. */
427 if(!(cn
->flags
& CN_EXPANDED
)) return;
429 qsort(cn
->children
.vec
, cn
->children
.nvec
, sizeof (struct choosenode
*),
435 /* Searching --------------------------------------------------------------- */
437 /** @brief qsort() callback for ordering tracks */
438 static int compare_track_for_qsort(const void *a
, const void *b
) {
439 return compare_path(*(char **)a
, *(char **)b
);
442 /** @brief Return true iff @p file is a child of @p dir */
443 static int is_child(const char *dir
, const char *file
) {
444 const size_t dlen
= strlen(dir
);
446 return (!strncmp(file
, dir
, dlen
)
448 && strchr(file
+ dlen
+ 1, '/') == 0);
451 /** @brief Return true iff @p file is a descendant of @p dir */
452 static int is_descendant(const char *dir
, const char *file
) {
453 const size_t dlen
= strlen(dir
);
455 return !strncmp(file
, dir
, dlen
) && file
[dlen
] == '/';
458 /** @brief Called to fill a node in the search results tree */
459 static void fill_search_node(struct choosenode
*cn
) {
461 const size_t plen
= strlen(cn
->path
);
463 char *dir
, *last
= 0;
465 D(("fill_search_node %s", cn
->path
));
466 /* We depend on the search results being sorted as by compare_path(). */
468 for(n
= 0; n
< nsearchresults
; ++n
) {
469 /* We only care about descendants of CN */
470 if(!is_descendant(cn
->path
, searchresults
[n
]))
472 s
= strchr(searchresults
[n
] + plen
+ 1, '/');
474 /* We've identified a subdirectory of CN. */
475 dir
= xstrndup(searchresults
[n
], s
- searchresults
[n
]);
476 if(!last
|| strcmp(dir
, last
)) {
477 /* Not a duplicate */
480 trackname_transform("dir", dir
, "display"),
481 trackname_transform("dir", dir
, "sort"),
482 CN_EXPANDABLE
, fill_search_node
);
485 /* We've identified a file in CN */
486 newnode(cn
, searchresults
[n
],
487 trackname_transform("track", searchresults
[n
], "display"),
488 trackname_transform("track", searchresults
[n
], "sort"),
489 0/*flags*/, 0/*fill*/);
495 /** @brief Called with a list of search results
497 * This is called from eclient with a (possibly empty) list of search results,
498 * and also from initiate_seatch with an always empty list to indicate that
499 * we're not searching for anything in particular. */
500 static void search_completed(void attribute((unused
)) *v
,
501 int nvec
, char **vec
) {
502 struct choosenode
*cn
;
506 search_in_flight
= 0;
507 if(search_obsolete
) {
508 /* This search has been obsoleted by user input since it started.
509 * Therefore we throw away the result and search again. */
514 /* We will replace the choose tree with a tree structured view of search
515 * results. First we must disabled the choose tree's widgets. */
516 delete_widgets(root
);
517 /* Put the tracks into order, grouped by directory. They'll probably
518 * come back this way anyway in current versions of the server, but it's
519 * cheap not to rely on it (compared with the massive effort we expend
521 qsort(vec
, nvec
, sizeof(char *), compare_track_for_qsort
);
523 nsearchresults
= nvec
;
524 cn
= root
= newnode(0/*parent*/, "", "Search results", "",
525 CN_EXPANDABLE
|CN_EXPANDED
, fill_search_node
);
526 /* Construct the initial tree. We do this in a single pass and expand
527 * everything, so you can actually see your search results. */
528 for(n
= 0; n
< nsearchresults
; ++n
) {
529 /* Firstly we might need to go up a few directories to each an ancestor
531 while(!is_descendant(cn
->path
, searchresults
[n
])) {
532 /* We report the update on each node the last time we see it (With
533 * display=0, the main purpose of this is to get the order of the
534 * children right.) */
538 /* Secondly we might need to insert some new directories */
539 while(!is_child(cn
->path
, searchresults
[n
])) {
540 /* Figure out the subdirectory */
541 dir
= xstrndup(searchresults
[n
],
542 strchr(searchresults
[n
] + strlen(cn
->path
) + 1,
543 '/') - searchresults
[n
]);
544 cn
= newnode(cn
, dir
,
545 trackname_transform("dir", dir
, "display"),
546 trackname_transform("dir", dir
, "sort"),
547 CN_EXPANDABLE
|CN_EXPANDED
, fill_search_node
);
549 /* Finally we can insert the track as a child of the current
551 newnode(cn
, searchresults
[n
],
552 trackname_transform("track", searchresults
[n
], "display"),
553 trackname_transform("track", searchresults
[n
], "sort"),
554 0/*flags*/, 0/*fill*/);
557 /* Update all the nodes back up to the root */
561 /* Now it's worth displaying the tree */
563 } else if(root
!= realroot
) {
564 delete_widgets(root
);
571 /** @brief Initiate a search
573 * If a search is underway we set @ref search_obsolete and restart the search
574 * in search_completed() above.
576 static void initiate_search(void) {
579 /* Find out what the user is after */
580 terms
= xstrdup(gtk_entry_get_text(GTK_ENTRY(searchentry
)));
581 /* Strip leading and trailing space */
582 while(*terms
== ' ') ++terms
;
583 e
= terms
+ strlen(terms
);
584 while(e
> terms
&& e
[-1] == ' ') --e
;
586 /* If a search is already underway then mark it as obsolete. We'll revisit
587 * when it returns. */
588 if(search_in_flight
) {
593 /* There's still something left. Initiate the search. */
594 if(disorder_eclient_search(client
, search_completed
, terms
, 0)) {
595 /* The search terms are bad! We treat this as if there were no search
596 * terms at all. Some kind of feedback would be handy. */
597 fprintf(stderr
, "bad terms [%s]\n", terms
); /* TODO */
598 search_completed(0, 0, 0);
600 search_in_flight
= 1;
603 /* No search terms - we want to see all tracks */
604 search_completed(0, 0, 0);
608 /** @brief Called when the cancel search button is clicked */
609 static void clearsearch_clicked(GtkButton
attribute((unused
)) *button
,
610 gpointer
attribute((unused
)) userdata
) {
611 gtk_entry_set_text(GTK_ENTRY(searchentry
), "");
614 /* Display functions ------------------------------------------------------- */
616 /** @brief Delete all the widgets in the tree */
617 static void delete_widgets(struct choosenode
*cn
) {
620 delete_cn_widgets(cn
);
621 for(n
= 0; n
< cn
->children
.nvec
; ++n
)
622 delete_widgets(cn
->children
.vec
[n
]);
623 cn
->flags
&= ~(CN_DISPLAYED
|CN_SELECTED
);
627 /** @brief Update the display */
628 static void redisplay_tree(void) {
629 struct displaydata d
;
630 guint oldwidth
, oldheight
;
632 D(("redisplay_tree"));
633 /* We'll count these up empirically each time */
636 /* Correct the layout and find out how much space it uses */
637 MTAG_PUSH("display_tree");
638 d
= display_tree(root
, 0, 0);
640 /* We must set the total size or scrolling will not work (it wouldn't be hard
641 * for GtkLayout to figure it out for itself but presumably you're supposed
642 * to be able to have widgets off the edge of the layuot.)
644 * There is a problem: if we shrink the size then the part of the screen that
645 * is outside the new size but inside the old one is not updated. I think
646 * this is arguably bug in GTK+ but it's easy to force a redraw if this
647 * region is nonempty.
649 gtk_layout_get_size(GTK_LAYOUT(chooselayout
), &oldwidth
, &oldheight
);
650 if(oldwidth
> d
.width
|| oldheight
> d
.height
)
651 gtk_widget_queue_draw(chooselayout
);
652 gtk_layout_set_size(GTK_LAYOUT(chooselayout
), d
.width
, d
.height
);
653 /* Notify the main menu of any recent changes */
657 /** @brief Recursive step for redisplay_tree()
659 * Makes sure all displayed widgets from CN down exist and are in their proper
660 * place and return the maximum space used.
662 static struct displaydata
display_tree(struct choosenode
*cn
, int x
, int y
) {
665 struct displaydata d
, cd
;
668 D(("display_tree %s %d,%d", cn
->path
, x
, y
));
670 /* An expandable item contains an arrow and a text label. When you press the
671 * button it flips its expand state.
673 * A non-expandable item has just a text label and no arrow.
676 MTAG_PUSH("make_widgets_1");
677 /* Widgets need to be created */
679 cn
->hbox
= gtk_hbox_new(FALSE
, 1);
680 if(cn
->flags
& CN_EXPANDABLE
) {
682 cn
->arrow
= gtk_arrow_new(cn
->flags
& CN_EXPANDED ? GTK_ARROW_DOWN
688 if((pb
= find_image("notes.png"))) {
690 cn
->marker
= gtk_image_new_from_pixbuf(pb
);
694 MTAG_PUSH("make_widgets_2");
696 cn
->label
= gtk_label_new(cn
->display
);
698 gtk_container_add(GTK_CONTAINER(cn
->hbox
), cn
->arrow
);
699 gtk_container_add(GTK_CONTAINER(cn
->hbox
), cn
->label
);
701 gtk_container_add(GTK_CONTAINER(cn
->hbox
), cn
->marker
);
703 MTAG_PUSH("make_widgets_3");
705 cn
->container
= gtk_event_box_new();
706 gtk_container_add(GTK_CONTAINER(cn
->container
), cn
->hbox
);
707 g_signal_connect(cn
->container
, "button-release-event",
708 G_CALLBACK(clicked_choosenode
), cn
);
709 g_signal_connect(cn
->container
, "button-press-event",
710 G_CALLBACK(clicked_choosenode
), cn
);
711 g_object_ref(cn
->container
);
712 gtk_widget_set_name(cn
->label
, "choose");
713 gtk_widget_set_name(cn
->container
, "choose");
714 /* Show everything by default */
715 gtk_widget_show_all(cn
->container
);
718 assert(cn
->container
);
719 /* Make sure the icon is right */
720 if(cn
->flags
& CN_EXPANDABLE
)
721 gtk_arrow_set(GTK_ARROW(cn
->arrow
),
722 cn
->flags
& CN_EXPANDED ? GTK_ARROW_DOWN
: GTK_ARROW_RIGHT
,
725 /* Make sure the queued marker is right */
726 /* TODO: doesn't always work */
727 (queued(cn
->path
) ? gtk_widget_show
: gtk_widget_hide
)(cn
->marker
);
728 /* Put the widget in the right place */
729 if(cn
->flags
& CN_DISPLAYED
)
730 gtk_layout_move(GTK_LAYOUT(chooselayout
), cn
->container
, x
, y
);
732 gtk_layout_put(GTK_LAYOUT(chooselayout
), cn
->container
, x
, y
);
733 cn
->flags
|= CN_DISPLAYED
;
734 /* Now chooselayout has a ref to the container */
735 g_object_unref(cn
->container
);
737 /* Set the widget's selection status */
738 if(!(cn
->flags
& CN_EXPANDABLE
))
739 display_selection(cn
);
740 /* Find the size used so we can get vertical positioning right. */
741 gtk_widget_size_request(cn
->container
, &req
);
742 d
.width
= x
+ req
.width
;
743 d
.height
= y
+ req
.height
;
744 if(cn
->flags
& CN_EXPANDED
) {
745 /* We'll offset children by the size of the arrow whatever it might be. */
747 gtk_widget_size_request(cn
->arrow
, &req
);
749 for(n
= 0; n
< cn
->children
.nvec
; ++n
) {
750 cd
= display_tree(cn
->children
.vec
[n
], x
+ aw
, d
.height
);
751 if(cd
.width
> d
.width
)
753 d
.height
= cd
.height
;
756 for(n
= 0; n
< cn
->children
.nvec
; ++n
)
757 undisplay_tree(cn
->children
.vec
[n
]);
759 if(!(cn
->flags
& CN_EXPANDABLE
)) {
761 if(cn
->flags
& CN_SELECTED
)
764 /* report back how much space we used */
765 D(("display_tree %s %d,%d total size %dx%d", cn
->path
, x
, y
,
770 /** @brief Remove widgets for newly hidden nodes */
771 static void undisplay_tree(struct choosenode
*cn
) {
774 D(("undisplay_tree %s", cn
->path
));
775 /* Remove this widget from the display */
776 if(cn
->flags
& CN_DISPLAYED
) {
777 gtk_container_remove(GTK_CONTAINER(chooselayout
), cn
->container
);
778 cn
->flags
^= CN_DISPLAYED
;
780 /* Remove children too */
781 for(n
= 0; n
< cn
->children
.nvec
; ++n
)
782 undisplay_tree(cn
->children
.vec
[n
]);
785 /* Selection --------------------------------------------------------------- */
787 /** @brief Mark the widget @p cn according to its selection state */
788 static void display_selection(struct choosenode
*cn
) {
789 /* Need foreground and background colors */
790 gtk_widget_set_state(cn
->label
, (cn
->flags
& CN_SELECTED
791 ? GTK_STATE_SELECTED
: GTK_STATE_NORMAL
));
792 gtk_widget_set_state(cn
->container
, (cn
->flags
& CN_SELECTED
793 ? GTK_STATE_SELECTED
: GTK_STATE_NORMAL
));
796 /** @brief Set the selection state of a widget
798 * Directories can never be selected, we just ignore attempts to do so. */
799 static void set_selection(struct choosenode
*cn
, int selected
) {
800 unsigned f
= selected ? CN_SELECTED
: 0;
802 D(("set_selection %d %s", selected
, cn
->path
));
803 if(!(cn
->flags
& CN_EXPANDABLE
) && (cn
->flags
& CN_SELECTED
) != f
) {
804 cn
->flags
^= CN_SELECTED
;
805 /* Maintain selection count */
810 display_selection(cn
);
811 /* Update main menu sensitivity */
816 /** @brief Recursively clear all selection bits from CN down */
817 static void clear_selection(struct choosenode
*cn
) {
820 set_selection(cn
, 0);
821 for(n
= 0; n
< cn
->children
.nvec
; ++n
)
822 clear_selection(cn
->children
.vec
[n
]);
825 /* User actions ------------------------------------------------------------ */
827 /** @brief Clicked on something
829 * This implements playing, all the modifiers for selection, etc.
831 static void clicked_choosenode(GtkWidget
attribute((unused
)) *widget
,
832 GdkEventButton
*event
,
833 gpointer user_data
) {
834 struct choosenode
*cn
= user_data
;
835 int ind
, last_ind
, n
;
837 D(("clicked_choosenode %s", cn
->path
));
838 if(event
->type
== GDK_BUTTON_RELEASE
839 && event
->button
== 1) {
841 if(cn
->flags
& CN_EXPANDABLE
) {
842 /* This is a directory. Flip its expansion status. */
843 if(cn
->flags
& CN_EXPANDED
)
849 /* This is a file. Adjust selection status */
850 /* TODO the basic logic here is essentially the same as that in queue.c.
851 * Can we share code at all? */
852 switch(event
->state
& (GDK_SHIFT_MASK
|GDK_CONTROL_MASK
)) {
854 clear_selection(root
);
855 set_selection(cn
, 1);
858 case GDK_CONTROL_MASK
:
859 set_selection(cn
, !(cn
->flags
& CN_SELECTED
));
863 case GDK_SHIFT_MASK
|GDK_CONTROL_MASK
:
864 if(last_click
&& last_click
->parent
== cn
->parent
) {
865 /* Figure out where the current and last clicks are in the list */
867 for(n
= 0; n
< cn
->parent
->children
.nvec
; ++n
) {
868 if(cn
->parent
->children
.vec
[n
] == cn
)
870 if(cn
->parent
->children
.vec
[n
] == last_click
)
873 /* Test shouldn't ever fail, but still */
874 if(ind
>= 0 && last_ind
>= 0) {
875 if(!(event
->state
& GDK_CONTROL_MASK
)) {
876 for(n
= 0; n
< cn
->parent
->children
.nvec
; ++n
)
877 set_selection(cn
->parent
->children
.vec
[n
], 0);
880 for(n
= last_ind
; n
<= ind
; ++n
)
881 set_selection(cn
->parent
->children
.vec
[n
], 1);
883 for(n
= ind
; n
<= last_ind
; ++n
)
884 set_selection(cn
->parent
->children
.vec
[n
], 1);
885 if(event
->state
& GDK_CONTROL_MASK
)
889 /* TODO trying to select a range that doesn't share a single parent
890 * currently does not work, but it ought to. */
894 } else if(event
->type
== GDK_BUTTON_RELEASE
895 && event
->button
== 2) {
896 /* Middle click - play the pointed track */
897 if(!(cn
->flags
& CN_EXPANDABLE
)) {
898 clear_selection(root
);
899 set_selection(cn
, 1);
900 gtk_label_set_text(GTK_LABEL(report_label
), "adding track to queue");
901 disorder_eclient_play(client
, cn
->path
, 0, 0);
904 } else if(event
->type
== GDK_BUTTON_PRESS
905 && event
->button
== 3) {
906 /* Right click. Pop up a menu. */
907 /* If the current file isn't selected, switch the selection to just that.
908 * (If we're looking at a directory then leave the selection alone.) */
909 if(!(cn
->flags
& CN_EXPANDABLE
) && !(cn
->flags
& CN_SELECTED
)) {
910 clear_selection(root
);
911 set_selection(cn
, 1);
914 /* Set the item sensitivity and callbacks */
915 for(n
= 0; n
< NMENUITEMS
; ++n
) {
916 if(menuitems
[n
].handlerid
)
917 g_signal_handler_disconnect(menuitems
[n
].w
,
918 menuitems
[n
].handlerid
);
919 gtk_widget_set_sensitive(menuitems
[n
].w
,
920 menuitems
[n
].sensitive(cn
));
921 menuitems
[n
].handlerid
= g_signal_connect
922 (menuitems
[n
].w
, "activate", G_CALLBACK(menuitems
[n
].activate
), cn
);
924 /* Pop up the menu */
925 gtk_widget_show_all(menu
);
926 gtk_menu_popup(GTK_MENU(menu
), 0, 0, 0, 0,
927 event
->button
, event
->time
);
931 /** @brief Called BY GTK+ to tell us the search entry box has changed */
932 static void searchentry_changed(GtkEditable
attribute((unused
)) *editable
,
933 gpointer
attribute((unused
)) user_data
) {
937 /* Menu items -------------------------------------------------------------- */
939 /** @brief Recursive step for gather_selected() */
940 static void recurse_selected(struct choosenode
*cn
, struct vector
*v
) {
943 if(cn
->flags
& CN_EXPANDABLE
) {
944 if(cn
->flags
& CN_EXPANDED
)
945 for(n
= 0; n
< cn
->children
.nvec
; ++n
)
946 recurse_selected(cn
->children
.vec
[n
], v
);
948 if((cn
->flags
& CN_SELECTED
) && cn
->path
)
949 vector_append(v
, (char *)cn
->path
);
953 /*** @brief Get a list of all the selected tracks */
954 static char **gather_selected(int *ntracks
) {
958 recurse_selected(root
, &v
);
959 vector_terminate(&v
);
960 if(ntracks
) *ntracks
= v
.nvec
;
964 /** @brief Called when the menu's play option is activated */
965 static void activate_play(GtkMenuItem
attribute((unused
)) *menuitem
,
966 gpointer
attribute((unused
)) user_data
) {
967 char **tracks
= gather_selected(0);
970 gtk_label_set_text(GTK_LABEL(report_label
), "adding track to queue");
971 for(n
= 0; tracks
[n
]; ++n
)
972 disorder_eclient_play(client
, tracks
[n
], 0, 0);
976 static void activate_remove(GtkMenuItem
attribute((unused
)) *menuitem
,
977 gpointer
attribute((unused
)) user_data
) {
978 /* TODO remove all selected tracks */
982 /** @brief Called when the menu's properties option is activated */
983 static void activate_properties(GtkMenuItem
attribute((unused
)) *menuitem
,
984 gpointer
attribute((unused
)) user_data
) {
986 char **tracks
= gather_selected(&ntracks
);
988 properties(ntracks
, tracks
);
991 /** @brief Determine whether the menu's play option should be sensitive */
992 static gboolean
sensitive_play(struct choosenode
attribute((unused
)) *cn
) {
993 return (!!files_selected
994 && (disorder_eclient_state(client
) & DISORDER_CONNECTED
));
998 static gboolean
sensitive_remove(struct choosenode
attribute((unused
)) *cn
) {
999 return FALSE
; /* not implemented yet */
1003 /** @brief Determine whether the menu's properties option should be sensitive */
1004 static gboolean
sensitive_properties(struct choosenode
attribute((unused
)) *cn
) {
1005 return !!files_selected
&& (disorder_eclient_state(client
) & DISORDER_CONNECTED
);
1008 /* Main menu plumbing ------------------------------------------------------ */
1010 /** @brief Determine whether the edit menu's properties option should be sensitive */
1011 static int choose_properties_sensitive(GtkWidget
attribute((unused
)) *w
) {
1012 return !!files_selected
&& (disorder_eclient_state(client
) & DISORDER_CONNECTED
);
1015 /** @brief Determine whether the edit menu's select all option should be sensitive
1017 * TODO not implemented, see also choose_selectall_activate()
1019 static int choose_selectall_sensitive(GtkWidget
attribute((unused
)) *w
) {
1023 /** @brief Called when the edit menu's properties option is activated */
1024 static void choose_properties_activate(GtkWidget
attribute((unused
)) *w
) {
1025 activate_properties(0, 0);
1028 /** @brief Called when the edit menu's select all option is activated
1030 * TODO not implemented, see choose_selectall_sensitive() */
1031 static void choose_selectall_activate(GtkWidget
attribute((unused
)) *w
) {
1034 /** @brief Main menu callbacks for Choose screen */
1035 static const struct tabtype tabtype_choose
= {
1036 choose_properties_sensitive
,
1037 choose_selectall_sensitive
,
1038 choose_properties_activate
,
1039 choose_selectall_activate
,
1042 /* Public entry points ----------------------------------------------------- */
1044 /** @brief Create a track choice widget */
1045 GtkWidget
*choose_widget(void) {
1047 GtkWidget
*scrolled
;
1048 GtkWidget
*vbox
, *hbox
, *clearsearch
;
1051 * +--vbox-------------------------------------------------------+
1052 * | +-hbox----------------------------------------------------+ |
1053 * | | searchentry | clearsearch | |
1054 * | +---------------------------------------------------------+ |
1055 * | +-scrolled------------------------------------------------+ |
1056 * | | +-chooselayout------------------------------------++--+ | |
1057 * | | | Tree structure is manually layed out in here ||^^| | |
1062 * | | +-------------------------------------------------++--+ | |
1063 * | | +-------------------------------------------------+ | |
1065 * | | +-------------------------------------------------+ | |
1066 * | +---------------------------------------------------------+ |
1067 * +-------------------------------------------------------------+
1070 /* Text entry box for search terms */
1072 searchentry
= gtk_entry_new();
1073 g_signal_connect(searchentry
, "changed", G_CALLBACK(searchentry_changed
), 0);
1074 gtk_tooltips_set_tip(tips
, searchentry
, "Enter search terms here; search is automatic", "");
1076 /* Cancel button to clear the search */
1078 clearsearch
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
1079 g_signal_connect(G_OBJECT(clearsearch
), "clicked",
1080 G_CALLBACK(clearsearch_clicked
), 0);
1081 gtk_tooltips_set_tip(tips
, clearsearch
, "Clear search terms", "");
1084 /* hbox packs the search box and the cancel button together on a line */
1086 hbox
= gtk_hbox_new(FALSE
/*homogeneous*/, 1/*spacing*/);
1087 gtk_box_pack_start(GTK_BOX(hbox
), searchentry
,
1088 TRUE
/*expand*/, TRUE
/*fill*/, 0/*padding*/);
1089 gtk_box_pack_end(GTK_BOX(hbox
), clearsearch
,
1090 FALSE
/*expand*/, FALSE
/*fill*/, 0/*padding*/);
1092 /* chooselayout contains the currently visible subset of the track
1095 chooselayout
= gtk_layout_new(0, 0);
1096 root
= newnode(0/*parent*/, "<root>", "All files", "",
1097 CN_EXPANDABLE
, fill_root_node
);
1099 expand_node(root
); /* will call redisplay_tree */
1100 /* Create the popup menu */
1102 menu
= gtk_menu_new();
1103 g_signal_connect(menu
, "destroy", G_CALLBACK(gtk_widget_destroyed
), &menu
);
1104 for(n
= 0; n
< NMENUITEMS
; ++n
) {
1106 menuitems
[n
].w
= gtk_menu_item_new_with_label(menuitems
[n
].name
);
1107 gtk_menu_attach(GTK_MENU(menu
), menuitems
[n
].w
, 0, 1, n
, n
+ 1);
1109 /* The layout is scrollable */
1110 scrolled
= scroll_widget(GTK_WIDGET(chooselayout
), "choose");
1112 /* The scrollable layout and the search hbox go together in a vbox */
1114 vbox
= gtk_vbox_new(FALSE
/*homogenous*/, 1/*spacing*/);
1115 gtk_box_pack_start(GTK_BOX(vbox
), hbox
,
1116 FALSE
/*expand*/, FALSE
/*fill*/, 0/*padding*/);
1117 gtk_box_pack_end(GTK_BOX(vbox
), scrolled
,
1118 TRUE
/*expand*/, TRUE
/*fill*/, 0/*padding*/);
1120 g_object_set_data(G_OBJECT(vbox
), "type", (void *)&tabtype_choose
);
1124 /** @brief Called when something we care about here might have changed */
1125 void choose_update(void) {
1134 indent-tabs-mode:nil