Gtk.DropTarget

g GObject.Object GObject.Object Gtk.EventController Gtk.EventController GObject.Object->Gtk.EventController Gtk.DropTarget Gtk.DropTarget Gtk.EventController->Gtk.DropTarget

Subclasses:

None

Methods

Inherited:

Gtk.EventController (13), GObject.Object (37)

Structs:

GObject.ObjectClass (5)

class

new (type, actions)

get_actions ()

get_current_drop ()

get_drop ()

get_formats ()

get_gtypes ()

get_preload ()

get_value ()

reject ()

set_actions (actions)

set_gtypes (types)

set_preload (preload)

Virtual Methods

Inherited:

GObject.Object (7)

Properties

Inherited:

Gtk.EventController (4)

Name

Type

Flags

Short Description

actions

Gdk.DragAction

r/w/en

current-drop

Gdk.Drop

r

formats

Gdk.ContentFormats

r/w/co

preload

bool

r/w/en

value

GObject.Value

r

Signals

Inherited:

GObject.Object (1)

Name

Short Description

accept

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

drop

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

enter

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

leave

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

motion

Emitted while the pointer is moving over the drop target.

Fields

Inherited:

GObject.Object (1)

Class Details

class Gtk.DropTarget(**kwargs)
Bases:

Gtk.EventController

Abstract:

No

Structure:

Gtk.DropTargetClass

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 bool on_drop (Gtk.DropTarget *target, const GObject.Value *value, double x, double y, object 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, GObject.Value.get_object (value)); else if (G_VALUE_HOLDS (value, GDK_TYPE_PIXBUF)) my_widget_set_pixbuf (self, GObject.Value.get_object (value)); else return False;

return True; }

static void my_widget_init (MyWidget *self) { Gtk.DropTarget *target = Gtk.DropTarget.new (GObject.TYPE_INVALID, Gdk.DragAction.COPY);

// This widget accepts two types of drop types: Gio.File objects // and GdkPixbuf.Pixbuf objects Gtk.DropTarget.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.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 [class`Gtk`.DropControllerMotion].

classmethod new(type, actions)[source]
Parameters:
Returns:

the new GtkDropTarget

Return type:

Gtk.DropTarget

Creates a new GtkDropTarget object.

If the drop target should support more than 1 type, pass GObject.TYPE_INVALID for type and then call [method`Gtk`.DropTarget.set_gtypes].

get_actions()[source]
Returns:

the actions that this drop target supports

Return type:

Gdk.DragAction

Gets the actions that this drop target supports.

get_current_drop()[source]
Returns:

The current drop

Return type:

Gdk.Drop or None

Gets the currently handled drop operation.

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

New in version 4.4.

get_drop()[source]
Returns:

The current drop

Return type:

Gdk.Drop or None

Gets the currently handled drop operation.

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

Deprecated since version 4.4: Use [method`Gtk`.DropTarget.get_current_drop] instead

get_formats()[source]
Returns:

the supported data formats

Return type:

Gdk.ContentFormats or None

Gets the data formats that this drop target accepts.

If the result is None, all formats are expected to be supported.

get_gtypes()[source]
Returns:

the G_TYPE_INVALID-terminated array of types included in formats

Return type:

[GObject.GType] or None

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

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

get_preload()[source]
Returns:

True if drop data should be preloaded

Return type:

bool

Gets whether data should be preloaded on hover.

get_value()[source]
Returns:

The current drop data

Return type:

GObject.Value or None

Gets the current drop data, as a GValue.

reject()[source]

Rejects the ongoing drop operation.

If no drop operation is ongoing, i.e when [property`Gtk`.DropTarget:current-drop] is None, 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.

set_actions(actions)[source]
Parameters:

actions (Gdk.DragAction) – the supported actions

Sets the actions that this drop target supports.

set_gtypes(types)[source]
Parameters:

types ([GObject.GType] or None) – all supported ``GType``s that can be dropped on the target

Sets the supported GTypes for this drop target.

set_preload(preload)[source]
Parameters:

preload (bool) – True to preload drop data

Sets whether data should be preloaded on hover.

Signal Details

Gtk.DropTarget.signals.accept(drop_target, drop)
Signal Name:

accept

Flags:

RUN_LAST

Parameters:
Returns:

True if drop is accepted

Return type:

bool

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 [method`Gtk`.DropTarget.reject] or ultimately by returning False from a [signal`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 [property`Gtk`.DropTarget:preload] property should be set and the value should be inspected via the ::notify:value signal, calling [method`Gtk`.DropTarget.reject] if required.

Gtk.DropTarget.signals.drop(drop_target, value, x, y)
Signal Name:

drop

Flags:

RUN_LAST

Parameters:
  • drop_target (Gtk.DropTarget) – The object which received the signal

  • value (GObject.Value) – the GValue being dropped

  • x (float) – the x coordinate of the current pointer position

  • y (float) – the y coordinate of the current pointer position

Returns:

whether the drop was accepted at the given pointer position

Return type:

bool

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.

Gtk.DropTarget.signals.enter(drop_target, x, y)
Signal Name:

enter

Flags:

RUN_LAST

Parameters:
  • drop_target (Gtk.DropTarget) – The object which received the signal

  • x (float) – the x coordinate of the current pointer position

  • y (float) – the y coordinate of the current pointer position

Returns:

Preferred action for this drag operation or 0 if dropping is not supported at the current x,`y` location.

Return type:

Gdk.DragAction

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

It can be used to set up custom highlighting.

Gtk.DropTarget.signals.leave(drop_target)
Signal Name:

leave

Flags:

RUN_LAST

Parameters:

drop_target (Gtk.DropTarget) – The object which received the signal

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

Its main purpose it to undo things done in [signal`Gtk`.DropTarget::enter].

Gtk.DropTarget.signals.motion(drop_target, x, y)
Signal Name:

motion

Flags:

RUN_LAST

Parameters:
  • drop_target (Gtk.DropTarget) – The object which received the signal

  • x (float) – the x coordinate of the current pointer position

  • y (float) – the y coordinate of the current pointer position

Returns:

Preferred action for this drag operation or 0 if dropping is not supported at the current x,`y` location.

Return type:

Gdk.DragAction

Emitted while the pointer is moving over the drop target.

Property Details

Gtk.DropTarget.props.actions
Name:

actions

Type:

Gdk.DragAction

Default Value:

0

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

The GdkDragActions that this drop target supports.

Gtk.DropTarget.props.current_drop
Name:

current-drop

Type:

Gdk.Drop

Default Value:

None

Flags:

READABLE

The GdkDrop that is currently being performed.

New in version 4.4.

Gtk.DropTarget.props.formats
Name:

formats

Type:

Gdk.ContentFormats

Default Value:

None

Flags:

READABLE, WRITABLE, CONSTRUCT_ONLY

The GdkContentFormats that determine the supported data formats.

Gtk.DropTarget.props.preload
Name:

preload

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

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

Setting this property allows finer grained reaction to an ongoing drop at the cost of loading more data.

The default value for this property is False to avoid downloading huge amounts of data by accident.

For example, if somebody drags a full document of gigabytes of text from a text editor across a widget with a preloading drop target, this data will be downloaded, even if the data is ultimately dropped elsewhere.

For a lot of data formats, the amount of data is very small (like %GDK_TYPE_RGBA), so enabling this property does not hurt at all. And for local-only Drag-and-Drop operations, no data transfer is done, so enabling it there is free.

Gtk.DropTarget.props.value
Name:

value

Type:

GObject.Value

Default Value:

None

Flags:

READABLE

The value for this drop operation.

This is None if the data has not been loaded yet or no drop operation is going on.

Data may be available before the [signal`Gtk`.DropTarget::drop] signal gets emitted - for example when the [property`Gtk`.DropTarget:preload] property is set. You can use the ::notify signal to be notified of available data.