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