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