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