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