Merged with trunk R920.
[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         cerr << "lets plan to serialize " << cnt << " from selection\n";
52
53         char* buf = new char[sz];
54         SerializedObjectPointers* sr = new (buf) SerializedObjectPointers;
55         
56         sr->cnt = cnt;
57         sr->size = sz;
58
59         snprintf (sr->type, sizeof (sr->type), "%s", type.c_str());
60
61         cnt = 0;
62
63         for (TreeSelection::ListHandle_Path::iterator x = selection->begin(); x != selection->end(); ++x, ++cnt) {
64                 cerr << "getting next item\n";
65                 TreeModel::Row row = *(model->get_iter (*x));
66                 row.get_value (data_column, sr->ptr[cnt]);
67         }
68
69         cerr << "returning an SR with size = " << sr->size << endl;
70         return sr;
71 }
72
73 void
74 DnDTreeView::on_drag_data_get(const RefPtr<DragContext>& context, SelectionData& selection_data, guint info, guint time)
75 {
76         if (selection_data.get_target() == "GTK_TREE_MODEL_ROW") {
77
78                 TreeView::on_drag_data_get (context, selection_data, info, time);
79                 
80         } else if (data_column >= 0) {
81                 
82                 Gtk::TreeSelection::ListHandle_Path selection = get_selection()->get_selected_rows ();
83                 SerializedObjectPointers* sr = serialize_pointers (get_model(), &selection, selection_data.get_target());
84                 selection_data.set (8, (guchar*)sr, sr->size);
85                 
86                 cerr << "selection data set to contain " << sr->size << endl;
87         }
88 }
89
90 void 
91 DnDTreeView::on_drag_data_received(const RefPtr<DragContext>& context, int x, int y, const SelectionData& selection_data, guint info, guint time)
92 {
93         if (suggested_action) {
94                 /* this is a drag motion callback. just update the status to
95                    say that we are still dragging, and that's it.
96                 */
97                 suggested_action = Gdk::DragAction (0);
98                 TreeView::on_drag_data_received (context, x, y, selection_data, info, time);
99                 return;
100         }
101
102         if (selection_data.get_target() == "GTK_TREE_MODEL_ROW") {
103                 
104                 TreeView::on_drag_data_received (context, x, y, selection_data, info, time);
105                 
106         } else if (data_column >= 0) {
107
108                 /* object D-n-D */
109                 
110                 const SerializedObjectPointers* sr = reinterpret_cast<const SerializedObjectPointers *>(selection_data.get_data());
111                 
112                 if (sr) {
113                         signal_object_drop (sr->type, sr->cnt, const_cast<void**>(sr->ptr));
114                 }
115
116         } else {
117                 /* some kind of target type added by the app, which will be handled by a signal handler */
118         }
119 }
120
121 bool 
122 DnDTreeView::on_drag_drop(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, guint time)
123 {
124         suggested_action = Gdk::DragAction (0);
125         return TreeView::on_drag_drop (context, x, y, time);
126 }
127