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