DropTarget


Object Hierarchy:

Gtk.DropTarget Gtk.DropTarget Gtk.DropTarget Gtk.EventController Gtk.EventController Gtk.EventController->Gtk.DropTarget GLib.Object GLib.Object GLib.Object->Gtk.EventController

Description:

[ CCode ( type_id = "gtk_drop_target_get_type ()" ) ]
public sealed class DropTarget : 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:GtkDropTarget: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, (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 [signal@Gtk.DropTarget:GtkDropTarget: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: GtkDropTarget:enter], [signal@Gtk.DropTarget:GtkDropTarget:motion] and [signal@Gtk.DropTarget:GtkDropTarget: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].


Namespace: Gtk
Package: gtk4

Content:

Properties:

Creation methods:

Methods:

Signals:

Inherited Members: