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