remove cruft (old cvsignore files)
[ardour.git] / libs / gtkmm2ext / gtkmm2ext / dndvbox.h
index 0ba86fa8db0e02ae9deb8164124db64ad7c38544..bc5ca6c7255dbef61a2272cd7574f97e32e400f7 100644 (file)
@@ -18,6 +18,7 @@
 */
 
 #include <gtkmm/box.h>
+#include "gtkmm2ext/widget_state.h"
 
 namespace Gtkmm2ext {
 
@@ -35,6 +36,9 @@ public:
 
        /** @return Text to use in the icon that is dragged */
        virtual std::string drag_text () const = 0;
+
+       /** Set the child's visual state */
+       virtual void set_visual_state (VisualState, bool onoff) = 0;
 };
 
 /** A VBox whose contents can be dragged and dropped */
@@ -42,7 +46,7 @@ template <class T>
 class DnDVBox : public Gtk::EventBox
 {
 public:
-       DnDVBox () : _drag_icon (0), _expecting_unwanted_button_event (false), _drag_placeholder (0)
+       DnDVBox () : _active (0), _drag_icon (0), _expecting_unwanted_button_event (false), _placeholder (0)
        {
                _targets.push_back (Gtk::TargetEntry ("processor"));
 
@@ -53,10 +57,12 @@ 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));
+
+               _internal_vbox.show ();
                
                drag_dest_set (_targets);
                signal_drag_data_received().connect (mem_fun (*this, &DnDVBox::drag_data_received));
@@ -82,7 +88,7 @@ public:
                _internal_vbox.pack_start (child->widget(), false, false);
                
                _children.push_back (child);
-               child->widget().show_all ();
+               child->widget().show ();
        }
 
        /** @return Children, sorted into the order that they are currently being displayed in the widget */
@@ -107,7 +113,22 @@ public:
                return _selection;
        }
 
-       /** @param Child
+       /** 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) {
+               T* old_active = _active;
+               _active = c;
+               if (old_active) {
+                       setup_child_state (old_active);
+               }
+               if (_active) {
+                       setup_child_state (_active);
+               }
+       }
+
+       /** @param child Child
         *  @return true if the child is selected, otherwise false.
         */
        bool selected (T* child) const {
@@ -125,6 +146,7 @@ public:
                }
 
                _children.clear ();
+               _active = 0;
        }
 
        void select_all ()
@@ -144,12 +166,83 @@ public:
                SelectionChanged (); /* EMIT SIGNAL */
        }
 
+       /** @param y y coordinate.
+        *  @return Pair consisting of the child under y (or 0) and the (fractional) index of the child under y (or -1)
+        */
+       std::pair<T*, double> get_child_at_position (int y) const
+       {
+               T* before;
+               T* after;
+
+               std::pair<T*, double> r;
+               
+               r.second = get_children_around_position (y, &before, &r.first, &after);
+
+               return r;
+       }
+
+       void set_spacing (int s) {
+               _internal_vbox.set_spacing (s);
+       }
+
+       void remove_placeholder ()
+       {
+               if (_placeholder) {
+                       _internal_vbox.remove (*_placeholder);
+                       _placeholder = 0;
+               }
+       }
+
+       /** Add a placeholder where a child would be put if it were added at the given y position.
+        *  @param y y position within the DnDVBox.
+        *  @return index of child that the placeholder represents, or -1 if it is at the end of all children.
+        */
+       int add_placeholder (double y)
+       {
+               return create_or_update_placeholder (get_child_at_position (y).second);
+       }
+       
+       /** Children have been reordered by a drag */
+       sigc::signal<void> Reordered;
+
+       /** A button has been pressed over the widget */
+       sigc::signal<bool, GdkEventButton*, T*> ButtonPress;
+
+       /** A button has been release over the widget */
+       sigc::signal<bool, GdkEventButton*, T*> ButtonRelease;
+
+       /** A child has been dropped onto this DnDVBox from another one;
+        *  Parameters are the source DnDVBox, our child which the other one was dropped on (or 0) and the DragContext.
+        */
+       sigc::signal<void, DnDVBox*, T*, Glib::RefPtr<Gdk::DragContext> const & > DropFromAnotherBox;
+       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.
         *  @param before Filled in with the child before, or 0.
-        *  @param before Filled in with the child under y, or 0.
+        *  @param at Filled in with the child under y, or 0.
         *  @param after Filled in with the child after, or 0.
-        *  @return Fractional position in terms of child height.
+        *  @return Fractional position in terms of child height, or -1 if not over a child.
         */
        double get_children_around_position (int y, T** before, T** at, T** after) const
        {
@@ -158,17 +251,28 @@ public:
                        return -1;
                }
 
-               /* fractional position in terms of children */
-               double const nf = double (y) / _children.front()->widget().get_allocation().get_height ();
-
                *before = 0;
 
-               int i = 0;
                typename std::list<T*>::const_iterator j = _children.begin ();
-               while (i < int (nf) && j != _children.end()) {
+
+               /* index of current child */
+               int i = 0;
+               /* top of current child */
+               double top = 0;
+               /* bottom of current child */
+               double bottom = bottom_of_child_ignoring_placeholder (*j);
+
+               while (y >= bottom && j != _children.end()) {
+
+                       top = bottom;
+                       
                        *before = *j;
                        ++i;
                        ++j;
+
+                       if (j != _children.end()) {
+                               bottom = bottom_of_child_ignoring_placeholder (*j);
+                       }
                }
 
                if (j == _children.end()) {
@@ -182,40 +286,9 @@ public:
                ++j;
                *after = j != _children.end() ? *j : 0;
 
-               return nf;
-       }
-
-       /** @param y y coordinate.
-        *  @return Pair consisting of the child under y (or 0) and the (fractional) index of the child under y (or -1)
-        */
-       std::pair<T*, double> get_child_at_position (int y) const
-       {
-               T* before;
-               T* after;
-
-               std::pair<T*, double> r;
-               
-               r.second = get_children_around_position (y, &before, &r.first, &after);
-
-               return r;
+               return i + ((y - top) / (bottom - top));
        }
-       /** Children have been reordered by a drag */
-       sigc::signal<void> Reordered;
 
-       /** A button has been pressed over the widget */
-       sigc::signal<bool, GdkEventButton*, T*> ButtonPress;
-
-       /** A button has been release over the widget */
-       sigc::signal<bool, GdkEventButton*, T*> ButtonRelease;
-
-       /** A child has been dropped onto this DnDVBox from another one;
-        *  Parameters are the source DnDVBox, our child which the other one was dropped on (or 0) and the DragContext.
-        */
-       sigc::signal<void, DnDVBox*, T*, Glib::RefPtr<Gdk::DragContext> const & > DropFromAnotherBox;
-       sigc::signal<void> SelectionChanged;
-
-private:
        void drag_begin (Glib::RefPtr<Gdk::DragContext> const & context, T* child)
        {
                _drag_child = child;
@@ -234,9 +307,6 @@ private:
                Glib::RefPtr<Gdk::Colormap const> c = s->get_rgba_colormap ();
                if (c) {
                        _drag_icon->set_colormap (c);
-                       _have_alpha = true;
-               } else {
-                       _have_alpha = false;
                }
 
                int w, h;
@@ -246,42 +316,32 @@ private:
                _drag_source = this;
        }
 
-       /* Draw the drag icon; we go to these lengths so that we can have the icon
-        * transparent if the window system supports it.
-        */
-       bool icon_expose (GdkEventExpose *)
+       /* Draw the drag icon */
+       bool icon_expose (GdkEventExpose*)
        {
-               cairo_t* cr = gdk_cairo_create (_drag_icon->get_window()->gobj());
-               
-               if (_have_alpha) {
-                       cairo_set_source_rgba (cr, 0.2, 0.2, 0.2, 0.5);
-               } else {
-                       cairo_set_source_rgba (cr, 0, 0, 0, 1);
-               }
-               
-               cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
-               cairo_paint (cr);
-               cairo_set_source_rgba (cr, 1, 1, 1, 0.8);
+               /* Just grab the child's widget and use that */
 
-               cairo_text_extents_t ext;
-               cairo_text_extents (cr, _drag_child->drag_text().c_str(), &ext);
-               
-               cairo_move_to (cr, 0, ext.height);
-               cairo_show_text (cr, _drag_child->drag_text().c_str ());
-               cairo_restore (cr);
-               cairo_destroy (cr);
+               int w, h;
+               _drag_icon->get_size (w, h);
+
+               cairo_t* cr = gdk_cairo_create (_drag_icon->get_window()->gobj ());
 
+               Glib::RefPtr<Gdk::Pixmap> p = _drag_child->widget().get_snapshot();
+               gdk_cairo_set_source_pixmap (cr, p->gobj(), 0, 0);
+               cairo_rectangle (cr, 0, 0, w, h);
+               cairo_fill (cr);
+               cairo_destroy (cr);
+               
                return false;
        }
        
-       
        void drag_data_get (Glib::RefPtr<Gdk::DragContext> const &, Gtk::SelectionData & selection_data, guint, guint, T* child)
        {
                selection_data.set (selection_data.get_target(), 8, (const guchar *) &child, sizeof (&child));
        }
        
        void drag_data_received (
-               Glib::RefPtr<Gdk::DragContext> const & context, int x, int y, Gtk::SelectionData const & selection_data, guint /*info*/, guint time
+               Glib::RefPtr<Gdk::DragContext> const & context, int /*x*/, int y, Gtk::SelectionData const & selection_data, guint /*info*/, guint time
                )
        {
                /* work out where it was dropped */
@@ -290,14 +350,30 @@ private:
                if (_drag_source == this) {
 
                        /* dropped from ourselves onto ourselves */
-                       
+
                        T* child = *((T **) selection_data.get_data());
-                       
-                       /* reorder child widgets accordingly */
+
                        if (drop.first == 0) {
                                _internal_vbox.reorder_child (child->widget(), -1);
                        } else {
-                               _internal_vbox.reorder_child (child->widget(), int (drop.second));
+
+                               /* where in the list this child should be dropped */
+                               int target = drop.second + 0.5;
+                               
+                               /* find out whether the child was `picked up' from before the drop position */
+                               int n = 0;
+                               typename std::list<T*>::const_iterator i = _children.begin ();
+                               while (i != _children.end() && *i != child && n < target) {
+                                       ++i;
+                                       ++n;
+                               }
+                               
+                               /* if so, adjust the drop position to account for this */
+                               if (n < target) {
+                                       --target;
+                               }
+                               
+                               _internal_vbox.reorder_child (child->widget(), target);
                        }
                        
                } else {
@@ -317,20 +393,30 @@ private:
                _drag_icon = 0;
                
                _drag_child = 0;
-               remove_drag_placeholder ();
+               remove_placeholder ();
 
                Reordered (); /* EMIT SIGNAL */
        }
 
-       void remove_drag_placeholder ()
+       /** Insert a placeholder at a given fractional child position, creating it if necessary.
+        *  @param c Fractional child position.
+        *  @return index of child that the placeholder represents, or -1 if it is at the end of all children.
+        */
+       int create_or_update_placeholder (double c)
        {
-               if (_drag_placeholder) {
-                       _internal_vbox.remove (*_drag_placeholder);
-                       _drag_placeholder = 0;
+               if (_placeholder == 0) {
+                       _placeholder = manage (new Gtk::Label (""));
+                       _internal_vbox.pack_start (*_placeholder, false, false);
+                       _placeholder->show ();
                }
+
+               /* round up the index, unless we're off the end of the children */
+               int const n = c < 0 ? -1 : int (c + 0.5);
+               _internal_vbox.reorder_child (*_placeholder, n);
+               return n;
        }
 
-       bool drag_motion (Glib::RefPtr<Gdk::DragContext> const &, int x, int y, guint)
+       bool drag_motion (Glib::RefPtr<Gdk::DragContext> const &, int /*x*/, int y, guint)
        {
                if (_children.empty ()) {
                        return false;
@@ -346,39 +432,31 @@ 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_drag_placeholder ();
+                       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_drag_placeholder ();
+                       remove_placeholder ();
                        return false;
                }
 
-               /* the index that the placeholder should be put at */
-               int const n = int (c + 0.5);
-               
-               if (_drag_placeholder == 0) {
-                       _drag_placeholder = manage (new Gtk::Label (""));
-                       _internal_vbox.pack_start (*_drag_placeholder, false, false);
-                       _drag_placeholder->show ();
-               }
-
-               if (c < 0) {
-                       _internal_vbox.reorder_child (*_drag_placeholder, -1);
-               } else {
-                       _internal_vbox.reorder_child (*_drag_placeholder, n);
-               }
-
+               create_or_update_placeholder (c);
                return false;
        }
 
        void drag_leave (Glib::RefPtr<Gdk::DragContext> const &, guint)
        {
-               remove_drag_placeholder ();
+               remove_placeholder ();
        }
 
        bool button_press (GdkEventButton* ev, T* child)
@@ -450,7 +528,6 @@ private:
                        }
                }
 
-
                return ButtonPress (ev, child); /* EMIT SIGNAL */
        }
        
@@ -468,27 +545,35 @@ private:
                return ButtonRelease (ev, child); /* EMIT SIGNAL */
        }
 
+       /** Setup a child's visual state correctly */
+       void setup_child_state (T* c)
+       {
+               assert (c);
+               c->set_visual_state (Selected, (selected (c) || (_active == c)));
+       }
+
        void clear_selection ()
        {
-               for (typename std::list<T*>::iterator i = _selection.begin(); i != _selection.end(); ++i) {
-                       (*i)->action_widget().set_state (Gtk::STATE_NORMAL);
-               }
+               std::list<T*> old_selection = _selection;
                _selection.clear ();
+               for (typename std::list<T*>::iterator i = old_selection.begin(); i != old_selection.end(); ++i) {
+                       setup_child_state (*i);
+               }
        }
        
        void add_to_selection (T* child)
        {
-               child->action_widget().set_state (Gtk::STATE_SELECTED);
                _selection.push_back (child);
+               setup_child_state (child);
        }
-               
-       
+
        void remove_from_selection (T* child)
        {
                typename std::list<T*>::iterator x = find (_selection.begin(), _selection.end(), child);
                if (x != _selection.end()) {
-                       child->action_widget().set_state (Gtk::STATE_NORMAL);
+                       T* c = *x;
                        _selection.erase (x);
+                       setup_child_state (c);
                }
        }
                
@@ -510,16 +595,15 @@ private:
        std::list<Gtk::TargetEntry> _targets;
        std::list<T*> _children;
        std::list<T*> _selection;
+       T* _active;
        Gtk::Window* _drag_icon;
        bool _expecting_unwanted_button_event;
-       /** A blank label used as a placeholder to indicate where a dragged item would
-        *  go if it were dropped now.
+       /** A blank label used as a placeholder to indicate where an item would
+        *  go if it were dropped or inserted "now".
         */
-       Gtk::Label* _drag_placeholder;
+       Gtk::Label* _placeholder;
        /** Our child being dragged, or 0 */
        T* _drag_child;
-       /** true if we can have an alpha-blended drag icon */
-       bool _have_alpha;
        
        static DnDVBox* _drag_source;