gtk.drop_target

Module for [DropTarget] class

Types 3

[gtk.drop_target.DropTarget] is an event controller to receive Drag-and-Drop operations.

The most basic way to use a [gtk.drop_target.DropTarget] to receive drops on a widget is to create it via [gtk.drop_target.DropTarget.new_], passing in the GType of the data you want to receive and connect to the [gtk.drop_target.DropTarget.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));
}

[gtk.drop_target.DropTarget] supports more options, such as:

  • rejecting potential drops via the [gtk.drop_target.DropTarget.accept] signal

and the [gtk.drop_target.DropTarget.reject] function to let other drop targets handle the drop

  • tracking an ongoing drag operation before the drop via the

[gtk.drop_target.DropTarget.enter], [gtk.drop_target.DropTarget.motion] and [gtk.drop_target.DropTarget.leave] signals

  • configuring how to receive data by setting the

[gtk.drop_target.DropTarget.preload] property and listening for its availability via the [gtk.drop_target.DropTarget.value] property

However, [gtk.drop_target.DropTarget] is ultimately modeled in a synchronous way and only supports data transferred via GType. If you want full control over an ongoing drop, the [gtk.drop_target_async.DropTargetAsync] 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 [gtk.types.StateFlags.DropActive] 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 [gtk.drop_controller_motion.DropControllerMotion].

Methods
GType _gType() @property
DropTarget self()Returns `this`, for use in `with` statements.
DropTargetGidBuilder builder()Get builder for [gtk.drop_target.DropTarget] Returns: New builder object
gdk.types.DragAction actions() @propertyGet `actions` property. Returns: The `GdkDragActions` that this drop target supports.
void actions(gdk.types.DragAction propval) @propertySet `actions` property. Params: propval = The `GdkDragActions` that this drop target supports.
gdk.drop.Drop currentDrop() @propertyGet `currentDrop` property. Returns: The [gdk.drop.Drop] that is currently being performed.
gdk.content_formats.ContentFormats formats() @propertyGet `formats` property. Returns: The [gdk.content_formats.ContentFormats] that determine the supported data formats.
bool preload() @propertyGet `preload` property. Returns: Whether the drop data should be preloaded when the pointer is only hovering over the widget but has not been released.
void preload(bool propval) @propertySet `preload` property. Params: propval = Whether the drop data should be preloaded when the pointer is only hovering over the widget but has not been released.
gobject.value.Value value() @propertyGet `value` property. Returns: The value for this drop operation.
gdk.types.DragAction getActions()Gets the actions that this drop target supports. Returns: the actions that this drop target supports
gdk.drop.Drop getCurrentDrop()Gets the currently handled drop operation.
gdk.drop.Drop getDrop()Gets the currently handled drop operation.
gdk.content_formats.ContentFormats getFormats()Gets the data formats that this drop target accepts.
gobject.types.GType[] getGtypes()Gets the list of supported `GType`s that can be dropped on the target.
bool getPreload()Gets whether data should be preloaded on hover. Returns: true if drop data should be preloaded
gobject.value.Value getValue()Gets the current drop data, as a [gobject.value.Value]. Returns: The current drop data
void reject()Rejects the ongoing drop operation.
void setActions(gdk.types.DragAction actions)Sets the actions that this drop target supports.
void setGtypes(gobject.types.GType[] types = null)Sets the supported `GType`s for this drop target.
void setPreload(bool preload)Sets whether data should be preloaded on hover.
gulong connectAccept(T)(T callback, Flag!"After" after = No.After) if (isCallable!T && is(ReturnType!T == bool) && (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] : gdk.drop.Drop))) && (Parameters!T.length < 2 || (ParameterStorageClassTuple!T[1] == ParameterStorageClass.none && is(Parameters!T[1] : gtk.drop_target.DropTarget))) && Parameters!T.length < 3)Connect to `Accept` signal.
gulong connectDrop(T)(T callback, Flag!"After" after = No.After) if (isCallable!T && is(ReturnType!T == bool) && (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] == gobject.value.Value))) && (Parameters!T.length < 2 || (ParameterStorageClassTuple!T[1] == ParameterStorageClass.none && is(Parameters!T[1] == double))) && (Parameters!T.length < 3 || (ParameterStorageClassTuple!T[2] == ParameterStorageClass.none && is(Parameters!T[2] == double))) && (Parameters!T.length < 4 || (ParameterStorageClassTuple!T[3] == ParameterStorageClass.none && is(Parameters!T[3] : gtk.drop_target.DropTarget))) && Parameters!T.length < 5)Connect to `Drop` signal.
gulong connectEnter(T)(T callback, Flag!"After" after = No.After) if (isCallable!T && is(ReturnType!T == gdk.types.DragAction) && (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] == double))) && (Parameters!T.length < 2 || (ParameterStorageClassTuple!T[1] == ParameterStorageClass.none && is(Parameters!T[1] == double))) && (Parameters!T.length < 3 || (ParameterStorageClassTuple!T[2] == ParameterStorageClass.none && is(Parameters!T[2] : gtk.drop_target.DropTarget))) && Parameters!T.length < 4)Connect to `Enter` signal.
gulong connectLeave(T)(T callback, Flag!"After" after = No.After) if (isCallable!T && is(ReturnType!T == void) && (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] : gtk.drop_target.DropTarget))) && Parameters!T.length < 2)Connect to `Leave` signal.
gulong connectMotion(T)(T callback, Flag!"After" after = No.After) if (isCallable!T && is(ReturnType!T == gdk.types.DragAction) && (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] == double))) && (Parameters!T.length < 2 || (ParameterStorageClassTuple!T[1] == ParameterStorageClass.none && is(Parameters!T[1] == double))) && (Parameters!T.length < 3 || (ParameterStorageClassTuple!T[2] == ParameterStorageClass.none && is(Parameters!T[2] : gtk.drop_target.DropTarget))) && Parameters!T.length < 4)Connect to `Motion` signal.
Constructors
this(void * ptr, Flag!"Take" take)
this(gobject.types.GType type, gdk.types.DragAction actions)Creates a new [gtk.drop_target.DropTarget] object.
Methods
T actions(gdk.types.DragAction propval)Set `actions` property. Params: propval = The `GdkDragActions` that this drop target supports. Returns: Builder instance for fluent chaining
T formats(gdk.content_formats.ContentFormats propval)Set `formats` property. Params: propval = The [gdk.content_formats.ContentFormats] that determine the supported data formats. Returns: Builder instance for fluent chaining
T preload(bool propval)Set `preload` property. Params: propval = Whether the drop data should be preloaded when the pointer is only hovering over the widget but has not been released.

Fluent builder for [gtk.drop_target.DropTarget]

Methods