chiark / gitweb /
speaker process terminating is fatal
[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 */
20
21#include "disobedience.h"
22
23/* Choose track ------------------------------------------------------------ */
24
e9b70a84 25WT(label);
26WT(event_box);
27WT(menu);
28WT(menu_item);
29WT(layout);
30WT(vbox);
31WT(arrow);
32WT(hbox);
33WT(button);
34WT(image);
35WT(entry);
36
460b9539 37/* We don't use the built-in tree widgets because they require that you know
38 * the children of a node on demand, and we have to wait for the server to tell
39 * us. */
40
41/* Types */
42
43struct choosenode;
44
45struct displaydata {
46 guint width; /* total width required */
47 guint height; /* total height required */
48};
49
50/* instantiate the node vector type */
b8956e9e
RK
51
52VECTOR_TYPE(nodevector, struct choosenode *, xrealloc);
460b9539 53
54struct choosenode {
55 struct choosenode *parent; /* parent node */
56 const char *path; /* full path or 0 */
57 const char *sort; /* sort key */
58 const char *display; /* display name */
59 int pending; /* pending resolve queries */
60 unsigned flags;
61#define CN_EXPANDABLE 0x0001 /* node is expandable */
62#define CN_EXPANDED 0x0002 /* node is expanded */
63/* Expandable items are directories; non-expandable ones are files */
64#define CN_DISPLAYED 0x0004 /* widget is displayed in layout */
65#define CN_SELECTED 0x0008 /* node is selected */
66 struct nodevector children; /* vector of children */
67 void (*fill)(struct choosenode *); /* request child fill or 0 for leaf */
68 GtkWidget *container; /* the container for this row */
69 GtkWidget *hbox; /* the hbox for this row */
70 GtkWidget *arrow; /* arrow widget or 0 */
71 GtkWidget *label; /* text label for this node */
72 GtkWidget *marker; /* queued marker */
73};
74
75struct menuitem {
76 /* Parameters */
77 const char *name; /* name */
78
79 /* Callbacks */
80 void (*activate)(GtkMenuItem *menuitem, gpointer user_data);
81 /* Called to activate the menu item. The user data is the choosenode the
82 * pointer is over. */
83
84 gboolean (*sensitive)(struct choosenode *cn);
85 /* Called to determine whether the menu item should be sensitive. TODO */
86
87 /* State */
88 gulong handlerid; /* signal handler ID */
89 GtkWidget *w; /* menu item widget */
90};
91
92/* Variables */
93
94static GtkWidget *chooselayout;
95static GtkWidget *searchentry; /* search terms */
96static struct choosenode *root;
97static struct choosenode *realroot;
98static GtkWidget *menu; /* our popup menu */
99static struct choosenode *last_click; /* last clicked node for selection */
100static int files_visible; /* total files visible */
101static int files_selected; /* total files selected */
102static int search_in_flight; /* a search is underway */
103static int search_obsolete; /* the current search is void */
104static char **searchresults; /* search results */
105static int nsearchresults; /* number of results */
106
107/* Forward Declarations */
108
77d95881 109static void clear_children(struct choosenode *cn);
460b9539 110static struct choosenode *newnode(struct choosenode *parent,
111 const char *path,
112 const char *display,
113 const char *sort,
114 unsigned flags,
115 void (*fill)(struct choosenode *));
116static void fill_root_node(struct choosenode *cn);
117static void fill_letter_node(struct choosenode *cn);
118static void fill_directory_node(struct choosenode *cn);
119static void got_files(void *v, int nvec, char **vec);
120static void got_resolved_file(void *v, const char *track);
121static void got_dirs(void *v, int nvec, char **vec);
122
123static void expand_node(struct choosenode *cn);
124static void contract_node(struct choosenode *cn);
125static void updated_node(struct choosenode *cn, int redisplay);
126
127static void display_selection(struct choosenode *cn);
128static void clear_selection(struct choosenode *cn);
129
130static void redisplay_tree(void);
131static struct displaydata display_tree(struct choosenode *cn, int x, int y);
132static void undisplay_tree(struct choosenode *cn);
133static void initiate_search(void);
134static void delete_widgets(struct choosenode *cn);
135
136static void clicked_choosenode(GtkWidget attribute((unused)) *widget,
137 GdkEventButton *event,
138 gpointer user_data);
139
140static void activate_play(GtkMenuItem *menuitem, gpointer user_data);
39fe1014 141#if 0
460b9539 142static void activate_remove(GtkMenuItem *menuitem, gpointer user_data);
39fe1014 143#endif
460b9539 144static void activate_properties(GtkMenuItem *menuitem, gpointer user_data);
145
146static gboolean sensitive_play(struct choosenode *cn);
39fe1014 147#if 0
460b9539 148static gboolean sensitive_remove(struct choosenode *cn);
39fe1014 149#endif
460b9539 150static gboolean sensitive_properties(struct choosenode *cn);
151
152static struct menuitem menuitems[] = {
39fe1014 153 { "Play track", activate_play, sensitive_play, 0, 0 },
154#if 0
155 /* Not implemented yet */
460b9539 156 { "Remove", activate_remove, sensitive_remove, 0, 0 },
39fe1014 157#endif
158 { "Track properties", activate_properties, sensitive_properties, 0, 0 },
460b9539 159};
160
161#define NMENUITEMS (int)(sizeof menuitems / sizeof *menuitems)
162
163/* Maintaining the data structure ------------------------------------------ */
164
165/* Create a new node */
166static struct choosenode *newnode(struct choosenode *parent,
167 const char *path,
168 const char *display,
169 const char *sort,
170 unsigned flags,
171 void (*fill)(struct choosenode *)) {
172 struct choosenode *const n = xmalloc(sizeof *n);
173
174 D(("newnode %s %s", path, display));
175 if(flags & CN_EXPANDABLE)
176 assert(fill);
177 else
178 assert(!fill);
179 n->parent = parent;
180 n->path = path;
181 n->display = display;
182 n->sort = sort;
183 n->flags = flags;
184 nodevector_init(&n->children);
185 n->fill = fill;
186 if(parent)
187 nodevector_append(&parent->children, n);
188 return n;
189}
190
191/* Fill the root */
192static void fill_root_node(struct choosenode *cn) {
193 int ch;
194 char *name;
195 struct callbackdata *cbd;
196
197 D(("fill_root_node"));
77d95881 198 clear_children(cn);
460b9539 199 if(choosealpha) {
200 if(!cn->children.nvec) { /* Only need to do this once */
201 for(ch = 'A'; ch <= 'Z'; ++ch) {
202 byte_xasprintf(&name, "%c", ch);
203 newnode(cn, "<letter>", name, name, CN_EXPANDABLE, fill_letter_node);
204 }
205 newnode(cn, "<letter>", "*", "~", CN_EXPANDABLE, fill_letter_node);
206 }
207 updated_node(cn, 1);
208 } else {
209 /* More de-duping possible here */
210 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
211 cbd = xmalloc(sizeof *cbd);
212 cbd->u.choosenode = cn;
213 disorder_eclient_dirs(client, got_dirs, "", 0, cbd);
214 cbd = xmalloc(sizeof *cbd);
215 cbd->u.choosenode = cn;
216 disorder_eclient_files(client, got_files, "", 0, cbd);
217 }
218}
219
e9b70a84 220static void delete_cn_widgets(struct choosenode *cn) {
221 if(cn->arrow) {
222 DW(arrow);
223 gtk_widget_destroy(cn->arrow);
224 cn->arrow = 0;
225 }
226 if(cn->label) {
227 DW(label);
228 gtk_widget_destroy(cn->label);
229 cn->label = 0;
230 }
231 if(cn->marker) {
232 DW(image);
233 gtk_widget_destroy(cn->marker);
234 cn->marker = 0;
235 }
236 if(cn->hbox) {
237 DW(hbox);
238 gtk_widget_destroy(cn->hbox);
239 cn->hbox = 0;
240 }
241 if(cn->container) {
242 DW(event_box);
243 gtk_widget_destroy(cn->container);
244 cn->container = 0;
245 }
246}
247
460b9539 248/* Clear all the children of CN */
249static void clear_children(struct choosenode *cn) {
250 int n;
251
252 D(("clear_children %s", cn->path));
253 /* Recursively clear subtrees */
254 for(n = 0; n < cn->children.nvec; ++n) {
255 clear_children(cn->children.vec[n]);
e9b70a84 256 delete_cn_widgets(cn->children.vec[n]);
460b9539 257 }
258 cn->children.nvec = 0;
259}
260
261/* Fill a child node */
262static void fill_letter_node(struct choosenode *cn) {
263 const char *regexp;
264 struct callbackdata *cbd;
265
266 D(("fill_letter_node %s", cn->display));
267 switch(cn->display[0]) {
268 default:
269 byte_xasprintf((char **)&regexp, "^(the )?%c", tolower(cn->display[0]));
270 break;
271 case 'T':
272 regexp = "^(?!the [^t])t";
273 break;
274 case '*':
275 regexp = "^[^a-z]";
276 break;
277 }
278 /* TODO: caching */
279 /* TODO: de-dupe against fill_directory_node */
280 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
281 clear_children(cn);
282 cbd = xmalloc(sizeof *cbd);
283 cbd->u.choosenode = cn;
284 disorder_eclient_dirs(client, got_dirs, "", regexp, cbd);
285 cbd = xmalloc(sizeof *cbd);
286 cbd->u.choosenode = cn;
287 disorder_eclient_files(client, got_files, "", regexp, cbd);
288}
289
290/* Called with a list of files just below some node */
291static void got_files(void *v, int nvec, char **vec) {
292 struct callbackdata *cbd = v;
293 struct choosenode *cn = cbd->u.choosenode;
294 int n;
295
296 D(("got_files %d files for %s", nvec, cn->path));
297 /* Complicated by the need to resolve aliases. We can save a bit of effort
298 * by re-using cbd though. */
299 cn->pending = nvec;
300 for(n = 0; n < nvec; ++n)
301 disorder_eclient_resolve(client, got_resolved_file, vec[n], cbd);
302}
303
304static void got_resolved_file(void *v, const char *track) {
305 struct callbackdata *cbd = v;
306 struct choosenode *cn = cbd->u.choosenode, *file_cn;
307
308 file_cn = newnode(cn, track,
309 trackname_transform("track", track, "display"),
310 trackname_transform("track", track, "sort"),
311 0/*flags*/, 0/*fill*/);
312 /* Only bother updating when we've got the lot */
313 if(--cn->pending == 0)
314 updated_node(cn, 1);
315}
316
317/* Called with a list of directories just below some node */
318static void got_dirs(void *v, int nvec, char **vec) {
319 struct callbackdata *cbd = v;
320 struct choosenode *cn = cbd->u.choosenode;
321 int n;
322
323 D(("got_dirs %d dirs for %s", nvec, cn->path));
324 for(n = 0; n < nvec; ++n)
325 newnode(cn, vec[n],
326 trackname_transform("dir", vec[n], "display"),
327 trackname_transform("dir", vec[n], "sort"),
328 CN_EXPANDABLE, fill_directory_node);
329 updated_node(cn, 1);
330}
331
332/* Fill a child node */
333static void fill_directory_node(struct choosenode *cn) {
334 struct callbackdata *cbd;
335
336 D(("fill_directory_node %s", cn->path));
337 /* TODO: caching */
338 /* TODO: de-dupe against fill_letter_node */
339 assert(report_label != 0);
340 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
1d0c28c7 341 clear_children(cn);
460b9539 342 cbd = xmalloc(sizeof *cbd);
343 cbd->u.choosenode = cn;
344 disorder_eclient_dirs(client, got_dirs, cn->path, 0, cbd);
345 cbd = xmalloc(sizeof *cbd);
346 cbd->u.choosenode = cn;
347 disorder_eclient_files(client, got_files, cn->path, 0, cbd);
348}
349
350/* Expand a node */
351static void expand_node(struct choosenode *cn) {
352 D(("expand_node %s", cn->path));
353 assert(cn->flags & CN_EXPANDABLE);
354 /* If node is already expanded do nothing. */
355 if(cn->flags & CN_EXPANDED) return;
356 /* We mark the node as expanded and request that it fill itself. When it has
357 * completed it will called updated_node() and we can redraw at that
358 * point. */
359 cn->flags |= CN_EXPANDED;
360 /* TODO: visual feedback */
361 cn->fill(cn);
362}
363
364/* Contract a node */
365static void contract_node(struct choosenode *cn) {
366 D(("contract_node %s", cn->path));
367 assert(cn->flags & CN_EXPANDABLE);
368 /* If node is already contracted do nothing. */
369 if(!(cn->flags & CN_EXPANDED)) return;
370 cn->flags &= ~CN_EXPANDED;
371 /* Clear selection below this node */
372 clear_selection(cn);
e9b70a84 373 /* Zot children. We never used to do this but the result would be that over
374 * time you'd end up with the entire tree pulled into memory. If the server
375 * is over a slow network it will make interactivity slightly worse; if
376 * anyone complains we can make it an option. */
377 clear_children(cn);
460b9539 378 /* We can contract a node immediately. */
379 redisplay_tree();
380}
381
382/* qsort callback for ordering choosenodes */
383static int compare_choosenode(const void *av, const void *bv) {
384 const struct choosenode *const *aa = av, *const *bb = bv;
385 const struct choosenode *a = *aa, *b = *bb;
386
387 return compare_tracks(a->sort, b->sort,
388 a->display, b->display,
389 a->path, b->path);
390}
391
392/* Called when an expandable node is updated. */
393static void updated_node(struct choosenode *cn, int redisplay) {
394 D(("updated_node %s", cn->path));
395 assert(cn->flags & CN_EXPANDABLE);
396 /* It might be that the node has been de-expanded since we requested the
397 * update. In that case we ignore this notification. */
398 if(!(cn->flags & CN_EXPANDED)) return;
399 /* Sort children */
400 qsort(cn->children.vec, cn->children.nvec, sizeof (struct choosenode *),
401 compare_choosenode);
402 if(redisplay)
403 redisplay_tree();
404}
405
406/* Searching --------------------------------------------------------------- */
407
408static int compare_track_for_qsort(const void *a, const void *b) {
409 return compare_path(*(char **)a, *(char **)b);
410}
411
412/* Return true iff FILE is a child of DIR */
413static int is_child(const char *dir, const char *file) {
414 const size_t dlen = strlen(dir);
415
416 return (!strncmp(file, dir, dlen)
417 && file[dlen] == '/'
418 && strchr(file + dlen + 1, '/') == 0);
419}
420
421/* Return true iff FILE is a descendant of DIR */
422static int is_descendant(const char *dir, const char *file) {
423 const size_t dlen = strlen(dir);
424
425 return !strncmp(file, dir, dlen) && file[dlen] == '/';
426}
427
428/* Called to fill a node in the search results tree */
429static void fill_search_node(struct choosenode *cn) {
430 int n;
431 const size_t plen = strlen(cn->path);
432 const char *s;
433 char *dir, *last = 0;
434
435 D(("fill_search_node %s", cn->path));
436 /* We depend on the search results being sorted as by compare_path(). */
9e9fc4ae 437 clear_children(cn);
460b9539 438 for(n = 0; n < nsearchresults; ++n) {
439 /* We only care about descendants of CN */
440 if(!is_descendant(cn->path, searchresults[n]))
441 continue;
442 s = strchr(searchresults[n] + plen + 1, '/');
443 if(s) {
444 /* We've identified a subdirectory of CN. */
445 dir = xstrndup(searchresults[n], s - searchresults[n]);
446 if(!last || strcmp(dir, last)) {
447 /* Not a duplicate */
448 last = dir;
449 newnode(cn, dir,
450 trackname_transform("dir", dir, "display"),
451 trackname_transform("dir", dir, "sort"),
452 CN_EXPANDABLE, fill_search_node);
453 }
454 } else {
455 /* We've identified a file in CN */
456 newnode(cn, searchresults[n],
457 trackname_transform("track", searchresults[n], "display"),
458 trackname_transform("track", searchresults[n], "sort"),
459 0/*flags*/, 0/*fill*/);
460 }
461 }
462 updated_node(cn, 1);
463}
464
465/* This is called from eclient with a (possibly empty) list of search results,
466 * and also from initiate_seatch with an always empty list to indicate that
467 * we're not searching for anything in particular. */
468static void search_completed(void attribute((unused)) *v,
469 int nvec, char **vec) {
470 struct choosenode *cn;
471 int n;
472 const char *dir;
473
474 search_in_flight = 0;
475 if(search_obsolete) {
476 /* This search has been obsoleted by user input since it started.
477 * Therefore we throw away the result and search again. */
478 search_obsolete = 0;
479 initiate_search();
480 } else {
481 if(nvec) {
482 /* We will replace the choose tree with a tree structured view of search
483 * results. First we must disabled the choose tree's widgets. */
484 delete_widgets(root);
485 /* Put the tracks into order, grouped by directory. They'll probably
486 * come back this way anyway in current versions of the server, but it's
487 * cheap not to rely on it (compared with the massive effort we expend
488 * later on) */
489 qsort(vec, nvec, sizeof(char *), compare_track_for_qsort);
490 searchresults = vec;
491 nsearchresults = nvec;
492 cn = root = newnode(0/*parent*/, "", "Search results", "",
493 CN_EXPANDABLE|CN_EXPANDED, fill_search_node);
494 /* Construct the initial tree. We do this in a single pass and expand
495 * everything, so you can actually see your search results. */
496 for(n = 0; n < nsearchresults; ++n) {
497 /* Firstly we might need to go up a few directories to each an ancestor
498 * of this track */
499 while(!is_descendant(cn->path, searchresults[n])) {
500 /* We report the update on each node the last time we see it (With
501 * display=0, the main purpose of this is to get the order of the
502 * children right.) */
503 updated_node(cn, 0);
504 cn = cn->parent;
505 }
506 /* Secondly we might need to insert some new directories */
507 while(!is_child(cn->path, searchresults[n])) {
508 /* Figure out the subdirectory */
509 dir = xstrndup(searchresults[n],
510 strchr(searchresults[n] + strlen(cn->path) + 1,
511 '/') - searchresults[n]);
512 cn = newnode(cn, dir,
513 trackname_transform("dir", dir, "display"),
514 trackname_transform("dir", dir, "sort"),
515 CN_EXPANDABLE|CN_EXPANDED, fill_search_node);
516 }
517 /* Finally we can insert the track as a child of the current
518 * directory */
519 newnode(cn, searchresults[n],
520 trackname_transform("track", searchresults[n], "display"),
521 trackname_transform("track", searchresults[n], "sort"),
522 0/*flags*/, 0/*fill*/);
523 }
524 while(cn) {
525 /* Update all the nodes back up to the root */
526 updated_node(cn, 0);
527 cn = cn->parent;
528 }
529 /* Now it's worth displaying the tree */
530 redisplay_tree();
531 } else if(root != realroot) {
532 delete_widgets(root);
533 root = realroot;
534 redisplay_tree();
535 }
536 }
537}
538
539static void initiate_search(void) {
540 char *terms, *e;
541
542 /* Find out what the user is after */
543 terms = xstrdup(gtk_entry_get_text(GTK_ENTRY(searchentry)));
544 /* Strip leading and trailing space */
545 while(*terms == ' ') ++terms;
546 e = terms + strlen(terms);
547 while(e > terms && e[-1] == ' ') --e;
548 *e = 0;
549 /* If a search is already underway then mark it as obsolete. We'll revisit
550 * when it returns. */
551 if(search_in_flight) {
552 search_obsolete = 1;
553 return;
554 }
555 if(*terms) {
556 /* There's still something left. Initiate the search. */
557 if(disorder_eclient_search(client, search_completed, terms, 0)) {
558 /* The search terms are bad! We treat this as if there were no search
559 * terms at all. Some kind of feedback would be handy. */
560 fprintf(stderr, "bad terms [%s]\n", terms); /* TODO */
561 search_completed(0, 0, 0);
562 } else {
563 search_in_flight = 1;
564 }
565 } else {
566 /* No search terms - we want to see all tracks */
567 search_completed(0, 0, 0);
568 }
569}
570
571/* Called when the cancel search button is clicked */
572static void clearsearch_clicked(GtkButton attribute((unused)) *button,
573 gpointer attribute((unused)) userdata) {
574 gtk_entry_set_text(GTK_ENTRY(searchentry), "");
575}
576
577/* Display functions ------------------------------------------------------- */
578
579/* Delete all the widgets in the tree */
580static void delete_widgets(struct choosenode *cn) {
581 int n;
582
e9b70a84 583 delete_cn_widgets(cn);
460b9539 584 for(n = 0; n < cn->children.nvec; ++n)
585 delete_widgets(cn->children.vec[n]);
586 cn->flags &= ~(CN_DISPLAYED|CN_SELECTED);
587 files_selected = 0;
588}
589
590/* Update the display */
591static void redisplay_tree(void) {
592 struct displaydata d;
593 guint oldwidth, oldheight;
594
595 D(("redisplay_tree"));
596 /* We'll count these up empirically each time */
597 files_selected = 0;
598 files_visible = 0;
599 /* Correct the layout and find out how much space it uses */
e1d4a32b 600 MTAG_PUSH("display_tree");
460b9539 601 d = display_tree(root, 0, 0);
e1d4a32b 602 MTAG_POP();
460b9539 603 /* We must set the total size or scrolling will not work (it wouldn't be hard
604 * for GtkLayout to figure it out for itself but presumably you're supposed
605 * to be able to have widgets off the edge of the layuot.)
606 *
607 * There is a problem: if we shrink the size then the part of the screen that
608 * is outside the new size but inside the old one is not updated. I think
609 * this is arguably bug in GTK+ but it's easy to force a redraw if this
610 * region is nonempty.
611 */
612 gtk_layout_get_size(GTK_LAYOUT(chooselayout), &oldwidth, &oldheight);
613 if(oldwidth > d.width || oldheight > d.height)
614 gtk_widget_queue_draw(chooselayout);
615 gtk_layout_set_size(GTK_LAYOUT(chooselayout), d.width, d.height);
616 /* Notify the main menu of any recent changes */
617 menu_update(-1);
618}
619
620/* Make sure all displayed widgets from CN down exist and are in their proper
621 * place and return the vertical space used. */
622static struct displaydata display_tree(struct choosenode *cn, int x, int y) {
623 int n, aw;
624 GtkRequisition req;
625 struct displaydata d, cd;
626 GdkPixbuf *pb;
627
628 D(("display_tree %s %d,%d", cn->path, x, y));
629
630 /* An expandable item contains an arrow and a text label. When you press the
631 * button it flips its expand state.
632 *
633 * A non-expandable item has just a text label and no arrow.
634 */
635 if(!cn->container) {
521b8841 636 MTAG_PUSH("make_widgets_1");
460b9539 637 /* Widgets need to be created */
e9b70a84 638 NW(hbox);
460b9539 639 cn->hbox = gtk_hbox_new(FALSE, 1);
640 if(cn->flags & CN_EXPANDABLE) {
e9b70a84 641 NW(arrow);
460b9539 642 cn->arrow = gtk_arrow_new(cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN
643 : GTK_ARROW_RIGHT,
644 GTK_SHADOW_NONE);
645 cn->marker = 0;
646 } else {
647 cn->arrow = 0;
e9b70a84 648 if((pb = find_image("notes.png"))) {
649 NW(image);
460b9539 650 cn->marker = gtk_image_new_from_pixbuf(pb);
e9b70a84 651 }
460b9539 652 }
521b8841 653 MTAG_POP();
654 MTAG_PUSH("make_widgets_2");
e9b70a84 655 NW(label);
460b9539 656 cn->label = gtk_label_new(cn->display);
657 if(cn->arrow)
658 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->arrow);
659 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->label);
660 if(cn->marker)
661 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->marker);
521b8841 662 MTAG_POP();
663 MTAG_PUSH("make_widgets_3");
e9b70a84 664 NW(event_box);
460b9539 665 cn->container = gtk_event_box_new();
666 gtk_container_add(GTK_CONTAINER(cn->container), cn->hbox);
667 g_signal_connect(cn->container, "button-release-event",
668 G_CALLBACK(clicked_choosenode), cn);
669 g_signal_connect(cn->container, "button-press-event",
670 G_CALLBACK(clicked_choosenode), cn);
671 g_object_ref(cn->container);
672 gtk_widget_set_name(cn->label, "choose");
673 gtk_widget_set_name(cn->container, "choose");
674 /* Show everything by default */
675 gtk_widget_show_all(cn->container);
e1d4a32b 676 MTAG_POP();
460b9539 677 }
678 assert(cn->container);
679 /* Make sure the icon is right */
680 if(cn->flags & CN_EXPANDABLE)
681 gtk_arrow_set(GTK_ARROW(cn->arrow),
682 cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN : GTK_ARROW_RIGHT,
683 GTK_SHADOW_NONE);
684 else if(cn->marker)
685 /* Make sure the queued marker is right */
686 /* TODO: doesn't always work */
687 (queued(cn->path) ? gtk_widget_show : gtk_widget_hide)(cn->marker);
688 /* Put the widget in the right place */
689 if(cn->flags & CN_DISPLAYED)
690 gtk_layout_move(GTK_LAYOUT(chooselayout), cn->container, x, y);
691 else {
692 gtk_layout_put(GTK_LAYOUT(chooselayout), cn->container, x, y);
693 cn->flags |= CN_DISPLAYED;
521b8841 694 /* Now chooselayout has a ref to the container */
695 g_object_unref(cn->container);
460b9539 696 }
697 /* Set the widget's selection status */
698 if(!(cn->flags & CN_EXPANDABLE))
699 display_selection(cn);
700 /* Find the size used so we can get vertical positioning right. */
701 gtk_widget_size_request(cn->container, &req);
702 d.width = x + req.width;
703 d.height = y + req.height;
704 if(cn->flags & CN_EXPANDED) {
705 /* We'll offset children by the size of the arrow whatever it might be. */
706 assert(cn->arrow);
707 gtk_widget_size_request(cn->arrow, &req);
708 aw = req.width;
709 for(n = 0; n < cn->children.nvec; ++n) {
710 cd = display_tree(cn->children.vec[n], x + aw, d.height);
711 if(cd.width > d.width)
712 d.width = cd.width;
713 d.height = cd.height;
714 }
715 } else {
716 for(n = 0; n < cn->children.nvec; ++n)
717 undisplay_tree(cn->children.vec[n]);
718 }
719 if(!(cn->flags & CN_EXPANDABLE)) {
720 ++files_visible;
721 if(cn->flags & CN_SELECTED)
722 ++files_selected;
723 }
724 /* report back how much space we used */
725 D(("display_tree %s %d,%d total size %dx%d", cn->path, x, y,
726 d.width, d.height));
727 return d;
728}
729
730/* Remove widgets for newly hidden nodes */
731static void undisplay_tree(struct choosenode *cn) {
732 int n;
733
734 D(("undisplay_tree %s", cn->path));
735 /* Remove this widget from the display */
736 if(cn->flags & CN_DISPLAYED) {
737 gtk_container_remove(GTK_CONTAINER(chooselayout), cn->container);
738 cn->flags ^= CN_DISPLAYED;
739 }
740 /* Remove children too */
741 for(n = 0; n < cn->children.nvec; ++n)
742 undisplay_tree(cn->children.vec[n]);
743}
744
745/* Selection --------------------------------------------------------------- */
746
747static void display_selection(struct choosenode *cn) {
748 /* Need foreground and background colors */
749 gtk_widget_set_state(cn->label, (cn->flags & CN_SELECTED
750 ? GTK_STATE_SELECTED : GTK_STATE_NORMAL));
751 gtk_widget_set_state(cn->container, (cn->flags & CN_SELECTED
752 ? GTK_STATE_SELECTED : GTK_STATE_NORMAL));
753}
754
755/* Set the selection state of a widget. Directories can never be selected, we
756 * just ignore attempts to do so. */
757static void set_selection(struct choosenode *cn, int selected) {
758 unsigned f = selected ? CN_SELECTED : 0;
759
760 D(("set_selection %d %s", selected, cn->path));
761 if(!(cn->flags & CN_EXPANDABLE) && (cn->flags & CN_SELECTED) != f) {
762 cn->flags ^= CN_SELECTED;
763 /* Maintain selection count */
764 if(selected)
765 ++files_selected;
766 else
767 --files_selected;
768 display_selection(cn);
769 /* Update main menu sensitivity */
770 menu_update(-1);
771 }
772}
773
774/* Recursively clear all selection bits from CN down */
775static void clear_selection(struct choosenode *cn) {
776 int n;
777
778 set_selection(cn, 0);
779 for(n = 0; n < cn->children.nvec; ++n)
780 clear_selection(cn->children.vec[n]);
781}
782
783/* User actions ------------------------------------------------------------ */
784
785/* Clicked on something */
786static void clicked_choosenode(GtkWidget attribute((unused)) *widget,
787 GdkEventButton *event,
788 gpointer user_data) {
789 struct choosenode *cn = user_data;
790 int ind, last_ind, n;
791
792 D(("clicked_choosenode %s", cn->path));
793 if(event->type == GDK_BUTTON_RELEASE
794 && event->button == 1) {
795 /* Left click */
796 if(cn->flags & CN_EXPANDABLE) {
797 /* This is a directory. Flip its expansion status. */
798 if(cn->flags & CN_EXPANDED)
799 contract_node(cn);
800 else
801 expand_node(cn);
802 last_click = 0;
803 } else {
804 /* This is a file. Adjust selection status */
805 /* TODO the basic logic here is essentially the same as that in queue.c.
806 * Can we share code at all? */
807 switch(event->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) {
808 case 0:
809 clear_selection(root);
810 set_selection(cn, 1);
811 last_click = cn;
812 break;
813 case GDK_CONTROL_MASK:
814 set_selection(cn, !(cn->flags & CN_SELECTED));
815 last_click = cn;
816 break;
817 case GDK_SHIFT_MASK:
818 case GDK_SHIFT_MASK|GDK_CONTROL_MASK:
819 if(last_click && last_click->parent == cn->parent) {
820 /* Figure out where the current and last clicks are in the list */
821 ind = last_ind = -1;
822 for(n = 0; n < cn->parent->children.nvec; ++n) {
823 if(cn->parent->children.vec[n] == cn)
824 ind = n;
825 if(cn->parent->children.vec[n] == last_click)
826 last_ind = n;
827 }
828 /* Test shouldn't ever fail, but still */
829 if(ind >= 0 && last_ind >= 0) {
830 if(!(event->state & GDK_CONTROL_MASK)) {
831 for(n = 0; n < cn->parent->children.nvec; ++n)
832 set_selection(cn->parent->children.vec[n], 0);
833 }
834 if(ind > last_ind)
835 for(n = last_ind; n <= ind; ++n)
836 set_selection(cn->parent->children.vec[n], 1);
837 else
838 for(n = ind; n <= last_ind; ++n)
839 set_selection(cn->parent->children.vec[n], 1);
840 if(event->state & GDK_CONTROL_MASK)
841 last_click = cn;
842 }
843 }
844 break;
845 }
846 }
847 } else if(event->type == GDK_BUTTON_RELEASE
848 && event->button == 2) {
849 /* Middle click - play the pointed track */
850 if(!(cn->flags & CN_EXPANDABLE)) {
851 clear_selection(root);
852 set_selection(cn, 1);
853 gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
854 disorder_eclient_play(client, cn->path, 0, 0);
855 last_click = 0;
856 }
857 } else if(event->type == GDK_BUTTON_PRESS
858 && event->button == 3) {
859 /* Right click. Pop up a menu. */
860 /* If the current file isn't selected, switch the selection to just that.
861 * (If we're looking at a directory then leave the selection alone.) */
862 if(!(cn->flags & CN_EXPANDABLE) && !(cn->flags & CN_SELECTED)) {
863 clear_selection(root);
864 set_selection(cn, 1);
865 last_click = cn;
866 }
867 /* Set the item sensitivity and callbacks */
868 for(n = 0; n < NMENUITEMS; ++n) {
869 if(menuitems[n].handlerid)
870 g_signal_handler_disconnect(menuitems[n].w,
871 menuitems[n].handlerid);
872 gtk_widget_set_sensitive(menuitems[n].w,
873 menuitems[n].sensitive(cn));
874 menuitems[n].handlerid = g_signal_connect
875 (menuitems[n].w, "activate", G_CALLBACK(menuitems[n].activate), cn);
876 }
877 /* Pop up the menu */
878 gtk_widget_show_all(menu);
879 gtk_menu_popup(GTK_MENU(menu), 0, 0, 0, 0,
880 event->button, event->time);
881 }
882}
883
884static void searchentry_changed(GtkEditable attribute((unused)) *editable,
885 gpointer attribute((unused)) user_data) {
886 initiate_search();
887}
888
889/* Menu items -------------------------------------------------------------- */
890
891static void recurse_selected(struct choosenode *cn, struct vector *v) {
892 int n;
893
894 if(cn->flags & CN_EXPANDABLE) {
895 if(cn->flags & CN_EXPANDED)
896 for(n = 0; n < cn->children.nvec; ++n)
897 recurse_selected(cn->children.vec[n], v);
898 } else {
899 if((cn->flags & CN_SELECTED) && cn->path)
900 vector_append(v, (char *)cn->path);
901 }
902}
903
904static char **gather_selected(int *ntracks) {
905 struct vector v;
906
907 vector_init(&v);
908 recurse_selected(root, &v);
909 vector_terminate(&v);
910 if(ntracks) *ntracks = v.nvec;
911 return v.vec;
912}
913
914static void activate_play(GtkMenuItem attribute((unused)) *menuitem,
915 gpointer attribute((unused)) user_data) {
916 char **tracks = gather_selected(0);
917 int n;
918
919 gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
920 for(n = 0; tracks[n]; ++n)
921 disorder_eclient_play(client, tracks[n], 0, 0);
922}
923
39fe1014 924#if 0
460b9539 925static void activate_remove(GtkMenuItem attribute((unused)) *menuitem,
926 gpointer attribute((unused)) user_data) {
927 /* TODO remove all selected tracks */
928}
39fe1014 929#endif
460b9539 930
931static void activate_properties(GtkMenuItem attribute((unused)) *menuitem,
932 gpointer attribute((unused)) user_data) {
933 int ntracks;
934 char **tracks = gather_selected(&ntracks);
935
936 properties(ntracks, tracks);
937}
938
939static gboolean sensitive_play(struct choosenode attribute((unused)) *cn) {
a76c9d45 940 return !!files_selected && disorder_eclient_connected(client);
460b9539 941}
942
39fe1014 943#if 0
460b9539 944static gboolean sensitive_remove(struct choosenode attribute((unused)) *cn) {
945 return FALSE; /* not implemented yet */
946}
39fe1014 947#endif
460b9539 948
949static gboolean sensitive_properties(struct choosenode attribute((unused)) *cn) {
a76c9d45 950 return !!files_selected && disorder_eclient_connected(client);
460b9539 951}
952
953/* Main menu plumbing ------------------------------------------------------ */
954
955static int choose_properties_sensitive(GtkWidget attribute((unused)) *w) {
a76c9d45 956 return !!files_selected && disorder_eclient_connected(client);
460b9539 957}
958
959static int choose_selectall_sensitive(GtkWidget attribute((unused)) *w) {
960 return FALSE; /* TODO */
961}
962
963static void choose_properties_activate(GtkWidget attribute((unused)) *w) {
964 activate_properties(0, 0);
965}
966
967static void choose_selectall_activate(GtkWidget attribute((unused)) *w) {
968 /* TODO */
969}
970
971static const struct tabtype tabtype_choose = {
972 choose_properties_sensitive,
973 choose_selectall_sensitive,
974 choose_properties_activate,
975 choose_selectall_activate,
976};
977
978/* Public entry points ----------------------------------------------------- */
979
980/* Create a track choice widget */
981GtkWidget *choose_widget(void) {
982 int n;
983 GtkWidget *scrolled;
984 GtkWidget *vbox, *hbox, *clearsearch;
985
986 /*
987 * +--vbox-------------------------------------------------------+
988 * | +-hbox----------------------------------------------------+ |
989 * | | searchentry | clearsearch | |
990 * | +---------------------------------------------------------+ |
991 * | +-scrolled------------------------------------------------+ |
992 * | | +-chooselayout------------------------------------++--+ | |
993 * | | | Tree structure is manually layed out in here ||^^| | |
994 * | | | || | | |
995 * | | | || | | |
996 * | | | || | | |
997 * | | | ||vv| | |
998 * | | +-------------------------------------------------++--+ | |
999 * | | +-------------------------------------------------+ | |
1000 * | | |< >| | |
1001 * | | +-------------------------------------------------+ | |
1002 * | +---------------------------------------------------------+ |
1003 * +-------------------------------------------------------------+
1004 */
1005
1006 /* Text entry box for search terms */
e9b70a84 1007 NW(entry);
460b9539 1008 searchentry = gtk_entry_new();
1009 g_signal_connect(searchentry, "changed", G_CALLBACK(searchentry_changed), 0);
1010
1011 /* Cancel button to clear the search */
e9b70a84 1012 NW(button);
460b9539 1013 clearsearch = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
1014 g_signal_connect(G_OBJECT(clearsearch), "clicked",
1015 G_CALLBACK(clearsearch_clicked), 0);
1016
1017 /* hbox packs the search box and the cancel button together on a line */
e9b70a84 1018 NW(hbox);
460b9539 1019 hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*spacing*/);
1020 gtk_box_pack_start(GTK_BOX(hbox), searchentry,
1021 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
1022 gtk_box_pack_end(GTK_BOX(hbox), clearsearch,
1023 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
1024
1025 /* chooselayout contains the currently visible subset of the track
1026 * namespace */
e9b70a84 1027 NW(layout);
460b9539 1028 chooselayout = gtk_layout_new(0, 0);
1029 root = newnode(0/*parent*/, "<root>", "All files", "",
1030 CN_EXPANDABLE, fill_root_node);
1031 realroot = root;
1032 expand_node(root); /* will call redisplay_tree */
1033 /* Create the popup menu */
e9b70a84 1034 NW(menu);
460b9539 1035 menu = gtk_menu_new();
1036 g_signal_connect(menu, "destroy", G_CALLBACK(gtk_widget_destroyed), &menu);
1037 for(n = 0; n < NMENUITEMS; ++n) {
e9b70a84 1038 NW(menu_item);
460b9539 1039 menuitems[n].w = gtk_menu_item_new_with_label(menuitems[n].name);
1040 gtk_menu_attach(GTK_MENU(menu), menuitems[n].w, 0, 1, n, n + 1);
1041 }
1042 /* The layout is scrollable */
1043 scrolled = scroll_widget(GTK_WIDGET(chooselayout), "choose");
1044
1045 /* The scrollable layout and the search hbox go together in a vbox */
e9b70a84 1046 NW(vbox);
460b9539 1047 vbox = gtk_vbox_new(FALSE/*homogenous*/, 1/*spacing*/);
1048 gtk_box_pack_start(GTK_BOX(vbox), hbox,
1049 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
1050 gtk_box_pack_end(GTK_BOX(vbox), scrolled,
1051 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
1052
1053 g_object_set_data(G_OBJECT(vbox), "type", (void *)&tabtype_choose);
1054 return vbox;
1055}
1056
1057/* Called when something we care about here might have changed */
1058void choose_update(void) {
1059 redisplay_tree();
1060}
1061
1062/*
1063Local Variables:
1064c-basic-offset:2
1065comment-column:40
1066fill-column:79
1067indent-tabs-mode:nil
1068End:
1069*/