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