/** @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 (Gtk::StateType) = 0;
};
/** A VBox whose contents can be dragged and dropped */
signal_button_release_event().connect (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));
_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 */
}
}
- /** @param Child
+ /** @param child Child
* @return true if the child is selected, otherwise false.
*/
bool selected (T* child) const {
}
_children.clear ();
+ _active = 0;
}
void select_all ()
/** 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
{
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 = (*j)->widget().get_allocation().get_height ();
+
+ while (y >= bottom && j != _children.end()) {
+
+ top = bottom;
+
*before = *j;
++i;
++j;
+
+ if (j != _children.end()) {
+ bottom += (*j)->widget().get_allocation().get_height ();
+ }
}
if (j == _children.end()) {
++j;
*after = j != _children.end() ? *j : 0;
- return nf;
+ return i + ((y - top) / (*at)->widget().get_allocation().get_height());
}
/** @param y y coordinate.
return r;
}
+
+ void set_spacing (int s) {
+ _internal_vbox.set_spacing (s);
+ }
/** Children have been reordered by a drag */
sigc::signal<void> Reordered;
_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* ev)
{
- 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));
{
/* work out where it was dropped */
std::pair<T*, double> const drop = get_child_at_position (y);
+
+
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 {
assert (c);
if (c == _active) {
- c->action_widget().set_state (Gtk::STATE_ACTIVE);
+ c->set_visual_state (Gtk::STATE_ACTIVE);
} else if (selected (c)) {
- c->action_widget().set_state (Gtk::STATE_SELECTED);
+ c->set_visual_state (Gtk::STATE_SELECTED);
} else {
- c->action_widget().set_state (Gtk::STATE_NORMAL);
+ c->set_visual_state (Gtk::STATE_NORMAL);
}
}