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