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).
42 #include "multidrag.h"
44 static gboolean multidrag_selection_block(GtkTreeSelection attribute((unused)) *selection,
45 GtkTreeModel attribute((unused)) *model,
46 GtkTreePath attribute((unused)) *path,
47 gboolean attribute((unused)) path_currently_selected,
49 return *(const gboolean *)data;
52 static void block_selection(GtkWidget *w, gboolean block,
54 static const gboolean which[] = { FALSE, TRUE };
55 GtkTreeSelection *s = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
56 gtk_tree_selection_set_select_function(s,
57 multidrag_selection_block,
58 (gboolean *)&which[!!block],
60 // Remember the pointer location
61 int *where = g_object_get_data(G_OBJECT(w), "multidrag-where");
63 where = g_malloc(2 * sizeof (int));
64 g_object_set_data(G_OBJECT(w), "multidrag-where", where);
68 // TODO release 'where' when object is destroyed
71 static gboolean multidrag_button_press_event(GtkWidget *w,
72 GdkEventButton *event,
73 gpointer attribute((unused)) user_data) {
74 /* By default we assume that anything this button press does should
76 block_selection(w, TRUE, -1, -1);
77 /* We are only interested in left-button behavior */
78 if(event->button != 1)
80 /* We are only interested in unmodified clicks (not SHIFT etc) */
81 if(event->state & GDK_MODIFIER_MASK)
83 /* We are only interested if a well-defined path is clicked */
85 if(!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(w),
91 //gtk_widget_grab_focus(w); // TODO why??
92 /* We are only interested if a selected row is clicked */
93 GtkTreeSelection *s = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
94 if(!gtk_tree_selection_path_is_selected(s, path))
96 /* We block subsequent selection changes and remember where the
98 block_selection(w, FALSE, event->x, event->y);
99 return FALSE; /* propagate */
102 static gboolean multidrag_button_release_event(GtkWidget *w,
103 GdkEventButton *event,
104 gpointer attribute((unused)) user_data) {
105 int *where = g_object_get_data(G_OBJECT(w), "multidrag-where");
107 /* Did button-press-event do anything? We just check the outcome rather than
108 * going through all the conditions it tests. */
109 if(where && where[0] != -1) {
110 // Remember where the down-click was
111 const int x = where[0], y = where[1];
112 // Re-allow selections
113 block_selection(w, TRUE, -1, -1);
114 if(x == event->x && y == event->y) {
115 // If the up-click is at the same location as the down-click,
118 GtkTreeViewColumn *col;
119 if(gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(w),
124 gtk_tree_view_set_cursor(GTK_TREE_VIEW(w), path, col, FALSE);
128 return FALSE; /* propagate */
131 /** @brief Allow multi-row drag for @p w
132 * @param w A GtkTreeView widget
134 * Suppresses the restriction of selections when a drag is started.
136 void make_treeview_multidrag(GtkWidget *w) {
137 g_signal_connect(w, "button-press-event",
138 G_CALLBACK(multidrag_button_press_event), NULL);
139 g_signal_connect(w, "button-release-event",
140 G_CALLBACK(multidrag_button_release_event), NULL);