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 -------------------------------------------------------- */
60 static void add_drag_targets(struct queuelike *ql);
61 static void remove_drag_targets(struct queuelike *ql);
62 static void redisplay_queue(struct queuelike *ql);
63 static GtkWidget *column_when(const struct queuelike *ql,
64 const struct queue_entry *q,
66 static GtkWidget *column_who(const struct queuelike *ql,
67 const struct queue_entry *q,
69 static GtkWidget *column_namepart(const struct queuelike *ql,
70 const struct queue_entry *q,
72 static GtkWidget *column_length(const struct queuelike *ql,
73 const struct queue_entry *q,
75 static int draggable_row(const struct queue_entry *q);
77 static const struct tabtype tabtype_queue; /* forward */
79 static const GtkTargetEntry dragtargets[] = {
80 { (char *)"disobedience-queue", GTK_TARGET_SAME_APP, 0 }
82 #define NDRAGTARGETS (int)(sizeof dragtargets / sizeof *dragtargets)
84 /* Definition of a column */
86 const char *name; /* Column name */
87 GtkWidget *(*widget)(const struct queuelike *ql,
88 const struct queue_entry *q,
89 const char *data); /* Make a label for this column */
90 const char *data; /* Data to pass to widget() */
91 gfloat xalign; /* Alignment of the label */
94 /* Need this in the middle of the types for NCOLUMNS */
95 static const struct column columns[] = {
96 { "When", column_when, 0, 1 },
97 { "Who", column_who, 0, 0 },
98 { "Artist", column_namepart, "artist", 0 },
99 { "Album", column_namepart, "album", 0 },
100 { "Title", column_namepart, "title", 0 },
101 { "Length", column_length, 0, 1 }
103 #define NCOLUMNS (int)(sizeof columns / sizeof *columns)
105 /* Data passed to menu item activation handlers */
106 struct menuiteminfo {
107 struct queuelike *ql; /* which queue we're dealing with */
108 struct queue_entry *q; /* hovered entry or 0 */
113 const char *name; /* name */
116 void (*activate)(GtkMenuItem *menuitem,
118 /* Called to activate the menu item. The user data is the queue entry that
119 * the pointer was over when the menu popped up. */
121 int (*sensitive)(struct queuelike *ql,
123 struct queue_entry *q);
124 /* Called to determine whether the menu item is usable. Returns TRUE if it
125 * should be sensitive and FALSE otherwise. Q points to the queue entry the
126 * pointer is over. */
129 gulong handlerid; /* signal handler ID */
130 GtkWidget *w; /* menu item widget */
135 const char *name; /* queue or recent */
138 void (*notify)(void);
139 /* Called when an update completes. */
141 struct queue_entry *(*fixup)(struct queue_entry *q);
142 /* Fix up the queue after update, or 0. Q is the list passed back from the
143 * server, the return value is assigned to ql->q. */
146 GtkWidget *mainlayout; /* main layout */
147 GtkWidget *mainscroll; /* scroller for main layout */
148 GtkWidget *titlelayout; /* title layout */
149 GtkWidget *titlecells[NCOLUMNS + 1]; /* title cells */
150 GtkWidget **cells; /* all the cells */
151 GtkWidget *menu; /* popup menu */
152 struct menuitem *menuitems; /* menu items */
153 GtkWidget *dragmark; /* drag destination marker */
154 GtkWidget **dropzones; /* drag targets */
157 struct queue_entry *q; /* head of queue */
158 struct queue_entry *last_click; /* last click */
159 int nrows; /* number of rows */
160 int mainrowheight; /* height of one row */
161 hash *selection; /* currently selected items */
162 int swallow_release; /* swallow button release from drag */
165 static struct queuelike ql_queue, ql_recent; /* queue and recently played */
166 static struct queue_entry *actual_queue; /* actual queue */
167 static struct queue_entry *playing_track; /* currenty playing */
168 static time_t last_playing = (time_t)-1; /* when last got playing */
169 static int namepart_lookups_outstanding;
170 static int namepart_completions_deferred; /* # of completions not processed */
171 static const struct cache_type cachetype_string = { 3600 };
172 static const struct cache_type cachetype_integer = { 3600 };
173 static GtkWidget *playing_length_label;
175 /* Debugging --------------------------------------------------------------- */
178 static void describe_widget(const char *name, GtkWidget *w, int indent) {
182 fprintf(stderr, "%*s[%s]: '%s'\n", indent, "",
183 name, gtk_widget_get_name(w));
184 gdk_window_get_position(w->window, &wx, &wy);
185 gdk_drawable_get_size(GDK_DRAWABLE(w->window), &ww, &wh);
186 fprintf(stderr, "%*s window %p: %dx%d at %dx%d\n",
187 indent, "", w->window, ww, wh, wx, wy);
190 static void dump_layout(const struct queuelike *ql) {
194 const struct queue_entry *q;
196 describe_widget("mainscroll", ql->mainscroll, 0);
197 describe_widget("mainlayout", ql->mainlayout, 1);
198 for(q = ql->q, row = 0; q; q = q->next, ++row)
199 for(col = 0; col < NCOLUMNS + 1; ++col)
200 if((w = ql->cells[row * (NCOLUMNS + 1) + col])) {
201 sprintf(s, "%dx%d", row, col);
202 describe_widget(s, w, 2);
203 if(GTK_BIN(w)->child)
204 describe_widget(0, w, 3);
209 /* Track detail lookup ----------------------------------------------------- */
211 /* A namepart lookup has completed or failed. */
212 static void namepart_completed_or_failed(void) {
213 D(("namepart_completed_or_failed"));
214 --namepart_lookups_outstanding;
215 if(!namepart_lookups_outstanding || namepart_completions_deferred > 24) {
216 redisplay_queue(&ql_queue);
217 redisplay_queue(&ql_recent);
218 namepart_completions_deferred = 0;
222 /* A namepart lookup has completed. */
223 static void namepart_completed(void *v, const char *value) {
224 struct callbackdata *cbd = v;
226 D(("namepart_completed"));
227 cache_put(&cachetype_string, cbd->u.key, value);
228 ++namepart_completions_deferred;
229 namepart_completed_or_failed();
232 /* A length lookup has completed. */
233 static void length_completed(void *v, long l) {
234 struct callbackdata *cbd = v;
237 D(("namepart_completed"));
238 value = xmalloc(sizeof *value);
240 cache_put(&cachetype_integer, cbd->u.key, value);
241 ++namepart_completions_deferred;
242 namepart_completed_or_failed();
245 /* A length or namepart lookup has failed. */
246 static void namepart_protocol_error(
247 struct callbackdata attribute((unused)) *cbd,
248 int attribute((unused)) code,
250 D(("namepart_protocol_error"));
251 gtk_label_set_text(GTK_LABEL(report_label), msg);
252 namepart_completed_or_failed();
255 /* Arrange to fill in a namepart cache entry */
256 static void namepart_fill(const char *track,
260 struct callbackdata *cbd;
262 ++namepart_lookups_outstanding;
263 cbd = xmalloc(sizeof *cbd);
264 cbd->onerror = namepart_protocol_error;
266 disorder_eclient_namepart(client, namepart_completed,
267 track, context, part, cbd);
270 /* Look up a namepart. If it is in the cache then just return its value. If
271 * not then look it up and arrange for the queues to be updated when its value
273 static const char *namepart(const char *track,
279 D(("namepart %s %s %s", track, context, part));
280 byte_xasprintf(&key, "namepart context=%s part=%s track=%s",
281 context, part, track);
282 value = cache_get(&cachetype_string, key);
285 /* stick a value in the cache so we don't issue another lookup if we
287 cache_put(&cachetype_string, key, value = "?");
288 namepart_fill(track, context, part, key);
293 /* Called from properties.c when we know a name part has changed */
294 void namepart_update(const char *track,
299 byte_xasprintf(&key, "namepart context=%s part=%s track=%s",
300 context, part, track);
301 /* Only refetch if it's actually in the cache */
302 if(cache_get(&cachetype_string, key))
303 namepart_fill(track, context, part, key);
306 /* Look up a track length. If it is in the cache then just return its value.
307 * If not then look it up and arrange for the queues to be updated when its
308 * value is available. */
309 static long getlength(const char *track) {
312 struct callbackdata *cbd;
313 static const long bogus = -1;
315 D(("getlength %s", track));
316 byte_xasprintf(&key, "length track=%s", track);
317 value = cache_get(&cachetype_integer, key);
319 D(("deferring..."));;
320 cache_put(&cachetype_integer, key, value = &bogus);
321 ++namepart_lookups_outstanding;
322 cbd = xmalloc(sizeof *cbd);
323 cbd->onerror = namepart_protocol_error;
325 disorder_eclient_length(client, length_completed, track, cbd);
330 /* Column constructors ----------------------------------------------------- */
332 /* Format the 'when' column */
333 static GtkWidget *column_when(const struct queuelike attribute((unused)) *ql,
334 const struct queue_entry *q,
335 const char attribute((unused)) *data) {
342 case playing_isscratch:
343 case playing_unplayed:
348 case playing_no_player:
350 case playing_scratched:
351 case playing_started:
353 case playing_quitting:
361 strftime(when, sizeof when, "%H:%M", localtime_r(&t, &tm));
364 return gtk_label_new(when);
367 /* Format the 'who' column */
368 static GtkWidget *column_who(const struct queuelike attribute((unused)) *ql,
369 const struct queue_entry *q,
370 const char attribute((unused)) *data) {
372 return gtk_label_new(q->submitter ? q->submitter : "");
375 /* Format one of the track name columns */
376 static GtkWidget *column_namepart(const struct queuelike
377 attribute((unused)) *ql,
378 const struct queue_entry *q,
380 D(("column_namepart"));
381 return gtk_label_new(namepart(q->track, "display", data));
384 /* Compute the length field */
385 static const char *text_length(const struct queue_entry *q) {
388 char *played = 0, *length = 0;
390 /* Work out what to say for the length */
391 l = getlength(q->track);
393 byte_xasprintf(&length, "%ld:%02ld", l / 60, l % 60);
395 byte_xasprintf(&length, "?:??");
396 /* For the currently playing track we want to report how much of the track
398 if(q == playing_track) {
399 /* log_state() arranges that we re-get the playing data whenever the
400 * pause/resume state changes */
401 if(last_state & DISORDER_TRACK_PAUSED)
402 l = playing_track->sofar;
405 l = playing_track->sofar + (now - last_playing);
407 byte_xasprintf(&played, "%ld:%02ld/%s", l / 60, l % 60, length);
413 /* Format the length column */
414 static GtkWidget *column_length(const struct queuelike attribute((unused)) *ql,
415 const struct queue_entry *q,
416 const char attribute((unused)) *data) {
417 D(("column_length"));
418 if(q == playing_track) {
419 assert(!playing_length_label);
420 playing_length_label = gtk_label_new(text_length(q));
421 /* Zot playing_length_label when it is destroyed */
422 g_signal_connect(playing_length_label, "destroy",
423 G_CALLBACK(gtk_widget_destroyed), &playing_length_label);
424 return playing_length_label;
426 return gtk_label_new(text_length(q));
429 /* Apply a new queue contents, transferring the selection from the old value */
430 static void update_queue(struct queuelike *ql, struct queue_entry *newq) {
431 struct queue_entry *q;
434 /* Propagate last_click across the change */
436 for(q = newq; q; q = q->next) {
437 if(!strcmp(q->id, ql->last_click->id))
442 /* Tell every queue entry which queue owns it */
443 for(q = newq; q; q = q->next)
445 /* Switch to the new queue */
447 /* Clean up any selected items that have fallen off */
448 for(q = ql->q; q; q = q->next)
449 selection_live(ql->selection, q->id);
450 selection_cleanup(ql->selection);
453 /* Wrap up a widget for putting into the queue or title */
454 static GtkWidget *wrap_queue_cell(GtkWidget *label,
460 D(("wrap_queue_cell"));
461 /* Padding should be in the label so there are no gaps in the
463 gtk_misc_set_padding(GTK_MISC(label), HCELLPADDING, VCELLPADDING);
464 /* Event box is just to hold a background color */
465 bg = gtk_event_box_new();
466 gtk_container_add(GTK_CONTAINER(bg), label);
468 /* Update maximum width */
469 gtk_widget_size_request(label, &req);
470 if(req.width > *wp) *wp = req.width;
472 /* Set widget names */
473 gtk_widget_set_name(bg, name);
474 gtk_widget_set_name(label, name);
478 /* Create the wrapped widget for a cell in the queue display */
479 static GtkWidget *get_queue_cell(struct queuelike *ql,
480 const struct queue_entry *q,
486 D(("get_queue_cell %d %d", row, col));
487 label = columns[col].widget(ql, q, columns[col].data);
488 gtk_misc_set_alignment(GTK_MISC(label), columns[col].xalign, 0);
489 return wrap_queue_cell(label, name, wp);
492 /* Add a padding cell to the end of a row */
493 static GtkWidget *get_padding_cell(const char *name) {
494 D(("get_padding_cell"));
495 return wrap_queue_cell(gtk_label_new(""), name, 0);
498 /* User button press and menu ---------------------------------------------- */
500 /* Update widget states in order to reflect the selection status */
501 static void set_widget_states(struct queuelike *ql) {
502 struct queue_entry *q;
505 for(q = ql->q, row = 0; q; q = q->next, ++row) {
506 for(col = 0; col < NCOLUMNS + 1; ++col)
507 gtk_widget_set_state(ql->cells[row * (NCOLUMNS + 1) + col],
508 selection_selected(ql->selection, q->id) ?
509 GTK_STATE_SELECTED : GTK_STATE_NORMAL);
511 /* Might need to change sensitivity of 'Properties' in main menu */
515 static int queue_before(const struct queue_entry *a,
516 const struct queue_entry *b) {
522 /* A button was pressed and released */
523 static gboolean queuelike_button_released(GtkWidget attribute((unused)) *widget,
524 GdkEventButton *event,
525 gpointer user_data) {
526 struct queue_entry *q = user_data, *qq;
527 struct queuelike *ql = q->ql;
528 struct menuiteminfo *mii;
531 /* Might be a release left over from a drag */
532 if(ql->swallow_release) {
533 ql->swallow_release = 0;
534 return FALSE; /* propagate */
537 if(event->type == GDK_BUTTON_PRESS
538 && event->button == 3) {
539 /* Right button click.
540 * If the current item is not selected then switch the selection to just
542 if(q && !selection_selected(ql->selection, q->id)) {
543 selection_empty(ql->selection);
544 selection_set(ql->selection, q->id, 1);
546 set_widget_states(ql);
548 /* Set the sensitivity of each menu item and (re-)establish the signal
550 for(n = 0; ql->menuitems[n].name; ++n) {
551 if(ql->menuitems[n].handlerid)
552 g_signal_handler_disconnect(ql->menuitems[n].w,
553 ql->menuitems[n].handlerid);
554 gtk_widget_set_sensitive(ql->menuitems[n].w,
555 ql->menuitems[n].sensitive(ql,
558 mii = xmalloc(sizeof *mii);
561 ql->menuitems[n].handlerid = g_signal_connect
562 (ql->menuitems[n].w, "activate",
563 G_CALLBACK(ql->menuitems[n].activate), mii);
565 /* Update the menu according to context */
566 gtk_widget_show_all(ql->menu);
567 gtk_menu_popup(GTK_MENU(ql->menu), 0, 0, 0, 0,
568 event->button, event->time);
569 return TRUE; /* hide the click from other widgets */
571 if(event->type == GDK_BUTTON_RELEASE
572 && event->button == 1) {
573 /* no modifiers: select this, unselect everything else, set last click
574 * +ctrl: flip selection of this, set last click
575 * +shift: select from last click to here, don't set last click
576 * +ctrl+shift: select from last click to here, set last click
578 switch(event->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) {
580 selection_empty(ql->selection);
581 selection_set(ql->selection, q->id, 1);
584 case GDK_CONTROL_MASK:
585 selection_flip(ql->selection, q->id);
589 case GDK_SHIFT_MASK|GDK_CONTROL_MASK:
591 if(!(event->state & GDK_CONTROL_MASK))
592 selection_empty(ql->selection);
593 selection_set(ql->selection, q->id, 1);
595 if(queue_before(ql->last_click, q))
596 while(qq != ql->last_click) {
598 selection_set(ql->selection, qq->id, 1);
601 while(qq != ql->last_click) {
603 selection_set(ql->selection, qq->id, 1);
605 if(event->state & GDK_CONTROL_MASK)
610 set_widget_states(ql);
611 gtk_widget_queue_draw(ql->mainlayout);
613 return FALSE; /* propagate */
616 /* A button was pressed or released on the mainlayout. For debugging only at
618 static gboolean mainlayout_button(GtkWidget attribute((unused)) *widget,
619 GdkEventButton attribute((unused)) *event,
620 gpointer attribute((unused)) user_data) {
621 return FALSE; /* propagate */
624 void queue_select_all(struct queuelike *ql) {
625 struct queue_entry *qq;
627 for(qq = ql->q; qq; qq = qq->next)
628 selection_set(ql->selection, qq->id, 1);
630 set_widget_states(ql);
633 void queue_properties(struct queuelike *ql) {
635 const struct queue_entry *qq;
638 for(qq = ql->q; qq; qq = qq->next)
639 if(selection_selected(ql->selection, qq->id))
640 vector_append(&v, (char *)qq->track);
642 properties(v.nvec, v.vec);
645 /* Drag and drop rearrangement --------------------------------------------- */
647 static int draggable_row(const struct queue_entry *q) {
648 return q->ql == &ql_queue && q != playing_track;
651 /* Called when a drag begings */
652 static void queue_drag_begin(GtkWidget attribute((unused)) *widget,
653 GdkDragContext attribute((unused)) *dc,
655 struct queue_entry *q = data;
656 struct queuelike *ql = q->ql;
658 /* Make sure the playing track is not selected, since it cannot be dragged */
660 selection_set(ql->selection, playing_track->id, 0);
661 /* If the dragged item is not in the selection then change the selection to
663 if(!selection_selected(ql->selection, q->id)) {
664 selection_empty(ql->selection);
665 selection_set(ql->selection, q->id, 1);
666 set_widget_states(ql);
668 /* Ignore the eventual button release */
669 ql->swallow_release = 1;
670 /* Create dropzones */
671 add_drag_targets(ql);
674 /* Convert an ID back into a queue entry and a screen row number */
675 static struct queue_entry *findentry(struct queuelike *ql,
679 struct queue_entry *q;
682 for(q = ql->q, row = 0; q && strcmp(q->id, id); q = q->next, ++row)
686 row = playing_track ? 0 : -1;
688 if(rowp) *rowp = row;
692 /* Called when data is dropped */
693 static gboolean queue_drag_drop(GtkWidget attribute((unused)) *widget,
694 GdkDragContext *drag_context,
695 gint attribute((unused)) x,
696 gint attribute((unused)) y,
698 gpointer user_data) {
699 struct queuelike *ql = &ql_queue;
700 const char *id = user_data;
702 struct queue_entry *q;
704 if(!id || (playing_track && !strcmp(id, playing_track->id)))
707 for(q = ql->q; q; q = q->next)
708 if(q != playing_track && selection_selected(ql->selection, q->id))
709 vector_append(&vec, (char *)q->id);
710 disorder_eclient_moveafter(client, id, vec.nvec, (const char **)vec.vec,
711 0/*completed*/, 0/*v*/);
712 gtk_drag_finish(drag_context, TRUE, TRUE, when);
713 /* Destroy dropzones */
714 remove_drag_targets(ql);
718 /* Called when we enter, or move within, a drop zone */
719 static gboolean queue_drag_motion(GtkWidget attribute((unused)) *widget,
720 GdkDragContext *drag_context,
721 gint attribute((unused)) x,
722 gint attribute((unused)) y,
724 gpointer user_data) {
725 struct queuelike *ql = &ql_queue;
726 const char *id = user_data;
728 struct queue_entry *q = findentry(ql, id, &row);
732 ql->dragmark = gtk_event_box_new();
733 g_signal_connect(ql->dragmark, "destroy",
734 G_CALLBACK(gtk_widget_destroyed), &ql->dragmark);
735 gtk_widget_set_size_request(ql->dragmark, 10240, row ? 4 : 2);
736 gtk_widget_set_name(ql->dragmark, "queue-drag");
737 gtk_layout_put(GTK_LAYOUT(ql->mainlayout), ql->dragmark, 0,
738 (row + 1) * ql->mainrowheight - !!row);
740 gtk_layout_move(GTK_LAYOUT(ql->mainlayout), ql->dragmark, 0,
741 (row + 1) * ql->mainrowheight - !!row);
742 gtk_widget_show(ql->dragmark);
743 gdk_drag_status(drag_context, GDK_ACTION_MOVE, when);
746 /* ID has gone AWOL */
750 /* Called when we leave a drop zone */
751 static void queue_drag_leave(GtkWidget attribute((unused)) *widget,
752 GdkDragContext attribute((unused)) *drag_context,
753 guint attribute((unused)) when,
754 gpointer attribute((unused)) user_data) {
755 struct queuelike *ql = &ql_queue;
758 gtk_widget_hide(ql->dragmark);
761 /* Add a drag target at position Y. ID is the track to insert the moved tracks
762 * after, and might be 0 to insert before the start. */
763 static void add_drag_target(struct queuelike *ql, int y, int row,
767 assert(ql->dropzones[row] == 0);
768 eventbox = gtk_event_box_new();
769 /* Make the target zone invisible */
770 gtk_event_box_set_visible_window(GTK_EVENT_BOX(eventbox), FALSE);
771 /* Make it large enough */
772 gtk_widget_set_size_request(eventbox, 10240,
773 y ? ql->mainrowheight : ql->mainrowheight / 2);
775 gtk_layout_put(GTK_LAYOUT(ql->mainlayout), eventbox, 0,
776 y ? y - ql->mainrowheight / 2 : 0);
777 /* Mark it as capable of receiving drops */
778 gtk_drag_dest_set(eventbox,
780 dragtargets, NDRAGTARGETS, GDK_ACTION_MOVE);
781 g_signal_connect(eventbox, "drag-drop",
782 G_CALLBACK(queue_drag_drop), (char *)id);
783 /* Monitor drag motion */
784 g_signal_connect(eventbox, "drag-motion",
785 G_CALLBACK(queue_drag_motion), (char *)id);
786 g_signal_connect(eventbox, "drag-leave",
787 G_CALLBACK(queue_drag_leave), (char *)id);
788 /* The widget needs to be shown to receive drags */
789 gtk_widget_show(eventbox);
790 /* Remember the drag targets */
791 ql->dropzones[row] = eventbox;
792 g_signal_connect(eventbox, "destroy",
793 G_CALLBACK(gtk_widget_destroyed), &ql->dropzones[row]);
796 /* Create dropzones for dragging into */
797 static void add_drag_targets(struct queuelike *ql) {
799 struct queue_entry *q;
801 /* Create an array to store the widgets */
802 ql->dropzones = xcalloc(ql->nrows, sizeof (GtkWidget *));
804 /* Add a drag target before the first row provided it's not the playing
806 if(!playing_track || ql->q != playing_track)
807 add_drag_target(ql, 0, 0, 0);
808 /* Put a drag target at the bottom of every row */
809 for(q = ql->q, row = 0; q; q = q->next, ++row) {
810 y += ql->mainrowheight;
811 add_drag_target(ql, y, row, q->id);
815 /* Remove the dropzones */
816 static void remove_drag_targets(struct queuelike *ql) {
819 for(row = 0; row < ql->nrows; ++row) {
820 if(ql->dropzones[row]) {
821 gtk_widget_destroy(ql->dropzones[row]);
823 assert(ql->dropzones[row] == 0);
827 /* Layout ------------------------------------------------------------------ */
829 /* Redisplay the queue. */
830 static void redisplay_queue(struct queuelike *ql) {
831 struct queue_entry *q;
837 int maxwidths[NCOLUMNS], x, y, titlerowheight;
838 int totalwidth = 10240; /* TODO: can we be less blunt */
840 D(("redisplay_queue"));
841 /* Eliminate all the existing widgets and start from scratch */
842 for(c = gtk_container_get_children(GTK_CONTAINER(ql->mainlayout));
845 /* Destroy both the label and the eventbox */
846 if(GTK_BIN(c->data)->child)
847 gtk_widget_destroy(GTK_BIN(c->data)->child);
848 gtk_widget_destroy(GTK_WIDGET(c->data));
850 /* Adjust the row count */
851 for(q = ql->q, ql->nrows = 0; q; q = q->next)
853 /* We need to create all the widgets before we can position them */
854 ql->cells = xcalloc(ql->nrows * (NCOLUMNS + 1), sizeof *ql->cells);
855 /* Minimum width is given by the column headings */
856 for(col = 0; col < NCOLUMNS; ++col) {
857 /* Reset size so we don't inherit last iteration's maximum size */
858 gtk_widget_set_size_request(GTK_BIN(ql->titlecells[col])->child, -1, -1);
859 gtk_widget_size_request(GTK_BIN(ql->titlecells[col])->child, &req);
860 maxwidths[col] = req.width;
862 /* Find the vertical size of the title bar */
863 gtk_widget_size_request(GTK_BIN(ql->titlecells[0])->child, &req);
864 titlerowheight = req.height;
867 /* Construct the widgets */
868 for(q = ql->q, row = 0; q; q = q->next, ++row) {
869 /* Figure out the widget name for this row */
870 if(q == playing_track) name = "row-playing";
871 else name = row % 2 ? "row-even" : "row-odd";
872 /* Make the widget for each column */
873 for(col = 0; col <= NCOLUMNS; ++col) {
874 /* Create and store the widget */
876 w = get_queue_cell(ql, q, row, col, name, &maxwidths[col]);
878 w = get_padding_cell(name);
879 ql->cells[row * (NCOLUMNS + 1) + col] = w;
880 /* Maybe mark it draggable */
881 if(draggable_row(q)) {
882 gtk_drag_source_set(w, GDK_BUTTON1_MASK,
883 dragtargets, NDRAGTARGETS, GDK_ACTION_MOVE);
884 g_signal_connect(w, "drag-begin", G_CALLBACK(queue_drag_begin), q);
886 /* Catch button presses */
887 g_signal_connect(w, "button-release-event",
888 G_CALLBACK(queuelike_button_released), q);
889 g_signal_connect(w, "button-press-event",
890 G_CALLBACK(queuelike_button_released), q);
893 /* ...and of each row in the main layout */
894 gtk_widget_size_request(GTK_BIN(ql->cells[0])->child, &req);
895 ql->mainrowheight = req.height;
896 /* Now we know the maximum width of each column we can set the size of
897 * everything and position it */
898 for(row = 0, q = ql->q; row < ql->nrows; ++row, q = q->next) {
900 for(col = 0; col < NCOLUMNS; ++col) {
901 w = ql->cells[row * (NCOLUMNS + 1) + col];
902 gtk_widget_set_size_request(GTK_BIN(w)->child,
904 gtk_layout_put(GTK_LAYOUT(ql->mainlayout), w, x, y);
907 w = ql->cells[row * (NCOLUMNS + 1) + col];
908 gtk_widget_set_size_request(GTK_BIN(w)->child,
910 gtk_layout_put(GTK_LAYOUT(ql->mainlayout), w, x, y);
911 y += ql->mainrowheight;
916 for(col = 0; col < NCOLUMNS; ++col) {
917 gtk_widget_set_size_request(GTK_BIN(ql->titlecells[col])->child,
919 gtk_layout_move(GTK_LAYOUT(ql->titlelayout), ql->titlecells[col], x, 0);
922 gtk_widget_set_size_request(GTK_BIN(ql->titlecells[col])->child,
924 gtk_layout_move(GTK_LAYOUT(ql->titlelayout), ql->titlecells[col], x, 0);
926 set_widget_states(ql);
927 /* Make sure it's all visible */
928 gtk_widget_show_all(ql->mainlayout);
929 gtk_widget_show_all(ql->titlelayout);
930 /* Layouts might shrink to arrange for the area they shrink out of to be
932 gtk_widget_queue_draw(ql->mainlayout);
933 gtk_widget_queue_draw(ql->titlelayout);
934 /* Adjust the size of the layout */
935 gtk_layout_set_size(GTK_LAYOUT(ql->mainlayout), x, y);
936 gtk_layout_set_size(GTK_LAYOUT(ql->titlelayout), x, titlerowheight);
937 gtk_widget_set_size_request(ql->titlelayout, -1, titlerowheight);
940 /* Called with new queue/recent contents */
941 static void queuelike_completed(void *v, struct queue_entry *q) {
942 struct callbackdata *cbd = v;
943 struct queuelike *ql = cbd->u.ql;
945 D(("queuelike_complete"));
946 /* Install the new queue */
947 update_queue(ql, ql->fixup(q));
948 /* Update the display */
952 /* Update sensitivity of main menu items */
956 /* Called with a new currently playing track */
957 static void playing_completed(void attribute((unused)) *v,
958 struct queue_entry *q) {
959 struct callbackdata cbd;
960 D(("playing_completed"));
962 /* Record when we got the playing track data so we know how old the 'sofar'
965 cbd.u.ql = &ql_queue;
966 queuelike_completed(&cbd, actual_queue);
969 static void queue_scrolled(GtkAdjustment *adjustment,
970 gpointer user_data) {
971 GtkAdjustment *titleadj = user_data;
973 D(("queue_scrolled"));
974 gtk_adjustment_set_value(titleadj, adjustment->value);
977 /* Create a queuelike thing (queue/recent) */
978 static GtkWidget *queuelike(struct queuelike *ql,
979 struct queue_entry *(*fixup)(struct queue_entry *),
980 void (*notify)(void),
981 struct menuitem *menuitems,
983 GtkWidget *vbox, *mainscroll, *titlescroll, *label;
984 GtkAdjustment *mainadj, *titleadj;
990 ql->menuitems = menuitems;
992 ql->mainrowheight = !0; /* else division by 0 */
993 ql->selection = selection_new();
994 /* Create the layouts */
995 ql->mainlayout = gtk_layout_new(0, 0);
996 ql->titlelayout = gtk_layout_new(0, 0);
997 /* Scroll the layouts */
998 ql->mainscroll = mainscroll = scroll_widget(ql->mainlayout, name);
999 titlescroll = scroll_widget(ql->titlelayout, name);
1000 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(titlescroll),
1001 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
1002 mainadj = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(mainscroll));
1003 titleadj = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(titlescroll));
1004 g_signal_connect(mainadj, "changed", G_CALLBACK(queue_scrolled), titleadj);
1005 g_signal_connect(mainadj, "value-changed", G_CALLBACK(queue_scrolled), titleadj);
1006 /* Fill the titles and put them anywhere */
1007 for(col = 0; col < NCOLUMNS; ++col) {
1008 label = gtk_label_new(columns[col].name);
1009 gtk_misc_set_alignment(GTK_MISC(label), columns[col].xalign, 0);
1010 ql->titlecells[col] = wrap_queue_cell(label, "row-title", 0);
1011 gtk_layout_put(GTK_LAYOUT(ql->titlelayout), ql->titlecells[col], 0, 0);
1013 ql->titlecells[col] = get_padding_cell("row-title");
1014 gtk_layout_put(GTK_LAYOUT(ql->titlelayout), ql->titlecells[col], 0, 0);
1015 /* Pack the lot together in a vbox */
1016 vbox = gtk_vbox_new(0, 0);
1017 gtk_box_pack_start(GTK_BOX(vbox), titlescroll, 0, 0, 0);
1018 gtk_box_pack_start(GTK_BOX(vbox), mainscroll, 1, 1, 0);
1019 /* Create the popup menu */
1020 ql->menu = gtk_menu_new();
1021 g_signal_connect(ql->menu, "destroy",
1022 G_CALLBACK(gtk_widget_destroyed), &ql->menu);
1023 for(n = 0; menuitems[n].name; ++n) {
1024 menuitems[n].w = gtk_menu_item_new_with_label(menuitems[n].name);
1025 gtk_menu_attach(GTK_MENU(ql->menu), menuitems[n].w, 0, 1, n, n + 1);
1027 g_object_set_data(G_OBJECT(vbox), "type", (void *)&tabtype_queue);
1028 g_object_set_data(G_OBJECT(vbox), "queue", ql);
1029 /* Catch button presses */
1030 g_signal_connect(ql->mainlayout, "button-release-event",
1031 G_CALLBACK(mainlayout_button), ql);
1033 g_signal_connect(ql->mainlayout, "button-press-event",
1034 G_CALLBACK(mainlayout_button), ql);
1039 /* Popup menu items -------------------------------------------------------- */
1041 /* Count the number of items selected */
1042 static int queue_count_selected(const struct queuelike *ql) {
1043 return hash_count(ql->selection);
1046 /* Count the number of items selected */
1047 static int queue_count_entries(const struct queuelike *ql) {
1049 const struct queue_entry *q;
1051 for(q = ql->q; q; q = q->next)
1056 /* Count the number of items selected, excluding the playing track if there is
1058 static int count_selected_nonplaying(const struct queuelike *ql) {
1059 int nselected = queue_count_selected(ql);
1061 if(ql->q == playing_track && selection_selected(ql->selection, ql->q->id))
1066 static int scratch_sensitive(struct queuelike attribute((unused)) *ql,
1067 struct menuitem attribute((unused)) *m,
1068 struct queue_entry attribute((unused)) *q) {
1069 /* We can scratch if the playing track is selected */
1070 return playing_track && selection_selected(ql->selection, playing_track->id);
1073 static void scratch_activate(GtkMenuItem attribute((unused)) *menuitem,
1074 gpointer attribute((unused)) user_data) {
1076 disorder_eclient_scratch(client, playing_track->id, 0, 0);
1079 static int remove_sensitive(struct queuelike *ql,
1080 struct menuitem attribute((unused)) *m,
1081 struct queue_entry *q) {
1082 /* We can remove if we're hovering over a particular track or any non-playing
1083 * tracks are selected */
1084 return (q && q != playing_track) || count_selected_nonplaying(ql);
1087 static void remove_activate(GtkMenuItem attribute((unused)) *menuitem,
1088 gpointer user_data) {
1089 const struct menuiteminfo *mii = user_data;
1090 struct queue_entry *q = mii->q;
1091 struct queuelike *ql = mii->ql;
1093 if(count_selected_nonplaying(mii->ql)) {
1094 /* Remove selected tracks */
1095 for(q = ql->q; q; q = q->next)
1096 if(selection_selected(ql->selection, q->id) && q != playing_track)
1097 disorder_eclient_remove(client, q->id, 0, 0);
1099 /* Remove just the hovered track */
1100 disorder_eclient_remove(client, q->id, 0, 0);
1103 static int properties_sensitive(struct queuelike *ql,
1104 struct menuitem attribute((unused)) *m,
1105 struct queue_entry attribute((unused)) *q) {
1106 /* "Properties" is sensitive if at least something is selected */
1107 return hash_count(ql->selection) > 0;
1110 static void properties_activate(GtkMenuItem attribute((unused)) *menuitem,
1111 gpointer user_data) {
1112 const struct menuiteminfo *mii = user_data;
1114 queue_properties(mii->ql);
1117 static int selectall_sensitive(struct queuelike *ql,
1118 struct menuitem attribute((unused)) *m,
1119 struct queue_entry attribute((unused)) *q) {
1120 /* Sensitive if there is anything to select */
1124 static void selectall_activate(GtkMenuItem attribute((unused)) *menuitem,
1125 gpointer user_data) {
1126 const struct menuiteminfo *mii = user_data;
1127 queue_select_all(mii->ql);
1130 /* The queue --------------------------------------------------------------- */
1132 /* Fix up the queue by sticking the currently playing track on the front */
1133 static struct queue_entry *fixup_queue(struct queue_entry *q) {
1138 actual_queue->prev = playing_track;
1139 playing_track->next = actual_queue;
1140 return playing_track;
1142 return actual_queue;
1145 /* Called regularly to adjust the so-far played label (redrawing the whole
1146 * queue once a second makes disobedience occupy >10% of the CPU on my Athlon
1147 * which is ureasonable expensive) */
1148 static gboolean adjust_sofar(gpointer attribute((unused)) data) {
1149 if(playing_length_label && playing_track)
1150 gtk_label_set_text(GTK_LABEL(playing_length_label),
1151 text_length(playing_track));
1155 /* Popup menu for the queue. Put the properties first so that finger trouble
1156 * is less dangerous. */
1157 static struct menuitem queue_menu[] = {
1158 { "Properties", properties_activate, properties_sensitive, 0, 0 },
1159 { "Select all", selectall_activate, selectall_sensitive, 0, 0 },
1160 { "Scratch", scratch_activate, scratch_sensitive, 0, 0 },
1161 { "Remove", remove_activate, remove_sensitive, 0, 0 },
1165 GtkWidget *queue_widget(void) {
1166 D(("queue_widget"));
1167 /* Arrange periodic update of the so-far played field */
1168 g_timeout_add(1000/*ms*/, adjust_sofar, 0);
1169 /* We pass choose_update() as our notify function since the choose screen
1170 * marks tracks that are playing/in the queue. */
1171 return queuelike(&ql_queue, fixup_queue, choose_update, queue_menu,
1175 void queue_update(void) {
1176 struct callbackdata *cbd;
1178 D(("queue_update"));
1179 cbd = xmalloc(sizeof *cbd);
1181 cbd->u.ql = &ql_queue;
1182 gtk_label_set_text(GTK_LABEL(report_label), "updating queue");
1183 disorder_eclient_queue(client, queuelike_completed, cbd);
1186 void playing_update(void) {
1187 D(("playing_update"));
1188 gtk_label_set_text(GTK_LABEL(report_label), "updating playing track");
1189 disorder_eclient_playing(client, playing_completed, 0);
1192 /* Recently played tracks -------------------------------------------------- */
1194 static struct queue_entry *fixup_recent(struct queue_entry *q) {
1195 /* 'recent' is in the wrong order. TODO: globally fix this! */
1196 struct queue_entry *qr = 0, *qn;
1198 D(("fixup_recent"));
1201 /* Swap next/prev pointers */
1204 /* Remember last node for new head */
1212 static struct menuitem recent_menu[] = {
1213 { "Properties", properties_activate, properties_sensitive,0, 0 },
1214 { "Select all", selectall_activate, selectall_sensitive, 0, 0 },
1218 GtkWidget *recent_widget(void) {
1219 D(("recent_widget"));
1220 return queuelike(&ql_recent, fixup_recent, 0, recent_menu, "recent");
1223 void recent_update(void) {
1224 struct callbackdata *cbd;
1226 D(("recent_update"));
1227 cbd = xmalloc(sizeof *cbd);
1229 cbd->u.ql = &ql_recent;
1230 gtk_label_set_text(GTK_LABEL(report_label), "updating recently played list");
1231 disorder_eclient_recent(client, queuelike_completed, cbd);
1234 /* Main menu plumbing ------------------------------------------------------ */
1236 static int queue_properties_sensitive(GtkWidget *w) {
1237 return !!queue_count_selected(g_object_get_data(G_OBJECT(w), "queue"));
1240 static int queue_selectall_sensitive(GtkWidget *w) {
1241 return !!queue_count_entries(g_object_get_data(G_OBJECT(w), "queue"));
1244 static void queue_properties_activate(GtkWidget *w) {
1245 queue_properties(g_object_get_data(G_OBJECT(w), "queue"));
1248 static void queue_selectall_activate(GtkWidget *w) {
1249 queue_select_all(g_object_get_data(G_OBJECT(w), "queue"));
1252 static const struct tabtype tabtype_queue = {
1253 queue_properties_sensitive,
1254 queue_selectall_sensitive,
1255 queue_properties_activate,
1256 queue_selectall_activate,
1259 /* Other entry points ------------------------------------------------------ */
1261 int queued(const char *track) {
1262 struct queue_entry *q;
1264 D(("queued %s", track));
1265 for(q = ql_queue.q; q; q = q->next)
1266 if(!strcmp(q->track, track))
1276 indent-tabs-mode:nil
1279 /* arch-tag:kxVCqYNfvMJkYlkf2Pn8pg */