chiark / gitweb /
doxygen
[disorder] / disobedience / choose.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
eb525fcd 3 * Copyright (C) 2006, 2007 Richard Kettlewell
460b9539 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 */
b9bdafc7
RK
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 */
460b9539 27
28#include "disobedience.h"
29
30/* Choose track ------------------------------------------------------------ */
31
e9b70a84 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
460b9539 44/* Types */
45
46struct choosenode;
47
b9bdafc7 48/** @brief Accumulated information about the tree widget */
460b9539 49struct displaydata {
b9bdafc7
RK
50 /** @brief Maximum width required */
51 guint width;
52 /** @brief Maximum height required */
53 guint height;
460b9539 54};
55
56/* instantiate the node vector type */
b8956e9e
RK
57
58VECTOR_TYPE(nodevector, struct choosenode *, xrealloc);
460b9539 59
b9bdafc7 60/** @brief One node in the virtual filesystem */
460b9539 61struct choosenode {
b9bdafc7
RK
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 */
460b9539 67 unsigned flags;
b9bdafc7
RK
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 */
460b9539 82};
83
b9bdafc7 84/** @brief One item in the popup menu */
460b9539 85struct menuitem {
86 /* Parameters */
b9bdafc7 87 const char *name; /**< @brief name */
460b9539 88
89 /* Callbacks */
90 void (*activate)(GtkMenuItem *menuitem, gpointer user_data);
b9bdafc7
RK
91 /**< @brief Called to activate the menu item.
92 *
93 * @p user_data is the choosenode the mouse pointer is over. */
460b9539 94
95 gboolean (*sensitive)(struct choosenode *cn);
b9bdafc7
RK
96 /* @brief Called to determine whether the menu item should be sensitive.
97 *
98 * TODO? */
460b9539 99
100 /* State */
b9bdafc7
RK
101 gulong handlerid; /**< @brief signal handler ID */
102 GtkWidget *w; /**< @brief menu item widget */
460b9539 103};
104
105/* Variables */
106
107static GtkWidget *chooselayout;
b9bdafc7 108static GtkWidget *searchentry; /**< @brief search terms */
460b9539 109static struct choosenode *root;
110static struct choosenode *realroot;
b9bdafc7
RK
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 */
460b9539 119
120/* Forward Declarations */
121
77d95881 122static void clear_children(struct choosenode *cn);
460b9539 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);
39fe1014 154#if 0
460b9539 155static void activate_remove(GtkMenuItem *menuitem, gpointer user_data);
39fe1014 156#endif
460b9539 157static void activate_properties(GtkMenuItem *menuitem, gpointer user_data);
158
159static gboolean sensitive_play(struct choosenode *cn);
39fe1014 160#if 0
460b9539 161static gboolean sensitive_remove(struct choosenode *cn);
39fe1014 162#endif
460b9539 163static gboolean sensitive_properties(struct choosenode *cn);
164
b9bdafc7 165/** @brief Menu items */
460b9539 166static struct menuitem menuitems[] = {
39fe1014 167 { "Play track", activate_play, sensitive_play, 0, 0 },
168#if 0
169 /* Not implemented yet */
460b9539 170 { "Remove", activate_remove, sensitive_remove, 0, 0 },
39fe1014 171#endif
172 { "Track properties", activate_properties, sensitive_properties, 0, 0 },
460b9539 173};
174
b9bdafc7 175/** @brief Count of menu items */
460b9539 176#define NMENUITEMS (int)(sizeof menuitems / sizeof *menuitems)
177
178/* Maintaining the data structure ------------------------------------------ */
179
b9bdafc7 180/** @brief Create a new node */
460b9539 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
b9bdafc7 206/** @brief Fill the root */
460b9539 207static void fill_root_node(struct choosenode *cn) {
208 int ch;
209 char *name;
210 struct callbackdata *cbd;
211
212 D(("fill_root_node"));
77d95881 213 clear_children(cn);
460b9539 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
b9bdafc7 235/** @brief Delete all the widgets owned by @p cn */
e9b70a84 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
b9bdafc7
RK
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 */
460b9539 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]);
e9b70a84 276 delete_cn_widgets(cn->children.vec[n]);
460b9539 277 }
278 cn->children.nvec = 0;
279}
280
b9bdafc7 281/** @brief Fill a letter node */
460b9539 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
b9bdafc7 310/** @brief Called with a list of files just below some node */
460b9539 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
b9bdafc7 324/** @brief Called with an alias resolved filename */
460b9539 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
b9bdafc7 329 /* TODO as below */
460b9539 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
b9bdafc7 339/** @brief Called with a list of directories just below some node */
460b9539 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));
b9bdafc7
RK
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 */
460b9539 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
b9bdafc7 361/** @brief Fill a child node */
460b9539 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");
1d0c28c7 370 clear_children(cn);
460b9539 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
b9bdafc7 379/** @brief Expand a node */
460b9539 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
b9bdafc7 393/** @brief Contract a node */
460b9539 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);
e9b70a84 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);
460b9539 407 /* We can contract a node immediately. */
408 redisplay_tree();
409}
410
b9bdafc7 411/** @brief qsort() callback for ordering choosenodes */
460b9539 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
b9bdafc7 421/** @brief Called when an expandable node is updated. */
460b9539 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
b9bdafc7 437/** @brief qsort() callback for ordering tracks */
460b9539 438static int compare_track_for_qsort(const void *a, const void *b) {
439 return compare_path(*(char **)a, *(char **)b);
440}
441
b9bdafc7 442/** @brief Return true iff @p file is a child of @p dir */
460b9539 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
b9bdafc7 451/** @brief Return true iff @p file is a descendant of @p dir */
460b9539 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
b9bdafc7 458/** @brief Called to fill a node in the search results tree */
460b9539 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(). */
9e9fc4ae 467 clear_children(cn);
460b9539 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
b9bdafc7
RK
495/** @brief Called with a list of search results
496 *
497 * This is called from eclient with a (possibly empty) list of search results,
460b9539 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
b9bdafc7
RK
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 */
460b9539 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
b9bdafc7 608/** @brief Called when the cancel search button is clicked */
460b9539 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
b9bdafc7 616/** @brief Delete all the widgets in the tree */
460b9539 617static void delete_widgets(struct choosenode *cn) {
618 int n;
619
e9b70a84 620 delete_cn_widgets(cn);
460b9539 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
b9bdafc7 627/** @brief Update the display */
460b9539 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 */
e1d4a32b 637 MTAG_PUSH("display_tree");
460b9539 638 d = display_tree(root, 0, 0);
e1d4a32b 639 MTAG_POP();
460b9539 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
b9bdafc7
RK
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 */
460b9539 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) {
521b8841 676 MTAG_PUSH("make_widgets_1");
460b9539 677 /* Widgets need to be created */
e9b70a84 678 NW(hbox);
460b9539 679 cn->hbox = gtk_hbox_new(FALSE, 1);
680 if(cn->flags & CN_EXPANDABLE) {
e9b70a84 681 NW(arrow);
460b9539 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;
e9b70a84 688 if((pb = find_image("notes.png"))) {
689 NW(image);
460b9539 690 cn->marker = gtk_image_new_from_pixbuf(pb);
e9b70a84 691 }
460b9539 692 }
521b8841 693 MTAG_POP();
694 MTAG_PUSH("make_widgets_2");
e9b70a84 695 NW(label);
460b9539 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);
521b8841 702 MTAG_POP();
703 MTAG_PUSH("make_widgets_3");
e9b70a84 704 NW(event_box);
460b9539 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);
e1d4a32b 716 MTAG_POP();
460b9539 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;
521b8841 734 /* Now chooselayout has a ref to the container */
735 g_object_unref(cn->container);
460b9539 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
b9bdafc7 770/** @brief Remove widgets for newly hidden nodes */
460b9539 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
b9bdafc7 787/** @brief Mark the widget @p cn according to its selection state */
460b9539 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
b9bdafc7
RK
796/** @brief Set the selection state of a widget
797 *
798 * Directories can never be selected, we just ignore attempts to do so. */
460b9539 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
b9bdafc7 816/** @brief Recursively clear all selection bits from CN down */
460b9539 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
b9bdafc7
RK
827/** @brief Clicked on something
828 *
829 * This implements playing, all the modifiers for selection, etc.
830 */
460b9539 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 }
b9bdafc7
RK
889 /* TODO trying to select a range that doesn't share a single parent
890 * currently does not work, but it ought to. */
460b9539 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
b9bdafc7 931/** @brief Called BY GTK+ to tell us the search entry box has changed */
460b9539 932static void searchentry_changed(GtkEditable attribute((unused)) *editable,
933 gpointer attribute((unused)) user_data) {
934 initiate_search();
935}
936
937/* Menu items -------------------------------------------------------------- */
938
b9bdafc7 939/** @brief Recursive step for gather_selected() */
460b9539 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
b9bdafc7 953/*** @brief Get a list of all the selected tracks */
460b9539 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
8a80dbab 964/** @brief Called when the menu's play option is activated */
460b9539 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
39fe1014 975#if 0
460b9539 976static void activate_remove(GtkMenuItem attribute((unused)) *menuitem,
977 gpointer attribute((unused)) user_data) {
978 /* TODO remove all selected tracks */
979}
39fe1014 980#endif
460b9539 981
b9bdafc7 982/** @brief Called when the menu's properties option is activated */
460b9539 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
b9bdafc7 991/** @brief Determine whether the menu's play option should be sensitive */
460b9539 992static gboolean sensitive_play(struct choosenode attribute((unused)) *cn) {
8f763f1b
RK
993 return (!!files_selected
994 && (disorder_eclient_state(client) & DISORDER_CONNECTED));
460b9539 995}
996
39fe1014 997#if 0
460b9539 998static gboolean sensitive_remove(struct choosenode attribute((unused)) *cn) {
999 return FALSE; /* not implemented yet */
1000}
39fe1014 1001#endif
460b9539 1002
b9bdafc7 1003/** @brief Determine whether the menu's properties option should be sensitive */
460b9539 1004static gboolean sensitive_properties(struct choosenode attribute((unused)) *cn) {
8f763f1b 1005 return !!files_selected && (disorder_eclient_state(client) & DISORDER_CONNECTED);
460b9539 1006}
1007
1008/* Main menu plumbing ------------------------------------------------------ */
1009
b9bdafc7 1010/** @brief Determine whether the edit menu's properties option should be sensitive */
460b9539 1011static int choose_properties_sensitive(GtkWidget attribute((unused)) *w) {
8f763f1b 1012 return !!files_selected && (disorder_eclient_state(client) & DISORDER_CONNECTED);
460b9539 1013}
1014
b9bdafc7
RK
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 */
460b9539 1019static int choose_selectall_sensitive(GtkWidget attribute((unused)) *w) {
b9bdafc7 1020 return FALSE;
460b9539 1021}
1022
b9bdafc7 1023/** @brief Called when the edit menu's properties option is activated */
460b9539 1024static void choose_properties_activate(GtkWidget attribute((unused)) *w) {
1025 activate_properties(0, 0);
1026}
1027
b9bdafc7
RK
1028/** @brief Called when the edit menu's select all option is activated
1029 *
1030 * TODO not implemented, see choose_selectall_sensitive() */
460b9539 1031static void choose_selectall_activate(GtkWidget attribute((unused)) *w) {
460b9539 1032}
1033
b9bdafc7 1034/** @brief Main menu callbacks for Choose screen */
460b9539 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
b9bdafc7 1044/** @brief Create a track choice widget */
460b9539 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 */
e9b70a84 1071 NW(entry);
460b9539 1072 searchentry = gtk_entry_new();
1073 g_signal_connect(searchentry, "changed", G_CALLBACK(searchentry_changed), 0);
a8c9507f 1074 gtk_tooltips_set_tip(tips, searchentry, "Enter search terms here; search is automatic", "");
460b9539 1075
1076 /* Cancel button to clear the search */
e9b70a84 1077 NW(button);
460b9539 1078 clearsearch = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
1079 g_signal_connect(G_OBJECT(clearsearch), "clicked",
1080 G_CALLBACK(clearsearch_clicked), 0);
a8c9507f
RK
1081 gtk_tooltips_set_tip(tips, clearsearch, "Clear search terms", "");
1082
460b9539 1083
1084 /* hbox packs the search box and the cancel button together on a line */
e9b70a84 1085 NW(hbox);
460b9539 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 */
e9b70a84 1094 NW(layout);
460b9539 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 */
e9b70a84 1101 NW(menu);
460b9539 1102 menu = gtk_menu_new();
1103 g_signal_connect(menu, "destroy", G_CALLBACK(gtk_widget_destroyed), &menu);
1104 for(n = 0; n < NMENUITEMS; ++n) {
e9b70a84 1105 NW(menu_item);
460b9539 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 */
e9b70a84 1113 NW(vbox);
460b9539 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
b9bdafc7 1124/** @brief Called when something we care about here might have changed */
460b9539 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*/