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