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