chiark / gitweb /
Start using event_*() calls in Disobedience.
[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);
4190a4d9 217static void got_files(void *v, const char *error, int nvec, char **vec);
06bfbba4 218static void got_resolved_file(void *v, const char *error, const char *track);
4190a4d9 219static void got_dirs(void *v, const char *error, int nvec, char **vec);
460b9539 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 D(("fill_root_node"));
77d95881 362 clear_children(cn);
c3df9503
RK
363 /* More de-duping possible here */
364 if(cn->flags & CN_GETTING_ANY)
365 return;
366 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
4190a4d9
RK
367 disorder_eclient_dirs(client, got_dirs, "", 0, cn);
368 disorder_eclient_files(client, got_files, "", 0, cn);
c3df9503
RK
369 cn->flags |= CN_GETTING_FILES|CN_GETTING_DIRS;
370 gets_in_flight += 2;
460b9539 371}
372
b9bdafc7 373/** @brief Delete all the widgets owned by @p cn */
e9b70a84 374static void delete_cn_widgets(struct choosenode *cn) {
375 if(cn->arrow) {
376 DW(arrow);
377 gtk_widget_destroy(cn->arrow);
378 cn->arrow = 0;
379 }
380 if(cn->label) {
381 DW(label);
382 gtk_widget_destroy(cn->label);
383 cn->label = 0;
384 }
385 if(cn->marker) {
386 DW(image);
387 gtk_widget_destroy(cn->marker);
388 cn->marker = 0;
389 }
390 if(cn->hbox) {
391 DW(hbox);
392 gtk_widget_destroy(cn->hbox);
393 cn->hbox = 0;
394 }
395 if(cn->container) {
396 DW(event_box);
397 gtk_widget_destroy(cn->container);
398 cn->container = 0;
399 }
400}
401
b9bdafc7
RK
402/** @brief Recursively clear all the children of @p cn
403 *
404 * All the widgets at or below @p cn are deleted. All choosenodes below
405 * it are emptied. i.e. we prune the tree at @p cn.
406 */
460b9539 407static void clear_children(struct choosenode *cn) {
408 int n;
409
410 D(("clear_children %s", cn->path));
411 /* Recursively clear subtrees */
412 for(n = 0; n < cn->children.nvec; ++n) {
413 clear_children(cn->children.vec[n]);
e9b70a84 414 delete_cn_widgets(cn->children.vec[n]);
460b9539 415 }
416 cn->children.nvec = 0;
417}
418
b9bdafc7 419/** @brief Called with a list of files just below some node */
4190a4d9
RK
420static void got_files(void *v, const char *error, int nvec, char **vec) {
421 struct choosenode *cn = v;
460b9539 422 int n;
423
4190a4d9
RK
424 if(error) {
425 popup_protocol_error(0, error);
426 return;
427 }
428
734df821 429 D(("got_files %d files for %s %s", nvec, cn->path, cnflags(cn)));
06bfbba4 430 /* Complicated by the need to resolve aliases. */
16e145a5 431 cn->flags &= ~CN_GETTING_FILES;
c3df9503 432 --gets_in_flight;
5459347d
RK
433 if((cn->pending = nvec)) {
434 cn->flags |= CN_RESOLVING_FILES;
c3df9503 435 for(n = 0; n < nvec; ++n) {
06bfbba4 436 disorder_eclient_resolve(client, got_resolved_file, vec[n], cn);
c3df9503
RK
437 ++gets_in_flight;
438 }
5459347d 439 }
6a06a735
RK
440 /* If there are no files and the directories are all read by now, we're
441 * done */
442 if(!(cn->flags & CN_GETTING_ANY))
443 filled(cn);
cf1f8eef
RK
444 if(!gets_in_flight)
445 redisplay_tree("got_files");
460b9539 446}
447
b9bdafc7 448/** @brief Called with an alias resolved filename */
06bfbba4
RK
449static void got_resolved_file(void *v, const char *error, const char *track) {
450 struct choosenode *const cn = v, *file_cn;
451
452 if(error) {
453 popup_protocol_error(0, error);
454 } else {
455 D(("resolved %s %s %d left", cn->path, cnflags(cn), cn->pending - 1));
456 /* TODO as below */
457 file_cn = newnode(cn, track,
458 trackname_transform("track", track, "display"),
459 trackname_transform("track", track, "sort"),
460 0/*flags*/, 0/*fill*/);
461 --gets_in_flight;
462 /* Only bother updating when we've got the lot */
463 if(--cn->pending == 0) {
464 cn->flags &= ~CN_RESOLVING_FILES;
465 updated_node(cn, gets_in_flight == 0, "got_resolved_file");
466 if(!(cn->flags & CN_GETTING_ANY))
467 filled(cn);
468 }
16e145a5 469 }
460b9539 470}
471
b9bdafc7 472/** @brief Called with a list of directories just below some node */
4190a4d9
RK
473static void got_dirs(void *v, const char *error, int nvec, char **vec) {
474 struct choosenode *cn = v;
460b9539 475 int n;
4190a4d9
RK
476
477 if(error) {
478 popup_protocol_error(0, error);
479 return;
480 }
460b9539 481
734df821 482 D(("got_dirs %d dirs for %s %s", nvec, cn->path, cnflags(cn)));
b9bdafc7
RK
483 /* TODO this depends on local configuration for trackname_transform().
484 * This will work, since the defaults are now built-in, but it'll be
485 * (potentially) different to the server's configured settings.
486 *
487 * Really we want a variant of files/dirs that produces both the
488 * raw filename and the transformed name for a chosen context.
489 */
c3df9503 490 --gets_in_flight;
460b9539 491 for(n = 0; n < nvec; ++n)
492 newnode(cn, vec[n],
493 trackname_transform("dir", vec[n], "display"),
494 trackname_transform("dir", vec[n], "sort"),
495 CN_EXPANDABLE, fill_directory_node);
cf1f8eef 496 updated_node(cn, gets_in_flight == 0, "got_dirs");
16e145a5
RK
497 cn->flags &= ~CN_GETTING_DIRS;
498 if(!(cn->flags & CN_GETTING_ANY))
499 filled(cn);
460b9539 500}
501
b9bdafc7 502/** @brief Fill a child node */
460b9539 503static void fill_directory_node(struct choosenode *cn) {
504 struct callbackdata *cbd;
505
506 D(("fill_directory_node %s", cn->path));
507 /* TODO: caching */
16e145a5
RK
508 if(cn->flags & CN_GETTING_ANY)
509 return;
460b9539 510 assert(report_label != 0);
511 gtk_label_set_text(GTK_LABEL(report_label), "getting files");
1d0c28c7 512 clear_children(cn);
460b9539 513 cbd = xmalloc(sizeof *cbd);
514 cbd->u.choosenode = cn;
515 disorder_eclient_dirs(client, got_dirs, cn->path, 0, cbd);
516 cbd = xmalloc(sizeof *cbd);
517 cbd->u.choosenode = cn;
518 disorder_eclient_files(client, got_files, cn->path, 0, cbd);
16e145a5 519 cn->flags |= CN_GETTING_FILES|CN_GETTING_DIRS;
c3df9503 520 gets_in_flight += 2;
460b9539 521}
522
b9bdafc7 523/** @brief Expand a node */
6a06a735 524static void expand_node(struct choosenode *cn, int contingent) {
734df821 525 D(("expand_node %s %d %s", cn->path, contingent, cnflags(cn)));
460b9539 526 assert(cn->flags & CN_EXPANDABLE);
527 /* If node is already expanded do nothing. */
528 if(cn->flags & CN_EXPANDED) return;
529 /* We mark the node as expanded and request that it fill itself. When it has
530 * completed it will called updated_node() and we can redraw at that
531 * point. */
532 cn->flags |= CN_EXPANDED;
6a06a735
RK
533 if(contingent)
534 cn->flags |= CN_CONTINGENT;
535 else
536 cn->flags &= ~CN_CONTINGENT;
537 /* If this node is not contingently expanded, mark all its parents back to
538 * the root as not contingent either, so they won't be contracted when the
539 * search results change */
540 if(!contingent) {
541 struct choosenode *cnp;
542
543 for(cnp = cn->parent; cnp; cnp = cnp->parent)
544 cnp->flags &= ~CN_CONTINGENT;
545 }
460b9539 546 /* TODO: visual feedback */
547 cn->fill(cn);
548}
549
6a06a735
RK
550/** @brief Make sure all the search results below @p cn are expanded
551 * @param cn Node to start at
552 */
553static void expand_from(struct choosenode *cn) {
554 int n;
6a06a735
RK
555
556 if(nsearchvisible == nsearchresults)
557 /* We're done */
558 return;
559 /* Are any of the search tracks at/below this point? */
c3df9503
RK
560 if(!(cn == root || hash_find(searchhash, cn->path)))
561 return;
6a06a735
RK
562 D(("expand_from %d/%d visible %s",
563 nsearchvisible, nsearchresults, cn->path));
564 if(cn->flags & CN_EXPANDABLE) {
565 if(cn->flags & CN_EXPANDED)
566 /* This node is marked as expanded already. children.nvec might be 0,
567 * indicating that expansion is still underway. We should get another
568 * callback when it is expanded. */
c3df9503 569 for(n = 0; n < cn->children.nvec && gets_in_flight < 10; ++n)
6a06a735
RK
570 expand_from(cn->children.vec[n]);
571 else {
572 /* This node is not expanded yet */
573 expand_node(cn, 1);
574 }
c3df9503 575 } else {
6a06a735
RK
576 /* This is an actual search result */
577 ++nsearchvisible;
c3df9503 578 progress_window_progress(spw, nsearchvisible, nsearchresults);
fcc8b9f7 579 if(nsearchvisible == nsearchresults) {
73dd383f
RK
580 if(suppress_redisplay) {
581 suppress_redisplay = 0;
582 redisplay_tree("all search results visible");
583 }
fcc8b9f7
RK
584 /* We've got the lot. We make sure the first result is visible. */
585 cn = first_search_result(root);
586 gtk_adjustment_clamp_page(vadjust, cn->ymin, cn->ymax);
587 }
c3df9503 588 }
6a06a735
RK
589}
590
591/** @brief Contract all contingently expanded nodes below @p cn */
592static void contract_contingent(struct choosenode *cn) {
593 int n;
594
595 if(cn->flags & CN_CONTINGENT)
596 contract_node(cn);
597 else
598 for(n = 0; n < cn->children.nvec; ++n)
599 contract_contingent(cn->children.vec[n]);
600}
601
b9bdafc7 602/** @brief Contract a node */
460b9539 603static void contract_node(struct choosenode *cn) {
604 D(("contract_node %s", cn->path));
605 assert(cn->flags & CN_EXPANDABLE);
606 /* If node is already contracted do nothing. */
607 if(!(cn->flags & CN_EXPANDED)) return;
6a06a735 608 cn->flags &= ~(CN_EXPANDED|CN_CONTINGENT);
460b9539 609 /* Clear selection below this node */
610 clear_selection(cn);
e9b70a84 611 /* Zot children. We never used to do this but the result would be that over
612 * time you'd end up with the entire tree pulled into memory. If the server
613 * is over a slow network it will make interactivity slightly worse; if
614 * anyone complains we can make it an option. */
615 clear_children(cn);
460b9539 616 /* We can contract a node immediately. */
73dd383f 617 redisplay_tree("contract_node");
460b9539 618}
619
b9bdafc7 620/** @brief qsort() callback for ordering choosenodes */
460b9539 621static int compare_choosenode(const void *av, const void *bv) {
622 const struct choosenode *const *aa = av, *const *bb = bv;
623 const struct choosenode *a = *aa, *b = *bb;
624
625 return compare_tracks(a->sort, b->sort,
626 a->display, b->display,
627 a->path, b->path);
628}
629
b9bdafc7 630/** @brief Called when an expandable node is updated. */
73dd383f
RK
631static void updated_node(struct choosenode *cn, int redisplay,
632 const char *why) {
460b9539 633 D(("updated_node %s", cn->path));
634 assert(cn->flags & CN_EXPANDABLE);
635 /* It might be that the node has been de-expanded since we requested the
636 * update. In that case we ignore this notification. */
637 if(!(cn->flags & CN_EXPANDED)) return;
638 /* Sort children */
639 qsort(cn->children.vec, cn->children.nvec, sizeof (struct choosenode *),
640 compare_choosenode);
73dd383f
RK
641 if(redisplay) {
642 char whywhy[1024];
643
644 snprintf(whywhy, sizeof whywhy, "updated_node %s", why);
645 redisplay_tree(whywhy);
646 }
460b9539 647}
648
649/* Searching --------------------------------------------------------------- */
650
fcc8b9f7
RK
651/** @brief Return true if @p track is a search result
652 *
653 * In particular the return value is one more than the index of the track
654 * @p searchresults.
655 */
6a06a735 656static int is_search_result(const char *track) {
fcc8b9f7
RK
657 void *r;
658
659 if(searchhash && (r = hash_find(searchhash, track)))
660 return 1 + *(int *)r;
661 else
662 return 0;
663}
664
665/** @brief Return the first search result at or below @p cn */
666static struct choosenode *first_search_result(struct choosenode *cn) {
667 int n;
668 struct choosenode *r;
669
670 if(cn->flags & CN_EXPANDABLE) {
671 for(n = 0; n < cn->children.nvec; ++n)
672 if((r = first_search_result(cn->children.vec[n])))
673 return r;
674 } else if(is_search_result(cn->path))
675 return cn;
676 return 0;
460b9539 677}
678
b9bdafc7
RK
679/** @brief Called with a list of search results
680 *
681 * This is called from eclient with a (possibly empty) list of search results,
460b9539 682 * and also from initiate_seatch with an always empty list to indicate that
683 * we're not searching for anything in particular. */
684static void search_completed(void attribute((unused)) *v,
4190a4d9 685 const char *error,
460b9539 686 int nvec, char **vec) {
460b9539 687 int n;
c3df9503 688 char *s;
460b9539 689
4190a4d9
RK
690 if(error) {
691 popup_protocol_error(0, error);
692 return;
693 }
694
460b9539 695 search_in_flight = 0;
6a06a735
RK
696 /* Contract any choosenodes that were only expanded to show search
697 * results */
73dd383f 698 suppress_redisplay = 1;
6a06a735 699 contract_contingent(root);
73dd383f 700 suppress_redisplay = 0;
460b9539 701 if(search_obsolete) {
702 /* This search has been obsoleted by user input since it started.
703 * Therefore we throw away the result and search again. */
704 search_obsolete = 0;
705 initiate_search();
706 } else {
6a06a735
RK
707 /* Stash the search results */
708 searchresults = vec;
709 nsearchresults = nvec;
460b9539 710 if(nvec) {
6a06a735 711 /* Create a new search hash for fast identification of results */
fcc8b9f7 712 searchhash = hash_new(sizeof(int));
c3df9503 713 for(n = 0; n < nvec; ++n) {
fcc8b9f7
RK
714 int *const ip = xmalloc(sizeof (int *));
715 static const int minus_1 = -1;
716 *ip = n;
c3df9503 717 /* The filename itself lives in the hash */
fcc8b9f7 718 hash_add(searchhash, vec[n], ip, HASH_INSERT_OR_REPLACE);
c3df9503
RK
719 /* So do its ancestor directories */
720 for(s = vec[n] + 1; *s; ++s) {
721 if(*s == '/') {
722 *s = 0;
fcc8b9f7 723 hash_add(searchhash, vec[n], &minus_1, HASH_INSERT_OR_REPLACE);
c3df9503
RK
724 *s = '/';
725 }
726 }
727 }
6a06a735
RK
728 /* We don't yet know that the results are visible */
729 nsearchvisible = 0;
c3df9503
RK
730 if(spw) {
731 progress_window_progress(spw, 0, 0);
732 spw = 0;
733 }
734 if(nsearchresults > 50)
735 spw = progress_window_new("Fetching search results");
6a06a735
RK
736 /* Initiate expansion */
737 expand_from(root);
fcc8b9f7
RK
738 /* The search results buttons are usable */
739 gtk_widget_set_sensitive(nextsearch, 1);
740 gtk_widget_set_sensitive(prevsearch, 1);
73dd383f 741 suppress_redisplay = 1; /* avoid lots of redisplays */
6a06a735
RK
742 } else {
743 searchhash = 0; /* for the gc */
73dd383f 744 redisplay_tree("no search results"); /* remove search markers */
fcc8b9f7
RK
745 /* The search results buttons are not usable */
746 gtk_widget_set_sensitive(nextsearch, 0);
747 gtk_widget_set_sensitive(prevsearch, 0);
460b9539 748 }
749 }
750}
751
b9bdafc7
RK
752/** @brief Initiate a search
753 *
754 * If a search is underway we set @ref search_obsolete and restart the search
755 * in search_completed() above.
756 */
460b9539 757static void initiate_search(void) {
758 char *terms, *e;
759
760 /* Find out what the user is after */
761 terms = xstrdup(gtk_entry_get_text(GTK_ENTRY(searchentry)));
762 /* Strip leading and trailing space */
763 while(*terms == ' ') ++terms;
764 e = terms + strlen(terms);
765 while(e > terms && e[-1] == ' ') --e;
766 *e = 0;
767 /* If a search is already underway then mark it as obsolete. We'll revisit
768 * when it returns. */
769 if(search_in_flight) {
770 search_obsolete = 1;
771 return;
772 }
773 if(*terms) {
774 /* There's still something left. Initiate the search. */
775 if(disorder_eclient_search(client, search_completed, terms, 0)) {
776 /* The search terms are bad! We treat this as if there were no search
777 * terms at all. Some kind of feedback would be handy. */
778 fprintf(stderr, "bad terms [%s]\n", terms); /* TODO */
4190a4d9 779 search_completed(0, 0, 0, 0);
460b9539 780 } else {
781 search_in_flight = 1;
782 }
783 } else {
784 /* No search terms - we want to see all tracks */
4190a4d9 785 search_completed(0, 0, 0, 0);
460b9539 786 }
787}
788
b9bdafc7 789/** @brief Called when the cancel search button is clicked */
460b9539 790static void clearsearch_clicked(GtkButton attribute((unused)) *button,
791 gpointer attribute((unused)) userdata) {
792 gtk_entry_set_text(GTK_ENTRY(searchentry), "");
793}
794
fcc8b9f7
RK
795/** @brief Called when the 'next search result' button is clicked */
796static void next_clicked(GtkButton attribute((unused)) *button,
797 gpointer attribute((unused)) userdata) {
798 /* We want to find the highest (lowest ymax) track that is below the current
799 * visible range */
800 int n;
801 const gdouble bottom = gtk_adjustment_get_value(vadjust) + vadjust->page_size;
802 const struct choosenode *candidate = 0;
803
804 for(n = 0; n < nsearchresults; ++n) {
805 const struct choosenode *const cn = searchnodes[n];
806
807 if(cn
808 && cn->ymax > bottom
809 && (candidate == 0
810 || cn->ymax < candidate->ymax))
811 candidate = cn;
812 }
813 if(candidate)
814 gtk_adjustment_clamp_page(vadjust, candidate->ymin, candidate->ymax);
815}
816
817/** @brief Called when the 'previous search result' button is clicked */
818static void prev_clicked(GtkButton attribute((unused)) *button,
819 gpointer attribute((unused)) userdata) {
820 /* We want to find the lowest (greated ymax) track that is above the current
821 * visible range */
822 int n;
823 const gdouble top = gtk_adjustment_get_value(vadjust);
824 const struct choosenode *candidate = 0;
825
826 for(n = 0; n < nsearchresults; ++n) {
827 const struct choosenode *const cn = searchnodes[n];
828
829 if(cn
830 && cn->ymin < top
831 && (candidate == 0
832 || cn->ymax > candidate->ymax))
833 candidate = cn;
834 }
835 if(candidate)
836 gtk_adjustment_clamp_page(vadjust, candidate->ymin, candidate->ymax);
837}
838
460b9539 839/* Display functions ------------------------------------------------------- */
840
b9bdafc7 841/** @brief Delete all the widgets in the tree */
460b9539 842static void delete_widgets(struct choosenode *cn) {
843 int n;
844
e9b70a84 845 delete_cn_widgets(cn);
460b9539 846 for(n = 0; n < cn->children.nvec; ++n)
847 delete_widgets(cn->children.vec[n]);
848 cn->flags &= ~(CN_DISPLAYED|CN_SELECTED);
849 files_selected = 0;
850}
851
b9bdafc7 852/** @brief Update the display */
73dd383f 853static void redisplay_tree(const char *why) {
460b9539 854 struct displaydata d;
855 guint oldwidth, oldheight;
856
73dd383f
RK
857 D(("redisplay_tree %s", why));
858 if(suppress_redisplay) {
859 /*fprintf(stderr, "redisplay_tree %s suppressed\n", why);*/
860 return;
861 }
165086a0
RK
862 if(gets_in_flight) {
863 /*fprintf(stderr, "redisplay_tree %s suppressed (gets_in_flight)\n", why);*/
864 return;
865 }
81cd1894
RK
866 INIT();
867 BEGIN(total);
73dd383f 868 /*fprintf(stderr, "redisplay_tree %s *** NOT SUPPRESSED ***\n", why);*/
460b9539 869 /* We'll count these up empirically each time */
870 files_selected = 0;
871 files_visible = 0;
872 /* Correct the layout and find out how much space it uses */
e1d4a32b 873 MTAG_PUSH("display_tree");
fcc8b9f7
RK
874 searchnodes = nsearchresults ? xcalloc(nsearchresults,
875 sizeof (struct choosenode *)) : 0;
460b9539 876 d = display_tree(root, 0, 0);
e1d4a32b 877 MTAG_POP();
81cd1894
RK
878
879 BEGIN(gtkbits);
460b9539 880 /* We must set the total size or scrolling will not work (it wouldn't be hard
881 * for GtkLayout to figure it out for itself but presumably you're supposed
882 * to be able to have widgets off the edge of the layuot.)
883 *
884 * There is a problem: if we shrink the size then the part of the screen that
885 * is outside the new size but inside the old one is not updated. I think
886 * this is arguably bug in GTK+ but it's easy to force a redraw if this
887 * region is nonempty.
888 */
889 gtk_layout_get_size(GTK_LAYOUT(chooselayout), &oldwidth, &oldheight);
890 if(oldwidth > d.width || oldheight > d.height)
891 gtk_widget_queue_draw(chooselayout);
892 gtk_layout_set_size(GTK_LAYOUT(chooselayout), d.width, d.height);
81cd1894 893 END(gtkbits);
460b9539 894 /* Notify the main menu of any recent changes */
81cd1894 895 BEGIN(menuupdate);
460b9539 896 menu_update(-1);
81cd1894
RK
897 END(menuupdate);
898 END(total);
899 REPORT();
460b9539 900}
901
b9bdafc7 902/** @brief Recursive step for redisplay_tree()
fcc8b9f7
RK
903 * @param cn Node to display
904 * @param x X coordinate for @p cn
905 * @param y Y coordinate for @p cn
b9bdafc7
RK
906 *
907 * Makes sure all displayed widgets from CN down exist and are in their proper
908 * place and return the maximum space used.
909 */
460b9539 910static struct displaydata display_tree(struct choosenode *cn, int x, int y) {
911 int n, aw;
912 GtkRequisition req;
913 struct displaydata d, cd;
914 GdkPixbuf *pb;
fcc8b9f7 915 const int search_result = is_search_result(cn->path);
460b9539 916
917 D(("display_tree %s %d,%d", cn->path, x, y));
918
919 /* An expandable item contains an arrow and a text label. When you press the
920 * button it flips its expand state.
921 *
922 * A non-expandable item has just a text label and no arrow.
923 */
924 if(!cn->container) {
81cd1894 925 BEGIN(new_widgets);
521b8841 926 MTAG_PUSH("make_widgets_1");
460b9539 927 /* Widgets need to be created */
e9b70a84 928 NW(hbox);
460b9539 929 cn->hbox = gtk_hbox_new(FALSE, 1);
930 if(cn->flags & CN_EXPANDABLE) {
e9b70a84 931 NW(arrow);
460b9539 932 cn->arrow = gtk_arrow_new(cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN
933 : GTK_ARROW_RIGHT,
934 GTK_SHADOW_NONE);
935 cn->marker = 0;
936 } else {
937 cn->arrow = 0;
e9b70a84 938 if((pb = find_image("notes.png"))) {
939 NW(image);
460b9539 940 cn->marker = gtk_image_new_from_pixbuf(pb);
e9b70a84 941 }
460b9539 942 }
521b8841 943 MTAG_POP();
944 MTAG_PUSH("make_widgets_2");
e9b70a84 945 NW(label);
460b9539 946 cn->label = gtk_label_new(cn->display);
947 if(cn->arrow)
948 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->arrow);
949 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->label);
950 if(cn->marker)
951 gtk_container_add(GTK_CONTAINER(cn->hbox), cn->marker);
521b8841 952 MTAG_POP();
953 MTAG_PUSH("make_widgets_3");
e9b70a84 954 NW(event_box);
460b9539 955 cn->container = gtk_event_box_new();
956 gtk_container_add(GTK_CONTAINER(cn->container), cn->hbox);
957 g_signal_connect(cn->container, "button-release-event",
958 G_CALLBACK(clicked_choosenode), cn);
959 g_signal_connect(cn->container, "button-press-event",
960 G_CALLBACK(clicked_choosenode), cn);
961 g_object_ref(cn->container);
460b9539 962 /* Show everything by default */
963 gtk_widget_show_all(cn->container);
e1d4a32b 964 MTAG_POP();
81cd1894 965 END(new_widgets);
460b9539 966 }
967 assert(cn->container);
d4ef4132 968 /* Set colors */
81cd1894 969 BEGIN(colors);
33288048
RK
970 if(search_result) {
971 gtk_widget_set_style(cn->container, search_style);
972 gtk_widget_set_style(cn->label, search_style);
973 } else {
974 gtk_widget_set_style(cn->container, layout_style);
975 gtk_widget_set_style(cn->label, layout_style);
976 }
81cd1894 977 END(colors);
460b9539 978 /* Make sure the icon is right */
81cd1894 979 BEGIN(markers);
460b9539 980 if(cn->flags & CN_EXPANDABLE)
981 gtk_arrow_set(GTK_ARROW(cn->arrow),
982 cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN : GTK_ARROW_RIGHT,
983 GTK_SHADOW_NONE);
984 else if(cn->marker)
985 /* Make sure the queued marker is right */
986 /* TODO: doesn't always work */
987 (queued(cn->path) ? gtk_widget_show : gtk_widget_hide)(cn->marker);
81cd1894 988 END(markers);
460b9539 989 /* Put the widget in the right place */
81cd1894 990 BEGIN(location);
460b9539 991 if(cn->flags & CN_DISPLAYED)
992 gtk_layout_move(GTK_LAYOUT(chooselayout), cn->container, x, y);
993 else {
994 gtk_layout_put(GTK_LAYOUT(chooselayout), cn->container, x, y);
995 cn->flags |= CN_DISPLAYED;
521b8841 996 /* Now chooselayout has a ref to the container */
997 g_object_unref(cn->container);
460b9539 998 }
81cd1894 999 END(location);
460b9539 1000 /* Set the widget's selection status */
81cd1894 1001 BEGIN(selection);
460b9539 1002 if(!(cn->flags & CN_EXPANDABLE))
1003 display_selection(cn);
81cd1894 1004 END(selection);
460b9539 1005 /* Find the size used so we can get vertical positioning right. */
1006 gtk_widget_size_request(cn->container, &req);
1007 d.width = x + req.width;
1008 d.height = y + req.height;
7e60b670
RK
1009 cn->ymin = y;
1010 cn->ymax = d.height;
460b9539 1011 if(cn->flags & CN_EXPANDED) {
1012 /* We'll offset children by the size of the arrow whatever it might be. */
1013 assert(cn->arrow);
1014 gtk_widget_size_request(cn->arrow, &req);
1015 aw = req.width;
1016 for(n = 0; n < cn->children.nvec; ++n) {
1017 cd = display_tree(cn->children.vec[n], x + aw, d.height);
1018 if(cd.width > d.width)
1019 d.width = cd.width;
1020 d.height = cd.height;
1021 }
1022 } else {
81cd1894 1023 BEGIN(undisplay);
460b9539 1024 for(n = 0; n < cn->children.nvec; ++n)
1025 undisplay_tree(cn->children.vec[n]);
81cd1894 1026 END(undisplay);
460b9539 1027 }
1028 if(!(cn->flags & CN_EXPANDABLE)) {
1029 ++files_visible;
1030 if(cn->flags & CN_SELECTED)
1031 ++files_selected;
1032 }
fcc8b9f7
RK
1033 /* update the search results array */
1034 if(search_result)
1035 searchnodes[search_result - 1] = cn;
460b9539 1036 /* report back how much space we used */
1037 D(("display_tree %s %d,%d total size %dx%d", cn->path, x, y,
1038 d.width, d.height));
1039 return d;
1040}
1041
b9bdafc7 1042/** @brief Remove widgets for newly hidden nodes */
460b9539 1043static void undisplay_tree(struct choosenode *cn) {
1044 int n;
1045
1046 D(("undisplay_tree %s", cn->path));
1047 /* Remove this widget from the display */
1048 if(cn->flags & CN_DISPLAYED) {
1049 gtk_container_remove(GTK_CONTAINER(chooselayout), cn->container);
1050 cn->flags ^= CN_DISPLAYED;
1051 }
1052 /* Remove children too */
1053 for(n = 0; n < cn->children.nvec; ++n)
1054 undisplay_tree(cn->children.vec[n]);
1055}
1056
1057/* Selection --------------------------------------------------------------- */
1058
b9bdafc7 1059/** @brief Mark the widget @p cn according to its selection state */
460b9539 1060static void display_selection(struct choosenode *cn) {
1061 /* Need foreground and background colors */
1062 gtk_widget_set_state(cn->label, (cn->flags & CN_SELECTED
1063 ? GTK_STATE_SELECTED : GTK_STATE_NORMAL));
1064 gtk_widget_set_state(cn->container, (cn->flags & CN_SELECTED
1065 ? GTK_STATE_SELECTED : GTK_STATE_NORMAL));
1066}
1067
b9bdafc7
RK
1068/** @brief Set the selection state of a widget
1069 *
1070 * Directories can never be selected, we just ignore attempts to do so. */
460b9539 1071static void set_selection(struct choosenode *cn, int selected) {
1072 unsigned f = selected ? CN_SELECTED : 0;
1073
1074 D(("set_selection %d %s", selected, cn->path));
1075 if(!(cn->flags & CN_EXPANDABLE) && (cn->flags & CN_SELECTED) != f) {
1076 cn->flags ^= CN_SELECTED;
1077 /* Maintain selection count */
1078 if(selected)
1079 ++files_selected;
1080 else
1081 --files_selected;
1082 display_selection(cn);
1083 /* Update main menu sensitivity */
1084 menu_update(-1);
1085 }
1086}
1087
b9bdafc7 1088/** @brief Recursively clear all selection bits from CN down */
460b9539 1089static void clear_selection(struct choosenode *cn) {
1090 int n;
1091
1092 set_selection(cn, 0);
1093 for(n = 0; n < cn->children.nvec; ++n)
1094 clear_selection(cn->children.vec[n]);
1095}
1096
1097/* User actions ------------------------------------------------------------ */
1098
53fa91bb
RK
1099/** @brief Called when disorder_eclient_play completes */
1100void play_completed(void attribute((unused)) *v,
1101 const char *error) {
1102 if(error)
1103 popup_protocol_error(0, error);
1104}
1105
b9bdafc7
RK
1106/** @brief Clicked on something
1107 *
1108 * This implements playing, all the modifiers for selection, etc.
1109 */
460b9539 1110static void clicked_choosenode(GtkWidget attribute((unused)) *widget,
1111 GdkEventButton *event,
1112 gpointer user_data) {
1113 struct choosenode *cn = user_data;
1114 int ind, last_ind, n;
1115
1116 D(("clicked_choosenode %s", cn->path));
1117 if(event->type == GDK_BUTTON_RELEASE
1118 && event->button == 1) {
1119 /* Left click */
1120 if(cn->flags & CN_EXPANDABLE) {
1121 /* This is a directory. Flip its expansion status. */
1122 if(cn->flags & CN_EXPANDED)
1123 contract_node(cn);
1124 else
6a06a735 1125 expand_node(cn, 0/*!contingent*/);
460b9539 1126 last_click = 0;
1127 } else {
1128 /* This is a file. Adjust selection status */
1129 /* TODO the basic logic here is essentially the same as that in queue.c.
1130 * Can we share code at all? */
1131 switch(event->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) {
1132 case 0:
1133 clear_selection(root);
1134 set_selection(cn, 1);
1135 last_click = cn;
1136 break;
1137 case GDK_CONTROL_MASK:
1138 set_selection(cn, !(cn->flags & CN_SELECTED));
1139 last_click = cn;
1140 break;
1141 case GDK_SHIFT_MASK:
1142 case GDK_SHIFT_MASK|GDK_CONTROL_MASK:
1143 if(last_click && last_click->parent == cn->parent) {
1144 /* Figure out where the current and last clicks are in the list */
1145 ind = last_ind = -1;
1146 for(n = 0; n < cn->parent->children.nvec; ++n) {
1147 if(cn->parent->children.vec[n] == cn)
1148 ind = n;
1149 if(cn->parent->children.vec[n] == last_click)
1150 last_ind = n;
1151 }
1152 /* Test shouldn't ever fail, but still */
1153 if(ind >= 0 && last_ind >= 0) {
1154 if(!(event->state & GDK_CONTROL_MASK)) {
1155 for(n = 0; n < cn->parent->children.nvec; ++n)
1156 set_selection(cn->parent->children.vec[n], 0);
1157 }
1158 if(ind > last_ind)
1159 for(n = last_ind; n <= ind; ++n)
1160 set_selection(cn->parent->children.vec[n], 1);
1161 else
1162 for(n = ind; n <= last_ind; ++n)
1163 set_selection(cn->parent->children.vec[n], 1);
1164 if(event->state & GDK_CONTROL_MASK)
1165 last_click = cn;
1166 }
1167 }
b9bdafc7
RK
1168 /* TODO trying to select a range that doesn't share a single parent
1169 * currently does not work, but it ought to. */
460b9539 1170 break;
1171 }
1172 }
1173 } else if(event->type == GDK_BUTTON_RELEASE
1174 && event->button == 2) {
1175 /* Middle click - play the pointed track */
1176 if(!(cn->flags & CN_EXPANDABLE)) {
1177 clear_selection(root);
1178 set_selection(cn, 1);
1179 gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
53fa91bb 1180 disorder_eclient_play(client, cn->path, play_completed, 0);
460b9539 1181 last_click = 0;
1182 }
1183 } else if(event->type == GDK_BUTTON_PRESS
1184 && event->button == 3) {
16e145a5
RK
1185 struct choose_menuitem *const menuitems =
1186 (cn->flags & CN_EXPANDABLE ? dir_menuitems : track_menuitems);
1187 GtkWidget *const menu =
1188 (cn->flags & CN_EXPANDABLE ? dir_menu : track_menu);
460b9539 1189 /* Right click. Pop up a menu. */
1190 /* If the current file isn't selected, switch the selection to just that.
1191 * (If we're looking at a directory then leave the selection alone.) */
1192 if(!(cn->flags & CN_EXPANDABLE) && !(cn->flags & CN_SELECTED)) {
1193 clear_selection(root);
1194 set_selection(cn, 1);
1195 last_click = cn;
1196 }
1197 /* Set the item sensitivity and callbacks */
16e145a5 1198 for(n = 0; menuitems[n].name; ++n) {
460b9539 1199 if(menuitems[n].handlerid)
1200 g_signal_handler_disconnect(menuitems[n].w,
1201 menuitems[n].handlerid);
1202 gtk_widget_set_sensitive(menuitems[n].w,
1203 menuitems[n].sensitive(cn));
1204 menuitems[n].handlerid = g_signal_connect
1205 (menuitems[n].w, "activate", G_CALLBACK(menuitems[n].activate), cn);
1206 }
d4ef4132 1207 set_tool_colors(menu);
460b9539 1208 /* Pop up the menu */
1209 gtk_widget_show_all(menu);
1210 gtk_menu_popup(GTK_MENU(menu), 0, 0, 0, 0,
1211 event->button, event->time);
1212 }
1213}
1214
b9bdafc7 1215/** @brief Called BY GTK+ to tell us the search entry box has changed */
460b9539 1216static void searchentry_changed(GtkEditable attribute((unused)) *editable,
1217 gpointer attribute((unused)) user_data) {
1218 initiate_search();
1219}
1220
16e145a5 1221/* Track menu items -------------------------------------------------------- */
460b9539 1222
b9bdafc7 1223/** @brief Recursive step for gather_selected() */
460b9539 1224static void recurse_selected(struct choosenode *cn, struct vector *v) {
1225 int n;
1226
1227 if(cn->flags & CN_EXPANDABLE) {
1228 if(cn->flags & CN_EXPANDED)
1229 for(n = 0; n < cn->children.nvec; ++n)
1230 recurse_selected(cn->children.vec[n], v);
1231 } else {
1232 if((cn->flags & CN_SELECTED) && cn->path)
1233 vector_append(v, (char *)cn->path);
1234 }
1235}
1236
b9bdafc7 1237/*** @brief Get a list of all the selected tracks */
16e145a5 1238static const char **gather_selected(int *ntracks) {
460b9539 1239 struct vector v;
1240
1241 vector_init(&v);
1242 recurse_selected(root, &v);
1243 vector_terminate(&v);
1244 if(ntracks) *ntracks = v.nvec;
16e145a5 1245 return (const char **)v.vec;
460b9539 1246}
1247
16e145a5
RK
1248/** @brief Called when the track menu's play option is activated */
1249static void activate_track_play(GtkMenuItem attribute((unused)) *menuitem,
1250 gpointer attribute((unused)) user_data) {
1251 const char **tracks = gather_selected(0);
460b9539 1252 int n;
1253
1254 gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue");
1255 for(n = 0; tracks[n]; ++n)
53fa91bb 1256 disorder_eclient_play(client, tracks[n], play_completed, 0);
460b9539 1257}
1258
b9bdafc7 1259/** @brief Called when the menu's properties option is activated */
16e145a5
RK
1260static void activate_track_properties(GtkMenuItem attribute((unused)) *menuitem,
1261 gpointer attribute((unused)) user_data) {
460b9539 1262 int ntracks;
16e145a5 1263 const char **tracks = gather_selected(&ntracks);
460b9539 1264
1265 properties(ntracks, tracks);
1266}
1267
b9bdafc7 1268/** @brief Determine whether the menu's play option should be sensitive */
16e145a5 1269static gboolean sensitive_track_play(struct choosenode attribute((unused)) *cn) {
8f763f1b
RK
1270 return (!!files_selected
1271 && (disorder_eclient_state(client) & DISORDER_CONNECTED));
460b9539 1272}
1273
b9bdafc7 1274/** @brief Determine whether the menu's properties option should be sensitive */
16e145a5 1275static gboolean sensitive_track_properties(struct choosenode attribute((unused)) *cn) {
8f763f1b 1276 return !!files_selected && (disorder_eclient_state(client) & DISORDER_CONNECTED);
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
1381
1382
460b9539 1383/* Main menu plumbing ------------------------------------------------------ */
1384
b9bdafc7 1385/** @brief Determine whether the edit menu's properties option should be sensitive */
460b9539 1386static int choose_properties_sensitive(GtkWidget attribute((unused)) *w) {
8f763f1b 1387 return !!files_selected && (disorder_eclient_state(client) & DISORDER_CONNECTED);
460b9539 1388}
1389
b9bdafc7
RK
1390/** @brief Determine whether the edit menu's select all option should be sensitive
1391 *
1392 * TODO not implemented, see also choose_selectall_activate()
1393 */
460b9539 1394static int choose_selectall_sensitive(GtkWidget attribute((unused)) *w) {
b9bdafc7 1395 return FALSE;
460b9539 1396}
1397
fb009628
RK
1398/** @brief Determine whether the edit menu's select none option should be sensitive
1399 *
1400 * TODO not implemented, see also choose_selectnone_activate()
1401 */
1402static int choose_selectnone_sensitive(GtkWidget attribute((unused)) *w) {
1403 return FALSE;
1404}
1405
b9bdafc7 1406/** @brief Called when the edit menu's properties option is activated */
460b9539 1407static void choose_properties_activate(GtkWidget attribute((unused)) *w) {
16e145a5 1408 activate_track_properties(0, 0);
460b9539 1409}
1410
b9bdafc7
RK
1411/** @brief Called when the edit menu's select all option is activated
1412 *
1413 * TODO not implemented, see choose_selectall_sensitive() */
460b9539 1414static void choose_selectall_activate(GtkWidget attribute((unused)) *w) {
460b9539 1415}
1416
fb009628
RK
1417/** @brief Called when the edit menu's select none option is activated
1418 *
1419 * TODO not implemented, see choose_selectnone_sensitive() */
1420static void choose_selectnone_activate(GtkWidget attribute((unused)) *w) {
1421}
1422
b9bdafc7 1423/** @brief Main menu callbacks for Choose screen */
460b9539 1424static const struct tabtype tabtype_choose = {
1425 choose_properties_sensitive,
1426 choose_selectall_sensitive,
fb009628 1427 choose_selectnone_sensitive,
460b9539 1428 choose_properties_activate,
1429 choose_selectall_activate,
fb009628 1430 choose_selectnone_activate,
460b9539 1431};
1432
1433/* Public entry points ----------------------------------------------------- */
1434
73f1b9f3
RK
1435/** @brief Called to entirely reset the choose screen */
1436static void choose_reset(void) {
1437 if(root)
1438 undisplay_tree(root);
1439 root = newnode(0/*parent*/, "<root>", "All files", "",
1440 CN_EXPANDABLE, fill_root_node);
1441 expand_node(root, 0); /* will call redisplay_tree */
1442}
1443
b9bdafc7 1444/** @brief Create a track choice widget */
460b9539 1445GtkWidget *choose_widget(void) {
1446 int n;
1447 GtkWidget *scrolled;
1448 GtkWidget *vbox, *hbox, *clearsearch;
1449
1450 /*
1451 * +--vbox-------------------------------------------------------+
1452 * | +-hbox----------------------------------------------------+ |
1453 * | | searchentry | clearsearch | |
1454 * | +---------------------------------------------------------+ |
1455 * | +-scrolled------------------------------------------------+ |
1456 * | | +-chooselayout------------------------------------++--+ | |
1457 * | | | Tree structure is manually layed out in here ||^^| | |
1458 * | | | || | | |
1459 * | | | || | | |
1460 * | | | || | | |
1461 * | | | ||vv| | |
1462 * | | +-------------------------------------------------++--+ | |
1463 * | | +-------------------------------------------------+ | |
1464 * | | |< >| | |
1465 * | | +-------------------------------------------------+ | |
1466 * | +---------------------------------------------------------+ |
1467 * +-------------------------------------------------------------+
1468 */
1469
1470 /* Text entry box for search terms */
e9b70a84 1471 NW(entry);
460b9539 1472 searchentry = gtk_entry_new();
33288048 1473 gtk_widget_set_style(searchentry, tool_style);
460b9539 1474 g_signal_connect(searchentry, "changed", G_CALLBACK(searchentry_changed), 0);
a8c9507f 1475 gtk_tooltips_set_tip(tips, searchentry, "Enter search terms here; search is automatic", "");
460b9539 1476
1477 /* Cancel button to clear the search */
e9b70a84 1478 NW(button);
460b9539 1479 clearsearch = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
33288048 1480 gtk_widget_set_style(clearsearch, tool_style);
460b9539 1481 g_signal_connect(G_OBJECT(clearsearch), "clicked",
1482 G_CALLBACK(clearsearch_clicked), 0);
a8c9507f
RK
1483 gtk_tooltips_set_tip(tips, clearsearch, "Clear search terms", "");
1484
fcc8b9f7
RK
1485 /* Up and down buttons to find previous/next results; initially they are not
1486 * usable as there are no search results. */
1487 prevsearch = iconbutton("up.png", "Previous search result");
1488 g_signal_connect(G_OBJECT(prevsearch), "clicked",
1489 G_CALLBACK(prev_clicked), 0);
33288048 1490 gtk_widget_set_style(prevsearch, tool_style);
fcc8b9f7
RK
1491 gtk_widget_set_sensitive(prevsearch, 0);
1492 nextsearch = iconbutton("down.png", "Next search result");
1493 g_signal_connect(G_OBJECT(nextsearch), "clicked",
1494 G_CALLBACK(next_clicked), 0);
33288048 1495 gtk_widget_set_style(nextsearch, tool_style);
fcc8b9f7
RK
1496 gtk_widget_set_sensitive(nextsearch, 0);
1497
1498 /* hbox packs the search tools button together on a line */
e9b70a84 1499 NW(hbox);
460b9539 1500 hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*spacing*/);
1501 gtk_box_pack_start(GTK_BOX(hbox), searchentry,
1502 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
fcc8b9f7
RK
1503 gtk_box_pack_start(GTK_BOX(hbox), prevsearch,
1504 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
1505 gtk_box_pack_start(GTK_BOX(hbox), nextsearch,
1506 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
1507 gtk_box_pack_start(GTK_BOX(hbox), clearsearch,
1508 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
460b9539 1509
1510 /* chooselayout contains the currently visible subset of the track
1511 * namespace */
e9b70a84 1512 NW(layout);
460b9539 1513 chooselayout = gtk_layout_new(0, 0);
33288048 1514 gtk_widget_set_style(chooselayout, layout_style);
73f1b9f3
RK
1515 choose_reset();
1516 register_reset(choose_reset);
16e145a5
RK
1517 /* Create the popup menus */
1518 NW(menu);
1519 track_menu = gtk_menu_new();
1520 g_signal_connect(track_menu, "destroy", G_CALLBACK(gtk_widget_destroyed),
1521 &track_menu);
1522 for(n = 0; track_menuitems[n].name; ++n) {
1523 NW(menu_item);
1524 track_menuitems[n].w =
1525 gtk_menu_item_new_with_label(track_menuitems[n].name);
1526 gtk_menu_attach(GTK_MENU(track_menu), track_menuitems[n].w,
1527 0, 1, n, n + 1);
1528 }
e9b70a84 1529 NW(menu);
16e145a5
RK
1530 dir_menu = gtk_menu_new();
1531 g_signal_connect(dir_menu, "destroy", G_CALLBACK(gtk_widget_destroyed),
1532 &dir_menu);
1533 for(n = 0; dir_menuitems[n].name; ++n) {
e9b70a84 1534 NW(menu_item);
16e145a5
RK
1535 dir_menuitems[n].w =
1536 gtk_menu_item_new_with_label(dir_menuitems[n].name);
1537 gtk_menu_attach(GTK_MENU(dir_menu), dir_menuitems[n].w,
1538 0, 1, n, n + 1);
460b9539 1539 }
1540 /* The layout is scrollable */
d4ef4132 1541 scrolled = scroll_widget(chooselayout);
7e60b670 1542 vadjust = gtk_layout_get_vadjustment(GTK_LAYOUT(chooselayout));
460b9539 1543
1544 /* The scrollable layout and the search hbox go together in a vbox */
e9b70a84 1545 NW(vbox);
460b9539 1546 vbox = gtk_vbox_new(FALSE/*homogenous*/, 1/*spacing*/);
1547 gtk_box_pack_start(GTK_BOX(vbox), hbox,
1548 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
1549 gtk_box_pack_end(GTK_BOX(vbox), scrolled,
1550 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
1551
1552 g_object_set_data(G_OBJECT(vbox), "type", (void *)&tabtype_choose);
1553 return vbox;
1554}
1555
b9bdafc7 1556/** @brief Called when something we care about here might have changed */
460b9539 1557void choose_update(void) {
73dd383f 1558 redisplay_tree("choose_update");
460b9539 1559}
1560
1561/*
1562Local Variables:
1563c-basic-offset:2
1564comment-column:40
1565fill-column:79
1566indent-tabs-mode:nil
1567End:
1568*/