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