1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gtk.DropTarget;
26 
27 private import gdk.ContentFormats;
28 private import gdk.Drop;
29 private import glib.ConstructionException;
30 private import gobject.ObjectG;
31 private import gobject.Signals;
32 private import gobject.Value;
33 private import gtk.EventController;
34 private import gtk.c.functions;
35 public  import gtk.c.types;
36 private import std.algorithm;
37 
38 
39 /**
40  * `GtkDropTarget` is an event controller to receive Drag-and-Drop operations.
41  * 
42  * The most basic way to use a `GtkDropTarget` to receive drops on a
43  * widget is to create it via [ctor@Gtk.DropTarget.new], passing in the
44  * `GType` of the data you want to receive and connect to the
45  * [signal@Gtk.DropTarget::drop] signal to receive the data:
46  * 
47  * ```c
48  * static gboolean
49  * on_drop (GtkDropTarget *target,
50  * const GValue  *value,
51  * double         x,
52  * double         y,
53  * gpointer       data)
54  * {
55  * MyWidget *self = data;
56  * 
57  * // Call the appropriate setter depending on the type of data
58  * // that we received
59  * if (G_VALUE_HOLDS (value, G_TYPE_FILE))
60  * my_widget_set_file (self, g_value_get_object (value));
61  * else if (G_VALUE_HOLDS (value, GDK_TYPE_PIXBUF))
62  * my_widget_set_pixbuf (self, g_value_get_object (value));
63  * else
64  * return FALSE;
65  * 
66  * return TRUE;
67  * }
68  * 
69  * static void
70  * my_widget_init (MyWidget *self)
71  * {
72  * GtkDropTarget *target =
73  * gtk_drop_target_new (G_TYPE_INVALID, GDK_ACTION_COPY);
74  * 
75  * // This widget accepts two types of drop types: GFile objects
76  * // and GdkPixbuf objects
77  * gtk_drop_target_set_gtypes (target, (GTypes [2]) {
78  * G_TYPE_FILE,
79  * GDK_TYPE_PIXBUF,
80  * }, 2);
81  * 
82  * gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (target));
83  * }
84  * ```
85  * 
86  * `GtkDropTarget` supports more options, such as:
87  * 
88  * * rejecting potential drops via the [signal@Gtk.DropTarget::accept] signal
89  * and the [method@Gtk.DropTarget.reject] function to let other drop
90  * targets handle the drop
91  * * tracking an ongoing drag operation before the drop via the
92  * [signal@Gtk.DropTarget::enter], [signal@Gtk.DropTarget::motion] and
93  * [signal@Gtk.DropTarget::leave] signals
94  * * configuring how to receive data by setting the
95  * [property@Gtk.DropTarget:preload] property and listening for its
96  * availability via the [property@Gtk.DropTarget:value] property
97  * 
98  * However, `GtkDropTarget` is ultimately modeled in a synchronous way
99  * and only supports data transferred via `GType`. If you want full control
100  * over an ongoing drop, the [class@Gtk.DropTargetAsync] object gives you
101  * this ability.
102  * 
103  * While a pointer is dragged over the drop target's widget and the drop
104  * has not been rejected, that widget will receive the
105  * %GTK_STATE_FLAG_DROP_ACTIVE state, which can be used to style the widget.
106  * 
107  * If you are not interested in receiving the drop, but just want to update
108  * UI state during a Drag-and-Drop operation (e.g. switching tabs), you can
109  * use [class@Gtk.DropControllerMotion].
110  */
111 public class DropTarget : EventController
112 {
113 	/** the main Gtk struct */
114 	protected GtkDropTarget* gtkDropTarget;
115 
116 	/** Get the main Gtk struct */
117 	public GtkDropTarget* getDropTargetStruct(bool transferOwnership = false)
118 	{
119 		if (transferOwnership)
120 			ownedRef = false;
121 		return gtkDropTarget;
122 	}
123 
124 	/** the main Gtk struct as a void* */
125 	protected override void* getStruct()
126 	{
127 		return cast(void*)gtkDropTarget;
128 	}
129 
130 	/**
131 	 * Sets our main struct and passes it to the parent class.
132 	 */
133 	public this (GtkDropTarget* gtkDropTarget, bool ownedRef = false)
134 	{
135 		this.gtkDropTarget = gtkDropTarget;
136 		super(cast(GtkEventController*)gtkDropTarget, ownedRef);
137 	}
138 
139 
140 	/** */
141 	public static GType getType()
142 	{
143 		return gtk_drop_target_get_type();
144 	}
145 
146 	/**
147 	 * Creates a new `GtkDropTarget` object.
148 	 *
149 	 * If the drop target should support more than 1 type, pass
150 	 * %G_TYPE_INVALID for @type and then call
151 	 * [method@Gtk.DropTarget.set_gtypes].
152 	 *
153 	 * Params:
154 	 *     type = The supported type or %G_TYPE_INVALID
155 	 *     actions = the supported actions
156 	 *
157 	 * Returns: the new `GtkDropTarget`
158 	 *
159 	 * Throws: ConstructionException GTK+ fails to create the object.
160 	 */
161 	public this(GType type, GdkDragAction actions)
162 	{
163 		auto __p = gtk_drop_target_new(type, actions);
164 
165 		if(__p is null)
166 		{
167 			throw new ConstructionException("null returned by new");
168 		}
169 
170 		this(cast(GtkDropTarget*) __p, true);
171 	}
172 
173 	/**
174 	 * Gets the actions that this drop target supports.
175 	 *
176 	 * Returns: the actions that this drop target supports
177 	 */
178 	public GdkDragAction getActions()
179 	{
180 		return gtk_drop_target_get_actions(gtkDropTarget);
181 	}
182 
183 	/**
184 	 * Gets the currently handled drop operation.
185 	 *
186 	 * If no drop operation is going on, %NULL is returned.
187 	 *
188 	 * Returns: The current drop
189 	 */
190 	public Drop getDrop()
191 	{
192 		auto __p = gtk_drop_target_get_drop(gtkDropTarget);
193 
194 		if(__p is null)
195 		{
196 			return null;
197 		}
198 
199 		return ObjectG.getDObject!(Drop)(cast(GdkDrop*) __p);
200 	}
201 
202 	/**
203 	 * Gets the data formats that this drop target accepts.
204 	 *
205 	 * If the result is %NULL, all formats are expected to be supported.
206 	 *
207 	 * Returns: the supported data formats
208 	 */
209 	public ContentFormats getFormats()
210 	{
211 		auto __p = gtk_drop_target_get_formats(gtkDropTarget);
212 
213 		if(__p is null)
214 		{
215 			return null;
216 		}
217 
218 		return ObjectG.getDObject!(ContentFormats)(cast(GdkContentFormats*) __p, true);
219 	}
220 
221 	/**
222 	 * Gets the list of supported `GTypes` for @self.
223 	 *
224 	 * If no type have been set, %NULL will be returned.
225 	 *
226 	 * Returns: %G_TYPE_INVALID-terminated array of types included in
227 	 *     @formats or %NULL if none.
228 	 */
229 	public GType[] getGtypes()
230 	{
231 		size_t nTypes;
232 
233 		auto __p = gtk_drop_target_get_gtypes(gtkDropTarget, &nTypes);
234 
235 		return __p[0 .. nTypes];
236 	}
237 
238 	/**
239 	 * Gets whether data should be preloaded on hover.
240 	 *
241 	 * Returns: %TRUE if drop data should be preloaded
242 	 */
243 	public bool getPreload()
244 	{
245 		return gtk_drop_target_get_preload(gtkDropTarget) != 0;
246 	}
247 
248 	/**
249 	 * Gets the current drop data, as a `GValue`.
250 	 *
251 	 * Returns: The current drop data
252 	 */
253 	public Value getValue()
254 	{
255 		auto __p = gtk_drop_target_get_value(gtkDropTarget);
256 
257 		if(__p is null)
258 		{
259 			return null;
260 		}
261 
262 		return ObjectG.getDObject!(Value)(cast(GValue*) __p);
263 	}
264 
265 	/**
266 	 * Rejects the ongoing drop operation.
267 	 *
268 	 * If no drop operation is ongoing, i.e when [property@Gtk.DropTarget:drop]
269 	 * is %NULL, this function does nothing.
270 	 *
271 	 * This function should be used when delaying the decision
272 	 * on whether to accept a drag or not until after reading
273 	 * the data.
274 	 */
275 	public void reject()
276 	{
277 		gtk_drop_target_reject(gtkDropTarget);
278 	}
279 
280 	/**
281 	 * Sets the actions that this drop target supports.
282 	 *
283 	 * Params:
284 	 *     actions = the supported actions
285 	 */
286 	public void setActions(GdkDragAction actions)
287 	{
288 		gtk_drop_target_set_actions(gtkDropTarget, actions);
289 	}
290 
291 	/**
292 	 * Sets the supported `GTypes` for this drop target.
293 	 *
294 	 * Params:
295 	 *     types = all supported #GTypes that can be dropped
296 	 */
297 	public void setGtypes(GType[] types)
298 	{
299 		gtk_drop_target_set_gtypes(gtkDropTarget, types.ptr, cast(size_t)types.length);
300 	}
301 
302 	/**
303 	 * Sets whether data should be preloaded on hover.
304 	 *
305 	 * Params:
306 	 *     preload = %TRUE to preload drop data
307 	 */
308 	public void setPreload(bool preload)
309 	{
310 		gtk_drop_target_set_preload(gtkDropTarget, preload);
311 	}
312 
313 	/**
314 	 * Emitted on the drop site when a drop operation is about to begin.
315 	 *
316 	 * If the drop is not accepted, %FALSE will be returned and the drop target
317 	 * will ignore the drop. If %TRUE is returned, the drop is accepted for now
318 	 * but may be rejected later via a call to [method@Gtk.DropTarget.reject]
319 	 * or ultimately by returning %FALSE from a [signal@Gtk.DropTarget::drop]
320 	 * handler.
321 	 *
322 	 * The default handler for this signal decides whether to accept the drop
323 	 * based on the formats provided by the @drop.
324 	 *
325 	 * If the decision whether the drop will be accepted or rejected depends
326 	 * on the data, this function should return %TRUE, the
327 	 * [property@Gtk.DropTarget:preload] property should be set and the value
328 	 * should be inspected via the ::notify:value signal, calling
329 	 * [method@Gtk.DropTarget.reject] if required.
330 	 *
331 	 * Params:
332 	 *     drop = the `GdkDrop`
333 	 *
334 	 * Returns: %TRUE if @drop is accepted
335 	 */
336 	gulong addOnAccept(bool delegate(Drop, DropTarget) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
337 	{
338 		return Signals.connect(this, "accept", dlg, connectFlags ^ ConnectFlags.SWAPPED);
339 	}
340 
341 	/**
342 	 * Emitted on the drop site when the user drops the data onto the widget.
343 	 *
344 	 * The signal handler must determine whether the pointer position is in
345 	 * a drop zone or not. If it is not in a drop zone, it returns %FALSE
346 	 * and no further processing is necessary.
347 	 *
348 	 * Otherwise, the handler returns %TRUE. In this case, this handler will
349 	 * accept the drop. The handler is responsible for rading the given @value
350 	 * and performing the drop operation.
351 	 *
352 	 * Params:
353 	 *     value = the #GValue being dropped
354 	 *     x = the x coordinate of the current pointer position
355 	 *     y = the y coordinate of the current pointer position
356 	 *
357 	 * Returns: whether the drop was accepted at the given pointer position
358 	 */
359 	gulong addOnDrop(bool delegate(Value, double, double, DropTarget) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
360 	{
361 		return Signals.connect(this, "drop", dlg, connectFlags ^ ConnectFlags.SWAPPED);
362 	}
363 
364 	/**
365 	 * Emitted on the drop site when the pointer enters the widget.
366 	 *
367 	 * It can be used to set up custom highlighting.
368 	 *
369 	 * Params:
370 	 *     x = the x coordinate of the current pointer position
371 	 *     y = the y coordinate of the current pointer position
372 	 *
373 	 * Returns: Preferred action for this drag operation or 0 if
374 	 *     dropping is not supported at the current @x,@y location.
375 	 */
376 	gulong addOnEnter(GdkDragAction delegate(double, double, DropTarget) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
377 	{
378 		return Signals.connect(this, "enter", dlg, connectFlags ^ ConnectFlags.SWAPPED);
379 	}
380 
381 	/**
382 	 * Emitted on the drop site when the pointer leaves the widget.
383 	 *
384 	 * Its main purpose it to undo things done in
385 	 * [signal@Gtk.DropTarget::enter].
386 	 */
387 	gulong addOnLeave(void delegate(DropTarget) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
388 	{
389 		return Signals.connect(this, "leave", dlg, connectFlags ^ ConnectFlags.SWAPPED);
390 	}
391 
392 	/**
393 	 * Emitted while the pointer is moving over the drop target.
394 	 *
395 	 * Params:
396 	 *     x = the x coordinate of the current pointer position
397 	 *     y = the y coordinate of the current pointer position
398 	 *
399 	 * Returns: Preferred action for this drag operation or 0 if
400 	 *     dropping is not supported at the current @x,@y location.
401 	 */
402 	gulong addOnMotion(GdkDragAction delegate(double, double, DropTarget) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
403 	{
404 		return Signals.connect(this, "motion", dlg, connectFlags ^ ConnectFlags.SWAPPED);
405 	}
406 }