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