DropTarget#
Superclasses: EventController, Object
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 new, passing in the
GType of the data you want to receive and connect to the
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:
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
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.
Constructors#
- class DropTarget
- classmethod new(type: GType, actions: DragAction) DropTarget#
Creates a new
GtkDropTargetobject.If the drop target should support more than 1 type, pass
Invalidfortypeand then callset_gtypes.- Parameters:
type – The supported type or
Invalidactions – the supported actions
Methods#
- class DropTarget
- get_actions() DragAction#
Gets the actions that this drop target supports.
- get_current_drop() Drop | None#
Gets the currently handled drop operation.
If no drop operation is going on,
Noneis returned.Added in version 4.4.
- get_drop() Drop | None#
Gets the currently handled drop operation.
If no drop operation is going on,
Noneis returned.Deprecated since version 4.4: Use
get_current_dropinstead
- get_formats() ContentFormats | None#
Gets the data formats that this drop target accepts.
If the result is
None, all formats are expected to be supported.
- get_gtypes() list[GType] | None#
Gets the list of supported
GTypes that can be dropped on the target.If no types have been set,
NULLwill be returned.
- reject() None#
Rejects the ongoing drop operation.
If no drop operation is ongoing, i.e when
current_dropisNone, 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: DragAction) None#
Sets the actions that this drop target supports.
- Parameters:
actions – the supported actions
Properties#
- class DropTarget
- props.actions: DragAction#
The
GdkDragActionsthat this drop target supports.
- props.drop: Drop#
The
GdkDropthat is currently being performed.Deprecated since version 4.4: Use
current_dropinstead
- props.formats: ContentFormats#
The
GdkContentFormatsthat determine the supported data formats.
- props.preload: bool#
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
Falseto 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.
- props.value: Value#
The value for this drop operation.
This is
Noneif the data has not been loaded yet or no drop operation is going on.Data may be available before the
dropsignal gets emitted - for example when thepreloadproperty is set. You can use the ::notify signal to be notified of available data.
Signals#
- class DropTarget.signals
- accept(drop: Drop) bool#
Emitted on the drop site when a drop operation is about to begin.
If the drop is not accepted,
Falsewill be returned and the drop target will ignore the drop. IfTrueis returned, the drop is accepted for now but may be rejected later via a call torejector ultimately by returningFalsefrom adrophandler.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, thepreloadproperty should be set and the value should be inspected via the ::notify:value signal, callingrejectif required.- Parameters:
drop – the
GdkDrop
- drop(value: Value, x: float, y: float) 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
Falseand 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 givenvalueand performing the drop operation.- Parameters:
value – the
GValuebeing droppedx – the x coordinate of the current pointer position
y – the y coordinate of the current pointer position
- enter(x: float, y: float) DragAction#
Emitted on the drop site when the pointer enters the widget.
It can be used to set up custom highlighting.
- Parameters:
x – the x coordinate of the current pointer position
y – the y coordinate of the current pointer position
- leave() None#
Emitted on the drop site when the pointer leaves the widget.
Its main purpose it to undo things done in
enter.
- motion(x: float, y: float) DragAction#
Emitted while the pointer is moving over the drop target.
- Parameters:
x – the x coordinate of the current pointer position
y – the y coordinate of the current pointer position