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