Class DropTarget

All Implemented Interfaces:
Proxy

@Generated("org.javagi.JavaGI") public class DropTarget extends EventController

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 DropTarget(), passing in the GType of the data you want to receive and connect to the Gtk.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));
}

GtkDropTarget supports more options, such as:

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

  • Constructor Details

    • DropTarget

      public DropTarget(MemorySegment address)
      Create a DropTarget instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • DropTarget

      public DropTarget(Type type, Set<DragAction> 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 setGtypes(Type[]).

      Parameters:
      type - The supported type or G_TYPE_INVALID
      actions - the supported actions
    • DropTarget

      public DropTarget(Type type, DragAction... 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 setGtypes(Type[]).

      Parameters:
      type - The supported type or G_TYPE_INVALID
      actions - the supported actions
    • DropTarget

      public DropTarget()
      Create a new DropTarget.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the DropTarget class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected DropTarget asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class EventController
      Returns:
      the instance as if it were its parent type
    • getActions

      public Set<DragAction> getActions()
      Gets the actions that this drop target supports.
      Returns:
      the actions that this drop target supports
    • getCurrentDrop

      public @Nullable Drop getCurrentDrop()

      Gets the currently handled drop operation.

      If no drop operation is going on, null is returned.

      Returns:
      The current drop
      Since:
      4.4
    • getDrop

      @Deprecated public @Nullable Drop getDrop()
      Deprecated.
      Use getCurrentDrop() instead

      Gets the currently handled drop operation.

      If no drop operation is going on, null is returned.

      Returns:
      The current drop
    • getFormats

      public @Nullable 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
    • getGtypes

      public @Nullable Type @Nullable [] getGtypes()

      Gets the list of supported GTypes that can be dropped on the target.

      If no types have been set, NULL will be returned.

      Returns:
      the G_TYPE_INVALID-terminated array of types included in formats
    • getPreload

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

      public @Nullable 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 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(Set<DragAction> actions)
      Sets the actions that this drop target supports.
      Parameters:
      actions - the supported actions
    • setActions

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

      public void setGtypes(@Nullable Type @Nullable [] types)
      Sets the supported GTypes for this drop target.
      Parameters:
      types - all supported GTypes that can be dropped on the target
    • setPreload

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

      Emitted on the drop site when a drop operation is about to begin.

      If the drop is not accepted, false will be returned and the drop target will ignore the drop. If true is returned, the drop is accepted for now but may be rejected later via a call to reject() or ultimately by returning false from a Gtk.DropTarget::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 true, the Gtk.DropTarget:preload property should be set and the value should be inspected via the ::notify:value signal, calling reject() if required.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitAccept

      public boolean emitAccept(@Nullable Drop drop)
      Emits the "accept" signal. See onAccept(DropTarget.AcceptCallback).
    • onDrop

      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 false and no further processing is necessary.

      Otherwise, the handler returns true. In this case, this handler will accept the drop. The handler is responsible for using the given value and performing the drop operation.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitDrop

      public boolean emitDrop(@Nullable Value value, double x, double y)
      Emits the "drop" signal. See onDrop(DropTarget.DropCallback).
    • onEnter

      Emitted on the drop site when the pointer enters the widget.

      It can be used to set up custom highlighting.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitEnter

      public Set<DragAction> emitEnter(double x, double y)
      Emits the "enter" signal. See onEnter(DropTarget.EnterCallback).
    • onLeave

      Emitted on the drop site when the pointer leaves the widget.

      Its main purpose it to undo things done in Gtk.DropTarget::enter.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitLeave

      public void emitLeave()
      Emits the "leave" signal. See onLeave(DropTarget.LeaveCallback).
    • onMotion

      Emitted while the pointer is moving over the drop target.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitMotion

      public Set<DragAction> emitMotion(double x, double y)
      Emits the "motion" signal. See onMotion(DropTarget.MotionCallback).
    • builder

      public static DropTarget.Builder<? extends DropTarget.Builder> builder()
      A DropTarget.Builder object constructs a DropTarget with the specified properties. Use the various set...() methods to set properties, and finish construction with DropTarget.Builder.build().
      Returns:
      the builder object