| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2008 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 3 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, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU 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, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | /** @file disobedience/choose.c |
| 19 | * @brief Hierarchical track selection and search |
| 20 | * |
| 21 | * We now use an ordinary GtkTreeStore/GtkTreeView. |
| 22 | * |
| 23 | * We don't want to pull the entire tree in memory, but we want directories to |
| 24 | * show up as having children. Therefore we give directories a placeholder |
| 25 | * child and replace their children when they are opened. Placeholders have |
| 26 | * TRACK_COLUMN="" and ISFILE_COLUMN=FALSE (so that they don't get check boxes, |
| 27 | * lengths, etc). |
| 28 | * |
| 29 | * TODO: |
| 30 | * - sweep up contracted nodes, replacing their content with a placeholder |
| 31 | */ |
| 32 | |
| 33 | #include "disobedience.h" |
| 34 | #include "choose.h" |
| 35 | #include "multidrag.h" |
| 36 | #include "queue-generic.h" |
| 37 | #include <gdk/gdkkeysyms.h> |
| 38 | |
| 39 | /** @brief Drag types */ |
| 40 | const GtkTargetEntry choose_targets[] = { |
| 41 | { |
| 42 | PLAYABLE_TRACKS, /* drag type */ |
| 43 | GTK_TARGET_SAME_APP|GTK_TARGET_OTHER_WIDGET, /* copying between widgets */ |
| 44 | PLAYABLE_TRACKS_ID /* ID value */ |
| 45 | }, |
| 46 | { |
| 47 | .target = NULL |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | /** @brief The current selection tree */ |
| 52 | GtkTreeStore *choose_store; |
| 53 | |
| 54 | /** @brief The view onto the selection tree */ |
| 55 | GtkWidget *choose_view; |
| 56 | |
| 57 | /** @brief The selection tree's selection */ |
| 58 | GtkTreeSelection *choose_selection; |
| 59 | |
| 60 | /** @brief Count of file listing operations in flight */ |
| 61 | static int choose_list_in_flight; |
| 62 | |
| 63 | /** @brief If nonzero autocollapse column won't be set */ |
| 64 | static int choose_suppress_set_autocollapse; |
| 65 | |
| 66 | static char *choose_get_string(GtkTreeIter *iter, int column) { |
| 67 | gchar *gs; |
| 68 | gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter, |
| 69 | column, &gs, |
| 70 | -1); |
| 71 | char *s = xstrdup(gs); |
| 72 | g_free(gs); |
| 73 | return s; |
| 74 | } |
| 75 | |
| 76 | char *choose_get_track(GtkTreeIter *iter) { |
| 77 | char *s = choose_get_string(iter, TRACK_COLUMN); |
| 78 | return *s ? s : 0; /* Placeholder -> NULL */ |
| 79 | } |
| 80 | |
| 81 | char *choose_get_sort(GtkTreeIter *iter) { |
| 82 | return choose_get_string(iter, SORT_COLUMN); |
| 83 | } |
| 84 | |
| 85 | char *choose_get_display(GtkTreeIter *iter) { |
| 86 | return choose_get_string(iter, NAME_COLUMN); |
| 87 | } |
| 88 | |
| 89 | int choose_is_file(GtkTreeIter *iter) { |
| 90 | gboolean isfile; |
| 91 | gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter, |
| 92 | ISFILE_COLUMN, &isfile, |
| 93 | -1); |
| 94 | return isfile; |
| 95 | } |
| 96 | |
| 97 | int choose_is_dir(GtkTreeIter *iter) { |
| 98 | gboolean isfile; |
| 99 | gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter, |
| 100 | ISFILE_COLUMN, &isfile, |
| 101 | -1); |
| 102 | if(isfile) |
| 103 | return FALSE; |
| 104 | return !choose_is_placeholder(iter); |
| 105 | } |
| 106 | |
| 107 | int choose_is_placeholder(GtkTreeIter *iter) { |
| 108 | return choose_get_string(iter, TRACK_COLUMN)[0] == 0; |
| 109 | } |
| 110 | |
| 111 | int choose_can_autocollapse(GtkTreeIter *iter) { |
| 112 | gboolean autocollapse; |
| 113 | gtk_tree_model_get(GTK_TREE_MODEL(choose_store), iter, |
| 114 | AUTOCOLLAPSE_COLUMN, &autocollapse, |
| 115 | -1); |
| 116 | return autocollapse; |
| 117 | } |
| 118 | |
| 119 | /** @brief Remove node @p it and all its children |
| 120 | * @param it Iterator, updated to point to next |
| 121 | * @return True if iterator remains valid |
| 122 | * |
| 123 | * TODO is this necessary? gtk_tree_store_remove() does not document what |
| 124 | * happens to children. |
| 125 | */ |
| 126 | static gboolean choose_remove_node(GtkTreeIter *it) { |
| 127 | GtkTreeIter child[1]; |
| 128 | gboolean childv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store), |
| 129 | child, |
| 130 | it); |
| 131 | while(childv) |
| 132 | childv = choose_remove_node(child); |
| 133 | return gtk_tree_store_remove(choose_store, it); |
| 134 | } |
| 135 | |
| 136 | /** @brief Update length and state fields */ |
| 137 | static gboolean choose_set_state_callback(GtkTreeModel attribute((unused)) *model, |
| 138 | GtkTreePath attribute((unused)) *path, |
| 139 | GtkTreeIter *it, |
| 140 | gpointer attribute((unused)) data) { |
| 141 | if(choose_is_file(it)) { |
| 142 | const char *track = choose_get_track(it); |
| 143 | const long l = namepart_length(track); |
| 144 | char length[64]; |
| 145 | if(l > 0) |
| 146 | byte_snprintf(length, sizeof length, "%ld:%02ld", l / 60, l % 60); |
| 147 | else |
| 148 | length[0] = 0; |
| 149 | gtk_tree_store_set(choose_store, it, |
| 150 | LENGTH_COLUMN, length, |
| 151 | STATE_COLUMN, queued(track), |
| 152 | -1); |
| 153 | if(choose_is_search_result(track)) |
| 154 | gtk_tree_store_set(choose_store, it, |
| 155 | BG_COLUMN, SEARCH_RESULT_BG, |
| 156 | FG_COLUMN, SEARCH_RESULT_FG, |
| 157 | -1); |
| 158 | else |
| 159 | gtk_tree_store_set(choose_store, it, |
| 160 | BG_COLUMN, (char *)0, |
| 161 | FG_COLUMN, (char *)0, |
| 162 | -1); |
| 163 | } |
| 164 | return FALSE; /* continue walking */ |
| 165 | } |
| 166 | |
| 167 | /** @brief Called when the queue or playing track change */ |
| 168 | static void choose_set_state(const char attribute((unused)) *event, |
| 169 | void attribute((unused)) *eventdata, |
| 170 | void attribute((unused)) *callbackdata) { |
| 171 | gtk_tree_model_foreach(GTK_TREE_MODEL(choose_store), |
| 172 | choose_set_state_callback, |
| 173 | NULL); |
| 174 | } |
| 175 | |
| 176 | /** @brief (Re-)populate a node |
| 177 | * @param parent_ref Node to populate or NULL to fill root |
| 178 | * @param nvec Number of children to add |
| 179 | * @param vec Children |
| 180 | * @param isfile 1 if children are files, 0 if directories |
| 181 | * |
| 182 | * Adjusts the set of files (or directories) below @p parent_ref to match those |
| 183 | * listed in @p nvec and @p vec. |
| 184 | * |
| 185 | * @p parent_ref will be destroyed. |
| 186 | */ |
| 187 | static void choose_populate(GtkTreeRowReference *parent_ref, |
| 188 | int nvec, char **vec, |
| 189 | int isfile) { |
| 190 | const char *type = isfile ? "track" : "dir"; |
| 191 | //fprintf(stderr, "%d new children of type %s\n", nvec, type); |
| 192 | if(!nvec) |
| 193 | goto skip; |
| 194 | /* Compute parent_* */ |
| 195 | GtkTreeIter pit[1], *parent_it; |
| 196 | GtkTreePath *parent_path; |
| 197 | if(parent_ref) { |
| 198 | parent_path = gtk_tree_row_reference_get_path(parent_ref); |
| 199 | parent_it = pit; |
| 200 | gboolean pitv = gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store), |
| 201 | pit, parent_path); |
| 202 | assert(pitv); |
| 203 | /*fprintf(stderr, "choose_populate %s: parent path is [%s]\n", |
| 204 | type, |
| 205 | gtk_tree_path_to_string(parent_path));*/ |
| 206 | } else { |
| 207 | parent_path = 0; |
| 208 | parent_it = 0; |
| 209 | /*fprintf(stderr, "choose_populate %s: populating the root\n", |
| 210 | type);*/ |
| 211 | } |
| 212 | /* Both td[] and the current node set are sorted so we can do a single linear |
| 213 | * pass to insert new nodes and remove unwanted ones. The total performance |
| 214 | * may be worse than linear depending on the performance of GTK+'s insert and |
| 215 | * delete operations. */ |
| 216 | //fprintf(stderr, "sorting tracks\n"); |
| 217 | struct tracksort_data *td = tracksort_init(nvec, vec, type); |
| 218 | GtkTreeIter it[1]; |
| 219 | gboolean itv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store), |
| 220 | it, |
| 221 | parent_it); |
| 222 | int inserted = 0, deleted_placeholder = 0; |
| 223 | //fprintf(stderr, "inserting tracks type=%s\n", type); |
| 224 | while(nvec > 0 || itv) { |
| 225 | /*fprintf(stderr, "td[] = %s, it=%s [%s]\n", |
| 226 | nvec > 0 ? td->track : "(none)", |
| 227 | itv ? choose_get_track(it) : "(!itv)", |
| 228 | itv ? (choose_is_file(it) ? "file" : "dir") : "");*/ |
| 229 | enum { INSERT, DELETE, SKIP_TREE, SKIP_BOTH } action; |
| 230 | const char *track = itv ? choose_get_track(it) : 0; |
| 231 | if(itv && !track) { |
| 232 | //fprintf(stderr, " placeholder\n"); |
| 233 | action = DELETE; |
| 234 | ++deleted_placeholder; |
| 235 | } else if(nvec > 0 && itv) { |
| 236 | /* There's both a tree row and a td[] entry */ |
| 237 | const int cmp = compare_tracks(td->sort, choose_get_sort(it), |
| 238 | td->display, choose_get_display(it), |
| 239 | td->track, track); |
| 240 | //fprintf(stderr, " cmp=%d\n", cmp); |
| 241 | if(cmp < 0) |
| 242 | /* td < it, so we insert td before it */ |
| 243 | action = INSERT; |
| 244 | else if(cmp > 0) { |
| 245 | /* td > it, so we must either delete it (if the same type) or skip it */ |
| 246 | if(choose_is_file(it) == isfile) |
| 247 | action = DELETE; |
| 248 | else |
| 249 | action = SKIP_TREE; |
| 250 | } else |
| 251 | /* td = it, so we step past both */ |
| 252 | action = SKIP_BOTH; |
| 253 | } else if(nvec > 0) { |
| 254 | /* We've reached the end of the tree rows, but new have tracks left in |
| 255 | * td[] */ |
| 256 | //fprintf(stderr, " inserting\n"); |
| 257 | action = INSERT; |
| 258 | } else { |
| 259 | /* We've reached the end of the new tracks from td[], but there are |
| 260 | * further tracks in the tree */ |
| 261 | //fprintf(stderr, " deleting\n"); |
| 262 | if(choose_is_file(it) == isfile) |
| 263 | action = DELETE; |
| 264 | else |
| 265 | action = SKIP_TREE; |
| 266 | } |
| 267 | |
| 268 | switch(action) { |
| 269 | case INSERT: { |
| 270 | //fprintf(stderr, " INSERT %s\n", td->track); |
| 271 | /* Insert a new row from td[] before it, or at the end if it is no longer |
| 272 | * valid */ |
| 273 | GtkTreeIter child[1]; |
| 274 | gtk_tree_store_insert_before(choose_store, |
| 275 | child, /* new row */ |
| 276 | parent_it, /* parent */ |
| 277 | itv ? it : NULL); /* successor */ |
| 278 | gtk_tree_store_set(choose_store, child, |
| 279 | NAME_COLUMN, td->display, |
| 280 | ISFILE_COLUMN, isfile, |
| 281 | TRACK_COLUMN, td->track, |
| 282 | SORT_COLUMN, td->sort, |
| 283 | AUTOCOLLAPSE_COLUMN, FALSE, |
| 284 | -1); |
| 285 | /* Update length and state; we expect this to kick off length lookups |
| 286 | * rather than necessarily get the right value the first time round. */ |
| 287 | choose_set_state_callback(0, 0, child, 0); |
| 288 | /* If we inserted a directory, insert a placeholder too, so it appears to |
| 289 | * have children; it will be deleted when we expand the directory. */ |
| 290 | if(!isfile) { |
| 291 | //fprintf(stderr, " inserting a placeholder\n"); |
| 292 | GtkTreeIter placeholder[1]; |
| 293 | |
| 294 | gtk_tree_store_append(choose_store, placeholder, child); |
| 295 | gtk_tree_store_set(choose_store, placeholder, |
| 296 | NAME_COLUMN, "Waddling...", |
| 297 | TRACK_COLUMN, "", |
| 298 | ISFILE_COLUMN, FALSE, |
| 299 | -1); |
| 300 | } |
| 301 | ++inserted; |
| 302 | ++td; |
| 303 | --nvec; |
| 304 | break; |
| 305 | } |
| 306 | case SKIP_BOTH: |
| 307 | //fprintf(stderr, " SKIP_BOTH\n"); |
| 308 | ++td; |
| 309 | --nvec; |
| 310 | /* fall thru */ |
| 311 | case SKIP_TREE: |
| 312 | //fprintf(stderr, " SKIP_TREE\n"); |
| 313 | itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it); |
| 314 | break; |
| 315 | case DELETE: |
| 316 | //fprintf(stderr, " DELETE\n"); |
| 317 | itv = choose_remove_node(it); |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | /*fprintf(stderr, "inserted=%d deleted_placeholder=%d\n\n", |
| 322 | inserted, deleted_placeholder);*/ |
| 323 | if(parent_ref) { |
| 324 | /* If we deleted a placeholder then we must re-expand the row */ |
| 325 | if(deleted_placeholder) { |
| 326 | ++choose_suppress_set_autocollapse; |
| 327 | gtk_tree_view_expand_row(GTK_TREE_VIEW(choose_view), parent_path, FALSE); |
| 328 | --choose_suppress_set_autocollapse; |
| 329 | } |
| 330 | gtk_tree_row_reference_free(parent_ref); |
| 331 | gtk_tree_path_free(parent_path); |
| 332 | } |
| 333 | skip: |
| 334 | /* We only notify others that we've inserted tracks when there are no more |
| 335 | * insertions pending, so that they don't have to keep track of how many |
| 336 | * requests they've made. */ |
| 337 | if(--choose_list_in_flight == 0) { |
| 338 | /* Notify interested parties that we inserted some tracks, AFTER making |
| 339 | * sure that the row is properly expanded */ |
| 340 | //fprintf(stderr, "raising choose-more-tracks\n"); |
| 341 | event_raise("choose-more-tracks", 0); |
| 342 | } |
| 343 | //fprintf(stderr, "choose_list_in_flight -> %d-\n", choose_list_in_flight); |
| 344 | } |
| 345 | |
| 346 | static void choose_dirs_completed(void *v, |
| 347 | const char *err, |
| 348 | int nvec, char **vec) { |
| 349 | if(err) { |
| 350 | popup_protocol_error(0, err); |
| 351 | return; |
| 352 | } |
| 353 | choose_populate(v, nvec, vec, 0/*!isfile*/); |
| 354 | } |
| 355 | |
| 356 | static void choose_files_completed(void *v, |
| 357 | const char *err, |
| 358 | int nvec, char **vec) { |
| 359 | if(err) { |
| 360 | popup_protocol_error(0, err); |
| 361 | return; |
| 362 | } |
| 363 | choose_populate(v, nvec, vec, 1/*isfile*/); |
| 364 | } |
| 365 | |
| 366 | void choose_play_completed(void attribute((unused)) *v, |
| 367 | const char *err) { |
| 368 | if(err) |
| 369 | popup_protocol_error(0, err); |
| 370 | } |
| 371 | |
| 372 | static void choose_state_toggled |
| 373 | (GtkCellRendererToggle attribute((unused)) *cell_renderer, |
| 374 | gchar *path_str, |
| 375 | gpointer attribute((unused)) user_data) { |
| 376 | GtkTreeIter it[1]; |
| 377 | /* Identify the track */ |
| 378 | gboolean itv = |
| 379 | gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(choose_store), |
| 380 | it, |
| 381 | path_str); |
| 382 | if(!itv) |
| 383 | return; |
| 384 | if(!choose_is_file(it)) |
| 385 | return; |
| 386 | const char *track = choose_get_track(it); |
| 387 | if(queued(track)) |
| 388 | return; |
| 389 | disorder_eclient_play(client, track, choose_play_completed, 0); |
| 390 | |
| 391 | } |
| 392 | |
| 393 | /** @brief (Re-)get the children of @p path |
| 394 | * @param path Path to target row |
| 395 | * @param iter Iterator pointing at target row |
| 396 | * |
| 397 | * Called from choose_row_expanded() to make sure that the contents are present |
| 398 | * and from choose_refill_callback() to (re-)synchronize. |
| 399 | */ |
| 400 | static void choose_refill_row(GtkTreePath *path, |
| 401 | GtkTreeIter *iter) { |
| 402 | const char *track = choose_get_track(iter); |
| 403 | disorder_eclient_files(client, choose_files_completed, |
| 404 | track, |
| 405 | NULL, |
| 406 | gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store), |
| 407 | path)); |
| 408 | disorder_eclient_dirs(client, choose_dirs_completed, |
| 409 | track, |
| 410 | NULL, |
| 411 | gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store), |
| 412 | path)); |
| 413 | /* The row references are destroyed in the _completed handlers. */ |
| 414 | choose_list_in_flight += 2; |
| 415 | } |
| 416 | |
| 417 | static void choose_row_expanded(GtkTreeView attribute((unused)) *treeview, |
| 418 | GtkTreeIter *iter, |
| 419 | GtkTreePath *path, |
| 420 | gpointer attribute((unused)) user_data) { |
| 421 | /*fprintf(stderr, "row-expanded path=[%s]\n\n", |
| 422 | gtk_tree_path_to_string(path));*/ |
| 423 | /* We update a node's contents whenever it is expanded, even if it was |
| 424 | * already populated; the effect is that contracting and expanding a node |
| 425 | * suffices to update it to the latest state on the server. */ |
| 426 | choose_refill_row(path, iter); |
| 427 | if(!choose_suppress_set_autocollapse) { |
| 428 | if(choose_auto_expanding) { |
| 429 | /* This was an automatic expansion; mark it the row for auto-collapse. */ |
| 430 | gtk_tree_store_set(choose_store, iter, |
| 431 | AUTOCOLLAPSE_COLUMN, TRUE, |
| 432 | -1); |
| 433 | /*fprintf(stderr, "enable auto-collapse for %s\n", |
| 434 | gtk_tree_path_to_string(path));*/ |
| 435 | } else { |
| 436 | /* This was a manual expansion. Inhibit automatic collapse on this row |
| 437 | * and all its ancestors. */ |
| 438 | gboolean itv; |
| 439 | do { |
| 440 | gtk_tree_store_set(choose_store, iter, |
| 441 | AUTOCOLLAPSE_COLUMN, FALSE, |
| 442 | -1); |
| 443 | /*fprintf(stderr, "suppress auto-collapse for %s\n", |
| 444 | gtk_tree_model_get_string_from_iter(GTK_TREE_MODEL(choose_store), |
| 445 | iter));*/ |
| 446 | GtkTreeIter child = *iter; |
| 447 | itv = gtk_tree_model_iter_parent(GTK_TREE_MODEL(choose_store), |
| 448 | iter, |
| 449 | &child); |
| 450 | } while(itv); |
| 451 | /* The effect of this is that if you expand a row that's actually a |
| 452 | * sibling of the real target of the auto-expansion, it stays expanded |
| 453 | * when you clear a search. That's find and good, but it _still_ stays |
| 454 | * expanded if you expand it and then collapse it. |
| 455 | * |
| 456 | * An alternative policy would be to only auto-collapse rows that don't |
| 457 | * have any expanded children (apart from ones also subject to |
| 458 | * auto-collapse). I'm not sure what the most usable policy is. |
| 459 | */ |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | static void choose_auto_collapse_callback(GtkTreeView *tree_view, |
| 465 | GtkTreePath *path, |
| 466 | gpointer attribute((unused)) user_data) { |
| 467 | GtkTreeIter it[1]; |
| 468 | |
| 469 | gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store), it, path); |
| 470 | if(choose_can_autocollapse(it)) { |
| 471 | /*fprintf(stderr, "collapse %s\n", |
| 472 | gtk_tree_path_to_string(path));*/ |
| 473 | gtk_tree_store_set(choose_store, it, |
| 474 | AUTOCOLLAPSE_COLUMN, FALSE, |
| 475 | -1); |
| 476 | gtk_tree_view_collapse_row(tree_view, path); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | /** @brief Perform automatic collapse after a search is cleared */ |
| 481 | void choose_auto_collapse(void) { |
| 482 | gtk_tree_view_map_expanded_rows(GTK_TREE_VIEW(choose_view), |
| 483 | choose_auto_collapse_callback, |
| 484 | 0); |
| 485 | } |
| 486 | |
| 487 | /** @brief Called from choose_refill() with each expanded row */ |
| 488 | static void choose_refill_callback(GtkTreeView attribute((unused)) *tree_view, |
| 489 | GtkTreePath *path, |
| 490 | gpointer attribute((unused)) user_data) { |
| 491 | GtkTreeIter it[1]; |
| 492 | |
| 493 | gtk_tree_model_get_iter(GTK_TREE_MODEL(choose_store), it, path); |
| 494 | choose_refill_row(path, it); |
| 495 | } |
| 496 | |
| 497 | /** @brief Synchronize all visible data with the server |
| 498 | * |
| 499 | * Called at startup, when a rescan completes, and via periodic_slow(). |
| 500 | */ |
| 501 | static void choose_refill(const char attribute((unused)) *event, |
| 502 | void attribute((unused)) *eventdata, |
| 503 | void attribute((unused)) *callbackdata) { |
| 504 | //fprintf(stderr, "choose_refill\n"); |
| 505 | /* Update the root */ |
| 506 | disorder_eclient_files(client, choose_files_completed, "", NULL, NULL); |
| 507 | disorder_eclient_dirs(client, choose_dirs_completed, "", NULL, NULL); |
| 508 | choose_list_in_flight += 2; |
| 509 | /* Update all expanded rows */ |
| 510 | gtk_tree_view_map_expanded_rows(GTK_TREE_VIEW(choose_view), |
| 511 | choose_refill_callback, |
| 512 | 0); |
| 513 | //fprintf(stderr, "choose_list_in_flight -> %d+\n", choose_list_in_flight); |
| 514 | } |
| 515 | |
| 516 | /** @brief Called for key-*-event on the main view |
| 517 | */ |
| 518 | static gboolean choose_key_event(GtkWidget attribute((unused)) *widget, |
| 519 | GdkEventKey *event, |
| 520 | gpointer user_data) { |
| 521 | /*fprintf(stderr, "choose_key_event type=%d state=%#x keyval=%#x\n", |
| 522 | event->type, event->state, event->keyval);*/ |
| 523 | switch(event->keyval) { |
| 524 | case GDK_Page_Up: |
| 525 | case GDK_Page_Down: |
| 526 | case GDK_Up: |
| 527 | case GDK_Down: |
| 528 | case GDK_Home: |
| 529 | case GDK_End: |
| 530 | return FALSE; /* We'll take these */ |
| 531 | case 'f': case 'F': |
| 532 | /* ^F is expected to start a search. We implement this by focusing the |
| 533 | * search entry box. */ |
| 534 | if((event->state & ~(GDK_LOCK_MASK|GDK_SHIFT_MASK)) == GDK_CONTROL_MASK |
| 535 | && event->type == GDK_KEY_PRESS) { |
| 536 | choose_search_new(); |
| 537 | return TRUE; /* Handled it */ |
| 538 | } |
| 539 | break; |
| 540 | case 'g': case 'G': |
| 541 | /* ^G is expected to go the next match. We simulate a click on the 'next' |
| 542 | * button. */ |
| 543 | if((event->state & ~(GDK_LOCK_MASK|GDK_SHIFT_MASK)) == GDK_CONTROL_MASK |
| 544 | && event->type == GDK_KEY_PRESS) { |
| 545 | choose_next_clicked(0, 0); |
| 546 | return TRUE; /* Handled it */ |
| 547 | } |
| 548 | break; |
| 549 | } |
| 550 | /* Anything not handled we redirected to the search entry field */ |
| 551 | gtk_widget_event(user_data, (GdkEvent *)event); |
| 552 | return TRUE; /* Handled it */ |
| 553 | } |
| 554 | |
| 555 | static gboolean choose_multidrag_predicate(GtkTreePath attribute((unused)) *path, |
| 556 | GtkTreeIter *iter) { |
| 557 | return choose_is_file(iter); |
| 558 | } |
| 559 | |
| 560 | |
| 561 | /** @brief Callback to add selected tracks to the selection data |
| 562 | * |
| 563 | * Called from choose_drag_data_get(). |
| 564 | */ |
| 565 | static void choose_drag_data_get_collect(GtkTreeModel attribute((unused)) *model, |
| 566 | GtkTreePath attribute((unused)) *path, |
| 567 | GtkTreeIter *iter, |
| 568 | gpointer data) { |
| 569 | struct dynstr *const result = data; |
| 570 | |
| 571 | if(choose_is_file(iter)) { /* no diretories */ |
| 572 | dynstr_append_string(result, ""); /* no ID */ |
| 573 | dynstr_append(result, '\n'); |
| 574 | dynstr_append_string(result, choose_get_track(iter)); |
| 575 | dynstr_append(result, '\n'); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | /** @brief Called to extract the dragged data from the choose view |
| 580 | * @param w Source widget (the tree view) |
| 581 | * @param dc Drag context |
| 582 | * @param data Where to put the answer |
| 583 | * @param info_ Target @c info parameter |
| 584 | * @param time_ Time data requested (for some reason not a @c time_t) |
| 585 | * @param user_data The queuelike |
| 586 | * |
| 587 | * Closely analogous to ql_drag_data_get(), and uses the same data format. |
| 588 | * IDs are sent as empty strings. |
| 589 | */ |
| 590 | static void choose_drag_data_get(GtkWidget *w, |
| 591 | GdkDragContext attribute((unused)) *dc, |
| 592 | GtkSelectionData *data, |
| 593 | guint attribute((unused)) info_, |
| 594 | guint attribute((unused)) time_, |
| 595 | gpointer attribute((unused)) user_data) { |
| 596 | struct dynstr result[1]; |
| 597 | GtkTreeSelection *sel; |
| 598 | |
| 599 | sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(w)); |
| 600 | dynstr_init(result); |
| 601 | gtk_tree_selection_selected_foreach(sel, |
| 602 | choose_drag_data_get_collect, |
| 603 | result); |
| 604 | gtk_selection_data_set(data, |
| 605 | GDK_TARGET_STRING, |
| 606 | 8, (guchar *)result->vec, result->nvec); |
| 607 | } |
| 608 | |
| 609 | /** @brief Create the choose tab */ |
| 610 | GtkWidget *choose_widget(void) { |
| 611 | /* Create the tree store. */ |
| 612 | choose_store = gtk_tree_store_new(CHOOSE_COLUMNS, |
| 613 | G_TYPE_BOOLEAN, |
| 614 | G_TYPE_STRING, |
| 615 | G_TYPE_STRING, |
| 616 | G_TYPE_BOOLEAN, |
| 617 | G_TYPE_STRING, |
| 618 | G_TYPE_STRING, |
| 619 | G_TYPE_STRING, |
| 620 | G_TYPE_STRING, |
| 621 | G_TYPE_BOOLEAN); |
| 622 | |
| 623 | /* Create the view */ |
| 624 | choose_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(choose_store)); |
| 625 | gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(choose_view), TRUE); |
| 626 | /* Suppress built-in typeahead find, we do our own search support. */ |
| 627 | gtk_tree_view_set_enable_search(GTK_TREE_VIEW(choose_view), FALSE); |
| 628 | |
| 629 | /* Create cell renderers and columns */ |
| 630 | /* TODO use a table */ |
| 631 | { |
| 632 | GtkCellRenderer *r = gtk_cell_renderer_toggle_new(); |
| 633 | GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes |
| 634 | ("Queued", |
| 635 | r, |
| 636 | "active", STATE_COLUMN, |
| 637 | "visible", ISFILE_COLUMN, |
| 638 | (char *)0); |
| 639 | gtk_tree_view_column_set_resizable(c, TRUE); |
| 640 | gtk_tree_view_column_set_reorderable(c, TRUE); |
| 641 | gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c); |
| 642 | g_signal_connect(r, "toggled", |
| 643 | G_CALLBACK(choose_state_toggled), 0); |
| 644 | } |
| 645 | { |
| 646 | GtkCellRenderer *r = gtk_cell_renderer_text_new(); |
| 647 | GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes |
| 648 | ("Length", |
| 649 | r, |
| 650 | "text", LENGTH_COLUMN, |
| 651 | (char *)0); |
| 652 | gtk_tree_view_column_set_resizable(c, TRUE); |
| 653 | gtk_tree_view_column_set_reorderable(c, TRUE); |
| 654 | g_object_set(r, "xalign", (gfloat)1.0, (char *)0); |
| 655 | gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c); |
| 656 | } |
| 657 | { |
| 658 | GtkCellRenderer *r = gtk_cell_renderer_text_new(); |
| 659 | GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes |
| 660 | ("Track", |
| 661 | r, |
| 662 | "text", NAME_COLUMN, |
| 663 | "background", BG_COLUMN, |
| 664 | "foreground", FG_COLUMN, |
| 665 | (char *)0); |
| 666 | gtk_tree_view_column_set_resizable(c, TRUE); |
| 667 | gtk_tree_view_column_set_reorderable(c, TRUE); |
| 668 | g_object_set(c, "expand", TRUE, (char *)0); |
| 669 | gtk_tree_view_append_column(GTK_TREE_VIEW(choose_view), c); |
| 670 | gtk_tree_view_set_expander_column(GTK_TREE_VIEW(choose_view), c); |
| 671 | } |
| 672 | |
| 673 | /* The selection should support multiple things being selected */ |
| 674 | choose_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(choose_view)); |
| 675 | gtk_tree_selection_set_mode(choose_selection, GTK_SELECTION_MULTIPLE); |
| 676 | |
| 677 | /* Catch button presses */ |
| 678 | g_signal_connect(choose_view, "button-press-event", |
| 679 | G_CALLBACK(choose_button_event), 0); |
| 680 | g_signal_connect(choose_view, "button-release-event", |
| 681 | G_CALLBACK(choose_button_event), 0); |
| 682 | /* Catch row expansions so we can fill in placeholders */ |
| 683 | g_signal_connect(choose_view, "row-expanded", |
| 684 | G_CALLBACK(choose_row_expanded), 0); |
| 685 | |
| 686 | event_register("queue-list-changed", choose_set_state, 0); |
| 687 | event_register("playing-track-changed", choose_set_state, 0); |
| 688 | event_register("search-results-changed", choose_set_state, 0); |
| 689 | event_register("lookups-completed", choose_set_state, 0); |
| 690 | event_register("choose-more-tracks", choose_menu_moretracks, 0); |
| 691 | |
| 692 | /* After a rescan we update the choose tree. We get a rescan-complete |
| 693 | * automatically at startup and upon connection too. */ |
| 694 | event_register("rescan-complete", choose_refill, 0); |
| 695 | |
| 696 | /* Make the widget scrollable */ |
| 697 | GtkWidget *scrolled = scroll_widget(choose_view); |
| 698 | |
| 699 | /* Pack vertically with the search widget */ |
| 700 | GtkWidget *vbox = gtk_vbox_new(FALSE/*homogenous*/, 1/*spacing*/); |
| 701 | gtk_box_pack_start(GTK_BOX(vbox), scrolled, |
| 702 | TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/); |
| 703 | gtk_box_pack_end(GTK_BOX(vbox), choose_search_widget(), |
| 704 | FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/); |
| 705 | |
| 706 | g_object_set_data(G_OBJECT(vbox), "type", (void *)&choose_tabtype); |
| 707 | |
| 708 | /* Redirect keyboard activity to the search widget */ |
| 709 | g_signal_connect(choose_view, "key-press-event", |
| 710 | G_CALLBACK(choose_key_event), choose_search_entry); |
| 711 | g_signal_connect(choose_view, "key-release-event", |
| 712 | G_CALLBACK(choose_key_event), choose_search_entry); |
| 713 | |
| 714 | /* Enable dragging of tracks out */ |
| 715 | gtk_drag_source_set(choose_view, |
| 716 | GDK_BUTTON1_MASK, |
| 717 | choose_targets, |
| 718 | 1, |
| 719 | GDK_ACTION_COPY); |
| 720 | g_signal_connect(choose_view, "drag-data-get", |
| 721 | G_CALLBACK(choose_drag_data_get), NULL); |
| 722 | make_treeview_multidrag(choose_view, |
| 723 | choose_multidrag_predicate); |
| 724 | |
| 725 | return vbox; |
| 726 | } |
| 727 | |
| 728 | /* |
| 729 | Local Variables: |
| 730 | c-basic-offset:2 |
| 731 | comment-column:40 |
| 732 | fill-column:79 |
| 733 | indent-tabs-mode:nil |
| 734 | End: |
| 735 | */ |