summaryrefslogtreecommitdiff
path: root/libs/gtkmm2ext/gtkmm2ext/dndtreeview.h
blob: 0801058d87680d63b32bc6f4aa791a7fd2a0db78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
    Copyright (C) 2000-2007 Paul Davis

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#ifndef __gtkmm2ext_dndtreeview_h__
#define __gtkmm2ext_dndtreeview_h__

#include <stdint.h>
#include <string>
#include <gtkmm/treeview.h>
#include <gtkmm/treeselection.h>
#include <gtkmm/selectiondata.h>

#include "gtkmm2ext/visibility.h"

namespace Gtkmm2ext {

template<class DataType>
struct /*LIBGTKMM2EXT_API*/ SerializedObjectPointers {
    uint32_t size;
    uint32_t cnt;
    char     type[32];
    DataType data[0];
};

class LIBGTKMM2EXT_API DnDTreeViewBase : public Gtk::TreeView
{
  private:
  public:
	DnDTreeViewBase ();
	~DnDTreeViewBase() {}

	void add_drop_targets (std::list<Gtk::TargetEntry>&);
	void add_object_drag (int column, std::string type_name);

	void on_drag_begin (Glib::RefPtr<Gdk::DragContext> const & context);
	void on_drag_end (Glib::RefPtr<Gdk::DragContext> const & context);

	bool on_button_press_event (GdkEventButton *ev) {
		press_start_x = ev->x;
		press_start_y = ev->y;
		return TreeView::on_button_press_event (ev);
	}

	void on_drag_leave(const Glib::RefPtr<Gdk::DragContext>& context, guint time) {
		suggested_action = context->get_suggested_action();
		TreeView::on_drag_leave (context, time);
	}

	bool on_drag_motion(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, guint time) {
		suggested_action = context->get_suggested_action();
		return TreeView::on_drag_motion (context, x, y, time);
	}

	bool on_drag_drop(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, guint time);

	void set_drag_column (int c) {
		_drag_column = c;
	}

  protected:
	std::list<Gtk::TargetEntry> draggable;
	Gdk::DragAction             suggested_action;
	int                         data_column;
	std::string                 object_type;

	double press_start_x;
	double press_start_y;
	int _drag_column;

	struct DragData {
	    DragData () : source (0) {}

	    Gtk::TreeView* source;
	    int            data_column;
	    std::string    object_type;
	};

	static DragData drag_data;

	void start_object_drag () {
		drag_data.source = this;
		drag_data.data_column = data_column;
		drag_data.object_type = object_type;
	}

	void end_object_drag () {
		drag_data.source = 0;
		drag_data.data_column = -1;
		drag_data.object_type = "";
	}
};

template<class DataType>
class /*LIBGTKMM2EXT_API*/ DnDTreeView : public DnDTreeViewBase
{
  public:
	DnDTreeView() {}
	~DnDTreeView() {}

	sigc::signal<void, const Glib::RefPtr<Gdk::DragContext>&, const Gtk::SelectionData&> signal_drop;

	void on_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, Gtk::SelectionData& selection_data, guint info, guint time) {
		if (selection_data.get_target() == "GTK_TREE_MODEL_ROW") {

			TreeView::on_drag_data_get (context, selection_data, info, time);

		} else if (selection_data.get_target() == object_type) {

			/* return a pointer to this object, which allows
			 * the receiver to call on_drag_data_received()
			 */
			void *c = this;
			selection_data.set (8, (guchar*)&c, sizeof(void*));
		}
	}

	void on_drag_data_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, const Gtk::SelectionData& selection_data, guint info, guint time) {
		if (suggested_action) {
			/* this is a drag motion callback. just update the status to
			   say that we are still dragging, and that's it.
			*/
			suggested_action = Gdk::DragAction (0);
			TreeView::on_drag_data_received (context, x, y, selection_data, info, time);
			return;
		}

		if (selection_data.get_target() == "GTK_TREE_MODEL_ROW") {
			TreeView::on_drag_data_received (context, x, y, selection_data, info, time);
		} else {
			/* some kind of target type, usually 'object_type' added by the app,
			 * which will be handled by a signal handler */
			for (std::list<Gtk::TargetEntry>::const_iterator i = draggable.begin(); i != draggable.end (); ++i) {
				if (selection_data.get_target() == (*i).get_target()) {
					signal_drop (context, selection_data);
					context->drag_finish (true, false, time);
					break;
				}
			}
		}
	}

	/**
	 * This can be called by the Treeview itself or by some other
	 * object that wants to get the list of dragged items.
	 */

	void get_object_drag_data (std::list<DataType>& l, Gtk::TreeView** source) const {

		if (drag_data.source == 0) {
			return;
		}

		Glib::RefPtr<Gtk::TreeModel> model = drag_data.source->get_model();
		DataType v;
		Gtk::TreeSelection::ListHandle_Path selection = drag_data.source->get_selection()->get_selected_rows ();

		for (Gtk::TreeSelection::ListHandle_Path::iterator x = selection.begin(); x != selection.end(); ++x) {
			model->get_iter (*x)->get_value (drag_data.data_column, v);
			l.push_back (v);
		}

		*source = drag_data.source;
	}
};

} // namespace

#endif /* __gtkmm2ext_dndtreeview_h__ */