Debian Codegraph @

examples/others/dnd/dndwindow.cc

raw
1 /* gtkmm example Copyright (C) 2002 gtkmm development team
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include "dndwindow.h"
18 #include "dnd_images.h"
19 #include <gtkmm/application.h>
20 #include <iostream>
21 #include <map>
22 #include <initializer_list>
23
24 // This example is similar to gtk/tests/testdnd.
25
26 DnDWindow::DnDWindow()
27 : m_Label_Drop("Drop here\n"),
28 m_Label_Drag("Drag Here\n"),
29 m_Label_Popup("Popup\n"),
30 m_PopupWindow()
31 {
32 set_title("Drag-and-drop example");
33 set_default_size(300, 150);
34
35 set_child(m_Grid);
36
37 m_drag_icon = Gdk::Pixbuf::create_from_xpm_data(drag_icon_xpm);
38 m_trashcan_open = Gdk::Pixbuf::create_from_xpm_data(trashcan_open_xpm);
39 m_trashcan_closed = Gdk::Pixbuf::create_from_xpm_data(trashcan_closed_xpm);
40
41 // Drop site
42
43 const GType ustring_type = Glib::Value<Glib::ustring>::value_type();
44 auto dest = Gtk::DropTarget::create(ustring_type, Gdk::DragAction::COPY | Gdk::DragAction::MOVE);
45 dest->signal_drop().connect(sigc::mem_fun(*this, &DnDWindow::on_label_drop_drop), false);
46 m_Label_Drop.add_controller(dest);
47
48 m_Grid.attach(m_Label_Drop, 0, 0);
49 m_Label_Drop.set_expand(true);
50
51 // Popup site
52
53 auto controller = Gtk::DropControllerMotion::create();
54 controller->signal_enter().connect(sigc::mem_fun(*this, &DnDWindow::on_label_popup_enter));
55 controller->signal_leave().connect(sigc::mem_fun(*this, &DnDWindow::on_label_popup_leave));
56 m_Label_Popup.add_controller(controller);
57
58 m_Grid.attach(m_Label_Popup, 1, 1);
59 m_Label_Popup.set_expand(true);
60
61 // Image
62
63 auto async = Gtk::DropTargetAsync::create(
64 Gdk::ContentFormats::create(ustring_type), Gdk::DragAction::COPY | Gdk::DragAction::MOVE);
65 async->signal_drag_enter().connect(sigc::mem_fun(*this, &DnDWindow::on_image_drag_enter), false);
66 async->signal_drag_leave().connect(sigc::mem_fun(*this, &DnDWindow::on_image_drag_leave));
67 async->signal_drop().connect(sigc::mem_fun(*this, &DnDWindow::on_image_drop), false);
68 m_Image.add_controller(async);
69
70 m_Image.set(m_trashcan_closed);
71
72 m_Grid.attach(m_Image, 1, 0);
73 m_Image.set_expand(true);
74
75 // Drag site
76
77 auto source = Gtk::DragSource::create();
78 Glib::Value<Glib::ustring> value;
79 value.init(value.value_type());
80 value.set("I'm data!");
81 auto content = Gdk::ContentProvider::create(value);
82 source->set_content(content);
83 source->set_actions(Gdk::DragAction::COPY | Gdk::DragAction::MOVE);
84 source->set_icon(Gdk::Texture::create_for_pixbuf(m_drag_icon), 0, 0);
85 source->signal_drag_end().connect(sigc::mem_fun(*this, &DnDWindow::on_label_drag_drag_end));
86 m_Label_Drag.add_controller(source);
87
88 m_Grid.attach(m_Label_Drag, 0, 1);
89 m_Label_Drag.set_expand(true);
90
91 create_popup();
92 }
93
94 DnDWindow::~DnDWindow()
95 {
96 }
97
98 void DnDWindow::create_popup()
99 {
100 //Create Grid and fill it:
101 auto pGrid = Gtk::make_managed<Gtk::Grid>();
102
103 auto formats = Gdk::ContentFormats::create(Glib::Value<Glib::ustring>::value_type());
104
105 for (int i = 0; i < 3; i++)
106 {
107 for (int j = 0; j < 3; j++)
108 {
109 char buffer[128];
110 g_snprintf(buffer, sizeof(buffer), "%d,%d", i, j);
111 auto pButton = Gtk::make_managed<Gtk::Button>(buffer);
112 pButton->set_expand(true);
113 pGrid->attach(*pButton, i, j);
114
115 const GType ustring_type = Glib::Value<Glib::ustring>::value_type();
116 auto dest = Gtk::DropTarget::create(ustring_type, Gdk::DragAction::COPY | Gdk::DragAction::MOVE);
117 dest->signal_enter().connect(sigc::mem_fun(*this, &DnDWindow::on_button_popup_enter), false);
118 dest->signal_leave().connect(sigc::mem_fun(*this, &DnDWindow::on_button_popup_leave));
119 dest->signal_drop().connect(sigc::mem_fun(*this, &DnDWindow::on_button_popup_drop), false);
120 pButton->add_controller(dest);
121 }
122 }
123
124 m_PopupWindow.set_child(*pGrid);
125 }
126
127 bool DnDWindow::on_label_drop_drop(const Glib::ValueBase& value, double, double)
128 {
129 if (G_VALUE_HOLDS(value.gobj(), Glib::Value<Glib::ustring>::value_type()))
130 {
131 // We got the value type that we expected.
132 Glib::Value<Glib::ustring> ustring_value;
133 ustring_value.init(value.gobj());
134 const Glib::ustring dropped_string = ustring_value.get();
135
136 std::cout << "Received \"" << dropped_string << "\" in label " << std::endl;
137 return true;
138 }
139 else
140 {
141 std::cout << "Received unexpected data type \""
142 << G_VALUE_TYPE_NAME(value.gobj()) << "\" in label " << std::endl;
143 return false;
144 }
145 }
146
147 void DnDWindow::on_label_popup_enter(double, double)
148 {
149 std::cout << "popsite enter" << std::endl;
150 if (!m_popup_timer)
151 m_popup_timer = Glib::signal_timeout().connect(sigc::mem_fun(*this, &DnDWindow::on_popup_timeout), 500);
152 }
153
154 void DnDWindow::on_label_popup_leave()
155 {
156 std::cout << "popsite leave" << std::endl;
157 if (m_popup_timer)
158 m_popup_timer.disconnect();
159 }
160
161 Gdk::DragAction DnDWindow::on_button_popup_enter(double, double)
162 {
163 std::cout << "popup enter" << std::endl;
164 if (!m_in_popup)
165 {
166 m_in_popup = true;
167 if (m_popdown_timer)
168 {
169 std::cout << "removed popdown" << std::endl;
170 m_popdown_timer.disconnect();
171 }
172 }
173 return Gdk::DragAction::COPY;
174 }
175
176 void DnDWindow::on_button_popup_leave()
177 {
178 std::cout << "popup leave" << std::endl;
179 if (m_in_popup)
180 {
181 m_in_popup = false;
182 if (!m_popdown_timer)
183 {
184 std::cout << "added popdown" << std::endl;
185 m_popdown_timer = Glib::signal_timeout().connect(sigc::mem_fun(*this, &DnDWindow::on_popdown_timeout), 500);
186 }
187 }
188 }
189
190 bool DnDWindow::on_button_popup_drop(const Glib::ValueBase& /* value */, double, double)
191 {
192 on_popdown_timeout();
193 return true;
194 }
195
196 Gdk::DragAction DnDWindow::on_image_drag_enter(const Glib::RefPtr<Gdk::Drop>& drop, double, double)
197 {
198 std::cout << "trashcan enter: " << drop->get_formats()->to_string() << std::endl;
199
200 if (!m_have_drag)
201 {
202 m_have_drag = true;
203 m_Image.set(m_trashcan_open);
204 }
205
206 return action_make_unique(drop->get_actions(), true);
207 }
208
209 void DnDWindow::on_image_drag_leave(const Glib::RefPtr<Gdk::Drop>& drop)
210 {
211 if (drop)
212 std::cout << "trashcan leave: " << drop->get_formats()->to_string() << std::endl;
213 else
214 std::cout << "trashcan leave: " << (m_have_drag ? "no drop" : "after drop") << std::endl;
215
216 m_have_drag = false;
217 m_Image.set(m_trashcan_closed);
218 }
219
220 bool DnDWindow::on_image_drop(const Glib::RefPtr<Gdk::Drop>& drop, double, double)
221 {
222 std::cout << "trashcan drop: " << drop->get_formats()->to_string() << std::endl;
223 m_have_drag = false;
224
225 m_Image.set(m_trashcan_closed);
226
227 drop->finish(action_make_unique(drop->get_actions(), true));
228 return true;
229 }
230
231 void DnDWindow::on_label_drag_drag_end(const Glib::RefPtr<Gdk::Drag>& /* drag */,
232 bool delete_data)
233 {
234 std::cout << (delete_data ? "D" : "Don't d") << "elete the data!" << std::endl;
235 }
236
237 bool DnDWindow::on_popdown_timeout()
238 {
239 m_popdown_timer.disconnect();
240
241 m_PopupWindow.set_visible(false);
242 m_popped_up = false;
243
244 return false;
245 }
246
247 bool DnDWindow::on_popup_timeout()
248 {
249 if (!m_popped_up)
250 {
251 m_PopupWindow.set_transient_for(*this);
252 m_PopupWindow.set_visible(true);
253 m_popped_up = true;
254 }
255
256 m_popup_timer.disconnect();
257
258 return false;
259 }
260
261 //static
262 Gdk::DragAction DnDWindow::action_make_unique(Gdk::DragAction actions, bool print)
263 {
264 if (!print && Gdk::Drag::action_is_unique(actions))
265 return actions;
266
267 if (print)
268 std::cout << "Possible actions:";
269
270 std::map<Gdk::DragAction, Glib::ustring> action_strings;
271 action_strings[Gdk::DragAction::COPY] = " COPY";
272 action_strings[Gdk::DragAction::MOVE] = " MOVE";
273 action_strings[Gdk::DragAction::LINK] = " LINK";
274
275 auto unique_action = static_cast<Gdk::DragAction>(0);
276
277 for (const auto tested_action :
278 {Gdk::DragAction::COPY, Gdk::DragAction::MOVE, Gdk::DragAction::LINK})
279 {
280 if (static_cast<int>(actions & tested_action))
281 {
282 if (print)
283 std::cout << action_strings[tested_action];
284 else
285 return tested_action;
286 if (static_cast<int>(unique_action) == 0)
287 unique_action = tested_action;
288 }
289 }
290 if (print)
291 std::cout << "; Selected action:" << action_strings[unique_action] << std::endl;
292 return unique_action;
293 }