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