| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2006-2008 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 3 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, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU 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, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | /** @file disobedience/queue-generic.c |
| 19 | * @brief Disobedience queue widgets |
| 20 | * |
| 21 | * This file provides contains code shared between all the queue-like |
| 22 | * widgets - the queue, the recent list and the added tracks list. |
| 23 | * |
| 24 | * This code is in the process of being rewritten to use the native list |
| 25 | * widget. |
| 26 | * |
| 27 | * There are three @ref queuelike objects: @ref ql_queue, @ref |
| 28 | * ql_recent and @ref ql_added. Each has an associated queue linked |
| 29 | * list and a list store containing the contents. |
| 30 | * |
| 31 | * When new contents turn up we rearrange the list store accordingly. |
| 32 | * |
| 33 | * NB that while in the server the playing track is not in the queue, in |
| 34 | * Disobedience, the playing does live in @c ql_queue.q, despite its different |
| 35 | * status to everything else found in that list. |
| 36 | * |
| 37 | * To do: |
| 38 | * - display playing row in a different color? |
| 39 | */ |
| 40 | #include "disobedience.h" |
| 41 | #include "popup.h" |
| 42 | #include "queue-generic.h" |
| 43 | |
| 44 | static struct queuelike *const queuelikes[] = { |
| 45 | &ql_queue, &ql_recent, &ql_added |
| 46 | }; |
| 47 | #define NQUEUELIKES (sizeof queuelikes / sizeof *queuelikes) |
| 48 | |
| 49 | /* Track detail lookup ----------------------------------------------------- */ |
| 50 | |
| 51 | static void queue_lookups_completed(const char attribute((unused)) *event, |
| 52 | void attribute((unused)) *eventdata, |
| 53 | void *callbackdata) { |
| 54 | struct queuelike *ql = callbackdata; |
| 55 | ql_update_list_store(ql); |
| 56 | } |
| 57 | |
| 58 | /* Column formatting -------------------------------------------------------- */ |
| 59 | |
| 60 | /** @brief Format the 'when' column */ |
| 61 | const char *column_when(const struct queue_entry *q, |
| 62 | const char attribute((unused)) *data) { |
| 63 | char when[64]; |
| 64 | struct tm tm; |
| 65 | time_t t; |
| 66 | |
| 67 | D(("column_when")); |
| 68 | switch(q->state) { |
| 69 | case playing_isscratch: |
| 70 | case playing_unplayed: |
| 71 | case playing_random: |
| 72 | t = q->expected; |
| 73 | break; |
| 74 | case playing_failed: |
| 75 | case playing_no_player: |
| 76 | case playing_ok: |
| 77 | case playing_scratched: |
| 78 | case playing_started: |
| 79 | case playing_paused: |
| 80 | case playing_quitting: |
| 81 | t = q->played; |
| 82 | break; |
| 83 | default: |
| 84 | t = 0; |
| 85 | break; |
| 86 | } |
| 87 | if(t) |
| 88 | strftime(when, sizeof when, "%H:%M", localtime_r(&t, &tm)); |
| 89 | else |
| 90 | when[0] = 0; |
| 91 | return xstrdup(when); |
| 92 | } |
| 93 | |
| 94 | /** @brief Format the 'who' column */ |
| 95 | const char *column_who(const struct queue_entry *q, |
| 96 | const char attribute((unused)) *data) { |
| 97 | D(("column_who")); |
| 98 | return q->submitter ? q->submitter : ""; |
| 99 | } |
| 100 | |
| 101 | /** @brief Format one of the track name columns */ |
| 102 | const char *column_namepart(const struct queue_entry *q, |
| 103 | const char *data) { |
| 104 | D(("column_namepart")); |
| 105 | return namepart(q->track, "display", data); |
| 106 | } |
| 107 | |
| 108 | /** @brief Format the length column */ |
| 109 | const char *column_length(const struct queue_entry *q, |
| 110 | const char attribute((unused)) *data) { |
| 111 | D(("column_length")); |
| 112 | long l; |
| 113 | time_t now; |
| 114 | char *played = 0, *length = 0; |
| 115 | |
| 116 | /* Work out what to say for the length */ |
| 117 | l = namepart_length(q->track); |
| 118 | if(l > 0) |
| 119 | byte_xasprintf(&length, "%ld:%02ld", l / 60, l % 60); |
| 120 | else |
| 121 | byte_xasprintf(&length, "?:??"); |
| 122 | /* For the currently playing track we want to report how much of the track |
| 123 | * has been played */ |
| 124 | if(q == playing_track) { |
| 125 | /* log_state() arranges that we re-get the playing data whenever the |
| 126 | * pause/resume state changes */ |
| 127 | if(last_state & DISORDER_TRACK_PAUSED) |
| 128 | l = playing_track->sofar; |
| 129 | else { |
| 130 | if(!last_playing) |
| 131 | return NULL; |
| 132 | xtime(&now); |
| 133 | l = playing_track->sofar + (now - last_playing); |
| 134 | } |
| 135 | byte_xasprintf(&played, "%ld:%02ld/%s", l / 60, l % 60, length); |
| 136 | return played; |
| 137 | } else |
| 138 | return length; |
| 139 | } |
| 140 | |
| 141 | /* List store maintenance -------------------------------------------------- */ |
| 142 | |
| 143 | /** @brief Return the @ref queue_entry corresponding to @p iter |
| 144 | * @param model Model that owns @p iter |
| 145 | * @param iter Tree iterator |
| 146 | * @return Pointer to queue entry |
| 147 | */ |
| 148 | struct queue_entry *ql_iter_to_q(GtkTreeModel *model, |
| 149 | GtkTreeIter *iter) { |
| 150 | struct queuelike *ql = g_object_get_data(G_OBJECT(model), "ql"); |
| 151 | GValue v[1]; |
| 152 | memset(v, 0, sizeof v); |
| 153 | gtk_tree_model_get_value(model, iter, ql->ncolumns + QUEUEPOINTER_COLUMN, v); |
| 154 | assert(G_VALUE_TYPE(v) == G_TYPE_POINTER); |
| 155 | struct queue_entry *const q = g_value_get_pointer(v); |
| 156 | g_value_unset(v); |
| 157 | return q; |
| 158 | } |
| 159 | |
| 160 | /** @brief Update one row of a list store |
| 161 | * @param q Queue entry |
| 162 | * @param iter Iterator referring to row or NULL to work it out |
| 163 | */ |
| 164 | void ql_update_row(struct queue_entry *q, |
| 165 | GtkTreeIter *iter) { |
| 166 | const struct queuelike *const ql = q->ql; |
| 167 | |
| 168 | D(("ql_update_row")); |
| 169 | /* If no iter was supplied, work it out */ |
| 170 | GtkTreeIter my_iter[1]; |
| 171 | if(!iter) { |
| 172 | gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), my_iter); |
| 173 | struct queue_entry *qq; |
| 174 | for(qq = ql->q; qq && q != qq; qq = qq->next) |
| 175 | gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), my_iter); |
| 176 | if(!qq) |
| 177 | return; |
| 178 | iter = my_iter; |
| 179 | } |
| 180 | /* Update all the columns */ |
| 181 | for(int col = 0; col < ql->ncolumns; ++col) { |
| 182 | const char *const v = ql->columns[col].value(q, |
| 183 | ql->columns[col].data); |
| 184 | if(v) |
| 185 | gtk_list_store_set(ql->store, iter, |
| 186 | col, v, |
| 187 | -1); |
| 188 | } |
| 189 | gtk_list_store_set(ql->store, iter, |
| 190 | ql->ncolumns + QUEUEPOINTER_COLUMN, q, |
| 191 | -1); |
| 192 | if(q == playing_track) |
| 193 | gtk_list_store_set(ql->store, iter, |
| 194 | ql->ncolumns + BACKGROUND_COLUMN, BG_PLAYING, |
| 195 | ql->ncolumns + FOREGROUND_COLUMN, FG_PLAYING, |
| 196 | -1); |
| 197 | else |
| 198 | gtk_list_store_set(ql->store, iter, |
| 199 | ql->ncolumns + BACKGROUND_COLUMN, (char *)0, |
| 200 | ql->ncolumns + FOREGROUND_COLUMN, (char *)0, |
| 201 | -1); |
| 202 | } |
| 203 | |
| 204 | /** @brief Update the list store |
| 205 | * @param ql Queuelike to update |
| 206 | * |
| 207 | * Called when new namepart data is available (and initially). Doesn't change |
| 208 | * the rows, just updates the cell values. |
| 209 | */ |
| 210 | void ql_update_list_store(struct queuelike *ql) { |
| 211 | D(("ql_update_list_store")); |
| 212 | GtkTreeIter iter[1]; |
| 213 | gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), iter); |
| 214 | for(struct queue_entry *q = ql->q; q; q = q->next) { |
| 215 | ql_update_row(q, iter); |
| 216 | gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | struct newqueue_data { |
| 221 | struct queue_entry *old, *new; |
| 222 | }; |
| 223 | |
| 224 | static void record_queue_map(hash *h, |
| 225 | const char *id, |
| 226 | struct queue_entry *old, |
| 227 | struct queue_entry *new) { |
| 228 | struct newqueue_data *nqd; |
| 229 | |
| 230 | if(!(nqd = hash_find(h, id))) { |
| 231 | static const struct newqueue_data empty[1]; |
| 232 | hash_add(h, id, empty, HASH_INSERT); |
| 233 | nqd = hash_find(h, id); |
| 234 | } |
| 235 | if(old) |
| 236 | nqd->old = old; |
| 237 | if(new) |
| 238 | nqd->new = new; |
| 239 | } |
| 240 | |
| 241 | #if 0 |
| 242 | static void dump_queue(struct queue_entry *head, struct queue_entry *mark) { |
| 243 | for(struct queue_entry *q = head; q; q = q->next) { |
| 244 | if(q == mark) |
| 245 | fprintf(stderr, "!"); |
| 246 | fprintf(stderr, "%s", q->id); |
| 247 | if(q->next) |
| 248 | fprintf(stderr, " "); |
| 249 | } |
| 250 | fprintf(stderr, "\n"); |
| 251 | } |
| 252 | |
| 253 | static void dump_rows(struct queuelike *ql) { |
| 254 | GtkTreeIter iter[1]; |
| 255 | gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), |
| 256 | iter); |
| 257 | while(it) { |
| 258 | struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter); |
| 259 | it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter); |
| 260 | fprintf(stderr, "%s", q->id); |
| 261 | if(it) |
| 262 | fprintf(stderr, " "); |
| 263 | } |
| 264 | fprintf(stderr, "\n"); |
| 265 | } |
| 266 | #endif |
| 267 | |
| 268 | /** @brief Reset the list store |
| 269 | * @param ql Queuelike to reset |
| 270 | * @param newq New queue contents/ordering |
| 271 | * |
| 272 | * Updates the queue to match @p newq |
| 273 | */ |
| 274 | void ql_new_queue(struct queuelike *ql, |
| 275 | struct queue_entry *newq) { |
| 276 | D(("ql_new_queue")); |
| 277 | ++suppress_actions; |
| 278 | |
| 279 | /* Tell every queue entry which queue owns it */ |
| 280 | //fprintf(stderr, "%s: filling in q->ql\n", ql->name); |
| 281 | for(struct queue_entry *q = newq; q; q = q->next) |
| 282 | q->ql = ql; |
| 283 | |
| 284 | //fprintf(stderr, "%s: constructing h\n", ql->name); |
| 285 | /* Construct map from id to new and old structures */ |
| 286 | hash *h = hash_new(sizeof(struct newqueue_data)); |
| 287 | for(struct queue_entry *q = ql->q; q; q = q->next) |
| 288 | record_queue_map(h, q->id, q, NULL); |
| 289 | for(struct queue_entry *q = newq; q; q = q->next) |
| 290 | record_queue_map(h, q->id, NULL, q); |
| 291 | |
| 292 | /* The easy bit: delete rows not present any more. In the same pass we |
| 293 | * update the secret column containing the queue_entry pointer. */ |
| 294 | //fprintf(stderr, "%s: deleting rows...\n", ql->name); |
| 295 | GtkTreeIter iter[1]; |
| 296 | gboolean it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), |
| 297 | iter); |
| 298 | int inserted = 0, deleted = 0, kept = 0; |
| 299 | while(it) { |
| 300 | struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter); |
| 301 | const struct newqueue_data *nqd = hash_find(h, q->id); |
| 302 | if(nqd->new) { |
| 303 | /* Tell this row that it belongs to the new version of the queue */ |
| 304 | gtk_list_store_set(ql->store, iter, |
| 305 | ql->ncolumns + QUEUEPOINTER_COLUMN, nqd->new, |
| 306 | -1); |
| 307 | it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter); |
| 308 | ++kept; |
| 309 | } else { |
| 310 | /* Delete this row (and move iter to the next one) */ |
| 311 | //fprintf(stderr, " delete %s", q->id); |
| 312 | it = gtk_list_store_remove(ql->store, iter); |
| 313 | ++deleted; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | /* Now every row's secret column is right, but we might be missing new rows |
| 318 | * and they might be in the wrong order */ |
| 319 | |
| 320 | /* We're going to have to support arbitrary rearrangements, so we might as |
| 321 | * well add new elements at the end. */ |
| 322 | //fprintf(stderr, "%s: adding rows...\n", ql->name); |
| 323 | struct queue_entry *after = 0; |
| 324 | for(struct queue_entry *q = newq; q; q = q->next) { |
| 325 | const struct newqueue_data *nqd = hash_find(h, q->id); |
| 326 | if(!nqd->old) { |
| 327 | if(after) { |
| 328 | /* Try to insert at the right sort of place */ |
| 329 | GtkTreeIter where[1]; |
| 330 | gboolean wit = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), |
| 331 | where); |
| 332 | while(wit && ql_iter_to_q(GTK_TREE_MODEL(ql->store), where) != after) |
| 333 | wit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), where); |
| 334 | if(wit) |
| 335 | gtk_list_store_insert_after(ql->store, iter, where); |
| 336 | else |
| 337 | gtk_list_store_append(ql->store, iter); |
| 338 | } else |
| 339 | gtk_list_store_prepend(ql->store, iter); |
| 340 | gtk_list_store_set(ql->store, iter, |
| 341 | ql->ncolumns + QUEUEPOINTER_COLUMN, q, |
| 342 | -1); |
| 343 | //fprintf(stderr, " add %s", q->id); |
| 344 | ++inserted; |
| 345 | } |
| 346 | after = newq; |
| 347 | } |
| 348 | |
| 349 | /* Now exactly the right set of rows are present, and they have the right |
| 350 | * queue_entry pointers in their secret column, but they may be in the wrong |
| 351 | * order. |
| 352 | * |
| 353 | * The current code is simple but amounts to a bubble-sort - we might easily |
| 354 | * called gtk_tree_model_iter_next a couple of thousand times. |
| 355 | */ |
| 356 | //fprintf(stderr, "%s: rearranging rows\n", ql->name); |
| 357 | //fprintf(stderr, "%s: queue state: ", ql->name); |
| 358 | //dump_queue(newq, 0); |
| 359 | //fprintf(stderr, "%s: row state: ", ql->name); |
| 360 | //dump_rows(ql); |
| 361 | it = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), |
| 362 | iter); |
| 363 | struct queue_entry *rq = newq; /* r for 'right, correct' */ |
| 364 | int swaps = 0, searches = 0; |
| 365 | while(it) { |
| 366 | struct queue_entry *q = ql_iter_to_q(GTK_TREE_MODEL(ql->store), iter); |
| 367 | //fprintf(stderr, " rq = %p, q = %p\n", rq, q); |
| 368 | //fprintf(stderr, " rq->id = %s, q->id = %s\n", rq->id, q->id); |
| 369 | |
| 370 | if(q != rq) { |
| 371 | //fprintf(stderr, " mismatch\n"); |
| 372 | GtkTreeIter next[1] = { *iter }; |
| 373 | gboolean nit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), next); |
| 374 | while(nit) { |
| 375 | struct queue_entry *nq = ql_iter_to_q(GTK_TREE_MODEL(ql->store), next); |
| 376 | //fprintf(stderr, " candidate: %s\n", nq->id); |
| 377 | if(nq == rq) |
| 378 | break; |
| 379 | nit = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), next); |
| 380 | ++searches; |
| 381 | } |
| 382 | assert(nit); |
| 383 | //fprintf(stderr, " found it\n"); |
| 384 | gtk_list_store_swap(ql->store, iter, next); |
| 385 | *iter = *next; |
| 386 | //fprintf(stderr, "%s: new row state: ", ql->name); |
| 387 | //dump_rows(ql); |
| 388 | ++swaps; |
| 389 | } |
| 390 | /* ...and onto the next one */ |
| 391 | it = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter); |
| 392 | rq = rq->next; |
| 393 | } |
| 394 | #if 0 |
| 395 | fprintf(stderr, "%6s: %3d kept %3d inserted %3d deleted %3d swaps %4d searches\n", ql->name, |
| 396 | kept, inserted, deleted, swaps, searches); |
| 397 | #endif |
| 398 | //fprintf(stderr, "done\n"); |
| 399 | ql->q = newq; |
| 400 | /* Set the rest of the columns in new rows */ |
| 401 | ql_update_list_store(ql); |
| 402 | --suppress_actions; |
| 403 | } |
| 404 | |
| 405 | /* Drag and drop has to be figured out experimentally, because it is not well |
| 406 | * documented. |
| 407 | * |
| 408 | * First you get a row-inserted. The path argument points to the destination |
| 409 | * row but this will not yet have had its values set. The source row is still |
| 410 | * present. AFAICT the iter argument points to the same place. |
| 411 | * |
| 412 | * Then you get a row-deleted. The path argument identifies the row that was |
| 413 | * deleted. By this stage the row inserted above has acquired its values. |
| 414 | * |
| 415 | * A complication is that the deletion will move the inserted row. For |
| 416 | * instance, if you do a drag that moves row 1 down to after the track that was |
| 417 | * formerly on row 9, in the row-inserted call it will show up as row 10, but |
| 418 | * in the row-deleted call, row 1 will have been deleted thus making the |
| 419 | * inserted row be row 9. |
| 420 | * |
| 421 | * So when we see the row-inserted we have no idea what track to move. |
| 422 | * Therefore we stash it until we see a row-deleted. |
| 423 | */ |
| 424 | |
| 425 | /** @brief row-inserted callback */ |
| 426 | static void ql_row_inserted(GtkTreeModel attribute((unused)) *treemodel, |
| 427 | GtkTreePath *path, |
| 428 | GtkTreeIter attribute((unused)) *iter, |
| 429 | gpointer user_data) { |
| 430 | struct queuelike *const ql = user_data; |
| 431 | if(!suppress_actions) { |
| 432 | #if 0 |
| 433 | char *ps = gtk_tree_path_to_string(path); |
| 434 | GtkTreeIter piter[1]; |
| 435 | gboolean pi = gtk_tree_model_get_iter(treemodel, piter, path); |
| 436 | struct queue_entry *pq = pi ? ql_iter_to_q(treemodel, piter) : 0; |
| 437 | struct queue_entry *iq = ql_iter_to_q(treemodel, iter); |
| 438 | |
| 439 | fprintf(stderr, "row-inserted %s path=%s pi=%d pq=%p path=%s iq=%p iter=%s\n", |
| 440 | ql->name, |
| 441 | ps, |
| 442 | pi, |
| 443 | pq, |
| 444 | (pi |
| 445 | ? (pq ? pq->track : "(pq=0)") |
| 446 | : "(pi=FALSE)"), |
| 447 | iq, |
| 448 | iq ? iq->track : "(iq=0)"); |
| 449 | |
| 450 | GtkTreeIter j[1]; |
| 451 | gboolean jt = gtk_tree_model_get_iter_first(treemodel, j); |
| 452 | int row = 0; |
| 453 | while(jt) { |
| 454 | struct queue_entry *q = ql_iter_to_q(treemodel, j); |
| 455 | fprintf(stderr, " %2d %s\n", row++, q ? q->track : "(no q)"); |
| 456 | jt = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), j); |
| 457 | } |
| 458 | g_free(ps); |
| 459 | #endif |
| 460 | /* Remember an iterator pointing at the insertion target */ |
| 461 | if(ql->drag_target) |
| 462 | gtk_tree_path_free(ql->drag_target); |
| 463 | ql->drag_target = gtk_tree_path_copy(path); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | /** @brief row-deleted callback */ |
| 468 | static void ql_row_deleted(GtkTreeModel attribute((unused)) *treemodel, |
| 469 | GtkTreePath *path, |
| 470 | gpointer user_data) { |
| 471 | struct queuelike *const ql = user_data; |
| 472 | |
| 473 | if(!suppress_actions) { |
| 474 | #if 0 |
| 475 | char *ps = gtk_tree_path_to_string(path); |
| 476 | fprintf(stderr, "row-deleted %s path=%s ql->drag_target=%s\n", |
| 477 | ql->name, ps, gtk_tree_path_to_string(ql->drag_target)); |
| 478 | GtkTreeIter j[1]; |
| 479 | gboolean jt = gtk_tree_model_get_iter_first(treemodel, j); |
| 480 | int row = 0; |
| 481 | while(jt) { |
| 482 | struct queue_entry *q = ql_iter_to_q(treemodel, j); |
| 483 | fprintf(stderr, " %2d %s\n", row++, q ? q->track : "(no q)"); |
| 484 | jt = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), j); |
| 485 | } |
| 486 | g_free(ps); |
| 487 | #endif |
| 488 | if(!ql->drag_target) { |
| 489 | error(0, "%s: unsuppressed row-deleted with no row-inserted", |
| 490 | ql->name); |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | /* Get the source and destination row numbers. */ |
| 495 | int srcrow = gtk_tree_path_get_indices(path)[0]; |
| 496 | int dstrow = gtk_tree_path_get_indices(ql->drag_target)[0]; |
| 497 | //fprintf(stderr, "srcrow=%d dstrow=%d\n", srcrow, dstrow); |
| 498 | |
| 499 | /* Note that the source row is computed AFTER the destination has been |
| 500 | * inserted, since GTK+ does the insert before the delete. Therefore if |
| 501 | * the source row is south (higher row number) of the destination, it will |
| 502 | * be one higher than expected. |
| 503 | * |
| 504 | * For instance if we drag row 1 to before row 0 we will see row-inserted |
| 505 | * for row 0 but then a row-deleted for row 2. |
| 506 | */ |
| 507 | if(srcrow > dstrow) |
| 508 | --srcrow; |
| 509 | |
| 510 | /* Tell the queue implementation */ |
| 511 | ql->drop(srcrow, dstrow); |
| 512 | |
| 513 | /* Dispose of stashed data */ |
| 514 | gtk_tree_path_free(ql->drag_target); |
| 515 | ql->drag_target = 0; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | /** @brief Initialize a @ref queuelike */ |
| 520 | GtkWidget *init_queuelike(struct queuelike *ql) { |
| 521 | D(("init_queuelike")); |
| 522 | /* Create the list store. We add an extra column to hold a pointer to the |
| 523 | * queue_entry. */ |
| 524 | GType *types = xcalloc(ql->ncolumns + EXTRA_COLUMNS, sizeof (GType)); |
| 525 | for(int n = 0; n < ql->ncolumns + EXTRA_COLUMNS; ++n) |
| 526 | types[n] = G_TYPE_STRING; |
| 527 | types[ql->ncolumns + QUEUEPOINTER_COLUMN] = G_TYPE_POINTER; |
| 528 | ql->store = gtk_list_store_newv(ql->ncolumns + EXTRA_COLUMNS, types); |
| 529 | g_object_set_data(G_OBJECT(ql->store), "ql", (void *)ql); |
| 530 | |
| 531 | /* Create the view */ |
| 532 | ql->view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(ql->store)); |
| 533 | gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(ql->view), TRUE); |
| 534 | |
| 535 | /* Create cell renderers and label columns */ |
| 536 | for(int n = 0; n < ql->ncolumns; ++n) { |
| 537 | GtkCellRenderer *r = gtk_cell_renderer_text_new(); |
| 538 | if(ql->columns[n].flags & COL_ELLIPSIZE) |
| 539 | g_object_set(r, "ellipsize", PANGO_ELLIPSIZE_END, (char *)0); |
| 540 | if(ql->columns[n].flags & COL_RIGHT) |
| 541 | g_object_set(r, "xalign", (gfloat)1.0, (char *)0); |
| 542 | GtkTreeViewColumn *c = gtk_tree_view_column_new_with_attributes |
| 543 | (ql->columns[n].name, |
| 544 | r, |
| 545 | "text", n, |
| 546 | "background", ql->ncolumns + BACKGROUND_COLUMN, |
| 547 | "foreground", ql->ncolumns + FOREGROUND_COLUMN, |
| 548 | (char *)0); |
| 549 | gtk_tree_view_column_set_resizable(c, TRUE); |
| 550 | gtk_tree_view_column_set_reorderable(c, TRUE); |
| 551 | if(ql->columns[n].flags & COL_EXPAND) |
| 552 | g_object_set(c, "expand", TRUE, (char *)0); |
| 553 | gtk_tree_view_append_column(GTK_TREE_VIEW(ql->view), c); |
| 554 | } |
| 555 | |
| 556 | /* The selection should support multiple things being selected */ |
| 557 | ql->selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ql->view)); |
| 558 | gtk_tree_selection_set_mode(ql->selection, GTK_SELECTION_MULTIPLE); |
| 559 | |
| 560 | /* Catch button presses */ |
| 561 | g_signal_connect(ql->view, "button-press-event", |
| 562 | G_CALLBACK(ql_button_release), ql); |
| 563 | |
| 564 | /* Drag+drop*/ |
| 565 | if(ql->drop) { |
| 566 | gtk_tree_view_set_reorderable(GTK_TREE_VIEW(ql->view), TRUE); |
| 567 | g_signal_connect(ql->store, |
| 568 | "row-inserted", |
| 569 | G_CALLBACK(ql_row_inserted), ql); |
| 570 | g_signal_connect(ql->store, |
| 571 | "row-deleted", |
| 572 | G_CALLBACK(ql_row_deleted), ql); |
| 573 | } |
| 574 | |
| 575 | /* TODO style? */ |
| 576 | |
| 577 | ql->init(); |
| 578 | |
| 579 | /* Update display text when lookups complete */ |
| 580 | event_register("lookups-completed", queue_lookups_completed, ql); |
| 581 | |
| 582 | GtkWidget *scrolled = scroll_widget(ql->view); |
| 583 | g_object_set_data(G_OBJECT(scrolled), "type", (void *)ql_tabtype(ql)); |
| 584 | return scrolled; |
| 585 | } |
| 586 | |
| 587 | /* |
| 588 | Local Variables: |
| 589 | c-basic-offset:2 |
| 590 | comment-column:40 |
| 591 | fill-column:79 |
| 592 | indent-tabs-mode:nil |
| 593 | End: |
| 594 | */ |