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 | */ |
20 | |
21 | #include "disobedience.h" |
22 | |
23 | /* Choose track ------------------------------------------------------------ */ |
24 | |
e9b70a84 |
25 | WT(label); |
26 | WT(event_box); |
27 | WT(menu); |
28 | WT(menu_item); |
29 | WT(layout); |
30 | WT(vbox); |
31 | WT(arrow); |
32 | WT(hbox); |
33 | WT(button); |
34 | WT(image); |
35 | WT(entry); |
36 | |
460b9539 |
37 | /* We don't use the built-in tree widgets because they require that you know |
38 | * the children of a node on demand, and we have to wait for the server to tell |
39 | * us. */ |
40 | |
41 | /* Types */ |
42 | |
43 | struct choosenode; |
44 | |
45 | struct displaydata { |
46 | guint width; /* total width required */ |
47 | guint height; /* total height required */ |
48 | }; |
49 | |
50 | /* instantiate the node vector type */ |
51 | VECTOR_TYPE(nodevector, struct choosenode *, xrealloc) |
52 | |
53 | struct choosenode { |
54 | struct choosenode *parent; /* parent node */ |
55 | const char *path; /* full path or 0 */ |
56 | const char *sort; /* sort key */ |
57 | const char *display; /* display name */ |
58 | int pending; /* pending resolve queries */ |
59 | unsigned flags; |
60 | #define CN_EXPANDABLE 0x0001 /* node is expandable */ |
61 | #define CN_EXPANDED 0x0002 /* node is expanded */ |
62 | /* Expandable items are directories; non-expandable ones are files */ |
63 | #define CN_DISPLAYED 0x0004 /* widget is displayed in layout */ |
64 | #define CN_SELECTED 0x0008 /* node is selected */ |
65 | struct nodevector children; /* vector of children */ |
66 | void (*fill)(struct choosenode *); /* request child fill or 0 for leaf */ |
67 | GtkWidget *container; /* the container for this row */ |
68 | GtkWidget *hbox; /* the hbox for this row */ |
69 | GtkWidget *arrow; /* arrow widget or 0 */ |
70 | GtkWidget *label; /* text label for this node */ |
71 | GtkWidget *marker; /* queued marker */ |
72 | }; |
73 | |
74 | struct menuitem { |
75 | /* Parameters */ |
76 | const char *name; /* name */ |
77 | |
78 | /* Callbacks */ |
79 | void (*activate)(GtkMenuItem *menuitem, gpointer user_data); |
80 | /* Called to activate the menu item. The user data is the choosenode the |
81 | * pointer is over. */ |
82 | |
83 | gboolean (*sensitive)(struct choosenode *cn); |
84 | /* Called to determine whether the menu item should be sensitive. TODO */ |
85 | |
86 | /* State */ |
87 | gulong handlerid; /* signal handler ID */ |
88 | GtkWidget *w; /* menu item widget */ |
89 | }; |
90 | |
91 | /* Variables */ |
92 | |
93 | static GtkWidget *chooselayout; |
94 | static GtkWidget *searchentry; /* search terms */ |
95 | static struct choosenode *root; |
96 | static struct choosenode *realroot; |
97 | static GtkWidget *menu; /* our popup menu */ |
98 | static struct choosenode *last_click; /* last clicked node for selection */ |
99 | static int files_visible; /* total files visible */ |
100 | static int files_selected; /* total files selected */ |
101 | static int search_in_flight; /* a search is underway */ |
102 | static int search_obsolete; /* the current search is void */ |
103 | static char **searchresults; /* search results */ |
104 | static int nsearchresults; /* number of results */ |
105 | |
106 | /* Forward Declarations */ |
107 | |
77d95881 |
108 | static void clear_children(struct choosenode *cn); |
460b9539 |
109 | static struct choosenode *newnode(struct choosenode *parent, |
110 | const char *path, |
111 | const char *display, |
112 | const char *sort, |
113 | unsigned flags, |
114 | void (*fill)(struct choosenode *)); |
115 | static void fill_root_node(struct choosenode *cn); |
116 | static void fill_letter_node(struct choosenode *cn); |
117 | static void fill_directory_node(struct choosenode *cn); |
118 | static void got_files(void *v, int nvec, char **vec); |
119 | static void got_resolved_file(void *v, const char *track); |
120 | static void got_dirs(void *v, int nvec, char **vec); |
121 | |
122 | static void expand_node(struct choosenode *cn); |
123 | static void contract_node(struct choosenode *cn); |
124 | static void updated_node(struct choosenode *cn, int redisplay); |
125 | |
126 | static void display_selection(struct choosenode *cn); |
127 | static void clear_selection(struct choosenode *cn); |
128 | |
129 | static void redisplay_tree(void); |
130 | static struct displaydata display_tree(struct choosenode *cn, int x, int y); |
131 | static void undisplay_tree(struct choosenode *cn); |
132 | static void initiate_search(void); |
133 | static void delete_widgets(struct choosenode *cn); |
134 | |
135 | static void clicked_choosenode(GtkWidget attribute((unused)) *widget, |
136 | GdkEventButton *event, |
137 | gpointer user_data); |
138 | |
139 | static void activate_play(GtkMenuItem *menuitem, gpointer user_data); |
140 | static void activate_remove(GtkMenuItem *menuitem, gpointer user_data); |
141 | static void activate_properties(GtkMenuItem *menuitem, gpointer user_data); |
142 | |
143 | static gboolean sensitive_play(struct choosenode *cn); |
144 | static gboolean sensitive_remove(struct choosenode *cn); |
145 | static gboolean sensitive_properties(struct choosenode *cn); |
146 | |
147 | static struct menuitem menuitems[] = { |
148 | { "Play", activate_play, sensitive_play, 0, 0 }, |
149 | { "Remove", activate_remove, sensitive_remove, 0, 0 }, |
150 | { "Properties", activate_properties, sensitive_properties, 0, 0 }, |
151 | }; |
152 | |
153 | #define NMENUITEMS (int)(sizeof menuitems / sizeof *menuitems) |
154 | |
155 | /* Maintaining the data structure ------------------------------------------ */ |
156 | |
157 | /* Create a new node */ |
158 | static struct choosenode *newnode(struct choosenode *parent, |
159 | const char *path, |
160 | const char *display, |
161 | const char *sort, |
162 | unsigned flags, |
163 | void (*fill)(struct choosenode *)) { |
164 | struct choosenode *const n = xmalloc(sizeof *n); |
165 | |
166 | D(("newnode %s %s", path, display)); |
167 | if(flags & CN_EXPANDABLE) |
168 | assert(fill); |
169 | else |
170 | assert(!fill); |
171 | n->parent = parent; |
172 | n->path = path; |
173 | n->display = display; |
174 | n->sort = sort; |
175 | n->flags = flags; |
176 | nodevector_init(&n->children); |
177 | n->fill = fill; |
178 | if(parent) |
179 | nodevector_append(&parent->children, n); |
180 | return n; |
181 | } |
182 | |
183 | /* Fill the root */ |
184 | static void fill_root_node(struct choosenode *cn) { |
185 | int ch; |
186 | char *name; |
187 | struct callbackdata *cbd; |
188 | |
189 | D(("fill_root_node")); |
77d95881 |
190 | clear_children(cn); |
460b9539 |
191 | if(choosealpha) { |
192 | if(!cn->children.nvec) { /* Only need to do this once */ |
193 | for(ch = 'A'; ch <= 'Z'; ++ch) { |
194 | byte_xasprintf(&name, "%c", ch); |
195 | newnode(cn, "<letter>", name, name, CN_EXPANDABLE, fill_letter_node); |
196 | } |
197 | newnode(cn, "<letter>", "*", "~", CN_EXPANDABLE, fill_letter_node); |
198 | } |
199 | updated_node(cn, 1); |
200 | } else { |
201 | /* More de-duping possible here */ |
202 | gtk_label_set_text(GTK_LABEL(report_label), "getting files"); |
203 | cbd = xmalloc(sizeof *cbd); |
204 | cbd->u.choosenode = cn; |
205 | disorder_eclient_dirs(client, got_dirs, "", 0, cbd); |
206 | cbd = xmalloc(sizeof *cbd); |
207 | cbd->u.choosenode = cn; |
208 | disorder_eclient_files(client, got_files, "", 0, cbd); |
209 | } |
210 | } |
211 | |
e9b70a84 |
212 | static void delete_cn_widgets(struct choosenode *cn) { |
213 | if(cn->arrow) { |
214 | DW(arrow); |
215 | gtk_widget_destroy(cn->arrow); |
216 | cn->arrow = 0; |
217 | } |
218 | if(cn->label) { |
219 | DW(label); |
220 | gtk_widget_destroy(cn->label); |
221 | cn->label = 0; |
222 | } |
223 | if(cn->marker) { |
224 | DW(image); |
225 | gtk_widget_destroy(cn->marker); |
226 | cn->marker = 0; |
227 | } |
228 | if(cn->hbox) { |
229 | DW(hbox); |
230 | gtk_widget_destroy(cn->hbox); |
231 | cn->hbox = 0; |
232 | } |
233 | if(cn->container) { |
234 | DW(event_box); |
235 | gtk_widget_destroy(cn->container); |
236 | cn->container = 0; |
237 | } |
238 | } |
239 | |
460b9539 |
240 | /* Clear all the children of CN */ |
241 | static void clear_children(struct choosenode *cn) { |
242 | int n; |
243 | |
244 | D(("clear_children %s", cn->path)); |
245 | /* Recursively clear subtrees */ |
246 | for(n = 0; n < cn->children.nvec; ++n) { |
247 | clear_children(cn->children.vec[n]); |
e9b70a84 |
248 | delete_cn_widgets(cn->children.vec[n]); |
460b9539 |
249 | } |
250 | cn->children.nvec = 0; |
251 | } |
252 | |
253 | /* Fill a child node */ |
254 | static void fill_letter_node(struct choosenode *cn) { |
255 | const char *regexp; |
256 | struct callbackdata *cbd; |
257 | |
258 | D(("fill_letter_node %s", cn->display)); |
259 | switch(cn->display[0]) { |
260 | default: |
261 | byte_xasprintf((char **)®exp, "^(the )?%c", tolower(cn->display[0])); |
262 | break; |
263 | case 'T': |
264 | regexp = "^(?!the [^t])t"; |
265 | break; |
266 | case '*': |
267 | regexp = "^[^a-z]"; |
268 | break; |
269 | } |
270 | /* TODO: caching */ |
271 | /* TODO: de-dupe against fill_directory_node */ |
272 | gtk_label_set_text(GTK_LABEL(report_label), "getting files"); |
273 | clear_children(cn); |
274 | cbd = xmalloc(sizeof *cbd); |
275 | cbd->u.choosenode = cn; |
276 | disorder_eclient_dirs(client, got_dirs, "", regexp, cbd); |
277 | cbd = xmalloc(sizeof *cbd); |
278 | cbd->u.choosenode = cn; |
279 | disorder_eclient_files(client, got_files, "", regexp, cbd); |
280 | } |
281 | |
282 | /* Called with a list of files just below some node */ |
283 | static void got_files(void *v, int nvec, char **vec) { |
284 | struct callbackdata *cbd = v; |
285 | struct choosenode *cn = cbd->u.choosenode; |
286 | int n; |
287 | |
288 | D(("got_files %d files for %s", nvec, cn->path)); |
289 | /* Complicated by the need to resolve aliases. We can save a bit of effort |
290 | * by re-using cbd though. */ |
291 | cn->pending = nvec; |
292 | for(n = 0; n < nvec; ++n) |
293 | disorder_eclient_resolve(client, got_resolved_file, vec[n], cbd); |
294 | } |
295 | |
296 | static void got_resolved_file(void *v, const char *track) { |
297 | struct callbackdata *cbd = v; |
298 | struct choosenode *cn = cbd->u.choosenode, *file_cn; |
299 | |
300 | file_cn = newnode(cn, track, |
301 | trackname_transform("track", track, "display"), |
302 | trackname_transform("track", track, "sort"), |
303 | 0/*flags*/, 0/*fill*/); |
304 | /* Only bother updating when we've got the lot */ |
305 | if(--cn->pending == 0) |
306 | updated_node(cn, 1); |
307 | } |
308 | |
309 | /* Called with a list of directories just below some node */ |
310 | static void got_dirs(void *v, int nvec, char **vec) { |
311 | struct callbackdata *cbd = v; |
312 | struct choosenode *cn = cbd->u.choosenode; |
313 | int n; |
314 | |
315 | D(("got_dirs %d dirs for %s", nvec, cn->path)); |
316 | for(n = 0; n < nvec; ++n) |
317 | newnode(cn, vec[n], |
318 | trackname_transform("dir", vec[n], "display"), |
319 | trackname_transform("dir", vec[n], "sort"), |
320 | CN_EXPANDABLE, fill_directory_node); |
321 | updated_node(cn, 1); |
322 | } |
323 | |
324 | /* Fill a child node */ |
325 | static void fill_directory_node(struct choosenode *cn) { |
326 | struct callbackdata *cbd; |
327 | |
328 | D(("fill_directory_node %s", cn->path)); |
329 | /* TODO: caching */ |
330 | /* TODO: de-dupe against fill_letter_node */ |
331 | assert(report_label != 0); |
332 | gtk_label_set_text(GTK_LABEL(report_label), "getting files"); |
1d0c28c7 |
333 | clear_children(cn); |
460b9539 |
334 | cbd = xmalloc(sizeof *cbd); |
335 | cbd->u.choosenode = cn; |
336 | disorder_eclient_dirs(client, got_dirs, cn->path, 0, cbd); |
337 | cbd = xmalloc(sizeof *cbd); |
338 | cbd->u.choosenode = cn; |
339 | disorder_eclient_files(client, got_files, cn->path, 0, cbd); |
340 | } |
341 | |
342 | /* Expand a node */ |
343 | static void expand_node(struct choosenode *cn) { |
344 | D(("expand_node %s", cn->path)); |
345 | assert(cn->flags & CN_EXPANDABLE); |
346 | /* If node is already expanded do nothing. */ |
347 | if(cn->flags & CN_EXPANDED) return; |
348 | /* We mark the node as expanded and request that it fill itself. When it has |
349 | * completed it will called updated_node() and we can redraw at that |
350 | * point. */ |
351 | cn->flags |= CN_EXPANDED; |
352 | /* TODO: visual feedback */ |
353 | cn->fill(cn); |
354 | } |
355 | |
356 | /* Contract a node */ |
357 | static void contract_node(struct choosenode *cn) { |
358 | D(("contract_node %s", cn->path)); |
359 | assert(cn->flags & CN_EXPANDABLE); |
360 | /* If node is already contracted do nothing. */ |
361 | if(!(cn->flags & CN_EXPANDED)) return; |
362 | cn->flags &= ~CN_EXPANDED; |
363 | /* Clear selection below this node */ |
364 | clear_selection(cn); |
e9b70a84 |
365 | /* Zot children. We never used to do this but the result would be that over |
366 | * time you'd end up with the entire tree pulled into memory. If the server |
367 | * is over a slow network it will make interactivity slightly worse; if |
368 | * anyone complains we can make it an option. */ |
369 | clear_children(cn); |
460b9539 |
370 | /* We can contract a node immediately. */ |
371 | redisplay_tree(); |
372 | } |
373 | |
374 | /* qsort callback for ordering choosenodes */ |
375 | static int compare_choosenode(const void *av, const void *bv) { |
376 | const struct choosenode *const *aa = av, *const *bb = bv; |
377 | const struct choosenode *a = *aa, *b = *bb; |
378 | |
379 | return compare_tracks(a->sort, b->sort, |
380 | a->display, b->display, |
381 | a->path, b->path); |
382 | } |
383 | |
384 | /* Called when an expandable node is updated. */ |
385 | static void updated_node(struct choosenode *cn, int redisplay) { |
386 | D(("updated_node %s", cn->path)); |
387 | assert(cn->flags & CN_EXPANDABLE); |
388 | /* It might be that the node has been de-expanded since we requested the |
389 | * update. In that case we ignore this notification. */ |
390 | if(!(cn->flags & CN_EXPANDED)) return; |
391 | /* Sort children */ |
392 | qsort(cn->children.vec, cn->children.nvec, sizeof (struct choosenode *), |
393 | compare_choosenode); |
394 | if(redisplay) |
395 | redisplay_tree(); |
396 | } |
397 | |
398 | /* Searching --------------------------------------------------------------- */ |
399 | |
400 | static int compare_track_for_qsort(const void *a, const void *b) { |
401 | return compare_path(*(char **)a, *(char **)b); |
402 | } |
403 | |
404 | /* Return true iff FILE is a child of DIR */ |
405 | static int is_child(const char *dir, const char *file) { |
406 | const size_t dlen = strlen(dir); |
407 | |
408 | return (!strncmp(file, dir, dlen) |
409 | && file[dlen] == '/' |
410 | && strchr(file + dlen + 1, '/') == 0); |
411 | } |
412 | |
413 | /* Return true iff FILE is a descendant of DIR */ |
414 | static int is_descendant(const char *dir, const char *file) { |
415 | const size_t dlen = strlen(dir); |
416 | |
417 | return !strncmp(file, dir, dlen) && file[dlen] == '/'; |
418 | } |
419 | |
420 | /* Called to fill a node in the search results tree */ |
421 | static void fill_search_node(struct choosenode *cn) { |
422 | int n; |
423 | const size_t plen = strlen(cn->path); |
424 | const char *s; |
425 | char *dir, *last = 0; |
426 | |
427 | D(("fill_search_node %s", cn->path)); |
428 | /* We depend on the search results being sorted as by compare_path(). */ |
429 | cn->children.nvec = 0; |
430 | for(n = 0; n < nsearchresults; ++n) { |
431 | /* We only care about descendants of CN */ |
432 | if(!is_descendant(cn->path, searchresults[n])) |
433 | continue; |
434 | s = strchr(searchresults[n] + plen + 1, '/'); |
435 | if(s) { |
436 | /* We've identified a subdirectory of CN. */ |
437 | dir = xstrndup(searchresults[n], s - searchresults[n]); |
438 | if(!last || strcmp(dir, last)) { |
439 | /* Not a duplicate */ |
440 | last = dir; |
441 | newnode(cn, dir, |
442 | trackname_transform("dir", dir, "display"), |
443 | trackname_transform("dir", dir, "sort"), |
444 | CN_EXPANDABLE, fill_search_node); |
445 | } |
446 | } else { |
447 | /* We've identified a file in CN */ |
448 | newnode(cn, searchresults[n], |
449 | trackname_transform("track", searchresults[n], "display"), |
450 | trackname_transform("track", searchresults[n], "sort"), |
451 | 0/*flags*/, 0/*fill*/); |
452 | } |
453 | } |
454 | updated_node(cn, 1); |
455 | } |
456 | |
457 | /* This is called from eclient with a (possibly empty) list of search results, |
458 | * and also from initiate_seatch with an always empty list to indicate that |
459 | * we're not searching for anything in particular. */ |
460 | static void search_completed(void attribute((unused)) *v, |
461 | int nvec, char **vec) { |
462 | struct choosenode *cn; |
463 | int n; |
464 | const char *dir; |
465 | |
466 | search_in_flight = 0; |
467 | if(search_obsolete) { |
468 | /* This search has been obsoleted by user input since it started. |
469 | * Therefore we throw away the result and search again. */ |
470 | search_obsolete = 0; |
471 | initiate_search(); |
472 | } else { |
473 | if(nvec) { |
474 | /* We will replace the choose tree with a tree structured view of search |
475 | * results. First we must disabled the choose tree's widgets. */ |
476 | delete_widgets(root); |
477 | /* Put the tracks into order, grouped by directory. They'll probably |
478 | * come back this way anyway in current versions of the server, but it's |
479 | * cheap not to rely on it (compared with the massive effort we expend |
480 | * later on) */ |
481 | qsort(vec, nvec, sizeof(char *), compare_track_for_qsort); |
482 | searchresults = vec; |
483 | nsearchresults = nvec; |
484 | cn = root = newnode(0/*parent*/, "", "Search results", "", |
485 | CN_EXPANDABLE|CN_EXPANDED, fill_search_node); |
486 | /* Construct the initial tree. We do this in a single pass and expand |
487 | * everything, so you can actually see your search results. */ |
488 | for(n = 0; n < nsearchresults; ++n) { |
489 | /* Firstly we might need to go up a few directories to each an ancestor |
490 | * of this track */ |
491 | while(!is_descendant(cn->path, searchresults[n])) { |
492 | /* We report the update on each node the last time we see it (With |
493 | * display=0, the main purpose of this is to get the order of the |
494 | * children right.) */ |
495 | updated_node(cn, 0); |
496 | cn = cn->parent; |
497 | } |
498 | /* Secondly we might need to insert some new directories */ |
499 | while(!is_child(cn->path, searchresults[n])) { |
500 | /* Figure out the subdirectory */ |
501 | dir = xstrndup(searchresults[n], |
502 | strchr(searchresults[n] + strlen(cn->path) + 1, |
503 | '/') - searchresults[n]); |
504 | cn = newnode(cn, dir, |
505 | trackname_transform("dir", dir, "display"), |
506 | trackname_transform("dir", dir, "sort"), |
507 | CN_EXPANDABLE|CN_EXPANDED, fill_search_node); |
508 | } |
509 | /* Finally we can insert the track as a child of the current |
510 | * directory */ |
511 | newnode(cn, searchresults[n], |
512 | trackname_transform("track", searchresults[n], "display"), |
513 | trackname_transform("track", searchresults[n], "sort"), |
514 | 0/*flags*/, 0/*fill*/); |
515 | } |
516 | while(cn) { |
517 | /* Update all the nodes back up to the root */ |
518 | updated_node(cn, 0); |
519 | cn = cn->parent; |
520 | } |
521 | /* Now it's worth displaying the tree */ |
522 | redisplay_tree(); |
523 | } else if(root != realroot) { |
524 | delete_widgets(root); |
525 | root = realroot; |
526 | redisplay_tree(); |
527 | } |
528 | } |
529 | } |
530 | |
531 | static void initiate_search(void) { |
532 | char *terms, *e; |
533 | |
534 | /* Find out what the user is after */ |
535 | terms = xstrdup(gtk_entry_get_text(GTK_ENTRY(searchentry))); |
536 | /* Strip leading and trailing space */ |
537 | while(*terms == ' ') ++terms; |
538 | e = terms + strlen(terms); |
539 | while(e > terms && e[-1] == ' ') --e; |
540 | *e = 0; |
541 | /* If a search is already underway then mark it as obsolete. We'll revisit |
542 | * when it returns. */ |
543 | if(search_in_flight) { |
544 | search_obsolete = 1; |
545 | return; |
546 | } |
547 | if(*terms) { |
548 | /* There's still something left. Initiate the search. */ |
549 | if(disorder_eclient_search(client, search_completed, terms, 0)) { |
550 | /* The search terms are bad! We treat this as if there were no search |
551 | * terms at all. Some kind of feedback would be handy. */ |
552 | fprintf(stderr, "bad terms [%s]\n", terms); /* TODO */ |
553 | search_completed(0, 0, 0); |
554 | } else { |
555 | search_in_flight = 1; |
556 | } |
557 | } else { |
558 | /* No search terms - we want to see all tracks */ |
559 | search_completed(0, 0, 0); |
560 | } |
561 | } |
562 | |
563 | /* Called when the cancel search button is clicked */ |
564 | static void clearsearch_clicked(GtkButton attribute((unused)) *button, |
565 | gpointer attribute((unused)) userdata) { |
566 | gtk_entry_set_text(GTK_ENTRY(searchentry), ""); |
567 | } |
568 | |
569 | /* Display functions ------------------------------------------------------- */ |
570 | |
571 | /* Delete all the widgets in the tree */ |
572 | static void delete_widgets(struct choosenode *cn) { |
573 | int n; |
574 | |
e9b70a84 |
575 | delete_cn_widgets(cn); |
460b9539 |
576 | for(n = 0; n < cn->children.nvec; ++n) |
577 | delete_widgets(cn->children.vec[n]); |
578 | cn->flags &= ~(CN_DISPLAYED|CN_SELECTED); |
579 | files_selected = 0; |
580 | } |
581 | |
582 | /* Update the display */ |
583 | static void redisplay_tree(void) { |
584 | struct displaydata d; |
585 | guint oldwidth, oldheight; |
586 | |
587 | D(("redisplay_tree")); |
588 | /* We'll count these up empirically each time */ |
589 | files_selected = 0; |
590 | files_visible = 0; |
591 | /* Correct the layout and find out how much space it uses */ |
592 | d = display_tree(root, 0, 0); |
593 | /* We must set the total size or scrolling will not work (it wouldn't be hard |
594 | * for GtkLayout to figure it out for itself but presumably you're supposed |
595 | * to be able to have widgets off the edge of the layuot.) |
596 | * |
597 | * There is a problem: if we shrink the size then the part of the screen that |
598 | * is outside the new size but inside the old one is not updated. I think |
599 | * this is arguably bug in GTK+ but it's easy to force a redraw if this |
600 | * region is nonempty. |
601 | */ |
602 | gtk_layout_get_size(GTK_LAYOUT(chooselayout), &oldwidth, &oldheight); |
603 | if(oldwidth > d.width || oldheight > d.height) |
604 | gtk_widget_queue_draw(chooselayout); |
605 | gtk_layout_set_size(GTK_LAYOUT(chooselayout), d.width, d.height); |
606 | /* Notify the main menu of any recent changes */ |
607 | menu_update(-1); |
608 | } |
609 | |
610 | /* Make sure all displayed widgets from CN down exist and are in their proper |
611 | * place and return the vertical space used. */ |
612 | static struct displaydata display_tree(struct choosenode *cn, int x, int y) { |
613 | int n, aw; |
614 | GtkRequisition req; |
615 | struct displaydata d, cd; |
616 | GdkPixbuf *pb; |
617 | |
618 | D(("display_tree %s %d,%d", cn->path, x, y)); |
619 | |
620 | /* An expandable item contains an arrow and a text label. When you press the |
621 | * button it flips its expand state. |
622 | * |
623 | * A non-expandable item has just a text label and no arrow. |
624 | */ |
625 | if(!cn->container) { |
626 | /* Widgets need to be created */ |
e9b70a84 |
627 | NW(hbox); |
460b9539 |
628 | cn->hbox = gtk_hbox_new(FALSE, 1); |
629 | if(cn->flags & CN_EXPANDABLE) { |
e9b70a84 |
630 | NW(arrow); |
460b9539 |
631 | cn->arrow = gtk_arrow_new(cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN |
632 | : GTK_ARROW_RIGHT, |
633 | GTK_SHADOW_NONE); |
634 | cn->marker = 0; |
635 | } else { |
636 | cn->arrow = 0; |
e9b70a84 |
637 | if((pb = find_image("notes.png"))) { |
638 | NW(image); |
460b9539 |
639 | cn->marker = gtk_image_new_from_pixbuf(pb); |
e9b70a84 |
640 | } |
460b9539 |
641 | } |
e9b70a84 |
642 | NW(label); |
460b9539 |
643 | cn->label = gtk_label_new(cn->display); |
644 | if(cn->arrow) |
645 | gtk_container_add(GTK_CONTAINER(cn->hbox), cn->arrow); |
646 | gtk_container_add(GTK_CONTAINER(cn->hbox), cn->label); |
647 | if(cn->marker) |
648 | gtk_container_add(GTK_CONTAINER(cn->hbox), cn->marker); |
e9b70a84 |
649 | NW(event_box); |
460b9539 |
650 | cn->container = gtk_event_box_new(); |
651 | gtk_container_add(GTK_CONTAINER(cn->container), cn->hbox); |
652 | g_signal_connect(cn->container, "button-release-event", |
653 | G_CALLBACK(clicked_choosenode), cn); |
654 | g_signal_connect(cn->container, "button-press-event", |
655 | G_CALLBACK(clicked_choosenode), cn); |
656 | g_object_ref(cn->container); |
657 | gtk_widget_set_name(cn->label, "choose"); |
658 | gtk_widget_set_name(cn->container, "choose"); |
659 | /* Show everything by default */ |
660 | gtk_widget_show_all(cn->container); |
661 | } |
662 | assert(cn->container); |
663 | /* Make sure the icon is right */ |
664 | if(cn->flags & CN_EXPANDABLE) |
665 | gtk_arrow_set(GTK_ARROW(cn->arrow), |
666 | cn->flags & CN_EXPANDED ? GTK_ARROW_DOWN : GTK_ARROW_RIGHT, |
667 | GTK_SHADOW_NONE); |
668 | else if(cn->marker) |
669 | /* Make sure the queued marker is right */ |
670 | /* TODO: doesn't always work */ |
671 | (queued(cn->path) ? gtk_widget_show : gtk_widget_hide)(cn->marker); |
672 | /* Put the widget in the right place */ |
673 | if(cn->flags & CN_DISPLAYED) |
674 | gtk_layout_move(GTK_LAYOUT(chooselayout), cn->container, x, y); |
675 | else { |
676 | gtk_layout_put(GTK_LAYOUT(chooselayout), cn->container, x, y); |
677 | cn->flags |= CN_DISPLAYED; |
678 | } |
679 | /* Set the widget's selection status */ |
680 | if(!(cn->flags & CN_EXPANDABLE)) |
681 | display_selection(cn); |
682 | /* Find the size used so we can get vertical positioning right. */ |
683 | gtk_widget_size_request(cn->container, &req); |
684 | d.width = x + req.width; |
685 | d.height = y + req.height; |
686 | if(cn->flags & CN_EXPANDED) { |
687 | /* We'll offset children by the size of the arrow whatever it might be. */ |
688 | assert(cn->arrow); |
689 | gtk_widget_size_request(cn->arrow, &req); |
690 | aw = req.width; |
691 | for(n = 0; n < cn->children.nvec; ++n) { |
692 | cd = display_tree(cn->children.vec[n], x + aw, d.height); |
693 | if(cd.width > d.width) |
694 | d.width = cd.width; |
695 | d.height = cd.height; |
696 | } |
697 | } else { |
698 | for(n = 0; n < cn->children.nvec; ++n) |
699 | undisplay_tree(cn->children.vec[n]); |
700 | } |
701 | if(!(cn->flags & CN_EXPANDABLE)) { |
702 | ++files_visible; |
703 | if(cn->flags & CN_SELECTED) |
704 | ++files_selected; |
705 | } |
706 | /* report back how much space we used */ |
707 | D(("display_tree %s %d,%d total size %dx%d", cn->path, x, y, |
708 | d.width, d.height)); |
709 | return d; |
710 | } |
711 | |
712 | /* Remove widgets for newly hidden nodes */ |
713 | static void undisplay_tree(struct choosenode *cn) { |
714 | int n; |
715 | |
716 | D(("undisplay_tree %s", cn->path)); |
717 | /* Remove this widget from the display */ |
718 | if(cn->flags & CN_DISPLAYED) { |
719 | gtk_container_remove(GTK_CONTAINER(chooselayout), cn->container); |
720 | cn->flags ^= CN_DISPLAYED; |
721 | } |
722 | /* Remove children too */ |
723 | for(n = 0; n < cn->children.nvec; ++n) |
724 | undisplay_tree(cn->children.vec[n]); |
725 | } |
726 | |
727 | /* Selection --------------------------------------------------------------- */ |
728 | |
729 | static void display_selection(struct choosenode *cn) { |
730 | /* Need foreground and background colors */ |
731 | gtk_widget_set_state(cn->label, (cn->flags & CN_SELECTED |
732 | ? GTK_STATE_SELECTED : GTK_STATE_NORMAL)); |
733 | gtk_widget_set_state(cn->container, (cn->flags & CN_SELECTED |
734 | ? GTK_STATE_SELECTED : GTK_STATE_NORMAL)); |
735 | } |
736 | |
737 | /* Set the selection state of a widget. Directories can never be selected, we |
738 | * just ignore attempts to do so. */ |
739 | static void set_selection(struct choosenode *cn, int selected) { |
740 | unsigned f = selected ? CN_SELECTED : 0; |
741 | |
742 | D(("set_selection %d %s", selected, cn->path)); |
743 | if(!(cn->flags & CN_EXPANDABLE) && (cn->flags & CN_SELECTED) != f) { |
744 | cn->flags ^= CN_SELECTED; |
745 | /* Maintain selection count */ |
746 | if(selected) |
747 | ++files_selected; |
748 | else |
749 | --files_selected; |
750 | display_selection(cn); |
751 | /* Update main menu sensitivity */ |
752 | menu_update(-1); |
753 | } |
754 | } |
755 | |
756 | /* Recursively clear all selection bits from CN down */ |
757 | static void clear_selection(struct choosenode *cn) { |
758 | int n; |
759 | |
760 | set_selection(cn, 0); |
761 | for(n = 0; n < cn->children.nvec; ++n) |
762 | clear_selection(cn->children.vec[n]); |
763 | } |
764 | |
765 | /* User actions ------------------------------------------------------------ */ |
766 | |
767 | /* Clicked on something */ |
768 | static void clicked_choosenode(GtkWidget attribute((unused)) *widget, |
769 | GdkEventButton *event, |
770 | gpointer user_data) { |
771 | struct choosenode *cn = user_data; |
772 | int ind, last_ind, n; |
773 | |
774 | D(("clicked_choosenode %s", cn->path)); |
775 | if(event->type == GDK_BUTTON_RELEASE |
776 | && event->button == 1) { |
777 | /* Left click */ |
778 | if(cn->flags & CN_EXPANDABLE) { |
779 | /* This is a directory. Flip its expansion status. */ |
780 | if(cn->flags & CN_EXPANDED) |
781 | contract_node(cn); |
782 | else |
783 | expand_node(cn); |
784 | last_click = 0; |
785 | } else { |
786 | /* This is a file. Adjust selection status */ |
787 | /* TODO the basic logic here is essentially the same as that in queue.c. |
788 | * Can we share code at all? */ |
789 | switch(event->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) { |
790 | case 0: |
791 | clear_selection(root); |
792 | set_selection(cn, 1); |
793 | last_click = cn; |
794 | break; |
795 | case GDK_CONTROL_MASK: |
796 | set_selection(cn, !(cn->flags & CN_SELECTED)); |
797 | last_click = cn; |
798 | break; |
799 | case GDK_SHIFT_MASK: |
800 | case GDK_SHIFT_MASK|GDK_CONTROL_MASK: |
801 | if(last_click && last_click->parent == cn->parent) { |
802 | /* Figure out where the current and last clicks are in the list */ |
803 | ind = last_ind = -1; |
804 | for(n = 0; n < cn->parent->children.nvec; ++n) { |
805 | if(cn->parent->children.vec[n] == cn) |
806 | ind = n; |
807 | if(cn->parent->children.vec[n] == last_click) |
808 | last_ind = n; |
809 | } |
810 | /* Test shouldn't ever fail, but still */ |
811 | if(ind >= 0 && last_ind >= 0) { |
812 | if(!(event->state & GDK_CONTROL_MASK)) { |
813 | for(n = 0; n < cn->parent->children.nvec; ++n) |
814 | set_selection(cn->parent->children.vec[n], 0); |
815 | } |
816 | if(ind > last_ind) |
817 | for(n = last_ind; n <= ind; ++n) |
818 | set_selection(cn->parent->children.vec[n], 1); |
819 | else |
820 | for(n = ind; n <= last_ind; ++n) |
821 | set_selection(cn->parent->children.vec[n], 1); |
822 | if(event->state & GDK_CONTROL_MASK) |
823 | last_click = cn; |
824 | } |
825 | } |
826 | break; |
827 | } |
828 | } |
829 | } else if(event->type == GDK_BUTTON_RELEASE |
830 | && event->button == 2) { |
831 | /* Middle click - play the pointed track */ |
832 | if(!(cn->flags & CN_EXPANDABLE)) { |
833 | clear_selection(root); |
834 | set_selection(cn, 1); |
835 | gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue"); |
836 | disorder_eclient_play(client, cn->path, 0, 0); |
837 | last_click = 0; |
838 | } |
839 | } else if(event->type == GDK_BUTTON_PRESS |
840 | && event->button == 3) { |
841 | /* Right click. Pop up a menu. */ |
842 | /* If the current file isn't selected, switch the selection to just that. |
843 | * (If we're looking at a directory then leave the selection alone.) */ |
844 | if(!(cn->flags & CN_EXPANDABLE) && !(cn->flags & CN_SELECTED)) { |
845 | clear_selection(root); |
846 | set_selection(cn, 1); |
847 | last_click = cn; |
848 | } |
849 | /* Set the item sensitivity and callbacks */ |
850 | for(n = 0; n < NMENUITEMS; ++n) { |
851 | if(menuitems[n].handlerid) |
852 | g_signal_handler_disconnect(menuitems[n].w, |
853 | menuitems[n].handlerid); |
854 | gtk_widget_set_sensitive(menuitems[n].w, |
855 | menuitems[n].sensitive(cn)); |
856 | menuitems[n].handlerid = g_signal_connect |
857 | (menuitems[n].w, "activate", G_CALLBACK(menuitems[n].activate), cn); |
858 | } |
859 | /* Pop up the menu */ |
860 | gtk_widget_show_all(menu); |
861 | gtk_menu_popup(GTK_MENU(menu), 0, 0, 0, 0, |
862 | event->button, event->time); |
863 | } |
864 | } |
865 | |
866 | static void searchentry_changed(GtkEditable attribute((unused)) *editable, |
867 | gpointer attribute((unused)) user_data) { |
868 | initiate_search(); |
869 | } |
870 | |
871 | /* Menu items -------------------------------------------------------------- */ |
872 | |
873 | static void recurse_selected(struct choosenode *cn, struct vector *v) { |
874 | int n; |
875 | |
876 | if(cn->flags & CN_EXPANDABLE) { |
877 | if(cn->flags & CN_EXPANDED) |
878 | for(n = 0; n < cn->children.nvec; ++n) |
879 | recurse_selected(cn->children.vec[n], v); |
880 | } else { |
881 | if((cn->flags & CN_SELECTED) && cn->path) |
882 | vector_append(v, (char *)cn->path); |
883 | } |
884 | } |
885 | |
886 | static char **gather_selected(int *ntracks) { |
887 | struct vector v; |
888 | |
889 | vector_init(&v); |
890 | recurse_selected(root, &v); |
891 | vector_terminate(&v); |
892 | if(ntracks) *ntracks = v.nvec; |
893 | return v.vec; |
894 | } |
895 | |
896 | static void activate_play(GtkMenuItem attribute((unused)) *menuitem, |
897 | gpointer attribute((unused)) user_data) { |
898 | char **tracks = gather_selected(0); |
899 | int n; |
900 | |
901 | gtk_label_set_text(GTK_LABEL(report_label), "adding track to queue"); |
902 | for(n = 0; tracks[n]; ++n) |
903 | disorder_eclient_play(client, tracks[n], 0, 0); |
904 | } |
905 | |
906 | static void activate_remove(GtkMenuItem attribute((unused)) *menuitem, |
907 | gpointer attribute((unused)) user_data) { |
908 | /* TODO remove all selected tracks */ |
909 | } |
910 | |
911 | static void activate_properties(GtkMenuItem attribute((unused)) *menuitem, |
912 | gpointer attribute((unused)) user_data) { |
913 | int ntracks; |
914 | char **tracks = gather_selected(&ntracks); |
915 | |
916 | properties(ntracks, tracks); |
917 | } |
918 | |
919 | static gboolean sensitive_play(struct choosenode attribute((unused)) *cn) { |
920 | return !!files_selected; |
921 | } |
922 | |
923 | static gboolean sensitive_remove(struct choosenode attribute((unused)) *cn) { |
924 | return FALSE; /* not implemented yet */ |
925 | } |
926 | |
927 | static gboolean sensitive_properties(struct choosenode attribute((unused)) *cn) { |
928 | return !!files_selected; |
929 | } |
930 | |
931 | /* Main menu plumbing ------------------------------------------------------ */ |
932 | |
933 | static int choose_properties_sensitive(GtkWidget attribute((unused)) *w) { |
934 | return !!files_selected; |
935 | } |
936 | |
937 | static int choose_selectall_sensitive(GtkWidget attribute((unused)) *w) { |
938 | return FALSE; /* TODO */ |
939 | } |
940 | |
941 | static void choose_properties_activate(GtkWidget attribute((unused)) *w) { |
942 | activate_properties(0, 0); |
943 | } |
944 | |
945 | static void choose_selectall_activate(GtkWidget attribute((unused)) *w) { |
946 | /* TODO */ |
947 | } |
948 | |
949 | static const struct tabtype tabtype_choose = { |
950 | choose_properties_sensitive, |
951 | choose_selectall_sensitive, |
952 | choose_properties_activate, |
953 | choose_selectall_activate, |
954 | }; |
955 | |
956 | /* Public entry points ----------------------------------------------------- */ |
957 | |
958 | /* Create a track choice widget */ |
959 | GtkWidget *choose_widget(void) { |
960 | int n; |
961 | GtkWidget *scrolled; |
962 | GtkWidget *vbox, *hbox, *clearsearch; |
963 | |
964 | /* |
965 | * +--vbox-------------------------------------------------------+ |
966 | * | +-hbox----------------------------------------------------+ | |
967 | * | | searchentry | clearsearch | | |
968 | * | +---------------------------------------------------------+ | |
969 | * | +-scrolled------------------------------------------------+ | |
970 | * | | +-chooselayout------------------------------------++--+ | | |
971 | * | | | Tree structure is manually layed out in here ||^^| | | |
972 | * | | | || | | | |
973 | * | | | || | | | |
974 | * | | | || | | | |
975 | * | | | ||vv| | | |
976 | * | | +-------------------------------------------------++--+ | | |
977 | * | | +-------------------------------------------------+ | | |
978 | * | | |< >| | | |
979 | * | | +-------------------------------------------------+ | | |
980 | * | +---------------------------------------------------------+ | |
981 | * +-------------------------------------------------------------+ |
982 | */ |
983 | |
984 | /* Text entry box for search terms */ |
e9b70a84 |
985 | NW(entry); |
460b9539 |
986 | searchentry = gtk_entry_new(); |
987 | g_signal_connect(searchentry, "changed", G_CALLBACK(searchentry_changed), 0); |
988 | |
989 | /* Cancel button to clear the search */ |
e9b70a84 |
990 | NW(button); |
460b9539 |
991 | clearsearch = gtk_button_new_from_stock(GTK_STOCK_CANCEL); |
992 | g_signal_connect(G_OBJECT(clearsearch), "clicked", |
993 | G_CALLBACK(clearsearch_clicked), 0); |
994 | |
995 | /* hbox packs the search box and the cancel button together on a line */ |
e9b70a84 |
996 | NW(hbox); |
460b9539 |
997 | hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*spacing*/); |
998 | gtk_box_pack_start(GTK_BOX(hbox), searchentry, |
999 | TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/); |
1000 | gtk_box_pack_end(GTK_BOX(hbox), clearsearch, |
1001 | FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/); |
1002 | |
1003 | /* chooselayout contains the currently visible subset of the track |
1004 | * namespace */ |
e9b70a84 |
1005 | NW(layout); |
460b9539 |
1006 | chooselayout = gtk_layout_new(0, 0); |
1007 | root = newnode(0/*parent*/, "<root>", "All files", "", |
1008 | CN_EXPANDABLE, fill_root_node); |
1009 | realroot = root; |
1010 | expand_node(root); /* will call redisplay_tree */ |
1011 | /* Create the popup menu */ |
e9b70a84 |
1012 | NW(menu); |
460b9539 |
1013 | menu = gtk_menu_new(); |
1014 | g_signal_connect(menu, "destroy", G_CALLBACK(gtk_widget_destroyed), &menu); |
1015 | for(n = 0; n < NMENUITEMS; ++n) { |
e9b70a84 |
1016 | NW(menu_item); |
460b9539 |
1017 | menuitems[n].w = gtk_menu_item_new_with_label(menuitems[n].name); |
1018 | gtk_menu_attach(GTK_MENU(menu), menuitems[n].w, 0, 1, n, n + 1); |
1019 | } |
1020 | /* The layout is scrollable */ |
1021 | scrolled = scroll_widget(GTK_WIDGET(chooselayout), "choose"); |
1022 | |
1023 | /* The scrollable layout and the search hbox go together in a vbox */ |
e9b70a84 |
1024 | NW(vbox); |
460b9539 |
1025 | vbox = gtk_vbox_new(FALSE/*homogenous*/, 1/*spacing*/); |
1026 | gtk_box_pack_start(GTK_BOX(vbox), hbox, |
1027 | FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/); |
1028 | gtk_box_pack_end(GTK_BOX(vbox), scrolled, |
1029 | TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/); |
1030 | |
1031 | g_object_set_data(G_OBJECT(vbox), "type", (void *)&tabtype_choose); |
1032 | return vbox; |
1033 | } |
1034 | |
1035 | /* Called when something we care about here might have changed */ |
1036 | void choose_update(void) { |
1037 | redisplay_tree(); |
1038 | } |
1039 | |
1040 | /* |
1041 | Local Variables: |
1042 | c-basic-offset:2 |
1043 | comment-column:40 |
1044 | fill-column:79 |
1045 | indent-tabs-mode:nil |
1046 | End: |
1047 | */ |