2 * This file is part of DisOrder
3 * Copyright (C) 2009 Richard Kettlewell
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.
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.
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/>.
18 /** @file disobedience/multidrag.c
19 * @brief Drag multiple rows of a GtkTreeView
21 * Normally when you start a drag, GtkTreeView sets the selection to just row
22 * you dragged from (because it can't cope with dragging more than one row at a
25 * Disobedience needs more. To implement this it intercepts button-press-event
26 * and button-release event and for clicks that might be the start of drags,
27 * suppresses changes to the selection. A consequence of this is that it needs
28 * to intercept button-release-event too, to restore the effect of the click,
29 * if it turns out not to be drag after all.
31 * The location of the initial click is stored in object data called @c
34 * Inspired by similar code in <a
35 * href="http://code.google.com/p/quodlibet/">Quodlibet</a> (another software
36 * jukebox, albeit as far as I can see a single-user one).
38 #include "disobedience.h"
40 static gboolean multidrag_selection_block(GtkTreeSelection attribute((unused)) *selection,
41 GtkTreeModel attribute((unused)) *model,
42 GtkTreePath attribute((unused)) *path,
43 gboolean attribute((unused)) path_currently_selected,
45 return *(const gboolean *)data;
48 static void block_selection(GtkWidget *w, gboolean block,
50 static const gboolean which[] = { FALSE, TRUE };
51 GtkTreeSelection *s = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
52 gtk_tree_selection_set_select_function(s,
53 multidrag_selection_block,
54 (gboolean *)&which[!!block],
56 // Remember the pointer location
57 int *where = g_object_get_data(G_OBJECT(w), "multidrag-where");
59 where = g_malloc(2 * sizeof (int));
60 g_object_set_data(G_OBJECT(w), "multidrag-where", where);
64 // TODO release 'where' when object is destroyed
67 static gboolean multidrag_button_press_event(GtkWidget *w,
68 GdkEventButton *event,
69 gpointer attribute((unused)) user_data) {
70 /* By default we assume that anything this button press does should
72 block_selection(w, TRUE, -1, -1);
73 /* We are only interested in left-button behavior */
74 if(event->button != 1)
76 /* We are only interested in unmodified clicks (not SHIFT etc) */
77 if(event->state & GDK_MODIFIER_MASK)
79 /* We are only interested if a well-defined path is clicked */
81 if(!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(w),
87 //gtk_widget_grab_focus(w); // TODO why??
88 /* We are only interested if a selected row is clicked */
89 GtkTreeSelection *s = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
90 if(!gtk_tree_selection_path_is_selected(s, path))
92 /* We block subsequent selection changes and remember where the
94 block_selection(w, FALSE, event->x, event->y);
95 return FALSE; /* propagate */
98 static gboolean multidrag_button_release_event(GtkWidget *w,
99 GdkEventButton *event,
100 gpointer attribute((unused)) user_data) {
101 int *where = g_object_get_data(G_OBJECT(w), "multidrag-where");
103 /* Did button-press-event do anything? We just check the outcome rather than
104 * going through all the conditions it tests. */
105 if(where && where[0] != -1) {
106 // Remember where the down-click was
107 const int x = where[0], y = where[1];
108 // Re-allow selections
109 block_selection(w, TRUE, -1, -1);
110 if(x == event->x && y == event->y) {
111 // If the up-click is at the same location as the down-click,
114 GtkTreeViewColumn *col;
115 if(gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(w),
120 gtk_tree_view_set_cursor(GTK_TREE_VIEW(w), path, col, FALSE);
124 return FALSE; /* propagate */
127 /** @brief Allow multi-row drag for @p w
128 * @param w A GtkTreeView widget
130 * Suppresses the restriction of selections when a drag is started.
132 void make_treeview_multidrag(GtkWidget *w) {
133 g_signal_connect(w, "button-press-event",
134 G_CALLBACK(multidrag_button_press_event), NULL);
135 g_signal_connect(w, "button-release-event",
136 G_CALLBACK(multidrag_button_release_event), NULL);