2 * This file is part of DisOrder
3 * Copyright (C) 2006 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.)
57 #include "disobedience.h"
59 /** @brief Horizontal padding for queue cells */
60 #define HCELLPADDING 4
62 /** @brief Vertical padding for queue cells */
63 #define VCELLPADDING 2
65 /* Queue management -------------------------------------------------------- */
76 static void add_drag_targets(struct queuelike
*ql
);
77 static void remove_drag_targets(struct queuelike
*ql
);
78 static void redisplay_queue(struct queuelike
*ql
);
79 static GtkWidget
*column_when(const struct queuelike
*ql
,
80 const struct queue_entry
*q
,
82 static GtkWidget
*column_who(const struct queuelike
*ql
,
83 const struct queue_entry
*q
,
85 static GtkWidget
*column_namepart(const struct queuelike
*ql
,
86 const struct queue_entry
*q
,
88 static GtkWidget
*column_length(const struct queuelike
*ql
,
89 const struct queue_entry
*q
,
91 static int draggable_row(const struct queue_entry
*q
);
93 static const struct tabtype tabtype_queue
; /* forward */
95 static const GtkTargetEntry dragtargets
[] = {
96 { (char *)"disobedience-queue", GTK_TARGET_SAME_APP
, 0 }
98 #define NDRAGTARGETS (int)(sizeof dragtargets / sizeof *dragtargets)
100 /** @brief Definition of a column */
102 const char *name
; /* Column name */
103 GtkWidget
*(*widget
)(const struct queuelike
*ql
,
104 const struct queue_entry
*q
,
105 const char *data
); /* Make a label for this column */
106 const char *data
; /* Data to pass to widget() */
107 gfloat xalign
; /* Alignment of the label */
110 /** @brief Table of columns
112 * Need this in the middle of the types for NCOLUMNS
114 static const struct column columns
[] = {
115 { "When", column_when
, 0, 1 },
116 { "Who", column_who
, 0, 0 },
117 { "Artist", column_namepart
, "artist", 0 },
118 { "Album", column_namepart
, "album", 0 },
119 { "Title", column_namepart
, "title", 0 },
120 { "Length", column_length
, 0, 1 }
123 /** @brief Number of columns */
124 #define NCOLUMNS (int)(sizeof columns / sizeof *columns)
126 /** @brief Data passed to menu item activation handlers */
127 struct menuiteminfo
{
128 struct queuelike
*ql
; /**< @brief which queue we're dealing with */
129 struct queue_entry
*q
; /**< @brief hovered entry or 0 */
132 /** @brief An item in the queue's popup menu */
134 /** @brief Menu item name */
137 /** @brief Called to activate the menu item
139 * The user data is the queue entry that the pointer was over when the menu
141 void (*activate
)(GtkMenuItem
*menuitem
,
144 /** @brief Called to determine whether the menu item is usable.
146 * Returns @c TRUE if it should be sensitive and @c FALSE otherwise. @p q
147 * points to the queue entry the pointer is over.
149 int (*sensitive
)(struct queuelike
*ql
,
151 struct queue_entry
*q
);
153 /** @brief Signal handler ID */
156 /** @brief Widget for menu item */
160 /** @brief A queue-like object
162 * There are (currently) two of these: @ref ql_queue and @ref ql_recent.
165 /** @brief Name of this queue */
168 /** @brief Called when an update completes */
169 void (*notify
)(void);
171 /** @brief Called to fix up the queue after update
172 * @param q The list passed back from the server
173 * @return Assigned to @c ql->q
175 struct queue_entry
*(*fixup
)(struct queue_entry
*q
);
178 GtkWidget
*mainlayout
; /**< @brief main layout */
179 GtkWidget
*mainscroll
; /**< @brief scroller for main layout */
180 GtkWidget
*titlelayout
; /**< @brief title layout */
181 GtkWidget
*titlecells
[NCOLUMNS
+ 1]; /**< @brief title cells */
182 GtkWidget
**cells
; /**< @brief all the cells */
183 GtkWidget
*menu
; /**< @brief popup menu */
184 struct menuitem
*menuitems
; /**< @brief menu items */
185 GtkWidget
*dragmark
; /**< @brief drag destination marker */
186 GtkWidget
**dropzones
; /**< @brief drag targets */
189 struct queue_entry
*q
; /**< @brief head of queue */
190 struct queue_entry
*last_click
; /**< @brief last click */
191 int nrows
; /**< @brief number of rows */
192 int mainrowheight
; /**< @brief height of one row */
193 hash
*selection
; /**< @brief currently selected items */
194 int swallow_release
; /**< @brief swallow button release from drag */
197 static struct queuelike ql_queue
; /**< @brief The main queue */
198 static struct queuelike ql_recent
; /*< @brief Recently-played tracks */
199 static struct queue_entry
*actual_queue
; /**< @brief actual queue */
200 static struct queue_entry
*playing_track
; /**< @brief currenty playing */
201 static time_t last_playing
= (time_t)-1; /**< @brief when last got playing */
202 static int namepart_lookups_outstanding
;
203 static int namepart_completions_deferred
; /* # of completions not processed */
204 static const struct cache_type cachetype_string
= { 3600 };
205 static const struct cache_type cachetype_integer
= { 3600 };
206 static GtkWidget
*playing_length_label
;
208 /* Debugging --------------------------------------------------------------- */
211 static void describe_widget(const char *name
, GtkWidget
*w
, int indent
) {
215 fprintf(stderr
, "%*s[%s]: '%s'\n", indent
, "",
216 name
, gtk_widget_get_name(w
));
217 gdk_window_get_position(w
->window
, &wx
, &wy
);
218 gdk_drawable_get_size(GDK_DRAWABLE(w
->window
), &ww
, &wh
);
219 fprintf(stderr
, "%*s window %p: %dx%d at %dx%d\n",
220 indent
, "", w
->window
, ww
, wh
, wx
, wy
);
223 static void dump_layout(const struct queuelike
*ql
) {
227 const struct queue_entry
*q
;
229 describe_widget("mainscroll", ql
->mainscroll
, 0);
230 describe_widget("mainlayout", ql
->mainlayout
, 1);
231 for(q
= ql
->q
, row
= 0; q
; q
= q
->next
, ++row
)
232 for(col
= 0; col
< NCOLUMNS
+ 1; ++col
)
233 if((w
= ql
->cells
[row
* (NCOLUMNS
+ 1) + col
])) {
234 sprintf(s
, "%dx%d", row
, col
);
235 describe_widget(s
, w
, 2);
236 if(GTK_BIN(w
)->child
)
237 describe_widget(0, w
, 3);
242 /* Track detail lookup ----------------------------------------------------- */
244 /** @brief Called when a namepart lookup has completed or failed */
245 static void namepart_completed_or_failed(void) {
246 D(("namepart_completed_or_failed"));
247 --namepart_lookups_outstanding
;
248 if(!namepart_lookups_outstanding
|| namepart_completions_deferred
> 24) {
249 redisplay_queue(&ql_queue
);
250 redisplay_queue(&ql_recent
);
251 namepart_completions_deferred
= 0;
255 /** @brief Called when A namepart lookup has completed */
256 static void namepart_completed(void *v
, const char *value
) {
257 struct callbackdata
*cbd
= v
;
259 D(("namepart_completed"));
260 cache_put(&cachetype_string
, cbd
->u
.key
, value
);
261 ++namepart_completions_deferred
;
262 namepart_completed_or_failed();
265 /** @brief Called when a length lookup has completed */
266 static void length_completed(void *v
, long l
) {
267 struct callbackdata
*cbd
= v
;
270 D(("namepart_completed"));
271 value
= xmalloc(sizeof *value
);
273 cache_put(&cachetype_integer
, cbd
->u
.key
, value
);
274 ++namepart_completions_deferred
;
275 namepart_completed_or_failed();
278 /** @brief Called when a length or namepart lookup has failed */
279 static void namepart_protocol_error(
280 struct callbackdata
attribute((unused
)) *cbd
,
281 int attribute((unused
)) code
,
283 D(("namepart_protocol_error"));
284 gtk_label_set_text(GTK_LABEL(report_label
), msg
);
285 namepart_completed_or_failed();
288 /** @brief Arrange to fill in a namepart cache entry */
289 static void namepart_fill(const char *track
,
293 struct callbackdata
*cbd
;
295 ++namepart_lookups_outstanding
;
296 cbd
= xmalloc(sizeof *cbd
);
297 cbd
->onerror
= namepart_protocol_error
;
299 disorder_eclient_namepart(client
, namepart_completed
,
300 track
, context
, part
, cbd
);
303 /** @brief Look up a namepart
305 * If it is in the cache then just return its value. If not then look it up
306 * and arrange for the queues to be updated when its value is available. */
307 static const char *namepart(const char *track
,
313 D(("namepart %s %s %s", track
, context
, part
));
314 byte_xasprintf(&key
, "namepart context=%s part=%s track=%s",
315 context
, part
, track
);
316 value
= cache_get(&cachetype_string
, key
);
319 /* stick a value in the cache so we don't issue another lookup if we
321 cache_put(&cachetype_string
, key
, value
= "?");
322 namepart_fill(track
, context
, part
, key
);
327 /** @brief Called from @ref disobedience/properties.c when we know a name part has changed */
328 void namepart_update(const char *track
,
333 byte_xasprintf(&key
, "namepart context=%s part=%s track=%s",
334 context
, part
, track
);
335 /* Only refetch if it's actually in the cache */
336 if(cache_get(&cachetype_string
, key
))
337 namepart_fill(track
, context
, part
, key
);
340 /** @brief Look up a track length
342 * If it is in the cache then just return its value. If not then look it up
343 * and arrange for the queues to be updated when its value is available. */
344 static long getlength(const char *track
) {
347 struct callbackdata
*cbd
;
348 static const long bogus
= -1;
350 D(("getlength %s", track
));
351 byte_xasprintf(&key
, "length track=%s", track
);
352 value
= cache_get(&cachetype_integer
, key
);
354 D(("deferring..."));;
355 cache_put(&cachetype_integer
, key
, value
= &bogus
);
356 ++namepart_lookups_outstanding
;
357 cbd
= xmalloc(sizeof *cbd
);
358 cbd
->onerror
= namepart_protocol_error
;
360 disorder_eclient_length(client
, length_completed
, track
, cbd
);
365 /* Column constructors ----------------------------------------------------- */
367 /** @brief Format the 'when' column */
368 static GtkWidget
*column_when(const struct queuelike
attribute((unused
)) *ql
,
369 const struct queue_entry
*q
,
370 const char attribute((unused
)) *data
) {
377 case playing_isscratch
:
378 case playing_unplayed
:
383 case playing_no_player
:
385 case playing_scratched
:
386 case playing_started
:
388 case playing_quitting
:
396 strftime(when
, sizeof when
, "%H:%M", localtime_r(&t
, &tm
));
400 return gtk_label_new(when
);
403 /** @brief Format the 'who' column */
404 static GtkWidget
*column_who(const struct queuelike
attribute((unused
)) *ql
,
405 const struct queue_entry
*q
,
406 const char attribute((unused
)) *data
) {
409 return gtk_label_new(q
->submitter ? q
->submitter
: "");
412 /** @brief Format one of the track name columns */
413 static GtkWidget
*column_namepart(const struct queuelike
414 attribute((unused
)) *ql
,
415 const struct queue_entry
*q
,
417 D(("column_namepart"));
419 return gtk_label_new(namepart(q
->track
, "display", data
));
422 /** @brief Compute the length field */
423 static const char *text_length(const struct queue_entry
*q
) {
426 char *played
= 0, *length
= 0;
428 /* Work out what to say for the length */
429 l
= getlength(q
->track
);
431 byte_xasprintf(&length
, "%ld:%02ld", l
/ 60, l
% 60);
433 byte_xasprintf(&length
, "?:??");
434 /* For the currently playing track we want to report how much of the track
436 if(q
== playing_track
) {
437 /* log_state() arranges that we re-get the playing data whenever the
438 * pause/resume state changes */
439 if(last_state
& DISORDER_TRACK_PAUSED
)
440 l
= playing_track
->sofar
;
443 l
= playing_track
->sofar
+ (now
- last_playing
);
445 byte_xasprintf(&played
, "%ld:%02ld/%s", l
/ 60, l
% 60, length
);
451 /** @brief Format the length column */
452 static GtkWidget
*column_length(const struct queuelike
attribute((unused
)) *ql
,
453 const struct queue_entry
*q
,
454 const char attribute((unused
)) *data
) {
455 D(("column_length"));
456 if(q
== playing_track
) {
457 assert(!playing_length_label
);
459 playing_length_label
= gtk_label_new(text_length(q
));
460 /* Zot playing_length_label when it is destroyed */
461 g_signal_connect(playing_length_label
, "destroy",
462 G_CALLBACK(gtk_widget_destroyed
), &playing_length_label
);
463 return playing_length_label
;
466 return gtk_label_new(text_length(q
));
471 /** @brief Apply a new queue contents, transferring the selection from the old value */
472 static void update_queue(struct queuelike
*ql
, struct queue_entry
*newq
) {
473 struct queue_entry
*q
;
476 /* Propagate last_click across the change */
478 for(q
= newq
; q
; q
= q
->next
) {
479 if(!strcmp(q
->id
, ql
->last_click
->id
))
484 /* Tell every queue entry which queue owns it */
485 for(q
= newq
; q
; q
= q
->next
)
487 /* Switch to the new queue */
489 /* Clean up any selected items that have fallen off */
490 for(q
= ql
->q
; q
; q
= q
->next
)
491 selection_live(ql
->selection
, q
->id
);
492 selection_cleanup(ql
->selection
);
495 /** @brief Wrap up a widget for putting into the queue or title */
496 static GtkWidget
*wrap_queue_cell(GtkWidget
*label
,
502 D(("wrap_queue_cell"));
503 /* Padding should be in the label so there are no gaps in the
505 gtk_misc_set_padding(GTK_MISC(label
), HCELLPADDING
, VCELLPADDING
);
506 /* Event box is just to hold a background color */
508 bg
= gtk_event_box_new();
509 gtk_container_add(GTK_CONTAINER(bg
), label
);
511 /* Update maximum width */
512 gtk_widget_size_request(label
, &req
);
513 if(req
.width
> *wp
) *wp
= req
.width
;
515 /* Set widget names */
516 gtk_widget_set_name(bg
, name
);
517 gtk_widget_set_name(label
, name
);
521 /** @brief Create the wrapped widget for a cell in the queue display */
522 static GtkWidget
*get_queue_cell(struct queuelike
*ql
,
523 const struct queue_entry
*q
,
529 D(("get_queue_cell %d %d", row
, col
));
530 label
= columns
[col
].widget(ql
, q
, columns
[col
].data
);
531 gtk_misc_set_alignment(GTK_MISC(label
), columns
[col
].xalign
, 0);
532 return wrap_queue_cell(label
, name
, wp
);
535 /** @brief Add a padding cell to the end of a row */
536 static GtkWidget
*get_padding_cell(const char *name
) {
537 D(("get_padding_cell"));
539 return wrap_queue_cell(gtk_label_new(""), name
, 0);
542 /* User button press and menu ---------------------------------------------- */
544 /** @brief Update widget states in order to reflect the selection status */
545 static void set_widget_states(struct queuelike
*ql
) {
546 struct queue_entry
*q
;
549 for(q
= ql
->q
, row
= 0; q
; q
= q
->next
, ++row
) {
550 for(col
= 0; col
< NCOLUMNS
+ 1; ++col
)
551 gtk_widget_set_state(ql
->cells
[row
* (NCOLUMNS
+ 1) + col
],
552 selection_selected(ql
->selection
, q
->id
) ?
553 GTK_STATE_SELECTED
: GTK_STATE_NORMAL
);
555 /* Might need to change sensitivity of 'Properties' in main menu */
559 /** @brief Ordering function for queue entries */
560 static int queue_before(const struct queue_entry
*a
,
561 const struct queue_entry
*b
) {
567 /** @brief A button was pressed and released */
568 static gboolean
queuelike_button_released(GtkWidget
attribute((unused
)) *widget
,
569 GdkEventButton
*event
,
570 gpointer user_data
) {
571 struct queue_entry
*q
= user_data
, *qq
;
572 struct queuelike
*ql
= q
->ql
;
573 struct menuiteminfo
*mii
;
576 /* Might be a release left over from a drag */
577 if(ql
->swallow_release
) {
578 ql
->swallow_release
= 0;
579 return FALSE
; /* propagate */
582 if(event
->type
== GDK_BUTTON_PRESS
583 && event
->button
== 3) {
584 /* Right button click.
585 * If the current item is not selected then switch the selection to just
587 if(q
&& !selection_selected(ql
->selection
, q
->id
)) {
588 selection_empty(ql
->selection
);
589 selection_set(ql
->selection
, q
->id
, 1);
591 set_widget_states(ql
);
593 /* Set the sensitivity of each menu item and (re-)establish the signal
595 for(n
= 0; ql
->menuitems
[n
].name
; ++n
) {
596 if(ql
->menuitems
[n
].handlerid
)
597 g_signal_handler_disconnect(ql
->menuitems
[n
].w
,
598 ql
->menuitems
[n
].handlerid
);
599 gtk_widget_set_sensitive(ql
->menuitems
[n
].w
,
600 ql
->menuitems
[n
].sensitive(ql
,
603 mii
= xmalloc(sizeof *mii
);
606 ql
->menuitems
[n
].handlerid
= g_signal_connect
607 (ql
->menuitems
[n
].w
, "activate",
608 G_CALLBACK(ql
->menuitems
[n
].activate
), mii
);
610 /* Update the menu according to context */
611 gtk_widget_show_all(ql
->menu
);
612 gtk_menu_popup(GTK_MENU(ql
->menu
), 0, 0, 0, 0,
613 event
->button
, event
->time
);
614 return TRUE
; /* hide the click from other widgets */
616 if(event
->type
== GDK_BUTTON_RELEASE
617 && event
->button
== 1) {
618 /* no modifiers: select this, unselect everything else, set last click
619 * +ctrl: flip selection of this, set last click
620 * +shift: select from last click to here, don't set last click
621 * +ctrl+shift: select from last click to here, set last click
623 switch(event
->state
& (GDK_SHIFT_MASK
|GDK_CONTROL_MASK
)) {
625 selection_empty(ql
->selection
);
626 selection_set(ql
->selection
, q
->id
, 1);
629 case GDK_CONTROL_MASK
:
630 selection_flip(ql
->selection
, q
->id
);
634 case GDK_SHIFT_MASK
|GDK_CONTROL_MASK
:
636 if(!(event
->state
& GDK_CONTROL_MASK
))
637 selection_empty(ql
->selection
);
638 selection_set(ql
->selection
, q
->id
, 1);
640 if(queue_before(ql
->last_click
, q
))
641 while(qq
!= ql
->last_click
) {
643 selection_set(ql
->selection
, qq
->id
, 1);
646 while(qq
!= ql
->last_click
) {
648 selection_set(ql
->selection
, qq
->id
, 1);
650 if(event
->state
& GDK_CONTROL_MASK
)
655 set_widget_states(ql
);
656 gtk_widget_queue_draw(ql
->mainlayout
);
658 return FALSE
; /* propagate */
661 /** @brief A button was pressed or released on the mainlayout
663 * For debugging only at the moment. */
664 static gboolean
mainlayout_button(GtkWidget
attribute((unused
)) *widget
,
665 GdkEventButton
attribute((unused
)) *event
,
666 gpointer
attribute((unused
)) user_data
) {
667 return FALSE
; /* propagate */
670 /** @brief Select all entries in a queue */
671 void queue_select_all(struct queuelike
*ql
) {
672 struct queue_entry
*qq
;
674 for(qq
= ql
->q
; qq
; qq
= qq
->next
)
675 selection_set(ql
->selection
, qq
->id
, 1);
677 set_widget_states(ql
);
680 /** @brief Pop up properties for selected tracks */
681 void queue_properties(struct queuelike
*ql
) {
683 const struct queue_entry
*qq
;
686 for(qq
= ql
->q
; qq
; qq
= qq
->next
)
687 if(selection_selected(ql
->selection
, qq
->id
))
688 vector_append(&v
, (char *)qq
->track
);
690 properties(v
.nvec
, v
.vec
);
693 /* Drag and drop rearrangement --------------------------------------------- */
695 /** @brief Return nonzero if @p is a draggable row */
696 static int draggable_row(const struct queue_entry
*q
) {
697 return q
->ql
== &ql_queue
&& q
!= playing_track
;
700 /** @brief Called when a drag begings */
701 static void queue_drag_begin(GtkWidget
attribute((unused
)) *widget
,
702 GdkDragContext
attribute((unused
)) *dc
,
704 struct queue_entry
*q
= data
;
705 struct queuelike
*ql
= q
->ql
;
707 /* Make sure the playing track is not selected, since it cannot be dragged */
709 selection_set(ql
->selection
, playing_track
->id
, 0);
710 /* If the dragged item is not in the selection then change the selection to
712 if(!selection_selected(ql
->selection
, q
->id
)) {
713 selection_empty(ql
->selection
);
714 selection_set(ql
->selection
, q
->id
, 1);
715 set_widget_states(ql
);
717 /* Ignore the eventual button release */
718 ql
->swallow_release
= 1;
719 /* Create dropzones */
720 add_drag_targets(ql
);
723 /** @brief Convert @p id back into a queue entry and a screen row number */
724 static struct queue_entry
*findentry(struct queuelike
*ql
,
728 struct queue_entry
*q
;
731 for(q
= ql
->q
, row
= 0; q
&& strcmp(q
->id
, id
); q
= q
->next
, ++row
)
735 row
= playing_track ?
0 : -1;
737 if(rowp
) *rowp
= row
;
741 /** @brief Called when data is dropped */
742 static gboolean
queue_drag_drop(GtkWidget
attribute((unused
)) *widget
,
743 GdkDragContext
*drag_context
,
744 gint
attribute((unused
)) x
,
745 gint
attribute((unused
)) y
,
747 gpointer user_data
) {
748 struct queuelike
*ql
= &ql_queue
;
749 const char *id
= user_data
;
751 struct queue_entry
*q
;
753 if(!id
|| (playing_track
&& !strcmp(id
, playing_track
->id
)))
756 for(q
= ql
->q
; q
; q
= q
->next
)
757 if(q
!= playing_track
&& selection_selected(ql
->selection
, q
->id
))
758 vector_append(&vec
, (char *)q
->id
);
759 disorder_eclient_moveafter(client
, id
, vec
.nvec
, (const char **)vec
.vec
,
760 0/*completed*/, 0/*v*/);
761 gtk_drag_finish(drag_context
, TRUE
, TRUE
, when
);
762 /* Destroy dropzones */
763 remove_drag_targets(ql
);
767 /** @brief Called when we enter, or move within, a drop zone */
768 static gboolean
queue_drag_motion(GtkWidget
attribute((unused
)) *widget
,
769 GdkDragContext
*drag_context
,
770 gint
attribute((unused
)) x
,
771 gint
attribute((unused
)) y
,
773 gpointer user_data
) {
774 struct queuelike
*ql
= &ql_queue
;
775 const char *id
= user_data
;
777 struct queue_entry
*q
= findentry(ql
, id
, &row
);
782 ql
->dragmark
= gtk_event_box_new();
783 g_signal_connect(ql
->dragmark
, "destroy",
784 G_CALLBACK(gtk_widget_destroyed
), &ql
->dragmark
);
785 gtk_widget_set_size_request(ql
->dragmark
, 10240, row ?
4 : 2);
786 gtk_widget_set_name(ql
->dragmark
, "queue-drag");
787 gtk_layout_put(GTK_LAYOUT(ql
->mainlayout
), ql
->dragmark
, 0,
788 (row
+ 1) * ql
->mainrowheight
- !!row
);
790 gtk_layout_move(GTK_LAYOUT(ql
->mainlayout
), ql
->dragmark
, 0,
791 (row
+ 1) * ql
->mainrowheight
- !!row
);
792 gtk_widget_show(ql
->dragmark
);
793 gdk_drag_status(drag_context
, GDK_ACTION_MOVE
, when
);
796 /* ID has gone AWOL */
800 /** @brief Called when we leave a drop zone */
801 static void queue_drag_leave(GtkWidget
attribute((unused
)) *widget
,
802 GdkDragContext
attribute((unused
)) *drag_context
,
803 guint
attribute((unused
)) when
,
804 gpointer
attribute((unused
)) user_data
) {
805 struct queuelike
*ql
= &ql_queue
;
808 gtk_widget_hide(ql
->dragmark
);
811 /** @brief Add a drag target at position @p y
813 * @p id is the track to insert the moved tracks after, and might be 0 to
814 * insert before the start. */
815 static void add_drag_target(struct queuelike
*ql
, int y
, int row
,
819 assert(ql
->dropzones
[row
] == 0);
821 eventbox
= gtk_event_box_new();
822 /* Make the target zone invisible */
823 gtk_event_box_set_visible_window(GTK_EVENT_BOX(eventbox
), FALSE
);
824 /* Make it large enough */
825 gtk_widget_set_size_request(eventbox
, 10240,
826 y ? ql
->mainrowheight
: ql
->mainrowheight
/ 2);
828 gtk_layout_put(GTK_LAYOUT(ql
->mainlayout
), eventbox
, 0,
829 y ? y
- ql
->mainrowheight
/ 2 : 0);
830 /* Mark it as capable of receiving drops */
831 gtk_drag_dest_set(eventbox
,
833 dragtargets
, NDRAGTARGETS
, GDK_ACTION_MOVE
);
834 g_signal_connect(eventbox
, "drag-drop",
835 G_CALLBACK(queue_drag_drop
), (char *)id
);
836 /* Monitor drag motion */
837 g_signal_connect(eventbox
, "drag-motion",
838 G_CALLBACK(queue_drag_motion
), (char *)id
);
839 g_signal_connect(eventbox
, "drag-leave",
840 G_CALLBACK(queue_drag_leave
), (char *)id
);
841 /* The widget needs to be shown to receive drags */
842 gtk_widget_show(eventbox
);
843 /* Remember the drag targets */
844 ql
->dropzones
[row
] = eventbox
;
845 g_signal_connect(eventbox
, "destroy",
846 G_CALLBACK(gtk_widget_destroyed
), &ql
->dropzones
[row
]);
849 /** @brief Create dropzones for dragging into */
850 static void add_drag_targets(struct queuelike
*ql
) {
852 struct queue_entry
*q
;
854 /* Create an array to store the widgets */
855 ql
->dropzones
= xcalloc(ql
->nrows
, sizeof (GtkWidget
*));
857 /* Add a drag target before the first row provided it's not the playing
859 if(!playing_track
|| ql
->q
!= playing_track
)
860 add_drag_target(ql
, 0, 0, 0);
861 /* Put a drag target at the bottom of every row */
862 for(q
= ql
->q
, row
= 0; q
; q
= q
->next
, ++row
) {
863 y
+= ql
->mainrowheight
;
864 add_drag_target(ql
, y
, row
, q
->id
);
868 /** @brief Remove the dropzones */
869 static void remove_drag_targets(struct queuelike
*ql
) {
872 for(row
= 0; row
< ql
->nrows
; ++row
) {
873 if(ql
->dropzones
[row
]) {
875 gtk_widget_destroy(ql
->dropzones
[row
]);
877 assert(ql
->dropzones
[row
] == 0);
881 /* Layout ------------------------------------------------------------------ */
883 /** @brief Redisplay a queue */
884 static void redisplay_queue(struct queuelike
*ql
) {
885 struct queue_entry
*q
;
891 int maxwidths
[NCOLUMNS
], x
, y
, titlerowheight
;
892 int totalwidth
= 10240; /* TODO: can we be less blunt */
894 D(("redisplay_queue"));
895 /* Eliminate all the existing widgets and start from scratch */
896 for(c
= children
= gtk_container_get_children(GTK_CONTAINER(ql
->mainlayout
));
899 /* Destroy both the label and the eventbox */
900 if(GTK_BIN(c
->data
)->child
) {
902 gtk_widget_destroy(GTK_BIN(c
->data
)->child
);
905 gtk_widget_destroy(GTK_WIDGET(c
->data
));
907 g_list_free(children
);
908 /* Adjust the row count */
909 for(q
= ql
->q
, ql
->nrows
= 0; q
; q
= q
->next
)
911 /* We need to create all the widgets before we can position them */
912 ql
->cells
= xcalloc(ql
->nrows
* (NCOLUMNS
+ 1), sizeof *ql
->cells
);
913 /* Minimum width is given by the column headings */
914 for(col
= 0; col
< NCOLUMNS
; ++col
) {
915 /* Reset size so we don't inherit last iteration's maximum size */
916 gtk_widget_set_size_request(GTK_BIN(ql
->titlecells
[col
])->child
, -1, -1);
917 gtk_widget_size_request(GTK_BIN(ql
->titlecells
[col
])->child
, &req
);
918 maxwidths
[col
] = req
.width
;
920 /* Find the vertical size of the title bar */
921 gtk_widget_size_request(GTK_BIN(ql
->titlecells
[0])->child
, &req
);
922 titlerowheight
= req
.height
;
925 /* Construct the widgets */
926 for(q
= ql
->q
, row
= 0; q
; q
= q
->next
, ++row
) {
927 /* Figure out the widget name for this row */
928 if(q
== playing_track
) name
= "row-playing";
929 else name
= row
% 2 ?
"row-even" : "row-odd";
930 /* Make the widget for each column */
931 for(col
= 0; col
<= NCOLUMNS
; ++col
) {
932 /* Create and store the widget */
934 w
= get_queue_cell(ql
, q
, row
, col
, name
, &maxwidths
[col
]);
936 w
= get_padding_cell(name
);
937 ql
->cells
[row
* (NCOLUMNS
+ 1) + col
] = w
;
938 /* Maybe mark it draggable */
939 if(draggable_row(q
)) {
940 gtk_drag_source_set(w
, GDK_BUTTON1_MASK
,
941 dragtargets
, NDRAGTARGETS
, GDK_ACTION_MOVE
);
942 g_signal_connect(w
, "drag-begin", G_CALLBACK(queue_drag_begin
), q
);
944 /* Catch button presses */
945 g_signal_connect(w
, "button-release-event",
946 G_CALLBACK(queuelike_button_released
), q
);
947 g_signal_connect(w
, "button-press-event",
948 G_CALLBACK(queuelike_button_released
), q
);
951 /* ...and of each row in the main layout */
952 gtk_widget_size_request(GTK_BIN(ql
->cells
[0])->child
, &req
);
953 ql
->mainrowheight
= req
.height
;
954 /* Now we know the maximum width of each column we can set the size of
955 * everything and position it */
956 for(row
= 0, q
= ql
->q
; row
< ql
->nrows
; ++row
, q
= q
->next
) {
958 for(col
= 0; col
< NCOLUMNS
; ++col
) {
959 w
= ql
->cells
[row
* (NCOLUMNS
+ 1) + col
];
960 gtk_widget_set_size_request(GTK_BIN(w
)->child
,
962 gtk_layout_put(GTK_LAYOUT(ql
->mainlayout
), w
, x
, y
);
965 w
= ql
->cells
[row
* (NCOLUMNS
+ 1) + col
];
966 gtk_widget_set_size_request(GTK_BIN(w
)->child
,
968 gtk_layout_put(GTK_LAYOUT(ql
->mainlayout
), w
, x
, y
);
969 y
+= ql
->mainrowheight
;
974 for(col
= 0; col
< NCOLUMNS
; ++col
) {
975 gtk_widget_set_size_request(GTK_BIN(ql
->titlecells
[col
])->child
,
977 gtk_layout_move(GTK_LAYOUT(ql
->titlelayout
), ql
->titlecells
[col
], x
, 0);
980 gtk_widget_set_size_request(GTK_BIN(ql
->titlecells
[col
])->child
,
982 gtk_layout_move(GTK_LAYOUT(ql
->titlelayout
), ql
->titlecells
[col
], x
, 0);
984 set_widget_states(ql
);
985 /* Make sure it's all visible */
986 gtk_widget_show_all(ql
->mainlayout
);
987 gtk_widget_show_all(ql
->titlelayout
);
988 /* Layouts might shrink to arrange for the area they shrink out of to be
990 gtk_widget_queue_draw(ql
->mainlayout
);
991 gtk_widget_queue_draw(ql
->titlelayout
);
992 /* Adjust the size of the layout */
993 gtk_layout_set_size(GTK_LAYOUT(ql
->mainlayout
), x
, y
);
994 gtk_layout_set_size(GTK_LAYOUT(ql
->titlelayout
), x
, titlerowheight
);
995 gtk_widget_set_size_request(ql
->titlelayout
, -1, titlerowheight
);
998 /** @brief Called with new queue/recent contents */
999 static void queuelike_completed(void *v
, struct queue_entry
*q
) {
1000 struct callbackdata
*cbd
= v
;
1001 struct queuelike
*ql
= cbd
->u
.ql
;
1003 D(("queuelike_complete"));
1004 /* Install the new queue */
1005 update_queue(ql
, ql
->fixup(q
));
1006 /* Update the display */
1007 redisplay_queue(ql
);
1010 /* Update sensitivity of main menu items */
1014 /** @brief Called with a new currently playing track */
1015 static void playing_completed(void attribute((unused
)) *v
,
1016 struct queue_entry
*q
) {
1017 struct callbackdata cbd
;
1018 D(("playing_completed"));
1020 /* Record when we got the playing track data so we know how old the 'sofar'
1022 time(&last_playing
);
1023 cbd
.u
.ql
= &ql_queue
;
1024 queuelike_completed(&cbd
, actual_queue
);
1027 /** @brief Called when the queue is scrolled */
1028 static void queue_scrolled(GtkAdjustment
*adjustment
,
1029 gpointer user_data
) {
1030 GtkAdjustment
*titleadj
= user_data
;
1032 D(("queue_scrolled"));
1033 gtk_adjustment_set_value(titleadj
, adjustment
->value
);
1036 /** @brief Create a queuelike thing (queue/recent) */
1037 static GtkWidget
*queuelike(struct queuelike
*ql
,
1038 struct queue_entry
*(*fixup
)(struct queue_entry
*),
1039 void (*notify
)(void),
1040 struct menuitem
*menuitems
,
1042 GtkWidget
*vbox
, *mainscroll
, *titlescroll
, *label
;
1043 GtkAdjustment
*mainadj
, *titleadj
;
1048 ql
->notify
= notify
;
1049 ql
->menuitems
= menuitems
;
1051 ql
->mainrowheight
= !0; /* else division by 0 */
1052 ql
->selection
= selection_new();
1053 /* Create the layouts */
1055 ql
->mainlayout
= gtk_layout_new(0, 0);
1057 ql
->titlelayout
= gtk_layout_new(0, 0);
1058 /* Scroll the layouts */
1059 ql
->mainscroll
= mainscroll
= scroll_widget(ql
->mainlayout
, name
);
1060 titlescroll
= scroll_widget(ql
->titlelayout
, name
);
1061 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(titlescroll
),
1062 GTK_POLICY_NEVER
, GTK_POLICY_NEVER
);
1063 mainadj
= gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(mainscroll
));
1064 titleadj
= gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(titlescroll
));
1065 g_signal_connect(mainadj
, "changed", G_CALLBACK(queue_scrolled
), titleadj
);
1066 g_signal_connect(mainadj
, "value-changed", G_CALLBACK(queue_scrolled
), titleadj
);
1067 /* Fill the titles and put them anywhere */
1068 for(col
= 0; col
< NCOLUMNS
; ++col
) {
1070 label
= gtk_label_new(columns
[col
].name
);
1071 gtk_misc_set_alignment(GTK_MISC(label
), columns
[col
].xalign
, 0);
1072 ql
->titlecells
[col
] = wrap_queue_cell(label
, "row-title", 0);
1073 gtk_layout_put(GTK_LAYOUT(ql
->titlelayout
), ql
->titlecells
[col
], 0, 0);
1075 ql
->titlecells
[col
] = get_padding_cell("row-title");
1076 gtk_layout_put(GTK_LAYOUT(ql
->titlelayout
), ql
->titlecells
[col
], 0, 0);
1077 /* Pack the lot together in a vbox */
1079 vbox
= gtk_vbox_new(0, 0);
1080 gtk_box_pack_start(GTK_BOX(vbox
), titlescroll
, 0, 0, 0);
1081 gtk_box_pack_start(GTK_BOX(vbox
), mainscroll
, 1, 1, 0);
1082 /* Create the popup menu */
1084 ql
->menu
= gtk_menu_new();
1085 g_signal_connect(ql
->menu
, "destroy",
1086 G_CALLBACK(gtk_widget_destroyed
), &ql
->menu
);
1087 for(n
= 0; menuitems
[n
].name
; ++n
) {
1089 menuitems
[n
].w
= gtk_menu_item_new_with_label(menuitems
[n
].name
);
1090 gtk_menu_attach(GTK_MENU(ql
->menu
), menuitems
[n
].w
, 0, 1, n
, n
+ 1);
1092 g_object_set_data(G_OBJECT(vbox
), "type", (void *)&tabtype_queue
);
1093 g_object_set_data(G_OBJECT(vbox
), "queue", ql
);
1094 /* Catch button presses */
1095 g_signal_connect(ql
->mainlayout
, "button-release-event",
1096 G_CALLBACK(mainlayout_button
), ql
);
1098 g_signal_connect(ql
->mainlayout
, "button-press-event",
1099 G_CALLBACK(mainlayout_button
), ql
);
1104 /* Popup menu items -------------------------------------------------------- */
1106 /** @brief Count the number of items selected */
1107 static int queue_count_selected(const struct queuelike
*ql
) {
1108 return hash_count(ql
->selection
);
1111 /** @brief Count the number of items selected */
1112 static int queue_count_entries(const struct queuelike
*ql
) {
1114 const struct queue_entry
*q
;
1116 for(q
= ql
->q
; q
; q
= q
->next
)
1121 /** @brief Count the number of items selected, excluding the playing track if
1123 static int count_selected_nonplaying(const struct queuelike
*ql
) {
1124 int nselected
= queue_count_selected(ql
);
1126 if(ql
->q
== playing_track
&& selection_selected(ql
->selection
, ql
->q
->id
))
1131 /** @brief Determine whether the scratch option should be sensitive */
1132 static int scratch_sensitive(struct queuelike
attribute((unused
)) *ql
,
1133 struct menuitem
attribute((unused
)) *m
,
1134 struct queue_entry
attribute((unused
)) *q
) {
1135 /* We can scratch if the playing track is selected */
1136 return (playing_track
1137 && (disorder_eclient_state(client
) & DISORDER_CONNECTED
)
1138 && selection_selected(ql
->selection
, playing_track
->id
));
1141 /** @brief Scratch the playing track */
1142 static void scratch_activate(GtkMenuItem
attribute((unused
)) *menuitem
,
1143 gpointer
attribute((unused
)) user_data
) {
1145 disorder_eclient_scratch(client
, playing_track
->id
, 0, 0);
1148 /** @brief Determine whether the remove option should be sensitive */
1149 static int remove_sensitive(struct queuelike
*ql
,
1150 struct menuitem
attribute((unused
)) *m
,
1151 struct queue_entry
*q
) {
1152 /* We can remove if we're hovering over a particular track or any non-playing
1153 * tracks are selected */
1154 return ((disorder_eclient_state(client
) & DISORDER_CONNECTED
)
1156 && q
!= playing_track
)
1157 || count_selected_nonplaying(ql
)));
1160 /** @brief Remove selected track(s) */
1161 static void remove_activate(GtkMenuItem
attribute((unused
)) *menuitem
,
1162 gpointer user_data
) {
1163 const struct menuiteminfo
*mii
= user_data
;
1164 struct queue_entry
*q
= mii
->q
;
1165 struct queuelike
*ql
= mii
->ql
;
1167 if(count_selected_nonplaying(mii
->ql
)) {
1168 /* Remove selected tracks */
1169 for(q
= ql
->q
; q
; q
= q
->next
)
1170 if(selection_selected(ql
->selection
, q
->id
) && q
!= playing_track
)
1171 disorder_eclient_remove(client
, q
->id
, 0, 0);
1173 /* Remove just the hovered track */
1174 disorder_eclient_remove(client
, q
->id
, 0, 0);
1177 /** @brief Determine whether the properties menu option should be sensitive */
1178 static int properties_sensitive(struct queuelike
*ql
,
1179 struct menuitem
attribute((unused
)) *m
,
1180 struct queue_entry
attribute((unused
)) *q
) {
1181 /* "Properties" is sensitive if at least something is selected */
1182 return (hash_count(ql
->selection
) > 0
1183 && (disorder_eclient_state(client
) & DISORDER_CONNECTED
));
1186 /** @brief Pop up properties for the selected tracks */
1187 static void properties_activate(GtkMenuItem
attribute((unused
)) *menuitem
,
1188 gpointer user_data
) {
1189 const struct menuiteminfo
*mii
= user_data
;
1191 queue_properties(mii
->ql
);
1194 /** @brief Determine whether the select all menu option should be sensitive */
1195 static int selectall_sensitive(struct queuelike
*ql
,
1196 struct menuitem
attribute((unused
)) *m
,
1197 struct queue_entry
attribute((unused
)) *q
) {
1198 /* Sensitive if there is anything to select */
1202 /** @brief Select all tracks */
1203 static void selectall_activate(GtkMenuItem
attribute((unused
)) *menuitem
,
1204 gpointer user_data
) {
1205 const struct menuiteminfo
*mii
= user_data
;
1206 queue_select_all(mii
->ql
);
1209 /* The queue --------------------------------------------------------------- */
1211 /** @brief Fix up the queue by sticking the currently playing track on the front */
1212 static struct queue_entry
*fixup_queue(struct queue_entry
*q
) {
1217 actual_queue
->prev
= playing_track
;
1218 playing_track
->next
= actual_queue
;
1219 return playing_track
;
1221 return actual_queue
;
1224 /** @brief Adjust track played label
1226 * Called regularly to adjust the so-far played label (redrawing the whole
1227 * queue once a second makes disobedience occupy >10% of the CPU on my Athlon
1228 * which is ureasonable expensive) */
1229 static gboolean
adjust_sofar(gpointer
attribute((unused
)) data
) {
1230 if(playing_length_label
&& playing_track
)
1231 gtk_label_set_text(GTK_LABEL(playing_length_label
),
1232 text_length(playing_track
));
1236 /** @brief Popup menu for the queue
1238 * Properties first so that finger trouble is less dangerous. */
1239 static struct menuitem queue_menu
[] = {
1240 { "Track properties", properties_activate
, properties_sensitive
, 0, 0 },
1241 { "Select all tracks", selectall_activate
, selectall_sensitive
, 0, 0 },
1242 { "Scratch track", scratch_activate
, scratch_sensitive
, 0, 0 },
1243 { "Remove track from queue", remove_activate
, remove_sensitive
, 0, 0 },
1247 /** @brief Called whenever @ref DISORDER_PLAYING or @ref DISORDER_TRACK_PAUSED changes
1249 * We monitor pause/resume as well as whether the track is playing in order to
1250 * keep the time played so far up to date correctly. See playing_completed().
1252 static void playing_update(void attribute((unused
)) *v
) {
1253 D(("playing_update"));
1254 gtk_label_set_text(GTK_LABEL(report_label
), "updating playing track");
1255 disorder_eclient_playing(client
, playing_completed
, 0);
1258 /** @brief Create the queue widget */
1259 GtkWidget
*queue_widget(void) {
1260 D(("queue_widget"));
1261 /* Arrange periodic update of the so-far played field */
1262 g_timeout_add(1000/*ms*/, adjust_sofar
, 0);
1263 /* Arrange a callback whenever the playing state changes */
1264 register_monitor(playing_update
, 0, DISORDER_PLAYING
|DISORDER_TRACK_PAUSED
);
1265 /* We pass choose_update() as our notify function since the choose screen
1266 * marks tracks that are playing/in the queue. */
1267 return queuelike(&ql_queue
, fixup_queue
, choose_update
, queue_menu
,
1271 /** @brief Arrange an update of the queue widget
1273 * Called when a track is added to the queue, removed from the queue (by user
1274 * cmmand or because it is to be played) or moved within the queue
1276 void queue_update(void) {
1277 struct callbackdata
*cbd
;
1279 D(("queue_update"));
1280 cbd
= xmalloc(sizeof *cbd
);
1282 cbd
->u
.ql
= &ql_queue
;
1283 gtk_label_set_text(GTK_LABEL(report_label
), "updating queue");
1284 disorder_eclient_queue(client
, queuelike_completed
, cbd
);
1287 /* Recently played tracks -------------------------------------------------- */
1289 /** @brief Fix up the recently played list
1291 * It's in the wrong order! TODO fix this globally */
1292 static struct queue_entry
*fixup_recent(struct queue_entry
*q
) {
1293 struct queue_entry
*qr
= 0, *qn
;
1295 D(("fixup_recent"));
1298 /* Swap next/prev pointers */
1301 /* Remember last node for new head */
1309 /** @brief Pop-up menu for recently played list */
1310 static struct menuitem recent_menu
[] = {
1311 { "Track properties", properties_activate
, properties_sensitive
,0, 0 },
1312 { "Select all tracks", selectall_activate
, selectall_sensitive
, 0, 0 },
1316 /** @brief Create the recently-played list */
1317 GtkWidget
*recent_widget(void) {
1318 D(("recent_widget"));
1319 return queuelike(&ql_recent
, fixup_recent
, 0, recent_menu
, "recent");
1322 /** @brief Update the recently played list
1324 * Called whenever a track is added to it or removed from it.
1326 void recent_update(void) {
1327 struct callbackdata
*cbd
;
1329 D(("recent_update"));
1330 cbd
= xmalloc(sizeof *cbd
);
1332 cbd
->u
.ql
= &ql_recent
;
1333 gtk_label_set_text(GTK_LABEL(report_label
), "updating recently played list");
1334 disorder_eclient_recent(client
, queuelike_completed
, cbd
);
1337 /* Main menu plumbing ------------------------------------------------------ */
1339 static int queue_properties_sensitive(GtkWidget
*w
) {
1340 return (!!queue_count_selected(g_object_get_data(G_OBJECT(w
), "queue"))
1341 && (disorder_eclient_state(client
) & DISORDER_CONNECTED
));
1344 static int queue_selectall_sensitive(GtkWidget
*w
) {
1345 return !!queue_count_entries(g_object_get_data(G_OBJECT(w
), "queue"));
1348 static void queue_properties_activate(GtkWidget
*w
) {
1349 queue_properties(g_object_get_data(G_OBJECT(w
), "queue"));
1352 static void queue_selectall_activate(GtkWidget
*w
) {
1353 queue_select_all(g_object_get_data(G_OBJECT(w
), "queue"));
1356 static const struct tabtype tabtype_queue
= {
1357 queue_properties_sensitive
,
1358 queue_selectall_sensitive
,
1359 queue_properties_activate
,
1360 queue_selectall_activate
,
1363 /* Other entry points ------------------------------------------------------ */
1365 /** @brief Return nonzero if @p track is in the queue */
1366 int queued(const char *track
) {
1367 struct queue_entry
*q
;
1369 D(("queued %s", track
));
1370 for(q
= ql_queue
.q
; q
; q
= q
->next
)
1371 if(!strcmp(q
->track
, track
))
1381 indent-tabs-mode:nil