2 * This file is part of DisOrder
3 * Copyright (C) 2006-2009 Richard Kettlewell
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file disobedience/queue-generic.c
19 * @brief Disobedience queue widgets
21 * This file provides contains code shared between all the queue-like
22 * widgets - the queue, the recent list and the added tracks list.
24 * This code is in the process of being rewritten to use the native list
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.
31 * When new contents turn up we rearrange the list store accordingly.
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.
38 * - display playing row in a different color?
40 #include "disobedience.h"
42 #include "queue-generic.h"
43 #include "multidrag.h"
45 static const GtkTargetEntry queuelike_targets
[] = {
47 (char *)"text/x-disorder-queued-tracks", /* drag type */
48 GTK_TARGET_SAME_WIDGET
, /* rearrangement within a widget */
52 (char *)"text/x-disorder-playable-tracks", /* drag type */
53 GTK_TARGET_SAME_APP
|GTK_TARGET_OTHER_WIDGET
, /* copying between widgets */
58 /* Track detail lookup ----------------------------------------------------- */
60 static void queue_lookups_completed(const char attribute((unused
)) *event
,
61 void attribute((unused
)) *eventdata
,
63 struct queuelike
*ql
= callbackdata
;
64 ql_update_list_store(ql
);
67 /* Column formatting -------------------------------------------------------- */
69 /** @brief Format the 'when' column */
70 const char *column_when(const struct queue_entry
*q
,
71 const char attribute((unused
)) *data
) {
78 case playing_isscratch
:
79 case playing_unplayed
:
84 case playing_no_player
:
86 case playing_scratched
:
89 case playing_quitting
:
97 strftime(when
, sizeof when
, "%H:%M", localtime_r(&t
, &tm
));
100 return xstrdup(when
);
103 /** @brief Format the 'who' column */
104 const char *column_who(const struct queue_entry
*q
,
105 const char attribute((unused
)) *data
) {
107 return q
->submitter ? q
->submitter
: "";
110 /** @brief Format one of the track name columns */
111 const char *column_namepart(const struct queue_entry
*q
,
113 D(("column_namepart"));
114 return namepart(q
->track
, "display", data
);
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"));
123 char *played
= 0, *length
= 0;
125 /* Work out what to say for the length */
126 l
= namepart_length(q
->track
);
128 byte_xasprintf(&length
, "%ld:%02ld", l
/ 60, l
% 60);
130 byte_xasprintf(&length
, "?:??");
131 /* For the currently playing track we want to report how much of the track
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
;
142 l
= playing_track
->sofar
+ (now
- last_playing
);
144 byte_xasprintf(&played
, "%ld:%02ld/%s", l
/ 60, l
% 60, length
);
150 /* List store maintenance -------------------------------------------------- */
152 /** @brief Return the @ref queue_entry corresponding to @p iter
153 * @param model Model that owns @p iter
154 * @param iter Tree iterator
155 * @return Pointer to queue entry
157 struct queue_entry
*ql_iter_to_q(GtkTreeModel
*model
,
159 struct queuelike
*ql
= g_object_get_data(G_OBJECT(model
), "ql");
161 memset(v
, 0, sizeof v
);
162 gtk_tree_model_get_value(model
, iter
, ql
->ncolumns
+ QUEUEPOINTER_COLUMN
, v
);
163 assert(G_VALUE_TYPE(v
) == G_TYPE_POINTER
);
164 struct queue_entry
*const q
= g_value_get_pointer(v
);
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
174 struct queue_entry
*ql_path_to_q(GtkTreeModel
*model
,
177 if(!gtk_tree_model_get_iter(model
, iter
, path
))
179 return ql_iter_to_q(model
, iter
);
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
186 void ql_update_row(struct queue_entry
*q
,
188 const struct queuelike
*const ql
= q
->ql
;
190 D(("ql_update_row"));
191 /* If no iter was supplied, work it out */
192 GtkTreeIter my_iter
[1];
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
);
202 /* Update all the columns */
203 for(int col
= 0; col
< ql
->ncolumns
; ++col
) {
204 const char *const v
= ql
->columns
[col
].value(q
,
205 ql
->columns
[col
].data
);
207 gtk_list_store_set(ql
->store
, iter
,
211 gtk_list_store_set(ql
->store
, iter
,
212 ql
->ncolumns
+ QUEUEPOINTER_COLUMN
, q
,
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
,
220 gtk_list_store_set(ql
->store
, iter
,
221 ql
->ncolumns
+ BACKGROUND_COLUMN
, (char *)0,
222 ql
->ncolumns
+ FOREGROUND_COLUMN
, (char *)0,
226 /** @brief Update the list store
227 * @param ql Queuelike to update
229 * Called when new namepart data is available (and initially). Doesn't change
230 * the rows, just updates the cell values.
232 void ql_update_list_store(struct queuelike
*ql
) {
233 D(("ql_update_list_store"));
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
);
242 struct newqueue_data
{
243 struct queue_entry
*old
, *new;
246 static void record_queue_map(hash
*h
,
248 struct queue_entry
*old
,
249 struct queue_entry
*new) {
250 struct newqueue_data
*nqd
;
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
);
264 static void dump_queue(struct queue_entry
*head
, struct queue_entry
*mark
) {
265 for(struct queue_entry
*q
= head
; q
; q
= q
->next
) {
267 fprintf(stderr
, "!");
268 fprintf(stderr
, "%s", q
->id
);
270 fprintf(stderr
, " ");
272 fprintf(stderr
, "\n");
275 static void dump_rows(struct queuelike
*ql
) {
277 gboolean it
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql
->store
),
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
);
284 fprintf(stderr
, " ");
286 fprintf(stderr
, "\n");
290 /** @brief Reset the list store
291 * @param ql Queuelike to reset
292 * @param newq New queue contents/ordering
294 * Updates the queue to match @p newq
296 void ql_new_queue(struct queuelike
*ql
,
297 struct queue_entry
*newq
) {
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
)
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
);
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);
318 gboolean it
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql
->store
),
320 int inserted
= 0, deleted
= 0, kept
= 0;
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
);
325 /* Tell this row that it belongs to the new version of the queue */
326 gtk_list_store_set(ql
->store
, iter
,
327 ql
->ncolumns
+ QUEUEPOINTER_COLUMN
, nqd
->new,
329 it
= gtk_tree_model_iter_next(GTK_TREE_MODEL(ql
->store
), iter
);
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
);
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 */
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
);
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
),
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
);
357 gtk_list_store_insert_after(ql
->store
, iter
, where
);
359 gtk_list_store_append(ql
->store
, iter
);
361 gtk_list_store_prepend(ql
->store
, iter
);
362 gtk_list_store_set(ql
->store
, iter
,
363 ql
->ncolumns
+ QUEUEPOINTER_COLUMN
, q
,
365 //fprintf(stderr, " add %s", q->id);
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
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.
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);
383 it
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql
->store
),
385 struct queue_entry
*rq
= newq
; /* r for 'right, correct' */
386 int swaps
= 0, searches
= 0;
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);
393 //fprintf(stderr, " mismatch\n");
394 GtkTreeIter next
[1] = { *iter
};
395 gboolean nit
= gtk_tree_model_iter_next(GTK_TREE_MODEL(ql
->store
), next
);
397 struct queue_entry
*nq
= ql_iter_to_q(GTK_TREE_MODEL(ql
->store
), next
);
398 //fprintf(stderr, " candidate: %s\n", nq->id);
401 nit
= gtk_tree_model_iter_next(GTK_TREE_MODEL(ql
->store
), next
);
405 //fprintf(stderr, " found it\n");
406 gtk_list_store_swap(ql
->store
, iter
, next
);
408 //fprintf(stderr, "%s: new row state: ", ql->name);
412 /* ...and onto the next one */
413 it
= gtk_tree_model_iter_next(GTK_TREE_MODEL(ql
->store
), iter
);
417 fprintf(stderr
, "%6s: %3d kept %3d inserted %3d deleted %3d swaps %4d searches\n", ql
->name
,
418 kept
, inserted
, deleted
, swaps
, searches
);
420 //fprintf(stderr, "done\n");
422 /* Set the rest of the columns in new rows */
423 ql_update_list_store(ql
);
427 /** @brief State for ql_drag_begin() and its callbacks */
428 struct ql_drag_begin_state
{
429 struct queuelike
*ql
;
435 /** @brief Callback to construct a row pixmap */
436 static void ql_drag_make_row_pixmaps(GtkTreeModel
attribute((unused
)) *model
,
438 GtkTreeIter
attribute((unused
)) *iter
,
440 struct ql_drag_begin_state
*qdbs
= data
;
442 qdbs
->pixmaps
[qdbs
->index
++]
443 = gtk_tree_view_create_row_drag_icon(GTK_TREE_VIEW(qdbs
->ql
->view
),
447 /** @brief Called when a drag operation from this queuelike begins
448 * @param w Source widget (the tree view)
449 * @param dc Drag context
450 * @param user_data The queuelike
452 static void ql_drag_begin(GtkWidget
attribute((unused
)) *w
,
453 GdkDragContext
attribute((unused
)) *dc
,
454 gpointer user_data
) {
455 struct queuelike
*const ql
= user_data
;
456 struct ql_drag_begin_state qdbs
[1];
459 //fprintf(stderr, "drag-begin\n");
460 memset(qdbs
, 0, sizeof *qdbs
);
462 /* Find out how many rows there are */
463 if(!(qdbs
->rows
= gtk_tree_selection_count_selected_rows(ql
->selection
)))
464 return; /* doesn't make sense */
465 /* Generate a pixmap for each row */
466 qdbs
->pixmaps
= xcalloc(qdbs
->rows
, sizeof *qdbs
->pixmaps
);
467 gtk_tree_selection_selected_foreach(ql
->selection
,
468 ql_drag_make_row_pixmaps
,
470 /* Determine the size of the final icon */
471 int height
= 0, width
= 0;
472 for(int n
= 0; n
< qdbs
->rows
; ++n
) {
474 gdk_drawable_get_size(qdbs
->pixmaps
[n
], &pxw
, &pxh
);
479 if(!width
|| !height
)
480 return; /* doesn't make sense */
481 /* Construct the icon */
482 icon
= gdk_pixmap_new(qdbs
->pixmaps
[0], width
, height
, -1);
483 GdkGC
*gc
= gdk_gc_new(icon
);
484 gdk_gc_set_colormap(gc
, gtk_widget_get_colormap(ql
->view
));
486 for(int n
= 0; n
< qdbs
->rows
; ++n
) {
488 gdk_drawable_get_size(qdbs
->pixmaps
[n
], &pxw
, &pxh
);
489 gdk_draw_drawable(icon
,
492 0, 0, /* source coords */
493 0, y
, /* dest coords */
494 pxw
, pxh
); /* size */
496 gdk_drawable_unref(qdbs
->pixmaps
[n
]);
497 qdbs
->pixmaps
[n
] = NULL
;
499 // TODO scale down a bit, the resulting icons are currently a bit on the
501 gtk_drag_source_set_icon(ql
->view
,
502 gtk_widget_get_colormap(ql
->view
),
507 /** @brief Called when a drag moves within a candidate destination
508 * @param w Destination widget
509 * @param dc Drag context
510 * @param x Current pointer location
511 * @param y Current pointer location
512 * @param time_ Current time
513 * @param user_data Pointer to queuelike
514 * @return TRUE in a dropzone, otherwise FALSE
516 static gboolean
ql_drag_motion(GtkWidget
*w
,
521 gpointer
attribute((unused
)) user_data
) {
522 //struct queuelike *const ql = user_data;
523 GdkDragAction action
= 0;
525 // GTK_DEST_DEFAULT_MOTION vets actions as follows:
526 // 1) if dc->suggested_action is in the gtk_drag_dest_set actions
527 // then dc->suggested_action is taken as the action.
528 // 2) otherwise if dc->actions intersects the gtk_drag_dest_set actions
529 // then the lowest-numbered member of the intersection is chosen.
530 // 3) otherwise no member is chosen and gdk_drag_status() is called
531 // with action=0 to refuse the drop.
532 if(dc
->suggested_action
) {
533 if(dc
->suggested_action
& (GDK_ACTION_MOVE
|GDK_ACTION_COPY
))
534 action
= dc
->suggested_action
;
535 } else if(dc
->actions
& GDK_ACTION_MOVE
)
536 action
= GDK_ACTION_MOVE
;
537 else if(dc
->actions
& GDK_ACTION_COPY
)
538 action
= GDK_ACTION_COPY
;
539 /*fprintf(stderr, "suggested %#x actions %#x result %#x\n",
540 dc->suggested_action, dc->actions, action);*/
542 // If the action is acceptable then we see if this widget is acceptable
543 if(gtk_drag_dest_find_target(w
, dc
, NULL
) == GDK_NONE
)
547 gdk_drag_status(dc
, action
, time_
);
549 // Highlight the drop area
551 GtkTreeViewDropPosition pos
;
553 if(gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(w
),
557 //fprintf(stderr, "gtk_tree_view_get_dest_row_at_pos() -> TRUE\n");
558 // Normalize drop position
560 case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE
:
561 pos
= GTK_TREE_VIEW_DROP_BEFORE
;
563 case GTK_TREE_VIEW_DROP_INTO_OR_AFTER
:
564 pos
= GTK_TREE_VIEW_DROP_AFTER
;
568 // Highlight drop target
569 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW(w
), path
, pos
);
571 //fprintf(stderr, "gtk_tree_view_get_dest_row_at_pos() -> FALSE\n");
572 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW(w
), NULL
, 0);
578 /** @brief Called when a drag moves leaves a candidate destination
579 * @param w Destination widget
580 * @param dc Drag context
581 * @param time_ Current time
582 * @param user_data Pointer to queuelike
584 static void ql_drag_leave(GtkWidget
*w
,
585 GdkDragContext
attribute((unused
)) *dc
,
586 guint
attribute((unused
)) time_
,
587 gpointer
attribute((unused
)) user_data
) {
588 //struct queuelike *const ql = user_data;
590 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW(w
), NULL
, 0);
593 /** @brief Callback to add selected tracks to the selection data
595 * Called from ql_drag_data_get().
597 static void ql_drag_data_get_collect(GtkTreeModel
*model
,
598 GtkTreePath
attribute((unused
)) *path
,
601 struct dynstr
*const result
= data
;
602 struct queue_entry
*const q
= ql_iter_to_q(model
, iter
);
604 dynstr_append_string(result
, q
->id
);
605 dynstr_append(result
, '\n');
606 dynstr_append_string(result
, q
->track
);
607 dynstr_append(result
, '\n');
610 /** @brief Called to extract the dragged data from the source queuelike
611 * @param w Source widget (the tree view)
612 * @param dc Drag context
613 * @param data Where to put the answer
614 * @param info_ Target @c info parameter
615 * @param time_ Time data requested (for some reason not a @c time_t)
616 * @param user_data The queuelike
618 static void ql_drag_data_get(GtkWidget
attribute((unused
)) *w
,
619 GdkDragContext
attribute((unused
)) *dc
,
620 GtkSelectionData
*data
,
621 guint
attribute((unused
)) info_
,
622 guint
attribute((unused
)) time_
,
623 gpointer user_data
) {
624 struct queuelike
*const ql
= user_data
;
625 struct dynstr result
[1];
627 /* The list of tracks is converted into a single string, consisting of IDs
628 * and track names. Each is terminated by a newline. Including both ID and
629 * track name means that the receiver can use whichever happens to be more
632 gtk_tree_selection_selected_foreach(ql
->selection
,
633 ql_drag_data_get_collect
,
635 // TODO must not be able to drag playing track!
636 //fprintf(stderr, "drag-data-get: %.*s\n",
637 // result->nvec, result->vec);
638 /* gtk_selection_data_set_text() insists that data->target is one of a
639 * variety of stringy atoms. TODO: where does this value actually come
641 gtk_selection_data_set(data
,
643 8, (guchar
*)result
->vec
, result
->nvec
);
646 /** @brief Called when drag data is received
647 * @param w Target widget (the tree view)
648 * @param dc Drag context
649 * @param x The drop location
650 * @param y The drop location
651 * @param data The selection data
652 * @param info_ The target type that was chosen
653 * @param time_ Time data received (for some reason not a @c time_t)
654 * @param user_data The queuelike
656 static void ql_drag_data_received(GtkWidget
attribute((unused
)) *w
,
657 GdkDragContext
attribute((unused
)) *dc
,
660 GtkSelectionData
*data
,
661 guint
attribute((unused
)) info_
,
662 guint
attribute((unused
)) time_
,
663 gpointer user_data
) {
664 struct queuelike
*const ql
= user_data
;
666 struct vector ids
[1], tracks
[1];
669 //fprintf(stderr, "drag-data-received: %d,%d info_=%u\n", x, y, info_);
670 /* Get the selection string */
671 p
= result
= (char *)gtk_selection_data_get_text(data
);
673 //fprintf(stderr, "gtk_selection_data_get_text() returned NULL\n");
676 //fprintf(stderr, "%s--\n", result);
677 /* Parse it back into IDs and track names */
681 char *nl
= strchr(p
, '\n');
685 //fprintf(stderr, " %s\n", p);
686 vector_append(parity
++ & 1 ? tracks
: ids
, xstrdup(p
));
690 if(ids
->nvec
!= tracks
->nvec
) {
691 //fprintf(stderr, " inconsistent drag data!\n");
694 vector_terminate(ids
);
695 vector_terminate(tracks
);
696 /* Figure out which row the drop precedes (if any) */
698 GtkTreeViewDropPosition pos
;
699 struct queue_entry
*q
;
700 if(!gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(ql
->view
), x
, y
,
702 //fprintf(stderr, "gtk_tree_view_get_dest_row_at_pos returned FALSE\n");
703 /* This generally means a drop past the end of the queue. We find the last
704 * element in the queue and ask to move after that. */
705 for(q
= ql
->q
; q
&& q
->next
; q
= q
->next
)
708 /* Convert the path to a queue entry pointer. */
709 q
= ql_path_to_q(GTK_TREE_MODEL(ql
->store
), path
);
710 //fprintf(stderr, " tree view likes to drop near %s\n",
711 // q->id ? q->id : "NULL");
712 /* TODO interpretation of q=NULL */
713 /* Q should point to the entry just before the insertion point, so that
714 * moveafter works, or NULL to insert right at the start. We don't support
715 * dropping into a row, since that doesn't make sense for us. */
717 case GTK_TREE_VIEW_DROP_BEFORE
:
718 case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE
:
721 //fprintf(stderr, " ...but we like to drop near %s\n",
722 // q ? q->id : "NULL");
729 /* Guarantee we never drop an empty list */
732 /* Note that q->id can match one of ids[]. This doesn't matter for
733 * moveafter but TODO may matter for playlist support. */
736 /* Rearrangement. Send ID and track data. */
737 ql
->drop(ql
, tracks
->nvec
, tracks
->vec
, ids
->vec
, q
);
740 /* Copying between widgets. IDs mean nothing so don't send them. */
741 ql
->drop(ql
, tracks
->nvec
, tracks
->vec
, NULL
, q
);
746 /** @brief Initialize a @ref queuelike */
747 GtkWidget
*init_queuelike(struct queuelike
*ql
) {
748 D(("init_queuelike"));
749 /* Create the list store. We add an extra column to hold a pointer to the
751 GType
*types
= xcalloc(ql
->ncolumns
+ EXTRA_COLUMNS
, sizeof (GType
));
752 for(int n
= 0; n
< ql
->ncolumns
+ EXTRA_COLUMNS
; ++n
)
753 types
[n
] = G_TYPE_STRING
;
754 types
[ql
->ncolumns
+ QUEUEPOINTER_COLUMN
] = G_TYPE_POINTER
;
755 ql
->store
= gtk_list_store_newv(ql
->ncolumns
+ EXTRA_COLUMNS
, types
);
756 g_object_set_data(G_OBJECT(ql
->store
), "ql", (void *)ql
);
758 /* Create the view */
759 ql
->view
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(ql
->store
));
760 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(ql
->view
), TRUE
);
762 /* Create cell renderers and label columns */
763 for(int n
= 0; n
< ql
->ncolumns
; ++n
) {
764 GtkCellRenderer
*r
= gtk_cell_renderer_text_new();
765 if(ql
->columns
[n
].flags
& COL_ELLIPSIZE
)
766 g_object_set(r
, "ellipsize", PANGO_ELLIPSIZE_END
, (char *)0);
767 if(ql
->columns
[n
].flags
& COL_RIGHT
)
768 g_object_set(r
, "xalign", (gfloat
)1.0, (char *)0);
769 GtkTreeViewColumn
*c
= gtk_tree_view_column_new_with_attributes
770 (ql
->columns
[n
].name
,
773 "background", ql
->ncolumns
+ BACKGROUND_COLUMN
,
774 "foreground", ql
->ncolumns
+ FOREGROUND_COLUMN
,
776 gtk_tree_view_column_set_resizable(c
, TRUE
);
777 gtk_tree_view_column_set_reorderable(c
, TRUE
);
778 if(ql
->columns
[n
].flags
& COL_EXPAND
)
779 g_object_set(c
, "expand", TRUE
, (char *)0);
780 gtk_tree_view_append_column(GTK_TREE_VIEW(ql
->view
), c
);
783 /* The selection should support multiple things being selected */
784 ql
->selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(ql
->view
));
785 gtk_tree_selection_set_mode(ql
->selection
, GTK_SELECTION_MULTIPLE
);
787 /* Catch button presses */
788 g_signal_connect(ql
->view
, "button-press-event",
789 G_CALLBACK(ql_button_release
), ql
);
793 /* Originally this was:
795 * gtk_tree_view_set_reorderable(GTK_TREE_VIEW(ql->view), TRUE);
797 * However this has a two deficiencies:
799 * 1) Only one row can be dragged at once. It would be nice
800 * to be able to do bulk rearrangements since the server
801 * can cope with that well.
802 * 2) Dragging between windows is not possible. When playlist
803 * support appears, it should be possible to drag tracks
804 * from the choose tag into the playlist.
806 * At the time of writing neither of these problems are fully solved, the
807 * code as it stands is just a stepping stone in that direction.
810 /* This view will act as a drag source */
811 gtk_drag_source_set(ql
->view
,
814 sizeof queuelike_targets
/ sizeof *queuelike_targets
,
816 /* This view will act as a drag destination */
817 gtk_drag_dest_set(ql
->view
,
818 GTK_DEST_DEFAULT_HIGHLIGHT
|GTK_DEST_DEFAULT_DROP
,
820 sizeof queuelike_targets
/ sizeof *queuelike_targets
,
821 GDK_ACTION_MOVE
|GDK_ACTION_COPY
);
822 g_signal_connect(ql
->view
, "drag-begin",
823 G_CALLBACK(ql_drag_begin
), ql
);
824 g_signal_connect(ql
->view
, "drag-motion",
825 G_CALLBACK(ql_drag_motion
), ql
);
826 g_signal_connect(ql
->view
, "drag-leave",
827 G_CALLBACK(ql_drag_leave
), ql
);
828 g_signal_connect(ql
->view
, "drag-data-get",
829 G_CALLBACK(ql_drag_data_get
), ql
);
830 g_signal_connect(ql
->view
, "drag-data-received",
831 G_CALLBACK(ql_drag_data_received
), ql
);
832 make_treeview_multidrag(ql
->view
);
834 /* For queues that cannot accept a drop we still accept a copy out */
835 gtk_drag_source_set(ql
->view
,
838 sizeof queuelike_targets
/ sizeof *queuelike_targets
,
840 g_signal_connect(ql
->view
, "drag-begin",
841 G_CALLBACK(ql_drag_begin
), ql
);
842 g_signal_connect(ql
->view
, "drag-data-get",
843 G_CALLBACK(ql_drag_data_get
), ql
);
844 make_treeview_multidrag(ql
->view
);
851 /* Update display text when lookups complete */
852 event_register("lookups-completed", queue_lookups_completed
, ql
);
854 GtkWidget
*scrolled
= scroll_widget(ql
->view
);
855 g_object_set_data(G_OBJECT(scrolled
), "type", (void *)ql_tabtype(ql
));