chiark / gitweb /
eclient string response calls now get errors reported to the completed
[disorder] / disobedience / choose.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
fb009628 3 * Copyright (C) 2006-2008 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"
81cd1894 29#include "timeval.h"
460b9539 30
31/* Choose track ------------------------------------------------------------ */
32
81cd1894
RK
33#if TDEBUG
34/* Timing */
35static struct {
36 struct timeval total;
37 struct timeval gtkbits;
38 struct timeval menuupdate;
39 struct timeval new_widgets;
40 struct timeval undisplay;
41 struct timeval colors;
42 struct timeval markers;
43 struct timeval location;
44 struct timeval selection;
45} times;
46
47#define BEGIN(WHAT) do { \
48 struct timeval started##WHAT, finished##WHAT; \
49 xgettimeofday(&started##WHAT, 0)
50
51#define END(WHAT) \
52 xgettimeofday(&finished##WHAT, 0); \
53 times.WHAT = tvadd(times.WHAT, tvsub(finished##WHAT, started##WHAT)); \
54} while(0)
55
56#define INIT() memset(&times, 0, sizeof times)
57
58#define REPORT() do { \
59 fprintf(stderr, "total=%g\n" \
60 "gtkbits=%g\n" \
61 "menuupdate=%g\n" \
62 "new_widgets=%g\n" \
63 "undisplay=%g\n" \
64 "colors=%g\n" \
65 "markers=%g\n" \
66 "location=%g\n" \
67 "selection=%g\n" \
68 "accumulation=%g\n" \
69 "\n", \
70 tvdouble(times.total), \
71 tvdouble(times.gtkbits), \
72 tvdouble(times.menuupdate), \
73 tvdouble(times.new_widgets), \
74 tvdouble(times.undisplay), \
75 tvdouble(times.colors), \
76 tvdouble(times.markers), \
77 tvdouble(times.location), \
78 tvdouble(times.selection), \
79 (tvdouble(times.gtkbits) \
80 + tvdouble(times.menuupdate) \
81 + tvdouble(times.new_widgets) \
82 + tvdouble(times.undisplay) \
83 + tvdouble(times.colors) \
84 + tvdouble(times.markers) \
85 + tvdouble(times.location) \
86 + tvdouble(times.selection))); \
87} while(0)
88#else
89#define BEGIN(WHAT) do {
90#define END(WHAT) } while(0)
91#define INIT() ((void)0)
92#define REPORT() ((void)0)
93#endif
94
e9b70a84 95WT(label);
96WT(event_box);
97WT(menu);
98WT(menu_item);
99WT(layout);
100WT(vbox);
101WT(arrow);
102WT(hbox);
103WT(button);
104WT(image);
105WT(entry);
106
460b9539 107/* Types */
108
109struct choosenode;
110
b9bdafc7 111/** @brief Accumulated information about the tree widget */
460b9539 112struct displaydata {
b9bdafc7
RK
113 /** @brief Maximum width required */
114 guint width;
115 /** @brief Maximum height required */
116 guint height;
460b9539 117};
118
119/* instantiate the node vector type */
b8956e9e
RK
120
121VECTOR_TYPE(nodevector, struct choosenode *, xrealloc);
460b9539 122
16e145a5
RK
123/** @brief Signature of function called when a choosenode is filled */
124typedef void (when_filled_callback)(struct choosenode *cn,
125 void *wfu);
126
b9bdafc7 127/** @brief One node in the virtual filesystem */
460b9539 128struct choosenode {
b9bdafc7
RK
129 struct choosenode *parent; /**< @brief parent node */
130 const char *path; /**< @brief full path or 0 */
131 const char *sort; /**< @brief sort key */
132 const char *display; /**< @brief display name */
133 int pending; /**< @brief pending resolve queries */
460b9539 134 unsigned flags;
b9bdafc7
RK
135#define CN_EXPANDABLE 0x0001 /**< @brief node is expandable */
136#define CN_EXPANDED 0x0002 /**< @brief node is expanded
137 *
138 * Expandable items are directories;
139 * non-expandable ones are files. */
140#define CN_DISPLAYED 0x0004 /**< @brief widget is displayed in layout */
141#define CN_SELECTED 0x0008 /**< @brief node is selected */
16e145a5
RK
142#define CN_GETTING_FILES 0x0010 /**< @brief files inbound */
143#define CN_RESOLVING_FILES 0x0020 /**< @brief resolved files inbound */
144#define CN_GETTING_DIRS 0x0040 /**< @brief directories inbound */
145#define CN_GETTING_ANY 0x0070 /**< @brief getting something */
6a06a735 146#define CN_CONTINGENT 0x0080 /**< @brief expansion contingent on search */
b9bdafc7
RK
147 struct nodevector children; /**< @brief vector of children */
148 void (*fill)(struct choosenode *); /**< @brief request child fill or 0 for leaf */
149 GtkWidget *container; /**< @brief the container for this row */
150 GtkWidget *hbox; /**< @brief the hbox for this row */
151 GtkWidget *arrow; /**< @brief arrow widget or 0 */
152 GtkWidget *label; /**< @brief text label for this node */
153 GtkWidget *marker; /**< @brief queued marker */
16e145a5
RK
154
155 when_filled_callback *whenfilled; /**< @brief called when filled or 0 */
156 void *wfu; /**< @brief passed to @c whenfilled */
7e60b670
RK
157 int ymin; /**< @brief least Y value */
158 int ymax; /**< @brief greatest Y value */
460b9539 159};
160
b9bdafc7 161/** @brief One item in the popup menu */
16e145a5 162struct choose_menuitem {
460b9539 163 /* Parameters */
b9bdafc7 164 const char *name; /**< @brief name */
460b9539 165
166 /* Callbacks */
167 void (*activate)(GtkMenuItem *menuitem, gpointer user_data);
b9bdafc7
RK
168 /**< @brief Called to activate the menu item.
169 *
170 * @p user_data is the choosenode the mouse pointer is over. */
460b9539 171
172 gboolean (*sensitive)(struct choosenode *cn);
b9bdafc7
RK
173 /* @brief Called to determine whether the menu item should be sensitive.
174 *
175 * TODO? */
460b9539 176
177 /* State */
b9bdafc7
RK
178 gulong handlerid; /**< @brief signal handler ID */
179 GtkWidget *w; /**< @brief menu item widget */
460b9539 180};
181
182/* Variables */
183
184static GtkWidget *chooselayout;
7e60b670 185static GtkAdjustment *vadjust;
b9bdafc7 186static GtkWidget *searchentry; /**< @brief search terms */
fcc8b9f7
RK
187static GtkWidget *nextsearch; /**< @brief next search result */
188static GtkWidget *prevsearch; /**< @brief previous search result */
460b9539 189static struct choosenode *root;
16e145a5
RK
190static GtkWidget *track_menu; /**< @brief track popup menu */
191static GtkWidget *dir_menu; /**< @brief directory popup menu */
b9bdafc7
RK
192static struct choosenode *last_click; /**< @brief last clicked node for selection */
193static int files_visible; /**< @brief total files visible */
194static int files_selected; /**< @brief total files selected */
c3df9503 195static int gets_in_flight; /**< @brief total gets in flight */
b9bdafc7
RK
196static int search_in_flight; /**< @brief a search is underway */
197static int search_obsolete; /**< @brief the current search is void */
198static char **searchresults; /**< @brief search results */
199static int nsearchresults; /**< @brief number of results */
6a06a735
RK
200static int nsearchvisible; /**< @brief number of search results visible */
201static struct hash *searchhash; /**< @brief hash of search results */
fcc8b9f7
RK
202static struct progress_window *spw; /**< @brief progress window */
203static struct choosenode **searchnodes; /**< @brief choosenodes of search results */
73dd383f 204static int suppress_redisplay; /**< @brief suppress redisplay */
460b9539 205
206/* Forward Declarations */
207
77d95881 208static void clear_children(struct choosenode *cn);
460b9539 209static struct choosenode *newnode(struct choosenode *parent,
210 const char *path,
211 const char *display,
212 const char *sort,
213 unsigned flags,
214 void (*fill)(struct choosenode *));
215static void fill_root_node(struct choosenode *cn);
460b9539 216static void fill_directory_node(struct choosenode *cn);
217static void got_files(void *v, int nvec, char **vec);
06bfbba4 218static void got_resolved_file(void *v, const char *error, const char *track);
460b9539 219static void got_dirs(void *v, int nvec, char **vec);
220
6a06a735 221static void expand_node(struct choosenode *cn, int contingent);
460b9539 222static void contract_node(struct choosenode *cn);
73dd383f
RK
223static void updated_node(struct choosenode *cn, int redisplay,
224 const char *why);
460b9539 225
226static void display_selection(struct choosenode *cn);
227static void clear_selection(struct choosenode *cn);
228
73dd383f 229static void redisplay_tree(const char *why);
460b9539 230static struct displaydata display_tree(struct choosenode *cn, int x, int y);
231static void undisplay_tree(struct choosenode *cn);
232static void initiate_search(void);
233static void delete_widgets(struct choosenode *cn);
6a06a735 234static void expand_from(struct choosenode *cn);
fcc8b9f7 235static struct choosenode *first_search_result(struct choosenode *cn);
460b9539 236
237static void clicked_choosenode(GtkWidget attribute((unused)) *widget,
238 GdkEventButton *event,
239 gpointer user_data);
240
16e145a5
RK
241static void activate_track_play(GtkMenuItem *menuitem, gpointer user_data);
242static void activate_track_properties(GtkMenuItem *menuitem, gpointer user_data);
243
244static gboolean sensitive_track_play(struct choosenode *cn);
245static gboolean sensitive_track_properties(struct choosenode *cn);
246
247static void activate_dir_play(GtkMenuItem *menuitem, gpointer user_data);
248static void activate_dir_properties(GtkMenuItem *menuitem, gpointer user_data);
249static void activate_dir_select(GtkMenuItem *menuitem, gpointer user_data);
250
251static gboolean sensitive_dir_play(struct choosenode *cn);
252static gboolean sensitive_dir_properties(struct choosenode *cn);
253static gboolean sensitive_dir_select(struct choosenode *cn);
254
255/** @brief Track menu items */
256static struct choose_menuitem track_menuitems[] = {
257 { "Play track", activate_track_play, sensitive_track_play, 0, 0 },
258 { "Track properties", activate_track_properties, sensitive_track_properties, 0, 0 },
259 { 0, 0, 0, 0, 0 }
260};
261
262/** @brief Directory menu items */
263static struct choose_menuitem dir_menuitems[] = {
264 { "Play all tracks", activate_dir_play, sensitive_dir_play, 0, 0 },
265 { "Track properties", activate_dir_properties, sensitive_dir_properties, 0, 0 },
266 { "Select all tracks", activate_dir_select, sensitive_dir_select, 0, 0 },
267 { 0, 0, 0, 0, 0 }
460b9539 268};
269
460b9539 270/* Maintaining the data structure ------------------------------------------ */
271
734df821 272static char *cnflags(const struct choosenode *cn) {
6a06a735
RK
273 unsigned f = cn->flags, n;
274 struct dynstr d[1];
275
276 static const char *bits[] = {
277 "expandable",
278 "expanded",
279 "displayed",
280 "selected",
281 "getting_files",
282 "resolving_files",
283 "getting_dirs",
284 "contingent"
285 };
286#define NBITS (sizeof bits / sizeof *bits)
287
288 dynstr_init(d);
289 if(!f)
290 dynstr_append(d, '0');
291 else {
292 for(n = 0; n < NBITS; ++n) {
293 const unsigned bit = 1 << n;
294 if(f & bit) {
295 if(d->nvec)
296 dynstr_append(d, '|');
297 dynstr_append_string(d, bits[n]);
298 f ^= bit;
299 }
300 }
301 if(f) {
302 char buf[32];
303 if(d->nvec)
304 dynstr_append(d, '|');
305 sprintf(buf, "%#x", f);
306 dynstr_append_string(d, buf);
307 }
308 }
309 dynstr_terminate(d);
310 return d->vec;
311}
312
b9bdafc7 313/** @brief Create a new node */
460b9539 314static struct choosenode *newnode(struct choosenode *parent,
315 const char *path,
316 const char *display,
317 const char *sort,
318 unsigned flags,
319 void (*fill)(struct choosenode *)) {
320 struct choosenode *const n = xmalloc(sizeof *n);
321
322 D(("newnode %s %s", path, display));
323 if(flags & CN_EXPANDABLE)
324 assert(fill);
325 else
326 assert(!fill);
327 n->parent = parent;
328 n->path = path;
329 n->display = display;
330 n->sort = sort;
331 n->flags = flags;
332 nodevector_init(&n->children);
333 n->fill = fill;
334 if(parent)
335 nodevector_append(&parent->children, n);
336 return n;
337}
338
16e145a5
RK
339/** @brief Called when a node has been filled
340 *
341 * Response for calling @c whenfilled.
342 */
343static void filled(struct choosenode *cn) {
344 when_filled_callback *const whenfilled = cn->whenfilled;
345
346 if(whenfilled) {
347 cn->whenfilled = 0;
348 whenfilled(cn, cn->wfu);
349 }
6a06a735
RK
350 if(nsearchvisible < nsearchresults) {
351 /* There is still search expansion work to do */
352 D(("filled %s %d/%d", cn->path, nsearchvisible, nsearchresults));
353 expand_from(cn);
354 }
c3df9503
RK
355 if(gets_in_flight == 0 && nsearchvisible < nsearchresults)
356 expand_from(root);
16e145a5
RK
357}
358
b9bdafc7 359/** @brief Fill the root */
460b9539 360static void fill_root_node(struct choosenode *cn) {
460b9539 361 struct callbackdata *cbd;
362
363 D(("fill_root_node"));
77d95881 364 clear_children(cn);
c3df9503
RK
365 /* More de-duping possible here */
366 if(cn->flags & CN_GETTING_ANY)
367 return;
368 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
369 cbd = xmalloc(sizeof *cbd);
370 cbd->u.choosenode = cn;
371 disorder_eclient_dirs(client, got_dirs, "", 0, cbd);
372 cbd = xmalloc(sizeof *cbd);
373 cbd->u.choosenode = cn;
374 disorder_eclient_files(client, got_files, "", 0, cbd);
375 cn->flags |= CN_GETTING_FILES|CN_GETTING_DIRS;
376 gets_in_flight += 2;
460b9539 377}
378
b9bdafc7 379/** @brief Delete all the widgets owned by @p cn */
e9b70a84 380static void delete_cn_widgets(struct choosenode *cn) {
381 if(cn->arrow) {
382 DW(arrow);
383 gtk_widget_destroy(cn->arrow);
384 cn->arrow = 0;
385 }
386 if(cn->label) {
387 DW(label);
388 gtk_widget_destroy(cn->label);
389 cn->label = 0;
390 }
391 if(cn->marker) {
392 DW(image);
393 gtk_widget_destroy(cn->marker);
394 cn->marker = 0;
395 }
396 if(cn->hbox) {
397 DW(hbox);
398 gtk_widget_destroy(cn->hbox);
399 cn->hbox = 0;
400 }
401 if(cn->container) {
402 DW(event_box);
403 gtk_widget_destroy(cn->container);
404 cn->container = 0;
405 }
406}
407
b9bdafc7
RK
408/** @brief Recursively clear all the children of @p cn
409 *
410 * All the widgets at or below @p cn are deleted. All choosenodes below
411 * it are emptied. i.e. we prune the tree at @p cn.
412 */
460b9539 413static void clear_children(struct choosenode *cn) {
414 int n;
415
416 D(("clear_children %s", cn->path));
417 /* Recursively clear subtrees */
418 for(n = 0; n < cn->children.nvec; ++n) {
419 clear_children(cn->children.vec[n]);
e9b70a84 420 delete_cn_widgets(cn->children.vec[n]);
460b9539 421 }
422 cn->children.nvec = 0;
423}
424
b9bdafc7 425/** @brief Called with a list of files just below some node */
460b9539 426static void got_files(void *v, int nvec, char **vec) {
427 struct callbackdata *cbd = v;
428 struct choosenode *cn = cbd->u.choosenode;
429 int n;
430
734df821 431 D(("got_files %d files for %s %s", nvec, cn->path, cnflags(cn)));
06bfbba4 432 /* Complicated by the need to resolve aliases. */
16e145a5 433 cn->flags &= ~CN_GETTING_FILES;
c3df9503 434 --gets_in_flight;
5459347d
RK
435 if((cn->pending = nvec)) {
436 cn->flags |= CN_RESOLVING_FILES;
c3df9503 437 for(n = 0; n < nvec; ++n) {
06bfbba4 438 disorder_eclient_resolve(client, got_resolved_file, vec[n], cn);
c3df9503
RK
439 ++gets_in_flight;
440 }
5459347d 441 }
6a06a735
RK
442 /* If there are no files and the directories are all read by now, we're
443 * done */
444 if(!(cn->flags & CN_GETTING_ANY))
445 filled(cn);
cf1f8eef
RK
446 if(!gets_in_flight)
447 redisplay_tree("got_files");
460b9539 448}
449
b9bdafc7 450/** @brief Called with an alias resolved filename */
06bfbba4
RK
451static void got_resolved_file(void *v, const char *error, const char *track) {
452 struct choosenode *const cn = v, *file_cn;
453
454 if(error) {
455 popup_protocol_error(0, error);
456 } else {
457 D(("resolved %s %s %d left", cn->path, cnflags(cn), cn->pending - 1));
458 /* TODO as below */
459 file_cn = newnode(cn, track,
460 trackname_transform("track", track, "display"),
461 trackname_transform("track", track, "sort"),
462 0/*flags*/, 0/*fill*/);
463 --gets_in_flight;
464 /* Only bother updating when we've got the lot */
465 if(--cn->pending == 0) {
466 cn->flags &= ~CN_RESOLVING_FILES;
467 updated_node(cn, gets_in_flight == 0, "got_resolved_file");
468 if(!(cn->flags & CN_GETTING_ANY))
469 filled(cn);
470 }
16e145a5 471 }
460b9539 472}
473
b9bdafc7 474/** @brief Called with a list of directories just below some node */
460b9539 475static void got_dirs(void *v, int nvec, char **vec) {
476 struct callbackdata *cbd = v;
477 struct choosenode *cn = cbd->u.choosenode;
478 int n;
479
734df821 480 D(("got_dirs %d dirs for %s %s", nvec, cn->path, cnflags(cn)));
b9bdafc7
RK
481 /* TODO this depends on local configuration for trackname_transform().
482 * This will work, since the defaults are now built-in, but it'll be
483 * (potentially) different to the server's configured settings.
484 *
485 * Really we want a variant of files/dirs that produces both the
486 * raw filename and the transformed name for a chosen context.
487 */
c3df9503 488 --gets_in_flight;
460b9539 489 for(n = 0; n < nvec; ++n)
490 newnode(cn, vec[n],
491 trackname_transform("dir", vec[n], "display"),
492 trackname_transform("dir", vec[n], "sort"),
493 CN_EXPANDABLE, fill_directory_node);
cf1f8eef 494 updated_node(cn, gets_in_flight == 0, "got_dirs");
16e145a5
RK
495 cn->flags &= ~CN_GETTING_DIRS;
496 if(!(cn->flags & CN_GETTING_ANY))
497 filled(cn);
460b9539 498}
499
b9bdafc7 500/** @brief Fill a child node */
460b9539 501static void fill_directory_node(struct choosenode *cn) {
502 struct callbackdata *cbd;
503
504 D(("fill_directory_node %s", cn->path));
505 /* TODO: caching */
16e145a5
RK
506 if(cn->flags & CN_GETTING_ANY)
507 return;
460b9539 508 assert(report_label != 0);
509 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
1d0c28c7 510 clear_children(cn);
460b9539 511 cbd = xmalloc(sizeof *cbd);
512 cbd->u.choosenode = cn;
513 disorder_eclient_dirs(client, got_dirs, cn->path, 0, cbd);
514 cbd = xmalloc(sizeof *cbd);
515 cbd->u.choosenode = cn;
516 disorder_eclient_files(client, got_files, cn->path, 0, cbd);
16e145a5 517 cn->flags |= CN_GETTING_FILES|CN_GETTING_DIRS;
c3df9503 518 gets_in_flight += 2;
460b9539 519}
520
b9bdafc7 521/** @brief Expand a node */
6a06a735 522static void expand_node(struct choosenode *cn, int contingent) {
734df821 523 D(("expand_node %s %d %s", cn->path, contingent, cnflags(cn)));
460b9539 524 assert(cn->flags & CN_EXPANDABLE);
525 /* If node is already expanded do nothing. */
526 if(cn->flags & CN_EXPANDED) return;
527 /* We mark the node as expanded and request that it fill itself. When it has
528 * completed it will called updated_node() and we can redraw at that
529 * point. */
530 cn->flags |= CN_EXPANDED;
6a06a735
RK
531 if(contingent)
532 cn->flags |= CN_CONTINGENT;
533 else
534 cn->flags &= ~CN_CONTINGENT;
535 /* If this node is not contingently expanded, mark all its parents back to
536 * the root as not contingent either, so they won't be contracted when the
537 * search results change */
538 if(!contingent) {
539 struct choosenode *cnp;
540
541 for(cnp = cn->parent; cnp; cnp = cnp->parent)
542 cnp->flags &= ~CN_CONTINGENT;
543 }
460b9539 544 /* TODO: visual feedback */
545 cn->fill(cn);
546}
547
6a06a735
RK
548/** @brief Make sure all the search results below @p cn are expanded
549 * @param cn Node to start at
550 */
551static void expand_from(struct choosenode *cn) {
552 int n;
6a06a735
RK
553
554 if(nsearchvisible == nsearchresults)
555 /* We're done */
556 return;
557 /* Are any of the search tracks at/below this point? */
c3df9503
RK
558 if(!(cn == root || hash_find(searchhash, cn->path)))
559 return;
6a06a735
RK
560 D(("expand_from %d/%d visible %s",
561 nsearchvisible, nsearchresults, cn->path));
562 if(cn->flags & CN_EXPANDABLE) {
563 if(cn->flags & CN_EXPANDED)
564 /* This node is marked as expanded already. children.nvec might be 0,
565 * indicating that expansion is still underway. We should get another
566 * callback when it is expanded. */
c3df9503 567 for(n = 0; n < cn->children.nvec && gets_in_flight < 10; ++n)
6a06a735
RK
568 expand_from(cn->children.vec[n]);
569 else {
570 /* This node is not expanded yet */
571 expand_node(cn, 1);
572 }
c3df9503 573 } else {
6a06a735
RK
574 /* This is an actual search result */
575 ++nsearchvisible;
c3df9503 576 progress_window_progress(spw, nsearchvisible, nsearchresults);
fcc8b9f7 577 if(nsearchvisible == nsearchresults) {
73dd383f
RK
578 if(suppress_redisplay) {
579 suppress_redisplay = 0;
580 redisplay_tree("all search results visible");
581 }
fcc8b9f7
RK
582 /* We've got the lot. We make sure the first result is visible. */
583 cn = first_search_result(root);
584 gtk_adjustment_clamp_page(vadjust, cn->ymin, cn->ymax);
585 }
c3df9503 586 }
6a06a735
RK
587}
588
589/** @brief Contract all contingently expanded nodes below @p cn */
590static void contract_contingent(struct choosenode *cn) {
591 int n;
592
593 if(cn->flags & CN_CONTINGENT)
594 contract_node(cn);
595 else
596 for(n = 0; n < cn->children.nvec; ++n)
597 contract_contingent(cn->children.vec[n]);
598}
599
b9bdafc7 600/** @brief Contract a node */
460b9539 601static void contract_node(struct choosenode *cn) {
602 D(("contract_node %s", cn->path));
603 assert(cn->flags & CN_EXPANDABLE);
604 /* If node is already contracted do nothing. */
605 if(!(cn->flags & CN_EXPANDED)) return;
6a06a735 606 cn->flags &= ~(CN_EXPANDED|CN_CONTINGENT);
460b9539 607 /* Clear selection below this node */
608 clear_selection(cn);
e9b70a84 609 /* Zot children. We never used to do this but the result would be that over
610 * time you'd end up with the entire tree pulled into memory. If the server
611 * is over a slow network it will make interactivity slightly worse; if
612 * anyone complains we can make it an option. */
613 clear_children(cn);
460b9539 614 /* We can contract a node immediately. */
73dd383f 615 redisplay_tree("contract_node");
460b9539 616}
617
b9bdafc7 618/** @brief qsort() callback for ordering choosenodes */
460b9539 619static int compare_choosenode(const void *av, const void *bv) {
620 const struct choosenode *const *aa = av, *const *bb = bv;
621 const struct choosenode *a = *aa, *b = *bb;
622
623 return compare_tracks(a->sort, b->sort,
624 a->display, b->display,
625 a->path, b->path);
626}
627
b9bdafc7 628/** @brief Called when an expandable node is updated. */
73dd383f
RK
629static void updated_node(struct choosenode *cn, int redisplay,
630 const char *why) {
460b9539 631 D(("updated_node %s", cn->path));
632 assert(cn->flags & CN_EXPANDABLE);
633 /* It might be that the node has been de-expanded since we requested the
634 * update. In that case we ignore this notification. */
635 if(!(cn->flags & CN_EXPANDED)) return;
636 /* Sort children */
637 qsort(cn->children.vec, cn->children.nvec, sizeof (struct choosenode *),
638 compare_choosenode);
73dd383f
RK
639 if(redisplay) {
640 char whywhy[1024];
641
642 snprintf(whywhy, sizeof whywhy, "updated_node %s", why);
643 redisplay_tree(whywhy);
644 }
460b9539 645}
646
647/* Searching --------------------------------------------------------------- */
648
fcc8b9f7
RK
649/** @brief Return true if @p track is a search result
650 *
651 * In particular the return value is one more than the index of the track
652 * @p searchresults.
653 */
6a06a735 654static int is_search_result(const char *track) {
fcc8b9f7
RK
655 void *r;
656
657 if(searchhash && (r = hash_find(searchhash, track)))
658 return 1 + *(int *)r;
659 else
660 return 0;
661}
662
663/** @brief Return the first search result at or below @p cn */
664static struct choosenode *first_search_result(struct choosenode *cn) {
665 int n;
666 struct choosenode *r;
667
668 if(cn->flags & CN_EXPANDABLE) {
669 for(n = 0; n < cn->children.nvec; ++n)
670 if((r = first_search_result(cn->children.vec[n])))
671 return r;
672 } else if(is_search_result(cn->path))
673 return cn;
674 return 0;
460b9539 675}
676
b9bdafc7
RK
677/** @brief Called with a list of search results
678 *
679 * This is called from eclient with a (possibly empty) list of search results,
460b9539 680 * and also from initiate_seatch with an always empty list to indicate that
681 * we're not searching for anything in particular. */
682static void search_completed(void attribute((unused)) *v,
683 int nvec, char **vec) {
460b9539 684 int n;
c3df9503 685 char *s;
460b9539 686
687 search_in_flight = 0;
6a06a735
RK
688 /* Contract any choosenodes that were only expanded to show search
689 * results */
73dd383f 690 suppress_redisplay = 1;
6a06a735 691 contract_contingent(root);
73dd383f 692 suppress_redisplay = 0;
460b9539 693 if(search_obsolete) {
694 /* This search has been obsoleted by user input since it started.
695 * Therefore we throw away the result and search again. */
696 search_obsolete = 0;
697 initiate_search();
698 } else {
6a06a735
RK
699 /* Stash the search results */
700 searchresults = vec;
701 nsearchresults = nvec;
460b9539 702 if(nvec) {
6a06a735 703 /* Create a new search hash for fast identification of results */
fcc8b9f7 704 searchhash = hash_new(sizeof(int));
c3df9503 705 for(n = 0; n < nvec; ++n) {
fcc8b9f7
RK
706 int *const ip = xmalloc(sizeof (int *));
707 static const int minus_1 = -1;
708 *ip = n;
c3df9503 709 /* The filename itself lives in the hash */
fcc8b9f7 710 hash_add(searchhash, vec[n], ip, HASH_INSERT_OR_REPLACE);
c3df9503
RK
711 /* So do its ancestor directories */
712 for(s = vec[n] + 1; *s; ++s) {
713 if(*s == '/') {
714 *s = 0;
fcc8b9f7 715 hash_add(searchhash, vec[n], &minus_1, HASH_INSERT_OR_REPLACE);
c3df9503
RK
716 *s = '/';
717 }
718 }
719 }
6a06a735
RK
720 /* We don't yet know that the results are visible */
721 nsearchvisible = 0;
c3df9503
RK
722 if(spw) {
723 progress_window_progress(spw, 0, 0);
724 spw = 0;
725 }
726 if(nsearchresults > 50)
727 spw = progress_window_new("Fetching search results");
6a06a735
RK
728 /* Initiate expansion */
729 expand_from(root);
fcc8b9f7
RK
730 /* The search results buttons are usable */
731 gtk_widget_set_sensitive(nextsearch, 1);
732 gtk_widget_set_sensitive(prevsearch, 1);
73dd383f 733 suppress_redisplay = 1; /* avoid lots of redisplays */
6a06a735
RK
734 } else {
735 searchhash = 0; /* for the gc */
73dd383f 736 redisplay_tree("no search results"); /* remove search markers */
fcc8b9f7
RK
737 /* The search results buttons are not usable */
738 gtk_widget_set_sensitive(nextsearch, 0);
739 gtk_widget_set_sensitive(prevsearch, 0);
460b9539 740 }
741 }
742}
743
b9bdafc7
RK
744/** @brief Initiate a search
745 *
746 * If a search is underway we set @ref search_obsolete and restart the search
747 * in search_completed() above.
748 */
460b9539 749static void initiate_search(void) {
750 char *terms, *e;
751
752 /* Find out what the user is after */
753 terms = xstrdup(gtk_entry_get_text(GTK_ENTRY(searchentry)));
754 /* Strip leading and trailing space */
755 while(*terms == ' ') ++terms;
756 e = terms + strlen(terms);
757 while(e > terms && e[-1] == ' ') --e;
758 *e = 0;
759 /* If a search is already underway then mark it as obsolete. We'll revisit
760 * when it returns. */
761 if(search_in_flight) {
762 search_obsolete = 1;
763 return;
764 }
765 if(*terms) {
766 /* There's still something left. Initiate the search. */
767 if(disorder_eclient_search(client, search_completed, terms, 0)) {
768 /* The search terms are bad! We treat this as if there were no search
769 * terms at all. Some kind of feedback would be handy. */
770 fprintf(stderr, "bad terms [%s]\n", terms); /* TODO */
771 search_completed(0, 0, 0);
772 } else {
773 search_in_flight = 1;
774 }
775 } else {
776 /* No search terms - we want to see all tracks */
777 search_completed(0, 0, 0);
778 }
779}
780
b9bdafc7 781/** @brief Called when the cancel search button is clicked */
460b9539 782static void clearsearch_clicked(GtkButton attribute((unused)) *button,
783 gpointer attribute((unused)) userdata) {
784 gtk_entry_set_text(GTK_ENTRY(searchentry), "");
785}
786
fcc8b9f7
RK
787/** @brief Called when the 'next search result' button is clicked */
788static void next_clicked(GtkButton attribute((unused)) *button,
789 gpointer attribute((unused)) userdata) {
790 /* We want to find the highest (lowest ymax) track that is below the current
791 * visible range */
792 int n;
793 const gdouble bottom = gtk_adjustment_get_value(vadjust) + vadjust->page_size;
794 const struct choosenode *candidate = 0;
795
796 for(n = 0; n < nsearchresults; ++n) {
797 const struct choosenode *const cn = searchnodes[n];
798
799 if(cn
800 && cn->ymax > bottom
801 && (candidate == 0
802 || cn->ymax < candidate->ymax))
803 candidate = cn;
804 }
805 if(candidate)
806 gtk_adjustment_clamp_page(vadjust, candidate->ymin, candidate->ymax);
807}
808
809/** @brief Called when the 'previous search result' button is clicked */
810static void prev_clicked(GtkButton attribute((unused)) *button,
811 gpointer attribute((unused)) userdata) {
812 /* We want to find the lowest (greated ymax) track that is above the current
813 * visible range */
814 int n;
815 const gdouble top = gtk_adjustment_get_value(vadjust);
816 const struct choosenode *candidate = 0;
817
818 for(n = 0; n < nsearchresults; ++n) {
819 const struct choosenode *const cn = searchnodes[n];
820
821 if(cn
822 && cn->ymin < top
823 && (candidate == 0
824 || cn->ymax > candidate->ymax))
825 candidate = cn;
826 }
827 if(candidate)
828 gtk_adjustment_clamp_page(vadjust, candidate->ymin, candidate->ymax);
829}
830
460b9539 831/* Display functions ------------------------------------------------------- */
832
b9bdafc7 833/** @brief Delete all the widgets in the tree */
460b9539 834static void delete_widgets(struct choosenode *cn) {
835 int n;
836
e9b70a84 837 delete_cn_widgets(cn);
460b9539 838 for(n = 0; n < cn->children.nvec; ++n)
839 delete_widgets(cn->children.vec[n]);
840 cn->flags &= ~(CN_DISPLAYED|CN_SELECTED);
841 files_selected = 0;
842}
843
b9bdafc7 844/** @brief Update the display */
73dd383f 845static void redisplay_tree(const char *why) {
460b9539 846 struct displaydata d;
847 guint oldwidth, oldheight;
848
73dd383f
RK
849 D(("redisplay_tree %s", why));
850 if(suppress_redisplay) {
851 /*fprintf(stderr, "redisplay_tree %s suppressed\n", why);*/
852 return;
853 }
165086a0
RK
854 if(gets_in_flight) {
855 /*fprintf(stderr, "redisplay_tree %s suppressed (gets_in_flight)\n", why);*/
856 return;
857 }
81cd1894
RK
858 INIT();
859 BEGIN(total);
73dd383f 860 /*fprintf(stderr, "redisplay_tree %s *** NOT SUPPRESSED ***\n", why);*/
460b9539 861 /* We'll count these up empirically each time */
862 files_selected = 0;
863 files_visible = 0;
864 /* Correct the layout and find out how much space it uses */
e1d4a32b 865 MTAG_PUSH("display_tree");
fcc8b9f7
RK
866 searchnodes = nsearchresults ? xcalloc(nsearchresults,
867 sizeof (struct choosenode *)) : 0;
460b9539 868 d = display_tree(root, 0, 0);
e1d4a32b 869 MTAG_POP();
81cd1894
RK
870
871 BEGIN(gtkbits);
460b9539 872 /* We must set the total size or scrolling will not work (it wouldn't be hard
873 * for GtkLayout to figure it out for itself but presumably you're supposed
874 * to be able to have widgets off the edge of the layuot.)
875 *
876 * There is a problem: if we shrink the size then the part of the screen that
877 * is outside the new size but inside the old one is not updated. I think
878 * this is arguably bug in GTK+ but it's easy to force a redraw if this
879 * region is nonempty.
880 */
881 gtk_layout_get_size(GTK_LAYOUT(chooselayout), &oldwidth, &oldheight);
882 if(oldwidth > d.width || oldheight > d.height)
883 gtk_widget_queue_draw(chooselayout);
884 gtk_layout_set_size(GTK_LAYOUT(chooselayout), d.width, d.height);
81cd1894 885 END(gtkbits);
460b9539 886 /* Notify the main menu of any recent changes */
81cd1894 887 BEGIN(menuupdate);
460b9539 888 menu_update(-1);
81cd1894
RK
889 END(menuupdate);
890 END(total);
891 REPORT();
460b9539 892}
893
b9bdafc7 894/** @brief Recursive step for redisplay_tree()
fcc8b9f7
RK
895 * @param cn Node to display
896 * @param x X coordinate for @p cn
897 * @param y Y coordinate for @p cn
b9bdafc7
RK
898 *
899 * Makes sure all displayed widgets from CN down exist and are in their proper
900 * place and return the maximum space used.
901 */
460b9539 902static struct displaydata display_tree(struct choosenode *cn, int x, int y) {
903 int n, aw;
904 GtkRequisition req;
905 struct displaydata d, cd;
906 GdkPixbuf *pb;
fcc8b9f7 907 const int search_result = is_search_result(cn->path);
460b9539 908
909 D(("display_tree %s %d,%d", cn->path, x, y));
910
911 /* An expandable item contains an arrow and a text label. When you press the
912 * button it flips its expand state.
913 *
914 * A non-expandable item has just a text label and no arrow.
915 */
916 if(!cn->container) {
81cd1894 917 BEGIN(new_widgets);
521b8841 918 MTAG_PUSH("make_widgets_1");
460b9539 919 /* Widgets need to be created */
e9b70a84 920 NW(hbox);
460b9539 921 cn->hbox = gtk_hbox_new(FALSE, 1);
922 if(cn->flags & CN_EXPANDABLE) {
e9b70a84 923 NW(arrow);
460b9539 924 cn->arrow = gtk_arrow_new(cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN
925 : GTK_ARROW_RIGHT,
926 GTK_SHADOW_NONE);
927 cn->marker = 0;
928 } else {
929 cn->arrow = 0;
e9b70a84 930 if((pb = find_image("notes.png"))) {
931 NW(image);
460b9539 932 cn->marker = gtk_image_new_from_pixbuf(pb);
e9b70a84 933 }
460b9539 934 }
521b8841 935 MTAG_POP();
936 MTAG_PUSH("make_widgets_2");
e9b70a84 937 NW(label);
460b9539 938 cn->label = gtk_label_new(cn->display);
939 if(cn->arrow)
940 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->arrow);
941 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->label);
942 if(cn->marker)
943 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->marker);
521b8841 944 MTAG_POP();
945 MTAG_PUSH("make_widgets_3");
e9b70a84 946 NW(event_box);
460b9539 947 cn->container = gtk_event_box_new();
948 gtk_container_add(GTK_CONTAINER(cn->container), cn->hbox);
949 g_signal_connect(cn->container, "button-release-event",
950 G_CALLBACK(clicked_choosenode), cn);
951 g_signal_connect(cn->container, "button-press-event",
952 G_CALLBACK(clicked_choosenode), cn);
953 g_object_ref(cn->container);
460b9539 954 /* Show everything by default */
955 gtk_widget_show_all(cn->container);
e1d4a32b 956 MTAG_POP();
81cd1894 957 END(new_widgets);
460b9539 958 }
959 assert(cn->container);
d4ef4132 960 /* Set colors */
81cd1894 961 BEGIN(colors);
33288048
RK
962 if(search_result) {
963 gtk_widget_set_style(cn->container, search_style);
964 gtk_widget_set_style(cn->label, search_style);
965 } else {
966 gtk_widget_set_style(cn->container, layout_style);
967 gtk_widget_set_style(cn->label, layout_style);
968 }
81cd1894 969 END(colors);
460b9539 970 /* Make sure the icon is right */
81cd1894 971 BEGIN(markers);
460b9539 972 if(cn->flags & CN_EXPANDABLE)
973 gtk_arrow_set(GTK_ARROW(cn->arrow),
974 cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN : GTK_ARROW_RIGHT,
975 GTK_SHADOW_NONE);
976 else if(cn->marker)
977 /* Make sure the queued marker is right */
978 /* TODO: doesn't always work */
979 (queued(cn->path) ? gtk_widget_show : gtk_widget_hide)(cn->marker);
81cd1894 980 END(markers);
460b9539 981 /* Put the widget in the right place */
81cd1894 982 BEGIN(location);
460b9539 983 if(cn->flags & CN_DISPLAYED)
984 gtk_layout_move(GTK_LAYOUT(chooselayout), cn->container, x, y);
985 else {
986 gtk_layout_put(GTK_LAYOUT(chooselayout), cn->container, x, y);
987 cn->flags |= CN_DISPLAYED;
521b8841 988 /* Now chooselayout has a ref to the container */
989 g_object_unref(cn->container);
460b9539 990 }
81cd1894 991 END(location);
460b9539 992 /* Set the widget's selection status */
81cd1894 993 BEGIN(selection);
460b9539 994 if(!(cn->flags & CN_EXPANDABLE))
995 display_selection(cn);
81cd1894 996 END(selection);
460b9539 997 /* Find the size used so we can get vertical positioning right. */
998 gtk_widget_size_request(cn->container, &req);
999 d.width = x + req.width;
1000 d.height = y + req.height;
7e60b670
RK
1001 cn->ymin = y;
1002 cn->ymax = d.height;
460b9539 1003 if(cn->flags & CN_EXPANDED) {
1004 /* We'll offset children by the size of the arrow whatever it might be. */
1005 assert(cn->arrow);
1006 gtk_widget_size_request(cn->arrow, &req);
1007 aw = req.width;
1008 for(n = 0; n < cn->children.nvec; ++n) {
1009 cd = display_tree(cn->children.vec[n], x + aw, d.height);
1010 if(cd.width > d.width)
1011 d.width = cd.width;
1012 d.height = cd.height;
1013 }
1014 } else {
81cd1894 1015 BEGIN(undisplay);
460b9539 1016 for(n = 0; n < cn->children.nvec; ++n)
1017 undisplay_tree(cn->children.vec[n]);
81cd1894 1018 END(undisplay);
460b9539 1019 }
1020 if(!(cn->flags & CN_EXPANDABLE)) {
1021 ++files_visible;
1022 if(cn->flags & CN_SELECTED)
1023 ++files_selected;
1024 }
fcc8b9f7
RK
1025 /* update the search results array */
1026 if(search_result)
1027 searchnodes[search_result - 1] = cn;
460b9539 1028 /* report back how much space we used */
1029 D(("display_tree %s %d,%d total size %dx%d", cn->path, x, y,
1030 d.width, d.height));
1031 return d;
1032}
1033
b9bdafc7 1034/** @brief Remove widgets for newly hidden nodes */
460b9539 1035static void undisplay_tree(struct choosenode *cn) {
1036 int n;
1037
1038 D(("undisplay_tree %s", cn->path));
1039 /* Remove this widget from the display */
1040 if(cn->flags & CN_DISPLAYED) {
1041 gtk_container_remove(GTK_CONTAINER(chooselayout), cn->container);
1042 cn->flags ^= CN_DISPLAYED;
1043 }
1044 /* Remove children too */
1045 for(n = 0; n < cn->children.nvec; ++n)
1046 undisplay_tree(cn->children.vec[n]);
1047}
1048
1049/* Selection --------------------------------------------------------------- */
1050
b9bdafc7 1051/** @brief Mark the widget @p cn according to its selection state */
460b9539 1052static void display_selection(struct choosenode *cn) {
1053 /* Need foreground and background colors */
1054 gtk_widget_set_state(cn->label, (cn->flags & CN_SELECTED
1055 ? GTK_STATE_SELECTED : GTK_STATE_NORMAL));
1056 gtk_widget_set_state(cn->container, (cn->flags & CN_SELECTED
1057 ? GTK_STATE_SELECTED : GTK_STATE_NORMAL));
1058}
1059
b9bdafc7
RK
1060/** @brief Set the selection state of a widget
1061 *
1062 * Directories can never be selected, we just ignore attempts to do so. */
460b9539 1063static void set_selection(struct choosenode *cn, int selected) {
1064 unsigned f = selected ? CN_SELECTED : 0;
1065
1066 D(("set_selection %d %s", selected, cn->path));
1067 if(!(cn->flags & CN_EXPANDABLE) && (cn->flags & CN_SELECTED) != f) {
1068 cn->flags ^= CN_SELECTED;
1069 /* Maintain selection count */
1070 if(selected)
1071 ++files_selected;
1072 else
1073 --files_selected;
1074 display_selection(cn);
1075 /* Update main menu sensitivity */
1076 menu_update(-1);
1077 }
1078}
1079
b9bdafc7 1080/** @brief Recursively clear all selection bits from CN down */
460b9539 1081static void clear_selection(struct choosenode *cn) {
1082 int n;
1083
1084 set_selection(cn, 0);
1085 for(n = 0; n < cn->children.nvec; ++n)
1086 clear_selection(cn->children.vec[n]);
1087}
1088
1089/* User actions ------------------------------------------------------------ */
1090
b9bdafc7
RK
1091/** @brief Clicked on something
1092 *
1093 * This implements playing, all the modifiers for selection, etc.
1094 */
460b9539 1095static void clicked_choosenode(GtkWidget attribute((unused)) *widget,
1096 GdkEventButton *event,
1097 gpointer user_data) {
1098 struct choosenode *cn = user_data;
1099 int ind, last_ind, n;
1100
1101 D(("clicked_choosenode %s", cn->path));
1102 if(event->type == GDK_BUTTON_RELEASE
1103 && event->button == 1) {
1104 /* Left click */
1105 if(cn->flags & CN_EXPANDABLE) {
1106 /* This is a directory. Flip its expansion status. */
1107 if(cn->flags & CN_EXPANDED)
1108 contract_node(cn);
1109 else
6a06a735 1110 expand_node(cn, 0/*!contingent*/);
460b9539 1111 last_click = 0;
1112 } else {
1113 /* This is a file. Adjust selection status */
1114 /* TODO the basic logic here is essentially the same as that in queue.c.
1115 * Can we share code at all? */
1116 switch(event->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) {
1117 case 0:
1118 clear_selection(root);
1119 set_selection(cn, 1);
1120 last_click = cn;
1121 break;
1122 case GDK_CONTROL_MASK:
1123 set_selection(cn, !(cn->flags & CN_SELECTED));
1124 last_click = cn;
1125 break;
1126 case GDK_SHIFT_MASK:
1127 case GDK_SHIFT_MASK|GDK_CONTROL_MASK:
1128 if(last_click && last_click->parent == cn->parent) {
1129 /* Figure out where the current and last clicks are in the list */
1130 ind = last_ind = -1;
1131 for(n = 0; n < cn->parent->children.nvec; ++n) {
1132 if(cn->parent->children.vec[n] == cn)
1133 ind = n;
1134 if(cn->parent->children.vec[n] == last_click)
1135 last_ind = n;
1136 }
1137 /* Test shouldn't ever fail, but still */
1138 if(ind >= 0 && last_ind >= 0) {
1139 if(!(event->state & GDK_CONTROL_MASK)) {
1140 for(n = 0; n < cn->parent->children.nvec; ++n)
1141 set_selection(cn->parent->children.vec[n], 0);
1142 }
1143 if(ind > last_ind)
1144 for(n = last_ind; n <= ind; ++n)
1145 set_selection(cn->parent->children.vec[n], 1);
1146 else
1147 for(n = ind; n <= last_ind; ++n)
1148 set_selection(cn->parent->children.vec[n], 1);
1149 if(event->state & GDK_CONTROL_MASK)
1150 last_click = cn;
1151 }
1152 }
b9bdafc7
RK
1153 /* TODO trying to select a range that doesn't share a single parent
1154 * currently does not work, but it ought to. */
460b9539 1155 break;
1156 }
1157 }
1158 } else if(event->type == GDK_BUTTON_RELEASE
1159 && event->button == 2) {
1160 /* Middle click - play the pointed track */
1161 if(!(cn->flags & CN_EXPANDABLE)) {
1162 clear_selection(root);
1163 set_selection(cn, 1);
1164 gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
1165 disorder_eclient_play(client, cn->path, 0, 0);
1166 last_click = 0;
1167 }
1168 } else if(event->type == GDK_BUTTON_PRESS
1169 && event->button == 3) {
16e145a5
RK
1170 struct choose_menuitem *const menuitems =
1171 (cn->flags & CN_EXPANDABLE ? dir_menuitems : track_menuitems);
1172 GtkWidget *const menu =
1173 (cn->flags & CN_EXPANDABLE ? dir_menu : track_menu);
460b9539 1174 /* Right click. Pop up a menu. */
1175 /* If the current file isn't selected, switch the selection to just that.
1176 * (If we're looking at a directory then leave the selection alone.) */
1177 if(!(cn->flags & CN_EXPANDABLE) && !(cn->flags & CN_SELECTED)) {
1178 clear_selection(root);
1179 set_selection(cn, 1);
1180 last_click = cn;
1181 }
1182 /* Set the item sensitivity and callbacks */
16e145a5 1183 for(n = 0; menuitems[n].name; ++n) {
460b9539 1184 if(menuitems[n].handlerid)
1185 g_signal_handler_disconnect(menuitems[n].w,
1186 menuitems[n].handlerid);
1187 gtk_widget_set_sensitive(menuitems[n].w,
1188 menuitems[n].sensitive(cn));
1189 menuitems[n].handlerid = g_signal_connect
1190 (menuitems[n].w, "activate", G_CALLBACK(menuitems[n].activate), cn);
1191 }
d4ef4132 1192 set_tool_colors(menu);
460b9539 1193 /* Pop up the menu */
1194 gtk_widget_show_all(menu);
1195 gtk_menu_popup(GTK_MENU(menu), 0, 0, 0, 0,
1196 event->button, event->time);
1197 }
1198}
1199
b9bdafc7 1200/** @brief Called BY GTK+ to tell us the search entry box has changed */
460b9539 1201static void searchentry_changed(GtkEditable attribute((unused)) *editable,
1202 gpointer attribute((unused)) user_data) {
1203 initiate_search();
1204}
1205
16e145a5 1206/* Track menu items -------------------------------------------------------- */
460b9539 1207
b9bdafc7 1208/** @brief Recursive step for gather_selected() */
460b9539 1209static void recurse_selected(struct choosenode *cn, struct vector *v) {
1210 int n;
1211
1212 if(cn->flags & CN_EXPANDABLE) {
1213 if(cn->flags & CN_EXPANDED)
1214 for(n = 0; n < cn->children.nvec; ++n)
1215 recurse_selected(cn->children.vec[n], v);
1216 } else {
1217 if((cn->flags & CN_SELECTED) && cn->path)
1218 vector_append(v, (char *)cn->path);
1219 }
1220}
1221
b9bdafc7 1222/*** @brief Get a list of all the selected tracks */
16e145a5 1223static const char **gather_selected(int *ntracks) {
460b9539 1224 struct vector v;
1225
1226 vector_init(&v);
1227 recurse_selected(root, &v);
1228 vector_terminate(&v);
1229 if(ntracks) *ntracks = v.nvec;
16e145a5 1230 return (const char **)v.vec;
460b9539 1231}
1232
16e145a5
RK
1233/** @brief Called when the track menu's play option is activated */
1234static void activate_track_play(GtkMenuItem attribute((unused)) *menuitem,
1235 gpointer attribute((unused)) user_data) {
1236 const char **tracks = gather_selected(0);
460b9539 1237 int n;
1238
1239 gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
1240 for(n = 0; tracks[n]; ++n)
1241 disorder_eclient_play(client, tracks[n], 0, 0);
1242}
1243
b9bdafc7 1244/** @brief Called when the menu's properties option is activated */
16e145a5
RK
1245static void activate_track_properties(GtkMenuItem attribute((unused)) *menuitem,
1246 gpointer attribute((unused)) user_data) {
460b9539 1247 int ntracks;
16e145a5 1248 const char **tracks = gather_selected(&ntracks);
460b9539 1249
1250 properties(ntracks, tracks);
1251}
1252
b9bdafc7 1253/** @brief Determine whether the menu's play option should be sensitive */
16e145a5 1254static gboolean sensitive_track_play(struct choosenode attribute((unused)) *cn) {
8f763f1b
RK
1255 return (!!files_selected
1256 && (disorder_eclient_state(client) & DISORDER_CONNECTED));
460b9539 1257}
1258
b9bdafc7 1259/** @brief Determine whether the menu's properties option should be sensitive */
16e145a5 1260static gboolean sensitive_track_properties(struct choosenode attribute((unused)) *cn) {
8f763f1b 1261 return !!files_selected && (disorder_eclient_state(client) & DISORDER_CONNECTED);
460b9539 1262}
1263
16e145a5
RK
1264/* Directory menu items ---------------------------------------------------- */
1265
1266/** @brief Return the file children of @p cn
1267 *
1268 * The list is terminated by a null pointer.
1269 */
1270static const char **dir_files(struct choosenode *cn, int *nfiles) {
1271 const char **files = xcalloc(cn->children.nvec + 1, sizeof (char *));
1272 int n, m;
1273
1274 for(n = m = 0; n < cn->children.nvec; ++n)
1275 if(!(cn->children.vec[n]->flags & CN_EXPANDABLE))
1276 files[m++] = cn->children.vec[n]->path;
1277 files[m] = 0;
1278 if(nfiles) *nfiles = m;
1279 return files;
1280}
1281
1282static void play_dir(struct choosenode *cn,
1283 void attribute((unused)) *wfu) {
1284 int ntracks, n;
1285 const char **tracks = dir_files(cn, &ntracks);
1286
1287 gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
1288 for(n = 0; n < ntracks; ++n)
1289 disorder_eclient_play(client, tracks[n], 0, 0);
1290}
1291
1292static void properties_dir(struct choosenode *cn,
1293 void attribute((unused)) *wfu) {
1294 int ntracks;
1295 const char **tracks = dir_files(cn, &ntracks);
1296
1297 properties(ntracks, tracks);
1298}
1299
1300static void select_dir(struct choosenode *cn,
1301 void attribute((unused)) *wfu) {
1302 int n;
1303
1304 clear_selection(root);
1305 for(n = 0; n < cn->children.nvec; ++n)
1306 set_selection(cn->children.vec[n], 1);
1307}
1308
1309/** @brief Ensure @p cn is expanded and then call @p callback */
1310static void call_with_dir(struct choosenode *cn,
1311 when_filled_callback *whenfilled,
1312 void *wfu) {
1313 if(!(cn->flags & CN_EXPANDABLE))
1314 return; /* something went wrong */
1315 if(cn->flags & CN_EXPANDED)
1316 /* @p cn is already open */
1317 whenfilled(cn, wfu);
1318 else {
1319 /* @p cn is not open, arrange for the callback to go off when it is
1320 * opened */
1321 cn->whenfilled = whenfilled;
1322 cn->wfu = wfu;
6a06a735 1323 expand_node(cn, 0/*not contingnet upon search*/);
16e145a5
RK
1324 }
1325}
1326
1327/** @brief Called when the directory menu's play option is activated */
1328static void activate_dir_play(GtkMenuItem attribute((unused)) *menuitem,
1329 gpointer user_data) {
1330 struct choosenode *const cn = (struct choosenode *)user_data;
1331
1332 call_with_dir(cn, play_dir, 0);
1333}
1334
1335/** @brief Called when the directory menu's properties option is activated */
1336static void activate_dir_properties(GtkMenuItem attribute((unused)) *menuitem,
1337 gpointer user_data) {
1338 struct choosenode *const cn = (struct choosenode *)user_data;
1339
1340 call_with_dir(cn, properties_dir, 0);
1341}
1342
1343/** @brief Called when the directory menu's select option is activated */
1344static void activate_dir_select(GtkMenuItem attribute((unused)) *menuitem,
1345 gpointer user_data) {
1346 struct choosenode *const cn = (struct choosenode *)user_data;
1347
1348 call_with_dir(cn, select_dir, 0);
1349}
1350
1351/** @brief Determine whether the directory menu's play option should be sensitive */
1352static gboolean sensitive_dir_play(struct choosenode attribute((unused)) *cn) {
1353 return !!(disorder_eclient_state(client) & DISORDER_CONNECTED);
1354}
1355
1356/** @brief Determine whether the directory menu's properties option should be sensitive */
1357static gboolean sensitive_dir_properties(struct choosenode attribute((unused)) *cn) {
1358 return !!(disorder_eclient_state(client) & DISORDER_CONNECTED);
1359}
1360
1361/** @brief Determine whether the directory menu's select option should be sensitive */
1362static gboolean sensitive_dir_select(struct choosenode attribute((unused)) *cn) {
1363 return TRUE;
1364}
1365
1366
1367
460b9539 1368/* Main menu plumbing ------------------------------------------------------ */
1369
b9bdafc7 1370/** @brief Determine whether the edit menu's properties option should be sensitive */
460b9539 1371static int choose_properties_sensitive(GtkWidget attribute((unused)) *w) {
8f763f1b 1372 return !!files_selected && (disorder_eclient_state(client) & DISORDER_CONNECTED);
460b9539 1373}
1374
b9bdafc7
RK
1375/** @brief Determine whether the edit menu's select all option should be sensitive
1376 *
1377 * TODO not implemented, see also choose_selectall_activate()
1378 */
460b9539 1379static int choose_selectall_sensitive(GtkWidget attribute((unused)) *w) {
b9bdafc7 1380 return FALSE;
460b9539 1381}
1382
fb009628
RK
1383/** @brief Determine whether the edit menu's select none option should be sensitive
1384 *
1385 * TODO not implemented, see also choose_selectnone_activate()
1386 */
1387static int choose_selectnone_sensitive(GtkWidget attribute((unused)) *w) {
1388 return FALSE;
1389}
1390
b9bdafc7 1391/** @brief Called when the edit menu's properties option is activated */
460b9539 1392static void choose_properties_activate(GtkWidget attribute((unused)) *w) {
16e145a5 1393 activate_track_properties(0, 0);
460b9539 1394}
1395
b9bdafc7
RK
1396/** @brief Called when the edit menu's select all option is activated
1397 *
1398 * TODO not implemented, see choose_selectall_sensitive() */
460b9539 1399static void choose_selectall_activate(GtkWidget attribute((unused)) *w) {
460b9539 1400}
1401
fb009628
RK
1402/** @brief Called when the edit menu's select none option is activated
1403 *
1404 * TODO not implemented, see choose_selectnone_sensitive() */
1405static void choose_selectnone_activate(GtkWidget attribute((unused)) *w) {
1406}
1407
b9bdafc7 1408/** @brief Main menu callbacks for Choose screen */
460b9539 1409static const struct tabtype tabtype_choose = {
1410 choose_properties_sensitive,
1411 choose_selectall_sensitive,
fb009628 1412 choose_selectnone_sensitive,
460b9539 1413 choose_properties_activate,
1414 choose_selectall_activate,
fb009628 1415 choose_selectnone_activate,
460b9539 1416};
1417
1418/* Public entry points ----------------------------------------------------- */
1419
73f1b9f3
RK
1420/** @brief Called to entirely reset the choose screen */
1421static void choose_reset(void) {
1422 if(root)
1423 undisplay_tree(root);
1424 root = newnode(0/*parent*/, "<root>", "All files", "",
1425 CN_EXPANDABLE, fill_root_node);
1426 expand_node(root, 0); /* will call redisplay_tree */
1427}
1428
b9bdafc7 1429/** @brief Create a track choice widget */
460b9539 1430GtkWidget *choose_widget(void) {
1431 int n;
1432 GtkWidget *scrolled;
1433 GtkWidget *vbox, *hbox, *clearsearch;
1434
1435 /*
1436 * +--vbox-------------------------------------------------------+
1437 * | +-hbox----------------------------------------------------+ |
1438 * | | searchentry | clearsearch | |
1439 * | +---------------------------------------------------------+ |
1440 * | +-scrolled------------------------------------------------+ |
1441 * | | +-chooselayout------------------------------------++--+ | |
1442 * | | | Tree structure is manually layed out in here ||^^| | |
1443 * | | | || | | |
1444 * | | | || | | |
1445 * | | | || | | |
1446 * | | | ||vv| | |
1447 * | | +-------------------------------------------------++--+ | |
1448 * | | +-------------------------------------------------+ | |
1449 * | | |< >| | |
1450 * | | +-------------------------------------------------+ | |
1451 * | +---------------------------------------------------------+ |
1452 * +-------------------------------------------------------------+
1453 */
1454
1455 /* Text entry box for search terms */
e9b70a84 1456 NW(entry);
460b9539 1457 searchentry = gtk_entry_new();
33288048 1458 gtk_widget_set_style(searchentry, tool_style);
460b9539 1459 g_signal_connect(searchentry, "changed", G_CALLBACK(searchentry_changed), 0);
a8c9507f 1460 gtk_tooltips_set_tip(tips, searchentry, "Enter search terms here; search is automatic", "");
460b9539 1461
1462 /* Cancel button to clear the search */
e9b70a84 1463 NW(button);
460b9539 1464 clearsearch = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
33288048 1465 gtk_widget_set_style(clearsearch, tool_style);
460b9539 1466 g_signal_connect(G_OBJECT(clearsearch), "clicked",
1467 G_CALLBACK(clearsearch_clicked), 0);
a8c9507f
RK
1468 gtk_tooltips_set_tip(tips, clearsearch, "Clear search terms", "");
1469
fcc8b9f7
RK
1470 /* Up and down buttons to find previous/next results; initially they are not
1471 * usable as there are no search results. */
1472 prevsearch = iconbutton("up.png", "Previous search result");
1473 g_signal_connect(G_OBJECT(prevsearch), "clicked",
1474 G_CALLBACK(prev_clicked), 0);
33288048 1475 gtk_widget_set_style(prevsearch, tool_style);
fcc8b9f7
RK
1476 gtk_widget_set_sensitive(prevsearch, 0);
1477 nextsearch = iconbutton("down.png", "Next search result");
1478 g_signal_connect(G_OBJECT(nextsearch), "clicked",
1479 G_CALLBACK(next_clicked), 0);
33288048 1480 gtk_widget_set_style(nextsearch, tool_style);
fcc8b9f7
RK
1481 gtk_widget_set_sensitive(nextsearch, 0);
1482
1483 /* hbox packs the search tools button together on a line */
e9b70a84 1484 NW(hbox);
460b9539 1485 hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*spacing*/);
1486 gtk_box_pack_start(GTK_BOX(hbox), searchentry,
1487 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
fcc8b9f7
RK
1488 gtk_box_pack_start(GTK_BOX(hbox), prevsearch,
1489 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
1490 gtk_box_pack_start(GTK_BOX(hbox), nextsearch,
1491 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
1492 gtk_box_pack_start(GTK_BOX(hbox), clearsearch,
1493 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
460b9539 1494
1495 /* chooselayout contains the currently visible subset of the track
1496 * namespace */
e9b70a84 1497 NW(layout);
460b9539 1498 chooselayout = gtk_layout_new(0, 0);
33288048 1499 gtk_widget_set_style(chooselayout, layout_style);
73f1b9f3
RK
1500 choose_reset();
1501 register_reset(choose_reset);
16e145a5
RK
1502 /* Create the popup menus */
1503 NW(menu);
1504 track_menu = gtk_menu_new();
1505 g_signal_connect(track_menu, "destroy", G_CALLBACK(gtk_widget_destroyed),
1506 &track_menu);
1507 for(n = 0; track_menuitems[n].name; ++n) {
1508 NW(menu_item);
1509 track_menuitems[n].w =
1510 gtk_menu_item_new_with_label(track_menuitems[n].name);
1511 gtk_menu_attach(GTK_MENU(track_menu), track_menuitems[n].w,
1512 0, 1, n, n + 1);
1513 }
e9b70a84 1514 NW(menu);
16e145a5
RK
1515 dir_menu = gtk_menu_new();
1516 g_signal_connect(dir_menu, "destroy", G_CALLBACK(gtk_widget_destroyed),
1517 &dir_menu);
1518 for(n = 0; dir_menuitems[n].name; ++n) {
e9b70a84 1519 NW(menu_item);
16e145a5
RK
1520 dir_menuitems[n].w =
1521 gtk_menu_item_new_with_label(dir_menuitems[n].name);
1522 gtk_menu_attach(GTK_MENU(dir_menu), dir_menuitems[n].w,
1523 0, 1, n, n + 1);
460b9539 1524 }
1525 /* The layout is scrollable */
d4ef4132 1526 scrolled = scroll_widget(chooselayout);
7e60b670 1527 vadjust = gtk_layout_get_vadjustment(GTK_LAYOUT(chooselayout));
460b9539 1528
1529 /* The scrollable layout and the search hbox go together in a vbox */
e9b70a84 1530 NW(vbox);
460b9539 1531 vbox = gtk_vbox_new(FALSE/*homogenous*/, 1/*spacing*/);
1532 gtk_box_pack_start(GTK_BOX(vbox), hbox,
1533 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
1534 gtk_box_pack_end(GTK_BOX(vbox), scrolled,
1535 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
1536
1537 g_object_set_data(G_OBJECT(vbox), "type", (void *)&tabtype_choose);
1538 return vbox;
1539}
1540
b9bdafc7 1541/** @brief Called when something we care about here might have changed */
460b9539 1542void choose_update(void) {
73dd383f 1543 redisplay_tree("choose_update");
460b9539 1544}
1545
1546/*
1547Local Variables:
1548c-basic-offset:2
1549comment-column:40
1550fill-column:79
1551indent-tabs-mode:nil
1552End:
1553*/