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