DropTarget

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 gobject.types.TYPE_FLAG_RESERVED_ID_BIT 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:

However, gtk.drop_target.DropTarget is ultimately modeled in a synchronous way and only supports data transferred via gobject.types.TYPE_FLAG_RESERVED_ID_BIT. 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.

Constructors

this
this(gobject.types.GType type, gdk.types.DragAction actions)

Creates a new gtk.drop_target.DropTarget object.

Members

Functions

connectAccept
ulong connectAccept(T callback, Flag!"After" after)

Connect to Accept signal.

connectDrop
ulong connectDrop(T callback, Flag!"After" after)

Connect to Drop signal.

connectEnter
ulong connectEnter(T callback, Flag!"After" after)

Connect to Enter signal.

connectLeave
ulong connectLeave(T callback, Flag!"After" after)

Connect to Leave signal.

connectMotion
ulong connectMotion(T callback, Flag!"After" after)

Connect to Motion signal.

getActions
gdk.types.DragAction getActions()

Gets the actions that this drop target supports.

getCurrentDrop
gdk.drop.Drop getCurrentDrop()

Gets the currently handled drop operation.

getDrop
gdk.drop.Drop getDrop()

Gets the currently handled drop operation.

getFormats
gdk.content_formats.ContentFormats getFormats()

Gets the data formats that this drop target accepts.

getGtypes
gobject.types.GType[] getGtypes()

Gets the list of supported gobject.types.TYPE_FLAG_RESERVED_ID_BITs that can be dropped on the target.

getPreload
bool getPreload()

Gets whether data should be preloaded on hover.

getValue
gobject.value.Value getValue()

Gets the current drop data, as a gobject.value.Value.

reject
void reject()

Rejects the ongoing drop operation.

setActions
void setActions(gdk.types.DragAction actions)

Sets the actions that this drop target supports.

setGtypes
void setGtypes(gobject.types.GType[] types)

Sets the supported gobject.types.TYPE_FLAG_RESERVED_ID_BITs for this drop target.

setPreload
void setPreload(bool preload)

Sets whether data should be preloaded on hover.

Inherited Members

From EventController

getCurrentEvent
gdk.event.Event getCurrentEvent()

Returns the event that is currently being handled by the controller.

getCurrentEventDevice
gdk.device.Device getCurrentEventDevice()

Returns the device of the event that is currently being handled by the controller.

getCurrentEventState
gdk.types.ModifierType getCurrentEventState()

Returns the modifier state of the event that is currently being handled by the controller.

getCurrentEventTime
uint getCurrentEventTime()

Returns the timestamp of the event that is currently being handled by the controller.

getName
string getName()

Gets the name of controller.

getPropagationLimit
gtk.types.PropagationLimit getPropagationLimit()

Gets the propagation limit of the event controller.

getPropagationPhase
gtk.types.PropagationPhase getPropagationPhase()

Gets the propagation phase at which controller handles events.

getWidget
gtk.widget.Widget getWidget()

Returns the gtk.widget.Widget this controller relates to.

reset
void reset()

Resets the controller to a clean state.

setName
void setName(string name)

Sets a name on the controller that can be used for debugging.

setPropagationLimit
void setPropagationLimit(gtk.types.PropagationLimit limit)

Sets the event propagation limit on the event controller.

setPropagationPhase
void setPropagationPhase(gtk.types.PropagationPhase phase)

Sets the propagation phase at which a controller handles events.

setStaticName
void setStaticName(string name)

Sets a name on the controller that can be used for debugging.