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