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