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