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