-/* User button press and menu ---------------------------------------------- */
-
-/** @brief Update widget states in order to reflect the selection status */
-static void set_widget_states(struct queuelike *ql) {
- struct queue_entry *q;
- int row, col;
-
- for(q = ql->q, row = 0; q; q = q->next, ++row) {
- for(col = 0; col < ql->ncolumns + 1; ++col)
- gtk_widget_set_state(ql->cells[row * (ql->ncolumns + 1) + col],
- selection_selected(ql->selection, q->id) ?
- GTK_STATE_SELECTED : GTK_STATE_NORMAL);
- }
- /* Might need to change sensitivity of 'Properties' in main menu */
- menu_update(-1);
-}
-
-/** @brief Ordering function for queue entries */
-static int queue_before(const struct queue_entry *a,
- const struct queue_entry *b) {
- while(a && a != b)
- a = a->next;
- return !!a;
-}
-
-/** @brief A button was pressed and released */
-static gboolean queuelike_button_released(GtkWidget attribute((unused)) *widget,
- GdkEventButton *event,
- gpointer user_data) {
- struct queue_entry *q = user_data, *qq;
- struct queuelike *ql = q->ql;
- struct menuiteminfo *mii;
- int n;
-
- /* Might be a release left over from a drag */
- if(ql->swallow_release) {
- ql->swallow_release = 0;
- return FALSE; /* propagate */
- }
-
- if(event->type == GDK_BUTTON_PRESS
- && event->button == 3) {
- /* Right button click.
- * If the current item is not selected then switch the selection to just
- * this item */
- if(q && !selection_selected(ql->selection, q->id)) {
- selection_empty(ql->selection);
- selection_set(ql->selection, q->id, 1);
- ql->last_click = q;
- set_widget_states(ql);
- }
- /* Set the sensitivity of each menu item and (re-)establish the signal
- * handlers */
- for(n = 0; ql->menuitems[n].name; ++n) {
- if(ql->menuitems[n].handlerid)
- g_signal_handler_disconnect(ql->menuitems[n].w,
- ql->menuitems[n].handlerid);
- gtk_widget_set_sensitive(ql->menuitems[n].w,
- ql->menuitems[n].sensitive(ql,
- &ql->menuitems[n],
- q));
- mii = xmalloc(sizeof *mii);
- mii->ql = ql;
- mii->q = q;
- ql->menuitems[n].handlerid = g_signal_connect
- (ql->menuitems[n].w, "activate",
- G_CALLBACK(ql->menuitems[n].activate), mii);
- }
- /* Update the menu according to context */
- gtk_widget_show_all(ql->menu);
- gtk_menu_popup(GTK_MENU(ql->menu), 0, 0, 0, 0,
- event->button, event->time);
- return TRUE; /* hide the click from other widgets */
- }
- if(event->type == GDK_BUTTON_RELEASE
- && event->button == 1) {
- /* no modifiers: select this, unselect everything else, set last click
- * +ctrl: flip selection of this, set last click
- * +shift: select from last click to here, don't set last click
- * +ctrl+shift: select from last click to here, set last click
- */
- switch(event->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) {
- case 0:
- selection_empty(ql->selection);
- selection_set(ql->selection, q->id, 1);
- ql->last_click = q;
- break;
- case GDK_CONTROL_MASK:
- selection_flip(ql->selection, q->id);
- ql->last_click = q;
- break;
- case GDK_SHIFT_MASK:
- case GDK_SHIFT_MASK|GDK_CONTROL_MASK:
- if(ql->last_click) {
- if(!(event->state & GDK_CONTROL_MASK))
- selection_empty(ql->selection);
- selection_set(ql->selection, q->id, 1);
- qq = q;
- if(queue_before(ql->last_click, q))
- while(qq != ql->last_click) {
- qq = qq->prev;
- selection_set(ql->selection, qq->id, 1);
- }
- else
- while(qq != ql->last_click) {
- qq = qq->next;
- selection_set(ql->selection, qq->id, 1);
- }
- if(event->state & GDK_CONTROL_MASK)
- ql->last_click = q;
- }
- break;
- }
- set_widget_states(ql);
- gtk_widget_queue_draw(ql->mainlayout);
- }
- return FALSE; /* propagate */
-}
-
-/** @brief A button was pressed or released on the mainlayout
- *
- * For debugging only at the moment. */
-static gboolean mainlayout_button(GtkWidget attribute((unused)) *widget,
- GdkEventButton attribute((unused)) *event,
- gpointer attribute((unused)) user_data) {
- return FALSE; /* propagate */
-}
-
-/** @brief Select all entries in a queue */
-void queue_select_all(struct queuelike *ql) {
- struct queue_entry *qq;
-
- for(qq = ql->q; qq; qq = qq->next)
- selection_set(ql->selection, qq->id, 1);
- ql->last_click = 0;
- set_widget_states(ql);
-}
-
-/** @brief Deselect all entries in a queue */
-void queue_select_none(struct queuelike *ql) {
- struct queue_entry *qq;
-
- for(qq = ql->q; qq; qq = qq->next)
- selection_set(ql->selection, qq->id, 0);
- ql->last_click = 0;
- set_widget_states(ql);
-}
-
-/** @brief Pop up properties for selected tracks */
-void queue_properties(struct queuelike *ql) {
- struct vector v;
- const struct queue_entry *qq;
-
- vector_init(&v);
- for(qq = ql->q; qq; qq = qq->next)
- if(selection_selected(ql->selection, qq->id))
- vector_append(&v, (char *)qq->track);
- if(v.nvec)
- properties(v.nvec, (const char **)v.vec);
-}
-
-/* Drag and drop rearrangement --------------------------------------------- */
-
-/** @brief Return nonzero if @p is a draggable row
- *
- * Only tracks in the main queue are draggable (and the currently playing track
- * is not draggable).
- */
-static int draggable_row(const struct queue_entry *q) {
- return q->ql == &ql_queue && q != playing_track;
-}
-
-/** @brief Called when a drag begings */
-static void queue_drag_begin(GtkWidget attribute((unused)) *widget,
- GdkDragContext attribute((unused)) *dc,
- gpointer data) {
- struct queue_entry *q = data;
- struct queuelike *ql = q->ql;
-
- /* Make sure the playing track is not selected, since it cannot be dragged */
- if(playing_track)
- selection_set(ql->selection, playing_track->id, 0);
- /* If the dragged item is not in the selection then change the selection to
- * just that */
- if(!selection_selected(ql->selection, q->id)) {
- selection_empty(ql->selection);
- selection_set(ql->selection, q->id, 1);
- set_widget_states(ql);
- }
- /* Ignore the eventual button release */
- ql->swallow_release = 1;
- /* Create dropzones */
- add_drag_targets(ql);
-}
-
-/** @brief Convert @p id back into a queue entry and a screen row number */
-static struct queue_entry *findentry(struct queuelike *ql,
- const char *id,
- int *rowp) {
- int row;
- struct queue_entry *q;
-
- if(id) {
- for(q = ql->q, row = 0; q && strcmp(q->id, id); q = q->next, ++row)
- ;
- } else {
- q = 0;
- row = playing_track ? 0 : -1;
- }
- if(rowp) *rowp = row;
- return q;
-}
-
-/** @brief Called when data is dropped */
-static gboolean queue_drag_drop(GtkWidget attribute((unused)) *widget,
- GdkDragContext *drag_context,
- gint attribute((unused)) x,
- gint attribute((unused)) y,
- guint when,
- gpointer user_data) {
- struct queuelike *ql = &ql_queue;
- const char *id = user_data;
- struct vector vec;
- struct queue_entry *q;
-
- if(!id || (playing_track && !strcmp(id, playing_track->id)))
- id = "";
- vector_init(&vec);
- for(q = ql->q; q; q = q->next)
- if(q != playing_track && selection_selected(ql->selection, q->id))
- vector_append(&vec, (char *)q->id);
- disorder_eclient_moveafter(client, id, vec.nvec, (const char **)vec.vec,
- 0/*completed*/, 0/*v*/);
- gtk_drag_finish(drag_context, TRUE, TRUE, when);
- /* Destroy dropzones */
- remove_drag_targets(ql);
- return TRUE;
-}
-
-/** @brief Called when we enter, or move within, a drop zone */
-static gboolean queue_drag_motion(GtkWidget attribute((unused)) *widget,
- GdkDragContext *drag_context,
- gint attribute((unused)) x,
- gint attribute((unused)) y,
- guint when,
- gpointer user_data) {
- struct queuelike *ql = &ql_queue;
- const char *id = user_data;
- int row;
- struct queue_entry *q = findentry(ql, id, &row);
-
- if(!id || q) {
- if(!ql->dragmark) {
- NW(event_box);
- ql->dragmark = gtk_event_box_new();
- g_signal_connect(ql->dragmark, "destroy",
- G_CALLBACK(gtk_widget_destroyed), &ql->dragmark);
- gtk_widget_set_size_request(ql->dragmark, 10240, row ? 4 : 2);
- gtk_widget_set_style(ql->dragmark, drag_style);
- gtk_layout_put(GTK_LAYOUT(ql->mainlayout), ql->dragmark, 0,
- (row + 1) * ql->mainrowheight - !!row);
- } else
- gtk_layout_move(GTK_LAYOUT(ql->mainlayout), ql->dragmark, 0,
- (row + 1) * ql->mainrowheight - !!row);
- gtk_widget_show(ql->dragmark);
- gdk_drag_status(drag_context, GDK_ACTION_MOVE, when);
- return TRUE;
- } else
- /* ID has gone AWOL */
- return FALSE;
-}
-
-/** @brief Called when we leave a drop zone */
-static void queue_drag_leave(GtkWidget attribute((unused)) *widget,
- GdkDragContext attribute((unused)) *drag_context,
- guint attribute((unused)) when,
- gpointer attribute((unused)) user_data) {
- struct queuelike *ql = &ql_queue;
-
- if(ql->dragmark)
- gtk_widget_hide(ql->dragmark);
-}
-
-/** @brief Add a drag target
- * @param ql The queue-like (in practice this is always @ref ql_queue)
- * @param y The Y coordinate to place the drag target
- * @param id Track to insert moved tracks after, or NULL
- *
- * Adds a drop zone at Y coordinate @p y, which is assumed to lie between two
- * tracks (or before the start of the queue or after the end of the queue). If
- * tracks are dragged into this dropzone then they will be moved @em after
- * track @p id, or to the start of the queue if @p id is NULL.
- *
- * We remember all the dropzones in @c ql->dropzones so they can be destroyed
- * later.
- */
-static void add_drag_target(struct queuelike *ql, int y,
- const char *id) {
- GtkWidget *eventbox;
-
- NW(event_box);
- eventbox = gtk_event_box_new();
- /* Make the target zone invisible */
- gtk_event_box_set_visible_window(GTK_EVENT_BOX(eventbox), FALSE);
- /* Make it large enough */
- gtk_widget_set_size_request(eventbox, 10240,
- y ? ql->mainrowheight : ql->mainrowheight / 2);
- /* Position it */
- gtk_layout_put(GTK_LAYOUT(ql->mainlayout), eventbox, 0,
- y ? y - ql->mainrowheight / 2 : 0);
- /* Mark it as capable of receiving drops */
- gtk_drag_dest_set(eventbox,
- 0,
- dragtargets, NDRAGTARGETS, GDK_ACTION_MOVE);
- g_signal_connect(eventbox, "drag-drop",
- G_CALLBACK(queue_drag_drop), (char *)id);
- /* Monitor drag motion */
- g_signal_connect(eventbox, "drag-motion",
- G_CALLBACK(queue_drag_motion), (char *)id);
- g_signal_connect(eventbox, "drag-leave",
- G_CALLBACK(queue_drag_leave), (char *)id);
- /* The widget needs to be shown to receive drags */
- gtk_widget_show(eventbox);
- /* Remember the drag targets */
- ql->dropzones[ql->ndropzones] = eventbox;
- g_signal_connect(eventbox, "destroy",
- G_CALLBACK(gtk_widget_destroyed),
- &ql->dropzones[ql->ndropzones]);
- ++ql->ndropzones;
-}
-
-/** @brief Create dropzones for dragging into */
-static void add_drag_targets(struct queuelike *ql) {
- int y;
- struct queue_entry *q;
-
- /* Create an array to store the widgets */
- ql->dropzones = xcalloc(ql->nrows, sizeof (GtkWidget *));
- ql->ndropzones = 0;
- y = 0;
- /* Add a drag target before the first row provided it's not the playing
- * track */
- if(!playing_track || ql->q != playing_track)
- add_drag_target(ql, 0, 0);
- /* Put a drag target at the bottom of every row */
- for(q = ql->q; q; q = q->next) {
- y += ql->mainrowheight;
- add_drag_target(ql, y, q->id);
- }
-}
-
-/** @brief Remove the dropzones */
-static void remove_drag_targets(struct queuelike *ql) {
- int n;
-
- for(n = 0; n < ql->ndropzones; ++n) {
- if(ql->dropzones[n]) {
- DW(event_box);
- gtk_widget_destroy(ql->dropzones[n]);
- }
- assert(ql->dropzones[n] == 0);
- }