enough mind-blowing crazy code, lets do intra-ardour DnD the right way
authorPaul Davis <paul@linuxaudiosystems.com>
Tue, 25 Nov 2008 00:03:21 +0000 (00:03 +0000)
committerPaul Davis <paul@linuxaudiosystems.com>
Tue, 25 Nov 2008 00:03:21 +0000 (00:03 +0000)
git-svn-id: svn://localhost/ardour2/branches/2.0-ongoing@4245 d708f5d6-7413-0410-9779-e7cbd77b26cf

gtk2_ardour/redirect_box.cc
gtk2_ardour/redirect_box.h
libs/gtkmm2ext/dndtreeview.cc
libs/gtkmm2ext/gtkmm2ext/dndtreeview.h

index f0f06d12d6c53e9a61befacbfe3f9e2f67ef23ab..25fa0191addd69f226883981ac9d3a8b38641a1a 100644 (file)
@@ -121,7 +121,7 @@ RedirectBox::RedirectBox (Placement pcmnt, Session& sess, PluginSelector &plugse
        redirect_display.get_column(0)->set_sizing(TREE_VIEW_COLUMN_FIXED);
        redirect_display.get_column(0)->set_fixed_width(48);
        redirect_display.add_object_drag (columns.redirect.index(), "redirects");
-       redirect_display.signal_object_drop.connect (mem_fun (*this, &RedirectBox::object_drop));
+       redirect_display.signal_drop.connect (mem_fun (*this, &RedirectBox::object_drop));
 
        TreeViewColumn* name_col = redirect_display.get_column(0);
        CellRendererText* renderer = dynamic_cast<CellRendererText*>(redirect_display.get_column_cell_renderer (0));
@@ -169,22 +169,8 @@ RedirectBox::route_going_away ()
 }
 
 void
-RedirectBox::object_drop (string type, uint32_t cnt, const boost::shared_ptr<Redirect>* ptr)
+RedirectBox::object_drop (const list<boost::shared_ptr<Redirect> >& redirects)
 {
-       cerr << "Object drop, type = " << type << " cnt = " << cnt << endl;
-
-       if (type != "redirects" || cnt == 0 || !ptr) {
-               return;
-       }
-
-       /* do something with the dropped redirects */
-
-       list<boost::shared_ptr<Redirect> > redirects;
-       
-       for (uint32_t n = 0; n < cnt; ++n) {
-               redirects.push_back (ptr[n]);
-       }
-       
        paste_redirect_list (redirects);
 }
 
@@ -194,7 +180,6 @@ RedirectBox::update()
        redisplay_redirects (0);
 }
 
-
 void
 RedirectBox::set_width (Width w)
 {
@@ -953,11 +938,11 @@ RedirectBox::paste_redirects ()
 }
 
 void
-RedirectBox::paste_redirect_list (list<boost::shared_ptr<Redirect> >& redirects)
+RedirectBox::paste_redirect_list (const list<boost::shared_ptr<Redirect> >& redirects)
 {
        list<boost::shared_ptr<Redirect> > copies;
 
-       for (list<boost::shared_ptr<Redirect> >::iterator i = redirects.begin(); i != redirects.end(); ++i) {
+       for (list<boost::shared_ptr<Redirect> >::const_iterator i = redirects.begin(); i != redirects.end(); ++i) {
 
                boost::shared_ptr<Redirect> copy = Redirect::clone (*i);
 
index 0d0ce84bd2ca0be655adc06640eda4391d843247..bb00d820c30e031bc7c4278c61725fa1d8ecc9f9 100644 (file)
@@ -125,7 +125,7 @@ class RedirectBox : public Gtk::HBox, public PluginInterestedObject
        Gtkmm2ext::DnDTreeView<boost::shared_ptr<ARDOUR::Redirect> > redirect_display;
        Gtk::ScrolledWindow    redirect_scroller;
 
-       void object_drop (std::string type, uint32_t cnt, const boost::shared_ptr<ARDOUR::Redirect>*);
+       void object_drop (const std::list<boost::shared_ptr<ARDOUR::Redirect> >&);
 
        Width _width;
        
@@ -184,7 +184,7 @@ class RedirectBox : public Gtk::HBox, public PluginInterestedObject
        void get_selected_redirects (vector<boost::shared_ptr<ARDOUR::Redirect> >&);
 
        static Glib::RefPtr<Gtk::Action> paste_action;
-       void paste_redirect_list (std::list<boost::shared_ptr<ARDOUR::Redirect> >& redirects);
+       void paste_redirect_list (const std::list<boost::shared_ptr<ARDOUR::Redirect> >& redirects);
        
        void activate_redirect (boost::shared_ptr<ARDOUR::Redirect>);
        void deactivate_redirect (boost::shared_ptr<ARDOUR::Redirect>);
index a840114defe7b708fef2ae455a00506611bdf303..0695dd32814b03f64e4b0d8030d7786d1b8e98b6 100644 (file)
@@ -29,6 +29,8 @@ using namespace Gtk;
 using namespace Glib;
 using namespace Gtkmm2ext;
 
+DnDTreeViewBase::DragData DnDTreeViewBase::drag_data;
+
 DnDTreeViewBase::DnDTreeViewBase ()
        : TreeView ()
 {
@@ -57,6 +59,7 @@ DnDTreeViewBase::add_object_drag (int column, string type_name)
 {
        draggable.push_back (TargetEntry (type_name, TargetFlags(0)));
        data_column = column;
+       object_type = type_name;
 
        enable_model_drag_source (draggable);
        enable_model_drag_dest (draggable);
index e7f4aeb83df5c0869e26652cc67edff38c420ed8..08d54bb139f38ac6181630f0adb28d3283ed60c0 100644 (file)
@@ -62,6 +62,21 @@ class DnDTreeViewBase : public Gtk::TreeView
        std::list<Gtk::TargetEntry> draggable;
        Gdk::DragAction             suggested_action;
        int                         data_column;
+       std::string                 object_type;
+
+       struct DragData {
+           Gtk::TreeView* source;
+           int            data_column;
+           std::string    object_type;
+       };
+       
+       static DragData drag_data;
+
+       void start_object_drag () {
+               drag_data.source = this;
+               drag_data.data_column = data_column;
+               drag_data.object_type = object_type;
+       }
 };
 
 template<class DataType>
@@ -71,26 +86,23 @@ class DnDTreeView : public DnDTreeViewBase
        DnDTreeView() {} 
        ~DnDTreeView() {}
 
-       sigc::signal<void,std::string,uint32_t,const DataType*> signal_object_drop;
+       sigc::signal<void,const std::list<DataType>& > signal_drop;
 
        void on_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, Gtk::SelectionData& selection_data, guint info, guint time) {
                if (selection_data.get_target() == "GTK_TREE_MODEL_ROW") {
 
                        TreeView::on_drag_data_get (context, selection_data, info, time);
-                       
-               } else if (data_column >= 0) {
-                       Gtk::TreeSelection::ListHandle_Path selection = get_selection()->get_selected_rows ();
-                       SerializedObjectPointers<DataType>* sr = serialize_pointers (get_model(), &selection, selection_data.get_target());
-                       selection_data.set (8, (guchar*)sr, sr->size);
-                       /* we don't need the allocated block anymore,
-                          it will have been copied. this is wierd - 
-                          the objects we put into the block have
-                          effectively been moved into the copy
-                          made by Gtk::SelectionData::set(),
-                          leaving our memory block a mere ghost.
-                          we're just fixing a memory leak here.
-                       */
-                       delete [] (reinterpret_cast<char*>(sr));
+
+               } else if (selection_data.get_target() == object_type) {
+
+                       start_object_drag ();
+
+                       /* we don't care about the data passed around by DnD, but
+                          we have to provide something otherwise it will stop.
+                        */
+
+                       guchar c;
+                       selection_data.set (8, (guchar*)&c, 1);
                }
        }
        
@@ -108,30 +120,11 @@ class DnDTreeView : public DnDTreeViewBase
                        
                        TreeView::on_drag_data_received (context, x, y, selection_data, info, time);
 
-               } else if (data_column >= 0) {
-                       
-                       /* object D-n-D */
-                       
-                       const void* data = selection_data.get_data();
-                       const SerializedObjectPointers<DataType>* sr = reinterpret_cast<const SerializedObjectPointers<DataType> *>(data);
-                       
-                       if (sr) {
-                               signal_object_drop (sr->type, sr->cnt, sr->data);
-
-                               /* now clean up the pointers in the blob.
-                                  Note that we make an explicit call
-                                  to the destructor rather than using
-                                  delete - the object does not own
-                                  the memory in which it lives - it 
-                                  was allocated as a single contiguous
-                                  block (see below).
-                                */
-
-                               for (uint32_t x = 0; x < sr->cnt; ++x) {
-                                       sr->data[x].~DataType();
-                               }
-                       }
+
+               } else if (selection_data.get_target() == object_type) {
                        
+                       end_object_drag ();
+
                } else {
                        /* some kind of target type added by the app, which will be handled by a signal handler */
                }
@@ -139,45 +132,18 @@ class DnDTreeView : public DnDTreeViewBase
 
   private:
 
-       SerializedObjectPointers<DataType>* serialize_pointers (Glib::RefPtr<Gtk::TreeModel> model, 
-                                                               Gtk::TreeSelection::ListHandle_Path* selection,
-                                                               Glib::ustring type) {
-
-               /* this nasty chunk of code is here because X's DnD protocol (probably other graphics UI's too) 
-                  requires that we package up the entire data collection for DnD in a single contiguous region
-                  (so that it can be trivially copied between address spaces). We don't know the type of DataType so
-                  we have to mix-and-match C and C++ programming techniques here to get the right result.
-
-                  The C trick is to use the "someType foo[0];" declaration trick to create a zero-sized array at the
-                  end of a SerializedObjectPointers<DataType object. Then we allocate a raw memory buffer that extends
-                  past that array and thus provides space for however many DataType items we actually want to pass
-                  around.
-
-                  The C++ trick is to use the placement operator new() syntax to initialize that extra
-                  memory properly.
-               */
-               
-               uint32_t cnt = selection->size();
-               uint32_t sz = (sizeof (DataType) * cnt) + sizeof (SerializedObjectPointers<DataType>);
-
-               char* buf = new char[sz];
-               SerializedObjectPointers<DataType>* sr = (SerializedObjectPointers<DataType>*) buf;
-
-               for (uint32_t i = 0; i < cnt; ++i) {
-                       new ((void *) &sr->data[i]) DataType ();
-               }
-               
-               sr->cnt = cnt;
-               sr->size = sz;
-               snprintf (sr->type, sizeof (sr->type), "%s", type.c_str());
-               
-               cnt = 0;
+       void end_object_drag () {
+               Glib::RefPtr<Gtk::TreeModel> model = drag_data.source->get_model();
+               DataType v;
+               std::list<DataType> l;
+               Gtk::TreeSelection::ListHandle_Path selection = drag_data.source->get_selection()->get_selected_rows ();
                
-               for (Gtk::TreeSelection::ListHandle_Path::iterator x = selection->begin(); x != selection->end(); ++x, ++cnt) {
-                       model->get_iter (*x)->get_value (data_column, sr->data[cnt]);
+               for (Gtk::TreeSelection::ListHandle_Path::iterator x = selection.begin(); x != selection.end(); ++x) {
+                       model->get_iter (*x)->get_value (drag_data.data_column, v);
+                       l.push_back (v);
                }
 
-               return sr;
+               signal_drop (l);
        }
 };