chiark / gitweb /
doxygen
[disorder] / disobedience / queue.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2006 Richard Kettlewell
4 *
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.
9 *
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.
14 *
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
18 * USA
19 */
3ffa2d15
RK
20/** @file disobedience/queue.c
21 * @brief Queue widgets
22 *
23 * This file provides both the queue widget and the recently-played widget.
460b9539 24 *
717ba987
RK
25 * A queue layout is structured as follows:
26 *
27 * <pre>
460b9539 28 * vbox
29 * titlescroll
30 * titlelayout
31 * titlecells[col] eventbox (made by wrap_queue_cell)
32 * titlecells[col]->child label (from columns[])
33 * mainscroll
34 * mainlayout
35 * cells[row * N + c] eventbox (made by wrap_queue_cell)
36 * cells[row * N + c]->child label (from column constructors)
717ba987 37 * </pre>
460b9539 38 *
39 * titlescroll never has any scrollbars. Instead whenever mainscroll's
40 * horizontal adjustment is changed, queue_scrolled adjusts titlescroll to
41 * match, forcing the title and the queue to pan in sync but allowing the queue
42 * to scroll independently.
43 *
44 * Whenever the queue changes everything below mainlayout is thrown away and
45 * reconstructed from scratch. Name lookups are cached, so this doesn't imply
46 * lots of disorder protocol traffic.
47 *
48 * The last cell on each row is the padding cell, and this extends ridiculously
49 * far to the right. (Can we do better?)
50 *
51 * When drag and drop is active we create extra eventboxes to act as dropzones.
52 * These only exist while the drag proceeds, as otherwise they steal events
53 * from more deserving widgets. (It might work to hide them when not in use
54 * too but this way around the d+d code is a bit more self-contained.)
55 */
56
717ba987
RK
57#include "disobedience.h"
58
59/** @brief Horizontal padding for queue cells */
60#define HCELLPADDING 4
61
62/** @brief Vertical padding for queue cells */
63#define VCELLPADDING 2
64
460b9539 65/* Queue management -------------------------------------------------------- */
66
e9b70a84 67WT(label);
68WT(event_box);
69WT(menu);
70WT(menu_item);
71WT(layout);
72WT(vbox);
73
460b9539 74struct queuelike;
75
76static void add_drag_targets(struct queuelike *ql);
77static void remove_drag_targets(struct queuelike *ql);
78static void redisplay_queue(struct queuelike *ql);
79static GtkWidget *column_when(const struct queuelike *ql,
80 const struct queue_entry *q,
81 const char *data);
82static GtkWidget *column_who(const struct queuelike *ql,
83 const struct queue_entry *q,
84 const char *data);
85static GtkWidget *column_namepart(const struct queuelike *ql,
86 const struct queue_entry *q,
87 const char *data);
88static GtkWidget *column_length(const struct queuelike *ql,
89 const struct queue_entry *q,
90 const char *data);
91static int draggable_row(const struct queue_entry *q);
92
93static const struct tabtype tabtype_queue; /* forward */
94
95static const GtkTargetEntry dragtargets[] = {
96 { (char *)"disobedience-queue", GTK_TARGET_SAME_APP, 0 }
97};
98#define NDRAGTARGETS (int)(sizeof dragtargets / sizeof *dragtargets)
99
717ba987 100/** @brief Definition of a column */
460b9539 101struct column {
102 const char *name; /* Column name */
103 GtkWidget *(*widget)(const struct queuelike *ql,
104 const struct queue_entry *q,
105 const char *data); /* Make a label for this column */
106 const char *data; /* Data to pass to widget() */
107 gfloat xalign; /* Alignment of the label */
108};
109
717ba987
RK
110/** @brief Table of columns
111 *
112 * Need this in the middle of the types for NCOLUMNS
113 */
460b9539 114static const struct column columns[] = {
115 { "When", column_when, 0, 1 },
116 { "Who", column_who, 0, 0 },
117 { "Artist", column_namepart, "artist", 0 },
118 { "Album", column_namepart, "album", 0 },
119 { "Title", column_namepart, "title", 0 },
120 { "Length", column_length, 0, 1 }
121};
717ba987
RK
122
123/** @brief Number of columns */
460b9539 124#define NCOLUMNS (int)(sizeof columns / sizeof *columns)
125
717ba987 126/** @brief Data passed to menu item activation handlers */
460b9539 127struct menuiteminfo {
717ba987
RK
128 struct queuelike *ql; /**< @brief which queue we're dealing with */
129 struct queue_entry *q; /**< @brief hovered entry or 0 */
460b9539 130};
131
717ba987 132/** @brief An item in the queue's popup menu */
460b9539 133struct menuitem {
717ba987
RK
134 /** @brief Menu item name */
135 const char *name;
460b9539 136
717ba987
RK
137 /** @brief Called to activate the menu item
138 *
139 * The user data is the queue entry that the pointer was over when the menu
140 * popped up. */
460b9539 141 void (*activate)(GtkMenuItem *menuitem,
142 gpointer user_data);
460b9539 143
717ba987
RK
144 /** @brief Called to determine whether the menu item is usable.
145 *
146 * Returns @c TRUE if it should be sensitive and @c FALSE otherwise. @p q
147 * points to the queue entry the pointer is over.
148 */
460b9539 149 int (*sensitive)(struct queuelike *ql,
150 struct menuitem *m,
151 struct queue_entry *q);
460b9539 152
717ba987
RK
153 /** @brief Signal handler ID */
154 gulong handlerid;
155
156 /** @brief Widget for menu item */
157 GtkWidget *w;
460b9539 158};
159
717ba987
RK
160/** @brief A queue-like object
161 *
162 * There are (currently) two of these: @ref ql_queue and @ref ql_recent.
163 */
460b9539 164struct queuelike {
717ba987
RK
165 /** @brief Name of this queue */
166 const char *name;
460b9539 167
717ba987 168 /** @brief Called when an update completes */
460b9539 169 void (*notify)(void);
717ba987
RK
170
171 /** @brief Called to fix up the queue after update
172 * @param q The list passed back from the server
173 * @return Assigned to @c ql->q
174 */
460b9539 175 struct queue_entry *(*fixup)(struct queue_entry *q);
460b9539 176
177 /* Widgets */
717ba987
RK
178 GtkWidget *mainlayout; /**< @brief main layout */
179 GtkWidget *mainscroll; /**< @brief scroller for main layout */
180 GtkWidget *titlelayout; /**< @brief title layout */
181 GtkWidget *titlecells[NCOLUMNS + 1]; /**< @brief title cells */
182 GtkWidget **cells; /**< @brief all the cells */
183 GtkWidget *menu; /**< @brief popup menu */
184 struct menuitem *menuitems; /**< @brief menu items */
185 GtkWidget *dragmark; /**< @brief drag destination marker */
186 GtkWidget **dropzones; /**< @brief drag targets */
460b9539 187
188 /* State */
717ba987
RK
189 struct queue_entry *q; /**< @brief head of queue */
190 struct queue_entry *last_click; /**< @brief last click */
191 int nrows; /**< @brief number of rows */
192 int mainrowheight; /**< @brief height of one row */
193 hash *selection; /**< @brief currently selected items */
194 int swallow_release; /**< @brief swallow button release from drag */
460b9539 195};
196
717ba987
RK
197static struct queuelike ql_queue; /**< @brief The main queue */
198static struct queuelike ql_recent; /*< @brief Recently-played tracks */
199static struct queue_entry *actual_queue; /**< @brief actual queue */
200static struct queue_entry *playing_track; /**< @brief currenty playing */
201static time_t last_playing = (time_t)-1; /**< @brief when last got playing */
460b9539 202static int namepart_lookups_outstanding;
203static int namepart_completions_deferred; /* # of completions not processed */
204static const struct cache_type cachetype_string = { 3600 };
205static const struct cache_type cachetype_integer = { 3600 };
206static GtkWidget *playing_length_label;
207
208/* Debugging --------------------------------------------------------------- */
209
210#if 0
211static void describe_widget(const char *name, GtkWidget *w, int indent) {
212 int ww, wh, wx, wy;
213
214 if(name)
215 fprintf(stderr, "%*s[%s]: '%s'\n", indent, "",
216 name, gtk_widget_get_name(w));
217 gdk_window_get_position(w->window, &wx, &wy);
218 gdk_drawable_get_size(GDK_DRAWABLE(w->window), &ww, &wh);
219 fprintf(stderr, "%*s window %p: %dx%d at %dx%d\n",
220 indent, "", w->window, ww, wh, wx, wy);
221}
222
223static void dump_layout(const struct queuelike *ql) {
224 GtkWidget *w;
225 char s[20];
226 int row, col;
227 const struct queue_entry *q;
228
229 describe_widget("mainscroll", ql->mainscroll, 0);
230 describe_widget("mainlayout", ql->mainlayout, 1);
231 for(q = ql->q, row = 0; q; q = q->next, ++row)
232 for(col = 0; col < NCOLUMNS + 1; ++col)
233 if((w = ql->cells[row * (NCOLUMNS + 1) + col])) {
234 sprintf(s, "%dx%d", row, col);
235 describe_widget(s, w, 2);
236 if(GTK_BIN(w)->child)
237 describe_widget(0, w, 3);
238 }
239}
240#endif
241
242/* Track detail lookup ----------------------------------------------------- */
243
717ba987 244/** @brief Called when a namepart lookup has completed or failed */
460b9539 245static void namepart_completed_or_failed(void) {
246 D(("namepart_completed_or_failed"));
247 --namepart_lookups_outstanding;
248 if(!namepart_lookups_outstanding || namepart_completions_deferred > 24) {
249 redisplay_queue(&ql_queue);
250 redisplay_queue(&ql_recent);
251 namepart_completions_deferred = 0;
252 }
253}
254
717ba987 255/** @brief Called when A namepart lookup has completed */
460b9539 256static void namepart_completed(void *v, const char *value) {
257 struct callbackdata *cbd = v;
258
259 D(("namepart_completed"));
260 cache_put(&cachetype_string, cbd->u.key, value);
261 ++namepart_completions_deferred;
262 namepart_completed_or_failed();
263}
264
717ba987 265/** @brief Called when a length lookup has completed */
460b9539 266static void length_completed(void *v, long l) {
267 struct callbackdata *cbd = v;
268 long *value;
269
270 D(("namepart_completed"));
271 value = xmalloc(sizeof *value);
272 *value = l;
273 cache_put(&cachetype_integer, cbd->u.key, value);
274 ++namepart_completions_deferred;
275 namepart_completed_or_failed();
276}
277
717ba987 278/** @brief Called when a length or namepart lookup has failed */
460b9539 279static void namepart_protocol_error(
280 struct callbackdata attribute((unused)) *cbd,
281 int attribute((unused)) code,
282 const char *msg) {
283 D(("namepart_protocol_error"));
284 gtk_label_set_text(GTK_LABEL(report_label), msg);
285 namepart_completed_or_failed();
286}
287
717ba987 288/** @brief Arrange to fill in a namepart cache entry */
460b9539 289static void namepart_fill(const char *track,
290 const char *context,
291 const char *part,
292 const char *key) {
293 struct callbackdata *cbd;
294
295 ++namepart_lookups_outstanding;
296 cbd = xmalloc(sizeof *cbd);
297 cbd->onerror = namepart_protocol_error;
298 cbd->u.key = key;
299 disorder_eclient_namepart(client, namepart_completed,
300 track, context, part, cbd);
301}
302
717ba987
RK
303/** @brief Look up a namepart
304 *
305 * If it is in the cache then just return its value. If not then look it up
306 * and arrange for the queues to be updated when its value is available. */
460b9539 307static const char *namepart(const char *track,
308 const char *context,
309 const char *part) {
310 char *key;
311 const char *value;
312
313 D(("namepart %s %s %s", track, context, part));
314 byte_xasprintf(&key, "namepart context=%s part=%s track=%s",
315 context, part, track);
316 value = cache_get(&cachetype_string, key);
317 if(!value) {
318 D(("deferring..."));
319 /* stick a value in the cache so we don't issue another lookup if we
320 * revisit */
321 cache_put(&cachetype_string, key, value = "?");
322 namepart_fill(track, context, part, key);
323 }
324 return value;
325}
326
717ba987 327/** @brief Called from @ref disobedience/properties.c when we know a name part has changed */
460b9539 328void namepart_update(const char *track,
329 const char *context,
330 const char *part) {
331 char *key;
332
333 byte_xasprintf(&key, "namepart context=%s part=%s track=%s",
334 context, part, track);
335 /* Only refetch if it's actually in the cache */
336 if(cache_get(&cachetype_string, key))
337 namepart_fill(track, context, part, key);
338}
339
717ba987
RK
340/** @brief Look up a track length
341 *
342 * If it is in the cache then just return its value. If not then look it up
343 * and arrange for the queues to be updated when its value is available. */
460b9539 344static long getlength(const char *track) {
345 char *key;
346 const long *value;
347 struct callbackdata *cbd;
348 static const long bogus = -1;
349
350 D(("getlength %s", track));
351 byte_xasprintf(&key, "length track=%s", track);
352 value = cache_get(&cachetype_integer, key);
353 if(!value) {
354 D(("deferring..."));;
355 cache_put(&cachetype_integer, key, value = &bogus);
356 ++namepart_lookups_outstanding;
357 cbd = xmalloc(sizeof *cbd);
358 cbd->onerror = namepart_protocol_error;
359 cbd->u.key = key;
360 disorder_eclient_length(client, length_completed, track, cbd);
361 }
362 return *value;
363}
364
365/* Column constructors ----------------------------------------------------- */
366
717ba987 367/** @brief Format the 'when' column */
460b9539 368static GtkWidget *column_when(const struct queuelike attribute((unused)) *ql,
369 const struct queue_entry *q,
370 const char attribute((unused)) *data) {
371 char when[64];
372 struct tm tm;
373 time_t t;
374
375 D(("column_when"));
376 switch(q->state) {
377 case playing_isscratch:
378 case playing_unplayed:
379 case playing_random:
380 t = q->expected;
381 break;
382 case playing_failed:
383 case playing_no_player:
384 case playing_ok:
385 case playing_scratched:
386 case playing_started:
387 case playing_paused:
388 case playing_quitting:
389 t = q->played;
390 break;
391 default:
392 t = 0;
393 break;
394 }
395 if(t)
396 strftime(when, sizeof when, "%H:%M", localtime_r(&t, &tm));
397 else
398 when[0] = 0;
e9b70a84 399 NW(label);
460b9539 400 return gtk_label_new(when);
401}
402
717ba987 403/** @brief Format the 'who' column */
460b9539 404static GtkWidget *column_who(const struct queuelike attribute((unused)) *ql,
405 const struct queue_entry *q,
406 const char attribute((unused)) *data) {
407 D(("column_who"));
e9b70a84 408 NW(label);
460b9539 409 return gtk_label_new(q->submitter ? q->submitter : "");
410}
411
717ba987 412/** @brief Format one of the track name columns */
460b9539 413static GtkWidget *column_namepart(const struct queuelike
414 attribute((unused)) *ql,
415 const struct queue_entry *q,
416 const char *data) {
417 D(("column_namepart"));
e9b70a84 418 NW(label);
460b9539 419 return gtk_label_new(namepart(q->track, "display", data));
420}
421
717ba987 422/** @brief Compute the length field */
460b9539 423static const char *text_length(const struct queue_entry *q) {
424 long l;
425 time_t now;
426 char *played = 0, *length = 0;
427
428 /* Work out what to say for the length */
429 l = getlength(q->track);
430 if(l > 0)
431 byte_xasprintf(&length, "%ld:%02ld", l / 60, l % 60);
432 else
433 byte_xasprintf(&length, "?:??");
434 /* For the currently playing track we want to report how much of the track
435 * has been played */
436 if(q == playing_track) {
437 /* log_state() arranges that we re-get the playing data whenever the
438 * pause/resume state changes */
439 if(last_state & DISORDER_TRACK_PAUSED)
440 l = playing_track->sofar;
441 else {
442 time(&now);
443 l = playing_track->sofar + (now - last_playing);
444 }
445 byte_xasprintf(&played, "%ld:%02ld/%s", l / 60, l % 60, length);
446 return played;
447 } else
448 return length;
449}
450
717ba987 451/** @brief Format the length column */
460b9539 452static GtkWidget *column_length(const struct queuelike attribute((unused)) *ql,
453 const struct queue_entry *q,
454 const char attribute((unused)) *data) {
455 D(("column_length"));
456 if(q == playing_track) {
457 assert(!playing_length_label);
e9b70a84 458 NW(label);
460b9539 459 playing_length_label = gtk_label_new(text_length(q));
460 /* Zot playing_length_label when it is destroyed */
461 g_signal_connect(playing_length_label, "destroy",
462 G_CALLBACK(gtk_widget_destroyed), &playing_length_label);
463 return playing_length_label;
e9b70a84 464 } else {
465 NW(label);
460b9539 466 return gtk_label_new(text_length(q));
e9b70a84 467 }
468
460b9539 469}
470
717ba987 471/** @brief Apply a new queue contents, transferring the selection from the old value */
460b9539 472static void update_queue(struct queuelike *ql, struct queue_entry *newq) {
473 struct queue_entry *q;
474
475 D(("update_queue"));
476 /* Propagate last_click across the change */
477 if(ql->last_click) {
478 for(q = newq; q; q = q->next) {
479 if(!strcmp(q->id, ql->last_click->id))
480 break;
481 ql->last_click = q;
482 }
483 }
484 /* Tell every queue entry which queue owns it */
485 for(q = newq; q; q = q->next)
486 q->ql = ql;
487 /* Switch to the new queue */
488 ql->q = newq;
489 /* Clean up any selected items that have fallen off */
490 for(q = ql->q; q; q = q->next)
491 selection_live(ql->selection, q->id);
492 selection_cleanup(ql->selection);
493}
494
717ba987 495/** @brief Wrap up a widget for putting into the queue or title */
460b9539 496static GtkWidget *wrap_queue_cell(GtkWidget *label,
497 const char *name,
498 int *wp) {
499 GtkRequisition req;
500 GtkWidget *bg;
501
502 D(("wrap_queue_cell"));
503 /* Padding should be in the label so there are no gaps in the
504 * background */
505 gtk_misc_set_padding(GTK_MISC(label), HCELLPADDING, VCELLPADDING);
506 /* Event box is just to hold a background color */
e9b70a84 507 NW(event_box);
460b9539 508 bg = gtk_event_box_new();
509 gtk_container_add(GTK_CONTAINER(bg), label);
510 if(wp) {
511 /* Update maximum width */
512 gtk_widget_size_request(label, &req);
513 if(req.width > *wp) *wp = req.width;
514 }
515 /* Set widget names */
516 gtk_widget_set_name(bg, name);
517 gtk_widget_set_name(label, name);
518 return bg;
519}
520
717ba987 521/** @brief Create the wrapped widget for a cell in the queue display */
460b9539 522static GtkWidget *get_queue_cell(struct queuelike *ql,
523 const struct queue_entry *q,
524 int row,
525 int col,
526 const char *name,
527 int *wp) {
528 GtkWidget *label;
529 D(("get_queue_cell %d %d", row, col));
530 label = columns[col].widget(ql, q, columns[col].data);
531 gtk_misc_set_alignment(GTK_MISC(label), columns[col].xalign, 0);
532 return wrap_queue_cell(label, name, wp);
533}
534
717ba987 535/** @brief Add a padding cell to the end of a row */
460b9539 536static GtkWidget *get_padding_cell(const char *name) {
537 D(("get_padding_cell"));
e9b70a84 538 NW(label);
460b9539 539 return wrap_queue_cell(gtk_label_new(""), name, 0);
540}
541
542/* User button press and menu ---------------------------------------------- */
543
717ba987 544/** @brief Update widget states in order to reflect the selection status */
460b9539 545static void set_widget_states(struct queuelike *ql) {
546 struct queue_entry *q;
547 int row, col;
548
549 for(q = ql->q, row = 0; q; q = q->next, ++row) {
550 for(col = 0; col < NCOLUMNS + 1; ++col)
551 gtk_widget_set_state(ql->cells[row * (NCOLUMNS + 1) + col],
552 selection_selected(ql->selection, q->id) ?
553 GTK_STATE_SELECTED : GTK_STATE_NORMAL);
554 }
555 /* Might need to change sensitivity of 'Properties' in main menu */
556 menu_update(-1);
557}
558
717ba987 559/** @brief Ordering function for queue entries */
460b9539 560static int queue_before(const struct queue_entry *a,
561 const struct queue_entry *b) {
562 while(a && a != b)
563 a = a->next;
564 return !!a;
565}
566
717ba987 567/** @brief A button was pressed and released */
460b9539 568static gboolean queuelike_button_released(GtkWidget attribute((unused)) *widget,
569 GdkEventButton *event,
570 gpointer user_data) {
571 struct queue_entry *q = user_data, *qq;
572 struct queuelike *ql = q->ql;
573 struct menuiteminfo *mii;
574 int n;
575
576 /* Might be a release left over from a drag */
577 if(ql->swallow_release) {
578 ql->swallow_release = 0;
579 return FALSE; /* propagate */
580 }
581
582 if(event->type == GDK_BUTTON_PRESS
583 && event->button == 3) {
584 /* Right button click.
585 * If the current item is not selected then switch the selection to just
586 * this item */
587 if(q && !selection_selected(ql->selection, q->id)) {
588 selection_empty(ql->selection);
589 selection_set(ql->selection, q->id, 1);
590 ql->last_click = q;
591 set_widget_states(ql);
592 }
593 /* Set the sensitivity of each menu item and (re-)establish the signal
594 * handlers */
595 for(n = 0; ql->menuitems[n].name; ++n) {
596 if(ql->menuitems[n].handlerid)
597 g_signal_handler_disconnect(ql->menuitems[n].w,
598 ql->menuitems[n].handlerid);
599 gtk_widget_set_sensitive(ql->menuitems[n].w,
600 ql->menuitems[n].sensitive(ql,
601 &ql->menuitems[n],
602 q));
603 mii = xmalloc(sizeof *mii);
604 mii->ql = ql;
605 mii->q = q;
606 ql->menuitems[n].handlerid = g_signal_connect
607 (ql->menuitems[n].w, "activate",
608 G_CALLBACK(ql->menuitems[n].activate), mii);
609 }
610 /* Update the menu according to context */
611 gtk_widget_show_all(ql->menu);
612 gtk_menu_popup(GTK_MENU(ql->menu), 0, 0, 0, 0,
613 event->button, event->time);
614 return TRUE; /* hide the click from other widgets */
615 }
616 if(event->type == GDK_BUTTON_RELEASE
617 && event->button == 1) {
618 /* no modifiers: select this, unselect everything else, set last click
619 * +ctrl: flip selection of this, set last click
620 * +shift: select from last click to here, don't set last click
621 * +ctrl+shift: select from last click to here, set last click
622 */
623 switch(event->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) {
624 case 0:
625 selection_empty(ql->selection);
626 selection_set(ql->selection, q->id, 1);
627 ql->last_click = q;
628 break;
629 case GDK_CONTROL_MASK:
630 selection_flip(ql->selection, q->id);
631 ql->last_click = q;
632 break;
633 case GDK_SHIFT_MASK:
634 case GDK_SHIFT_MASK|GDK_CONTROL_MASK:
635 if(ql->last_click) {
636 if(!(event->state & GDK_CONTROL_MASK))
637 selection_empty(ql->selection);
638 selection_set(ql->selection, q->id, 1);
639 qq = q;
640 if(queue_before(ql->last_click, q))
641 while(qq != ql->last_click) {
642 qq = qq->prev;
643 selection_set(ql->selection, qq->id, 1);
644 }
645 else
646 while(qq != ql->last_click) {
647 qq = qq->next;
648 selection_set(ql->selection, qq->id, 1);
649 }
650 if(event->state & GDK_CONTROL_MASK)
651 ql->last_click = q;
652 }
653 break;
654 }
655 set_widget_states(ql);
656 gtk_widget_queue_draw(ql->mainlayout);
657 }
658 return FALSE; /* propagate */
659}
660
717ba987
RK
661/** @brief A button was pressed or released on the mainlayout
662 *
663 * For debugging only at the moment. */
460b9539 664static gboolean mainlayout_button(GtkWidget attribute((unused)) *widget,
665 GdkEventButton attribute((unused)) *event,
666 gpointer attribute((unused)) user_data) {
667 return FALSE; /* propagate */
668}
669
717ba987 670/** @brief Select all entries in a queue */
460b9539 671void queue_select_all(struct queuelike *ql) {
672 struct queue_entry *qq;
673
674 for(qq = ql->q; qq; qq = qq->next)
675 selection_set(ql->selection, qq->id, 1);
676 ql->last_click = 0;
677 set_widget_states(ql);
678}
679
717ba987 680/** @brief Pop up properties for selected tracks */
460b9539 681void queue_properties(struct queuelike *ql) {
682 struct vector v;
683 const struct queue_entry *qq;
684
685 vector_init(&v);
686 for(qq = ql->q; qq; qq = qq->next)
687 if(selection_selected(ql->selection, qq->id))
688 vector_append(&v, (char *)qq->track);
689 if(v.nvec)
690 properties(v.nvec, v.vec);
691}
692
693/* Drag and drop rearrangement --------------------------------------------- */
694
717ba987 695/** @brief Return nonzero if @p is a draggable row */
460b9539 696static int draggable_row(const struct queue_entry *q) {
697 return q->ql == &ql_queue && q != playing_track;
698}
699
717ba987 700/** @brief Called when a drag begings */
460b9539 701static void queue_drag_begin(GtkWidget attribute((unused)) *widget,
702 GdkDragContext attribute((unused)) *dc,
703 gpointer data) {
704 struct queue_entry *q = data;
705 struct queuelike *ql = q->ql;
706
707 /* Make sure the playing track is not selected, since it cannot be dragged */
708 if(playing_track)
709 selection_set(ql->selection, playing_track->id, 0);
710 /* If the dragged item is not in the selection then change the selection to
711 * just that */
712 if(!selection_selected(ql->selection, q->id)) {
713 selection_empty(ql->selection);
714 selection_set(ql->selection, q->id, 1);
715 set_widget_states(ql);
716 }
717 /* Ignore the eventual button release */
718 ql->swallow_release = 1;
719 /* Create dropzones */
720 add_drag_targets(ql);
721}
722
717ba987 723/** @brief Convert @p id back into a queue entry and a screen row number */
460b9539 724static struct queue_entry *findentry(struct queuelike *ql,
725 const char *id,
726 int *rowp) {
727 int row;
728 struct queue_entry *q;
729
730 if(id) {
731 for(q = ql->q, row = 0; q && strcmp(q->id, id); q = q->next, ++row)
732 ;
733 } else {
734 q = 0;
735 row = playing_track ? 0 : -1;
736 }
737 if(rowp) *rowp = row;
738 return q;
739}
740
717ba987 741/** @brief Called when data is dropped */
460b9539 742static gboolean queue_drag_drop(GtkWidget attribute((unused)) *widget,
743 GdkDragContext *drag_context,
744 gint attribute((unused)) x,
745 gint attribute((unused)) y,
746 guint when,
747 gpointer user_data) {
748 struct queuelike *ql = &ql_queue;
749 const char *id = user_data;
750 struct vector vec;
751 struct queue_entry *q;
752
753 if(!id || (playing_track && !strcmp(id, playing_track->id)))
754 id = "";
755 vector_init(&vec);
756 for(q = ql->q; q; q = q->next)
757 if(q != playing_track && selection_selected(ql->selection, q->id))
758 vector_append(&vec, (char *)q->id);
759 disorder_eclient_moveafter(client, id, vec.nvec, (const char **)vec.vec,
760 0/*completed*/, 0/*v*/);
761 gtk_drag_finish(drag_context, TRUE, TRUE, when);
762 /* Destroy dropzones */
763 remove_drag_targets(ql);
764 return TRUE;
765}
766
717ba987 767/** @brief Called when we enter, or move within, a drop zone */
460b9539 768static gboolean queue_drag_motion(GtkWidget attribute((unused)) *widget,
769 GdkDragContext *drag_context,
770 gint attribute((unused)) x,
771 gint attribute((unused)) y,
772 guint when,
773 gpointer user_data) {
774 struct queuelike *ql = &ql_queue;
775 const char *id = user_data;
776 int row;
777 struct queue_entry *q = findentry(ql, id, &row);
778
779 if(!id || q) {
780 if(!ql->dragmark) {
e9b70a84 781 NW(event_box);
460b9539 782 ql->dragmark = gtk_event_box_new();
783 g_signal_connect(ql->dragmark, "destroy",
784 G_CALLBACK(gtk_widget_destroyed), &ql->dragmark);
785 gtk_widget_set_size_request(ql->dragmark, 10240, row ? 4 : 2);
786 gtk_widget_set_name(ql->dragmark, "queue-drag");
787 gtk_layout_put(GTK_LAYOUT(ql->mainlayout), ql->dragmark, 0,
788 (row + 1) * ql->mainrowheight - !!row);
789 } else
790 gtk_layout_move(GTK_LAYOUT(ql->mainlayout), ql->dragmark, 0,
791 (row + 1) * ql->mainrowheight - !!row);
792 gtk_widget_show(ql->dragmark);
793 gdk_drag_status(drag_context, GDK_ACTION_MOVE, when);
794 return TRUE;
795 } else
796 /* ID has gone AWOL */
797 return FALSE;
798}
799
717ba987 800/** @brief Called when we leave a drop zone */
460b9539 801static void queue_drag_leave(GtkWidget attribute((unused)) *widget,
802 GdkDragContext attribute((unused)) *drag_context,
803 guint attribute((unused)) when,
804 gpointer attribute((unused)) user_data) {
805 struct queuelike *ql = &ql_queue;
806
807 if(ql->dragmark)
808 gtk_widget_hide(ql->dragmark);
809}
810
717ba987
RK
811/** @brief Add a drag target at position @p y
812 *
813 * @p id is the track to insert the moved tracks after, and might be 0 to
814 * insert before the start. */
460b9539 815static void add_drag_target(struct queuelike *ql, int y, int row,
816 const char *id) {
817 GtkWidget *eventbox;
818
819 assert(ql->dropzones[row] == 0);
e9b70a84 820 NW(event_box);
460b9539 821 eventbox = gtk_event_box_new();
822 /* Make the target zone invisible */
823 gtk_event_box_set_visible_window(GTK_EVENT_BOX(eventbox), FALSE);
824 /* Make it large enough */
825 gtk_widget_set_size_request(eventbox, 10240,
826 y ? ql->mainrowheight : ql->mainrowheight / 2);
827 /* Position it */
828 gtk_layout_put(GTK_LAYOUT(ql->mainlayout), eventbox, 0,
829 y ? y - ql->mainrowheight / 2 : 0);
830 /* Mark it as capable of receiving drops */
831 gtk_drag_dest_set(eventbox,
832 0,
833 dragtargets, NDRAGTARGETS, GDK_ACTION_MOVE);
834 g_signal_connect(eventbox, "drag-drop",
835 G_CALLBACK(queue_drag_drop), (char *)id);
836 /* Monitor drag motion */
837 g_signal_connect(eventbox, "drag-motion",
838 G_CALLBACK(queue_drag_motion), (char *)id);
839 g_signal_connect(eventbox, "drag-leave",
840 G_CALLBACK(queue_drag_leave), (char *)id);
841 /* The widget needs to be shown to receive drags */
842 gtk_widget_show(eventbox);
843 /* Remember the drag targets */
844 ql->dropzones[row] = eventbox;
845 g_signal_connect(eventbox, "destroy",
846 G_CALLBACK(gtk_widget_destroyed), &ql->dropzones[row]);
847}
848
717ba987 849/** @brief Create dropzones for dragging into */
460b9539 850static void add_drag_targets(struct queuelike *ql) {
851 int row, y;
852 struct queue_entry *q;
853
854 /* Create an array to store the widgets */
855 ql->dropzones = xcalloc(ql->nrows, sizeof (GtkWidget *));
856 y = 0;
857 /* Add a drag target before the first row provided it's not the playing
858 * track */
859 if(!playing_track || ql->q != playing_track)
860 add_drag_target(ql, 0, 0, 0);
861 /* Put a drag target at the bottom of every row */
862 for(q = ql->q, row = 0; q; q = q->next, ++row) {
863 y += ql->mainrowheight;
864 add_drag_target(ql, y, row, q->id);
865 }
866}
867
717ba987 868/** @brief Remove the dropzones */
460b9539 869static void remove_drag_targets(struct queuelike *ql) {
870 int row;
871
872 for(row = 0; row < ql->nrows; ++row) {
873 if(ql->dropzones[row]) {
e9b70a84 874 DW(event_box);
460b9539 875 gtk_widget_destroy(ql->dropzones[row]);
876 }
877 assert(ql->dropzones[row] == 0);
878 }
879}
880
881/* Layout ------------------------------------------------------------------ */
882
717ba987 883/** @brief Redisplay a queue */
460b9539 884static void redisplay_queue(struct queuelike *ql) {
885 struct queue_entry *q;
886 int row, col;
9eabf9e3 887 GList *c, *children;
460b9539 888 const char *name;
889 GtkRequisition req;
890 GtkWidget *w;
891 int maxwidths[NCOLUMNS], x, y, titlerowheight;
892 int totalwidth = 10240; /* TODO: can we be less blunt */
893
894 D(("redisplay_queue"));
895 /* Eliminate all the existing widgets and start from scratch */
9eabf9e3 896 for(c = children = gtk_container_get_children(GTK_CONTAINER(ql->mainlayout));
460b9539 897 c;
898 c = c->next) {
899 /* Destroy both the label and the eventbox */
e9b70a84 900 if(GTK_BIN(c->data)->child) {
901 DW(label);
460b9539 902 gtk_widget_destroy(GTK_BIN(c->data)->child);
e9b70a84 903 }
904 DW(event_box);
460b9539 905 gtk_widget_destroy(GTK_WIDGET(c->data));
906 }
9eabf9e3 907 g_list_free(children);
460b9539 908 /* Adjust the row count */
909 for(q = ql->q, ql->nrows = 0; q; q = q->next)
910 ++ql->nrows;
911 /* We need to create all the widgets before we can position them */
912 ql->cells = xcalloc(ql->nrows * (NCOLUMNS + 1), sizeof *ql->cells);
913 /* Minimum width is given by the column headings */
914 for(col = 0; col < NCOLUMNS; ++col) {
915 /* Reset size so we don't inherit last iteration's maximum size */
916 gtk_widget_set_size_request(GTK_BIN(ql->titlecells[col])->child, -1, -1);
917 gtk_widget_size_request(GTK_BIN(ql->titlecells[col])->child, &req);
918 maxwidths[col] = req.width;
919 }
920 /* Find the vertical size of the title bar */
921 gtk_widget_size_request(GTK_BIN(ql->titlecells[0])->child, &req);
922 titlerowheight = req.height;
923 y = 0;
924 if(ql->nrows) {
925 /* Construct the widgets */
926 for(q = ql->q, row = 0; q; q = q->next, ++row) {
927 /* Figure out the widget name for this row */
928 if(q == playing_track) name = "row-playing";
929 else name = row % 2 ? "row-even" : "row-odd";
930 /* Make the widget for each column */
931 for(col = 0; col <= NCOLUMNS; ++col) {
932 /* Create and store the widget */
933 if(col < NCOLUMNS)
934 w = get_queue_cell(ql, q, row, col, name, &maxwidths[col]);
935 else
936 w = get_padding_cell(name);
937 ql->cells[row * (NCOLUMNS + 1) + col] = w;
938 /* Maybe mark it draggable */
939 if(draggable_row(q)) {
940 gtk_drag_source_set(w, GDK_BUTTON1_MASK,
941 dragtargets, NDRAGTARGETS, GDK_ACTION_MOVE);
942 g_signal_connect(w, "drag-begin", G_CALLBACK(queue_drag_begin), q);
943 }
944 /* Catch button presses */
945 g_signal_connect(w, "button-release-event",
946 G_CALLBACK(queuelike_button_released), q);
947 g_signal_connect(w, "button-press-event",
948 G_CALLBACK(queuelike_button_released), q);
949 }
950 }
951 /* ...and of each row in the main layout */
952 gtk_widget_size_request(GTK_BIN(ql->cells[0])->child, &req);
953 ql->mainrowheight = req.height;
954 /* Now we know the maximum width of each column we can set the size of
955 * everything and position it */
956 for(row = 0, q = ql->q; row < ql->nrows; ++row, q = q->next) {
957 x = 0;
958 for(col = 0; col < NCOLUMNS; ++col) {
959 w = ql->cells[row * (NCOLUMNS + 1) + col];
960 gtk_widget_set_size_request(GTK_BIN(w)->child,
961 maxwidths[col], -1);
962 gtk_layout_put(GTK_LAYOUT(ql->mainlayout), w, x, y);
963 x += maxwidths[col];
964 }
965 w = ql->cells[row * (NCOLUMNS + 1) + col];
966 gtk_widget_set_size_request(GTK_BIN(w)->child,
967 totalwidth - x, -1);
968 gtk_layout_put(GTK_LAYOUT(ql->mainlayout), w, x, y);
969 y += ql->mainrowheight;
970 }
971 }
972 /* Titles */
973 x = 0;
974 for(col = 0; col < NCOLUMNS; ++col) {
975 gtk_widget_set_size_request(GTK_BIN(ql->titlecells[col])->child,
976 maxwidths[col], -1);
977 gtk_layout_move(GTK_LAYOUT(ql->titlelayout), ql->titlecells[col], x, 0);
978 x += maxwidths[col];
979 }
980 gtk_widget_set_size_request(GTK_BIN(ql->titlecells[col])->child,
981 totalwidth - x, -1);
982 gtk_layout_move(GTK_LAYOUT(ql->titlelayout), ql->titlecells[col], x, 0);
983 /* Set the states */
984 set_widget_states(ql);
985 /* Make sure it's all visible */
986 gtk_widget_show_all(ql->mainlayout);
987 gtk_widget_show_all(ql->titlelayout);
988 /* Layouts might shrink to arrange for the area they shrink out of to be
989 * redrawn */
990 gtk_widget_queue_draw(ql->mainlayout);
991 gtk_widget_queue_draw(ql->titlelayout);
992 /* Adjust the size of the layout */
993 gtk_layout_set_size(GTK_LAYOUT(ql->mainlayout), x, y);
994 gtk_layout_set_size(GTK_LAYOUT(ql->titlelayout), x, titlerowheight);
995 gtk_widget_set_size_request(ql->titlelayout, -1, titlerowheight);
996}
997
717ba987 998/** @brief Called with new queue/recent contents */
460b9539 999static void queuelike_completed(void *v, struct queue_entry *q) {
1000 struct callbackdata *cbd = v;
1001 struct queuelike *ql = cbd->u.ql;
1002
1003 D(("queuelike_complete"));
1004 /* Install the new queue */
1005 update_queue(ql, ql->fixup(q));
1006 /* Update the display */
1007 redisplay_queue(ql);
1008 if(ql->notify)
1009 ql->notify();
1010 /* Update sensitivity of main menu items */
1011 menu_update(-1);
1012}
1013
717ba987 1014/** @brief Called with a new currently playing track */
460b9539 1015static void playing_completed(void attribute((unused)) *v,
1016 struct queue_entry *q) {
1017 struct callbackdata cbd;
1018 D(("playing_completed"));
1019 playing_track = q;
1020 /* Record when we got the playing track data so we know how old the 'sofar'
1021 * field is */
1022 time(&last_playing);
1023 cbd.u.ql = &ql_queue;
1024 queuelike_completed(&cbd, actual_queue);
1025}
1026
717ba987 1027/** @brief Called when the queue is scrolled */
460b9539 1028static void queue_scrolled(GtkAdjustment *adjustment,
1029 gpointer user_data) {
1030 GtkAdjustment *titleadj = user_data;
1031
1032 D(("queue_scrolled"));
1033 gtk_adjustment_set_value(titleadj, adjustment->value);
1034}
1035
717ba987 1036/** @brief Create a queuelike thing (queue/recent) */
460b9539 1037static GtkWidget *queuelike(struct queuelike *ql,
1038 struct queue_entry *(*fixup)(struct queue_entry *),
1039 void (*notify)(void),
1040 struct menuitem *menuitems,
1041 const char *name) {
1042 GtkWidget *vbox, *mainscroll, *titlescroll, *label;
1043 GtkAdjustment *mainadj, *titleadj;
1044 int col, n;
1045
1046 D(("queuelike"));
1047 ql->fixup = fixup;
1048 ql->notify = notify;
1049 ql->menuitems = menuitems;
1050 ql->name = name;
1051 ql->mainrowheight = !0; /* else division by 0 */
1052 ql->selection = selection_new();
1053 /* Create the layouts */
e9b70a84 1054 NW(layout);
460b9539 1055 ql->mainlayout = gtk_layout_new(0, 0);
e9b70a84 1056 NW(layout);
460b9539 1057 ql->titlelayout = gtk_layout_new(0, 0);
1058 /* Scroll the layouts */
1059 ql->mainscroll = mainscroll = scroll_widget(ql->mainlayout, name);
1060 titlescroll = scroll_widget(ql->titlelayout, name);
1061 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(titlescroll),
1062 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
1063 mainadj = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(mainscroll));
1064 titleadj = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(titlescroll));
1065 g_signal_connect(mainadj, "changed", G_CALLBACK(queue_scrolled), titleadj);
1066 g_signal_connect(mainadj, "value-changed", G_CALLBACK(queue_scrolled), titleadj);
1067 /* Fill the titles and put them anywhere */
1068 for(col = 0; col < NCOLUMNS; ++col) {
e9b70a84 1069 NW(label);
460b9539 1070 label = gtk_label_new(columns[col].name);
1071 gtk_misc_set_alignment(GTK_MISC(label), columns[col].xalign, 0);
1072 ql->titlecells[col] = wrap_queue_cell(label, "row-title", 0);
1073 gtk_layout_put(GTK_LAYOUT(ql->titlelayout), ql->titlecells[col], 0, 0);
1074 }
1075 ql->titlecells[col] = get_padding_cell("row-title");
1076 gtk_layout_put(GTK_LAYOUT(ql->titlelayout), ql->titlecells[col], 0, 0);
1077 /* Pack the lot together in a vbox */
e9b70a84 1078 NW(vbox);
460b9539 1079 vbox = gtk_vbox_new(0, 0);
1080 gtk_box_pack_start(GTK_BOX(vbox), titlescroll, 0, 0, 0);
1081 gtk_box_pack_start(GTK_BOX(vbox), mainscroll, 1, 1, 0);
1082 /* Create the popup menu */
e9b70a84 1083 NW(menu);
460b9539 1084 ql->menu = gtk_menu_new();
1085 g_signal_connect(ql->menu, "destroy",
1086 G_CALLBACK(gtk_widget_destroyed), &ql->menu);
1087 for(n = 0; menuitems[n].name; ++n) {
e9b70a84 1088 NW(menu_item);
460b9539 1089 menuitems[n].w = gtk_menu_item_new_with_label(menuitems[n].name);
1090 gtk_menu_attach(GTK_MENU(ql->menu), menuitems[n].w, 0, 1, n, n + 1);
1091 }
1092 g_object_set_data(G_OBJECT(vbox), "type", (void *)&tabtype_queue);
1093 g_object_set_data(G_OBJECT(vbox), "queue", ql);
1094 /* Catch button presses */
1095 g_signal_connect(ql->mainlayout, "button-release-event",
1096 G_CALLBACK(mainlayout_button), ql);
1097#if 0
1098 g_signal_connect(ql->mainlayout, "button-press-event",
1099 G_CALLBACK(mainlayout_button), ql);
1100#endif
1101 return vbox;
1102}
1103
1104/* Popup menu items -------------------------------------------------------- */
1105
717ba987 1106/** @brief Count the number of items selected */
460b9539 1107static int queue_count_selected(const struct queuelike *ql) {
1108 return hash_count(ql->selection);
1109}
1110
717ba987 1111/** @brief Count the number of items selected */
460b9539 1112static int queue_count_entries(const struct queuelike *ql) {
1113 int nitems = 0;
1114 const struct queue_entry *q;
1115
1116 for(q = ql->q; q; q = q->next)
1117 ++nitems;
1118 return nitems;
1119}
1120
717ba987
RK
1121/** @brief Count the number of items selected, excluding the playing track if
1122 * there is one */
460b9539 1123static int count_selected_nonplaying(const struct queuelike *ql) {
1124 int nselected = queue_count_selected(ql);
1125
1126 if(ql->q == playing_track && selection_selected(ql->selection, ql->q->id))
1127 --nselected;
1128 return nselected;
1129}
1130
717ba987 1131/** @brief Determine whether the scratch option should be sensitive */
460b9539 1132static int scratch_sensitive(struct queuelike attribute((unused)) *ql,
1133 struct menuitem attribute((unused)) *m,
1134 struct queue_entry attribute((unused)) *q) {
1135 /* We can scratch if the playing track is selected */
62f22260 1136 return (playing_track
8f763f1b 1137 && (disorder_eclient_state(client) & DISORDER_CONNECTED)
62f22260 1138 && selection_selected(ql->selection, playing_track->id));
460b9539 1139}
1140
717ba987 1141/** @brief Scratch the playing track */
460b9539 1142static void scratch_activate(GtkMenuItem attribute((unused)) *menuitem,
1143 gpointer attribute((unused)) user_data) {
1144 if(playing_track)
1145 disorder_eclient_scratch(client, playing_track->id, 0, 0);
1146}
1147
717ba987 1148/** @brief Determine whether the remove option should be sensitive */
460b9539 1149static int remove_sensitive(struct queuelike *ql,
1150 struct menuitem attribute((unused)) *m,
1151 struct queue_entry *q) {
1152 /* We can remove if we're hovering over a particular track or any non-playing
1153 * tracks are selected */
8f763f1b 1154 return ((disorder_eclient_state(client) & DISORDER_CONNECTED)
62f22260 1155 && ((q
1156 && q != playing_track)
1157 || count_selected_nonplaying(ql)));
460b9539 1158}
1159
717ba987 1160/** @brief Remove selected track(s) */
460b9539 1161static void remove_activate(GtkMenuItem attribute((unused)) *menuitem,
1162 gpointer user_data) {
1163 const struct menuiteminfo *mii = user_data;
1164 struct queue_entry *q = mii->q;
1165 struct queuelike *ql = mii->ql;
1166
1167 if(count_selected_nonplaying(mii->ql)) {
1168 /* Remove selected tracks */
1169 for(q = ql->q; q; q = q->next)
1170 if(selection_selected(ql->selection, q->id) && q != playing_track)
1171 disorder_eclient_remove(client, q->id, 0, 0);
1172 } else if(q)
1173 /* Remove just the hovered track */
1174 disorder_eclient_remove(client, q->id, 0, 0);
1175}
1176
717ba987 1177/** @brief Determine whether the properties menu option should be sensitive */
460b9539 1178static int properties_sensitive(struct queuelike *ql,
1179 struct menuitem attribute((unused)) *m,
1180 struct queue_entry attribute((unused)) *q) {
1181 /* "Properties" is sensitive if at least something is selected */
62f22260 1182 return (hash_count(ql->selection) > 0
8f763f1b 1183 && (disorder_eclient_state(client) & DISORDER_CONNECTED));
460b9539 1184}
1185
717ba987 1186/** @brief Pop up properties for the selected tracks */
460b9539 1187static void properties_activate(GtkMenuItem attribute((unused)) *menuitem,
1188 gpointer user_data) {
1189 const struct menuiteminfo *mii = user_data;
1190
1191 queue_properties(mii->ql);
1192}
1193
717ba987 1194/** @brief Determine whether the select all menu option should be sensitive */
460b9539 1195static int selectall_sensitive(struct queuelike *ql,
1196 struct menuitem attribute((unused)) *m,
1197 struct queue_entry attribute((unused)) *q) {
1198 /* Sensitive if there is anything to select */
1199 return !!ql->q;
1200}
1201
717ba987 1202/** @brief Select all tracks */
460b9539 1203static void selectall_activate(GtkMenuItem attribute((unused)) *menuitem,
1204 gpointer user_data) {
1205 const struct menuiteminfo *mii = user_data;
1206 queue_select_all(mii->ql);
1207}
1208
1209/* The queue --------------------------------------------------------------- */
1210
717ba987 1211/** @brief Fix up the queue by sticking the currently playing track on the front */
460b9539 1212static struct queue_entry *fixup_queue(struct queue_entry *q) {
1213 D(("fixup_queue"));
1214 actual_queue = q;
1215 if(playing_track) {
1216 if(actual_queue)
1217 actual_queue->prev = playing_track;
1218 playing_track->next = actual_queue;
1219 return playing_track;
1220 } else
1221 return actual_queue;
1222}
1223
717ba987
RK
1224/** @brief Adjust track played label
1225 *
1226 * Called regularly to adjust the so-far played label (redrawing the whole
460b9539 1227 * queue once a second makes disobedience occupy >10% of the CPU on my Athlon
1228 * which is ureasonable expensive) */
1229static gboolean adjust_sofar(gpointer attribute((unused)) data) {
1230 if(playing_length_label && playing_track)
1231 gtk_label_set_text(GTK_LABEL(playing_length_label),
1232 text_length(playing_track));
1233 return TRUE;
1234}
1235
717ba987
RK
1236/** @brief Popup menu for the queue
1237 *
1238 * Properties first so that finger trouble is less dangerous. */
460b9539 1239static struct menuitem queue_menu[] = {
39fe1014 1240 { "Track properties", properties_activate, properties_sensitive, 0, 0 },
1241 { "Select all tracks", selectall_activate, selectall_sensitive, 0, 0 },
1242 { "Scratch track", scratch_activate, scratch_sensitive, 0, 0 },
1243 { "Remove track from queue", remove_activate, remove_sensitive, 0, 0 },
460b9539 1244 { 0, 0, 0, 0, 0 }
1245};
1246
3ffa2d15
RK
1247/** @brief Called whenever @ref DISORDER_PLAYING or @ref DISORDER_TRACK_PAUSED changes
1248 *
1249 * We monitor pause/resume as well as whether the track is playing in order to
1250 * keep the time played so far up to date correctly. See playing_completed().
1251 */
1252static void playing_update(void attribute((unused)) *v) {
1253 D(("playing_update"));
1254 gtk_label_set_text(GTK_LABEL(report_label), "updating playing track");
1255 disorder_eclient_playing(client, playing_completed, 0);
1256}
1257
1258/** @brief Create the queue widget */
460b9539 1259GtkWidget *queue_widget(void) {
1260 D(("queue_widget"));
1261 /* Arrange periodic update of the so-far played field */
1262 g_timeout_add(1000/*ms*/, adjust_sofar, 0);
3ffa2d15
RK
1263 /* Arrange a callback whenever the playing state changes */
1264 register_monitor(playing_update, 0, DISORDER_PLAYING|DISORDER_TRACK_PAUSED);
460b9539 1265 /* We pass choose_update() as our notify function since the choose screen
1266 * marks tracks that are playing/in the queue. */
1267 return queuelike(&ql_queue, fixup_queue, choose_update, queue_menu,
1268 "queue");
1269}
1270
717ba987
RK
1271/** @brief Arrange an update of the queue widget
1272 *
1273 * Called when a track is added to the queue, removed from the queue (by user
1274 * cmmand or because it is to be played) or moved within the queue
1275 */
460b9539 1276void queue_update(void) {
1277 struct callbackdata *cbd;
1278
1279 D(("queue_update"));
1280 cbd = xmalloc(sizeof *cbd);
1281 cbd->onerror = 0;
1282 cbd->u.ql = &ql_queue;
1283 gtk_label_set_text(GTK_LABEL(report_label), "updating queue");
1284 disorder_eclient_queue(client, queuelike_completed, cbd);
1285}
1286
460b9539 1287/* Recently played tracks -------------------------------------------------- */
1288
717ba987
RK
1289/** @brief Fix up the recently played list
1290 *
1291 * It's in the wrong order! TODO fix this globally */
460b9539 1292static struct queue_entry *fixup_recent(struct queue_entry *q) {
460b9539 1293 struct queue_entry *qr = 0, *qn;
1294
1295 D(("fixup_recent"));
1296 while(q) {
1297 qn = q->next;
1298 /* Swap next/prev pointers */
1299 q->next = q->prev;
1300 q->prev = qn;
1301 /* Remember last node for new head */
1302 qr = q;
1303 /* Next node */
1304 q = qn;
1305 }
1306 return qr;
1307}
1308
717ba987 1309/** @brief Pop-up menu for recently played list */
460b9539 1310static struct menuitem recent_menu[] = {
39fe1014 1311 { "Track properties", properties_activate, properties_sensitive,0, 0 },
1312 { "Select all tracks", selectall_activate, selectall_sensitive, 0, 0 },
460b9539 1313 { 0, 0, 0, 0, 0 }
1314};
1315
717ba987 1316/** @brief Create the recently-played list */
460b9539 1317GtkWidget *recent_widget(void) {
1318 D(("recent_widget"));
1319 return queuelike(&ql_recent, fixup_recent, 0, recent_menu, "recent");
1320}
1321
717ba987
RK
1322/** @brief Update the recently played list
1323 *
1324 * Called whenever a track is added to it or removed from it.
1325 */
460b9539 1326void recent_update(void) {
1327 struct callbackdata *cbd;
1328
1329 D(("recent_update"));
1330 cbd = xmalloc(sizeof *cbd);
1331 cbd->onerror = 0;
1332 cbd->u.ql = &ql_recent;
1333 gtk_label_set_text(GTK_LABEL(report_label), "updating recently played list");
1334 disorder_eclient_recent(client, queuelike_completed, cbd);
1335}
1336
1337/* Main menu plumbing ------------------------------------------------------ */
1338
1339static int queue_properties_sensitive(GtkWidget *w) {
8f763f1b
RK
1340 return (!!queue_count_selected(g_object_get_data(G_OBJECT(w), "queue"))
1341 && (disorder_eclient_state(client) & DISORDER_CONNECTED));
460b9539 1342}
1343
1344static int queue_selectall_sensitive(GtkWidget *w) {
1345 return !!queue_count_entries(g_object_get_data(G_OBJECT(w), "queue"));
1346}
1347
1348static void queue_properties_activate(GtkWidget *w) {
1349 queue_properties(g_object_get_data(G_OBJECT(w), "queue"));
1350}
1351
1352static void queue_selectall_activate(GtkWidget *w) {
1353 queue_select_all(g_object_get_data(G_OBJECT(w), "queue"));
1354}
1355
1356static const struct tabtype tabtype_queue = {
1357 queue_properties_sensitive,
1358 queue_selectall_sensitive,
1359 queue_properties_activate,
1360 queue_selectall_activate,
1361};
1362
1363/* Other entry points ------------------------------------------------------ */
1364
717ba987 1365/** @brief Return nonzero if @p track is in the queue */
460b9539 1366int queued(const char *track) {
1367 struct queue_entry *q;
1368
1369 D(("queued %s", track));
1370 for(q = ql_queue.q; q; q = q->next)
1371 if(!strcmp(q->track, track))
1372 return 1;
1373 return 0;
1374}
1375
1376/*
1377Local Variables:
1378c-basic-offset:2
1379comment-column:40
1380fill-column:79
1381indent-tabs-mode:nil
1382End:
1383*/