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.
27 * Firstly it intercepts button-press-event and button-release event and for
28 * clicks that might be the start of drags, suppresses changes to the
29 * selection. A consequence of this is that it needs to intercept
30 * button-release-event too, to restore the effect of the click, if it turns
31 * out not to be drag after all.
33 * The location of the initial click is stored in object data called @c
36 * Secondly it intercepts drag-begin and constructs an icon from the rows to be
39 * Inspired by similar code in <a
40 * href="http://code.google.com/p/quodlibet/">Quodlibet</a> (another software
41 * jukebox, albeit as far as I can see a single-user one).
49 #include "multidrag.h"
51 static gboolean multidrag_selection_block(GtkTreeSelection attribute((unused)) *selection,
52 GtkTreeModel attribute((unused)) *model,
53 GtkTreePath attribute((unused)) *path,
54 gboolean attribute((unused)) path_currently_selected,
56 return *(const gboolean *)data;
59 static void block_selection(GtkWidget *w, gboolean block,
61 static const gboolean which[] = { FALSE, TRUE };
62 GtkTreeSelection *s = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
63 gtk_tree_selection_set_select_function(s,
64 multidrag_selection_block,
65 (gboolean *)&which[!!block],
67 // Remember the pointer location
68 int *where = g_object_get_data(G_OBJECT(w), "multidrag-where");
70 where = g_malloc(2 * sizeof (int));
71 g_object_set_data(G_OBJECT(w), "multidrag-where", where);
75 // TODO release 'where' when object is destroyed
78 static gboolean multidrag_button_press_event(GtkWidget *w,
79 GdkEventButton *event,
80 gpointer attribute((unused)) user_data) {
81 /* By default we assume that anything this button press does should
83 block_selection(w, TRUE, -1, -1);
84 /* We are only interested in left-button behavior */
85 if(event->button != 1)
87 /* We are only interested in unmodified clicks (not SHIFT etc) */
88 if(event->state & GDK_MODIFIER_MASK)
90 /* We are only interested if a well-defined path is clicked */
92 if(!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(w),
98 //gtk_widget_grab_focus(w); // TODO why??
99 /* We are only interested if a selected row is clicked */
100 GtkTreeSelection *s = gtk_tree_view_get_selection(GTK_TREE_VIEW(w));
101 if(!gtk_tree_selection_path_is_selected(s, path))
103 /* We block subsequent selection changes and remember where the
105 block_selection(w, FALSE, event->x, event->y);
106 return FALSE; /* propagate */
109 static gboolean multidrag_button_release_event(GtkWidget *w,
110 GdkEventButton *event,
111 gpointer attribute((unused)) user_data) {
112 int *where = g_object_get_data(G_OBJECT(w), "multidrag-where");
114 /* Did button-press-event do anything? We just check the outcome rather than
115 * going through all the conditions it tests. */
116 if(where && where[0] != -1) {
117 // Remember where the down-click was
118 const int x = where[0], y = where[1];
119 // Re-allow selections
120 block_selection(w, TRUE, -1, -1);
121 if(x == event->x && y == event->y) {
122 // If the up-click is at the same location as the down-click,
125 GtkTreeViewColumn *col;
126 if(gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(w),
131 gtk_tree_view_set_cursor(GTK_TREE_VIEW(w), path, col, FALSE);
135 return FALSE; /* propagate */
138 /** @brief State for multidrag_begin() and its callbacks */
139 struct multidrag_begin_state {
146 /** @brief Callback to construct a row pixmap */
147 static void multidrag_make_row_pixmaps(GtkTreeModel attribute((unused)) *model,
149 GtkTreeIter attribute((unused)) *iter,
151 struct multidrag_begin_state *qdbs = data;
153 qdbs->pixmaps[qdbs->index++]
154 = gtk_tree_view_create_row_drag_icon(qdbs->view, path);
157 /** @brief Called when a drag operation starts
158 * @param w Source widget (the tree view)
159 * @param dc Drag context
160 * @param user_data Not used
162 static void multidrag_drag_begin(GtkWidget *w,
163 GdkDragContext attribute((unused)) *dc,
164 gpointer attribute((unused)) user_data) {
165 struct multidrag_begin_state qdbs[1];
167 GtkTreeSelection *sel;
169 //fprintf(stderr, "drag-begin\n");
170 memset(qdbs, 0, sizeof *qdbs);
171 qdbs->view = GTK_TREE_VIEW(w);
172 sel = gtk_tree_view_get_selection(qdbs->view);
173 /* Find out how many rows there are */
174 if(!(qdbs->rows = gtk_tree_selection_count_selected_rows(sel)))
175 return; /* doesn't make sense */
176 /* Generate a pixmap for each row */
177 qdbs->pixmaps = g_new(GdkPixmap *, qdbs->rows);
178 gtk_tree_selection_selected_foreach(sel,
179 multidrag_make_row_pixmaps,
181 /* Determine the size of the final icon */
182 int height = 0, width = 0;
183 for(int n = 0; n < qdbs->rows; ++n) {
185 gdk_drawable_get_size(qdbs->pixmaps[n], &pxw, &pxh);
190 if(!width || !height)
191 return; /* doesn't make sense */
192 /* Construct the icon */
193 icon = gdk_pixmap_new(qdbs->pixmaps[0], width, height, -1);
194 GdkGC *gc = gdk_gc_new(icon);
195 gdk_gc_set_colormap(gc, gtk_widget_get_colormap(w));
197 for(int n = 0; n < qdbs->rows; ++n) {
199 gdk_drawable_get_size(qdbs->pixmaps[n], &pxw, &pxh);
200 gdk_draw_drawable(icon,
203 0, 0, /* source coords */
204 0, y, /* dest coords */
205 pxw, pxh); /* size */
207 gdk_drawable_unref(qdbs->pixmaps[n]);
208 qdbs->pixmaps[n] = NULL;
210 g_free(qdbs->pixmaps);
211 qdbs->pixmaps = NULL;
212 // TODO scale down a bit, the resulting icons are currently a bit on the
214 gtk_drag_source_set_icon(w,
215 gtk_widget_get_colormap(w),
220 /** @brief Allow multi-row drag for @p w
221 * @param w A GtkTreeView widget
223 * Suppresses the restriction of selections when a drag is started.
225 void make_treeview_multidrag(GtkWidget *w) {
226 g_signal_connect(w, "button-press-event",
227 G_CALLBACK(multidrag_button_press_event), NULL);
228 g_signal_connect(w, "button-release-event",
229 G_CALLBACK(multidrag_button_release_event), NULL);
230 g_signal_connect(w, "drag-begin",
231 G_CALLBACK(multidrag_drag_begin), NULL);