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