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