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
21 #include "disobedience.h"
23 #define HCELLPADDING 4
24 #define VCELLPADDING 2
26 /* 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)
38 * titlescroll never has any scrollbars. Instead whenever mainscroll's
39 * horizontal adjustment is changed, queue_scrolled adjusts titlescroll to
40 * match, forcing the title and the queue to pan in sync but allowing the queue
41 * to scroll independently.
43 * Whenever the queue changes everything below mainlayout is thrown away and
44 * reconstructed from scratch. Name lookups are cached, so this doesn't imply
45 * lots of disorder protocol traffic.
47 * The last cell on each row is the padding cell, and this extends ridiculously
48 * far to the right. (Can we do better?)
50 * When drag and drop is active we create extra eventboxes to act as dropzones.
51 * These only exist while the drag proceeds, as otherwise they steal events
52 * from more deserving widgets. (It might work to hide them when not in use
53 * too but this way around the d+d code is a bit more self-contained.)
56 /* Queue management -------------------------------------------------------- */
67 static void add_drag_targets(struct queuelike *ql);
68 static void remove_drag_targets(struct queuelike *ql);
69 static void redisplay_queue(struct queuelike *ql);
70 static GtkWidget *column_when(const struct queuelike *ql,
71 const struct queue_entry *q,
73 static GtkWidget *column_who(const struct queuelike *ql,
74 const struct queue_entry *q,
76 static GtkWidget *column_namepart(const struct queuelike *ql,
77 const struct queue_entry *q,
79 static GtkWidget *column_length(const struct queuelike *ql,
80 const struct queue_entry *q,
82 static int draggable_row(const struct queue_entry *q);
84 static const struct tabtype tabtype_queue; /* forward */
86 static const GtkTargetEntry dragtargets[] = {
87 { (char *)"disobedience-queue", GTK_TARGET_SAME_APP, 0 }
89 #define NDRAGTARGETS (int)(sizeof dragtargets / sizeof *dragtargets)
91 /* Definition of a column */
93 const char *name; /* Column name */
94 GtkWidget *(*widget)(const struct queuelike *ql,
95 const struct queue_entry *q,
96 const char *data); /* Make a label for this column */
97 const char *data; /* Data to pass to widget() */
98 gfloat xalign; /* Alignment of the label */
101 /* Need this in the middle of the types for NCOLUMNS */
102 static const struct column columns[] = {
103 { "When", column_when, 0, 1 },
104 { "Who", column_who, 0, 0 },
105 { "Artist", column_namepart, "artist", 0 },
106 { "Album", column_namepart, "album", 0 },
107 { "Title", column_namepart, "title", 0 },
108 { "Length", column_length, 0, 1 }
110 #define NCOLUMNS (int)(sizeof columns / sizeof *columns)
112 /* Data passed to menu item activation handlers */
113 struct menuiteminfo {
114 struct queuelike *ql; /* which queue we're dealing with */
115 struct queue_entry *q; /* hovered entry or 0 */
120 const char *name; /* name */
123 void (*activate)(GtkMenuItem *menuitem,
125 /* Called to activate the menu item. The user data is the queue entry that
126 * the pointer was over when the menu popped up. */
128 int (*sensitive)(struct queuelike *ql,
130 struct queue_entry *q);
131 /* Called to determine whether the menu item is usable. Returns TRUE if it
132 * should be sensitive and FALSE otherwise. Q points to the queue entry the
133 * pointer is over. */
136 gulong handlerid; /* signal handler ID */
137 GtkWidget *w; /* menu item widget */
142 const char *name; /* queue or recent */
145 void (*notify)(void);
146 /* Called when an update completes. */
148 struct queue_entry *(*fixup)(struct queue_entry *q);
149 /* Fix up the queue after update, or 0. Q is the list passed back from the
150 * server, the return value is assigned to ql->q. */
153 GtkWidget *mainlayout; /* main layout */
154 GtkWidget *mainscroll; /* scroller for main layout */
155 GtkWidget *titlelayout; /* title layout */
156 GtkWidget *titlecells[NCOLUMNS + 1]; /* title cells */
157 GtkWidget **cells; /* all the cells */
158 GtkWidget *menu; /* popup menu */
159 struct menuitem *menuitems; /* menu items */
160 GtkWidget *dragmark; /* drag destination marker */
161 GtkWidget **dropzones; /* drag targets */
164 struct queue_entry *q; /* head of queue */
165 struct queue_entry *last_click; /* last click */
166 int nrows; /* number of rows */
167 int mainrowheight; /* height of one row */
168 hash *selection; /* currently selected items */
169 int swallow_release; /* swallow button release from drag */
172 static struct queuelike ql_queue, ql_recent; /* queue and recently played */
173 static struct queue_entry *actual_queue; /* actual queue */
174 static struct queue_entry *playing_track; /* currenty playing */
175 static time_t last_playing = (time_t)-1; /* when last got playing */
176 static int namepart_lookups_outstanding;
177 static int namepart_completions_deferred; /* # of completions not processed */
178 static const struct cache_type cachetype_string = { 3600 };
179 static const struct cache_type cachetype_integer = { 3600 };
180 static GtkWidget *playing_length_label;
182 /* Debugging --------------------------------------------------------------- */
185 static void describe_widget(const char *name, GtkWidget *w, int indent) {
189 fprintf(stderr, "%*s[%s]: '%s'\n", indent, "",
190 name, gtk_widget_get_name(w));
191 gdk_window_get_position(w->window, &wx, &wy);
192 gdk_drawable_get_size(GDK_DRAWABLE(w->window), &ww, &wh);
193 fprintf(stderr, "%*s window %p: %dx%d at %dx%d\n",
194 indent, "", w->window, ww, wh, wx, wy);
197 static void dump_layout(const struct queuelike *ql) {
201 const struct queue_entry *q;
203 describe_widget("mainscroll", ql->mainscroll, 0);
204 describe_widget("mainlayout", ql->mainlayout, 1);
205 for(q = ql->q, row = 0; q; q = q->next, ++row)
206 for(col = 0; col < NCOLUMNS + 1; ++col)
207 if((w = ql->cells[row * (NCOLUMNS + 1) + col])) {
208 sprintf(s, "%dx%d", row, col);
209 describe_widget(s, w, 2);
210 if(GTK_BIN(w)->child)
211 describe_widget(0, w, 3);
216 /* Track detail lookup ----------------------------------------------------- */
218 /* A namepart lookup has completed or failed. */
219 static void namepart_completed_or_failed(void) {
220 D(("namepart_completed_or_failed"));
221 --namepart_lookups_outstanding;
222 if(!namepart_lookups_outstanding || namepart_completions_deferred > 24) {
223 redisplay_queue(&ql_queue);
224 redisplay_queue(&ql_recent);
225 namepart_completions_deferred = 0;
229 /* A namepart lookup has completed. */
230 static void namepart_completed(void *v, const char *value) {
231 struct callbackdata *cbd = v;
233 D(("namepart_completed"));
234 cache_put(&cachetype_string, cbd->u.key, value);
235 ++namepart_completions_deferred;
236 namepart_completed_or_failed();
239 /* A length lookup has completed. */
240 static void length_completed(void *v, long l) {
241 struct callbackdata *cbd = v;
244 D(("namepart_completed"));
245 value = xmalloc(sizeof *value);
247 cache_put(&cachetype_integer, cbd->u.key, value);
248 ++namepart_completions_deferred;
249 namepart_completed_or_failed();
252 /* A length or namepart lookup has failed. */
253 static void namepart_protocol_error(
254 struct callbackdata attribute((unused)) *cbd,
255 int attribute((unused)) code,
257 D(("namepart_protocol_error"));
258 gtk_label_set_text(GTK_LABEL(report_label), msg);
259 namepart_completed_or_failed();
262 /* Arrange to fill in a namepart cache entry */
263 static void namepart_fill(const char *track,
267 struct callbackdata *cbd;
269 ++namepart_lookups_outstanding;
270 cbd = xmalloc(sizeof *cbd);
271 cbd->onerror = namepart_protocol_error;
273 disorder_eclient_namepart(client, namepart_completed,
274 track, context, part, cbd);
277 /* Look up a namepart. If it is in the cache then just return its value. If
278 * not then look it up and arrange for the queues to be updated when its value
280 static const char *namepart(const char *track,
286 D(("namepart %s %s %s", track, context, part));
287 byte_xasprintf(&key, "namepart context=%s part=%s track=%s",
288 context, part, track);
289 value = cache_get(&cachetype_string, key);
292 /* stick a value in the cache so we don't issue another lookup if we
294 cache_put(&cachetype_string, key, value = "?");
295 namepart_fill(track, context, part, key);
300 /* Called from properties.c when we know a name part has changed */
301 void namepart_update(const char *track,
306 byte_xasprintf(&key, "namepart context=%s part=%s track=%s",
307 context, part, track);
308 /* Only refetch if it's actually in the cache */
309 if(cache_get(&cachetype_string, key))
310 namepart_fill(track, context, part, key);
313 /* Look up a track length. If it is in the cache then just return its value.
314 * If not then look it up and arrange for the queues to be updated when its
315 * value is available. */
316 static long getlength(const char *track) {
319 struct callbackdata *cbd;
320 static const long bogus = -1;
322 D(("getlength %s", track));
323 byte_xasprintf(&key, "length track=%s", track);
324 value = cache_get(&cachetype_integer, key);
326 D(("deferring..."));;
327 cache_put(&cachetype_integer, key, value = &bogus);
328 ++namepart_lookups_outstanding;
329 cbd = xmalloc(sizeof *cbd);
330 cbd->onerror = namepart_protocol_error;
332 disorder_eclient_length(client, length_completed, track, cbd);
337 /* Column constructors ----------------------------------------------------- */
339 /* Format the 'when' column */
340 static GtkWidget *column_when(const struct queuelike attribute((unused)) *ql,
341 const struct queue_entry *q,
342 const char attribute((unused)) *data) {
349 case playing_isscratch:
350 case playing_unplayed:
355 case playing_no_player:
357 case playing_scratched:
358 case playing_started:
360 case playing_quitting:
368 strftime(when, sizeof when, "%H:%M", localtime_r(&t, &tm));
372 return gtk_label_new(when);
375 /* Format the 'who' column */
376 static GtkWidget *column_who(const struct queuelike attribute((unused)) *ql,
377 const struct queue_entry *q,
378 const char attribute((unused)) *data) {
381 return gtk_label_new(q->submitter ? q->submitter : "");
384 /* Format one of the track name columns */
385 static GtkWidget *column_namepart(const struct queuelike
386 attribute((unused)) *ql,
387 const struct queue_entry *q,
389 D(("column_namepart"));
391 return gtk_label_new(namepart(q->track, "display", data));
394 /* Compute the length field */
395 static const char *text_length(const struct queue_entry *q) {
398 char *played = 0, *length = 0;
400 /* Work out what to say for the length */
401 l = getlength(q->track);
403 byte_xasprintf(&length, "%ld:%02ld", l / 60, l % 60);
405 byte_xasprintf(&length, "?:??");
406 /* For the currently playing track we want to report how much of the track
408 if(q == playing_track) {
409 /* log_state() arranges that we re-get the playing data whenever the
410 * pause/resume state changes */
411 if(last_state & DISORDER_TRACK_PAUSED)
412 l = playing_track->sofar;
415 l = playing_track->sofar + (now - last_playing);
417 byte_xasprintf(&played, "%ld:%02ld/%s", l / 60, l % 60, length);
423 /* Format the length column */
424 static GtkWidget *column_length(const struct queuelike attribute((unused)) *ql,
425 const struct queue_entry *q,
426 const char attribute((unused)) *data) {
427 D(("column_length"));
428 if(q == playing_track) {
429 assert(!playing_length_label);
431 playing_length_label = gtk_label_new(text_length(q));
432 /* Zot playing_length_label when it is destroyed */
433 g_signal_connect(playing_length_label, "destroy",
434 G_CALLBACK(gtk_widget_destroyed), &playing_length_label);
435 return playing_length_label;
438 return gtk_label_new(text_length(q));
443 /* Apply a new queue contents, transferring the selection from the old value */
444 static void update_queue(struct queuelike *ql, struct queue_entry *newq) {
445 struct queue_entry *q;
448 /* Propagate last_click across the change */
450 for(q = newq; q; q = q->next) {
451 if(!strcmp(q->id, ql->last_click->id))
456 /* Tell every queue entry which queue owns it */
457 for(q = newq; q; q = q->next)
459 /* Switch to the new queue */
461 /* Clean up any selected items that have fallen off */
462 for(q = ql->q; q; q = q->next)
463 selection_live(ql->selection, q->id);
464 selection_cleanup(ql->selection);
467 /* Wrap up a widget for putting into the queue or title */
468 static GtkWidget *wrap_queue_cell(GtkWidget *label,
474 D(("wrap_queue_cell"));
475 /* Padding should be in the label so there are no gaps in the
477 gtk_misc_set_padding(GTK_MISC(label), HCELLPADDING, VCELLPADDING);
478 /* Event box is just to hold a background color */
480 bg = gtk_event_box_new();
481 gtk_container_add(GTK_CONTAINER(bg), label);
483 /* Update maximum width */
484 gtk_widget_size_request(label, &req);
485 if(req.width > *wp) *wp = req.width;
487 /* Set widget names */
488 gtk_widget_set_name(bg, name);
489 gtk_widget_set_name(label, name);
493 /* Create the wrapped widget for a cell in the queue display */
494 static GtkWidget *get_queue_cell(struct queuelike *ql,
495 const struct queue_entry *q,
501 D(("get_queue_cell %d %d", row, col));
502 label = columns[col].widget(ql, q, columns[col].data);
503 gtk_misc_set_alignment(GTK_MISC(label), columns[col].xalign, 0);
504 return wrap_queue_cell(label, name, wp);
507 /* Add a padding cell to the end of a row */
508 static GtkWidget *get_padding_cell(const char *name) {
509 D(("get_padding_cell"));
511 return wrap_queue_cell(gtk_label_new(""), name, 0);
514 /* User button press and menu ---------------------------------------------- */
516 /* Update widget states in order to reflect the selection status */
517 static void set_widget_states(struct queuelike *ql) {
518 struct queue_entry *q;
521 for(q = ql->q, row = 0; q; q = q->next, ++row) {
522 for(col = 0; col < NCOLUMNS + 1; ++col)
523 gtk_widget_set_state(ql->cells[row * (NCOLUMNS + 1) + col],
524 selection_selected(ql->selection, q->id) ?
525 GTK_STATE_SELECTED : GTK_STATE_NORMAL);
527 /* Might need to change sensitivity of 'Properties' in main menu */
531 static int queue_before(const struct queue_entry *a,
532 const struct queue_entry *b) {
538 /* A button was pressed and released */
539 static gboolean queuelike_button_released(GtkWidget attribute((unused)) *widget,
540 GdkEventButton *event,
541 gpointer user_data) {
542 struct queue_entry *q = user_data, *qq;
543 struct queuelike *ql = q->ql;
544 struct menuiteminfo *mii;
547 /* Might be a release left over from a drag */
548 if(ql->swallow_release) {
549 ql->swallow_release = 0;
550 return FALSE; /* propagate */
553 if(event->type == GDK_BUTTON_PRESS
554 && event->button == 3) {
555 /* Right button click.
556 * If the current item is not selected then switch the selection to just
558 if(q && !selection_selected(ql->selection, q->id)) {
559 selection_empty(ql->selection);
560 selection_set(ql->selection, q->id, 1);
562 set_widget_states(ql);
564 /* Set the sensitivity of each menu item and (re-)establish the signal
566 for(n = 0; ql->menuitems[n].name; ++n) {
567 if(ql->menuitems[n].handlerid)
568 g_signal_handler_disconnect(ql->menuitems[n].w,
569 ql->menuitems[n].handlerid);
570 gtk_widget_set_sensitive(ql->menuitems[n].w,
571 ql->menuitems[n].sensitive(ql,
574 mii = xmalloc(sizeof *mii);
577 ql->menuitems[n].handlerid = g_signal_connect
578 (ql->menuitems[n].w, "activate",
579 G_CALLBACK(ql->menuitems[n].activate), mii);
581 /* Update the menu according to context */
582 gtk_widget_show_all(ql->menu);
583 gtk_menu_popup(GTK_MENU(ql->menu), 0, 0, 0, 0,
584 event->button, event->time);
585 return TRUE; /* hide the click from other widgets */
587 if(event->type == GDK_BUTTON_RELEASE
588 && event->button == 1) {
589 /* no modifiers: select this, unselect everything else, set last click
590 * +ctrl: flip selection of this, set last click
591 * +shift: select from last click to here, don't set last click
592 * +ctrl+shift: select from last click to here, set last click
594 switch(event->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) {
596 selection_empty(ql->selection);
597 selection_set(ql->selection, q->id, 1);
600 case GDK_CONTROL_MASK:
601 selection_flip(ql->selection, q->id);
605 case GDK_SHIFT_MASK|GDK_CONTROL_MASK:
607 if(!(event->state & GDK_CONTROL_MASK))
608 selection_empty(ql->selection);
609 selection_set(ql->selection, q->id, 1);
611 if(queue_before(ql->last_click, q))
612 while(qq != ql->last_click) {
614 selection_set(ql->selection, qq->id, 1);
617 while(qq != ql->last_click) {
619 selection_set(ql->selection, qq->id, 1);
621 if(event->state & GDK_CONTROL_MASK)
626 set_widget_states(ql);
627 gtk_widget_queue_draw(ql->mainlayout);
629 return FALSE; /* propagate */
632 /* A button was pressed or released on the mainlayout. For debugging only at
634 static gboolean mainlayout_button(GtkWidget attribute((unused)) *widget,
635 GdkEventButton attribute((unused)) *event,
636 gpointer attribute((unused)) user_data) {
637 return FALSE; /* propagate */
640 void queue_select_all(struct queuelike *ql) {
641 struct queue_entry *qq;
643 for(qq = ql->q; qq; qq = qq->next)
644 selection_set(ql->selection, qq->id, 1);
646 set_widget_states(ql);
649 void queue_properties(struct queuelike *ql) {
651 const struct queue_entry *qq;
654 for(qq = ql->q; qq; qq = qq->next)
655 if(selection_selected(ql->selection, qq->id))
656 vector_append(&v, (char *)qq->track);
658 properties(v.nvec, v.vec);
661 /* Drag and drop rearrangement --------------------------------------------- */
663 static int draggable_row(const struct queue_entry *q) {
664 return q->ql == &ql_queue && q != playing_track;
667 /* Called when a drag begings */
668 static void queue_drag_begin(GtkWidget attribute((unused)) *widget,
669 GdkDragContext attribute((unused)) *dc,
671 struct queue_entry *q = data;
672 struct queuelike *ql = q->ql;
674 /* Make sure the playing track is not selected, since it cannot be dragged */
676 selection_set(ql->selection, playing_track->id, 0);
677 /* If the dragged item is not in the selection then change the selection to
679 if(!selection_selected(ql->selection, q->id)) {
680 selection_empty(ql->selection);
681 selection_set(ql->selection, q->id, 1);
682 set_widget_states(ql);
684 /* Ignore the eventual button release */
685 ql->swallow_release = 1;
686 /* Create dropzones */
687 add_drag_targets(ql);
690 /* Convert an ID back into a queue entry and a screen row number */
691 static struct queue_entry *findentry(struct queuelike *ql,
695 struct queue_entry *q;
698 for(q = ql->q, row = 0; q && strcmp(q->id, id); q = q->next, ++row)
702 row = playing_track ? 0 : -1;
704 if(rowp) *rowp = row;
708 /* Called when data is dropped */
709 static gboolean queue_drag_drop(GtkWidget attribute((unused)) *widget,
710 GdkDragContext *drag_context,
711 gint attribute((unused)) x,
712 gint attribute((unused)) y,
714 gpointer user_data) {
715 struct queuelike *ql = &ql_queue;
716 const char *id = user_data;
718 struct queue_entry *q;
720 if(!id || (playing_track && !strcmp(id, playing_track->id)))
723 for(q = ql->q; q; q = q->next)
724 if(q != playing_track && selection_selected(ql->selection, q->id))
725 vector_append(&vec, (char *)q->id);
726 disorder_eclient_moveafter(client, id, vec.nvec, (const char **)vec.vec,
727 0/*completed*/, 0/*v*/);
728 gtk_drag_finish(drag_context, TRUE, TRUE, when);
729 /* Destroy dropzones */
730 remove_drag_targets(ql);
734 /* Called when we enter, or move within, a drop zone */
735 static gboolean queue_drag_motion(GtkWidget attribute((unused)) *widget,
736 GdkDragContext *drag_context,
737 gint attribute((unused)) x,
738 gint attribute((unused)) y,
740 gpointer user_data) {
741 struct queuelike *ql = &ql_queue;
742 const char *id = user_data;
744 struct queue_entry *q = findentry(ql, id, &row);
749 ql->dragmark = gtk_event_box_new();
750 g_signal_connect(ql->dragmark, "destroy",
751 G_CALLBACK(gtk_widget_destroyed), &ql->dragmark);
752 gtk_widget_set_size_request(ql->dragmark, 10240, row ? 4 : 2);
753 gtk_widget_set_name(ql->dragmark, "queue-drag");
754 gtk_layout_put(GTK_LAYOUT(ql->mainlayout), ql->dragmark, 0,
755 (row + 1) * ql->mainrowheight - !!row);
757 gtk_layout_move(GTK_LAYOUT(ql->mainlayout), ql->dragmark, 0,
758 (row + 1) * ql->mainrowheight - !!row);
759 gtk_widget_show(ql->dragmark);
760 gdk_drag_status(drag_context, GDK_ACTION_MOVE, when);
763 /* ID has gone AWOL */
767 /* Called when we leave a drop zone */
768 static void queue_drag_leave(GtkWidget attribute((unused)) *widget,
769 GdkDragContext attribute((unused)) *drag_context,
770 guint attribute((unused)) when,
771 gpointer attribute((unused)) user_data) {
772 struct queuelike *ql = &ql_queue;
775 gtk_widget_hide(ql->dragmark);
778 /* Add a drag target at position Y. ID is the track to insert the moved tracks
779 * after, and might be 0 to insert before the start. */
780 static void add_drag_target(struct queuelike *ql, int y, int row,
784 assert(ql->dropzones[row] == 0);
786 eventbox = gtk_event_box_new();
787 /* Make the target zone invisible */
788 gtk_event_box_set_visible_window(GTK_EVENT_BOX(eventbox), FALSE);
789 /* Make it large enough */
790 gtk_widget_set_size_request(eventbox, 10240,
791 y ? ql->mainrowheight : ql->mainrowheight / 2);
793 gtk_layout_put(GTK_LAYOUT(ql->mainlayout), eventbox, 0,
794 y ? y - ql->mainrowheight / 2 : 0);
795 /* Mark it as capable of receiving drops */
796 gtk_drag_dest_set(eventbox,
798 dragtargets, NDRAGTARGETS, GDK_ACTION_MOVE);
799 g_signal_connect(eventbox, "drag-drop",
800 G_CALLBACK(queue_drag_drop), (char *)id);
801 /* Monitor drag motion */
802 g_signal_connect(eventbox, "drag-motion",
803 G_CALLBACK(queue_drag_motion), (char *)id);
804 g_signal_connect(eventbox, "drag-leave",
805 G_CALLBACK(queue_drag_leave), (char *)id);
806 /* The widget needs to be shown to receive drags */
807 gtk_widget_show(eventbox);
808 /* Remember the drag targets */
809 ql->dropzones[row] = eventbox;
810 g_signal_connect(eventbox, "destroy",
811 G_CALLBACK(gtk_widget_destroyed), &ql->dropzones[row]);
814 /* Create dropzones for dragging into */
815 static void add_drag_targets(struct queuelike *ql) {
817 struct queue_entry *q;
819 /* Create an array to store the widgets */
820 ql->dropzones = xcalloc(ql->nrows, sizeof (GtkWidget *));
822 /* Add a drag target before the first row provided it's not the playing
824 if(!playing_track || ql->q != playing_track)
825 add_drag_target(ql, 0, 0, 0);
826 /* Put a drag target at the bottom of every row */
827 for(q = ql->q, row = 0; q; q = q->next, ++row) {
828 y += ql->mainrowheight;
829 add_drag_target(ql, y, row, q->id);
833 /* Remove the dropzones */
834 static void remove_drag_targets(struct queuelike *ql) {
837 for(row = 0; row < ql->nrows; ++row) {
838 if(ql->dropzones[row]) {
840 gtk_widget_destroy(ql->dropzones[row]);
842 assert(ql->dropzones[row] == 0);
846 /* Layout ------------------------------------------------------------------ */
848 /* Redisplay the queue. */
849 static void redisplay_queue(struct queuelike *ql) {
850 struct queue_entry *q;
856 int maxwidths[NCOLUMNS], x, y, titlerowheight;
857 int totalwidth = 10240; /* TODO: can we be less blunt */
859 D(("redisplay_queue"));
860 /* Eliminate all the existing widgets and start from scratch */
861 for(c = children = gtk_container_get_children(GTK_CONTAINER(ql->mainlayout));
864 /* Destroy both the label and the eventbox */
865 if(GTK_BIN(c->data)->child) {
867 gtk_widget_destroy(GTK_BIN(c->data)->child);
870 gtk_widget_destroy(GTK_WIDGET(c->data));
872 g_list_free(children);
873 /* Adjust the row count */
874 for(q = ql->q, ql->nrows = 0; q; q = q->next)
876 /* We need to create all the widgets before we can position them */
877 ql->cells = xcalloc(ql->nrows * (NCOLUMNS + 1), sizeof *ql->cells);
878 /* Minimum width is given by the column headings */
879 for(col = 0; col < NCOLUMNS; ++col) {
880 /* Reset size so we don't inherit last iteration's maximum size */
881 gtk_widget_set_size_request(GTK_BIN(ql->titlecells[col])->child, -1, -1);
882 gtk_widget_size_request(GTK_BIN(ql->titlecells[col])->child, &req);
883 maxwidths[col] = req.width;
885 /* Find the vertical size of the title bar */
886 gtk_widget_size_request(GTK_BIN(ql->titlecells[0])->child, &req);
887 titlerowheight = req.height;
890 /* Construct the widgets */
891 for(q = ql->q, row = 0; q; q = q->next, ++row) {
892 /* Figure out the widget name for this row */
893 if(q == playing_track) name = "row-playing";
894 else name = row % 2 ? "row-even" : "row-odd";
895 /* Make the widget for each column */
896 for(col = 0; col <= NCOLUMNS; ++col) {
897 /* Create and store the widget */
899 w = get_queue_cell(ql, q, row, col, name, &maxwidths[col]);
901 w = get_padding_cell(name);
902 ql->cells[row * (NCOLUMNS + 1) + col] = w;
903 /* Maybe mark it draggable */
904 if(draggable_row(q)) {
905 gtk_drag_source_set(w, GDK_BUTTON1_MASK,
906 dragtargets, NDRAGTARGETS, GDK_ACTION_MOVE);
907 g_signal_connect(w, "drag-begin", G_CALLBACK(queue_drag_begin), q);
909 /* Catch button presses */
910 g_signal_connect(w, "button-release-event",
911 G_CALLBACK(queuelike_button_released), q);
912 g_signal_connect(w, "button-press-event",
913 G_CALLBACK(queuelike_button_released), q);
916 /* ...and of each row in the main layout */
917 gtk_widget_size_request(GTK_BIN(ql->cells[0])->child, &req);
918 ql->mainrowheight = req.height;
919 /* Now we know the maximum width of each column we can set the size of
920 * everything and position it */
921 for(row = 0, q = ql->q; row < ql->nrows; ++row, q = q->next) {
923 for(col = 0; col < NCOLUMNS; ++col) {
924 w = ql->cells[row * (NCOLUMNS + 1) + col];
925 gtk_widget_set_size_request(GTK_BIN(w)->child,
927 gtk_layout_put(GTK_LAYOUT(ql->mainlayout), w, x, y);
930 w = ql->cells[row * (NCOLUMNS + 1) + col];
931 gtk_widget_set_size_request(GTK_BIN(w)->child,
933 gtk_layout_put(GTK_LAYOUT(ql->mainlayout), w, x, y);
934 y += ql->mainrowheight;
939 for(col = 0; col < NCOLUMNS; ++col) {
940 gtk_widget_set_size_request(GTK_BIN(ql->titlecells[col])->child,
942 gtk_layout_move(GTK_LAYOUT(ql->titlelayout), ql->titlecells[col], x, 0);
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);
949 set_widget_states(ql);
950 /* Make sure it's all visible */
951 gtk_widget_show_all(ql->mainlayout);
952 gtk_widget_show_all(ql->titlelayout);
953 /* Layouts might shrink to arrange for the area they shrink out of to be
955 gtk_widget_queue_draw(ql->mainlayout);
956 gtk_widget_queue_draw(ql->titlelayout);
957 /* Adjust the size of the layout */
958 gtk_layout_set_size(GTK_LAYOUT(ql->mainlayout), x, y);
959 gtk_layout_set_size(GTK_LAYOUT(ql->titlelayout), x, titlerowheight);
960 gtk_widget_set_size_request(ql->titlelayout, -1, titlerowheight);
963 /* Called with new queue/recent contents */
964 static void queuelike_completed(void *v, struct queue_entry *q) {
965 struct callbackdata *cbd = v;
966 struct queuelike *ql = cbd->u.ql;
968 D(("queuelike_complete"));
969 /* Install the new queue */
970 update_queue(ql, ql->fixup(q));
971 /* Update the display */
975 /* Update sensitivity of main menu items */
979 /* Called with a new currently playing track */
980 static void playing_completed(void attribute((unused)) *v,
981 struct queue_entry *q) {
982 struct callbackdata cbd;
983 D(("playing_completed"));
985 /* Record when we got the playing track data so we know how old the 'sofar'
988 cbd.u.ql = &ql_queue;
989 queuelike_completed(&cbd, actual_queue);
992 static void queue_scrolled(GtkAdjustment *adjustment,
993 gpointer user_data) {
994 GtkAdjustment *titleadj = user_data;
996 D(("queue_scrolled"));
997 gtk_adjustment_set_value(titleadj, adjustment->value);
1000 /* Create a queuelike thing (queue/recent) */
1001 static GtkWidget *queuelike(struct queuelike *ql,
1002 struct queue_entry *(*fixup)(struct queue_entry *),
1003 void (*notify)(void),
1004 struct menuitem *menuitems,
1006 GtkWidget *vbox, *mainscroll, *titlescroll, *label;
1007 GtkAdjustment *mainadj, *titleadj;
1012 ql->notify = notify;
1013 ql->menuitems = menuitems;
1015 ql->mainrowheight = !0; /* else division by 0 */
1016 ql->selection = selection_new();
1017 /* Create the layouts */
1019 ql->mainlayout = gtk_layout_new(0, 0);
1021 ql->titlelayout = gtk_layout_new(0, 0);
1022 /* Scroll the layouts */
1023 ql->mainscroll = mainscroll = scroll_widget(ql->mainlayout, name);
1024 titlescroll = scroll_widget(ql->titlelayout, name);
1025 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(titlescroll),
1026 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
1027 mainadj = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(mainscroll));
1028 titleadj = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(titlescroll));
1029 g_signal_connect(mainadj, "changed", G_CALLBACK(queue_scrolled), titleadj);
1030 g_signal_connect(mainadj, "value-changed", G_CALLBACK(queue_scrolled), titleadj);
1031 /* Fill the titles and put them anywhere */
1032 for(col = 0; col < NCOLUMNS; ++col) {
1034 label = gtk_label_new(columns[col].name);
1035 gtk_misc_set_alignment(GTK_MISC(label), columns[col].xalign, 0);
1036 ql->titlecells[col] = wrap_queue_cell(label, "row-title", 0);
1037 gtk_layout_put(GTK_LAYOUT(ql->titlelayout), ql->titlecells[col], 0, 0);
1039 ql->titlecells[col] = get_padding_cell("row-title");
1040 gtk_layout_put(GTK_LAYOUT(ql->titlelayout), ql->titlecells[col], 0, 0);
1041 /* Pack the lot together in a vbox */
1043 vbox = gtk_vbox_new(0, 0);
1044 gtk_box_pack_start(GTK_BOX(vbox), titlescroll, 0, 0, 0);
1045 gtk_box_pack_start(GTK_BOX(vbox), mainscroll, 1, 1, 0);
1046 /* Create the popup menu */
1048 ql->menu = gtk_menu_new();
1049 g_signal_connect(ql->menu, "destroy",
1050 G_CALLBACK(gtk_widget_destroyed), &ql->menu);
1051 for(n = 0; menuitems[n].name; ++n) {
1053 menuitems[n].w = gtk_menu_item_new_with_label(menuitems[n].name);
1054 gtk_menu_attach(GTK_MENU(ql->menu), menuitems[n].w, 0, 1, n, n + 1);
1056 g_object_set_data(G_OBJECT(vbox), "type", (void *)&tabtype_queue);
1057 g_object_set_data(G_OBJECT(vbox), "queue", ql);
1058 /* Catch button presses */
1059 g_signal_connect(ql->mainlayout, "button-release-event",
1060 G_CALLBACK(mainlayout_button), ql);
1062 g_signal_connect(ql->mainlayout, "button-press-event",
1063 G_CALLBACK(mainlayout_button), ql);
1068 /* Popup menu items -------------------------------------------------------- */
1070 /* Count the number of items selected */
1071 static int queue_count_selected(const struct queuelike *ql) {
1072 return hash_count(ql->selection);
1075 /* Count the number of items selected */
1076 static int queue_count_entries(const struct queuelike *ql) {
1078 const struct queue_entry *q;
1080 for(q = ql->q; q; q = q->next)
1085 /* Count the number of items selected, excluding the playing track if there is
1087 static int count_selected_nonplaying(const struct queuelike *ql) {
1088 int nselected = queue_count_selected(ql);
1090 if(ql->q == playing_track && selection_selected(ql->selection, ql->q->id))
1095 static int scratch_sensitive(struct queuelike attribute((unused)) *ql,
1096 struct menuitem attribute((unused)) *m,
1097 struct queue_entry attribute((unused)) *q) {
1098 /* We can scratch if the playing track is selected */
1099 return playing_track && selection_selected(ql->selection, playing_track->id);
1102 static void scratch_activate(GtkMenuItem attribute((unused)) *menuitem,
1103 gpointer attribute((unused)) user_data) {
1105 disorder_eclient_scratch(client, playing_track->id, 0, 0);
1108 static int remove_sensitive(struct queuelike *ql,
1109 struct menuitem attribute((unused)) *m,
1110 struct queue_entry *q) {
1111 /* We can remove if we're hovering over a particular track or any non-playing
1112 * tracks are selected */
1113 return (q && q != playing_track) || count_selected_nonplaying(ql);
1116 static void remove_activate(GtkMenuItem attribute((unused)) *menuitem,
1117 gpointer user_data) {
1118 const struct menuiteminfo *mii = user_data;
1119 struct queue_entry *q = mii->q;
1120 struct queuelike *ql = mii->ql;
1122 if(count_selected_nonplaying(mii->ql)) {
1123 /* Remove selected tracks */
1124 for(q = ql->q; q; q = q->next)
1125 if(selection_selected(ql->selection, q->id) && q != playing_track)
1126 disorder_eclient_remove(client, q->id, 0, 0);
1128 /* Remove just the hovered track */
1129 disorder_eclient_remove(client, q->id, 0, 0);
1132 static int properties_sensitive(struct queuelike *ql,
1133 struct menuitem attribute((unused)) *m,
1134 struct queue_entry attribute((unused)) *q) {
1135 /* "Properties" is sensitive if at least something is selected */
1136 return hash_count(ql->selection) > 0;
1139 static void properties_activate(GtkMenuItem attribute((unused)) *menuitem,
1140 gpointer user_data) {
1141 const struct menuiteminfo *mii = user_data;
1143 queue_properties(mii->ql);
1146 static int selectall_sensitive(struct queuelike *ql,
1147 struct menuitem attribute((unused)) *m,
1148 struct queue_entry attribute((unused)) *q) {
1149 /* Sensitive if there is anything to select */
1153 static void selectall_activate(GtkMenuItem attribute((unused)) *menuitem,
1154 gpointer user_data) {
1155 const struct menuiteminfo *mii = user_data;
1156 queue_select_all(mii->ql);
1159 /* The queue --------------------------------------------------------------- */
1161 /* Fix up the queue by sticking the currently playing track on the front */
1162 static struct queue_entry *fixup_queue(struct queue_entry *q) {
1167 actual_queue->prev = playing_track;
1168 playing_track->next = actual_queue;
1169 return playing_track;
1171 return actual_queue;
1174 /* Called regularly to adjust the so-far played label (redrawing the whole
1175 * queue once a second makes disobedience occupy >10% of the CPU on my Athlon
1176 * which is ureasonable expensive) */
1177 static gboolean adjust_sofar(gpointer attribute((unused)) data) {
1178 if(playing_length_label && playing_track)
1179 gtk_label_set_text(GTK_LABEL(playing_length_label),
1180 text_length(playing_track));
1184 /* Popup menu for the queue. Put the properties first so that finger trouble
1185 * is less dangerous. */
1186 static struct menuitem queue_menu[] = {
1187 { "Track properties", properties_activate, properties_sensitive, 0, 0 },
1188 { "Select all tracks", selectall_activate, selectall_sensitive, 0, 0 },
1189 { "Scratch track", scratch_activate, scratch_sensitive, 0, 0 },
1190 { "Remove track from queue", remove_activate, remove_sensitive, 0, 0 },
1194 GtkWidget *queue_widget(void) {
1195 D(("queue_widget"));
1196 /* Arrange periodic update of the so-far played field */
1197 g_timeout_add(1000/*ms*/, adjust_sofar, 0);
1198 /* We pass choose_update() as our notify function since the choose screen
1199 * marks tracks that are playing/in the queue. */
1200 return queuelike(&ql_queue, fixup_queue, choose_update, queue_menu,
1204 void queue_update(void) {
1205 struct callbackdata *cbd;
1207 D(("queue_update"));
1208 cbd = xmalloc(sizeof *cbd);
1210 cbd->u.ql = &ql_queue;
1211 gtk_label_set_text(GTK_LABEL(report_label), "updating queue");
1212 disorder_eclient_queue(client, queuelike_completed, cbd);
1215 void playing_update(void) {
1216 D(("playing_update"));
1217 gtk_label_set_text(GTK_LABEL(report_label), "updating playing track");
1218 disorder_eclient_playing(client, playing_completed, 0);
1221 /* Recently played tracks -------------------------------------------------- */
1223 static struct queue_entry *fixup_recent(struct queue_entry *q) {
1224 /* 'recent' is in the wrong order. TODO: globally fix this! */
1225 struct queue_entry *qr = 0, *qn;
1227 D(("fixup_recent"));
1230 /* Swap next/prev pointers */
1233 /* Remember last node for new head */
1241 static struct menuitem recent_menu[] = {
1242 { "Track properties", properties_activate, properties_sensitive,0, 0 },
1243 { "Select all tracks", selectall_activate, selectall_sensitive, 0, 0 },
1247 GtkWidget *recent_widget(void) {
1248 D(("recent_widget"));
1249 return queuelike(&ql_recent, fixup_recent, 0, recent_menu, "recent");
1252 void recent_update(void) {
1253 struct callbackdata *cbd;
1255 D(("recent_update"));
1256 cbd = xmalloc(sizeof *cbd);
1258 cbd->u.ql = &ql_recent;
1259 gtk_label_set_text(GTK_LABEL(report_label), "updating recently played list");
1260 disorder_eclient_recent(client, queuelike_completed, cbd);
1263 /* Main menu plumbing ------------------------------------------------------ */
1265 static int queue_properties_sensitive(GtkWidget *w) {
1266 return !!queue_count_selected(g_object_get_data(G_OBJECT(w), "queue"));
1269 static int queue_selectall_sensitive(GtkWidget *w) {
1270 return !!queue_count_entries(g_object_get_data(G_OBJECT(w), "queue"));
1273 static void queue_properties_activate(GtkWidget *w) {
1274 queue_properties(g_object_get_data(G_OBJECT(w), "queue"));
1277 static void queue_selectall_activate(GtkWidget *w) {
1278 queue_select_all(g_object_get_data(G_OBJECT(w), "queue"));
1281 static const struct tabtype tabtype_queue = {
1282 queue_properties_sensitive,
1283 queue_selectall_sensitive,
1284 queue_properties_activate,
1285 queue_selectall_activate,
1288 /* Other entry points ------------------------------------------------------ */
1290 int queued(const char *track) {
1291 struct queue_entry *q;
1293 D(("queued %s", track));
1294 for(q = ql_queue.q; q; q = q->next)
1295 if(!strcmp(q->track, track))
1305 indent-tabs-mode:nil