Class DropTarget

All Implemented Interfaces:
PointerInterface

public class DropTarget extends EventController
`GtkDropTarget` is 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 [ctor@Gtk.DropTarget.new], passing in the
`GType` of the data you want to receive and connect to the
[signal@Gtk.DropTarget::drop] signal to receive the data:

```c
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, (GTypes [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:

* rejecting potential drops via the [signal@Gtk.DropTarget::accept] signal
and the [method@Gtk.DropTarget.reject] function to let other drop
targets handle the drop
* tracking an ongoing drag operation before the drop via the
[signal@Gtk.DropTarget::enter], [signal@Gtk.DropTarget::motion] and
[signal@Gtk.DropTarget::leave] signals
* configuring how to receive data by setting the
[property@Gtk.DropTarget:preload] property and listening for its
availability via the [property@Gtk.DropTarget: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 [class@Gtk.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_STATE_FLAG_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 [class@Gtk.DropControllerMotion].

https://docs.gtk.org/gtk4/class.DropTarget.html

  • Field Details

  • Constructor Details

    • DropTarget

      public DropTarget(PointerContainer pointer)
    • DropTarget

      public DropTarget(long type, int actions)
      Creates a new `GtkDropTarget` object.

      If the drop target should support more than 1 type, pass
      %G_TYPE_INVALID for @type and then call
      [method@Gtk.DropTarget.set_gtypes].
      Parameters:
      type - The supported type or %G_TYPE_INVALID
      actions - the supported actions
  • Method Details

    • getClassHandler

      public static ClassHandler getClassHandler()
    • getActions

      public int getActions()
      Gets the actions that this drop target supports.
      Returns:
      the actions that this drop target supports
    • getCurrentDrop

      public Drop getCurrentDrop()
      Gets the currently handled drop operation.

      If no drop operation is going on, %NULL is returned.
      Returns:
      The current drop
    • getFormats

      public ContentFormats getFormats()
      Gets the data formats that this drop target accepts.

      If the result is %NULL, all formats are expected to be supported.
      Returns:
      the supported data formats
    • getPreload

      public boolean getPreload()
      Gets whether data should be preloaded on hover.
      Returns:
      %TRUE if drop data should be preloaded
    • getValue

      public Value getValue()
      Gets the current drop data, as a `GValue`.
      Returns:
      The current drop data
    • reject

      public void reject()
      Rejects the ongoing drop operation.

      If no drop operation is ongoing, i.e when [property@Gtk.DropTarget:current-drop]
      is %NULL, 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.
    • setActions

      public void setActions(int actions)
      Sets the actions that this drop target supports.
      Parameters:
      actions - the supported actions
    • setGtypes

      public void setGtypes(@Nullable Int64 types, long n_types)
      Sets the supported `GTypes` for this drop target.
      Parameters:
      types - all supported `GType`s that can be dropped on the target
      n_types - number of @types
    • setPreload

      public void setPreload(boolean preload)
      Sets whether data should be preloaded on hover.
      Parameters:
      preload - %TRUE to preload drop data
    • onAccept

      public SignalHandler onAccept(DropTarget.OnAccept signal)
      Connect to signal "accept".
      See DropTarget.OnAccept.onAccept(ch.bailu.gtk.gdk.Drop) for signal description.
      Field SIGNAL_ON_ACCEPT contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onDrop

      public SignalHandler onDrop(DropTarget.OnDrop signal)
      Connect to signal "drop".
      See DropTarget.OnDrop.onDrop(ch.bailu.gtk.gobject.Value, double, double) for signal description.
      Field SIGNAL_ON_DROP contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onEnter

      public SignalHandler onEnter(DropTarget.OnEnter signal)
      Connect to signal "enter".
      See DropTarget.OnEnter.onEnter(double, double) for signal description.
      Field SIGNAL_ON_ENTER contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onLeave

      public SignalHandler onLeave(DropTarget.OnLeave signal)
      Connect to signal "leave".
      See DropTarget.OnLeave.onLeave() for signal description.
      Field SIGNAL_ON_LEAVE contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onMotion

      public SignalHandler onMotion(DropTarget.OnMotion signal)
      Connect to signal "motion".
      See DropTarget.OnMotion.onMotion(double, double) for signal description.
      Field SIGNAL_ON_MOTION contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • getTypeID

      public static long getTypeID()
    • getParentTypeID

      public static long getParentTypeID()
    • getTypeSize

      public static TypeSystem.TypeSize getTypeSize()
    • getParentTypeSize

      public static TypeSystem.TypeSize getParentTypeSize()
    • getInstanceSize

      public static int getInstanceSize()