Commit | Line | Data |
---|---|---|
6a7eb118 | 1 | /* |
6a7eb118 RK |
2 | * Copyright (C) 2009 Richard Kettlewell |
3 | * | |
75fd36e9 RK |
4 | * Note that this license ONLY applies to multidrag.c and multidrag.h, not to |
5 | * the rest of DisOrder. | |
6a7eb118 | 6 | * |
75fd36e9 RK |
7 | * Permission is hereby granted, free of charge, to any person |
8 | * obtaining a copy of this software and associated documentation files | |
9 | * (the "Software"), to deal in the Software without restriction, | |
10 | * including without limitation the rights to use, copy, modify, merge, | |
11 | * publish, distribute, sublicense, and/or sell copies of the Software, | |
12 | * and to permit persons to whom the Software is furnished to do so, | |
13 | * subject to the following conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be | |
16 | * included in all copies or substantial portions of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE | |
22 | * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | |
23 | * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
6a7eb118 RK |
25 | */ |
26 | /** @file disobedience/multidrag.c | |
27 | * @brief Drag multiple rows of a GtkTreeView | |
30b358a3 RK |
28 | * |
29 | * Normally when you start a drag, GtkTreeView sets the selection to just row | |
30 | * you dragged from (because it can't cope with dragging more than one row at a | |
31 | * time). | |
32 | * | |
846b01c2 RK |
33 | * Disobedience needs more. |
34 | * | |
35 | * Firstly it intercepts button-press-event and button-release event and for | |
36 | * clicks that might be the start of drags, suppresses changes to the | |
37 | * selection. A consequence of this is that it needs to intercept | |
38 | * button-release-event too, to restore the effect of the click, if it turns | |
39 | * out not to be drag after all. | |
30b358a3 RK |
40 | * |
41 | * The location of the initial click is stored in object data called @c | |
42 | * multidrag-where. | |
43 | * | |
846b01c2 RK |
44 | * Secondly it intercepts drag-begin and constructs an icon from the rows to be |
45 | * dragged. | |
46 | * | |
30b358a3 RK |
47 | * Inspired by similar code in <a |
48 | * href="http://code.google.com/p/quodlibet/">Quodlibet</a> (another software | |
49 | * jukebox, albeit as far as I can see a single-user one). | |
6a7eb118 | 50 | */ |
3ae36d41 | 51 | #include <config.h> |
846b01c2 RK |
52 | |
53 | #include <string.h> | |
3ae36d41 RK |
54 | #include <glib.h> |
55 | #include <gtk/gtk.h> | |
56 | ||
57 | #include "multidrag.h" | |
6a7eb118 RK |
58 | |
59 | static gboolean multidrag_selection_block(GtkTreeSelection attribute((unused)) *selection, | |
60 | GtkTreeModel attribute((unused)) *model, | |
61 | GtkTreePath attribute((unused)) *path, | |
62 | gboolean attribute((unused)) path_currently_selected, | |
63 | gpointer data) { | |
30b358a3 | 64 | return *(const gboolean *)data; |
6a7eb118 RK |
65 | } |
66 | ||
67 | static void block_selection(GtkWidget *w, gboolean block, | |
68 | int x, int y) { | |
30b358a3 | 69 | static const gboolean which[] = { FALSE, TRUE }; |
6a7eb118 RK |
70 | GtkTreeSelection *s = gtk_tree_view_get_selection(GTK_TREE_VIEW(w)); |
71 | gtk_tree_selection_set_select_function(s, | |
72 | multidrag_selection_block, | |
30b358a3 | 73 | (gboolean *)&which[!!block], |
6a7eb118 RK |
74 | NULL); |
75 | // Remember the pointer location | |
76 | int *where = g_object_get_data(G_OBJECT(w), "multidrag-where"); | |
77 | if(!where) { | |
78 | where = g_malloc(2 * sizeof (int)); | |
79 | g_object_set_data(G_OBJECT(w), "multidrag-where", where); | |
80 | } | |
81 | where[0] = x; | |
82 | where[1] = y; | |
30b358a3 | 83 | // TODO release 'where' when object is destroyed |
6a7eb118 RK |
84 | } |
85 | ||
86 | static gboolean multidrag_button_press_event(GtkWidget *w, | |
87 | GdkEventButton *event, | |
88 | gpointer attribute((unused)) user_data) { | |
89 | /* By default we assume that anything this button press does should | |
90 | * act as normal */ | |
91 | block_selection(w, TRUE, -1, -1); | |
92 | /* We are only interested in left-button behavior */ | |
93 | if(event->button != 1) | |
94 | return FALSE; | |
95 | /* We are only interested in unmodified clicks (not SHIFT etc) */ | |
96 | if(event->state & GDK_MODIFIER_MASK) | |
97 | return FALSE; | |
98 | /* We are only interested if a well-defined path is clicked */ | |
99 | GtkTreePath *path; | |
100 | if(!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(w), | |
101 | event->x, event->y, | |
102 | &path, | |
103 | NULL, | |
104 | NULL, NULL)) | |
105 | return FALSE; | |
106 | //gtk_widget_grab_focus(w); // TODO why?? | |
107 | /* We are only interested if a selected row is clicked */ | |
108 | GtkTreeSelection *s = gtk_tree_view_get_selection(GTK_TREE_VIEW(w)); | |
109 | if(!gtk_tree_selection_path_is_selected(s, path)) | |
110 | return FALSE; | |
111 | /* We block subsequent selection changes and remember where the | |
112 | * click was */ | |
113 | block_selection(w, FALSE, event->x, event->y); | |
114 | return FALSE; /* propagate */ | |
115 | } | |
116 | ||
117 | static gboolean multidrag_button_release_event(GtkWidget *w, | |
118 | GdkEventButton *event, | |
119 | gpointer attribute((unused)) user_data) { | |
120 | int *where = g_object_get_data(G_OBJECT(w), "multidrag-where"); | |
121 | ||
30b358a3 RK |
122 | /* Did button-press-event do anything? We just check the outcome rather than |
123 | * going through all the conditions it tests. */ | |
6a7eb118 RK |
124 | if(where && where[0] != -1) { |
125 | // Remember where the down-click was | |
126 | const int x = where[0], y = where[1]; | |
127 | // Re-allow selections | |
128 | block_selection(w, TRUE, -1, -1); | |
129 | if(x == event->x && y == event->y) { | |
130 | // If the up-click is at the same location as the down-click, | |
131 | // it's not a drag. | |
132 | GtkTreePath *path; | |
133 | GtkTreeViewColumn *col; | |
134 | if(gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(w), | |
135 | event->x, event->y, | |
136 | &path, | |
137 | &col, | |
138 | NULL, NULL)) { | |
139 | gtk_tree_view_set_cursor(GTK_TREE_VIEW(w), path, col, FALSE); | |
140 | } | |
141 | } | |
142 | } | |
143 | return FALSE; /* propagate */ | |
144 | } | |
145 | ||
846b01c2 RK |
146 | /** @brief State for multidrag_begin() and its callbacks */ |
147 | struct multidrag_begin_state { | |
148 | GtkTreeView *view; | |
ff18efce | 149 | multidrag_row_predicate *predicate; |
846b01c2 RK |
150 | int rows; |
151 | int index; | |
152 | GdkPixmap **pixmaps; | |
153 | }; | |
154 | ||
155 | /** @brief Callback to construct a row pixmap */ | |
156 | static void multidrag_make_row_pixmaps(GtkTreeModel attribute((unused)) *model, | |
157 | GtkTreePath *path, | |
ff18efce | 158 | GtkTreeIter *iter, |
846b01c2 RK |
159 | gpointer data) { |
160 | struct multidrag_begin_state *qdbs = data; | |
161 | ||
ff18efce RK |
162 | if(qdbs->predicate(path, iter)) { |
163 | qdbs->pixmaps[qdbs->index++] | |
164 | = gtk_tree_view_create_row_drag_icon(qdbs->view, path); | |
165 | } | |
846b01c2 RK |
166 | } |
167 | ||
168 | /** @brief Called when a drag operation starts | |
169 | * @param w Source widget (the tree view) | |
170 | * @param dc Drag context | |
ff18efce | 171 | * @param user_data Row predicate |
846b01c2 RK |
172 | */ |
173 | static void multidrag_drag_begin(GtkWidget *w, | |
174 | GdkDragContext attribute((unused)) *dc, | |
ff18efce | 175 | gpointer user_data) { |
846b01c2 RK |
176 | struct multidrag_begin_state qdbs[1]; |
177 | GdkPixmap *icon; | |
178 | GtkTreeSelection *sel; | |
179 | ||
180 | //fprintf(stderr, "drag-begin\n"); | |
181 | memset(qdbs, 0, sizeof *qdbs); | |
182 | qdbs->view = GTK_TREE_VIEW(w); | |
ff18efce | 183 | qdbs->predicate = (multidrag_row_predicate *)user_data; |
846b01c2 RK |
184 | sel = gtk_tree_view_get_selection(qdbs->view); |
185 | /* Find out how many rows there are */ | |
186 | if(!(qdbs->rows = gtk_tree_selection_count_selected_rows(sel))) | |
187 | return; /* doesn't make sense */ | |
188 | /* Generate a pixmap for each row */ | |
189 | qdbs->pixmaps = g_new(GdkPixmap *, qdbs->rows); | |
190 | gtk_tree_selection_selected_foreach(sel, | |
191 | multidrag_make_row_pixmaps, | |
192 | qdbs); | |
ff18efce RK |
193 | /* Might not have used all rows */ |
194 | qdbs->rows = qdbs->index; | |
846b01c2 RK |
195 | /* Determine the size of the final icon */ |
196 | int height = 0, width = 0; | |
197 | for(int n = 0; n < qdbs->rows; ++n) { | |
198 | int pxw, pxh; | |
199 | gdk_drawable_get_size(qdbs->pixmaps[n], &pxw, &pxh); | |
200 | if(pxw > width) | |
201 | width = pxw; | |
202 | height += pxh; | |
203 | } | |
204 | if(!width || !height) | |
205 | return; /* doesn't make sense */ | |
206 | /* Construct the icon */ | |
207 | icon = gdk_pixmap_new(qdbs->pixmaps[0], width, height, -1); | |
208 | GdkGC *gc = gdk_gc_new(icon); | |
209 | gdk_gc_set_colormap(gc, gtk_widget_get_colormap(w)); | |
210 | int y = 0; | |
211 | for(int n = 0; n < qdbs->rows; ++n) { | |
212 | int pxw, pxh; | |
213 | gdk_drawable_get_size(qdbs->pixmaps[n], &pxw, &pxh); | |
214 | gdk_draw_drawable(icon, | |
215 | gc, | |
216 | qdbs->pixmaps[n], | |
217 | 0, 0, /* source coords */ | |
218 | 0, y, /* dest coords */ | |
219 | pxw, pxh); /* size */ | |
220 | y += pxh; | |
221 | gdk_drawable_unref(qdbs->pixmaps[n]); | |
222 | qdbs->pixmaps[n] = NULL; | |
223 | } | |
224 | g_free(qdbs->pixmaps); | |
225 | qdbs->pixmaps = NULL; | |
226 | // TODO scale down a bit, the resulting icons are currently a bit on the | |
227 | // large side. | |
228 | gtk_drag_source_set_icon(w, | |
229 | gtk_widget_get_colormap(w), | |
230 | icon, | |
231 | NULL); | |
232 | } | |
233 | ||
ff18efce RK |
234 | static gboolean multidrag_default_predicate(GtkTreePath attribute((unused)) *path, |
235 | GtkTreeIter attribute((unused)) *iter) { | |
236 | return TRUE; | |
237 | } | |
238 | ||
30b358a3 RK |
239 | /** @brief Allow multi-row drag for @p w |
240 | * @param w A GtkTreeView widget | |
ff18efce RK |
241 | * @param predicate Function called to test rows for draggability, or NULL |
242 | * | |
243 | * Suppresses the restriction of selections when a drag is started, and | |
244 | * intercepts drag-begin to construct an icon. | |
30b358a3 | 245 | * |
ff18efce RK |
246 | * @p predicate should return TRUE for draggable rows and FALSE otherwise, to |
247 | * control what goes in the icon. If NULL, equivalent to a function that | |
248 | * always returns TRUE. | |
30b358a3 | 249 | */ |
ff18efce RK |
250 | void make_treeview_multidrag(GtkWidget *w, |
251 | multidrag_row_predicate *predicate) { | |
252 | if(!predicate) | |
253 | predicate = multidrag_default_predicate; | |
6a7eb118 RK |
254 | g_signal_connect(w, "button-press-event", |
255 | G_CALLBACK(multidrag_button_press_event), NULL); | |
256 | g_signal_connect(w, "button-release-event", | |
257 | G_CALLBACK(multidrag_button_release_event), NULL); | |
846b01c2 | 258 | g_signal_connect(w, "drag-begin", |
ff18efce | 259 | G_CALLBACK(multidrag_drag_begin), predicate); |
6a7eb118 RK |
260 | } |
261 | ||
262 | /* | |
263 | Local Variables: | |
264 | c-basic-offset:2 | |
265 | comment-column:40 | |
266 | fill-column:79 | |
267 | indent-tabs-mode:nil | |
268 | End: | |
269 | */ |