Fix crash when dragging something from another task over the editor window.
[ardour.git] / libs / gtkmm2ext / gtkmm2ext / dndtreeview.h
1 /*
2     Copyright (C) 2000-2007 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __gtkmm2ext_dndtreeview_h__
21 #define __gtkmm2ext_dndtreeview_h__
22
23 #include <stdint.h>
24 #include <string>
25 #include <gtkmm/treeview.h>
26 #include <gtkmm/treeselection.h>
27 #include <gtkmm/selectiondata.h>
28
29 namespace Gtkmm2ext {
30
31 template<class DataType>
32 struct SerializedObjectPointers {
33     uint32_t size;
34     uint32_t cnt;
35     char     type[32];
36     DataType data[0];
37 };
38
39 class DnDTreeViewBase : public Gtk::TreeView 
40 {
41   private:
42   public:
43         DnDTreeViewBase ();
44         ~DnDTreeViewBase() {}
45
46         void add_drop_targets (std::list<Gtk::TargetEntry>&);
47         void add_object_drag (int column, std::string type_name);
48
49         void on_drag_begin (Glib::RefPtr<Gdk::DragContext> const & context) {
50                 Gtk::TreeView::on_drag_begin (context);
51                 start_object_drag ();
52         }
53         
54         void on_drag_leave(const Glib::RefPtr<Gdk::DragContext>& context, guint time) {
55                 suggested_action = context->get_suggested_action();
56                 TreeView::on_drag_leave (context, time);
57         }
58
59         bool on_drag_motion(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, guint time) {
60                 suggested_action = context->get_suggested_action();
61                 return TreeView::on_drag_motion (context, x, y, time);
62         }
63
64         bool on_drag_drop(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, guint time);
65
66   protected:
67         std::list<Gtk::TargetEntry> draggable;
68         Gdk::DragAction             suggested_action;
69         int                         data_column;
70         std::string                 object_type;
71
72         struct DragData {
73             DragData () : source (0) {}
74                 
75             Gtk::TreeView* source;
76             int            data_column;
77             std::string    object_type;
78         };
79         
80         static DragData drag_data;
81
82         void start_object_drag () {
83                 drag_data.source = this;
84                 drag_data.data_column = data_column;
85                 drag_data.object_type = object_type;
86         }
87 };
88
89 template<class DataType>
90 class DnDTreeView : public DnDTreeViewBase
91 {
92   public:
93         DnDTreeView() {} 
94         ~DnDTreeView() {}
95
96         sigc::signal<void,const std::list<DataType>&,Gtk::TreeView*,int,int,Glib::RefPtr<Gdk::DragContext>&> signal_drop;
97
98         void on_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, Gtk::SelectionData& selection_data, guint info, guint time) {
99                 if (selection_data.get_target() == "GTK_TREE_MODEL_ROW") {
100
101                         TreeView::on_drag_data_get (context, selection_data, info, time);
102
103                 } else if (selection_data.get_target() == object_type) {
104
105                         /* we don't care about the data passed around by DnD, but
106                            we have to provide something otherwise it will stop.
107                          */
108
109                         guchar c;
110                         selection_data.set (8, (guchar*)&c, 1);
111                 }
112         }
113         
114         void on_drag_data_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, const Gtk::SelectionData& selection_data, guint info, guint time) {
115                 if (suggested_action) {
116                         /* this is a drag motion callback. just update the status to
117                            say that we are still dragging, and that's it.
118                         */
119                         suggested_action = Gdk::DragAction (0);
120                         TreeView::on_drag_data_received (context, x, y, selection_data, info, time);
121                         return;
122                 }
123                 
124                 if (selection_data.get_target() == "GTK_TREE_MODEL_ROW") {
125                         
126                         TreeView::on_drag_data_received (context, x, y, selection_data, info, time);
127                         
128
129                 } else if (selection_data.get_target() == object_type) {
130                         
131                         end_object_drag (const_cast<Glib::RefPtr<Gdk::DragContext>& > (context), x, y);
132
133                 } else {
134                         /* some kind of target type added by the app, which will be handled by a signal handler */
135                 }
136         }
137
138         /**
139          * This can be called by the Treeview itself or by some other
140          * object that wants to get the list of dragged items.
141          */
142
143         void get_object_drag_data (std::list<DataType>& l, Gtk::TreeView** source) {
144
145                 if (drag_data.source == 0) {
146                         return;
147                 }
148                         
149                 Glib::RefPtr<Gtk::TreeModel> model = drag_data.source->get_model();
150                 DataType v;
151                 Gtk::TreeSelection::ListHandle_Path selection = drag_data.source->get_selection()->get_selected_rows ();
152                 
153                 for (Gtk::TreeSelection::ListHandle_Path::iterator x = selection.begin(); x != selection.end(); ++x) {
154                         model->get_iter (*x)->get_value (drag_data.data_column, v);
155                         l.push_back (v);
156                 }
157                 
158                 *source = drag_data.source;
159         }
160
161   private:
162         void end_object_drag (Glib::RefPtr<Gdk::DragContext>& context, int x, int y) {
163                 std::list<DataType> l;
164                 Gtk::TreeView* source;
165                 get_object_drag_data (l, &source);
166                 signal_drop (l, source, x, y, context);
167         }
168
169 };
170
171 } // namespace
172  
173 #endif /* __gtkmm2ext_dndtreeview_h__ */