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