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.
26 #include "disobedience.h"
28 #define HCELLPADDING 4
29 #define VCELLPADDING 2
31 /* A queue layout is structured as follows:
36 * titlecells[col] eventbox (made by wrap_queue_cell)
37 * titlecells[col]->child label (from columns[])
40 * cells[row * N + c] eventbox (made by wrap_queue_cell)
41 * cells[row * N + c]->child label (from column constructors)
43 * titlescroll never has any scrollbars. Instead whenever mainscroll's
44 * horizontal adjustment is changed, queue_scrolled adjusts titlescroll to
45 * match, forcing the title and the queue to pan in sync but allowing the queue
46 * to scroll independently.
48 * Whenever the queue changes everything below mainlayout is thrown away and
49 * reconstructed from scratch. Name lookups are cached, so this doesn't imply
50 * lots of disorder protocol traffic.
52 * The last cell on each row is the padding cell, and this extends ridiculously
53 * far to the right. (Can we do better?)
55 * When drag and drop is active we create extra eventboxes to act as dropzones.
56 * These only exist while the drag proceeds, as otherwise they steal events
57 * from more deserving widgets. (It might work to hide them when not in use
58 * too but this way around the d+d code is a bit more self-contained.)
61 /* Queue management -------------------------------------------------------- */
72 static void add_drag_targets(struct queuelike *ql);
73 static void remove_drag_targets(struct queuelike *ql);
74 static void redisplay_queue(struct queuelike *ql);
75 static GtkWidget *column_when(const struct queuelike *ql,
76 const struct queue_entry *q,
78 static GtkWidget *column_who(const struct queuelike *ql,
79 const struct queue_entry *q,
81 static GtkWidget *column_namepart(const struct queuelike *ql,
82 const struct queue_entry *q,
84 static GtkWidget *column_length(const struct queuelike *ql,
85 const struct queue_entry *q,
87 static int draggable_row(const struct queue_entry *q);
89 static const struct tabtype tabtype_queue; /* forward */
91 static const GtkTargetEntry dragtargets[] = {
92 { (char *)"disobedience-queue", GTK_TARGET_SAME_APP, 0 }
94 #define NDRAGTARGETS (int)(sizeof dragtargets / sizeof *dragtargets)
96 /* Definition of a column */
98 const char *name; /* Column name */
99 GtkWidget *(*widget)(const struct queuelike *ql,
100 const struct queue_entry *q,
101 const char *data); /* Make a label for this column */
102 const char *data; /* Data to pass to widget() */
103 gfloat xalign; /* Alignment of the label */
106 /* Need this in the middle of the types for NCOLUMNS */
107 static const struct column columns[] = {
108 { "When", column_when, 0, 1 },
109 { "Who", column_who, 0, 0 },
110 { "Artist", column_namepart, "artist", 0 },
111 { "Album", column_namepart, "album", 0 },
112 { "Title", column_namepart, "title", 0 },
113 { "Length", column_length, 0, 1 }
115 #define NCOLUMNS (int)(sizeof columns / sizeof *columns)
117 /* Data passed to menu item activation handlers */
118 struct menuiteminfo {
119 struct queuelike *ql; /* which queue we're dealing with */
120 struct queue_entry *q; /* hovered entry or 0 */
125 const char *name; /* name */
128 void (*activate)(GtkMenuItem *menuitem,
130 /* Called to activate the menu item. The user data is the queue entry that
131 * the pointer was over when the menu popped up. */
133 int (*sensitive)(struct queuelike *ql,
135 struct queue_entry *q);
136 /* Called to determine whether the menu item is usable. Returns TRUE if it
137 * should be sensitive and FALSE otherwise. Q points to the queue entry the
138 * pointer is over. */
141 gulong handlerid; /* signal handler ID */
142 GtkWidget *w; /* menu item widget */
147 const char *name; /* queue or recent */
150 void (*notify)(void);
151 /* Called when an update completes. */
153 struct queue_entry *(*fixup)(struct queue_entry *q);
154 /* Fix up the queue after update, or 0. Q is the list passed back from the
155 * server, the return value is assigned to ql->q. */
158 GtkWidget *mainlayout; /* main layout */
159 GtkWidget *mainscroll; /* scroller for main layout */
160 GtkWidget *titlelayout; /* title layout */
161 GtkWidget *titlecells[NCOLUMNS + 1]; /* title cells */
162 GtkWidget **cells; /* all the cells */
163 GtkWidget *menu; /* popup menu */
164 struct menuitem *menuitems; /* menu items */
165 GtkWidget *dragmark; /* drag destination marker */
166 GtkWidget **dropzones; /* drag targets */
169 struct queue_entry *q; /* head of queue */
170 struct queue_entry *last_click; /* last click */
171 int nrows; /* number of rows */
172 int mainrowheight; /* height of one row */
173 hash *selection; /* currently selected items */
174 int swallow_release; /* swallow button release from drag */
177 static struct queuelike ql_queue, ql_recent; /* queue and recently played */
178 static struct queue_entry *actual_queue; /* actual queue */
179 static struct queue_entry *playing_track; /* currenty playing */
180 static time_t last_playing = (time_t)-1; /* when last got playing */
181 static int namepart_lookups_outstanding;
182 static int namepart_completions_deferred; /* # of completions not processed */
183 static const struct cache_type cachetype_string = { 3600 };
184 static const struct cache_type cachetype_integer = { 3600 };
185 static GtkWidget *playing_length_label;
187 /* Debugging --------------------------------------------------------------- */
190 static void describe_widget(const char *name, GtkWidget *w, int indent) {
194 fprintf(stderr, "%*s[%s]: '%s'\n", indent, "",
195 name, gtk_widget_get_name(w));
196 gdk_window_get_position(w->window, &wx, &wy);
197 gdk_drawable_get_size(GDK_DRAWABLE(w->window), &ww, &wh);
198 fprintf(stderr, "%*s window %p: %dx%d at %dx%d\n",
199 indent, "", w->window, ww, wh, wx, wy);
202 static void dump_layout(const struct queuelike *ql) {
206 const struct queue_entry *q;
208 describe_widget("mainscroll", ql->mainscroll, 0);
209 describe_widget("mainlayout", ql->mainlayout, 1);
210 for(q = ql->q, row = 0; q; q = q->next, ++row)
211 for(col = 0; col < NCOLUMNS + 1; ++col)
212 if((w = ql->cells[row * (NCOLUMNS + 1) + col])) {
213 sprintf(s, "%dx%d", row, col);
214 describe_widget(s, w, 2);
215 if(GTK_BIN(w)->child)
216 describe_widget(0, w, 3);
221 /* Track detail lookup ----------------------------------------------------- */
223 /* A namepart lookup has completed or failed. */
224 static void namepart_completed_or_failed(void) {
225 D(("namepart_completed_or_failed"));
226 --namepart_lookups_outstanding;
227 if(!namepart_lookups_outstanding || namepart_completions_deferred > 24) {
228 redisplay_queue(&ql_queue);
229 redisplay_queue(&ql_recent);
230 namepart_completions_deferred = 0;
234 /* A namepart lookup has completed. */
235 static void namepart_completed(void *v, const char *value) {
236 struct callbackdata *cbd = v;
238 D(("namepart_completed"));
239 cache_put(&cachetype_string, cbd->u.key, value);
240 ++namepart_completions_deferred;
241 namepart_completed_or_failed();
244 /* A length lookup has completed. */
245 static void length_completed(void *v, long l) {
246 struct callbackdata *cbd = v;
249 D(("namepart_completed"));
250 value = xmalloc(sizeof *value);
252 cache_put(&cachetype_integer, cbd->u.key, value);
253 ++namepart_completions_deferred;
254 namepart_completed_or_failed();
257 /* A length or namepart lookup has failed. */
258 static void namepart_protocol_error(
259 struct callbackdata attribute((unused)) *cbd,
260 int attribute((unused)) code,
262 D(("namepart_protocol_error"));
263 gtk_label_set_text(GTK_LABEL(report_label), msg);
264 namepart_completed_or_failed();
267 /* Arrange to fill in a namepart cache entry */
268 static void namepart_fill(const char *track,
272 struct callbackdata *cbd;
274 ++namepart_lookups_outstanding;
275 cbd = xmalloc(sizeof *cbd);
276 cbd->onerror = namepart_protocol_error;
278 disorder_eclient_namepart(client, namepart_completed,
279 track, context, part, cbd);
282 /* Look up a namepart. If it is in the cache then just return its value. If
283 * not then look it up and arrange for the queues to be updated when its value
285 static const char *namepart(const char *track,
291 D(("namepart %s %s %s", track, context, part));
292 byte_xasprintf(&key, "namepart context=%s part=%s track=%s",
293 context, part, track);
294 value = cache_get(&cachetype_string, key);
297 /* stick a value in the cache so we don't issue another lookup if we
299 cache_put(&cachetype_string, key, value = "?");
300 namepart_fill(track, context, part, key);
305 /* Called from properties.c when we know a name part has changed */
306 void namepart_update(const char *track,
311 byte_xasprintf(&key, "namepart context=%s part=%s track=%s",
312 context, part, track);
313 /* Only refetch if it's actually in the cache */
314 if(cache_get(&cachetype_string, key))
315 namepart_fill(track, context, part, key);
318 /* Look up a track length. If it is in the cache then just return its value.
319 * If not then look it up and arrange for the queues to be updated when its
320 * value is available. */
321 static long getlength(const char *track) {
324 struct callbackdata *cbd;
325 static const long bogus = -1;
327 D(("getlength %s", track));
328 byte_xasprintf(&key, "length track=%s", track);
329 value = cache_get(&cachetype_integer, key);
331 D(("deferring..."));;
332 cache_put(&cachetype_integer, key, value = &bogus);
333 ++namepart_lookups_outstanding;
334 cbd = xmalloc(sizeof *cbd);
335 cbd->onerror = namepart_protocol_error;
337 disorder_eclient_length(client, length_completed, track, cbd);
342 /* Column constructors ----------------------------------------------------- */
344 /* Format the 'when' column */
345 static GtkWidget *column_when(const struct queuelike attribute((unused)) *ql,
346 const struct queue_entry *q,
347 const char attribute((unused)) *data) {
354 case playing_isscratch:
355 case playing_unplayed:
360 case playing_no_player:
362 case playing_scratched:
363 case playing_started:
365 case playing_quitting:
373 strftime(when, sizeof when, "%H:%M", localtime_r(&t, &tm));
377 return gtk_label_new(when);
380 /* Format the 'who' column */
381 static GtkWidget *column_who(const struct queuelike attribute((unused)) *ql,
382 const struct queue_entry *q,
383 const char attribute((unused)) *data) {
386 return gtk_label_new(q->submitter ? q->submitter : "");
389 /* Format one of the track name columns */
390 static GtkWidget *column_namepart(const struct queuelike
391 attribute((unused)) *ql,
392 const struct queue_entry *q,
394 D(("column_namepart"));
396 return gtk_label_new(namepart(q->track, "display", data));
399 /* Compute the length field */
400 static const char *text_length(const struct queue_entry *q) {
403 char *played = 0, *length = 0;
405 /* Work out what to say for the length */
406 l = getlength(q->track);
408 byte_xasprintf(&length, "%ld:%02ld", l / 60, l % 60);
410 byte_xasprintf(&length, "?:??");
411 /* For the currently playing track we want to report how much of the track
413 if(q == playing_track) {
414 /* log_state() arranges that we re-get the playing data whenever the
415 * pause/resume state changes */
416 if(last_state & DISORDER_TRACK_PAUSED)
417 l = playing_track->sofar;
420 l = playing_track->sofar + (now - last_playing);
422 byte_xasprintf(&played, "%ld:%02ld/%s", l / 60, l % 60, length);
428 /* Format the length column */
429 static GtkWidget *column_length(const struct queuelike attribute((unused)) *ql,
430 const struct queue_entry *q,
431 const char attribute((unused)) *data) {
432 D(("column_length"));
433 if(q == playing_track) {
434 assert(!playing_length_label);
436 playing_length_label = gtk_label_new(text_length(q));
437 /* Zot playing_length_label when it is destroyed */
438 g_signal_connect(playing_length_label, "destroy",
439 G_CALLBACK(gtk_widget_destroyed), &playing_length_label);
440 return playing_length_label;
443 return gtk_label_new(text_length(q));
448 /* Apply a new queue contents, transferring the selection from the old value */
449 static void update_queue(struct queuelike *ql, struct queue_entry *newq) {
450 struct queue_entry *q;
453 /* Propagate last_click across the change */
455 for(q = newq; q; q = q->next) {
456 if(!strcmp(q->id, ql->last_click->id))
461 /* Tell every queue entry which queue owns it */
462 for(q = newq; q; q = q->next)
464 /* Switch to the new queue */
466 /* Clean up any selected items that have fallen off */
467 for(q = ql->q; q; q = q->next)
468 selection_live(ql->selection, q->id);
469 selection_cleanup(ql->selection);
472 /* Wrap up a widget for putting into the queue or title */
473 static GtkWidget *wrap_queue_cell(GtkWidget *label,
479 D(("wrap_queue_cell"));
480 /* Padding should be in the label so there are no gaps in the
482 gtk_misc_set_padding(GTK_MISC(label), HCELLPADDING, VCELLPADDING);
483 /* Event box is just to hold a background color */
485 bg = gtk_event_box_new();
486 gtk_container_add(GTK_CONTAINER(bg), label);
488 /* Update maximum width */
489 gtk_widget_size_request(label, &req);
490 if(req.width > *wp) *wp = req.width;
492 /* Set widget names */
493 gtk_widget_set_name(bg, name);
494 gtk_widget_set_name(label, name);
498 /* Create the wrapped widget for a cell in the queue display */
499 static GtkWidget *get_queue_cell(struct queuelike *ql,
500 const struct queue_entry *q,
506 D(("get_queue_cell %d %d", row, col));
507 label = columns[col].widget(ql, q, columns[col].data);
508 gtk_misc_set_alignment(GTK_MISC(label), columns[col].xalign, 0);
509 return wrap_queue_cell(label, name, wp);
512 /* Add a padding cell to the end of a row */
513 static GtkWidget *get_padding_cell(const char *name) {
514 D(("get_padding_cell"));
516 return wrap_queue_cell(gtk_label_new(""), name, 0);
519 /* User button press and menu ---------------------------------------------- */
521 /* Update widget states in order to reflect the selection status */
522 static void set_widget_states(struct queuelike *ql) {
523 struct queue_entry *q;
526 for(q = ql->q, row = 0; q; q = q->next, ++row) {
527 for(col = 0; col < NCOLUMNS + 1; ++col)
528 gtk_widget_set_state(ql->cells[row * (NCOLUMNS + 1) + col],
529 selection_selected(ql->selection, q->id) ?
530 GTK_STATE_SELECTED : GTK_STATE_NORMAL);
532 /* Might need to change sensitivity of 'Properties' in main menu */
536 static int queue_before(const struct queue_entry *a,
537 const struct queue_entry *b) {
543 /* A button was pressed and released */
544 static gboolean queuelike_button_released(GtkWidget attribute((unused)) *widget,
545 GdkEventButton *event,
546 gpointer user_data) {
547 struct queue_entry *q = user_data, *qq;
548 struct queuelike *ql = q->ql;
549 struct menuiteminfo *mii;
552 /* Might be a release left over from a drag */
553 if(ql->swallow_release) {
554 ql->swallow_release = 0;
555 return FALSE; /* propagate */
558 if(event->type == GDK_BUTTON_PRESS
559 && event->button == 3) {
560 /* Right button click.
561 * If the current item is not selected then switch the selection to just
563 if(q && !selection_selected(ql->selection, q->id)) {
564 selection_empty(ql->selection);
565 selection_set(ql->selection, q->id, 1);
567 set_widget_states(ql);
569 /* Set the sensitivity of each menu item and (re-)establish the signal
571 for(n = 0; ql->menuitems[n].name; ++n) {
572 if(ql->menuitems[n].handlerid)
573 g_signal_handler_disconnect(ql->menuitems[n].w,
574 ql->menuitems[n].handlerid);
575 gtk_widget_set_sensitive(ql->menuitems[n].w,
576 ql->menuitems[n].sensitive(ql,
579 mii = xmalloc(sizeof *mii);
582 ql->menuitems[n].handlerid = g_signal_connect
583 (ql->menuitems[n].w, "activate",
584 G_CALLBACK(ql->menuitems[n].activate), mii);
586 /* Update the menu according to context */
587 gtk_widget_show_all(ql->menu);
588 gtk_menu_popup(GTK_MENU(ql->menu), 0, 0, 0, 0,
589 event->button, event->time);
590 return TRUE; /* hide the click from other widgets */
592 if(event->type == GDK_BUTTON_RELEASE
593 && event->button == 1) {
594 /* no modifiers: select this, unselect everything else, set last click
595 * +ctrl: flip selection of this, set last click
596 * +shift: select from last click to here, don't set last click
597 * +ctrl+shift: select from last click to here, set last click
599 switch(event->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) {
601 selection_empty(ql->selection);
602 selection_set(ql->selection, q->id, 1);
605 case GDK_CONTROL_MASK:
606 selection_flip(ql->selection, q->id);
610 case GDK_SHIFT_MASK|GDK_CONTROL_MASK:
612 if(!(event->state & GDK_CONTROL_MASK))
613 selection_empty(ql->selection);
614 selection_set(ql->selection, q->id, 1);
616 if(queue_before(ql->last_click, q))
617 while(qq != ql->last_click) {
619 selection_set(ql->selection, qq->id, 1);
622 while(qq != ql->last_click) {
624 selection_set(ql->selection, qq->id, 1);
626 if(event->state & GDK_CONTROL_MASK)
631 set_widget_states(ql);
632 gtk_widget_queue_draw(ql->mainlayout);
634 return FALSE; /* propagate */
637 /* A button was pressed or released on the mainlayout. For debugging only at
639 static gboolean mainlayout_button(GtkWidget attribute((unused)) *widget,
640 GdkEventButton attribute((unused)) *event,
641 gpointer attribute((unused)) user_data) {
642 return FALSE; /* propagate */
645 void queue_select_all(struct queuelike *ql) {
646 struct queue_entry *qq;
648 for(qq = ql->q; qq; qq = qq->next)
649 selection_set(ql->selection, qq->id, 1);
651 set_widget_states(ql);
654 void queue_properties(struct queuelike *ql) {
656 const struct queue_entry *qq;
659 for(qq = ql->q; qq; qq = qq->next)
660 if(selection_selected(ql->selection, qq->id))
661 vector_append(&v, (char *)qq->track);
663 properties(v.nvec, v.vec);
666 /* Drag and drop rearrangement --------------------------------------------- */
668 static int draggable_row(const struct queue_entry *q) {
669 return q->ql == &ql_queue && q != playing_track;
672 /* Called when a drag begings */
673 static void queue_drag_begin(GtkWidget attribute((unused)) *widget,
674 GdkDragContext attribute((unused)) *dc,
676 struct queue_entry *q = data;
677 struct queuelike *ql = q->ql;
679 /* Make sure the playing track is not selected, since it cannot be dragged */
681 selection_set(ql->selection, playing_track->id, 0);
682 /* If the dragged item is not in the selection then change the selection to
684 if(!selection_selected(ql->selection, q->id)) {
685 selection_empty(ql->selection);
686 selection_set(ql->selection, q->id, 1);
687 set_widget_states(ql);
689 /* Ignore the eventual button release */
690 ql->swallow_release = 1;
691 /* Create dropzones */
692 add_drag_targets(ql);
695 /* Convert an ID back into a queue entry and a screen row number */
696 static struct queue_entry *findentry(struct queuelike *ql,
700 struct queue_entry *q;
703 for(q = ql->q, row = 0; q && strcmp(q->id, id); q = q->next, ++row)
707 row = playing_track ? 0 : -1;
709 if(rowp) *rowp = row;
713 /* Called when data is dropped */
714 static gboolean queue_drag_drop(GtkWidget attribute((unused)) *widget,
715 GdkDragContext *drag_context,
716 gint attribute((unused)) x,
717 gint attribute((unused)) y,
719 gpointer user_data) {
720 struct queuelike *ql = &ql_queue;
721 const char *id = user_data;
723 struct queue_entry *q;
725 if(!id || (playing_track && !strcmp(id, playing_track->id)))
728 for(q = ql->q; q; q = q->next)
729 if(q != playing_track && selection_selected(ql->selection, q->id))
730 vector_append(&vec, (char *)q->id);
731 disorder_eclient_moveafter(client, id, vec.nvec, (const char **)vec.vec,
732 0/*completed*/, 0/*v*/);
733 gtk_drag_finish(drag_context, TRUE, TRUE, when);
734 /* Destroy dropzones */
735 remove_drag_targets(ql);
739 /* Called when we enter, or move within, a drop zone */
740 static gboolean queue_drag_motion(GtkWidget attribute((unused)) *widget,
741 GdkDragContext *drag_context,
742 gint attribute((unused)) x,
743 gint attribute((unused)) y,
745 gpointer user_data) {
746 struct queuelike *ql = &ql_queue;
747 const char *id = user_data;
749 struct queue_entry *q = findentry(ql, id, &row);
754 ql->dragmark = gtk_event_box_new();
755 g_signal_connect(ql->dragmark, "destroy",
756 G_CALLBACK(gtk_widget_destroyed), &ql->dragmark);
757 gtk_widget_set_size_request(ql->dragmark, 10240, row ? 4 : 2);
758 gtk_widget_set_name(ql->dragmark, "queue-drag");
759 gtk_layout_put(GTK_LAYOUT(ql->mainlayout), ql->dragmark, 0,
760 (row + 1) * ql->mainrowheight - !!row);
762 gtk_layout_move(GTK_LAYOUT(ql->mainlayout), ql->dragmark, 0,
763 (row + 1) * ql->mainrowheight - !!row);
764 gtk_widget_show(ql->dragmark);
765 gdk_drag_status(drag_context, GDK_ACTION_MOVE, when);
768 /* ID has gone AWOL */
772 /* Called when we leave a drop zone */
773 static void queue_drag_leave(GtkWidget attribute((unused)) *widget,
774 GdkDragContext attribute((unused)) *drag_context,
775 guint attribute((unused)) when,
776 gpointer attribute((unused)) user_data) {
777 struct queuelike *ql = &ql_queue;
780 gtk_widget_hide(ql->dragmark);
783 /* Add a drag target at position Y. ID is the track to insert the moved tracks
784 * after, and might be 0 to insert before the start. */
785 static void add_drag_target(struct queuelike *ql, int y, int row,
789 assert(ql->dropzones[row] == 0);
791 eventbox = gtk_event_box_new();
792 /* Make the target zone invisible */
793 gtk_event_box_set_visible_window(GTK_EVENT_BOX(eventbox), FALSE);
794 /* Make it large enough */
795 gtk_widget_set_size_request(eventbox, 10240,
796 y ? ql->mainrowheight : ql->mainrowheight / 2);
798 gtk_layout_put(GTK_LAYOUT(ql->mainlayout), eventbox, 0,
799 y ? y - ql->mainrowheight / 2 : 0);
800 /* Mark it as capable of receiving drops */
801 gtk_drag_dest_set(eventbox,
803 dragtargets, NDRAGTARGETS, GDK_ACTION_MOVE);
804 g_signal_connect(eventbox, "drag-drop",
805 G_CALLBACK(queue_drag_drop), (char *)id);
806 /* Monitor drag motion */
807 g_signal_connect(eventbox, "drag-motion",
808 G_CALLBACK(queue_drag_motion), (char *)id);
809 g_signal_connect(eventbox, "drag-leave",
810 G_CALLBACK(queue_drag_leave), (char *)id);
811 /* The widget needs to be shown to receive drags */
812 gtk_widget_show(eventbox);
813 /* Remember the drag targets */
814 ql->dropzones[row] = eventbox;
815 g_signal_connect(eventbox, "destroy",
816 G_CALLBACK(gtk_widget_destroyed), &ql->dropzones[row]);
819 /* Create dropzones for dragging into */
820 static void add_drag_targets(struct queuelike *ql) {
822 struct queue_entry *q;
824 /* Create an array to store the widgets */
825 ql->dropzones = xcalloc(ql->nrows, sizeof (GtkWidget *));
827 /* Add a drag target before the first row provided it's not the playing
829 if(!playing_track || ql->q != playing_track)
830 add_drag_target(ql, 0, 0, 0);
831 /* Put a drag target at the bottom of every row */
832 for(q = ql->q, row = 0; q; q = q->next, ++row) {
833 y += ql->mainrowheight;
834 add_drag_target(ql, y, row, q->id);
838 /* Remove the dropzones */
839 static void remove_drag_targets(struct queuelike *ql) {
842 for(row = 0; row < ql->nrows; ++row) {
843 if(ql->dropzones[row]) {
845 gtk_widget_destroy(ql->dropzones[row]);
847 assert(ql->dropzones[row] == 0);
851 /* Layout ------------------------------------------------------------------ */
853 /* Redisplay the queue. */
854 static void redisplay_queue(struct queuelike *ql) {
855 struct queue_entry *q;
861 int maxwidths[NCOLUMNS], x, y, titlerowheight;
862 int totalwidth = 10240; /* TODO: can we be less blunt */
864 D(("redisplay_queue"));
865 /* Eliminate all the existing widgets and start from scratch */
866 for(c = children = gtk_container_get_children(GTK_CONTAINER(ql->mainlayout));
869 /* Destroy both the label and the eventbox */
870 if(GTK_BIN(c->data)->child) {
872 gtk_widget_destroy(GTK_BIN(c->data)->child);
875 gtk_widget_destroy(GTK_WIDGET(c->data));
877 g_list_free(children);
878 /* Adjust the row count */
879 for(q = ql->q, ql->nrows = 0; q; q = q->next)
881 /* We need to create all the widgets before we can position them */
882 ql->cells = xcalloc(ql->nrows * (NCOLUMNS + 1), sizeof *ql->cells);
883 /* Minimum width is given by the column headings */
884 for(col = 0; col < NCOLUMNS; ++col) {
885 /* Reset size so we don't inherit last iteration's maximum size */
886 gtk_widget_set_size_request(GTK_BIN(ql->titlecells[col])->child, -1, -1);
887 gtk_widget_size_request(GTK_BIN(ql->titlecells[col])->child, &req);
888 maxwidths[col] = req.width;
890 /* Find the vertical size of the title bar */
891 gtk_widget_size_request(GTK_BIN(ql->titlecells[0])->child, &req);
892 titlerowheight = req.height;
895 /* Construct the widgets */
896 for(q = ql->q, row = 0; q; q = q->next, ++row) {
897 /* Figure out the widget name for this row */
898 if(q == playing_track) name = "row-playing";
899 else name = row % 2 ? "row-even" : "row-odd";
900 /* Make the widget for each column */
901 for(col = 0; col <= NCOLUMNS; ++col) {
902 /* Create and store the widget */
904 w = get_queue_cell(ql, q, row, col, name, &maxwidths[col]);
906 w = get_padding_cell(name);
907 ql->cells[row * (NCOLUMNS + 1) + col] = w;
908 /* Maybe mark it draggable */
909 if(draggable_row(q)) {
910 gtk_drag_source_set(w, GDK_BUTTON1_MASK,
911 dragtargets, NDRAGTARGETS, GDK_ACTION_MOVE);
912 g_signal_connect(w, "drag-begin", G_CALLBACK(queue_drag_begin), q);
914 /* Catch button presses */
915 g_signal_connect(w, "button-release-event",
916 G_CALLBACK(queuelike_button_released), q);
917 g_signal_connect(w, "button-press-event",
918 G_CALLBACK(queuelike_button_released), q);
921 /* ...and of each row in the main layout */
922 gtk_widget_size_request(GTK_BIN(ql->cells[0])->child, &req);
923 ql->mainrowheight = req.height;
924 /* Now we know the maximum width of each column we can set the size of
925 * everything and position it */
926 for(row = 0, q = ql->q; row < ql->nrows; ++row, q = q->next) {
928 for(col = 0; col < NCOLUMNS; ++col) {
929 w = ql->cells[row * (NCOLUMNS + 1) + col];
930 gtk_widget_set_size_request(GTK_BIN(w)->child,
932 gtk_layout_put(GTK_LAYOUT(ql->mainlayout), w, x, y);
935 w = ql->cells[row * (NCOLUMNS + 1) + col];
936 gtk_widget_set_size_request(GTK_BIN(w)->child,
938 gtk_layout_put(GTK_LAYOUT(ql->mainlayout), w, x, y);
939 y += ql->mainrowheight;
944 for(col = 0; col < NCOLUMNS; ++col) {
945 gtk_widget_set_size_request(GTK_BIN(ql->titlecells[col])->child,
947 gtk_layout_move(GTK_LAYOUT(ql->titlelayout), ql->titlecells[col], x, 0);
950 gtk_widget_set_size_request(GTK_BIN(ql->titlecells[col])->child,
952 gtk_layout_move(GTK_LAYOUT(ql->titlelayout), ql->titlecells[col], x, 0);
954 set_widget_states(ql);
955 /* Make sure it's all visible */
956 gtk_widget_show_all(ql->mainlayout);
957 gtk_widget_show_all(ql->titlelayout);
958 /* Layouts might shrink to arrange for the area they shrink out of to be
960 gtk_widget_queue_draw(ql->mainlayout);
961 gtk_widget_queue_draw(ql->titlelayout);
962 /* Adjust the size of the layout */
963 gtk_layout_set_size(GTK_LAYOUT(ql->mainlayout), x, y);
964 gtk_layout_set_size(GTK_LAYOUT(ql->titlelayout), x, titlerowheight);
965 gtk_widget_set_size_request(ql->titlelayout, -1, titlerowheight);
968 /* Called with new queue/recent contents */
969 static void queuelike_completed(void *v, struct queue_entry *q) {
970 struct callbackdata *cbd = v;
971 struct queuelike *ql = cbd->u.ql;
973 D(("queuelike_complete"));
974 /* Install the new queue */
975 update_queue(ql, ql->fixup(q));
976 /* Update the display */
980 /* Update sensitivity of main menu items */
984 /* Called with a new currently playing track */
985 static void playing_completed(void attribute((unused)) *v,
986 struct queue_entry *q) {
987 struct callbackdata cbd;
988 D(("playing_completed"));
990 /* Record when we got the playing track data so we know how old the 'sofar'
993 cbd.u.ql = &ql_queue;
994 queuelike_completed(&cbd, actual_queue);
997 static void queue_scrolled(GtkAdjustment *adjustment,
998 gpointer user_data) {
999 GtkAdjustment *titleadj = user_data;
1001 D(("queue_scrolled"));
1002 gtk_adjustment_set_value(titleadj, adjustment->value);
1005 /* Create a queuelike thing (queue/recent) */
1006 static GtkWidget *queuelike(struct queuelike *ql,
1007 struct queue_entry *(*fixup)(struct queue_entry *),
1008 void (*notify)(void),
1009 struct menuitem *menuitems,
1011 GtkWidget *vbox, *mainscroll, *titlescroll, *label;
1012 GtkAdjustment *mainadj, *titleadj;
1017 ql->notify = notify;
1018 ql->menuitems = menuitems;
1020 ql->mainrowheight = !0; /* else division by 0 */
1021 ql->selection = selection_new();
1022 /* Create the layouts */
1024 ql->mainlayout = gtk_layout_new(0, 0);
1026 ql->titlelayout = gtk_layout_new(0, 0);
1027 /* Scroll the layouts */
1028 ql->mainscroll = mainscroll = scroll_widget(ql->mainlayout, name);
1029 titlescroll = scroll_widget(ql->titlelayout, name);
1030 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(titlescroll),
1031 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
1032 mainadj = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(mainscroll));
1033 titleadj = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(titlescroll));
1034 g_signal_connect(mainadj, "changed", G_CALLBACK(queue_scrolled), titleadj);
1035 g_signal_connect(mainadj, "value-changed", G_CALLBACK(queue_scrolled), titleadj);
1036 /* Fill the titles and put them anywhere */
1037 for(col = 0; col < NCOLUMNS; ++col) {
1039 label = gtk_label_new(columns[col].name);
1040 gtk_misc_set_alignment(GTK_MISC(label), columns[col].xalign, 0);
1041 ql->titlecells[col] = wrap_queue_cell(label, "row-title", 0);
1042 gtk_layout_put(GTK_LAYOUT(ql->titlelayout), ql->titlecells[col], 0, 0);
1044 ql->titlecells[col] = get_padding_cell("row-title");
1045 gtk_layout_put(GTK_LAYOUT(ql->titlelayout), ql->titlecells[col], 0, 0);
1046 /* Pack the lot together in a vbox */
1048 vbox = gtk_vbox_new(0, 0);
1049 gtk_box_pack_start(GTK_BOX(vbox), titlescroll, 0, 0, 0);
1050 gtk_box_pack_start(GTK_BOX(vbox), mainscroll, 1, 1, 0);
1051 /* Create the popup menu */
1053 ql->menu = gtk_menu_new();
1054 g_signal_connect(ql->menu, "destroy",
1055 G_CALLBACK(gtk_widget_destroyed), &ql->menu);
1056 for(n = 0; menuitems[n].name; ++n) {
1058 menuitems[n].w = gtk_menu_item_new_with_label(menuitems[n].name);
1059 gtk_menu_attach(GTK_MENU(ql->menu), menuitems[n].w, 0, 1, n, n + 1);
1061 g_object_set_data(G_OBJECT(vbox), "type", (void *)&tabtype_queue);
1062 g_object_set_data(G_OBJECT(vbox), "queue", ql);
1063 /* Catch button presses */
1064 g_signal_connect(ql->mainlayout, "button-release-event",
1065 G_CALLBACK(mainlayout_button), ql);
1067 g_signal_connect(ql->mainlayout, "button-press-event",
1068 G_CALLBACK(mainlayout_button), ql);
1073 /* Popup menu items -------------------------------------------------------- */
1075 /* Count the number of items selected */
1076 static int queue_count_selected(const struct queuelike *ql) {
1077 return hash_count(ql->selection);
1080 /* Count the number of items selected */
1081 static int queue_count_entries(const struct queuelike *ql) {
1083 const struct queue_entry *q;
1085 for(q = ql->q; q; q = q->next)
1090 /* Count the number of items selected, excluding the playing track if there is
1092 static int count_selected_nonplaying(const struct queuelike *ql) {
1093 int nselected = queue_count_selected(ql);
1095 if(ql->q == playing_track && selection_selected(ql->selection, ql->q->id))
1100 static int scratch_sensitive(struct queuelike attribute((unused)) *ql,
1101 struct menuitem attribute((unused)) *m,
1102 struct queue_entry attribute((unused)) *q) {
1103 /* We can scratch if the playing track is selected */
1104 return (playing_track
1105 && (disorder_eclient_state(client) & DISORDER_CONNECTED)
1106 && selection_selected(ql->selection, playing_track->id));
1109 static void scratch_activate(GtkMenuItem attribute((unused)) *menuitem,
1110 gpointer attribute((unused)) user_data) {
1112 disorder_eclient_scratch(client, playing_track->id, 0, 0);
1115 static int remove_sensitive(struct queuelike *ql,
1116 struct menuitem attribute((unused)) *m,
1117 struct queue_entry *q) {
1118 /* We can remove if we're hovering over a particular track or any non-playing
1119 * tracks are selected */
1120 return ((disorder_eclient_state(client) & DISORDER_CONNECTED)
1122 && q != playing_track)
1123 || count_selected_nonplaying(ql)));
1126 static void remove_activate(GtkMenuItem attribute((unused)) *menuitem,
1127 gpointer user_data) {
1128 const struct menuiteminfo *mii = user_data;
1129 struct queue_entry *q = mii->q;
1130 struct queuelike *ql = mii->ql;
1132 if(count_selected_nonplaying(mii->ql)) {
1133 /* Remove selected tracks */
1134 for(q = ql->q; q; q = q->next)
1135 if(selection_selected(ql->selection, q->id) && q != playing_track)
1136 disorder_eclient_remove(client, q->id, 0, 0);
1138 /* Remove just the hovered track */
1139 disorder_eclient_remove(client, q->id, 0, 0);
1142 static int properties_sensitive(struct queuelike *ql,
1143 struct menuitem attribute((unused)) *m,
1144 struct queue_entry attribute((unused)) *q) {
1145 /* "Properties" is sensitive if at least something is selected */
1146 return (hash_count(ql->selection) > 0
1147 && (disorder_eclient_state(client) & DISORDER_CONNECTED));
1150 static void properties_activate(GtkMenuItem attribute((unused)) *menuitem,
1151 gpointer user_data) {
1152 const struct menuiteminfo *mii = user_data;
1154 queue_properties(mii->ql);
1157 static int selectall_sensitive(struct queuelike *ql,
1158 struct menuitem attribute((unused)) *m,
1159 struct queue_entry attribute((unused)) *q) {
1160 /* Sensitive if there is anything to select */
1164 static void selectall_activate(GtkMenuItem attribute((unused)) *menuitem,
1165 gpointer user_data) {
1166 const struct menuiteminfo *mii = user_data;
1167 queue_select_all(mii->ql);
1170 /* The queue --------------------------------------------------------------- */
1172 /* Fix up the queue by sticking the currently playing track on the front */
1173 static struct queue_entry *fixup_queue(struct queue_entry *q) {
1178 actual_queue->prev = playing_track;
1179 playing_track->next = actual_queue;
1180 return playing_track;
1182 return actual_queue;
1185 /* Called regularly to adjust the so-far played label (redrawing the whole
1186 * queue once a second makes disobedience occupy >10% of the CPU on my Athlon
1187 * which is ureasonable expensive) */
1188 static gboolean adjust_sofar(gpointer attribute((unused)) data) {
1189 if(playing_length_label && playing_track)
1190 gtk_label_set_text(GTK_LABEL(playing_length_label),
1191 text_length(playing_track));
1195 /* Popup menu for the queue. Put the properties first so that finger trouble
1196 * is less dangerous. */
1197 static struct menuitem queue_menu[] = {
1198 { "Track properties", properties_activate, properties_sensitive, 0, 0 },
1199 { "Select all tracks", selectall_activate, selectall_sensitive, 0, 0 },
1200 { "Scratch track", scratch_activate, scratch_sensitive, 0, 0 },
1201 { "Remove track from queue", remove_activate, remove_sensitive, 0, 0 },
1205 /** @brief Called whenever @ref DISORDER_PLAYING or @ref DISORDER_TRACK_PAUSED changes
1207 * We monitor pause/resume as well as whether the track is playing in order to
1208 * keep the time played so far up to date correctly. See playing_completed().
1210 static void playing_update(void attribute((unused)) *v) {
1211 D(("playing_update"));
1212 gtk_label_set_text(GTK_LABEL(report_label), "updating playing track");
1213 disorder_eclient_playing(client, playing_completed, 0);
1216 /** @brief Create the queue widget */
1217 GtkWidget *queue_widget(void) {
1218 D(("queue_widget"));
1219 /* Arrange periodic update of the so-far played field */
1220 g_timeout_add(1000/*ms*/, adjust_sofar, 0);
1221 /* Arrange a callback whenever the playing state changes */
1222 register_monitor(playing_update, 0, DISORDER_PLAYING|DISORDER_TRACK_PAUSED);
1223 /* We pass choose_update() as our notify function since the choose screen
1224 * marks tracks that are playing/in the queue. */
1225 return queuelike(&ql_queue, fixup_queue, choose_update, queue_menu,
1229 void queue_update(void) {
1230 struct callbackdata *cbd;
1232 D(("queue_update"));
1233 cbd = xmalloc(sizeof *cbd);
1235 cbd->u.ql = &ql_queue;
1236 gtk_label_set_text(GTK_LABEL(report_label), "updating queue");
1237 disorder_eclient_queue(client, queuelike_completed, cbd);
1240 /* Recently played tracks -------------------------------------------------- */
1242 static struct queue_entry *fixup_recent(struct queue_entry *q) {
1243 /* 'recent' is in the wrong order. TODO: globally fix this! */
1244 struct queue_entry *qr = 0, *qn;
1246 D(("fixup_recent"));
1249 /* Swap next/prev pointers */
1252 /* Remember last node for new head */
1260 static struct menuitem recent_menu[] = {
1261 { "Track properties", properties_activate, properties_sensitive,0, 0 },
1262 { "Select all tracks", selectall_activate, selectall_sensitive, 0, 0 },
1266 GtkWidget *recent_widget(void) {
1267 D(("recent_widget"));
1268 return queuelike(&ql_recent, fixup_recent, 0, recent_menu, "recent");
1271 void recent_update(void) {
1272 struct callbackdata *cbd;
1274 D(("recent_update"));
1275 cbd = xmalloc(sizeof *cbd);
1277 cbd->u.ql = &ql_recent;
1278 gtk_label_set_text(GTK_LABEL(report_label), "updating recently played list");
1279 disorder_eclient_recent(client, queuelike_completed, cbd);
1282 /* Main menu plumbing ------------------------------------------------------ */
1284 static int queue_properties_sensitive(GtkWidget *w) {
1285 return (!!queue_count_selected(g_object_get_data(G_OBJECT(w), "queue"))
1286 && (disorder_eclient_state(client) & DISORDER_CONNECTED));
1289 static int queue_selectall_sensitive(GtkWidget *w) {
1290 return !!queue_count_entries(g_object_get_data(G_OBJECT(w), "queue"));
1293 static void queue_properties_activate(GtkWidget *w) {
1294 queue_properties(g_object_get_data(G_OBJECT(w), "queue"));
1297 static void queue_selectall_activate(GtkWidget *w) {
1298 queue_select_all(g_object_get_data(G_OBJECT(w), "queue"));
1301 static const struct tabtype tabtype_queue = {
1302 queue_properties_sensitive,
1303 queue_selectall_sensitive,
1304 queue_properties_activate,
1305 queue_selectall_activate,
1308 /* Other entry points ------------------------------------------------------ */
1310 int queued(const char *track) {
1311 struct queue_entry *q;
1313 D(("queued %s", track));
1314 for(q = ql_queue.q; q; q = q->next)
1315 if(!strcmp(q->track, track))
1325 indent-tabs-mode:nil