allow DnD between DnDTreeView and DnDVbox
[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 #include "gtkmm2ext/visibility.h"
30
31 namespace Gtkmm2ext {
32
33 template<class DataType>
34 struct /*LIBGTKMM2EXT_API*/ SerializedObjectPointers {
35     uint32_t size;
36     uint32_t cnt;
37     char     type[32];
38     DataType data[0];
39 };
40
41 class LIBGTKMM2EXT_API DnDTreeViewBase : public Gtk::TreeView
42 {
43   private:
44   public:
45         DnDTreeViewBase ();
46         ~DnDTreeViewBase() {}
47
48         void add_drop_targets (std::list<Gtk::TargetEntry>&);
49         void add_object_drag (int column, std::string type_name);
50
51         void on_drag_begin (Glib::RefPtr<Gdk::DragContext> const & context);
52         void on_drag_end (Glib::RefPtr<Gdk::DragContext> const & context);
53
54         bool on_button_press_event (GdkEventButton *ev) {
55                 press_start_x = ev->x;
56                 press_start_y = ev->y;
57                 return TreeView::on_button_press_event (ev);
58         }
59
60         void on_drag_leave(const Glib::RefPtr<Gdk::DragContext>& context, guint time) {
61                 suggested_action = context->get_suggested_action();
62                 TreeView::on_drag_leave (context, time);
63         }
64
65         bool on_drag_motion(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, guint time) {
66                 suggested_action = context->get_suggested_action();
67                 return TreeView::on_drag_motion (context, x, y, time);
68         }
69
70         bool on_drag_drop(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, guint time);
71
72         void set_drag_column (int c) {
73                 _drag_column = c;
74         }
75
76   protected:
77         std::list<Gtk::TargetEntry> draggable;
78         Gdk::DragAction             suggested_action;
79         int                         data_column;
80         std::string                 object_type;
81
82         double press_start_x;
83         double press_start_y;
84         int _drag_column;
85
86         struct DragData {
87             DragData () : source (0) {}
88
89             Gtk::TreeView* source;
90             int            data_column;
91             std::string    object_type;
92         };
93
94         static DragData drag_data;
95
96         void start_object_drag () {
97                 drag_data.source = this;
98                 drag_data.data_column = data_column;
99                 drag_data.object_type = object_type;
100         }
101
102         void end_object_drag () {
103                 drag_data.source = 0;
104                 drag_data.data_column = -1;
105                 drag_data.object_type = "";
106         }
107 };
108
109 template<class DataType>
110 class /*LIBGTKMM2EXT_API*/ DnDTreeView : public DnDTreeViewBase
111 {
112   public:
113         DnDTreeView() {}
114         ~DnDTreeView() {}
115
116         sigc::signal<void, const Glib::RefPtr<Gdk::DragContext>&, const Gtk::SelectionData&> signal_drop;
117
118         void on_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, Gtk::SelectionData& selection_data, guint info, guint time) {
119                 if (selection_data.get_target() == "GTK_TREE_MODEL_ROW") {
120
121                         TreeView::on_drag_data_get (context, selection_data, info, time);
122
123                 } else if (selection_data.get_target() == object_type) {
124
125                         /* return a pointer to this object, which allows
126                          * the receiver to call on_drag_data_received()
127                          */
128                         void *c = this;
129                         selection_data.set (8, (guchar*)&c, sizeof(void*));
130                 }
131         }
132
133         void on_drag_data_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, const Gtk::SelectionData& selection_data, guint info, guint time) {
134                 if (suggested_action) {
135                         /* this is a drag motion callback. just update the status to
136                            say that we are still dragging, and that's it.
137                         */
138                         suggested_action = Gdk::DragAction (0);
139                         TreeView::on_drag_data_received (context, x, y, selection_data, info, time);
140                         return;
141                 }
142
143                 if (selection_data.get_target() == "GTK_TREE_MODEL_ROW") {
144                         TreeView::on_drag_data_received (context, x, y, selection_data, info, time);
145                 } else if (selection_data.get_target() == object_type) {
146                         signal_drop (context, selection_data);
147                 } else {
148                         /* some kind of target type added by the app, which will be handled by a signal handler */
149                 }
150         }
151
152         /**
153          * This can be called by the Treeview itself or by some other
154          * object that wants to get the list of dragged items.
155          */
156
157         void get_object_drag_data (std::list<DataType>& l, Gtk::TreeView** source) const {
158
159                 if (drag_data.source == 0) {
160                         return;
161                 }
162
163                 Glib::RefPtr<Gtk::TreeModel> model = drag_data.source->get_model();
164                 DataType v;
165                 Gtk::TreeSelection::ListHandle_Path selection = drag_data.source->get_selection()->get_selected_rows ();
166
167                 for (Gtk::TreeSelection::ListHandle_Path::iterator x = selection.begin(); x != selection.end(); ++x) {
168                         model->get_iter (*x)->get_value (drag_data.data_column, v);
169                         l.push_back (v);
170                 }
171
172                 *source = drag_data.source;
173         }
174 };
175
176 } // namespace
177
178 #endif /* __gtkmm2ext_dndtreeview_h__ */