chiark / gitweb /
doxygen
[disorder] / disobedience / choose.c
... / ...
CommitLineData
1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2006, 2007 Richard Kettlewell
4 *
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.
9 *
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.
14 *
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
18 * USA
19 */
20/** @file disobedience/choose.c
21 * @brief Hierarchical track selection and search
22 *
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
25 * us.
26 */
27
28#include "disobedience.h"
29
30/* Choose track ------------------------------------------------------------ */
31
32WT(label);
33WT(event_box);
34WT(menu);
35WT(menu_item);
36WT(layout);
37WT(vbox);
38WT(arrow);
39WT(hbox);
40WT(button);
41WT(image);
42WT(entry);
43
44/* Types */
45
46struct choosenode;
47
48/** @brief Accumulated information about the tree widget */
49struct displaydata {
50 /** @brief Maximum width required */
51 guint width;
52 /** @brief Maximum height required */
53 guint height;
54};
55
56/* instantiate the node vector type */
57
58VECTOR_TYPE(nodevector, struct choosenode *, xrealloc);
59
60/** @brief One node in the virtual filesystem */
61struct choosenode {
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 */
67 unsigned flags;
68#define CN_EXPANDABLE 0x0001 /**< @brief node is expandable */
69#define CN_EXPANDED 0x0002 /**< @brief node is expanded
70 *
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 */
82};
83
84/** @brief One item in the popup menu */
85struct menuitem {
86 /* Parameters */
87 const char *name; /**< @brief name */
88
89 /* Callbacks */
90 void (*activate)(GtkMenuItem *menuitem, gpointer user_data);
91 /**< @brief Called to activate the menu item.
92 *
93 * @p user_data is the choosenode the mouse pointer is over. */
94
95 gboolean (*sensitive)(struct choosenode *cn);
96 /* @brief Called to determine whether the menu item should be sensitive.
97 *
98 * TODO? */
99
100 /* State */
101 gulong handlerid; /**< @brief signal handler ID */
102 GtkWidget *w; /**< @brief menu item widget */
103};
104
105/* Variables */
106
107static GtkWidget *chooselayout;
108static GtkWidget *searchentry; /**< @brief search terms */
109static struct choosenode *root;
110static struct choosenode *realroot;
111static GtkWidget *menu; /**< @brief our popup menu */
112static struct choosenode *last_click; /**< @brief last clicked node for selection */
113static int files_visible; /**< @brief total files visible */
114static int files_selected; /**< @brief total files selected */
115static int search_in_flight; /**< @brief a search is underway */
116static int search_obsolete; /**< @brief the current search is void */
117static char **searchresults; /**< @brief search results */
118static int nsearchresults; /**< @brief number of results */
119
120/* Forward Declarations */
121
122static void clear_children(struct choosenode *cn);
123static struct choosenode *newnode(struct choosenode *parent,
124 const char *path,
125 const char *display,
126 const char *sort,
127 unsigned flags,
128 void (*fill)(struct choosenode *));
129static void fill_root_node(struct choosenode *cn);
130static void fill_letter_node(struct choosenode *cn);
131static void fill_directory_node(struct choosenode *cn);
132static void got_files(void *v, int nvec, char **vec);
133static void got_resolved_file(void *v, const char *track);
134static void got_dirs(void *v, int nvec, char **vec);
135
136static void expand_node(struct choosenode *cn);
137static void contract_node(struct choosenode *cn);
138static void updated_node(struct choosenode *cn, int redisplay);
139
140static void display_selection(struct choosenode *cn);
141static void clear_selection(struct choosenode *cn);
142
143static void redisplay_tree(void);
144static struct displaydata display_tree(struct choosenode *cn, int x, int y);
145static void undisplay_tree(struct choosenode *cn);
146static void initiate_search(void);
147static void delete_widgets(struct choosenode *cn);
148
149static void clicked_choosenode(GtkWidget attribute((unused)) *widget,
150 GdkEventButton *event,
151 gpointer user_data);
152
153static void activate_play(GtkMenuItem *menuitem, gpointer user_data);
154#if 0
155static void activate_remove(GtkMenuItem *menuitem, gpointer user_data);
156#endif
157static void activate_properties(GtkMenuItem *menuitem, gpointer user_data);
158
159static gboolean sensitive_play(struct choosenode *cn);
160#if 0
161static gboolean sensitive_remove(struct choosenode *cn);
162#endif
163static gboolean sensitive_properties(struct choosenode *cn);
164
165/** @brief Menu items */
166static struct menuitem menuitems[] = {
167 { "Play track", activate_play, sensitive_play, 0, 0 },
168#if 0
169 /* Not implemented yet */
170 { "Remove", activate_remove, sensitive_remove, 0, 0 },
171#endif
172 { "Track properties", activate_properties, sensitive_properties, 0, 0 },
173};
174
175/** @brief Count of menu items */
176#define NMENUITEMS (int)(sizeof menuitems / sizeof *menuitems)
177
178/* Maintaining the data structure ------------------------------------------ */
179
180/** @brief Create a new node */
181static struct choosenode *newnode(struct choosenode *parent,
182 const char *path,
183 const char *display,
184 const char *sort,
185 unsigned flags,
186 void (*fill)(struct choosenode *)) {
187 struct choosenode *const n = xmalloc(sizeof *n);
188
189 D(("newnode %s %s", path, display));
190 if(flags & CN_EXPANDABLE)
191 assert(fill);
192 else
193 assert(!fill);
194 n->parent = parent;
195 n->path = path;
196 n->display = display;
197 n->sort = sort;
198 n->flags = flags;
199 nodevector_init(&n->children);
200 n->fill = fill;
201 if(parent)
202 nodevector_append(&parent->children, n);
203 return n;
204}
205
206/** @brief Fill the root */
207static void fill_root_node(struct choosenode *cn) {
208 int ch;
209 char *name;
210 struct callbackdata *cbd;
211
212 D(("fill_root_node"));
213 clear_children(cn);
214 if(choosealpha) {
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);
219 }
220 newnode(cn, "<letter>", "*", "~", CN_EXPANDABLE, fill_letter_node);
221 }
222 updated_node(cn, 1);
223 } else {
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);
232 }
233}
234
235/** @brief Delete all the widgets owned by @p cn */
236static void delete_cn_widgets(struct choosenode *cn) {
237 if(cn->arrow) {
238 DW(arrow);
239 gtk_widget_destroy(cn->arrow);
240 cn->arrow = 0;
241 }
242 if(cn->label) {
243 DW(label);
244 gtk_widget_destroy(cn->label);
245 cn->label = 0;
246 }
247 if(cn->marker) {
248 DW(image);
249 gtk_widget_destroy(cn->marker);
250 cn->marker = 0;
251 }
252 if(cn->hbox) {
253 DW(hbox);
254 gtk_widget_destroy(cn->hbox);
255 cn->hbox = 0;
256 }
257 if(cn->container) {
258 DW(event_box);
259 gtk_widget_destroy(cn->container);
260 cn->container = 0;
261 }
262}
263
264/** @brief Recursively clear all the children of @p cn
265 *
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.
268 */
269static void clear_children(struct choosenode *cn) {
270 int n;
271
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]);
277 }
278 cn->children.nvec = 0;
279}
280
281/** @brief Fill a letter node */
282static void fill_letter_node(struct choosenode *cn) {
283 const char *regexp;
284 struct callbackdata *cbd;
285
286 D(("fill_letter_node %s", cn->display));
287 switch(cn->display[0]) {
288 default:
289 byte_xasprintf((char **)&regexp, "^(the )?%c", tolower(cn->display[0]));
290 break;
291 case 'T':
292 regexp = "^(?!the [^t])t";
293 break;
294 case '*':
295 regexp = "^[^a-z]";
296 break;
297 }
298 /* TODO: caching */
299 /* TODO: de-dupe against fill_directory_node */
300 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
301 clear_children(cn);
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);
308}
309
310/** @brief Called with a list of files just below some node */
311static void got_files(void *v, int nvec, char **vec) {
312 struct callbackdata *cbd = v;
313 struct choosenode *cn = cbd->u.choosenode;
314 int n;
315
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. */
319 cn->pending = nvec;
320 for(n = 0; n < nvec; ++n)
321 disorder_eclient_resolve(client, got_resolved_file, vec[n], cbd);
322}
323
324/** @brief Called with an alias resolved filename */
325static void got_resolved_file(void *v, const char *track) {
326 struct callbackdata *cbd = v;
327 struct choosenode *cn = cbd->u.choosenode, *file_cn;
328
329 /* TODO as below */
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)
336 updated_node(cn, 1);
337}
338
339/** @brief Called with a list of directories just below some node */
340static void got_dirs(void *v, int nvec, char **vec) {
341 struct callbackdata *cbd = v;
342 struct choosenode *cn = cbd->u.choosenode;
343 int n;
344
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.
349 *
350 * Really we want a variant of files/dirs that produces both the
351 * raw filename and the transformed name for a chosen context.
352 */
353 for(n = 0; n < nvec; ++n)
354 newnode(cn, vec[n],
355 trackname_transform("dir", vec[n], "display"),
356 trackname_transform("dir", vec[n], "sort"),
357 CN_EXPANDABLE, fill_directory_node);
358 updated_node(cn, 1);
359}
360
361/** @brief Fill a child node */
362static void fill_directory_node(struct choosenode *cn) {
363 struct callbackdata *cbd;
364
365 D(("fill_directory_node %s", cn->path));
366 /* TODO: caching */
367 /* TODO: de-dupe against fill_letter_node */
368 assert(report_label != 0);
369 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
370 clear_children(cn);
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);
377}
378
379/** @brief Expand a node */
380static 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
387 * point. */
388 cn->flags |= CN_EXPANDED;
389 /* TODO: visual feedback */
390 cn->fill(cn);
391}
392
393/** @brief Contract a node */
394static 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 */
401 clear_selection(cn);
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. */
406 clear_children(cn);
407 /* We can contract a node immediately. */
408 redisplay_tree();
409}
410
411/** @brief qsort() callback for ordering choosenodes */
412static 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;
415
416 return compare_tracks(a->sort, b->sort,
417 a->display, b->display,
418 a->path, b->path);
419}
420
421/** @brief Called when an expandable node is updated. */
422static 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;
428 /* Sort children */
429 qsort(cn->children.vec, cn->children.nvec, sizeof (struct choosenode *),
430 compare_choosenode);
431 if(redisplay)
432 redisplay_tree();
433}
434
435/* Searching --------------------------------------------------------------- */
436
437/** @brief qsort() callback for ordering tracks */
438static int compare_track_for_qsort(const void *a, const void *b) {
439 return compare_path(*(char **)a, *(char **)b);
440}
441
442/** @brief Return true iff @p file is a child of @p dir */
443static int is_child(const char *dir, const char *file) {
444 const size_t dlen = strlen(dir);
445
446 return (!strncmp(file, dir, dlen)
447 && file[dlen] == '/'
448 && strchr(file + dlen + 1, '/') == 0);
449}
450
451/** @brief Return true iff @p file is a descendant of @p dir */
452static int is_descendant(const char *dir, const char *file) {
453 const size_t dlen = strlen(dir);
454
455 return !strncmp(file, dir, dlen) && file[dlen] == '/';
456}
457
458/** @brief Called to fill a node in the search results tree */
459static void fill_search_node(struct choosenode *cn) {
460 int n;
461 const size_t plen = strlen(cn->path);
462 const char *s;
463 char *dir, *last = 0;
464
465 D(("fill_search_node %s", cn->path));
466 /* We depend on the search results being sorted as by compare_path(). */
467 clear_children(cn);
468 for(n = 0; n < nsearchresults; ++n) {
469 /* We only care about descendants of CN */
470 if(!is_descendant(cn->path, searchresults[n]))
471 continue;
472 s = strchr(searchresults[n] + plen + 1, '/');
473 if(s) {
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 */
478 last = dir;
479 newnode(cn, dir,
480 trackname_transform("dir", dir, "display"),
481 trackname_transform("dir", dir, "sort"),
482 CN_EXPANDABLE, fill_search_node);
483 }
484 } else {
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*/);
490 }
491 }
492 updated_node(cn, 1);
493}
494
495/** @brief Called with a list of search results
496 *
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. */
500static void search_completed(void attribute((unused)) *v,
501 int nvec, char **vec) {
502 struct choosenode *cn;
503 int n;
504 const char *dir;
505
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. */
510 search_obsolete = 0;
511 initiate_search();
512 } else {
513 if(nvec) {
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
520 * later on) */
521 qsort(vec, nvec, sizeof(char *), compare_track_for_qsort);
522 searchresults = vec;
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
530 * of this track */
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.) */
535 updated_node(cn, 0);
536 cn = cn->parent;
537 }
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);
548 }
549 /* Finally we can insert the track as a child of the current
550 * directory */
551 newnode(cn, searchresults[n],
552 trackname_transform("track", searchresults[n], "display"),
553 trackname_transform("track", searchresults[n], "sort"),
554 0/*flags*/, 0/*fill*/);
555 }
556 while(cn) {
557 /* Update all the nodes back up to the root */
558 updated_node(cn, 0);
559 cn = cn->parent;
560 }
561 /* Now it's worth displaying the tree */
562 redisplay_tree();
563 } else if(root != realroot) {
564 delete_widgets(root);
565 root = realroot;
566 redisplay_tree();
567 }
568 }
569}
570
571/** @brief Initiate a search
572 *
573 * If a search is underway we set @ref search_obsolete and restart the search
574 * in search_completed() above.
575 */
576static void initiate_search(void) {
577 char *terms, *e;
578
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;
585 *e = 0;
586 /* If a search is already underway then mark it as obsolete. We'll revisit
587 * when it returns. */
588 if(search_in_flight) {
589 search_obsolete = 1;
590 return;
591 }
592 if(*terms) {
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);
599 } else {
600 search_in_flight = 1;
601 }
602 } else {
603 /* No search terms - we want to see all tracks */
604 search_completed(0, 0, 0);
605 }
606}
607
608/** @brief Called when the cancel search button is clicked */
609static void clearsearch_clicked(GtkButton attribute((unused)) *button,
610 gpointer attribute((unused)) userdata) {
611 gtk_entry_set_text(GTK_ENTRY(searchentry), "");
612}
613
614/* Display functions ------------------------------------------------------- */
615
616/** @brief Delete all the widgets in the tree */
617static void delete_widgets(struct choosenode *cn) {
618 int n;
619
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);
624 files_selected = 0;
625}
626
627/** @brief Update the display */
628static void redisplay_tree(void) {
629 struct displaydata d;
630 guint oldwidth, oldheight;
631
632 D(("redisplay_tree"));
633 /* We'll count these up empirically each time */
634 files_selected = 0;
635 files_visible = 0;
636 /* Correct the layout and find out how much space it uses */
637 MTAG_PUSH("display_tree");
638 d = display_tree(root, 0, 0);
639 MTAG_POP();
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.)
643 *
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.
648 */
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 */
654 menu_update(-1);
655}
656
657/** @brief Recursive step for redisplay_tree()
658 *
659 * Makes sure all displayed widgets from CN down exist and are in their proper
660 * place and return the maximum space used.
661 */
662static struct displaydata display_tree(struct choosenode *cn, int x, int y) {
663 int n, aw;
664 GtkRequisition req;
665 struct displaydata d, cd;
666 GdkPixbuf *pb;
667
668 D(("display_tree %s %d,%d", cn->path, x, y));
669
670 /* An expandable item contains an arrow and a text label. When you press the
671 * button it flips its expand state.
672 *
673 * A non-expandable item has just a text label and no arrow.
674 */
675 if(!cn->container) {
676 MTAG_PUSH("make_widgets_1");
677 /* Widgets need to be created */
678 NW(hbox);
679 cn->hbox = gtk_hbox_new(FALSE, 1);
680 if(cn->flags & CN_EXPANDABLE) {
681 NW(arrow);
682 cn->arrow = gtk_arrow_new(cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN
683 : GTK_ARROW_RIGHT,
684 GTK_SHADOW_NONE);
685 cn->marker = 0;
686 } else {
687 cn->arrow = 0;
688 if((pb = find_image("notes.png"))) {
689 NW(image);
690 cn->marker = gtk_image_new_from_pixbuf(pb);
691 }
692 }
693 MTAG_POP();
694 MTAG_PUSH("make_widgets_2");
695 NW(label);
696 cn->label = gtk_label_new(cn->display);
697 if(cn->arrow)
698 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->arrow);
699 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->label);
700 if(cn->marker)
701 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->marker);
702 MTAG_POP();
703 MTAG_PUSH("make_widgets_3");
704 NW(event_box);
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);
716 MTAG_POP();
717 }
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,
723 GTK_SHADOW_NONE);
724 else if(cn->marker)
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);
731 else {
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);
736 }
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. */
746 assert(cn->arrow);
747 gtk_widget_size_request(cn->arrow, &req);
748 aw = req.width;
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)
752 d.width = cd.width;
753 d.height = cd.height;
754 }
755 } else {
756 for(n = 0; n < cn->children.nvec; ++n)
757 undisplay_tree(cn->children.vec[n]);
758 }
759 if(!(cn->flags & CN_EXPANDABLE)) {
760 ++files_visible;
761 if(cn->flags & CN_SELECTED)
762 ++files_selected;
763 }
764 /* report back how much space we used */
765 D(("display_tree %s %d,%d total size %dx%d", cn->path, x, y,
766 d.width, d.height));
767 return d;
768}
769
770/** @brief Remove widgets for newly hidden nodes */
771static void undisplay_tree(struct choosenode *cn) {
772 int n;
773
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;
779 }
780 /* Remove children too */
781 for(n = 0; n < cn->children.nvec; ++n)
782 undisplay_tree(cn->children.vec[n]);
783}
784
785/* Selection --------------------------------------------------------------- */
786
787/** @brief Mark the widget @p cn according to its selection state */
788static 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));
794}
795
796/** @brief Set the selection state of a widget
797 *
798 * Directories can never be selected, we just ignore attempts to do so. */
799static void set_selection(struct choosenode *cn, int selected) {
800 unsigned f = selected ? CN_SELECTED : 0;
801
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 */
806 if(selected)
807 ++files_selected;
808 else
809 --files_selected;
810 display_selection(cn);
811 /* Update main menu sensitivity */
812 menu_update(-1);
813 }
814}
815
816/** @brief Recursively clear all selection bits from CN down */
817static void clear_selection(struct choosenode *cn) {
818 int n;
819
820 set_selection(cn, 0);
821 for(n = 0; n < cn->children.nvec; ++n)
822 clear_selection(cn->children.vec[n]);
823}
824
825/* User actions ------------------------------------------------------------ */
826
827/** @brief Clicked on something
828 *
829 * This implements playing, all the modifiers for selection, etc.
830 */
831static 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;
836
837 D(("clicked_choosenode %s", cn->path));
838 if(event->type == GDK_BUTTON_RELEASE
839 && event->button == 1) {
840 /* Left click */
841 if(cn->flags & CN_EXPANDABLE) {
842 /* This is a directory. Flip its expansion status. */
843 if(cn->flags & CN_EXPANDED)
844 contract_node(cn);
845 else
846 expand_node(cn);
847 last_click = 0;
848 } else {
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)) {
853 case 0:
854 clear_selection(root);
855 set_selection(cn, 1);
856 last_click = cn;
857 break;
858 case GDK_CONTROL_MASK:
859 set_selection(cn, !(cn->flags & CN_SELECTED));
860 last_click = cn;
861 break;
862 case GDK_SHIFT_MASK:
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 */
866 ind = last_ind = -1;
867 for(n = 0; n < cn->parent->children.nvec; ++n) {
868 if(cn->parent->children.vec[n] == cn)
869 ind = n;
870 if(cn->parent->children.vec[n] == last_click)
871 last_ind = n;
872 }
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);
878 }
879 if(ind > last_ind)
880 for(n = last_ind; n <= ind; ++n)
881 set_selection(cn->parent->children.vec[n], 1);
882 else
883 for(n = ind; n <= last_ind; ++n)
884 set_selection(cn->parent->children.vec[n], 1);
885 if(event->state & GDK_CONTROL_MASK)
886 last_click = cn;
887 }
888 }
889 /* TODO trying to select a range that doesn't share a single parent
890 * currently does not work, but it ought to. */
891 break;
892 }
893 }
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);
902 last_click = 0;
903 }
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);
912 last_click = cn;
913 }
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);
923 }
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);
928 }
929}
930
931/** @brief Called BY GTK+ to tell us the search entry box has changed */
932static void searchentry_changed(GtkEditable attribute((unused)) *editable,
933 gpointer attribute((unused)) user_data) {
934 initiate_search();
935}
936
937/* Menu items -------------------------------------------------------------- */
938
939/** @brief Recursive step for gather_selected() */
940static void recurse_selected(struct choosenode *cn, struct vector *v) {
941 int n;
942
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);
947 } else {
948 if((cn->flags & CN_SELECTED) && cn->path)
949 vector_append(v, (char *)cn->path);
950 }
951}
952
953/*** @brief Get a list of all the selected tracks */
954static char **gather_selected(int *ntracks) {
955 struct vector v;
956
957 vector_init(&v);
958 recurse_selected(root, &v);
959 vector_terminate(&v);
960 if(ntracks) *ntracks = v.nvec;
961 return v.vec;
962}
963
964/** @brief Called when the menu's play option is activated */
965static void activate_play(GtkMenuItem attribute((unused)) *menuitem,
966 gpointer attribute((unused)) user_data) {
967 char **tracks = gather_selected(0);
968 int n;
969
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);
973}
974
975#if 0
976static void activate_remove(GtkMenuItem attribute((unused)) *menuitem,
977 gpointer attribute((unused)) user_data) {
978 /* TODO remove all selected tracks */
979}
980#endif
981
982/** @brief Called when the menu's properties option is activated */
983static void activate_properties(GtkMenuItem attribute((unused)) *menuitem,
984 gpointer attribute((unused)) user_data) {
985 int ntracks;
986 char **tracks = gather_selected(&ntracks);
987
988 properties(ntracks, tracks);
989}
990
991/** @brief Determine whether the menu's play option should be sensitive */
992static gboolean sensitive_play(struct choosenode attribute((unused)) *cn) {
993 return (!!files_selected
994 && (disorder_eclient_state(client) & DISORDER_CONNECTED));
995}
996
997#if 0
998static gboolean sensitive_remove(struct choosenode attribute((unused)) *cn) {
999 return FALSE; /* not implemented yet */
1000}
1001#endif
1002
1003/** @brief Determine whether the menu's properties option should be sensitive */
1004static gboolean sensitive_properties(struct choosenode attribute((unused)) *cn) {
1005 return !!files_selected && (disorder_eclient_state(client) & DISORDER_CONNECTED);
1006}
1007
1008/* Main menu plumbing ------------------------------------------------------ */
1009
1010/** @brief Determine whether the edit menu's properties option should be sensitive */
1011static int choose_properties_sensitive(GtkWidget attribute((unused)) *w) {
1012 return !!files_selected && (disorder_eclient_state(client) & DISORDER_CONNECTED);
1013}
1014
1015/** @brief Determine whether the edit menu's select all option should be sensitive
1016 *
1017 * TODO not implemented, see also choose_selectall_activate()
1018 */
1019static int choose_selectall_sensitive(GtkWidget attribute((unused)) *w) {
1020 return FALSE;
1021}
1022
1023/** @brief Called when the edit menu's properties option is activated */
1024static void choose_properties_activate(GtkWidget attribute((unused)) *w) {
1025 activate_properties(0, 0);
1026}
1027
1028/** @brief Called when the edit menu's select all option is activated
1029 *
1030 * TODO not implemented, see choose_selectall_sensitive() */
1031static void choose_selectall_activate(GtkWidget attribute((unused)) *w) {
1032}
1033
1034/** @brief Main menu callbacks for Choose screen */
1035static const struct tabtype tabtype_choose = {
1036 choose_properties_sensitive,
1037 choose_selectall_sensitive,
1038 choose_properties_activate,
1039 choose_selectall_activate,
1040};
1041
1042/* Public entry points ----------------------------------------------------- */
1043
1044/** @brief Create a track choice widget */
1045GtkWidget *choose_widget(void) {
1046 int n;
1047 GtkWidget *scrolled;
1048 GtkWidget *vbox, *hbox, *clearsearch;
1049
1050 /*
1051 * +--vbox-------------------------------------------------------+
1052 * | +-hbox----------------------------------------------------+ |
1053 * | | searchentry | clearsearch | |
1054 * | +---------------------------------------------------------+ |
1055 * | +-scrolled------------------------------------------------+ |
1056 * | | +-chooselayout------------------------------------++--+ | |
1057 * | | | Tree structure is manually layed out in here ||^^| | |
1058 * | | | || | | |
1059 * | | | || | | |
1060 * | | | || | | |
1061 * | | | ||vv| | |
1062 * | | +-------------------------------------------------++--+ | |
1063 * | | +-------------------------------------------------+ | |
1064 * | | |< >| | |
1065 * | | +-------------------------------------------------+ | |
1066 * | +---------------------------------------------------------+ |
1067 * +-------------------------------------------------------------+
1068 */
1069
1070 /* Text entry box for search terms */
1071 NW(entry);
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", "");
1075
1076 /* Cancel button to clear the search */
1077 NW(button);
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", "");
1082
1083
1084 /* hbox packs the search box and the cancel button together on a line */
1085 NW(hbox);
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*/);
1091
1092 /* chooselayout contains the currently visible subset of the track
1093 * namespace */
1094 NW(layout);
1095 chooselayout = gtk_layout_new(0, 0);
1096 root = newnode(0/*parent*/, "<root>", "All files", "",
1097 CN_EXPANDABLE, fill_root_node);
1098 realroot = root;
1099 expand_node(root); /* will call redisplay_tree */
1100 /* Create the popup menu */
1101 NW(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) {
1105 NW(menu_item);
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);
1108 }
1109 /* The layout is scrollable */
1110 scrolled = scroll_widget(GTK_WIDGET(chooselayout), "choose");
1111
1112 /* The scrollable layout and the search hbox go together in a vbox */
1113 NW(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*/);
1119
1120 g_object_set_data(G_OBJECT(vbox), "type", (void *)&tabtype_choose);
1121 return vbox;
1122}
1123
1124/** @brief Called when something we care about here might have changed */
1125void choose_update(void) {
1126 redisplay_tree();
1127}
1128
1129/*
1130Local Variables:
1131c-basic-offset:2
1132comment-column:40
1133fill-column:79
1134indent-tabs-mode:nil
1135End:
1136*/