X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fgtkmm2ext%2Fgtkmm2ext%2Fdndvbox.h;h=aba413b352af25b0415f8b6fc6537943ccdc928d;hb=a29fd4542e885cc8dff2e10ef5fc15912c6d4277;hp=232b5b50a7597f4bd4d51940f20a260fef98773c;hpb=d15fda6d751a465d278f477923075d4783f3b1ca;p=ardour.git diff --git a/libs/gtkmm2ext/gtkmm2ext/dndvbox.h b/libs/gtkmm2ext/gtkmm2ext/dndvbox.h index 232b5b50a7..aba413b352 100644 --- a/libs/gtkmm2ext/gtkmm2ext/dndvbox.h +++ b/libs/gtkmm2ext/gtkmm2ext/dndvbox.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2009 Paul Davis + Copyright (C) 2009 Paul Davis This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -24,15 +24,15 @@ namespace Gtkmm2ext { -/** Parent class for children of a DnDVBox */ -class LIBGTKMM2EXT_API DnDVBoxChild +/** Parent class for children of a DnDVBox */ +class /*LIBGTKMM2EXT_API*/ DnDVBoxChild { public: virtual ~DnDVBoxChild () {} - + /** @return The widget that is to be put into the DnDVBox */ virtual Gtk::Widget& widget () = 0; - + /** @return An EventBox containing the widget that should be used for selection, dragging etc. */ virtual Gtk::EventBox& action_widget () = 0; @@ -41,16 +41,25 @@ public: /** Set the child's visual state */ virtual void set_visual_state (VisualState, bool onoff) = 0; + + /** @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 ) */ + virtual bool is_selectable () const = 0; + + virtual bool drag_data_get (Glib::RefPtr const, Gtk::SelectionData &) { return false; } }; /** A VBox whose contents can be dragged and dropped */ template -class LIBGTKMM2EXT_API 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) + DnDVBox (std::list targets) + : _targets (targets) + , _active (0) + , _drag_icon (0) + , _expecting_unwanted_button_event (false) + , _placeholder (0) { - _targets.push_back (Gtk::TargetEntry ("processor")); add (_internal_vbox); add_events ( @@ -65,30 +74,34 @@ public: 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)); } - + virtual ~DnDVBox () { clear (); - + delete _drag_icon; } /** Add a child at the end of the widget. The DnDVBox will take responsibility for deleting the child */ - void add_child (T* child) + void add_child (T* child, std::list targets = std::list()) { - child->action_widget().drag_source_set (_targets); + if (targets.empty ()) { + child->action_widget().drag_source_set (_targets); + } else { + child->action_widget().drag_source_set (targets); + } child->action_widget().signal_drag_begin().connect (sigc::bind (mem_fun (*this, &DnDVBox::drag_begin), child)); child->action_widget().signal_drag_data_get().connect (sigc::bind (mem_fun (*this, &DnDVBox::drag_data_get), child)); child->action_widget().signal_drag_end().connect (sigc::bind (mem_fun (*this, &DnDVBox::drag_end), child)); child->action_widget().signal_button_press_event().connect (sigc::bind (mem_fun (*this, &DnDVBox::button_press), child)); child->action_widget().signal_button_release_event().connect (sigc::bind (mem_fun (*this, &DnDVBox::button_release), child)); - + _internal_vbox.pack_start (child->widget(), false, false); - + _children.push_back (child); child->widget().show (); } @@ -115,7 +128,7 @@ public: return _selection; } - /** Set the `active' child; this is simply a child which is set to have the + /** 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. */ @@ -177,7 +190,7 @@ public: T* after; std::pair r; - + r.second = get_children_around_position (y, &before, &r.first, &after); return r; @@ -203,7 +216,7 @@ public: { return create_or_update_placeholder (get_child_at_position (y).second); } - + /** Children have been reordered by a drag */ sigc::signal Reordered; @@ -217,6 +230,7 @@ public: * Parameters are the source DnDVBox, our child which the other one was dropped on (or 0) and the DragContext. */ sigc::signal const & > DropFromAnotherBox; + sigc::signal const & > DropFromExternal; sigc::signal SelectionChanged; private: @@ -238,7 +252,7 @@ private: 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. @@ -267,7 +281,7 @@ private: while (y >= bottom && j != _children.end()) { top = bottom; - + *before = *j; ++i; ++j; @@ -294,13 +308,13 @@ private: void drag_begin (Glib::RefPtr const & context, T* child) { _drag_child = child; - + /* make up an icon for the drag */ _drag_icon = new Gtk::Window (Gtk::WINDOW_POPUP); - + 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)); _drag_icon->set_name (get_name ()); @@ -314,7 +328,7 @@ private: int w, h; _drag_icon->get_size (w, h); _drag_icon->drag_set_as_icon (context, w / 2, h / 2); - + _drag_source = this; } @@ -333,22 +347,30 @@ private: cairo_rectangle (cr, 0, 0, w, h); cairo_fill (cr); cairo_destroy (cr); - + return false; } - - void drag_data_get (Glib::RefPtr const &, Gtk::SelectionData & selection_data, guint, guint, T* child) + + void drag_data_get (Glib::RefPtr const &context, Gtk::SelectionData & selection_data, guint, guint, T* child) { - selection_data.set (selection_data.get_target(), 8, (const guchar *) &child, sizeof (&child)); + if (!child->drag_data_get(context, selection_data)) { + selection_data.set (selection_data.get_target(), 8, (const guchar *) &child, sizeof (&child)); + } } - + void drag_data_received ( Glib::RefPtr const & context, int /*x*/, int y, Gtk::SelectionData const & selection_data, guint /*info*/, guint time ) { /* work out where it was dropped */ std::pair const drop = get_child_at_position (y); - + + if (selection_data.get_target () != _targets.front ().get_target ()) { + DropFromExternal (selection_data, drop.first, context); + context->drag_finish (false, false, time); + return; + } + if (_drag_source == this) { /* dropped from ourselves onto ourselves */ @@ -361,7 +383,7 @@ private: /* 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::const_iterator i = _children.begin (); @@ -369,31 +391,31 @@ private: ++i; ++n; } - + /* if so, adjust the drop position to account for this */ if (n < target) { --target; } - + _internal_vbox.reorder_child (child->widget(), target); } - + } else { - + /* drag started in another DnDVBox; raise a signal to say what happened */ - + std::list dropped = _drag_source->selection (); DropFromAnotherBox (_drag_source, drop.first, context); } - + context->drag_finish (false, false, time); } - + void drag_end (Glib::RefPtr const &, T *) { delete _drag_icon; _drag_icon = 0; - + _drag_child = 0; remove_placeholder (); @@ -471,7 +493,7 @@ private: if (child) { _expecting_unwanted_button_event = true; } - + if (ev->button == 1 || ev->button == 3) { if (!selected (child)) { @@ -489,7 +511,7 @@ private: if (selecting && !was_selected) { add_to_selection (*i); } - + if (!selecting && !done) { if (selected (*i)) { selecting = true; @@ -506,19 +528,19 @@ private: } } else { - + if ((ev->state & Gdk::CONTROL_MASK) == 0) { clear_selection (); } - + if (child) { add_to_selection (child); } } - + SelectionChanged (); /* EMIT SIGNAL */ - + } else { /* XXX THIS NEEDS GENERALIZING FOR OS X */ if (ev->button == 1 && (ev->state & Gdk::CONTROL_MASK)) { @@ -532,7 +554,7 @@ private: return ButtonPress (ev, child); /* EMIT SIGNAL */ } - + bool button_release (GdkEventButton* ev, T* child) { if (_expecting_unwanted_button_event == true && child == 0) { @@ -562,9 +584,11 @@ private: setup_child_state (*i); } } - + void add_to_selection (T* child) { + if ( !child->is_selectable() ) + return; _selection.push_back (child); setup_child_state (child); } @@ -578,21 +602,21 @@ private: setup_child_state (c); } } - + T* child_from_widget (Gtk::Widget const * w) const { typename std::list::const_iterator i = _children.begin(); while (i != _children.end() && &(*i)->widget() != w) { ++i; } - + if (i == _children.end()) { return 0; } return *i; } - + Gtk::VBox _internal_vbox; std::list _targets; std::list _children; @@ -606,12 +630,12 @@ private: Gtk::Label* _placeholder; /** Our child being dragged, or 0 */ T* _drag_child; - + static DnDVBox* _drag_source; - + }; template DnDVBox* DnDVBox::_drag_source = 0; - + }