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