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