Merge branch 'master' into cairocanvas
[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                 Gtk::TreeView::on_drag_begin (context);
53                 start_object_drag ();
54         }
55         
56         void on_drag_leave(const Glib::RefPtr<Gdk::DragContext>& context, guint time) {
57                 suggested_action = context->get_suggested_action();
58                 TreeView::on_drag_leave (context, time);
59         }
60
61         bool on_drag_motion(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, guint time) {
62                 suggested_action = context->get_suggested_action();
63                 return TreeView::on_drag_motion (context, x, y, time);
64         }
65
66         bool on_drag_drop(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, guint time);
67
68   protected:
69         std::list<Gtk::TargetEntry> draggable;
70         Gdk::DragAction             suggested_action;
71         int                         data_column;
72         std::string                 object_type;
73
74         struct DragData {
75             DragData () : source (0) {}
76                 
77             Gtk::TreeView* source;
78             int            data_column;
79             std::string    object_type;
80         };
81         
82         static DragData drag_data;
83
84         void start_object_drag () {
85                 drag_data.source = this;
86                 drag_data.data_column = data_column;
87                 drag_data.object_type = object_type;
88         }
89 };
90
91 template<class DataType>
92 class LIBGTKMM2EXT_API DnDTreeView : public DnDTreeViewBase
93 {
94   public:
95         DnDTreeView() {} 
96         ~DnDTreeView() {}
97
98         sigc::signal<void,const std::list<DataType>&,Gtk::TreeView*,int,int,Glib::RefPtr<Gdk::DragContext>&> signal_drop;
99
100         void on_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, Gtk::SelectionData& selection_data, guint info, guint time) {
101                 if (selection_data.get_target() == "GTK_TREE_MODEL_ROW") {
102
103                         TreeView::on_drag_data_get (context, selection_data, info, time);
104
105                 } else if (selection_data.get_target() == object_type) {
106
107                         /* we don't care about the data passed around by DnD, but
108                            we have to provide something otherwise it will stop.
109                          */
110
111                         guchar c;
112                         selection_data.set (8, (guchar*)&c, 1);
113                 }
114         }
115         
116         void on_drag_data_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, const Gtk::SelectionData& selection_data, guint info, guint time) {
117                 if (suggested_action) {
118                         /* this is a drag motion callback. just update the status to
119                            say that we are still dragging, and that's it.
120                         */
121                         suggested_action = Gdk::DragAction (0);
122                         TreeView::on_drag_data_received (context, x, y, selection_data, info, time);
123                         return;
124                 }
125                 
126                 if (selection_data.get_target() == "GTK_TREE_MODEL_ROW") {
127                         
128                         TreeView::on_drag_data_received (context, x, y, selection_data, info, time);
129                         
130
131                 } else if (selection_data.get_target() == object_type) {
132                         
133                         end_object_drag (const_cast<Glib::RefPtr<Gdk::DragContext>& > (context), x, y);
134
135                 } else {
136                         /* some kind of target type added by the app, which will be handled by a signal handler */
137                 }
138         }
139
140         /**
141          * This can be called by the Treeview itself or by some other
142          * object that wants to get the list of dragged items.
143          */
144
145         void get_object_drag_data (std::list<DataType>& l, Gtk::TreeView** source) {
146
147                 if (drag_data.source == 0) {
148                         return;
149                 }
150                         
151                 Glib::RefPtr<Gtk::TreeModel> model = drag_data.source->get_model();
152                 DataType v;
153                 Gtk::TreeSelection::ListHandle_Path selection = drag_data.source->get_selection()->get_selected_rows ();
154                 
155                 for (Gtk::TreeSelection::ListHandle_Path::iterator x = selection.begin(); x != selection.end(); ++x) {
156                         model->get_iter (*x)->get_value (drag_data.data_column, v);
157                         l.push_back (v);
158                 }
159                 
160                 *source = drag_data.source;
161         }
162
163   private:
164         void end_object_drag (Glib::RefPtr<Gdk::DragContext>& context, int x, int y) {
165                 std::list<DataType> l;
166                 Gtk::TreeView* source;
167                 get_object_drag_data (l, &source);
168                 signal_drop (l, source, x, y, context);
169         }
170
171 };
172
173 } // namespace
174  
175 #endif /* __gtkmm2ext_dndtreeview_h__ */