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