Gtk.DropTarget
Description: Reference for Gtk.DropTarget

### Class

# GtkDropTarget

since: 4.0

#### Description [src]

    final class Gtk.DropTarget : Gtk.EventController
    {
      /* No available fields */
    }

`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 `gtk_drop_target_new()`, passing in the `GType` of the data you want to receive and connect to the `GtkDropTarget::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, (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 `GtkDropTarget::accept` signal and the `gtk_drop_target_reject()` function to let other drop targets handle the drop
- tracking an ongoing drag operation before the drop via the `GtkDropTarget::enter`, `GtkDropTarget::motion` and `GtkDropTarget::leave` signals
- configuring how to receive data by setting the `GtkDropTarget:preload` property and listening for its availability via the `GtkDropTarget: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 `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 `GtkDropControllerMotion`.

Available since: 4.0

#### Ancestors

- GtkEventController
- GObject

#### Constructors

###### gtk_drop_target_new

Creates a new `GtkDropTarget` object.

since: 4.0

#### Instance methods

###### gtk_drop_target_get_actions

Gets the actions that this drop target supports.

since: 4.0

###### gtk_drop_target_get_current_drop

Gets the currently handled drop operation.

since: 4.4

###### gtk_drop_target_get_drop

Gets the currently handled drop operation.

deprecated: 4.4 since: 4.0

###### gtk_drop_target_get_formats

Gets the data formats that this drop target accepts.

since: 4.0

###### gtk_drop_target_get_gtypes

Gets the list of supported `GType`s that can be dropped on the target.

since: 4.0

###### gtk_drop_target_get_preload

Gets whether data should be preloaded on hover.

since: 4.0

###### gtk_drop_target_get_value

Gets the current drop data, as a `GValue`.

since: 4.0

###### gtk_drop_target_reject

Rejects the ongoing drop operation.

since: 4.0

###### gtk_drop_target_set_actions

Sets the actions that this drop target supports.

since: 4.0

###### gtk_drop_target_set_gtypes

Sets the supported `GTypes` for this drop target.

since: 4.0

###### gtk_drop_target_set_preload

Sets whether data should be preloaded on hover.

since: 4.0

##### Methods inherited from GtkEventController (13)

###### gtk_event_controller_get_current_event

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

since: 4.0

###### gtk_event_controller_get_current_event_device

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

since: 4.0

###### gtk_event_controller_get_current_event_state

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

since: 4.0

###### gtk_event_controller_get_current_event_time

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

since: 4.0

###### gtk_event_controller_get_name

Gets the name of `controller`.

since: 4.0

###### gtk_event_controller_get_propagation_limit

Gets the propagation limit of the event controller.

since: 4.0

###### gtk_event_controller_get_propagation_phase

Gets the propagation phase at which `controller` handles events.

since: 4.0

###### gtk_event_controller_get_widget

Returns the `GtkWidget` this controller relates to.

since: 4.0

###### gtk_event_controller_reset

Resets the `controller` to a clean state.

since: 4.0

###### gtk_event_controller_set_name

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

since: 4.0

###### gtk_event_controller_set_propagation_limit

Sets the event propagation limit on the event controller.

since: 4.0

###### gtk_event_controller_set_propagation_phase

Sets the propagation phase at which a controller handles events.

since: 4.0

###### gtk_event_controller_set_static_name

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

since: 4.8

##### Methods inherited from GObject (43)

Please see GObject for a full list of methods.

#### Properties

###### Gtk.DropTarget:actions

The `GdkDragActions` that this drop target supports.

since: 4.0

###### Gtk.DropTarget:current-drop

The `GdkDrop` that is currently being performed.

since: 4.4

###### Gtk.DropTarget:drop

The `GdkDrop` that is currently being performed.

deprecated: 4.4 since: 4.0

###### Gtk.DropTarget:formats

The `GdkContentFormats` that determine the supported data formats.

since: 4.0

###### Gtk.DropTarget:preload

Whether the drop data should be preloaded when the pointer is only hovering over the widget but has not been released.

since: 4.0

###### Gtk.DropTarget:value

The value for this drop operation.

since: 4.0

##### Properties inherited from GtkEventController (4)

###### Gtk.EventController:name

The name for this controller, typically used for debugging purposes.

since: 4.0

###### Gtk.EventController:propagation-limit

The limit for which events this controller will handle.

since: 4.0

###### Gtk.EventController:propagation-phase

The propagation phase at which this controller will handle events.

since: 4.0

###### Gtk.EventController:widget

The widget receiving the `GdkEvents` that the controller will handle.

since: 4.0

#### Signals

###### Gtk.DropTarget::accept

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

since: 4.0

###### Gtk.DropTarget::drop

Emitted on the drop site when the user drops the data onto the widget.

since: 4.0

###### Gtk.DropTarget::enter

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

since: 4.0

###### Gtk.DropTarget::leave

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

since: 4.0

###### Gtk.DropTarget::motion

Emitted while the pointer is moving over the drop target.

since: 4.0

##### Signals inherited from GObject (1)

###### GObject::notify

The notify signal is emitted on an object when one of its properties has its value set through g_object_set_property(), g_object_set(), et al.

since: 2.0

#### Class structure

    struct GtkDropTargetClass {
      /* no available fields */
    }

No description available.