It derives from <GtkEventController>.
An event controller to receive Drag-and-Drop operations.
The most basic way to use a <GtkDropTarget> to
receive drops on a widget is to create it via drop-target:new, passing in the GType of the data you want
to receive and connect to the drop-target:drop signal to receive the data:
static gboolean
on_drop (GtkDropTarget *target,
const GValue *value,
double x,
double y,
gpointer data)
{
MyWidget *self = data;
// Call the appropriate setter depending on the type of data
// that we received
if (G_VALUE_HOLDS (value, G_TYPE_FILE))
my_widget_set_file (self, g_value_get_object (value));
else if (G_VALUE_HOLDS (value, GDK_TYPE_PIXBUF))
my_widget_set_pixbuf (self, g_value_get_object (value));
else
return FALSE;
return TRUE;
}
static void
my_widget_init (MyWidget *self)
{
GtkDropTarget *target =
gtk_drop_target_new (G_TYPE_INVALID, GDK_ACTION_COPY);
// This widget accepts two types of drop types: GFile objects
// and GdkPixbuf objects
gtk_drop_target_set_gtypes (target, (GType [2]) {
G_TYPE_FILE,
GDK_TYPE_PIXBUF,
}, 2);
g_signal_connect (target, "drop", G_CALLBACK (on_drop), self);
gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (target));
}
<GtkDropTarget> supports more options, such as:
drop-target:accept signal and the drop-target:reject function to let other drop targets handle
the drop
drop-target:enter,
drop-target:motion and
drop-target:leave signals
drop-target:preload property and listening for its
availability via the drop-target:value property
However, <GtkDropTarget> is ultimately modeled
in a synchronous way and only supports data transferred via
GType. If you want full control over an ongoing drop, the
<GtkDropTargetAsync> object gives you this
ability.
While a pointer is dragged over the drop target’s widget and the drop
has not been rejected, that widget will receive the
drop-active state, which can be used to style
the widget.
If you are not interested in receiving the drop, but just want to update
UI state during a Drag-and-Drop operation (e.g. switching tabs), you
can use <GtkDropControllerMotion>.
The GdkDragActions that this drop target supports.
Its value is a <GdkDragAction> (or, to set
it, a list of its symbols, or one of them).
Set it with (set! (drop-target:actions self) value).
The <GdkDrop> that is currently being performed.
Its value is a <GdkDrop>, or #f.
It is read only.
The <GdkDrop> that is currently being performed.
Its value is a <GdkDrop>, or #f.
It is read only.
The <GdkContentFormats> that determine
the supported data formats.
Its value is a <GdkContentFormats>, or
#f.
It can only be set when the object is made, with #:formats.
Whether the drop data should be preloaded when the pointer is only hovering over the widget but has not been released.
Setting this property allows finer grained reaction to an ongoing drop at the cost of loading more data.
The default value for this property is #f to avoid downloading
huge amounts of data by accident.
For example, if somebody drags a full document of gigabytes of text from a text editor across a widget with a preloading drop target, this data will be downloaded, even if the data is ultimately dropped elsewhere.
For a lot of data formats, the amount of data is very small (like
GDK_TYPE_RGBA), so enabling this property does not hurt at all.
And for local-only Drag-and-Drop operations, no data transfer is done,
so enabling it there is free.
Its value is a boolean.
Set it with (set! (drop-target:preload self) value).
The value for this drop operation.
This is #f if the data has not been loaded yet or no drop
operation is going on.
Data may be available before the drop-target:drop signal gets emitted - for example when the
drop-target:preload property
is set. You can use the ::notify signal to be notified of available
data.
Its value is any value.
It is read only.
Gets the actions that this drop target supports.
Passed: a <GtkDropTarget>.
Returned: a <GdkDragAction>.
the actions that this drop target supports
Gets the currently handled drop operation.
If no drop operation is going on, #f is returned.
Passed: a <GtkDropTarget>.
Returned: a <GdkDrop>, or #f.
The current drop
Gets the currently handled drop operation.
If no drop operation is going on, #f is returned.
Passed: a <GtkDropTarget>.
Returned: a <GdkDrop>, or #f.
The current drop
Gets the data formats that this drop target accepts.
If the result is #f, all formats are expected to be supported.
Passed: a <GtkDropTarget>.
Returned: a <GdkContentFormats>, or
#f.
the supported data formats
Gets the list of supported GTypes that can be dropped on the
target.
If no types have been set, NULL will be returned.
Passed: a <GtkDropTarget>.
Returned: a vector, each a class, or a GType number, or #f.
the G_TYPE_INVALID-terminated array of types included in formats
Not returned: it is the length of %return.
the number of GTypes contained in the return value
Gets whether data should be preloaded on hover.
Passed: a <GtkDropTarget>.
Returned: a boolean.
#t if drop data should be preloaded
Gets the current drop data, as a <GValue>.
Passed: a <GtkDropTarget>.
Returned: a <GValue>, or #f.
The current drop data
Creates a new <GtkDropTarget> object.
If the drop target should support more than 1 type, pass
G_TYPE_INVALID for type and then call drop-target:set-gtypes.
Passed: a class, or a GType number.
The supported type or G_TYPE_INVALID
Passed: a <GdkDragAction>, or a list of its
symbols, or one of them.
the supported actions
Returned: a <GtkDropTarget>.
the new <GtkDropTarget>
Rejects the ongoing drop operation.
If no drop operation is ongoing, i.e when drop-target:current-drop is #f, this function
does nothing.
This function should be used when delaying the decision on whether to accept a drag or not until after reading the data.
Passed: a <GtkDropTarget>.
Sets the actions that this drop target supports.
Passed: a <GtkDropTarget>.
Passed: a <GdkDragAction>, or a list of its
symbols, or one of them.
the supported actions
Sets the supported GTypes for this drop target.
Passed: a <GtkDropTarget>.
Passed: a list or vector, each a class, or a GType number, or #f.
all supported GTypes that can be dropped on the target
Not passed: guile-gi gives the length of types.
number of types
Sets whether data should be preloaded on hover.
Passed: a <GtkDropTarget>.
Passed: a boolean.
#t to preload drop data
Connect a handler with (connect object drop-target:accept
handler). It is called as (handler self drop) ⇒ %return.
Emitted on the drop site when a drop operation is about to begin.
If the drop is not accepted, #f will be returned and the drop
target will ignore the drop. If #t is returned, the drop is
accepted for now but may be rejected later via a call to
drop-target:reject or ultimately by
returning #f from a drop-target:drop handler.
The default handler for this signal decides whether to accept the drop based on the formats provided by the drop.
If the decision whether the drop will be accepted or rejected depends on
the data, this function should return #t, the
drop-target:preload property
should be set and the value should be inspected via the ::notify:value
signal, calling drop-target:reject if
required.
Passed: a <GtkDropTarget>.
Passed: a <GdkDrop>.
the <GdkDrop>
Returned: a boolean.
#t if drop is accepted
Connect a handler with (connect object drop-target:drop handler).
It is called as (handler self value x y) ⇒ %return.
Emitted on the drop site when the user drops the data onto the widget.
The signal handler must determine whether the pointer position is in a
drop zone or not. If it is not in a drop zone, it returns #f and
no further processing is necessary.
Otherwise, the handler returns #t. In this case, this handler
will accept the drop. The handler is responsible for using the given
value and performing the drop operation.
Passed: a <GtkDropTarget>.
Passed: a <GValue>.
the <GValue> being dropped
Passed: a real number.
the x coordinate of the current pointer position
Passed: a real number.
the y coordinate of the current pointer position
Returned: a boolean.
whether the drop was accepted at the given pointer position
Connect a handler with (connect object drop-target:enter
handler). It is called as (handler self x y) ⇒ %return.
Emitted on the drop site when the pointer enters the widget.
It can be used to set up custom highlighting.
Passed: a <GtkDropTarget>.
Passed: a real number.
the x coordinate of the current pointer position
Passed: a real number.
the y coordinate of the current pointer position
Returned: a <GdkDragAction>, or a list of
its symbols, or one of them.
Preferred action for this drag operation or
none if dropping is not supported at the
current x,y location.
Connect a handler with (connect object drop-target:leave
handler). It is called as (handler self).
Emitted on the drop site when the pointer leaves the widget.
Its main purpose it to undo things done in drop-target:enter.
Passed: a <GtkDropTarget>.
Connect a handler with (connect object drop-target:motion
handler). It is called as (handler self x y) ⇒ %return.
Emitted while the pointer is moving over the drop target.
Passed: a <GtkDropTarget>.
Passed: a real number.
the x coordinate of the current pointer position
Passed: a real number.
the y coordinate of the current pointer position
Returned: a <GdkDragAction>, or a list of
its symbols, or one of them.
Preferred action for this drag operation or
none if dropping is not supported at the
current x,y location.