| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2006-2009 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/queue-generic.c |
| 19 | * @brief Disobedience queue widgets |
| 20 | * |
| 21 | * This file provides contains code shared between all the queue-like |
| 22 | * widgets - the queue, the recent list and the added tracks list. |
| 23 | * |
| 24 | * This code is in the process of being rewritten to use the native list |
| 25 | * widget. |
| 26 | * |
| 27 | * There are three @ref queuelike objects: @ref ql_queue, @ref |
| 28 | * ql_recent and @ref ql_added. Each has an associated queue linked |
| 29 | * list and a list store containing the contents. |
| 30 | * |
| 31 | * When new contents turn up we rearrange the list store accordingly. |
| 32 | * |
| 33 | * NB that while in the server the playing track is not in the queue, in |
| 34 | * Disobedience, the playing does live in @c ql_queue.q, despite its different |
| 35 | * status to everything else found in that list. |
| 36 | * |
| 37 | * To do: |
| 38 | * - display playing row in a different color? |
| 39 | */ |
| 40 | #include "disobedience.h" |
| 41 | #include "popup.h" |
| 42 | #include "queue-generic.h" |
| 43 | #include "multidrag.h" |
| 44 | #include "autoscroll.h" |
| 45 | |
| 46 | /* Track detail lookup ----------------------------------------------------- */ |
| 47 | |
| 48 | static void queue_lookups_completed(const char attribute((unused)) *event, |
| 49 | void attribute((unused)) *eventdata, |
| 50 | void *callbackdata) { |
| 51 | struct queuelike *ql = callbackdata; |
| 52 | ql_update_list_store(ql); |
| 53 | } |
| 54 | |
| 55 | /* Column formatting -------------------------------------------------------- */ |
| 56 | |
| 57 | /** @brief Format the 'when' column */ |
| 58 | const char *column_when(const struct queue_entry *q, |
| 59 | const char attribute((unused)) *data) { |
| 60 | char when[64]; |
| 61 | struct tm tm; |
| 62 | time_t t; |
| 63 | |
| 64 | D(("column_when")); |
| 65 | switch(q->state) { |
| 66 | case playing_isscratch: |
| 67 | case playing_unplayed: |
| 68 | case playing_random: |
| 69 | t = q->expected; |
| 70 | break; |
| 71 | case playing_failed: |
| 72 | case playing_no_player: |
| 73 | case playing_ok: |
| 74 | case playing_scratched: |
| 75 | case playing_started: |
| 76 | case playing_paused: |
| 77 | case playing_quitting: |
| 78 | t = q->played; |
| 79 | break; |
| 80 | default: |
| 81 | t = 0; |
| 82 | break; |
| 83 | } |
| 84 | if(t) |
| 85 | strftime(when, sizeof when, "%H:%M", localtime_r(&t, &tm)); |
| 86 | else |
| 87 | when[0] = 0; |
| 88 | return xstrdup(when); |
| 89 | } |
| 90 | |
| 91 | /** @brief Format the 'who' column */ |
| 92 | const char *column_who(const struct queue_entry *q, |
| 93 | const char attribute((unused)) *data) { |
| 94 | D(("column_who")); |
| 95 | return q->submitter ? q->submitter : ""; |
| 96 | } |
| 97 | |
| 98 | /** @brief Format one of the track name columns */ |
| 99 | const char *column_namepart(const struct queue_entry *q, |
| 100 | const char *data) { |
| 101 | D(("column_namepart")); |
| 102 | return namepart(q->track, "display", data); |
| 103 | } |
| 104 | |
| 105 | /** @brief Format the length column */ |
| 106 | const char *column_length(const struct queue_entry *q, |
| 107 | const char attribute((unused)) *data) { |
| 108 | D(("column_length")); |
| 109 | long l; |
| 110 | time_t now; |
| 111 | char *played = 0, *length = 0; |
| 112 | |
| 113 | /* Work out what to say for the length */ |
| 114 | l = namepart_length(q->track); |
| 115 | if(l > 0) |
| 116 | byte_xasprintf(&length, "%ld:%02ld", l / 60, l % 60); |
| 117 | else |
| 118 | byte_xasprintf(&length, "?:??"); |
| 119 | /* For the currently playing track we want to report how much of the track |
| 120 | * has been played */ |
| 121 | if(q == playing_track) { |
| 122 | /* log_state() arranges that we re-get the playing data whenever the |
| 123 | * pause/resume state changes */ |
| 124 | if(last_state & DISORDER_TRACK_PAUSED) |
| 125 | l = playing_track->sofar; |
| 126 | else { |
| 127 | if(!last_playing) |
| 128 | return NULL; |
| 129 | xtime(&now); |
| 130 | l = playing_track->sofar + (now - last_playing); |
| 131 | } |
| 132 | byte_xasprintf(&played, "%ld:%02ld/%s", l / 60, l % 60, length); |
| 133 | return played; |
| 134 | } else |
| 135 | return length; |
| 136 | } |
| 137 | |
| 138 | /* List store maintenance -------------------------------------------------- */ |
| 139 | |
| 140 | /** @brief Return the @ref queue_entry corresponding to @p iter |
| 141 | * @param model Model that owns @p iter |
| 142 | * @param iter Tree iterator |
| 143 | * @return Pointer to queue entry |
| 144 | */ |
| 145 | struct queue_entry *ql_iter_to_q(GtkTreeModel *model, |
| 146 | GtkTreeIter *iter) { |
| 147 | struct queuelike *ql = g_object_get_data(G_OBJECT(model), "ql"); |
| 148 | GValue v[1]; |
| 149 | memset(v, 0, sizeof v); |
| 150 | gtk_tree_model_get_value(model, iter, ql->ncolumns + QUEUEPOINTER_COLUMN, v); |
| 151 | assert(G_VALUE_TYPE(v) == G_TYPE_POINTER); |
| 152 | struct queue_entry *const q = g_value_get_pointer(v); |
| 153 | g_value_unset(v); |
| 154 | return q; |
| 155 | } |
| 156 | |
| 157 | /** @brief Return the @ref queue_entry corresponding to @p path |
| 158 | * @param model Model to query |
| 159 | * @param path Path into tree |
| 160 | * @return Pointer to queue entry or NULL |
| 161 | */ |
| 162 | struct queue_entry *ql_path_to_q(GtkTreeModel *model, |
| 163 | GtkTreePath *path) { |
| 164 | GtkTreeIter iter[1]; |
| 165 | if(!gtk_tree_model_get_iter(model, iter, path)) |
| 166 | return NULL; |
| 167 | return ql_iter_to_q(model, iter); |
| 168 | } |
| 169 | |
| 170 | /** @brief Update one row of a list store |
| 171 | * @param q Queue entry |
| 172 | * @param iter Iterator referring to row or NULL to work it out |
| 173 | */ |
| 174 | void ql_update_row(struct queue_entry *q, |
| 175 | GtkTreeIter *iter) { |
| 176 | const struct queuelike *const ql = q->ql; |
| 177 | |
| 178 | D(("ql_update_row")); |
| 179 | /* If no iter was supplied, work it out */ |
| 180 | GtkTreeIter my_iter[1]; |
| 181 | if(!iter) { |
| 182 | gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), my_iter); |
| 183 | struct queue_entry *qq; |
| 184 | for(qq = ql->q; qq && q != qq; qq = qq->next) |
| 185 | gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), my_iter); |
| 186 | if(!qq) |
| 187 | return; |
| 188 | iter = my_iter; |
| 189 | } |
| 190 | /* Update all the columns */ |
| 191 | for(int col = 0; col < ql->ncolumns; ++col) { |
| 192 | const char *const v = ql->columns[col].value(q, |
| 193 | ql->columns[col].data); |
| 194 | if(v) |
| 195 | gtk_list_store_set(ql->store, iter, |
| 196 | col, v, |
| 197 | -1); |
| 198 | } |
| 199 | gtk_list_store_set(ql->store, iter, |
| 200 | ql->ncolumns + QUEUEPOINTER_COLUMN, q, |
| 201 | -1); |
| 202 | if(q == playing_track) |
| 203 | gtk_list_store_set(ql->store, iter, |
| 204 | ql->ncolumns + BACKGROUND_COLUMN, BG_PLAYING, |
| 205 | ql->ncolumns + FOREGROUND_COLUMN, FG_PLAYING, |
| 206 | -1); |
| 207 | else |
| 208 | gtk_list_store_set(ql->store, iter, |
| 209 | ql->ncolumns + BACKGROUND_COLUMN, (char *)0, |
| 210 | ql->ncolumns + FOREGROUND_COLUMN, (char *)0, |
| 211 | -1); |
| 212 | } |
| 213 | |
| 214 | /** @brief Update the list store |
| 215 | * @param ql Queuelike to update |
| 216 | * |
| 217 | * Called when new namepart data is available (and initially). Doesn't change |
| 218 | * the rows, just updates the cell values. |
| 219 | */ |
| 220 | void ql_update_list_store(struct queuelike *ql) { |
| 221 | D(("ql_update_list_store")); |
| 222 | GtkTreeIter iter[1]; |
| 223 | gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), iter); |
| 224 | for(struct queue_entry *q = ql->q; q; q = q->next) { |
| 225 | ql_update_row(q, iter); |
| 226 | gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | struct newqueue_data { |
| 231 | struct queue_entry *old, *new; |
| 232 | }; |
| 233 | |
| 234 | static void record_queue_map(hash *h, |
| 235 | const char *id, |
| 236 | struct queue_entry *old, |
| 237 | struct queue_entry *new) { |
| 238 | struct newqueue_data *nqd; |
| 239 | |
| 240 | if(!(nqd = hash_find(h, id))) { |
| 241 | static const struct newqueue_data empty[1]; |
| 242 | hash_add(h, id, empty, HASH_INSERT); |
| 243 | nqd = hash_find(h, id); |
| 244 | } |
| 245 | if(old) { |
| 246 | #if DEBUG_QUEUE |
| 247 | fprintf(stderr, " old: %s\n", id); |
| 248 | #endif |
| 249 | nqd->old = old; |
| 250 | } |
| 251 | if(new) { |
| 252 | #if DEBUG_QUEUE |
| 253 | fprintf(stderr, " new: %s\n", id); |
| 254 | #endif |
| 255 | nqd->new = new; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | #if DEBUG_QUEUE |
| 260 | static void dump_queue(struct queue_entry *head, struct queue_entry *mark) { |
| 261 | for(struct queue_entry *q = head; q; q = q->next) { |
| 262 | if(q == mark) |
| 263 | fprintf(stderr, " !"); |
| 264 | fprintf(stderr, " %s\n", q->id); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | static void dump_rows(struct queuelike *ql) { |
| 269 | GtkTreeIter iter[1]; |
| 270 | gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), |
| 271 | iter); |
| 272 | while(it) { |
| 273 | struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter); |
| 274 | it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter); |
| 275 | fprintf(stderr, " %s\n", q->id); |
| 276 | } |
| 277 | } |
| 278 | #endif |
| 279 | |
| 280 | /** @brief Reset the list store |
| 281 | * @param ql Queuelike to reset |
| 282 | * @param newq New queue contents/ordering |
| 283 | * |
| 284 | * Updates the queue to match @p newq |
| 285 | */ |
| 286 | void ql_new_queue(struct queuelike *ql, |
| 287 | struct queue_entry *newq) { |
| 288 | D(("ql_new_queue")); |
| 289 | ++suppress_actions; |
| 290 | |
| 291 | /* Tell every queue entry which queue owns it */ |
| 292 | #if DEBUG_QUEUE |
| 293 | fprintf(stderr, "%s: filling in q->ql\n", ql->name); |
| 294 | #endif |
| 295 | for(struct queue_entry *q = newq; q; q = q->next) |
| 296 | q->ql = ql; |
| 297 | |
| 298 | #if DEBUG_QUEUE |
| 299 | fprintf(stderr, "%s: constructing h\n", ql->name); |
| 300 | #endif |
| 301 | /* Construct map from id to new and old structures */ |
| 302 | hash *h = hash_new(sizeof(struct newqueue_data)); |
| 303 | for(struct queue_entry *q = ql->q; q; q = q->next) |
| 304 | record_queue_map(h, q->id, q, NULL); |
| 305 | for(struct queue_entry *q = newq; q; q = q->next) |
| 306 | record_queue_map(h, q->id, NULL, q); |
| 307 | |
| 308 | /* The easy bit: delete rows not present any more. In the same pass we |
| 309 | * update the secret column containing the queue_entry pointer. */ |
| 310 | #if DEBUG_QUEUE |
| 311 | fprintf(stderr, "%s: deleting rows...\n", ql->name); |
| 312 | #endif |
| 313 | GtkTreeIter iter[1]; |
| 314 | gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), |
| 315 | iter); |
| 316 | int inserted = 0, deleted = 0, kept = 0; |
| 317 | while(it) { |
| 318 | struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter); |
| 319 | const struct newqueue_data *nqd = hash_find(h, q->id); |
| 320 | if(nqd->new) { |
| 321 | /* Tell this row that it belongs to the new version of the queue */ |
| 322 | gtk_list_store_set(ql->store, iter, |
| 323 | ql->ncolumns + QUEUEPOINTER_COLUMN, nqd->new, |
| 324 | -1); |
| 325 | it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter); |
| 326 | /* We'll need the new start time */ |
| 327 | nqd->new->when = q->when; |
| 328 | ++kept; |
| 329 | } else { |
| 330 | /* Delete this row (and move iter to the next one) */ |
| 331 | #if DEBUG_QUEUE |
| 332 | fprintf(stderr, " delete %s\n", q->id); |
| 333 | #endif |
| 334 | it = gtk_list_store_remove(ql->store, iter); |
| 335 | ++deleted; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /* Now every row's secret column is right, but we might be missing new rows |
| 340 | * and they might be in the wrong order */ |
| 341 | |
| 342 | /* We're going to have to support arbitrary rearrangements, so we might as |
| 343 | * well add new elements at the end. */ |
| 344 | #if DEBUG_QUEUE |
| 345 | fprintf(stderr, "%s: adding rows...\n", ql->name); |
| 346 | #endif |
| 347 | struct queue_entry *after = 0; |
| 348 | for(struct queue_entry *q = newq; q; q = q->next) { |
| 349 | const struct newqueue_data *nqd = hash_find(h, q->id); |
| 350 | if(!nqd->old) { |
| 351 | if(after) { |
| 352 | /* Try to insert at the right sort of place */ |
| 353 | GtkTreeIter where[1]; |
| 354 | gboolean wit = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), |
| 355 | where); |
| 356 | while(wit && ql_iter_to_q(GTK_TREE_MODEL(ql->store), where) != after) |
| 357 | wit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), where); |
| 358 | if(wit) |
| 359 | gtk_list_store_insert_after(ql->store, iter, where); |
| 360 | else |
| 361 | gtk_list_store_append(ql->store, iter); |
| 362 | } else |
| 363 | gtk_list_store_prepend(ql->store, iter); |
| 364 | gtk_list_store_set(ql->store, iter, |
| 365 | ql->ncolumns + QUEUEPOINTER_COLUMN, q, |
| 366 | -1); |
| 367 | #if DEBUG_QUEUE |
| 368 | fprintf(stderr, " add %s\n", q->id); |
| 369 | #endif |
| 370 | ++inserted; |
| 371 | } |
| 372 | after = newq; |
| 373 | } |
| 374 | |
| 375 | /* Now exactly the right set of rows are present, and they have the right |
| 376 | * queue_entry pointers in their secret column, but they may be in the wrong |
| 377 | * order. |
| 378 | * |
| 379 | * The current code is simple but amounts to a bubble-sort - we might easily |
| 380 | * called gtk_tree_model_iter_next a couple of thousand times. |
| 381 | */ |
| 382 | #if DEBUG_QUEUE |
| 383 | fprintf(stderr, "%s: rearranging rows\n", ql->name); |
| 384 | fprintf(stderr, "%s: target state:\n", ql->name); |
| 385 | dump_queue(newq, 0); |
| 386 | fprintf(stderr, "%s: current state:\n", ql->name); |
| 387 | dump_rows(ql); |
| 388 | #endif |
| 389 | it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), iter); |
| 390 | struct queue_entry *tq = newq; /* t-for-target */ |
| 391 | int swaps = 0, searches = 0; |
| 392 | int row = 0; |
| 393 | while(it) { |
| 394 | struct queue_entry *cq = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter); |
| 395 | /* c-for-current */ |
| 396 | |
| 397 | /* Everything has the right queue pointer (see above) so it's sufficient to |
| 398 | * compare pointers to detect mismatches */ |
| 399 | if(cq != tq) { |
| 400 | #if DEBUG_QUEUE |
| 401 | fprintf(stderr, " pointer mismatch at row %d\n", row); |
| 402 | fprintf(stderr, " target id %s\n", tq->id); |
| 403 | fprintf(stderr, " actual id %s\n", cq->id); |
| 404 | #endif |
| 405 | /* Start looking for the target row fromn the next row */ |
| 406 | GtkTreeIter next[1] = { *iter }; |
| 407 | gboolean nit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), next); |
| 408 | while(nit) { |
| 409 | struct queue_entry *nq = ql_iter_to_q(GTK_TREE_MODEL(ql->store), next); |
| 410 | #if DEBUG_QUEUE |
| 411 | fprintf(stderr, " candidate: %s\n", nq->id); |
| 412 | #endif |
| 413 | if(nq == tq) |
| 414 | break; |
| 415 | nit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), next); |
| 416 | ++searches; |
| 417 | } |
| 418 | /* Note that this assertion will fail in the face of duplicate IDs. |
| 419 | * q->id really does need to be unique. */ |
| 420 | assert(nit); |
| 421 | gtk_list_store_swap(ql->store, iter, next); |
| 422 | *iter = *next; |
| 423 | #if DEBUG_QUEUE |
| 424 | fprintf(stderr, "%s: found it. new row state:\n", ql->name); |
| 425 | dump_rows(ql); |
| 426 | #endif |
| 427 | ++swaps; |
| 428 | } |
| 429 | /* ...and onto the next one */ |
| 430 | it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter); |
| 431 | tq = tq->next; |
| 432 | ++row; |
| 433 | } |
| 434 | #if DEBUG_QUEUE |
| 435 | fprintf(stderr, "%6s: %3d kept %3d inserted %3d deleted %3d swaps %4d searches\n", ql->name, |
| 436 | kept, inserted, deleted, swaps, searches); |
| 437 | fprintf(stderr, "done\n"); |
| 438 | #endif |
| 439 | ql->q = newq; |
| 440 | /* Set the rest of the columns in new rows */ |
| 441 | ql_update_list_store(ql); |
| 442 | --suppress_actions; |
| 443 | } |
| 444 | |
| 445 | /* Drag and drop ------------------------------------------------------------ */ |
| 446 | |
| 447 | /** @brief Identify the drop path |
| 448 | * @param w Destination tree view widget |
| 449 | * @param model Underlying tree model |
| 450 | * @param wx X coordinate |
| 451 | * @param wy Y coordinate |
| 452 | * @param posp Where to store relative position |
| 453 | * @return Target path or NULL |
| 454 | * |
| 455 | * This is used by ql_drag_motion() and ql_drag_data_received() to identify a |
| 456 | * drop would or does land. It's important that they use the same code since |
| 457 | * otherwise the visual feedback can be inconsistent with the actual effect! |
| 458 | * |
| 459 | * Remember to free the returned path. |
| 460 | */ |
| 461 | static GtkTreePath *ql_drop_path(GtkWidget *w, |
| 462 | GtkTreeModel *model, |
| 463 | int wx, int wy, |
| 464 | GtkTreeViewDropPosition *posp) { |
| 465 | GtkTreePath *path = NULL; |
| 466 | GtkTreeViewDropPosition pos; |
| 467 | GtkTreeIter iter[1], last[1]; |
| 468 | int tx, ty; |
| 469 | |
| 470 | gtk_tree_view_convert_widget_to_tree_coords(GTK_TREE_VIEW(w), |
| 471 | wx, wy, &tx, &ty); |
| 472 | if(gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(w), |
| 473 | wx, wy, |
| 474 | &path, |
| 475 | &pos)) { |
| 476 | //fprintf(stderr, "gtk_tree_view_get_dest_row_at_pos() -> TRUE\n"); |
| 477 | // Normalize drop position |
| 478 | switch(pos) { |
| 479 | case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE: |
| 480 | pos = GTK_TREE_VIEW_DROP_BEFORE; |
| 481 | break; |
| 482 | case GTK_TREE_VIEW_DROP_INTO_OR_AFTER: |
| 483 | pos = GTK_TREE_VIEW_DROP_AFTER; |
| 484 | break; |
| 485 | default: break; |
| 486 | } |
| 487 | } else if(gtk_tree_model_get_iter_first(model, iter)) { |
| 488 | /* If the pointer isn't over any particular row then either it's below |
| 489 | * the last row, in which case we want the dropzone to be below that row; |
| 490 | * or it's above the first row (in the column headings) in which case we |
| 491 | * want the dropzone to be above that row. */ |
| 492 | if(ty >= 0) { |
| 493 | /* Find the last row */ |
| 494 | do { |
| 495 | *last = *iter; |
| 496 | } while(gtk_tree_model_iter_next(model, iter)); |
| 497 | /* The drop target is just after it */ |
| 498 | pos = GTK_TREE_VIEW_DROP_AFTER; |
| 499 | *iter = *last; |
| 500 | } else { |
| 501 | /* The drop target will be just before the first row */ |
| 502 | pos = GTK_TREE_VIEW_DROP_BEFORE; |
| 503 | } |
| 504 | path = gtk_tree_model_get_path(model, iter); |
| 505 | } |
| 506 | *posp = pos; |
| 507 | return path; |
| 508 | } |
| 509 | |
| 510 | #if 0 |
| 511 | static const char *act(GdkDragAction action) { |
| 512 | struct dynstr d[1]; |
| 513 | |
| 514 | dynstr_init(d); |
| 515 | if(action & GDK_ACTION_DEFAULT) |
| 516 | dynstr_append_string(d, "|DEFAULT"); |
| 517 | if(action & GDK_ACTION_COPY) |
| 518 | dynstr_append_string(d, "|COPY"); |
| 519 | if(action & GDK_ACTION_MOVE) |
| 520 | dynstr_append_string(d, "|MOVE"); |
| 521 | if(action & GDK_ACTION_LINK) |
| 522 | dynstr_append_string(d, "|LINK"); |
| 523 | if(action & GDK_ACTION_PRIVATE) |
| 524 | dynstr_append_string(d, "|PRIVATE"); |
| 525 | if(action & GDK_ACTION_ASK) |
| 526 | dynstr_append_string(d, "|ASK"); |
| 527 | dynstr_terminate(d); |
| 528 | return d->nvec ? d->vec + 1 : ""; |
| 529 | } |
| 530 | #endif |
| 531 | |
| 532 | /** @brief Called when a drag moves within a candidate destination |
| 533 | * @param w Destination widget |
| 534 | * @param dc Drag context |
| 535 | * @param x Current pointer location |
| 536 | * @param y Current pointer location |
| 537 | * @param time_ Current time |
| 538 | * @param user_data Pointer to queuelike |
| 539 | * @return TRUE in a dropzone, otherwise FALSE |
| 540 | * |
| 541 | * This is the handler for the "drag-motion" signal. |
| 542 | */ |
| 543 | static gboolean ql_drag_motion(GtkWidget *w, |
| 544 | GdkDragContext *dc, |
| 545 | gint x, |
| 546 | gint y, |
| 547 | guint time_, |
| 548 | gpointer user_data) { |
| 549 | struct queuelike *const ql = user_data; |
| 550 | GdkDragAction action = 0; |
| 551 | |
| 552 | // GTK_DEST_DEFAULT_MOTION vets actions as follows: |
| 553 | // 1) if dc->suggested_action is in the gtk_drag_dest_set actions |
| 554 | // then dc->suggested_action is taken as the action. |
| 555 | // 2) otherwise if dc->actions intersects the gtk_drag_dest_set actions |
| 556 | // then the lowest-numbered member of the intersection is chosen. |
| 557 | // 3) otherwise no member is chosen and gdk_drag_status() is called |
| 558 | // with action=0 to refuse the drop. |
| 559 | if(dc->suggested_action) { |
| 560 | if(dc->suggested_action & (GDK_ACTION_MOVE|GDK_ACTION_COPY)) |
| 561 | action = dc->suggested_action; |
| 562 | } else if(dc->actions & GDK_ACTION_MOVE) |
| 563 | action = GDK_ACTION_MOVE; |
| 564 | else if(dc->actions & GDK_ACTION_COPY) |
| 565 | action = GDK_ACTION_COPY; |
| 566 | /* TODO this comes up with the wrong answer sometimes. If we are in the |
| 567 | * middle of a rearrange then the suggested action will be COPY, which we'll |
| 568 | * take, even though MOVE would actually be appropriate. The drag still |
| 569 | * seems to work, but it _is_ wrong. The answer is to take the target into |
| 570 | * account. */ |
| 571 | /*fprintf(stderr, "suggested %s actions %s result %s\n", |
| 572 | act(dc->suggested_action), act(dc->actions), act(action));*/ |
| 573 | if(action) { |
| 574 | // If the action is acceptable then we see if this widget is acceptable |
| 575 | if(gtk_drag_dest_find_target(w, dc, NULL) == GDK_NONE) |
| 576 | action = 0; |
| 577 | } |
| 578 | // Report the status |
| 579 | //fprintf(stderr, "drag action: %u\n", action); |
| 580 | gdk_drag_status(dc, action, time_); |
| 581 | if(action) { |
| 582 | GtkTreeViewDropPosition pos; |
| 583 | |
| 584 | // Find the drop target |
| 585 | GtkTreePath *path = ql_drop_path(w, GTK_TREE_MODEL(ql->store), x, y, &pos); |
| 586 | // Highlight drop target |
| 587 | gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW(w), path, pos); |
| 588 | if(path) |
| 589 | gtk_tree_path_free(path); |
| 590 | } |
| 591 | autoscroll_add(GTK_TREE_VIEW(w)); |
| 592 | return TRUE; /* We are (always) in a drop zone */ |
| 593 | } |
| 594 | |
| 595 | /** @brief Called when a drag moves leaves a candidate destination |
| 596 | * @param w Destination widget |
| 597 | * @param dc Drag context |
| 598 | * @param time_ Current time |
| 599 | * @param user_data Pointer to queuelike |
| 600 | * |
| 601 | * This is the handler for the "drag-leave" signal. |
| 602 | * |
| 603 | * It turns out that we get a drag-leave event when the data is dropped, too |
| 604 | * (See _gtk_drag_dest_handle_event). This seems logically consistent and is |
| 605 | * convenient too - for instance it's why autoscroll_remove() gets called at |
| 606 | * the end of a drag+drop sequence. |
| 607 | */ |
| 608 | static void ql_drag_leave(GtkWidget *w, |
| 609 | GdkDragContext attribute((unused)) *dc, |
| 610 | guint attribute((unused)) time_, |
| 611 | gpointer attribute((unused)) user_data) { |
| 612 | //struct queuelike *const ql = user_data; |
| 613 | |
| 614 | gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW(w), NULL, 0); |
| 615 | autoscroll_remove(GTK_TREE_VIEW(w)); |
| 616 | } |
| 617 | |
| 618 | /** @brief Callback to add selected tracks to the selection data |
| 619 | * |
| 620 | * Called from ql_drag_data_get(). |
| 621 | */ |
| 622 | static void ql_drag_data_get_collect(GtkTreeModel *model, |
| 623 | GtkTreePath attribute((unused)) *path, |
| 624 | GtkTreeIter *iter, |
| 625 | gpointer data) { |
| 626 | struct dynstr *const result = data; |
| 627 | struct queue_entry *const q = ql_iter_to_q(model, iter); |
| 628 | |
| 629 | dynstr_append_string(result, q->id); |
| 630 | dynstr_append(result, '\n'); |
| 631 | dynstr_append_string(result, q->track); |
| 632 | dynstr_append(result, '\n'); |
| 633 | } |
| 634 | |
| 635 | /** @brief Called to extract the dragged data from the source queuelike |
| 636 | * @param w Source widget (the tree view) |
| 637 | * @param dc Drag context |
| 638 | * @param data Where to put the answer |
| 639 | * @param info_ Target @c info parameter |
| 640 | * @param time_ Time data requested (for some reason not a @c time_t) |
| 641 | * @param user_data The queuelike |
| 642 | * |
| 643 | * The list of tracks is converted into a single string, consisting of IDs |
| 644 | * and track names. Each is terminated by a newline. Including both ID and |
| 645 | * track name means that the receiver can use whichever happens to be more |
| 646 | * convenient. |
| 647 | * |
| 648 | * If there are no IDs for rows in this widget then the ID half is undefined. |
| 649 | * |
| 650 | * This is the handler for the "drag-data-get" signal. |
| 651 | */ |
| 652 | static void ql_drag_data_get(GtkWidget attribute((unused)) *w, |
| 653 | GdkDragContext attribute((unused)) *dc, |
| 654 | GtkSelectionData *data, |
| 655 | guint attribute((unused)) info, |
| 656 | guint attribute((unused)) time_, |
| 657 | gpointer user_data) { |
| 658 | struct queuelike *const ql = user_data; |
| 659 | struct dynstr result[1]; |
| 660 | |
| 661 | //fprintf(stderr, "ql_drag_data_get %s info=%d\n", ql->name, info); |
| 662 | dynstr_init(result); |
| 663 | gtk_tree_selection_selected_foreach(ql->selection, |
| 664 | ql_drag_data_get_collect, |
| 665 | result); |
| 666 | // TODO must not be able to drag playing track! |
| 667 | //fprintf(stderr, "drag-data-get: %.*s\n", |
| 668 | // result->nvec, result->vec); |
| 669 | /* gtk_selection_data_set_text() insists that data->target is one of a |
| 670 | * variety of stringy atoms. TODO: where does this value actually come |
| 671 | * from? */ |
| 672 | gtk_selection_data_set(data, |
| 673 | GDK_TARGET_STRING, |
| 674 | 8, (guchar *)result->vec, result->nvec); |
| 675 | } |
| 676 | |
| 677 | /** @brief Called when drag data is received |
| 678 | * @param w Target widget (the tree view) |
| 679 | * @param dc Drag context |
| 680 | * @param x The drop location |
| 681 | * @param y The drop location |
| 682 | * @param data The selection data |
| 683 | * @param info_ The target type that was chosen |
| 684 | * @param time_ Time data received (for some reason not a @c time_t) |
| 685 | * @param user_data The queuelike |
| 686 | * |
| 687 | * This is the handler for the "drag-data-received" signal. |
| 688 | */ |
| 689 | static void ql_drag_data_received(GtkWidget attribute((unused)) *w, |
| 690 | GdkDragContext attribute((unused)) *dc, |
| 691 | gint x, |
| 692 | gint y, |
| 693 | GtkSelectionData *data, |
| 694 | guint info_, |
| 695 | guint attribute((unused)) time_, |
| 696 | gpointer user_data) { |
| 697 | struct queuelike *const ql = user_data; |
| 698 | char *result, *p; |
| 699 | struct vector ids[1], tracks[1]; |
| 700 | int parity = 0; |
| 701 | |
| 702 | //fprintf(stderr, "drag-data-received: %d,%d info=%u\n", x, y, info_); |
| 703 | /* Get the selection string */ |
| 704 | p = result = (char *)gtk_selection_data_get_text(data); |
| 705 | if(!result) { |
| 706 | //fprintf(stderr, "gtk_selection_data_get_text() returned NULL\n"); |
| 707 | return; |
| 708 | } |
| 709 | //fprintf(stderr, "%s--\n", result); |
| 710 | /* Parse it back into IDs and track names */ |
| 711 | vector_init(ids); |
| 712 | vector_init(tracks); |
| 713 | while(*p) { |
| 714 | char *nl = strchr(p, '\n'); |
| 715 | if(!nl) |
| 716 | break; |
| 717 | *nl = 0; |
| 718 | //fprintf(stderr, " %s\n", p); |
| 719 | vector_append(parity++ & 1 ? tracks : ids, xstrdup(p)); |
| 720 | p = nl + 1; |
| 721 | } |
| 722 | g_free(result); |
| 723 | if(ids->nvec != tracks->nvec) { |
| 724 | //fprintf(stderr, " inconsistent drag data!\n"); |
| 725 | return; |
| 726 | } |
| 727 | vector_terminate(ids); |
| 728 | vector_terminate(tracks); |
| 729 | /* Figure out which row the drop precedes (if any) */ |
| 730 | GtkTreeViewDropPosition pos; |
| 731 | struct queue_entry *q; |
| 732 | GtkTreePath *path = ql_drop_path(w, GTK_TREE_MODEL(ql->store), x, y, &pos); |
| 733 | if(path) { |
| 734 | q = ql_path_to_q(GTK_TREE_MODEL(ql->store), path); |
| 735 | //fprintf(stderr, " drop path: %s q=%p pos=%d\n", |
| 736 | // gtk_tree_path_to_string(path), q, pos); |
| 737 | } else { |
| 738 | /* This generally means a drop past the end of the queue. We find the last |
| 739 | * element in the queue and ask to move after that. */ |
| 740 | for(q = ql->q; q && q->next; q = q->next) |
| 741 | ; |
| 742 | //fprintf(stderr, " after end. q=%p. pos=%d\n", q, pos); |
| 743 | } |
| 744 | switch(pos) { |
| 745 | case GTK_TREE_VIEW_DROP_BEFORE: |
| 746 | case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE: |
| 747 | if(q) { |
| 748 | q = q->prev; |
| 749 | //fprintf(stderr, " but we like to drop near %s\n", |
| 750 | // q ? q->id : "NULL"); |
| 751 | } |
| 752 | break; |
| 753 | default: |
| 754 | break; |
| 755 | } |
| 756 | /* Guarantee we never drop an empty list */ |
| 757 | if(!tracks->nvec) |
| 758 | return; |
| 759 | /* Note that q->id can match one of ids[]. This doesn't matter for |
| 760 | * moveafter but TODO may matter for playlist support. */ |
| 761 | switch(info_) { |
| 762 | case QUEUED_TRACKS_ID: |
| 763 | case PLAYLIST_TRACKS_ID: |
| 764 | /* Rearrangement within some widget. Send ID and track data. */ |
| 765 | ql->drop(ql, tracks->nvec, tracks->vec, ids->vec, q); |
| 766 | break; |
| 767 | case PLAYABLE_TRACKS_ID: |
| 768 | /* Copying between widgets. IDs mean nothing so don't send them. */ |
| 769 | ql->drop(ql, tracks->nvec, tracks->vec, NULL, q); |
| 770 | break; |
| 771 | } |
| 772 | if(path) |
| 773 | gtk_tree_path_free(path); |
| 774 | } |
| 775 | |
| 776 | static int count_drag_targets(const GtkTargetEntry *targets) { |
| 777 | const GtkTargetEntry *t = targets; |
| 778 | |
| 779 | while(t->target) |
| 780 | ++t; |
| 781 | return t - targets; |
| 782 | } |
| 783 | |
| 784 | /** @brief Initialize a @ref queuelike */ |
| 785 | GtkWidget *init_queuelike(struct queuelike *ql) { |
| 786 | D(("init_queuelike")); |
| 787 | /* Create the list store. We add an extra column to hold a pointer to the |
| 788 | * queue_entry. */ |
| 789 | GType *types = xcalloc(ql->ncolumns + EXTRA_COLUMNS, sizeof (GType)); |
| 790 | for(int n = 0; n < ql->ncolumns + EXTRA_COLUMNS; ++n) |
| 791 | types[n] = G_TYPE_STRING; |
| 792 | types[ql->ncolumns + QUEUEPOINTER_COLUMN] = G_TYPE_POINTER; |
| 793 | ql->store = gtk_list_store_newv(ql->ncolumns + EXTRA_COLUMNS, types); |
| 794 | g_object_set_data(G_OBJECT(ql->store), "ql", (void *)ql); |
| 795 | |
| 796 | /* Create the view */ |
| 797 | ql->view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(ql->store)); |
| 798 | gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(ql->view), TRUE); |
| 799 | |
| 800 | /* Create cell renderers and label columns */ |
| 801 | for(int n = 0; n < ql->ncolumns; ++n) { |
| 802 | GtkCellRenderer *r = gtk_cell_renderer_text_new(); |
| 803 | if(ql->columns[n].flags & COL_ELLIPSIZE) |
| 804 | g_object_set(r, "ellipsize", PANGO_ELLIPSIZE_END, (char *)0); |
| 805 | if(ql->columns[n].flags & COL_RIGHT) |
| 806 | g_object_set(r, "xalign", (gfloat)1.0, (char *)0); |
| 807 | GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes |
| 808 | (ql->columns[n].name, |
| 809 | r, |
| 810 | "text", n, |
| 811 | "cell-background", ql->ncolumns + BACKGROUND_COLUMN, |
| 812 | "foreground", ql->ncolumns + FOREGROUND_COLUMN, |
| 813 | (char *)0); |
| 814 | gtk_tree_view_column_set_resizable(c, TRUE); |
| 815 | gtk_tree_view_column_set_reorderable(c, TRUE); |
| 816 | if(ql->columns[n].flags & COL_EXPAND) |
| 817 | g_object_set(c, "expand", TRUE, (char *)0); |
| 818 | gtk_tree_view_append_column(GTK_TREE_VIEW(ql->view), c); |
| 819 | } |
| 820 | |
| 821 | /* The selection should support multiple things being selected */ |
| 822 | ql->selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ql->view)); |
| 823 | g_object_ref(ql->selection); |
| 824 | gtk_tree_selection_set_mode(ql->selection, GTK_SELECTION_MULTIPLE); |
| 825 | |
| 826 | /* Catch button presses */ |
| 827 | g_signal_connect(ql->view, "button-press-event", |
| 828 | G_CALLBACK(ql_button_release), ql); |
| 829 | |
| 830 | /* Drag+drop*/ |
| 831 | if(ql->drop) { |
| 832 | /* Originally this was: |
| 833 | * |
| 834 | * gtk_tree_view_set_reorderable(GTK_TREE_VIEW(ql->view), TRUE); |
| 835 | * |
| 836 | * However this has a two deficiencies: |
| 837 | * |
| 838 | * 1) Only one row can be dragged at once. It would be nice |
| 839 | * to be able to do bulk rearrangements since the server |
| 840 | * can cope with that well. |
| 841 | * 2) Dragging between windows is not possible. When playlist |
| 842 | * support appears, it should be possible to drag tracks |
| 843 | * from the choose tag into the playlist. |
| 844 | * |
| 845 | * At the time of writing neither of these problems are fully solved, the |
| 846 | * code as it stands is just a stepping stone in that direction. |
| 847 | */ |
| 848 | |
| 849 | /* This view will act as a drag source */ |
| 850 | gtk_drag_source_set(ql->view, |
| 851 | GDK_BUTTON1_MASK, |
| 852 | ql->drag_source_targets, |
| 853 | count_drag_targets(ql->drag_source_targets), |
| 854 | ql->drag_dest_actions); |
| 855 | /* This view will act as a drag destination */ |
| 856 | gtk_drag_dest_set(ql->view, |
| 857 | GTK_DEST_DEFAULT_HIGHLIGHT|GTK_DEST_DEFAULT_DROP, |
| 858 | ql->drag_dest_targets, |
| 859 | count_drag_targets(ql->drag_dest_targets), |
| 860 | ql->drag_dest_actions); |
| 861 | g_signal_connect(ql->view, "drag-motion", |
| 862 | G_CALLBACK(ql_drag_motion), ql); |
| 863 | g_signal_connect(ql->view, "drag-leave", |
| 864 | G_CALLBACK(ql_drag_leave), ql); |
| 865 | g_signal_connect(ql->view, "drag-data-get", |
| 866 | G_CALLBACK(ql_drag_data_get), ql); |
| 867 | g_signal_connect(ql->view, "drag-data-received", |
| 868 | G_CALLBACK(ql_drag_data_received), ql); |
| 869 | make_treeview_multidrag(ql->view, NULL); |
| 870 | // TODO playing track should be refused by predicate arg |
| 871 | } else { |
| 872 | /* For queues that cannot accept a drop we still accept a copy out */ |
| 873 | gtk_drag_source_set(ql->view, |
| 874 | GDK_BUTTON1_MASK, |
| 875 | ql->drag_source_targets, |
| 876 | count_drag_targets(ql->drag_source_targets), |
| 877 | ql->drag_source_actions); |
| 878 | g_signal_connect(ql->view, "drag-data-get", |
| 879 | G_CALLBACK(ql_drag_data_get), ql); |
| 880 | make_treeview_multidrag(ql->view, NULL); |
| 881 | } |
| 882 | |
| 883 | /* TODO style? */ |
| 884 | |
| 885 | if(ql->init) |
| 886 | ql->init(ql); |
| 887 | |
| 888 | /* Update display text when lookups complete */ |
| 889 | event_register("lookups-completed", queue_lookups_completed, ql); |
| 890 | |
| 891 | GtkWidget *scrolled = scroll_widget(ql->view); |
| 892 | g_object_set_data(G_OBJECT(scrolled), "type", (void *)ql_tabtype(ql)); |
| 893 | return scrolled; |
| 894 | } |
| 895 | |
| 896 | /** @brief Destroy a queuelike |
| 897 | * @param ql Queuelike to destroy |
| 898 | * |
| 899 | * Returns @p ql to its initial state. |
| 900 | */ |
| 901 | void destroy_queuelike(struct queuelike *ql) { |
| 902 | if(ql->store) { |
| 903 | g_object_unref(ql->store); |
| 904 | ql->store = NULL; |
| 905 | } |
| 906 | if(ql->view) { |
| 907 | gtk_object_destroy(GTK_OBJECT(ql->view)); |
| 908 | ql->view = NULL; |
| 909 | } |
| 910 | if(ql->menu) { |
| 911 | gtk_object_destroy(GTK_OBJECT(ql->menu)); |
| 912 | ql->menu = NULL; |
| 913 | } |
| 914 | if(ql->selection) { |
| 915 | g_object_unref(ql->selection); |
| 916 | ql->selection = NULL; |
| 917 | } |
| 918 | ql->q = NULL; |
| 919 | } |
| 920 | |
| 921 | /* |
| 922 | Local Variables: |
| 923 | c-basic-offset:2 |
| 924 | comment-column:40 |
| 925 | fill-column:79 |
| 926 | indent-tabs-mode:nil |
| 927 | End: |
| 928 | */ |