many changes, read the diffs
[ardour.git] / libs / gtkmm2ext / dndtreeview.cc
1 #include <cstdio>
2 #include <iostream>
3
4 #include <gtkmm2ext/dndtreeview.h>
5
6 using namespace std;
7 using namespace sigc;
8 using namespace Gdk;
9 using namespace Gtk;
10 using namespace Glib;
11 using namespace Gtkmm2ext;
12
13 DnDTreeView::DnDTreeView ()
14         : TreeView ()
15 {
16         draggable.push_back (TargetEntry ("GTK_TREE_MODEL_ROW", TARGET_SAME_WIDGET));
17         data_column = -1;
18
19         enable_model_drag_source (draggable);
20         enable_model_drag_dest (draggable);
21
22         suggested_action = Gdk::DragAction (0);
23 }
24
25 void
26 DnDTreeView::add_drop_targets (list<TargetEntry>& targets)
27 {
28         for (list<TargetEntry>::iterator i = targets.begin(); i != targets.end(); ++i) {
29                 draggable.push_back (*i);
30         }
31         enable_model_drag_source (draggable);
32         enable_model_drag_dest (draggable);
33 }       
34
35 void
36 DnDTreeView::add_object_drag (int column, string type_name)
37 {
38         draggable.push_back (TargetEntry (type_name, TargetFlags(0)));
39         data_column = column;
40
41         enable_model_drag_source (draggable);
42         enable_model_drag_dest (draggable);
43 }
44
45 DnDTreeView::SerializedObjectPointers* 
46 DnDTreeView::serialize_pointers (RefPtr<TreeModel> model, TreeSelection::ListHandle_Path* selection, ustring type)
47 {
48         uint32_t cnt = selection->size();
49         uint32_t sz = (sizeof (void*) * cnt) + sizeof (SerializedObjectPointers);
50
51         char* buf = new char[sz];
52         SerializedObjectPointers* sr = new (buf) SerializedObjectPointers;
53         
54         sr->cnt = cnt;
55         sr->size = sz;
56
57         snprintf (sr->type, sizeof (sr->type), "%s", type.c_str());
58
59         cnt = 0;
60
61         for (TreeSelection::ListHandle_Path::iterator x = selection->begin(); x != selection->end(); ++x, ++cnt) {
62                 TreeModel::Row row = *(model->get_iter (*x));
63                 row.get_value (data_column, sr->ptr[cnt]);
64         }
65         
66         return sr;
67 }
68
69 void
70 DnDTreeView::on_drag_data_get(const RefPtr<DragContext>& context, SelectionData& selection_data, guint info, guint time)
71 {
72         cerr << "DnDTreeview::drag_data_get, target = " << selection_data.get_target() << endl;
73
74         if (selection_data.get_target() == "GTK_TREE_MODEL_ROW") {
75                 
76                 TreeView::on_drag_data_get (context, selection_data, info, time);
77                 
78         } else if (data_column >= 0) {
79                 
80                 Gtk::TreeSelection::ListHandle_Path selection = get_selection()->get_selected_rows ();
81                 SerializedObjectPointers* sr = serialize_pointers (get_model(), &selection, selection_data.get_target());
82                 selection_data.set (8, (guchar*)sr, sr->size);
83                 
84         }
85 }
86
87 void 
88 DnDTreeView::on_drag_data_received(const RefPtr<DragContext>& context, int x, int y, const SelectionData& selection_data, guint info, guint time)
89 {
90         cerr << "DnDTreeview::drag_data_received @ " << x << ',' << y << " target = " << selection_data.get_target() << endl;
91
92         if (suggested_action) {
93                 /* this is a drag motion callback. just update the status to
94                    say that we are still dragging, and that's it.
95                 */
96                 suggested_action = Gdk::DragAction (0);
97                 TreeView::on_drag_data_received (context, x, y, selection_data, info, time);
98                 return;
99         }
100
101         if (selection_data.get_target() == "GTK_TREE_MODEL_ROW") {
102                 
103                 TreeView::on_drag_data_received (context, x, y, selection_data, info, time);
104                 
105         } else if (data_column >= 0) {
106
107                 /* object D-n-D */
108                 
109                 const SerializedObjectPointers* sr = reinterpret_cast<const SerializedObjectPointers *>(selection_data.get_data());
110                 
111                 if (sr) {
112                         signal_object_drop (sr->type, sr->cnt, const_cast<void**>(sr->ptr));
113                 }
114
115         } else {
116                 /* some kind of target type added by the app, which will be handled by a signal handler */
117         }
118 }
119
120