Fix drop location for drag-and-drop in some cases.
[ardour.git] / libs / gtkmm2ext / gtkmm2ext / dndvbox.h
1 /*
2     Copyright (C) 2009 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <gtkmm/box.h>
21
22 namespace Gtkmm2ext {
23
24 /** Parent class for children of a DnDVBox */   
25 class DnDVBoxChild
26 {
27 public:
28         virtual ~DnDVBoxChild () {}
29         
30         /** @return The widget that is to be put into the DnDVBox */
31         virtual Gtk::Widget& widget () = 0;
32         
33         /** @return An EventBox containing the widget that should be used for selection, dragging etc. */
34         virtual Gtk::EventBox& action_widget () = 0;
35
36         /** @return Text to use in the icon that is dragged */
37         virtual std::string drag_text () const = 0;
38
39         /** Set the child's visual state */
40         virtual void set_visual_state (Gtk::StateType) = 0;
41 };
42
43 /** A VBox whose contents can be dragged and dropped */
44 template <class T>
45 class DnDVBox : public Gtk::EventBox
46 {
47 public:
48         DnDVBox () : _active (0), _drag_icon (0), _expecting_unwanted_button_event (false), _drag_placeholder (0)
49         {
50                 _targets.push_back (Gtk::TargetEntry ("processor"));
51
52                 add (_internal_vbox);
53                 add_events (
54                         Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK |
55                         Gdk::ENTER_NOTIFY_MASK | Gdk::LEAVE_NOTIFY_MASK |
56                         Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK
57                         );
58
59                 signal_button_press_event().connect (bind (mem_fun (*this, &DnDVBox::button_press), (T *) 0));
60                 signal_button_release_event().connect (bind (mem_fun (*this, &DnDVBox::button_release), (T *) 0));
61                 signal_drag_motion().connect (mem_fun (*this, &DnDVBox::drag_motion));
62                 signal_drag_leave().connect (mem_fun (*this, &DnDVBox::drag_leave));
63
64                 _internal_vbox.show ();
65                 
66                 drag_dest_set (_targets);
67                 signal_drag_data_received().connect (mem_fun (*this, &DnDVBox::drag_data_received));
68         }
69         
70         virtual ~DnDVBox ()
71         {
72                 clear ();
73                 
74                 delete _drag_icon;
75         }
76
77         /** Add a child at the end of the widget.  The DnDVBox will take responsibility for deleting the child */
78         void add_child (T* child)
79         {
80                 child->action_widget().drag_source_set (_targets);
81                 child->action_widget().signal_drag_begin().connect (sigc::bind (mem_fun (*this, &DnDVBox::drag_begin), child));
82                 child->action_widget().signal_drag_data_get().connect (sigc::bind (mem_fun (*this, &DnDVBox::drag_data_get), child));
83                 child->action_widget().signal_drag_end().connect (sigc::bind (mem_fun (*this, &DnDVBox::drag_end), child));
84                 child->action_widget().signal_button_press_event().connect (sigc::bind (mem_fun (*this, &DnDVBox::button_press), child));
85                 child->action_widget().signal_button_release_event().connect (sigc::bind (mem_fun (*this, &DnDVBox::button_release), child));
86                 
87                 _internal_vbox.pack_start (child->widget(), false, false);
88                 
89                 _children.push_back (child);
90                 child->widget().show ();
91         }
92
93         /** @return Children, sorted into the order that they are currently being displayed in the widget */
94         std::list<T*> children ()
95         {
96                 std::list<T*> sorted_children;
97
98                 std::list<Gtk::Widget*> widget_children = _internal_vbox.get_children ();
99                 for (std::list<Gtk::Widget*>::iterator i = widget_children.begin(); i != widget_children.end(); ++i) {
100                         T* c = child_from_widget (*i);
101
102                         if (c) {
103                                 sorted_children.push_back (c);
104                         }
105                 }
106
107                 return sorted_children;
108         }
109
110         /** @return Selected children */
111         std::list<T*> selection () const {
112                 return _selection;
113         }
114
115         /** Set the `active' child; this is simply a child which is set to have the Gtk
116          *  STATE_ACTIVE for whatever purposes the client may have.
117          *  @param c Child, or 0 for none.
118          */
119         void set_active (T* c) {
120                 T* old_active = _active;
121                 _active = c;
122                 if (old_active) {
123                         setup_child_state (old_active);
124                 }
125                 if (_active) {
126                         setup_child_state (_active);
127                 }
128         }
129
130         /** @param Child
131          *  @return true if the child is selected, otherwise false.
132          */
133         bool selected (T* child) const {
134                 return (find (_selection.begin(), _selection.end(), child) != _selection.end());
135         }
136
137         /** Clear all children from the widget */
138         void clear ()
139         {
140                 _selection.clear ();
141
142                 for (typename std::list<T*>::iterator i = _children.begin(); i != _children.end(); ++i) {
143                         _internal_vbox.remove ((*i)->widget());
144                         delete *i;
145                 }
146
147                 _children.clear ();
148                 _active = 0;
149         }
150
151         void select_all ()
152         {
153                 clear_selection ();
154                 for (typename std::list<T*>::iterator i = _children.begin(); i != _children.end(); ++i) {
155                         add_to_selection (*i);
156                 }
157
158                 SelectionChanged (); /* EMIT SIGNAL */
159         }
160
161         void select_none ()
162         {
163                 clear_selection ();
164
165                 SelectionChanged (); /* EMIT SIGNAL */
166         }
167
168         /** Look at a y coordinate and find the children below y, and the ones either side.
169          *  @param y y position.
170          *  @param before Filled in with the child before, or 0.
171          *  @param at Filled in with the child under y, or 0.
172          *  @param after Filled in with the child after, or 0.
173          *  @return Fractional position in terms of child height, or -1 if not over a child.
174          */
175         double get_children_around_position (int y, T** before, T** at, T** after) const
176         {
177                 if (_children.empty()) {
178                         *before = *at = *after = 0;
179                         return -1;
180                 }
181
182                 *before = 0;
183
184                 typename std::list<T*>::const_iterator j = _children.begin ();
185
186                 /* index of current child */
187                 int i = 0;
188                 /* top of current child */
189                 double top = 0;
190                 /* bottom of current child */
191                 double bottom = (*j)->widget().get_allocation().get_height ();
192
193                 while (y >= bottom && j != _children.end()) {
194
195                         top = bottom;
196                         
197                         *before = *j;
198                         ++i;
199                         ++j;
200
201                         if (j != _children.end()) {
202                                 bottom += (*j)->widget().get_allocation().get_height ();
203                         }
204                 }
205
206                 if (j == _children.end()) {
207                         *at = 0;
208                         *after = 0;
209                         return -1;
210                 }
211
212                 *at = *j;
213
214                 ++j;
215                 *after = j != _children.end() ? *j : 0;
216
217                 return i + ((y - top) / (*at)->widget().get_allocation().get_height());
218         }
219
220         /** @param y y coordinate.
221          *  @return Pair consisting of the child under y (or 0) and the (fractional) index of the child under y (or -1)
222          */
223         std::pair<T*, double> get_child_at_position (int y) const
224         {
225                 T* before;
226                 T* after;
227
228                 std::pair<T*, double> r;
229                 
230                 r.second = get_children_around_position (y, &before, &r.first, &after);
231
232                 return r;
233         }
234
235         void set_spacing (int s) {
236                 _internal_vbox.set_spacing (s);
237         }
238  
239         /** Children have been reordered by a drag */
240         sigc::signal<void> Reordered;
241
242         /** A button has been pressed over the widget */
243         sigc::signal<bool, GdkEventButton*, T*> ButtonPress;
244
245         /** A button has been release over the widget */
246         sigc::signal<bool, GdkEventButton*, T*> ButtonRelease;
247
248         /** A child has been dropped onto this DnDVBox from another one;
249          *  Parameters are the source DnDVBox, our child which the other one was dropped on (or 0) and the DragContext.
250          */
251         sigc::signal<void, DnDVBox*, T*, Glib::RefPtr<Gdk::DragContext> const & > DropFromAnotherBox;
252         sigc::signal<void> SelectionChanged;
253
254 private:
255         void drag_begin (Glib::RefPtr<Gdk::DragContext> const & context, T* child)
256         {
257                 _drag_child = child;
258                 
259                 /* make up an icon for the drag */
260                 _drag_icon = new Gtk::Window (Gtk::WINDOW_POPUP);
261                 
262                 Gtk::Allocation a = child->widget().get_allocation ();
263                 _drag_icon->set_size_request (a.get_width(), a.get_height());
264                 
265                 _drag_icon->signal_expose_event().connect (sigc::mem_fun (*this, &DnDVBox::icon_expose));
266                 _drag_icon->set_name (get_name ());
267
268                 /* make the icon transparent if possible */
269                 Glib::RefPtr<Gdk::Screen const> s = _drag_icon->get_screen ();
270                 Glib::RefPtr<Gdk::Colormap const> c = s->get_rgba_colormap ();
271                 if (c) {
272                         _drag_icon->set_colormap (c);
273                         _have_alpha = true;
274                 } else {
275                         _have_alpha = false;
276                 }
277
278                 int w, h;
279                 _drag_icon->get_size (w, h);
280                 _drag_icon->drag_set_as_icon (context, w / 2, h / 2);
281                 
282                 _drag_source = this;
283         }
284
285         /* Draw the drag icon */
286         bool icon_expose (GdkEventExpose* ev)
287         {
288                 /* Just grab the child's widget and use that */
289
290                 int w, h;
291                 _drag_icon->get_size (w, h);
292
293                 cairo_t* cr = gdk_cairo_create (_drag_icon->get_window()->gobj ());
294
295                 Glib::RefPtr<Gdk::Pixmap> p = _drag_child->widget().get_snapshot();
296                 gdk_cairo_set_source_pixmap (cr, p->gobj(), 0, 0);
297                 cairo_rectangle (cr, 0, 0, w, h);
298                 cairo_fill (cr);
299                 cairo_destroy (cr);
300                 
301                 return false;
302         }
303         
304         void drag_data_get (Glib::RefPtr<Gdk::DragContext> const &, Gtk::SelectionData & selection_data, guint, guint, T* child)
305         {
306                 selection_data.set (selection_data.get_target(), 8, (const guchar *) &child, sizeof (&child));
307         }
308         
309         void drag_data_received (
310                 Glib::RefPtr<Gdk::DragContext> const & context, int x, int y, Gtk::SelectionData const & selection_data, guint /*info*/, guint time
311                 )
312         {
313                 /* work out where it was dropped */
314                 std::pair<T*, double> const drop = get_child_at_position (y);
315
316                 
317                 
318                 if (_drag_source == this) {
319
320                         /* dropped from ourselves onto ourselves */
321
322                         T* child = *((T **) selection_data.get_data());
323
324                         if (drop.first == 0) {
325                                 _internal_vbox.reorder_child (child->widget(), -1);
326                         } else {
327
328                                 /* where in the list this child should be dropped */
329                                 int target = drop.second + 0.5;
330                                 
331                                 /* find out whether the child was `picked up' from before the drop position */
332                                 int n = 0;
333                                 typename std::list<T*>::const_iterator i = _children.begin ();
334                                 while (i != _children.end() && *i != child && n < target) {
335                                         ++i;
336                                         ++n;
337                                 }
338                                 
339                                 /* if so, adjust the drop position to account for this */
340                                 if (n < target) {
341                                         --target;
342                                 }
343                                 
344                                 _internal_vbox.reorder_child (child->widget(), target);
345                         }
346                         
347                 } else {
348                         
349                         /* drag started in another DnDVBox; raise a signal to say what happened */
350                         
351                         std::list<T*> dropped = _drag_source->selection ();
352                         DropFromAnotherBox (_drag_source, drop.first, context);
353                 }
354                 
355                 context->drag_finish (false, false, time);
356         }
357         
358         void drag_end (Glib::RefPtr<Gdk::DragContext> const &, T *)
359         {
360                 delete _drag_icon;
361                 _drag_icon = 0;
362                 
363                 _drag_child = 0;
364                 remove_drag_placeholder ();
365
366                 Reordered (); /* EMIT SIGNAL */
367         }
368
369         void remove_drag_placeholder ()
370         {
371                 if (_drag_placeholder) {
372                         _internal_vbox.remove (*_drag_placeholder);
373                         _drag_placeholder = 0;
374                 }
375         }
376
377         bool drag_motion (Glib::RefPtr<Gdk::DragContext> const &, int x, int y, guint)
378         {
379                 if (_children.empty ()) {
380                         return false;
381                 }
382
383                 T* before;
384                 T* at;
385                 T* after;
386
387                 /* decide where we currently are */
388                 double const c = get_children_around_position (y, &before, &at, &after);
389
390                 /* whether we're in the top or bottom half of the child that we're over */
391                 bool top_half = (c - int (c)) < 0.5;
392
393                 if (top_half && (before == _drag_child || at == _drag_child)) {
394                         /* dropping here would have no effect, so remove the visual cue */
395                         remove_drag_placeholder ();
396                         return false;
397                 }
398
399                 if (!top_half && (at == _drag_child || after == _drag_child)) {
400                         /* dropping here would have no effect, so remove the visual cue */
401                         remove_drag_placeholder ();
402                         return false;
403                 }
404
405                 /* the index that the placeholder should be put at */
406                 int const n = int (c + 0.5);
407                 
408                 if (_drag_placeholder == 0) {
409                         _drag_placeholder = manage (new Gtk::Label (""));
410                         _internal_vbox.pack_start (*_drag_placeholder, false, false);
411                         _drag_placeholder->show ();
412                 }
413
414                 if (c < 0) {
415                         _internal_vbox.reorder_child (*_drag_placeholder, -1);
416                 } else {
417                         _internal_vbox.reorder_child (*_drag_placeholder, n);
418                 }
419
420                 return false;
421         }
422
423         void drag_leave (Glib::RefPtr<Gdk::DragContext> const &, guint)
424         {
425                 remove_drag_placeholder ();
426         }
427
428         bool button_press (GdkEventButton* ev, T* child)
429         {
430                 if (_expecting_unwanted_button_event == true && child == 0) {
431                         _expecting_unwanted_button_event = false;
432                         return true;
433                 }
434
435                 if (child) {
436                         _expecting_unwanted_button_event = true;
437                 }
438                         
439                 if (ev->button == 1 || ev->button == 3) {
440
441                         if (!selected (child)) {
442
443                                 if ((ev->state & Gdk::SHIFT_MASK) && !_selection.empty()) {
444
445                                         /* Shift-click; select all between the clicked child and any existing selections */
446
447                                         bool selecting = false;
448                                         bool done = false;
449                                         for (typename std::list<T*>::const_iterator i = _children.begin(); i != _children.end(); ++i) {
450
451                                                 bool const was_selected = selected (*i);
452
453                                                 if (selecting && !was_selected) {
454                                                         add_to_selection (*i);
455                                                 }
456                                                 
457                                                 if (!selecting && !done) {
458                                                         if (selected (*i)) {
459                                                                 selecting = true;
460                                                         } else if (*i == child) {
461                                                                 selecting = true;
462                                                                 add_to_selection (child);
463                                                         }
464                                                 } else if (selecting) {
465                                                         if (was_selected || *i == child) {
466                                                                 selecting = false;
467                                                                 done = true;
468                                                         }
469                                                 }
470                                         }
471
472                                 } else {
473                                                 
474                                         if ((ev->state & Gdk::CONTROL_MASK) == 0) {
475                                                 clear_selection ();
476                                         }
477                                         
478                                         if (child) {
479                                                 add_to_selection (child);
480                                         }
481
482                                 }
483                                 
484                                 SelectionChanged (); /* EMIT SIGNAL */
485                                 
486                         } else {
487                                 /* XXX THIS NEEDS GENERALIZING FOR OS X */
488                                 if (ev->button == 1 && (ev->state & Gdk::CONTROL_MASK)) {
489                                         if (child && selected (child)) {
490                                                 remove_from_selection (child);
491                                                 SelectionChanged (); /* EMIT SIGNAL */
492                                         }
493                                 }
494                         }
495                 }
496
497
498                 return ButtonPress (ev, child); /* EMIT SIGNAL */
499         }
500         
501         bool button_release (GdkEventButton* ev, T* child)
502         {
503                 if (_expecting_unwanted_button_event == true && child == 0) {
504                         _expecting_unwanted_button_event = false;
505                         return true;
506                 }
507
508                 if (child) {
509                         _expecting_unwanted_button_event = true;
510                 }
511
512                 return ButtonRelease (ev, child); /* EMIT SIGNAL */
513         }
514
515         /** Setup a child's GTK state correctly */
516         void setup_child_state (T* c)
517         {
518                 assert (c);
519                 
520                 if (c == _active) {
521                         c->set_visual_state (Gtk::STATE_ACTIVE);
522                 } else if (selected (c)) {
523                         c->set_visual_state (Gtk::STATE_SELECTED);
524                 } else {
525                         c->set_visual_state (Gtk::STATE_NORMAL);
526                 }
527         }
528
529         void clear_selection ()
530         {
531                 std::list<T*> old_selection = _selection;
532                 _selection.clear ();
533                 for (typename std::list<T*>::iterator i = old_selection.begin(); i != old_selection.end(); ++i) {
534                         setup_child_state (*i);
535                 }
536         }
537         
538         void add_to_selection (T* child)
539         {
540                 _selection.push_back (child);
541                 setup_child_state (child);
542         }
543
544         void remove_from_selection (T* child)
545         {
546                 typename std::list<T*>::iterator x = find (_selection.begin(), _selection.end(), child);
547                 if (x != _selection.end()) {
548                         _selection.erase (x);
549                         setup_child_state (*x);
550                 }
551         }
552                 
553         T* child_from_widget (Gtk::Widget const * w) const
554         {
555                 typename std::list<T*>::const_iterator i = _children.begin();
556                 while (i != _children.end() && &(*i)->widget() != w) {
557                         ++i;
558                 }
559                 
560                 if (i == _children.end()) {
561                         return 0;
562                 }
563
564                 return *i;
565         }
566         
567         Gtk::VBox _internal_vbox;
568         std::list<Gtk::TargetEntry> _targets;
569         std::list<T*> _children;
570         std::list<T*> _selection;
571         T* _active;
572         Gtk::Window* _drag_icon;
573         bool _expecting_unwanted_button_event;
574         /** A blank label used as a placeholder to indicate where a dragged item would
575          *  go if it were dropped now.
576          */
577         Gtk::Label* _drag_placeholder;
578         /** Our child being dragged, or 0 */
579         T* _drag_child;
580         /** true if we can have an alpha-blended drag icon */
581         bool _have_alpha;
582         
583         static DnDVBox* _drag_source;
584         
585 };
586
587 template <class T>
588 DnDVBox<T>* DnDVBox<T>::_drag_source = 0;
589         
590 }