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