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