2 * This file is part of DisOrder
3 * Copyright (C) 2006-2008 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 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file disobedience/queue.c
21 * @brief Queue widgets
23 * This file provides both the queue widget and the recently-played widget.
25 * A queue layout is structured as follows:
31 * titlecells[col] eventbox (made by wrap_queue_cell)
32 * titlecells[col]->child label (from columns[])
35 * cells[row * N + c] eventbox (made by wrap_queue_cell)
36 * cells[row * N + c]->child label (from column constructors)
39 * titlescroll never has any scrollbars. Instead whenever mainscroll's
40 * horizontal adjustment is changed, queue_scrolled adjusts titlescroll to
41 * match, forcing the title and the queue to pan in sync but allowing the queue
42 * to scroll independently.
44 * Whenever the queue changes everything below mainlayout is thrown away and
45 * reconstructed from scratch. Name lookups are cached, so this doesn't imply
46 * lots of disorder protocol traffic.
48 * The last cell on each row is the padding cell, and this extends ridiculously
49 * far to the right. (Can we do better?)
51 * When drag and drop is active we create extra eventboxes to act as dropzones.
52 * These only exist while the drag proceeds, as otherwise they steal events
53 * from more deserving widgets. (It might work to hide them when not in use
54 * too but this way around the d+d code is a bit more self-contained.)
56 * NB that while in the server the playing track is not in the queue, in
57 * Disobedience, the playing does live in @c ql_queue.q, despite its different
58 * status to everything else found in that list.
61 #include "disobedience.h"
64 /** @brief Horizontal padding for queue cells */
65 #define HCELLPADDING 4
67 /** @brief Vertical padding for queue cells */
68 #define VCELLPADDING 2
70 /* Queue management -------------------------------------------------------- */
81 static void add_drag_targets(struct queuelike
*ql
);
82 static void remove_drag_targets(struct queuelike
*ql
);
83 static void redisplay_queue(struct queuelike
*ql
);
84 static GtkWidget
*column_when(const struct queuelike
*ql
,
85 const struct queue_entry
*q
,
87 static GtkWidget
*column_who(const struct queuelike
*ql
,
88 const struct queue_entry
*q
,
90 static GtkWidget
*column_namepart(const struct queuelike
*ql
,
91 const struct queue_entry
*q
,
93 static GtkWidget
*column_length(const struct queuelike
*ql
,
94 const struct queue_entry
*q
,
96 static int draggable_row(const struct queue_entry
*q
);
97 static void recent_changed(const char *event
,
100 static void added_changed(const char *event
,
103 static void queue_changed(const char *event
,
107 static const struct tabtype tabtype_queue
; /* forward */
109 static const GtkTargetEntry dragtargets
[] = {
110 { (char *)"disobedience-queue", GTK_TARGET_SAME_APP
, 0 }
112 #define NDRAGTARGETS (int)(sizeof dragtargets / sizeof *dragtargets)
114 /** @brief Definition of a column */
116 const char *name
; /* Column name */
117 GtkWidget
*(*widget
)(const struct queuelike
*ql
,
118 const struct queue_entry
*q
,
119 const char *data
); /* Make a label for this column */
120 const char *data
; /* Data to pass to widget() */
121 gfloat xalign
; /* Alignment of the label */
124 /** @brief Table of columns for queue and recently played list */
125 static const struct column maincolumns
[] = {
126 { "When", column_when
, 0, 1 },
127 { "Who", column_who
, 0, 0 },
128 { "Artist", column_namepart
, "artist", 0 },
129 { "Album", column_namepart
, "album", 0 },
130 { "Title", column_namepart
, "title", 0 },
131 { "Length", column_length
, 0, 1 }
134 /** @brief Number of columns in queue and recnetly played list */
135 #define NMAINCOLUMNS (int)(sizeof maincolumns / sizeof *maincolumns)
137 /** @brief Table of columns for recently added tracks */
138 static const struct column addedcolumns
[] = {
139 { "Artist", column_namepart
, "artist", 0 },
140 { "Album", column_namepart
, "album", 0 },
141 { "Title", column_namepart
, "title", 0 },
142 { "Length", column_length
, 0, 1 }
145 /** @brief Number of columns in recently added list */
146 #define NADDEDCOLUMNS (int)(sizeof addedcolumns / sizeof *addedcolumns)
148 /** @brief Maximum number of column in any @ref queuelike */
149 #define MAXCOLUMNS (NMAINCOLUMNS > NADDEDCOLUMNS ? NMAINCOLUMNS : NADDEDCOLUMNS)
151 /** @brief Data passed to menu item activation handlers */
152 struct menuiteminfo
{
153 struct queuelike
*ql
; /**< @brief which queue we're dealing with */
154 struct queue_entry
*q
; /**< @brief hovered entry or 0 */
157 /** @brief An item in the queue's popup menu */
158 struct queue_menuitem
{
159 /** @brief Menu item name */
162 /** @brief Called to activate the menu item
164 * The user data is the queue entry that the pointer was over when the menu
166 void (*activate
)(GtkMenuItem
*menuitem
,
169 /** @brief Called to determine whether the menu item is usable.
171 * Returns @c TRUE if it should be sensitive and @c FALSE otherwise. @p q
172 * points to the queue entry the pointer is over.
174 int (*sensitive
)(struct queuelike
*ql
,
175 struct queue_menuitem
*m
,
176 struct queue_entry
*q
);
178 /** @brief Signal handler ID */
181 /** @brief Widget for menu item */
185 /** @brief A queue-like object
187 * There are (currently) three of these: @ref ql_queue, @ref ql_recent and @ref
191 /** @brief Called when an update completes */
192 void (*notify
)(void);
194 /** @brief Called to fix up the queue after update
195 * @param q The list passed back from the server
196 * @return Assigned to @c ql->q
198 struct queue_entry
*(*fixup
)(struct queue_entry
*q
);
201 GtkWidget
*mainlayout
; /**< @brief main layout */
202 GtkWidget
*mainscroll
; /**< @brief scroller for main layout */
203 GtkWidget
*titlelayout
; /**< @brief title layout */
204 GtkWidget
*titlecells
[MAXCOLUMNS
+ 1]; /**< @brief title cells */
205 GtkWidget
**cells
; /**< @brief all the cells */
206 GtkWidget
*menu
; /**< @brief popup menu */
207 struct queue_menuitem
*menuitems
; /**< @brief menu items */
208 GtkWidget
*dragmark
; /**< @brief drag destination marker */
209 GtkWidget
**dropzones
; /**< @brief drag targets */
210 int ndropzones
; /**< @brief number of drag targets */
213 struct queue_entry
*q
; /**< @brief head of queue */
214 struct queue_entry
*last_click
; /**< @brief last click */
215 int nrows
; /**< @brief number of rows */
216 int mainrowheight
; /**< @brief height of one row */
217 hash
*selection
; /**< @brief currently selected items */
218 int swallow_release
; /**< @brief swallow button release from drag */
220 const struct column
*columns
; /**< @brief Table of columns */
221 int ncolumns
; /**< @brief Number of columns */
224 static struct queuelike ql_queue
; /**< @brief The main queue */
225 static struct queuelike ql_recent
; /*< @brief Recently-played tracks */
226 static struct queuelike ql_added
; /*< @brief Newly added tracks */
227 static struct queue_entry
*actual_queue
; /**< @brief actual queue */
228 struct queue_entry
*playing_track
; /**< @brief currently playing */
229 static time_t last_playing
= (time_t)-1; /**< @brief when last got playing */
230 static int namepart_lookups_outstanding
;
231 static int namepart_completions_deferred
; /* # of completions not processed */
232 static const struct cache_type cachetype_string
= { 3600 };
233 static const struct cache_type cachetype_integer
= { 3600 };
234 static GtkWidget
*playing_length_label
;
236 /* Debugging --------------------------------------------------------------- */
239 static void describe_widget(const char *name
, GtkWidget
*w
, int indent
) {
243 fprintf(stderr
, "%*s[%s]: '%s'\n", indent
, "",
244 name
, gtk_widget_get_name(w
));
245 gdk_window_get_position(w
->window
, &wx
, &wy
);
246 gdk_drawable_get_size(GDK_DRAWABLE(w
->window
), &ww
, &wh
);
247 fprintf(stderr
, "%*s window %p: %dx%d at %dx%d\n",
248 indent
, "", w
->window
, ww
, wh
, wx
, wy
);
251 static void dump_layout(const struct queuelike
*ql
) {
255 const struct queue_entry
*q
;
257 describe_widget("mainscroll", ql
->mainscroll
, 0);
258 describe_widget("mainlayout", ql
->mainlayout
, 1);
259 for(q
= ql
->q
, row
= 0; q
; q
= q
->next
, ++row
)
260 for(col
= 0; col
< ql
->ncolumns
+ 1; ++col
)
261 if((w
= ql
->cells
[row
* (ql
->ncolumns
+ 1) + col
])) {
262 sprintf(s
, "%dx%d", row
, col
);
263 describe_widget(s
, w
, 2);
264 if(GTK_BIN(w
)->child
)
265 describe_widget(0, w
, 3);
270 /* Track detail lookup ----------------------------------------------------- */
272 /** @brief Called when a namepart lookup has completed or failed */
273 static void namepart_completed_or_failed(void) {
274 D(("namepart_completed_or_failed"));
275 --namepart_lookups_outstanding
;
276 if(!namepart_lookups_outstanding
) {
277 redisplay_queue(&ql_queue
);
278 redisplay_queue(&ql_recent
);
279 redisplay_queue(&ql_added
);
280 namepart_completions_deferred
= 0;
284 /** @brief Called when a namepart lookup has completed */
285 static void namepart_completed(void *v
, const char *error
, const char *value
) {
287 gtk_label_set_text(GTK_LABEL(report_label
), error
);
291 cache_put(&cachetype_string
, key
, value
);
292 ++namepart_completions_deferred
;
294 namepart_completed_or_failed();
297 /** @brief Called when a length lookup has completed */
298 static void length_completed(void *v
, const char *error
, long l
) {
300 gtk_label_set_text(GTK_LABEL(report_label
), error
);
305 D(("namepart_completed"));
306 value
= xmalloc(sizeof *value
);
308 cache_put(&cachetype_integer
, key
, value
);
309 ++namepart_completions_deferred
;
311 namepart_completed_or_failed();
314 /** @brief Arrange to fill in a namepart cache entry */
315 static void namepart_fill(const char *track
,
319 ++namepart_lookups_outstanding
;
320 disorder_eclient_namepart(client
, namepart_completed
,
321 track
, context
, part
, (void *)key
);
324 /** @brief Look up a namepart
326 * If it is in the cache then just return its value. If not then look it up
327 * and arrange for the queues to be updated when its value is available. */
328 static const char *namepart(const char *track
,
334 D(("namepart %s %s %s", track
, context
, part
));
335 byte_xasprintf(&key
, "namepart context=%s part=%s track=%s",
336 context
, part
, track
);
337 value
= cache_get(&cachetype_string
, key
);
340 /* stick a value in the cache so we don't issue another lookup if we
342 cache_put(&cachetype_string
, key
, value
= "?");
343 namepart_fill(track
, context
, part
, key
);
348 /** @brief Called from @ref disobedience/properties.c when we know a name part has changed */
349 void namepart_update(const char *track
,
354 byte_xasprintf(&key
, "namepart context=%s part=%s track=%s",
355 context
, part
, track
);
356 /* Only refetch if it's actually in the cache */
357 if(cache_get(&cachetype_string
, key
))
358 namepart_fill(track
, context
, part
, key
);
361 /** @brief Look up a track length
363 * If it is in the cache then just return its value. If not then look it up
364 * and arrange for the queues to be updated when its value is available. */
365 static long getlength(const char *track
) {
368 static const long bogus
= -1;
370 D(("getlength %s", track
));
371 byte_xasprintf(&key
, "length track=%s", track
);
372 value
= cache_get(&cachetype_integer
, key
);
374 D(("deferring..."));;
375 cache_put(&cachetype_integer
, key
, value
= &bogus
);
376 ++namepart_lookups_outstanding
;
377 disorder_eclient_length(client
, length_completed
, track
, key
);
382 /* Column constructors ----------------------------------------------------- */
384 /** @brief Format the 'when' column */
385 static GtkWidget
*column_when(const struct queuelike
attribute((unused
)) *ql
,
386 const struct queue_entry
*q
,
387 const char attribute((unused
)) *data
) {
394 case playing_isscratch
:
395 case playing_unplayed
:
400 case playing_no_player
:
402 case playing_scratched
:
403 case playing_started
:
405 case playing_quitting
:
413 strftime(when
, sizeof when
, "%H:%M", localtime_r(&t
, &tm
));
417 return gtk_label_new(when
);
420 /** @brief Format the 'who' column */
421 static GtkWidget
*column_who(const struct queuelike
attribute((unused
)) *ql
,
422 const struct queue_entry
*q
,
423 const char attribute((unused
)) *data
) {
426 return gtk_label_new(q
->submitter ? q
->submitter
: "");
429 /** @brief Format one of the track name columns */
430 static GtkWidget
*column_namepart(const struct queuelike
431 attribute((unused
)) *ql
,
432 const struct queue_entry
*q
,
434 D(("column_namepart"));
436 return gtk_label_new(truncate_for_display(namepart(q
->track
, "display", data
),
437 config
->short_display
));
440 /** @brief Compute the length field */
441 static const char *text_length(const struct queue_entry
*q
) {
444 char *played
= 0, *length
= 0;
446 /* Work out what to say for the length */
447 l
= getlength(q
->track
);
449 byte_xasprintf(&length
, "%ld:%02ld", l
/ 60, l
% 60);
451 byte_xasprintf(&length
, "?:??");
452 /* For the currently playing track we want to report how much of the track
454 if(q
== playing_track
) {
455 /* log_state() arranges that we re-get the playing data whenever the
456 * pause/resume state changes */
457 if(last_state
& DISORDER_TRACK_PAUSED
)
458 l
= playing_track
->sofar
;
461 l
= playing_track
->sofar
+ (now
- last_playing
);
463 byte_xasprintf(&played
, "%ld:%02ld/%s", l
/ 60, l
% 60, length
);
469 /** @brief Format the length column */
470 static GtkWidget
*column_length(const struct queuelike
attribute((unused
)) *ql
,
471 const struct queue_entry
*q
,
472 const char attribute((unused
)) *data
) {
473 D(("column_length"));
474 if(q
== playing_track
) {
475 assert(!playing_length_label
);
477 playing_length_label
= gtk_label_new(text_length(q
));
478 /* Zot playing_length_label when it is destroyed */
479 g_signal_connect(playing_length_label
, "destroy",
480 G_CALLBACK(gtk_widget_destroyed
), &playing_length_label
);
481 return playing_length_label
;
484 return gtk_label_new(text_length(q
));
489 /** @brief Apply a new queue contents, transferring the selection from the old value */
490 static void update_queue(struct queuelike
*ql
, struct queue_entry
*newq
) {
491 struct queue_entry
*q
;
494 /* Propagate last_click across the change */
496 for(q
= newq
; q
; q
= q
->next
) {
497 if(!strcmp(q
->id
, ql
->last_click
->id
))
502 /* Tell every queue entry which queue owns it */
503 for(q
= newq
; q
; q
= q
->next
)
505 /* Switch to the new queue */
507 /* Clean up any selected items that have fallen off */
508 for(q
= ql
->q
; q
; q
= q
->next
)
509 selection_live(ql
->selection
, q
->id
);
510 selection_cleanup(ql
->selection
);
513 /** @brief Wrap up a widget for putting into the queue or title
514 * @param label Label to contain
515 * @param style Pointer to style to use
516 * @param wp Updated with maximum width (or NULL)
519 static GtkWidget
*wrap_queue_cell(GtkWidget
*label
,
525 D(("wrap_queue_cell"));
526 /* Padding should be in the label so there are no gaps in the
528 gtk_misc_set_padding(GTK_MISC(label
), HCELLPADDING
, VCELLPADDING
);
529 /* Event box is just to hold a background color */
531 bg
= gtk_event_box_new();
532 gtk_container_add(GTK_CONTAINER(bg
), label
);
534 /* Update maximum width */
535 gtk_widget_size_request(label
, &req
);
536 if(req
.width
> *wp
) *wp
= req
.width
;
539 gtk_widget_set_style(bg
, style
);
540 gtk_widget_set_style(label
, style
);
544 /** @brief Create the wrapped widget for a cell in the queue display */
545 static GtkWidget
*get_queue_cell(struct queuelike
*ql
,
546 const struct queue_entry
*q
,
552 D(("get_queue_cell %d %d", row
, col
));
553 label
= ql
->columns
[col
].widget(ql
, q
, ql
->columns
[col
].data
);
554 gtk_misc_set_alignment(GTK_MISC(label
), ql
->columns
[col
].xalign
, 0);
555 return wrap_queue_cell(label
, style
, wp
);
558 /** @brief Add a padding cell to the end of a row */
559 static GtkWidget
*get_padding_cell(GtkStyle
*style
) {
560 D(("get_padding_cell"));
562 return wrap_queue_cell(gtk_label_new(""), style
, 0);
565 /* User button press and menu ---------------------------------------------- */
567 /** @brief Update widget states in order to reflect the selection status */
568 static void set_widget_states(struct queuelike
*ql
) {
569 struct queue_entry
*q
;
572 for(q
= ql
->q
, row
= 0; q
; q
= q
->next
, ++row
) {
573 for(col
= 0; col
< ql
->ncolumns
+ 1; ++col
)
574 gtk_widget_set_state(ql
->cells
[row
* (ql
->ncolumns
+ 1) + col
],
575 selection_selected(ql
->selection
, q
->id
) ?
576 GTK_STATE_SELECTED
: GTK_STATE_NORMAL
);
578 /* Might need to change sensitivity of 'Properties' in main menu */
582 /** @brief Ordering function for queue entries */
583 static int queue_before(const struct queue_entry
*a
,
584 const struct queue_entry
*b
) {
590 /** @brief A button was pressed and released */
591 static gboolean
queuelike_button_released(GtkWidget
attribute((unused
)) *widget
,
592 GdkEventButton
*event
,
593 gpointer user_data
) {
594 struct queue_entry
*q
= user_data
, *qq
;
595 struct queuelike
*ql
= q
->ql
;
596 struct menuiteminfo
*mii
;
599 /* Might be a release left over from a drag */
600 if(ql
->swallow_release
) {
601 ql
->swallow_release
= 0;
602 return FALSE
; /* propagate */
605 if(event
->type
== GDK_BUTTON_PRESS
606 && event
->button
== 3) {
607 /* Right button click.
608 * If the current item is not selected then switch the selection to just
610 if(q
&& !selection_selected(ql
->selection
, q
->id
)) {
611 selection_empty(ql
->selection
);
612 selection_set(ql
->selection
, q
->id
, 1);
614 set_widget_states(ql
);
616 /* Set the sensitivity of each menu item and (re-)establish the signal
618 for(n
= 0; ql
->menuitems
[n
].name
; ++n
) {
619 if(ql
->menuitems
[n
].handlerid
)
620 g_signal_handler_disconnect(ql
->menuitems
[n
].w
,
621 ql
->menuitems
[n
].handlerid
);
622 gtk_widget_set_sensitive(ql
->menuitems
[n
].w
,
623 ql
->menuitems
[n
].sensitive(ql
,
626 mii
= xmalloc(sizeof *mii
);
629 ql
->menuitems
[n
].handlerid
= g_signal_connect
630 (ql
->menuitems
[n
].w
, "activate",
631 G_CALLBACK(ql
->menuitems
[n
].activate
), mii
);
633 /* Update the menu according to context */
634 gtk_widget_show_all(ql
->menu
);
635 gtk_menu_popup(GTK_MENU(ql
->menu
), 0, 0, 0, 0,
636 event
->button
, event
->time
);
637 return TRUE
; /* hide the click from other widgets */
639 if(event
->type
== GDK_BUTTON_RELEASE
640 && event
->button
== 1) {
641 /* no modifiers: select this, unselect everything else, set last click
642 * +ctrl: flip selection of this, set last click
643 * +shift: select from last click to here, don't set last click
644 * +ctrl+shift: select from last click to here, set last click
646 switch(event
->state
& (GDK_SHIFT_MASK
|GDK_CONTROL_MASK
)) {
648 selection_empty(ql
->selection
);
649 selection_set(ql
->selection
, q
->id
, 1);
652 case GDK_CONTROL_MASK
:
653 selection_flip(ql
->selection
, q
->id
);
657 case GDK_SHIFT_MASK
|GDK_CONTROL_MASK
:
659 if(!(event
->state
& GDK_CONTROL_MASK
))
660 selection_empty(ql
->selection
);
661 selection_set(ql
->selection
, q
->id
, 1);
663 if(queue_before(ql
->last_click
, q
))
664 while(qq
!= ql
->last_click
) {
666 selection_set(ql
->selection
, qq
->id
, 1);
669 while(qq
!= ql
->last_click
) {
671 selection_set(ql
->selection
, qq
->id
, 1);
673 if(event
->state
& GDK_CONTROL_MASK
)
678 set_widget_states(ql
);
679 gtk_widget_queue_draw(ql
->mainlayout
);
681 return FALSE
; /* propagate */
684 /** @brief A button was pressed or released on the mainlayout
686 * For debugging only at the moment. */
687 static gboolean
mainlayout_button(GtkWidget
attribute((unused
)) *widget
,
688 GdkEventButton
attribute((unused
)) *event
,
689 gpointer
attribute((unused
)) user_data
) {
690 return FALSE
; /* propagate */
693 /** @brief Select all entries in a queue */
694 void queue_select_all(struct queuelike
*ql
) {
695 struct queue_entry
*qq
;
697 for(qq
= ql
->q
; qq
; qq
= qq
->next
)
698 selection_set(ql
->selection
, qq
->id
, 1);
700 set_widget_states(ql
);
703 /** @brief Deselect all entries in a queue */
704 void queue_select_none(struct queuelike
*ql
) {
705 struct queue_entry
*qq
;
707 for(qq
= ql
->q
; qq
; qq
= qq
->next
)
708 selection_set(ql
->selection
, qq
->id
, 0);
710 set_widget_states(ql
);
713 /** @brief Pop up properties for selected tracks */
714 void queue_properties(struct queuelike
*ql
) {
716 const struct queue_entry
*qq
;
719 for(qq
= ql
->q
; qq
; qq
= qq
->next
)
720 if(selection_selected(ql
->selection
, qq
->id
))
721 vector_append(&v
, (char *)qq
->track
);
723 properties(v
.nvec
, (const char **)v
.vec
);
726 /* Drag and drop rearrangement --------------------------------------------- */
728 /** @brief Return nonzero if @p is a draggable row
730 * Only tracks in the main queue are draggable (and the currently playing track
733 static int draggable_row(const struct queue_entry
*q
) {
734 return q
->ql
== &ql_queue
&& q
!= playing_track
;
737 /** @brief Called when a drag begings */
738 static void queue_drag_begin(GtkWidget
attribute((unused
)) *widget
,
739 GdkDragContext
attribute((unused
)) *dc
,
741 struct queue_entry
*q
= data
;
742 struct queuelike
*ql
= q
->ql
;
744 /* Make sure the playing track is not selected, since it cannot be dragged */
746 selection_set(ql
->selection
, playing_track
->id
, 0);
747 /* If the dragged item is not in the selection then change the selection to
749 if(!selection_selected(ql
->selection
, q
->id
)) {
750 selection_empty(ql
->selection
);
751 selection_set(ql
->selection
, q
->id
, 1);
752 set_widget_states(ql
);
754 /* Ignore the eventual button release */
755 ql
->swallow_release
= 1;
756 /* Create dropzones */
757 add_drag_targets(ql
);
760 /** @brief Convert @p id back into a queue entry and a screen row number */
761 static struct queue_entry
*findentry(struct queuelike
*ql
,
765 struct queue_entry
*q
;
768 for(q
= ql
->q
, row
= 0; q
&& strcmp(q
->id
, id
); q
= q
->next
, ++row
)
772 row
= playing_track ?
0 : -1;
774 if(rowp
) *rowp
= row
;
778 static void move_completed(void attribute((unused
)) *v
,
781 popup_protocol_error(0, error
);
784 /** @brief Called when data is dropped */
785 static gboolean
queue_drag_drop(GtkWidget
attribute((unused
)) *widget
,
786 GdkDragContext
*drag_context
,
787 gint
attribute((unused
)) x
,
788 gint
attribute((unused
)) y
,
790 gpointer user_data
) {
791 struct queuelike
*ql
= &ql_queue
;
792 const char *id
= user_data
;
794 struct queue_entry
*q
;
796 if(!id
|| (playing_track
&& !strcmp(id
, playing_track
->id
)))
799 for(q
= ql
->q
; q
; q
= q
->next
)
800 if(q
!= playing_track
&& selection_selected(ql
->selection
, q
->id
))
801 vector_append(&vec
, (char *)q
->id
);
802 disorder_eclient_moveafter(client
, id
, vec
.nvec
, (const char **)vec
.vec
,
803 move_completed
, 0/*v*/);
804 gtk_drag_finish(drag_context
, TRUE
, TRUE
, when
);
805 /* Destroy dropzones */
806 remove_drag_targets(ql
);
810 /** @brief Called when we enter, or move within, a drop zone */
811 static gboolean
queue_drag_motion(GtkWidget
attribute((unused
)) *widget
,
812 GdkDragContext
*drag_context
,
813 gint
attribute((unused
)) x
,
814 gint
attribute((unused
)) y
,
816 gpointer user_data
) {
817 struct queuelike
*ql
= &ql_queue
;
818 const char *id
= user_data
;
820 struct queue_entry
*q
= findentry(ql
, id
, &row
);
825 ql
->dragmark
= gtk_event_box_new();
826 g_signal_connect(ql
->dragmark
, "destroy",
827 G_CALLBACK(gtk_widget_destroyed
), &ql
->dragmark
);
828 gtk_widget_set_size_request(ql
->dragmark
, 10240, row ?
4 : 2);
829 gtk_widget_set_style(ql
->dragmark
, drag_style
);
830 gtk_layout_put(GTK_LAYOUT(ql
->mainlayout
), ql
->dragmark
, 0,
831 (row
+ 1) * ql
->mainrowheight
- !!row
);
833 gtk_layout_move(GTK_LAYOUT(ql
->mainlayout
), ql
->dragmark
, 0,
834 (row
+ 1) * ql
->mainrowheight
- !!row
);
835 gtk_widget_show(ql
->dragmark
);
836 gdk_drag_status(drag_context
, GDK_ACTION_MOVE
, when
);
839 /* ID has gone AWOL */
843 /** @brief Called when we leave a drop zone */
844 static void queue_drag_leave(GtkWidget
attribute((unused
)) *widget
,
845 GdkDragContext
attribute((unused
)) *drag_context
,
846 guint
attribute((unused
)) when
,
847 gpointer
attribute((unused
)) user_data
) {
848 struct queuelike
*ql
= &ql_queue
;
851 gtk_widget_hide(ql
->dragmark
);
854 /** @brief Add a drag target
855 * @param ql The queue-like (in practice this is always @ref ql_queue)
856 * @param y The Y coordinate to place the drag target
857 * @param id Track to insert moved tracks after, or NULL
859 * Adds a drop zone at Y coordinate @p y, which is assumed to lie between two
860 * tracks (or before the start of the queue or after the end of the queue). If
861 * tracks are dragged into this dropzone then they will be moved @em after
862 * track @p id, or to the start of the queue if @p id is NULL.
864 * We remember all the dropzones in @c ql->dropzones so they can be destroyed
867 static void add_drag_target(struct queuelike
*ql
, int y
,
872 eventbox
= gtk_event_box_new();
873 /* Make the target zone invisible */
874 gtk_event_box_set_visible_window(GTK_EVENT_BOX(eventbox
), FALSE
);
875 /* Make it large enough */
876 gtk_widget_set_size_request(eventbox
, 10240,
877 y ? ql
->mainrowheight
: ql
->mainrowheight
/ 2);
879 gtk_layout_put(GTK_LAYOUT(ql
->mainlayout
), eventbox
, 0,
880 y ? y
- ql
->mainrowheight
/ 2 : 0);
881 /* Mark it as capable of receiving drops */
882 gtk_drag_dest_set(eventbox
,
884 dragtargets
, NDRAGTARGETS
, GDK_ACTION_MOVE
);
885 g_signal_connect(eventbox
, "drag-drop",
886 G_CALLBACK(queue_drag_drop
), (char *)id
);
887 /* Monitor drag motion */
888 g_signal_connect(eventbox
, "drag-motion",
889 G_CALLBACK(queue_drag_motion
), (char *)id
);
890 g_signal_connect(eventbox
, "drag-leave",
891 G_CALLBACK(queue_drag_leave
), (char *)id
);
892 /* The widget needs to be shown to receive drags */
893 gtk_widget_show(eventbox
);
894 /* Remember the drag targets */
895 ql
->dropzones
[ql
->ndropzones
] = eventbox
;
896 g_signal_connect(eventbox
, "destroy",
897 G_CALLBACK(gtk_widget_destroyed
),
898 &ql
->dropzones
[ql
->ndropzones
]);
902 /** @brief Create dropzones for dragging into */
903 static void add_drag_targets(struct queuelike
*ql
) {
905 struct queue_entry
*q
;
907 /* Create an array to store the widgets */
908 ql
->dropzones
= xcalloc(ql
->nrows
, sizeof (GtkWidget
*));
911 /* Add a drag target before the first row provided it's not the playing
913 if(!playing_track
|| ql
->q
!= playing_track
)
914 add_drag_target(ql
, 0, 0);
915 /* Put a drag target at the bottom of every row */
916 for(q
= ql
->q
; q
; q
= q
->next
) {
917 y
+= ql
->mainrowheight
;
918 add_drag_target(ql
, y
, q
->id
);
922 /** @brief Remove the dropzones */
923 static void remove_drag_targets(struct queuelike
*ql
) {
926 for(n
= 0; n
< ql
->ndropzones
; ++n
) {
927 if(ql
->dropzones
[n
]) {
929 gtk_widget_destroy(ql
->dropzones
[n
]);
931 assert(ql
->dropzones
[n
] == 0);
935 /* Layout ------------------------------------------------------------------ */
937 /** @brief Redisplay a queue */
938 static void redisplay_queue(struct queuelike
*ql
) {
939 struct queue_entry
*q
;
945 int maxwidths
[MAXCOLUMNS
], x
, y
, titlerowheight
;
946 int totalwidth
= 10240; /* TODO: can we be less blunt */
948 D(("redisplay_queue"));
949 /* Eliminate all the existing widgets and start from scratch */
950 for(c
= children
= gtk_container_get_children(GTK_CONTAINER(ql
->mainlayout
));
953 /* Destroy both the label and the eventbox */
954 if(GTK_BIN(c
->data
)->child
) {
956 gtk_widget_destroy(GTK_BIN(c
->data
)->child
);
959 gtk_widget_destroy(GTK_WIDGET(c
->data
));
961 g_list_free(children
);
962 /* Adjust the row count */
963 for(q
= ql
->q
, ql
->nrows
= 0; q
; q
= q
->next
)
965 /* We need to create all the widgets before we can position them */
966 ql
->cells
= xcalloc(ql
->nrows
* (ql
->ncolumns
+ 1), sizeof *ql
->cells
);
967 /* Minimum width is given by the column headings */
968 for(col
= 0; col
< ql
->ncolumns
; ++col
) {
969 /* Reset size so we don't inherit last iteration's maximum size */
970 gtk_widget_set_size_request(GTK_BIN(ql
->titlecells
[col
])->child
, -1, -1);
971 gtk_widget_size_request(GTK_BIN(ql
->titlecells
[col
])->child
, &req
);
972 maxwidths
[col
] = req
.width
;
974 /* Find the vertical size of the title bar */
975 gtk_widget_size_request(GTK_BIN(ql
->titlecells
[0])->child
, &req
);
976 titlerowheight
= req
.height
;
979 /* Construct the widgets */
980 for(q
= ql
->q
, row
= 0; q
; q
= q
->next
, ++row
) {
981 /* Figure out the widget name for this row */
982 if(q
== playing_track
) style
= active_style
;
983 else style
= row
% 2 ? even_style
: odd_style
;
984 /* Make the widget for each column */
985 for(col
= 0; col
<= ql
->ncolumns
; ++col
) {
986 /* Create and store the widget */
987 if(col
< ql
->ncolumns
)
988 w
= get_queue_cell(ql
, q
, row
, col
, style
, &maxwidths
[col
]);
990 w
= get_padding_cell(style
);
991 ql
->cells
[row
* (ql
->ncolumns
+ 1) + col
] = w
;
992 /* Maybe mark it draggable */
993 if(draggable_row(q
)) {
994 gtk_drag_source_set(w
, GDK_BUTTON1_MASK
,
995 dragtargets
, NDRAGTARGETS
, GDK_ACTION_MOVE
);
996 g_signal_connect(w
, "drag-begin", G_CALLBACK(queue_drag_begin
), q
);
998 /* Catch button presses */
999 g_signal_connect(w
, "button-release-event",
1000 G_CALLBACK(queuelike_button_released
), q
);
1001 g_signal_connect(w
, "button-press-event",
1002 G_CALLBACK(queuelike_button_released
), q
);
1005 /* ...and of each row in the main layout */
1006 gtk_widget_size_request(GTK_BIN(ql
->cells
[0])->child
, &req
);
1007 ql
->mainrowheight
= req
.height
;
1008 /* Now we know the maximum width of each column we can set the size of
1009 * everything and position it */
1010 for(row
= 0, q
= ql
->q
; row
< ql
->nrows
; ++row
, q
= q
->next
) {
1012 for(col
= 0; col
< ql
->ncolumns
; ++col
) {
1013 w
= ql
->cells
[row
* (ql
->ncolumns
+ 1) + col
];
1014 gtk_widget_set_size_request(GTK_BIN(w
)->child
,
1015 maxwidths
[col
], -1);
1016 gtk_layout_put(GTK_LAYOUT(ql
->mainlayout
), w
, x
, y
);
1017 x
+= maxwidths
[col
];
1019 w
= ql
->cells
[row
* (ql
->ncolumns
+ 1) + col
];
1020 gtk_widget_set_size_request(GTK_BIN(w
)->child
,
1021 totalwidth
- x
, -1);
1022 gtk_layout_put(GTK_LAYOUT(ql
->mainlayout
), w
, x
, y
);
1023 y
+= ql
->mainrowheight
;
1028 for(col
= 0; col
< ql
->ncolumns
; ++col
) {
1029 gtk_widget_set_size_request(GTK_BIN(ql
->titlecells
[col
])->child
,
1030 maxwidths
[col
], -1);
1031 gtk_layout_move(GTK_LAYOUT(ql
->titlelayout
), ql
->titlecells
[col
], x
, 0);
1032 x
+= maxwidths
[col
];
1034 gtk_widget_set_size_request(GTK_BIN(ql
->titlecells
[col
])->child
,
1035 totalwidth
- x
, -1);
1036 gtk_layout_move(GTK_LAYOUT(ql
->titlelayout
), ql
->titlecells
[col
], x
, 0);
1037 /* Set the states */
1038 set_widget_states(ql
);
1039 /* Make sure it's all visible */
1040 gtk_widget_show_all(ql
->mainlayout
);
1041 gtk_widget_show_all(ql
->titlelayout
);
1042 /* Layouts might shrink to arrange for the area they shrink out of to be
1044 gtk_widget_queue_draw(ql
->mainlayout
);
1045 gtk_widget_queue_draw(ql
->titlelayout
);
1046 /* Adjust the size of the layout */
1047 gtk_layout_set_size(GTK_LAYOUT(ql
->mainlayout
), x
, y
);
1048 gtk_layout_set_size(GTK_LAYOUT(ql
->titlelayout
), x
, titlerowheight
);
1049 gtk_widget_set_size_request(ql
->titlelayout
, -1, titlerowheight
);
1052 /** @brief Called with new queue/recent contents */
1053 static void queuelike_completed(void *v
,
1055 struct queue_entry
*q
) {
1057 popup_protocol_error(0, error
);
1059 struct queuelike
*const ql
= v
;
1061 D(("queuelike_complete"));
1062 /* Install the new queue */
1063 update_queue(ql
, ql
->fixup ? ql
->fixup(q
) : q
);
1064 /* Update the display */
1065 redisplay_queue(ql
);
1068 /* Update sensitivity of main menu items */
1073 /** @brief Called with a new currently playing track */
1074 static void playing_completed(void attribute((unused
)) *v
,
1076 struct queue_entry
*q
) {
1078 popup_protocol_error(0, error
);
1080 D(("playing_completed"));
1082 /* Record when we got the playing track data so we know how old the 'sofar'
1084 time(&last_playing
);
1085 queuelike_completed(&ql_queue
, 0, actual_queue
);
1089 /** @brief Called when the queue is scrolled */
1090 static void queue_scrolled(GtkAdjustment
*adjustment
,
1091 gpointer user_data
) {
1092 GtkAdjustment
*titleadj
= user_data
;
1094 D(("queue_scrolled"));
1095 gtk_adjustment_set_value(titleadj
, adjustment
->value
);
1098 /** @brief Create a queuelike thing (queue/recent) */
1099 static GtkWidget
*queuelike(struct queuelike
*ql
,
1100 struct queue_entry
*(*fixup
)(struct queue_entry
*),
1101 void (*notify
)(void),
1102 struct queue_menuitem
*menuitems
,
1103 const struct column
*columns
,
1105 GtkWidget
*vbox
, *mainscroll
, *titlescroll
, *label
;
1106 GtkAdjustment
*mainadj
, *titleadj
;
1111 ql
->notify
= notify
;
1112 ql
->menuitems
= menuitems
;
1113 ql
->mainrowheight
= !0; /* else division by 0 */
1114 ql
->selection
= selection_new();
1115 ql
->columns
= columns
;
1116 ql
->ncolumns
= ncolumns
;
1117 /* Create the layouts */
1119 ql
->mainlayout
= gtk_layout_new(0, 0);
1120 gtk_widget_set_style(ql
->mainlayout
, layout_style
);
1122 ql
->titlelayout
= gtk_layout_new(0, 0);
1123 gtk_widget_set_style(ql
->titlelayout
, title_style
);
1124 /* Scroll the layouts */
1125 ql
->mainscroll
= mainscroll
= scroll_widget(ql
->mainlayout
);
1126 titlescroll
= scroll_widget(ql
->titlelayout
);
1127 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(titlescroll
),
1128 GTK_POLICY_NEVER
, GTK_POLICY_NEVER
);
1129 mainadj
= gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(mainscroll
));
1130 titleadj
= gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(titlescroll
));
1131 g_signal_connect(mainadj
, "changed", G_CALLBACK(queue_scrolled
), titleadj
);
1132 g_signal_connect(mainadj
, "value-changed", G_CALLBACK(queue_scrolled
), titleadj
);
1133 /* Fill the titles and put them anywhere */
1134 for(col
= 0; col
< ql
->ncolumns
; ++col
) {
1136 label
= gtk_label_new(ql
->columns
[col
].name
);
1137 gtk_misc_set_alignment(GTK_MISC(label
), ql
->columns
[col
].xalign
, 0);
1138 ql
->titlecells
[col
] = wrap_queue_cell(label
, title_style
, 0);
1139 gtk_layout_put(GTK_LAYOUT(ql
->titlelayout
), ql
->titlecells
[col
], 0, 0);
1141 ql
->titlecells
[col
] = get_padding_cell(title_style
);
1142 gtk_layout_put(GTK_LAYOUT(ql
->titlelayout
), ql
->titlecells
[col
], 0, 0);
1143 /* Pack the lot together in a vbox */
1145 vbox
= gtk_vbox_new(0, 0);
1146 gtk_box_pack_start(GTK_BOX(vbox
), titlescroll
, 0, 0, 0);
1147 gtk_box_pack_start(GTK_BOX(vbox
), mainscroll
, 1, 1, 0);
1148 /* Create the popup menu */
1150 ql
->menu
= gtk_menu_new();
1151 g_signal_connect(ql
->menu
, "destroy",
1152 G_CALLBACK(gtk_widget_destroyed
), &ql
->menu
);
1153 for(n
= 0; menuitems
[n
].name
; ++n
) {
1155 menuitems
[n
].w
= gtk_menu_item_new_with_label(menuitems
[n
].name
);
1156 gtk_menu_attach(GTK_MENU(ql
->menu
), menuitems
[n
].w
, 0, 1, n
, n
+ 1);
1158 g_object_set_data(G_OBJECT(vbox
), "type", (void *)&tabtype_queue
);
1159 g_object_set_data(G_OBJECT(vbox
), "queue", ql
);
1160 /* Catch button presses */
1161 g_signal_connect(ql
->mainlayout
, "button-release-event",
1162 G_CALLBACK(mainlayout_button
), ql
);
1164 g_signal_connect(ql
->mainlayout
, "button-press-event",
1165 G_CALLBACK(mainlayout_button
), ql
);
1167 set_tool_colors(ql
->menu
);
1171 /* Popup menu items -------------------------------------------------------- */
1173 /** @brief Count the number of items selected */
1174 static int queue_count_selected(const struct queuelike
*ql
) {
1175 return hash_count(ql
->selection
);
1178 /** @brief Count the number of items selected */
1179 static int queue_count_entries(const struct queuelike
*ql
) {
1181 const struct queue_entry
*q
;
1183 for(q
= ql
->q
; q
; q
= q
->next
)
1188 /** @brief Count the number of items selected, excluding the playing track if
1190 static int count_selected_nonplaying(const struct queuelike
*ql
) {
1191 int nselected
= queue_count_selected(ql
);
1193 if(ql
->q
== playing_track
&& selection_selected(ql
->selection
, ql
->q
->id
))
1198 /** @brief Determine whether the scratch option should be sensitive */
1199 static int scratch_sensitive(struct queuelike
attribute((unused
)) *ql
,
1200 struct queue_menuitem
attribute((unused
)) *m
,
1201 struct queue_entry
attribute((unused
)) *q
) {
1202 /* We can scratch if the playing track is selected */
1203 return (playing_track
1204 && (disorder_eclient_state(client
) & DISORDER_CONNECTED
)
1205 && selection_selected(ql
->selection
, playing_track
->id
)
1206 && right_scratchable(last_rights
, config
->username
, playing_track
));
1209 /** @brief Called when disorder_eclient_scratch completes */
1210 static void scratch_completed(void attribute((unused
)) *v
,
1211 const char *error
) {
1213 popup_protocol_error(0, error
);
1216 /** @brief Scratch the playing track */
1217 static void scratch_activate(GtkMenuItem
attribute((unused
)) *menuitem
,
1218 gpointer
attribute((unused
)) user_data
) {
1220 disorder_eclient_scratch(client
, playing_track
->id
, scratch_completed
, 0);
1223 /** @brief Determine whether the remove option should be sensitive */
1224 static int remove_sensitive(struct queuelike
*ql
,
1225 struct queue_menuitem
attribute((unused
)) *m
,
1226 struct queue_entry
*q
) {
1227 /* We can remove if we're hovering over a particular track or any non-playing
1228 * tracks are selected */
1229 return ((disorder_eclient_state(client
) & DISORDER_CONNECTED
)
1231 && q
!= playing_track
)
1232 || count_selected_nonplaying(ql
))
1233 && right_removable(last_rights
, config
->username
, q
));
1236 static void remove_completed(void attribute((unused
)) *v
,
1237 const char *error
) {
1239 popup_protocol_error(0, error
);
1242 /** @brief Remove selected track(s) */
1243 static void remove_activate(GtkMenuItem
attribute((unused
)) *menuitem
,
1244 gpointer user_data
) {
1245 const struct menuiteminfo
*mii
= user_data
;
1246 struct queue_entry
*q
= mii
->q
;
1247 struct queuelike
*ql
= mii
->ql
;
1249 if(count_selected_nonplaying(mii
->ql
)) {
1250 /* Remove selected tracks */
1251 for(q
= ql
->q
; q
; q
= q
->next
)
1252 if(selection_selected(ql
->selection
, q
->id
) && q
!= playing_track
)
1253 disorder_eclient_remove(client
, q
->id
, move_completed
, 0);
1255 /* Remove just the hovered track */
1256 disorder_eclient_remove(client
, q
->id
, remove_completed
, 0);
1259 /** @brief Determine whether the properties menu option should be sensitive */
1260 static int properties_sensitive(struct queuelike
*ql
,
1261 struct queue_menuitem
attribute((unused
)) *m
,
1262 struct queue_entry
attribute((unused
)) *q
) {
1263 /* "Properties" is sensitive if at least something is selected */
1264 return (hash_count(ql
->selection
) > 0
1265 && (disorder_eclient_state(client
) & DISORDER_CONNECTED
)
1266 && (last_rights
& RIGHT_PREFS
));
1269 /** @brief Pop up properties for the selected tracks */
1270 static void properties_activate(GtkMenuItem
attribute((unused
)) *menuitem
,
1271 gpointer user_data
) {
1272 const struct menuiteminfo
*mii
= user_data
;
1274 queue_properties(mii
->ql
);
1277 /** @brief Determine whether the select all menu option should be sensitive */
1278 static int selectall_sensitive(struct queuelike
*ql
,
1279 struct queue_menuitem
attribute((unused
)) *m
,
1280 struct queue_entry
attribute((unused
)) *q
) {
1281 /* Sensitive if there is anything to select */
1285 /** @brief Select all tracks */
1286 static void selectall_activate(GtkMenuItem
attribute((unused
)) *menuitem
,
1287 gpointer user_data
) {
1288 const struct menuiteminfo
*mii
= user_data
;
1289 queue_select_all(mii
->ql
);
1292 /** @brief Determine whether the select none menu option should be sensitive */
1293 static int selectnone_sensitive(struct queuelike
*ql
,
1294 struct queue_menuitem
attribute((unused
)) *m
,
1295 struct queue_entry
attribute((unused
)) *q
) {
1296 /* Sensitive if there is anything selected */
1297 return hash_count(ql
->selection
) != 0;
1300 /** @brief Select no tracks */
1301 static void selectnone_activate(GtkMenuItem
attribute((unused
)) *menuitem
,
1302 gpointer user_data
) {
1303 const struct menuiteminfo
*mii
= user_data
;
1304 queue_select_none(mii
->ql
);
1307 /** @brief Determine whether the play menu option should be sensitive */
1308 static int play_sensitive(struct queuelike
*ql
,
1309 struct queue_menuitem
attribute((unused
)) *m
,
1310 struct queue_entry
attribute((unused
)) *q
) {
1311 /* "Play" is sensitive if at least something is selected */
1312 return (hash_count(ql
->selection
) > 0
1313 && (disorder_eclient_state(client
) & DISORDER_CONNECTED
)
1314 && (last_rights
& RIGHT_PLAY
));
1317 /** @brief Play the selected tracks */
1318 static void play_activate(GtkMenuItem
attribute((unused
)) *menuitem
,
1319 gpointer user_data
) {
1320 const struct menuiteminfo
*mii
= user_data
;
1321 struct queue_entry
*q
= mii
->q
;
1322 struct queuelike
*ql
= mii
->ql
;
1324 if(queue_count_selected(ql
)) {
1325 /* Play selected tracks */
1326 for(q
= ql
->q
; q
; q
= q
->next
)
1327 if(selection_selected(ql
->selection
, q
->id
))
1328 disorder_eclient_play(client
, q
->track
, play_completed
, 0);
1330 /* Nothing is selected, so play the hovered track */
1331 disorder_eclient_play(client
, q
->track
, play_completed
, 0);
1334 /* The queue --------------------------------------------------------------- */
1336 /** @brief Fix up the queue by sticking the currently playing track on the front */
1337 static struct queue_entry
*fixup_queue(struct queue_entry
*q
) {
1342 actual_queue
->prev
= playing_track
;
1343 playing_track
->next
= actual_queue
;
1344 return playing_track
;
1346 return actual_queue
;
1349 /** @brief Adjust track played label
1351 * Called regularly to adjust the so-far played label (redrawing the whole
1352 * queue once a second makes disobedience occupy >10% of the CPU on my Athlon
1353 * which is ureasonable expensive) */
1354 static gboolean
adjust_sofar(gpointer
attribute((unused
)) data
) {
1355 if(playing_length_label
&& playing_track
)
1356 gtk_label_set_text(GTK_LABEL(playing_length_label
),
1357 text_length(playing_track
));
1361 /** @brief Popup menu for the queue
1363 * Properties first so that finger trouble is less dangerous. */
1364 static struct queue_menuitem queue_menu
[] = {
1365 { "Track properties", properties_activate
, properties_sensitive
, 0, 0 },
1366 { "Select all tracks", selectall_activate
, selectall_sensitive
, 0, 0 },
1367 { "Deselect all tracks", selectnone_activate
, selectnone_sensitive
, 0, 0 },
1368 { "Scratch track", scratch_activate
, scratch_sensitive
, 0, 0 },
1369 { "Remove track from queue", remove_activate
, remove_sensitive
, 0, 0 },
1373 /** @brief Called whenever @ref DISORDER_PLAYING or @ref DISORDER_TRACK_PAUSED changes
1375 * We monitor pause/resume as well as whether the track is playing in order to
1376 * keep the time played so far up to date correctly. See playing_completed().
1378 static void playing_changed(const char attribute((unused
)) *event
,
1379 void attribute((unused
)) *evendata
,
1380 void attribute((unused
)) *callbackdata
) {
1381 D(("playing_changed"));
1382 gtk_label_set_text(GTK_LABEL(report_label
), "updating playing track");
1383 disorder_eclient_playing(client
, playing_completed
, 0);
1386 /** @brief Create the queue widget */
1387 GtkWidget
*queue_widget(void) {
1388 D(("queue_widget"));
1389 /* Arrange periodic update of the so-far played field */
1390 g_timeout_add(1000/*ms*/, adjust_sofar
, 0);
1391 /* Arrange a callback whenever the playing state changes */
1392 event_register("playing-changed", playing_changed
, 0);
1393 event_register("pause-changed", playing_changed
, 0);
1394 event_register("queue-changed", queue_changed
, 0);
1395 /* We pass choose_update() as our notify function since the choose screen
1396 * marks tracks that are playing/in the queue. */
1397 return queuelike(&ql_queue
, fixup_queue
, choose_update
, queue_menu
,
1398 maincolumns
, NMAINCOLUMNS
);
1401 /** @brief Arrange an update of the queue widget
1403 * Called when a track is added to the queue, removed from the queue (by user
1404 * cmmand or because it is to be played) or moved within the queue
1406 void queue_changed(const char attribute((unused
)) *event
,
1407 void attribute((unused
)) *eventdata
,
1408 void attribute((unused
)) *callbackdata
) {
1409 D(("queue_changed"));
1410 gtk_label_set_text(GTK_LABEL(report_label
), "updating queue");
1411 disorder_eclient_queue(client
, queuelike_completed
, &ql_queue
);
1414 /* Recently played tracks -------------------------------------------------- */
1416 /** @brief Fix up the recently played list
1418 * It's in the wrong order! TODO fix this globally */
1419 static struct queue_entry
*fixup_recent(struct queue_entry
*q
) {
1420 struct queue_entry
*qr
= 0, *qn
;
1422 D(("fixup_recent"));
1425 /* Swap next/prev pointers */
1428 /* Remember last node for new head */
1436 /** @brief Pop-up menu for recently played list */
1437 static struct queue_menuitem recent_menu
[] = {
1438 { "Track properties", properties_activate
, properties_sensitive
,0, 0 },
1439 { "Select all tracks", selectall_activate
, selectall_sensitive
, 0, 0 },
1440 { "Deselect all tracks", selectnone_activate
, selectnone_sensitive
, 0, 0 },
1444 /** @brief Create the recently-played list */
1445 GtkWidget
*recent_widget(void) {
1446 D(("recent_widget"));
1447 event_register("recent-changed",
1450 return queuelike(&ql_recent
, fixup_recent
, 0, recent_menu
,
1451 maincolumns
, NMAINCOLUMNS
);
1454 /** @brief Update the recently played list
1456 * Called whenever a track is added to it or removed from it.
1458 static void recent_changed(const char attribute((unused
)) *event
,
1459 void attribute((unused
)) *eventdata
,
1460 void attribute((unused
)) *callbackdata
) {
1461 D(("recent_changed"));
1462 gtk_label_set_text(GTK_LABEL(report_label
), "updating recently played list");
1463 disorder_eclient_recent(client
, queuelike_completed
, &ql_recent
);
1466 /* Newly added tracks ------------------------------------------------------ */
1468 /** @brief Pop-up menu for recently played list */
1469 static struct queue_menuitem added_menu
[] = {
1470 { "Track properties", properties_activate
, properties_sensitive
, 0, 0 },
1471 { "Play track", play_activate
, play_sensitive
, 0, 0 },
1472 { "Select all tracks", selectall_activate
, selectall_sensitive
, 0, 0 },
1473 { "Deselect all tracks", selectnone_activate
, selectnone_sensitive
, 0, 0 },
1477 /** @brief Create the newly-added list */
1478 GtkWidget
*added_widget(void) {
1479 D(("added_widget"));
1480 event_register("added-changed", added_changed
, 0);
1481 return queuelike(&ql_added
, 0/*fixup*/, 0/*notify*/, added_menu
,
1482 addedcolumns
, NADDEDCOLUMNS
);
1485 /** @brief Called with an updated list of newly-added tracks
1487 * This is called with a raw list of track names but the rest of @ref
1488 * disobedience/queue.c requires @ref queue_entry structures with a valid and
1489 * unique @c id field. This function fakes it.
1491 static void new_completed(void *v
,
1493 int nvec
, char **vec
) {
1495 popup_protocol_error(0, error
);
1497 struct queuelist
*ql
= v
;
1498 /* Convert the vector result to a queue linked list */
1499 struct queue_entry
*q
, *qh
, *qlast
= 0, **qq
= &qh
;
1502 for(n
= 0; n
< nvec
; ++n
) {
1503 q
= xmalloc(sizeof *q
);
1512 queuelike_completed(ql
, 0, qh
);
1516 /** @brief Update the newly-added list */
1517 static void added_changed(const char attribute((unused
)) *event
,
1518 void attribute((unused
)) *eventdata
,
1519 void attribute((unused
)) *callbackdata
) {
1520 D(("added_changed"));
1522 gtk_label_set_text(GTK_LABEL(report_label
),
1523 "updating newly added track list");
1524 disorder_eclient_new_tracks(client
, new_completed
, 0/*all*/, &ql_added
);
1527 /* Main menu plumbing ------------------------------------------------------ */
1529 static int queue_properties_sensitive(GtkWidget
*w
) {
1530 return (!!queue_count_selected(g_object_get_data(G_OBJECT(w
), "queue"))
1531 && (disorder_eclient_state(client
) & DISORDER_CONNECTED
)
1532 && (last_rights
& RIGHT_PREFS
));
1535 static int queue_selectall_sensitive(GtkWidget
*w
) {
1536 return !!queue_count_entries(g_object_get_data(G_OBJECT(w
), "queue"));
1539 static int queue_selectnone_sensitive(GtkWidget
*w
) {
1540 struct queuelike
*const ql
= g_object_get_data(G_OBJECT(w
), "queue");
1542 return hash_count(ql
->selection
) != 0;
1545 static void queue_properties_activate(GtkWidget
*w
) {
1546 queue_properties(g_object_get_data(G_OBJECT(w
), "queue"));
1549 static void queue_selectall_activate(GtkWidget
*w
) {
1550 queue_select_all(g_object_get_data(G_OBJECT(w
), "queue"));
1553 static void queue_selectnone_activate(GtkWidget
*w
) {
1554 queue_select_none(g_object_get_data(G_OBJECT(w
), "queue"));
1557 static const struct tabtype tabtype_queue
= {
1558 queue_properties_sensitive
,
1559 queue_selectall_sensitive
,
1560 queue_selectnone_sensitive
,
1561 queue_properties_activate
,
1562 queue_selectall_activate
,
1563 queue_selectnone_activate
,
1566 /* Other entry points ------------------------------------------------------ */
1568 /** @brief Return nonzero if @p track is in the queue */
1569 int queued(const char *track
) {
1570 struct queue_entry
*q
;
1572 D(("queued %s", track
));
1573 for(q
= ql_queue
.q
; q
; q
= q
->next
)
1574 if(!strcmp(q
->track
, track
))
1584 indent-tabs-mode:nil