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"
44 #include "autoscroll.h"
46 static const GtkTargetEntry queuelike_targets
[] = {
48 (char *)"text/x-disorder-queued-tracks", /* drag type */
49 GTK_TARGET_SAME_WIDGET
, /* rearrangement within a widget */
53 (char *)"text/x-disorder-playable-tracks", /* drag type */
54 GTK_TARGET_SAME_APP
|GTK_TARGET_OTHER_WIDGET
, /* copying between widgets */
59 /* Track detail lookup ----------------------------------------------------- */
61 static void queue_lookups_completed(const char attribute((unused
)) *event
,
62 void attribute((unused
)) *eventdata
,
64 struct queuelike
*ql
= callbackdata
;
65 ql_update_list_store(ql
);
68 /* Column formatting -------------------------------------------------------- */
70 /** @brief Format the 'when' column */
71 const char *column_when(const struct queue_entry
*q
,
72 const char attribute((unused
)) *data
) {
79 case playing_isscratch
:
80 case playing_unplayed
:
85 case playing_no_player
:
87 case playing_scratched
:
90 case playing_quitting
:
98 strftime(when
, sizeof when
, "%H:%M", localtime_r(&t
, &tm
));
101 return xstrdup(when
);
104 /** @brief Format the 'who' column */
105 const char *column_who(const struct queue_entry
*q
,
106 const char attribute((unused
)) *data
) {
108 return q
->submitter ? q
->submitter
: "";
111 /** @brief Format one of the track name columns */
112 const char *column_namepart(const struct queue_entry
*q
,
114 D(("column_namepart"));
115 return namepart(q
->track
, "display", data
);
118 /** @brief Format the length column */
119 const char *column_length(const struct queue_entry
*q
,
120 const char attribute((unused
)) *data
) {
121 D(("column_length"));
124 char *played
= 0, *length
= 0;
126 /* Work out what to say for the length */
127 l
= namepart_length(q
->track
);
129 byte_xasprintf(&length
, "%ld:%02ld", l
/ 60, l
% 60);
131 byte_xasprintf(&length
, "?:??");
132 /* For the currently playing track we want to report how much of the track
134 if(q
== playing_track
) {
135 /* log_state() arranges that we re-get the playing data whenever the
136 * pause/resume state changes */
137 if(last_state
& DISORDER_TRACK_PAUSED
)
138 l
= playing_track
->sofar
;
143 l
= playing_track
->sofar
+ (now
- last_playing
);
145 byte_xasprintf(&played
, "%ld:%02ld/%s", l
/ 60, l
% 60, length
);
151 /* List store maintenance -------------------------------------------------- */
153 /** @brief Return the @ref queue_entry corresponding to @p iter
154 * @param model Model that owns @p iter
155 * @param iter Tree iterator
156 * @return Pointer to queue entry
158 struct queue_entry
*ql_iter_to_q(GtkTreeModel
*model
,
160 struct queuelike
*ql
= g_object_get_data(G_OBJECT(model
), "ql");
162 memset(v
, 0, sizeof v
);
163 gtk_tree_model_get_value(model
, iter
, ql
->ncolumns
+ QUEUEPOINTER_COLUMN
, v
);
164 assert(G_VALUE_TYPE(v
) == G_TYPE_POINTER
);
165 struct queue_entry
*const q
= g_value_get_pointer(v
);
170 /** @brief Return the @ref queue_entry corresponding to @p path
171 * @param model Model to query
172 * @param path Path into tree
173 * @return Pointer to queue entry or NULL
175 struct queue_entry
*ql_path_to_q(GtkTreeModel
*model
,
178 if(!gtk_tree_model_get_iter(model
, iter
, path
))
180 return ql_iter_to_q(model
, iter
);
183 /** @brief Update one row of a list store
184 * @param q Queue entry
185 * @param iter Iterator referring to row or NULL to work it out
187 void ql_update_row(struct queue_entry
*q
,
189 const struct queuelike
*const ql
= q
->ql
;
191 D(("ql_update_row"));
192 /* If no iter was supplied, work it out */
193 GtkTreeIter my_iter
[1];
195 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql
->store
), my_iter
);
196 struct queue_entry
*qq
;
197 for(qq
= ql
->q
; qq
&& q
!= qq
; qq
= qq
->next
)
198 gtk_tree_model_iter_next(GTK_TREE_MODEL(ql
->store
), my_iter
);
203 /* Update all the columns */
204 for(int col
= 0; col
< ql
->ncolumns
; ++col
) {
205 const char *const v
= ql
->columns
[col
].value(q
,
206 ql
->columns
[col
].data
);
208 gtk_list_store_set(ql
->store
, iter
,
212 gtk_list_store_set(ql
->store
, iter
,
213 ql
->ncolumns
+ QUEUEPOINTER_COLUMN
, q
,
215 if(q
== playing_track
)
216 gtk_list_store_set(ql
->store
, iter
,
217 ql
->ncolumns
+ BACKGROUND_COLUMN
, BG_PLAYING
,
218 ql
->ncolumns
+ FOREGROUND_COLUMN
, FG_PLAYING
,
221 gtk_list_store_set(ql
->store
, iter
,
222 ql
->ncolumns
+ BACKGROUND_COLUMN
, (char *)0,
223 ql
->ncolumns
+ FOREGROUND_COLUMN
, (char *)0,
227 /** @brief Update the list store
228 * @param ql Queuelike to update
230 * Called when new namepart data is available (and initially). Doesn't change
231 * the rows, just updates the cell values.
233 void ql_update_list_store(struct queuelike
*ql
) {
234 D(("ql_update_list_store"));
236 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql
->store
), iter
);
237 for(struct queue_entry
*q
= ql
->q
; q
; q
= q
->next
) {
238 ql_update_row(q
, iter
);
239 gtk_tree_model_iter_next(GTK_TREE_MODEL(ql
->store
), iter
);
243 struct newqueue_data
{
244 struct queue_entry
*old
, *new;
247 static void record_queue_map(hash
*h
,
249 struct queue_entry
*old
,
250 struct queue_entry
*new) {
251 struct newqueue_data
*nqd
;
253 if(!(nqd
= hash_find(h
, id
))) {
254 static const struct newqueue_data empty
[1];
255 hash_add(h
, id
, empty
, HASH_INSERT
);
256 nqd
= hash_find(h
, id
);
260 fprintf(stderr
, " old: %s\n", id
);
266 fprintf(stderr
, " new: %s\n", id
);
273 static void dump_queue(struct queue_entry
*head
, struct queue_entry
*mark
) {
274 for(struct queue_entry
*q
= head
; q
; q
= q
->next
) {
276 fprintf(stderr
, " !");
277 fprintf(stderr
, " %s\n", q
->id
);
281 static void dump_rows(struct queuelike
*ql
) {
283 gboolean it
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql
->store
),
286 struct queue_entry
*q
= ql_iter_to_q(GTK_TREE_MODEL(ql
->store
), iter
);
287 it
= gtk_tree_model_iter_next(GTK_TREE_MODEL(ql
->store
), iter
);
288 fprintf(stderr
, " %s\n", q
->id
);
293 /** @brief Reset the list store
294 * @param ql Queuelike to reset
295 * @param newq New queue contents/ordering
297 * Updates the queue to match @p newq
299 void ql_new_queue(struct queuelike
*ql
,
300 struct queue_entry
*newq
) {
304 /* Tell every queue entry which queue owns it */
306 fprintf(stderr
, "%s: filling in q->ql\n", ql
->name
);
308 for(struct queue_entry
*q
= newq
; q
; q
= q
->next
)
312 fprintf(stderr
, "%s: constructing h\n", ql
->name
);
314 /* Construct map from id to new and old structures */
315 hash
*h
= hash_new(sizeof(struct newqueue_data
));
316 for(struct queue_entry
*q
= ql
->q
; q
; q
= q
->next
)
317 record_queue_map(h
, q
->id
, q
, NULL
);
318 for(struct queue_entry
*q
= newq
; q
; q
= q
->next
)
319 record_queue_map(h
, q
->id
, NULL
, q
);
321 /* The easy bit: delete rows not present any more. In the same pass we
322 * update the secret column containing the queue_entry pointer. */
324 fprintf(stderr
, "%s: deleting rows...\n", ql
->name
);
327 gboolean it
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql
->store
),
329 int inserted
= 0, deleted
= 0, kept
= 0;
331 struct queue_entry
*q
= ql_iter_to_q(GTK_TREE_MODEL(ql
->store
), iter
);
332 const struct newqueue_data
*nqd
= hash_find(h
, q
->id
);
334 /* Tell this row that it belongs to the new version of the queue */
335 gtk_list_store_set(ql
->store
, iter
,
336 ql
->ncolumns
+ QUEUEPOINTER_COLUMN
, nqd
->new,
338 it
= gtk_tree_model_iter_next(GTK_TREE_MODEL(ql
->store
), iter
);
341 /* Delete this row (and move iter to the next one) */
343 fprintf(stderr
, " delete %s\n", q
->id
);
345 it
= gtk_list_store_remove(ql
->store
, iter
);
350 /* Now every row's secret column is right, but we might be missing new rows
351 * and they might be in the wrong order */
353 /* We're going to have to support arbitrary rearrangements, so we might as
354 * well add new elements at the end. */
356 fprintf(stderr
, "%s: adding rows...\n", ql
->name
);
358 struct queue_entry
*after
= 0;
359 for(struct queue_entry
*q
= newq
; q
; q
= q
->next
) {
360 const struct newqueue_data
*nqd
= hash_find(h
, q
->id
);
363 /* Try to insert at the right sort of place */
364 GtkTreeIter where
[1];
365 gboolean wit
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql
->store
),
367 while(wit
&& ql_iter_to_q(GTK_TREE_MODEL(ql
->store
), where
) != after
)
368 wit
= gtk_tree_model_iter_next(GTK_TREE_MODEL(ql
->store
), where
);
370 gtk_list_store_insert_after(ql
->store
, iter
, where
);
372 gtk_list_store_append(ql
->store
, iter
);
374 gtk_list_store_prepend(ql
->store
, iter
);
375 gtk_list_store_set(ql
->store
, iter
,
376 ql
->ncolumns
+ QUEUEPOINTER_COLUMN
, q
,
379 fprintf(stderr
, " add %s\n", q
->id
);
386 /* Now exactly the right set of rows are present, and they have the right
387 * queue_entry pointers in their secret column, but they may be in the wrong
390 * The current code is simple but amounts to a bubble-sort - we might easily
391 * called gtk_tree_model_iter_next a couple of thousand times.
394 fprintf(stderr
, "%s: rearranging rows\n", ql
->name
);
395 fprintf(stderr
, "%s: target state:\n", ql
->name
);
397 fprintf(stderr
, "%s: current state:\n", ql
->name
);
400 it
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql
->store
), iter
);
401 struct queue_entry
*tq
= newq
; /* t-for-target */
402 int swaps
= 0, searches
= 0;
405 struct queue_entry
*cq
= ql_iter_to_q(GTK_TREE_MODEL(ql
->store
), iter
);
408 /* Everything has the right queue pointer (see above) so it's sufficient to
409 * compare pointers to detect mismatches */
412 fprintf(stderr
, " pointer mismatch at row %d\n", row
);
413 fprintf(stderr
, " target id %s\n", tq
->id
);
414 fprintf(stderr
, " actual id %s\n", cq
->id
);
416 /* Start looking for the target row fromn the next row */
417 GtkTreeIter next
[1] = { *iter
};
418 gboolean nit
= gtk_tree_model_iter_next(GTK_TREE_MODEL(ql
->store
), next
);
420 struct queue_entry
*nq
= ql_iter_to_q(GTK_TREE_MODEL(ql
->store
), next
);
422 fprintf(stderr
, " candidate: %s\n", nq
->id
);
426 nit
= gtk_tree_model_iter_next(GTK_TREE_MODEL(ql
->store
), next
);
430 gtk_list_store_swap(ql
->store
, iter
, next
);
433 fprintf(stderr
, "%s: found it. new row state:\n", ql
->name
);
438 /* ...and onto the next one */
439 it
= gtk_tree_model_iter_next(GTK_TREE_MODEL(ql
->store
), iter
);
444 fprintf(stderr
, "%6s: %3d kept %3d inserted %3d deleted %3d swaps %4d searches\n", ql
->name
,
445 kept
, inserted
, deleted
, swaps
, searches
);
446 fprintf(stderr
, "done\n");
449 /* Set the rest of the columns in new rows */
450 ql_update_list_store(ql
);
454 /* Drag and drop ------------------------------------------------------------ */
456 /** @brief Identify the drop path
457 * @param w Destination tree view widget
458 * @param model Underlying tree model
459 * @param wx X coordinate
460 * @param wy Y coordinate
461 * @param posp Where to store relative position
462 * @return Target path or NULL
464 * This is used by ql_drag_motion() and ql_drag_data_received() to identify a
465 * drop would or does land. It's important that they use the same code since
466 * otherwise the visual feedback can be inconsistent with the actual effect!
468 * Remember to free the returned path.
470 static GtkTreePath
*ql_drop_path(GtkWidget
*w
,
473 GtkTreeViewDropPosition
*posp
) {
474 GtkTreePath
*path
= NULL
;
475 GtkTreeViewDropPosition pos
;
476 GtkTreeIter iter
[1], last
[1];
479 gtk_tree_view_convert_widget_to_tree_coords(GTK_TREE_VIEW(w
),
481 if(gtk_tree_view_get_dest_row_at_pos(GTK_TREE_VIEW(w
),
485 //fprintf(stderr, "gtk_tree_view_get_dest_row_at_pos() -> TRUE\n");
486 // Normalize drop position
488 case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE
:
489 pos
= GTK_TREE_VIEW_DROP_BEFORE
;
491 case GTK_TREE_VIEW_DROP_INTO_OR_AFTER
:
492 pos
= GTK_TREE_VIEW_DROP_AFTER
;
496 } else if(gtk_tree_model_get_iter_first(model
, iter
)) {
497 /* If the pointer isn't over any particular row then either it's below
498 * the last row, in which case we want the dropzone to be below that row;
499 * or it's above the first row (in the column headings) in which case we
500 * want the dropzone to be above that row. */
502 /* Find the last row */
505 } while(gtk_tree_model_iter_next(model
, iter
));
506 /* The drop target is just after it */
507 pos
= GTK_TREE_VIEW_DROP_AFTER
;
510 /* The drop target will be just before the first row */
511 pos
= GTK_TREE_VIEW_DROP_BEFORE
;
513 path
= gtk_tree_model_get_path(model
, iter
);
519 /** @brief Called when a drag moves within a candidate destination
520 * @param w Destination widget
521 * @param dc Drag context
522 * @param x Current pointer location
523 * @param y Current pointer location
524 * @param time_ Current time
525 * @param user_data Pointer to queuelike
526 * @return TRUE in a dropzone, otherwise FALSE
528 * This is the handler for the "drag-motion" signal.
530 static gboolean
ql_drag_motion(GtkWidget
*w
,
535 gpointer user_data
) {
536 struct queuelike
*const ql
= user_data
;
537 GdkDragAction action
= 0;
539 // GTK_DEST_DEFAULT_MOTION vets actions as follows:
540 // 1) if dc->suggested_action is in the gtk_drag_dest_set actions
541 // then dc->suggested_action is taken as the action.
542 // 2) otherwise if dc->actions intersects the gtk_drag_dest_set actions
543 // then the lowest-numbered member of the intersection is chosen.
544 // 3) otherwise no member is chosen and gdk_drag_status() is called
545 // with action=0 to refuse the drop.
546 if(dc
->suggested_action
) {
547 if(dc
->suggested_action
& (GDK_ACTION_MOVE
|GDK_ACTION_COPY
))
548 action
= dc
->suggested_action
;
549 } else if(dc
->actions
& GDK_ACTION_MOVE
)
550 action
= GDK_ACTION_MOVE
;
551 else if(dc
->actions
& GDK_ACTION_COPY
)
552 action
= GDK_ACTION_COPY
;
553 /*fprintf(stderr, "suggested %#x actions %#x result %#x\n",
554 dc->suggested_action, dc->actions, action);*/
556 // If the action is acceptable then we see if this widget is acceptable
557 if(gtk_drag_dest_find_target(w
, dc
, NULL
) == GDK_NONE
)
561 //fprintf(stderr, "drag action: %u\n", action);
562 gdk_drag_status(dc
, action
, time_
);
564 GtkTreeViewDropPosition pos
;
566 // Find the drop target
567 GtkTreePath
*path
= ql_drop_path(w
, GTK_TREE_MODEL(ql
->store
), x
, y
, &pos
);
568 // Highlight drop target
569 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW(w
), path
, pos
);
571 gtk_tree_path_free(path
);
573 autoscroll_add(GTK_TREE_VIEW(w
));
574 return TRUE
; /* We are (always) in a drop zone */
577 /** @brief Called when a drag moves leaves a candidate destination
578 * @param w Destination widget
579 * @param dc Drag context
580 * @param time_ Current time
581 * @param user_data Pointer to queuelike
583 * This is the handler for the "drag-leave" signal.
585 * It turns out that we get a drag-leave event when the data is dropped, too
586 * (See _gtk_drag_dest_handle_event). This seems logically consistent and is
587 * convenient too - for instance it's why autoscroll_remove() gets called at
588 * the end of a drag+drop sequence.
590 static void ql_drag_leave(GtkWidget
*w
,
591 GdkDragContext
attribute((unused
)) *dc
,
592 guint
attribute((unused
)) time_
,
593 gpointer
attribute((unused
)) user_data
) {
594 //struct queuelike *const ql = user_data;
596 gtk_tree_view_set_drag_dest_row(GTK_TREE_VIEW(w
), NULL
, 0);
597 autoscroll_remove(GTK_TREE_VIEW(w
));
600 /** @brief Callback to add selected tracks to the selection data
602 * Called from ql_drag_data_get().
604 static void ql_drag_data_get_collect(GtkTreeModel
*model
,
605 GtkTreePath
attribute((unused
)) *path
,
608 struct dynstr
*const result
= data
;
609 struct queue_entry
*const q
= ql_iter_to_q(model
, iter
);
611 dynstr_append_string(result
, q
->id
);
612 dynstr_append(result
, '\n');
613 dynstr_append_string(result
, q
->track
);
614 dynstr_append(result
, '\n');
617 /** @brief Called to extract the dragged data from the source queuelike
618 * @param w Source widget (the tree view)
619 * @param dc Drag context
620 * @param data Where to put the answer
621 * @param info_ Target @c info parameter
622 * @param time_ Time data requested (for some reason not a @c time_t)
623 * @param user_data The queuelike
625 * The list of tracks is converted into a single string, consisting of IDs
626 * and track names. Each is terminated by a newline. Including both ID and
627 * track name means that the receiver can use whichever happens to be more
630 * If there are no IDs for rows in this widget then the ID half is undefined.
632 * This is the handler for the "drag-data-get" signal.
634 static void ql_drag_data_get(GtkWidget
attribute((unused
)) *w
,
635 GdkDragContext
attribute((unused
)) *dc
,
636 GtkSelectionData
*data
,
637 guint
attribute((unused
)) info_
,
638 guint
attribute((unused
)) time_
,
639 gpointer user_data
) {
640 struct queuelike
*const ql
= user_data
;
641 struct dynstr result
[1];
644 gtk_tree_selection_selected_foreach(ql
->selection
,
645 ql_drag_data_get_collect
,
647 // TODO must not be able to drag playing track!
648 //fprintf(stderr, "drag-data-get: %.*s\n",
649 // result->nvec, result->vec);
650 /* gtk_selection_data_set_text() insists that data->target is one of a
651 * variety of stringy atoms. TODO: where does this value actually come
653 gtk_selection_data_set(data
,
655 8, (guchar
*)result
->vec
, result
->nvec
);
658 /** @brief Called when drag data is received
659 * @param w Target widget (the tree view)
660 * @param dc Drag context
661 * @param x The drop location
662 * @param y The drop location
663 * @param data The selection data
664 * @param info_ The target type that was chosen
665 * @param time_ Time data received (for some reason not a @c time_t)
666 * @param user_data The queuelike
668 * This is the handler for the "drag-data-received" signal.
670 static void ql_drag_data_received(GtkWidget
attribute((unused
)) *w
,
671 GdkDragContext
attribute((unused
)) *dc
,
674 GtkSelectionData
*data
,
675 guint
attribute((unused
)) info_
,
676 guint
attribute((unused
)) time_
,
677 gpointer user_data
) {
678 struct queuelike
*const ql
= user_data
;
680 struct vector ids
[1], tracks
[1];
683 //fprintf(stderr, "drag-data-received: %d,%d info_=%u\n", x, y, info_);
684 /* Get the selection string */
685 p
= result
= (char *)gtk_selection_data_get_text(data
);
687 //fprintf(stderr, "gtk_selection_data_get_text() returned NULL\n");
690 //fprintf(stderr, "%s--\n", result);
691 /* Parse it back into IDs and track names */
695 char *nl
= strchr(p
, '\n');
699 //fprintf(stderr, " %s\n", p);
700 vector_append(parity
++ & 1 ? tracks
: ids
, xstrdup(p
));
704 if(ids
->nvec
!= tracks
->nvec
) {
705 //fprintf(stderr, " inconsistent drag data!\n");
708 vector_terminate(ids
);
709 vector_terminate(tracks
);
710 /* Figure out which row the drop precedes (if any) */
711 GtkTreeViewDropPosition pos
;
712 struct queue_entry
*q
;
713 GtkTreePath
*path
= ql_drop_path(w
, GTK_TREE_MODEL(ql
->store
), x
, y
, &pos
);
715 q
= ql_path_to_q(GTK_TREE_MODEL(ql
->store
), path
);
717 /* This generally means a drop past the end of the queue. We find the last
718 * element in the queue and ask to move after that. */
719 for(q
= ql
->q
; q
&& q
->next
; q
= q
->next
)
723 case GTK_TREE_VIEW_DROP_BEFORE
:
724 case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE
:
727 //fprintf(stderr, " ...but we like to drop near %s\n",
728 // q ? q->id : "NULL");
734 /* Guarantee we never drop an empty list */
737 /* Note that q->id can match one of ids[]. This doesn't matter for
738 * moveafter but TODO may matter for playlist support. */
741 /* Rearrangement. Send ID and track data. */
742 ql
->drop(ql
, tracks
->nvec
, tracks
->vec
, ids
->vec
, q
);
745 /* Copying between widgets. IDs mean nothing so don't send them. */
746 ql
->drop(ql
, tracks
->nvec
, tracks
->vec
, NULL
, q
);
750 gtk_tree_path_free(path
);
753 /** @brief Initialize a @ref queuelike */
754 GtkWidget
*init_queuelike(struct queuelike
*ql
) {
755 D(("init_queuelike"));
756 /* Create the list store. We add an extra column to hold a pointer to the
758 GType
*types
= xcalloc(ql
->ncolumns
+ EXTRA_COLUMNS
, sizeof (GType
));
759 for(int n
= 0; n
< ql
->ncolumns
+ EXTRA_COLUMNS
; ++n
)
760 types
[n
] = G_TYPE_STRING
;
761 types
[ql
->ncolumns
+ QUEUEPOINTER_COLUMN
] = G_TYPE_POINTER
;
762 ql
->store
= gtk_list_store_newv(ql
->ncolumns
+ EXTRA_COLUMNS
, types
);
763 g_object_set_data(G_OBJECT(ql
->store
), "ql", (void *)ql
);
765 /* Create the view */
766 ql
->view
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(ql
->store
));
767 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(ql
->view
), TRUE
);
769 /* Create cell renderers and label columns */
770 for(int n
= 0; n
< ql
->ncolumns
; ++n
) {
771 GtkCellRenderer
*r
= gtk_cell_renderer_text_new();
772 if(ql
->columns
[n
].flags
& COL_ELLIPSIZE
)
773 g_object_set(r
, "ellipsize", PANGO_ELLIPSIZE_END
, (char *)0);
774 if(ql
->columns
[n
].flags
& COL_RIGHT
)
775 g_object_set(r
, "xalign", (gfloat
)1.0, (char *)0);
776 GtkTreeViewColumn
*c
= gtk_tree_view_column_new_with_attributes
777 (ql
->columns
[n
].name
,
780 "background", ql
->ncolumns
+ BACKGROUND_COLUMN
,
781 "foreground", ql
->ncolumns
+ FOREGROUND_COLUMN
,
783 gtk_tree_view_column_set_resizable(c
, TRUE
);
784 gtk_tree_view_column_set_reorderable(c
, TRUE
);
785 if(ql
->columns
[n
].flags
& COL_EXPAND
)
786 g_object_set(c
, "expand", TRUE
, (char *)0);
787 gtk_tree_view_append_column(GTK_TREE_VIEW(ql
->view
), c
);
790 /* The selection should support multiple things being selected */
791 ql
->selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(ql
->view
));
792 g_object_ref(ql
->selection
);
793 gtk_tree_selection_set_mode(ql
->selection
, GTK_SELECTION_MULTIPLE
);
795 /* Catch button presses */
796 g_signal_connect(ql
->view
, "button-press-event",
797 G_CALLBACK(ql_button_release
), ql
);
801 /* Originally this was:
803 * gtk_tree_view_set_reorderable(GTK_TREE_VIEW(ql->view), TRUE);
805 * However this has a two deficiencies:
807 * 1) Only one row can be dragged at once. It would be nice
808 * to be able to do bulk rearrangements since the server
809 * can cope with that well.
810 * 2) Dragging between windows is not possible. When playlist
811 * support appears, it should be possible to drag tracks
812 * from the choose tag into the playlist.
814 * At the time of writing neither of these problems are fully solved, the
815 * code as it stands is just a stepping stone in that direction.
818 /* This view will act as a drag source */
819 gtk_drag_source_set(ql
->view
,
822 sizeof queuelike_targets
/ sizeof *queuelike_targets
,
824 /* This view will act as a drag destination */
825 gtk_drag_dest_set(ql
->view
,
826 GTK_DEST_DEFAULT_HIGHLIGHT
|GTK_DEST_DEFAULT_DROP
,
828 sizeof queuelike_targets
/ sizeof *queuelike_targets
,
829 GDK_ACTION_MOVE
|GDK_ACTION_COPY
);
830 g_signal_connect(ql
->view
, "drag-motion",
831 G_CALLBACK(ql_drag_motion
), ql
);
832 g_signal_connect(ql
->view
, "drag-leave",
833 G_CALLBACK(ql_drag_leave
), ql
);
834 g_signal_connect(ql
->view
, "drag-data-get",
835 G_CALLBACK(ql_drag_data_get
), ql
);
836 g_signal_connect(ql
->view
, "drag-data-received",
837 G_CALLBACK(ql_drag_data_received
), ql
);
838 make_treeview_multidrag(ql
->view
, NULL
);
839 // TODO playing track should be refused by predicate arg
841 /* For queues that cannot accept a drop we still accept a copy out */
842 gtk_drag_source_set(ql
->view
,
845 sizeof queuelike_targets
/ sizeof *queuelike_targets
,
847 g_signal_connect(ql
->view
, "drag-data-get",
848 G_CALLBACK(ql_drag_data_get
), ql
);
849 make_treeview_multidrag(ql
->view
, NULL
);
857 /* Update display text when lookups complete */
858 event_register("lookups-completed", queue_lookups_completed
, ql
);
860 GtkWidget
*scrolled
= scroll_widget(ql
->view
);
861 g_object_set_data(G_OBJECT(scrolled
), "type", (void *)ql_tabtype(ql
));
865 /** @brief Destroy a queuelike
866 * @param ql Queuelike to destroy
868 * Returns @p ql to its initial state.
870 void destroy_queuelike(struct queuelike
*ql
) {
872 g_object_unref(ql
->store
);
876 gtk_object_destroy(GTK_OBJECT(ql
->view
));
880 gtk_object_destroy(GTK_OBJECT(ql
->menu
));
884 g_object_unref(ql
->selection
);
885 ql
->selection
= NULL
;