merge with master
[ardour.git] / libs / gtkmm2ext / gtkmm2ext / dndvbox.h
index 95a6315de7a8eb5b0a6c6893f5bfcd86cd7f61c6..232b5b50a7597f4bd4d51940f20a260fef98773c 100644 (file)
 
 #include <gtkmm/box.h>
 
+#include "gtkmm2ext/visibility.h"
+#include "gtkmm2ext/widget_state.h"
+
 namespace Gtkmm2ext {
 
 /** Parent class for children of a DnDVBox */  
-class DnDVBoxChild
+class LIBGTKMM2EXT_API DnDVBoxChild
 {
 public:
        virtual ~DnDVBoxChild () {}
@@ -37,12 +40,12 @@ public:
        virtual std::string drag_text () const = 0;
 
        /** Set the child's visual state */
-       virtual void set_visual_state (Gtk::StateType) = 0;
+       virtual void set_visual_state (VisualState, bool onoff) = 0;
 };
 
 /** A VBox whose contents can be dragged and dropped */
 template <class T>
-class DnDVBox : public Gtk::EventBox
+class LIBGTKMM2EXT_API DnDVBox : public Gtk::EventBox
 {
 public:
        DnDVBox () : _active (0), _drag_icon (0), _expecting_unwanted_button_event (false), _placeholder (0)
@@ -56,8 +59,8 @@ public:
                        Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK
                        );
 
-               signal_button_press_event().connect (bind (mem_fun (*this, &DnDVBox::button_press), (T *) 0));
-               signal_button_release_event().connect (bind (mem_fun (*this, &DnDVBox::button_release), (T *) 0));
+               signal_button_press_event().connect (sigc::bind (mem_fun (*this, &DnDVBox::button_press), (T *) 0));
+               signal_button_release_event().connect (sigc::bind (mem_fun (*this, &DnDVBox::button_release), (T *) 0));
                signal_drag_motion().connect (mem_fun (*this, &DnDVBox::drag_motion));
                signal_drag_leave().connect (mem_fun (*this, &DnDVBox::drag_leave));
 
@@ -112,8 +115,8 @@ public:
                return _selection;
        }
 
-       /** Set the `active' child; this is simply a child which is set to have the Gtk
-        *  STATE_ACTIVE for whatever purposes the client may have.
+       /** Set the `active' child; this is simply a child which is set to have the 
+        *  visual state "active" for whatever purposes the client may have.
         *  @param c Child, or 0 for none.
         */
        void set_active (T* c) {
@@ -217,6 +220,24 @@ public:
        sigc::signal<void> SelectionChanged;
 
 private:
+
+       /** @return the bottom y position of a child, pretending any placeholder
+        *  is not there.
+        */
+       double bottom_of_child_ignoring_placeholder (T* child) const
+       {
+               Gtk::Allocation const a = child->widget().get_allocation ();
+               double bottom = a.get_y() + a.get_height();
+
+               if (_placeholder) {
+                       Gtk::Allocation const b = _placeholder->get_allocation ();
+                       if (b.get_y() < a.get_y()) {
+                               bottom -= (b.get_height () + _internal_vbox.get_spacing ());
+                       }
+               }
+
+               return bottom;
+       }
        
        /** Look at a y coordinate and find the children below y, and the ones either side.
         *  @param y y position.
@@ -241,8 +262,7 @@ private:
                /* top of current child */
                double top = 0;
                /* bottom of current child */
-               Gtk::Allocation const a = (*j)->widget().get_allocation();
-               double bottom = a.get_y() + a.get_height();
+               double bottom = bottom_of_child_ignoring_placeholder (*j);
 
                while (y >= bottom && j != _children.end()) {
 
@@ -253,8 +273,7 @@ private:
                        ++j;
 
                        if (j != _children.end()) {
-                               Gtk::Allocation const a = (*j)->widget().get_allocation();
-                               bottom = a.get_y() + a.get_height();
+                               bottom = bottom_of_child_ignoring_placeholder (*j);
                        }
                }
 
@@ -269,7 +288,7 @@ private:
                ++j;
                *after = j != _children.end() ? *j : 0;
 
-               return i + ((y - top) / (*at)->widget().get_allocation().get_height());
+               return i + ((y - top) / (bottom - top));
        }
 
        void drag_begin (Glib::RefPtr<Gdk::DragContext> const & context, T* child)
@@ -279,7 +298,7 @@ private:
                /* make up an icon for the drag */
                _drag_icon = new Gtk::Window (Gtk::WINDOW_POPUP);
                
-               Gtk::Allocation a = child->widget().get_allocation ();
+               Gtk::Allocation a = child->action_widget().get_allocation ();
                _drag_icon->set_size_request (a.get_width(), a.get_height());
                
                _drag_icon->signal_expose_event().connect (sigc::mem_fun (*this, &DnDVBox::icon_expose));
@@ -309,7 +328,7 @@ private:
 
                cairo_t* cr = gdk_cairo_create (_drag_icon->get_window()->gobj ());
 
-               Glib::RefPtr<Gdk::Pixmap> p = _drag_child->widget().get_snapshot();
+               Glib::RefPtr<Gdk::Pixmap> p = _drag_child->action_widget().get_snapshot();
                gdk_cairo_set_source_pixmap (cr, p->gobj(), 0, 0);
                cairo_rectangle (cr, 0, 0, w, h);
                cairo_fill (cr);
@@ -334,7 +353,7 @@ private:
 
                        /* dropped from ourselves onto ourselves */
 
-                       T* child = *((T **) selection_data.get_data());
+                       T* child = *((T * const *) selection_data.get_data());
 
                        if (drop.first == 0) {
                                _internal_vbox.reorder_child (child->widget(), -1);
@@ -415,13 +434,19 @@ private:
                /* whether we're in the top or bottom half of the child that we're over */
                bool top_half = (c - int (c)) < 0.5;
 
-               if (top_half && (before == _drag_child || at == _drag_child)) {
+               /* Note that when checking on whether to remove a placeholder, we never do
+                  so if _drag_child is 0 as this means that the child being dragged is
+                  coming from a different DnDVBox, so it will never be the same as any
+                  of our children.
+               */
+
+               if (top_half && _drag_child && (before == _drag_child || at == _drag_child)) {
                        /* dropping here would have no effect, so remove the visual cue */
                        remove_placeholder ();
                        return false;
                }
 
-               if (!top_half && (at == _drag_child || after == _drag_child)) {
+               if (!top_half && _drag_child && (at == _drag_child || after == _drag_child)) {
                        /* dropping here would have no effect, so remove the visual cue */
                        remove_placeholder ();
                        return false;
@@ -522,18 +547,11 @@ private:
                return ButtonRelease (ev, child); /* EMIT SIGNAL */
        }
 
-       /** Setup a child's GTK state correctly */
+       /** Setup a child's visual state correctly */
        void setup_child_state (T* c)
        {
                assert (c);
-               
-               if (c == _active) {
-                       c->set_visual_state (Gtk::STATE_ACTIVE);
-               } else if (selected (c)) {
-                       c->set_visual_state (Gtk::STATE_SELECTED);
-               } else {
-                       c->set_visual_state (Gtk::STATE_NORMAL);
-               }
+               c->set_visual_state (Selected, (selected (c) || (_active == c)));
        }
 
        void clear_selection ()
@@ -555,8 +573,9 @@ private:
        {
                typename std::list<T*>::iterator x = find (_selection.begin(), _selection.end(), child);
                if (x != _selection.end()) {
+                       T* c = *x;
                        _selection.erase (x);
-                       setup_child_state (*x);
+                       setup_child_state (c);
                }
        }