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