2 Copyright (C) 2000-2007 Paul Davis
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.
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.
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.
20 #ifndef __gtkmm2ext_dndtreeview_h__
21 #define __gtkmm2ext_dndtreeview_h__
25 #include <gtkmm/treeview.h>
26 #include <gtkmm/treeselection.h>
27 #include <gtkmm/selectiondata.h>
31 template<class DataType>
32 struct SerializedObjectPointers {
39 class DnDTreeViewBase : public Gtk::TreeView
46 void add_drop_targets (std::list<Gtk::TargetEntry>&);
47 void add_object_drag (int column, std::string type_name);
49 void on_drag_leave(const Glib::RefPtr<Gdk::DragContext>& context, guint time) {
50 suggested_action = context->get_suggested_action();
51 TreeView::on_drag_leave (context, time);
54 bool on_drag_motion(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, guint time) {
55 suggested_action = context->get_suggested_action();
56 return TreeView::on_drag_motion (context, x, y, time);
59 bool on_drag_drop(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, guint time);
62 std::list<Gtk::TargetEntry> draggable;
63 Gdk::DragAction suggested_action;
65 std::string object_type;
68 Gtk::TreeView* source;
70 std::string object_type;
73 static DragData drag_data;
75 void start_object_drag () {
76 drag_data.source = this;
77 drag_data.data_column = data_column;
78 drag_data.object_type = object_type;
82 template<class DataType>
83 class DnDTreeView : public DnDTreeViewBase
89 sigc::signal<void,const std::list<DataType>& > signal_drop;
91 void on_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, Gtk::SelectionData& selection_data, guint info, guint time) {
92 if (selection_data.get_target() == "GTK_TREE_MODEL_ROW") {
94 TreeView::on_drag_data_get (context, selection_data, info, time);
96 } else if (selection_data.get_target() == object_type) {
100 /* we don't care about the data passed around by DnD, but
101 we have to provide something otherwise it will stop.
105 selection_data.set (8, (guchar*)&c, 1);
109 void on_drag_data_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, const Gtk::SelectionData& selection_data, guint info, guint time) {
110 if (suggested_action) {
111 /* this is a drag motion callback. just update the status to
112 say that we are still dragging, and that's it.
114 suggested_action = Gdk::DragAction (0);
115 TreeView::on_drag_data_received (context, x, y, selection_data, info, time);
119 if (selection_data.get_target() == "GTK_TREE_MODEL_ROW") {
121 TreeView::on_drag_data_received (context, x, y, selection_data, info, time);
124 } else if (selection_data.get_target() == object_type) {
129 /* some kind of target type added by the app, which will be handled by a signal handler */
134 * this can be called by the Treeview itself or by some other
135 * object that wants to get the list of dragged items.
138 void get_object_drag_data (std::list<DataType>& l) {
139 Glib::RefPtr<Gtk::TreeModel> model = drag_data.source->get_model();
141 Gtk::TreeSelection::ListHandle_Path selection = drag_data.source->get_selection()->get_selected_rows ();
143 for (Gtk::TreeSelection::ListHandle_Path::iterator x = selection.begin(); x != selection.end(); ++x) {
144 model->get_iter (*x)->get_value (drag_data.data_column, v);
150 void end_object_drag () {
151 std::list<DataType> l;
152 get_object_drag_data (l);
160 #endif /* __gtkmm2ext_dndtreeview_h__ */