gtk.drag_source
Module for [DragSource] class
Types 3
[gtk.drag_source.DragSource] is an event controller to initiate Drag-And-Drop operations.
[gtk.drag_source.DragSource] can be set up with the necessary ingredients for a DND operation ahead of time. This includes the source for the data that is being transferred, in the form of a [gdk.content_provider.ContentProvider], the desired action, and the icon to use during the drag operation. After setting it up, the drag source must be added to a widget as an event controller, using [gtk.widget.Widget.addController].
static void
my_widget_init (MyWidget *self)
{
GtkDragSource *drag_source = gtk_drag_source_new ();
g_signal_connect (drag_source, "prepare", G_CALLBACK (on_drag_prepare), self);
g_signal_connect (drag_source, "drag-begin", G_CALLBACK (on_drag_begin), self);
gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (drag_source));
}Setting up the content provider and icon ahead of time only makes sense when the data does not change. More commonly, you will want to set them up just in time. To do so, [gtk.drag_source.DragSource] has [gtk.drag_source.DragSource.prepare] and signal@Gtk.DragSource::drag-begin signals.
The ::prepare signal is emitted before a drag is started, and can be used to set the content provider and actions that the drag should be started with.
static GdkContentProvider *
on_drag_prepare (GtkDragSource *source,
double x,
double y,
MyWidget *self)
{
// This widget supports two types of content: GFile objects
// and GdkPixbuf objects; GTK will handle the serialization
// of these types automatically
GFile *file = my_widget_get_file (self);
GdkPixbuf *pixbuf = my_widget_get_pixbuf (self);
return gdk_content_provider_new_union ((GdkContentProvider *[2]) {
gdk_content_provider_new_typed (G_TYPE_FILE, file),
gdk_content_provider_new_typed (GDK_TYPE_PIXBUF, pixbuf),
}, 2);
}The ::drag-begin signal is emitted after the [gdk.drag.Drag] object has been created, and can be used to set up the drag icon.
static void
on_drag_begin (GtkDragSource *source,
GdkDrag *drag,
MyWidget *self)
{
// Set the widget as the drag icon
GdkPaintable *paintable = gtk_widget_paintable_new (GTK_WIDGET (self));
gtk_drag_source_set_icon (source, paintable, 0, 0);
g_object_unref (paintable);
}During the DND operation, [gtk.drag_source.DragSource] emits signals that can be used to obtain updates about the status of the operation, but it is not normally necessary to connect to any signals, except for one case: when the supported actions include [gdk.types.DragAction.Move], you need to listen for the signal@Gtk.DragSource::drag-end signal and delete the data after it has been transferred.
DragSource self()Returns `this`, for use in `with` statements.DragSourceGidBuilder builder()Get builder for [gtk.drag_source.DragSource] Returns: New builder objectgdk.types.DragAction actions() @propertyGet `actions` property. Returns: The actions that are supported by drag operations from the source.void actions(gdk.types.DragAction propval) @propertySet `actions` property. Params: propval = The actions that are supported by drag operations from the source.gdk.content_provider.ContentProvider content() @propertyGet `content` property. Returns: The data that is offered by drag operations from this source.void content(gdk.content_provider.ContentProvider propval) @propertySet `content` property. Params: propval = The data that is offered by drag operations from this source.void dragCancel()Cancels a currently ongoing drag operation.gdk.types.DragAction getActions()Gets the actions that are currently set on the [gtk.drag_source.DragSource]. Returns: the actions set on sourcegdk.content_provider.ContentProvider getContent()Gets the current content provider of a [gtk.dragsource.DragSource]. Returns: the [gdk.contentprovider.ContentProvider] of sourcegdk.drag.Drag getDrag()Returns the underlying [gdk.drag.Drag] object for an ongoing drag. Returns: the [gdk.drag.Drag] of the current drag operationvoid setActions(gdk.types.DragAction actions)Sets the actions on the [gtk.drag_source.DragSource].void setContent(gdk.content_provider.ContentProvider content = null)Sets a content provider on a [gtk.drag_source.DragSource].void setIcon(gdk.paintable.Paintable paintable, int hotX, int hotY)Sets a paintable to use as icon during DND operations.gulong connectDragBegin(T)(T callback, Flag!"After" after = No.After) if (isCallable!T
&& is(ReturnType!T == void)
&& (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] : gdk.drag.Drag)))
&& (Parameters!T.length < 2 || (ParameterStorageClassTuple!T[1] == ParameterStorageClass.none && is(Parameters!T[1] : gtk.drag_source.DragSource)))
&& Parameters!T.length < 3)Connect to `DragBegin` signal.gulong connectDragCancel(T)(T callback, Flag!"After" after = No.After) if (isCallable!T
&& is(ReturnType!T == bool)
&& (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] : gdk.drag.Drag)))
&& (Parameters!T.length < 2 || (ParameterStorageClassTuple!T[1] == ParameterStorageClass.none && is(Parameters!T[1] == gdk.types.DragCancelReason)))
&& (Parameters!T.length < 3 || (ParameterStorageClassTuple!T[2] == ParameterStorageClass.none && is(Parameters!T[2] : gtk.drag_source.DragSource)))
&& Parameters!T.length < 4)Connect to `DragCancel` signal.gulong connectDragEnd(T)(T callback, Flag!"After" after = No.After) if (isCallable!T
&& is(ReturnType!T == void)
&& (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] : gdk.drag.Drag)))
&& (Parameters!T.length < 2 || (ParameterStorageClassTuple!T[1] == ParameterStorageClass.none && is(Parameters!T[1] == bool)))
&& (Parameters!T.length < 3 || (ParameterStorageClassTuple!T[2] == ParameterStorageClass.none && is(Parameters!T[2] : gtk.drag_source.DragSource)))
&& Parameters!T.length < 4)Connect to `DragEnd` signal.gulong connectPrepare(T)(T callback, Flag!"After" after = No.After) if (isCallable!T
&& is(ReturnType!T : gdk.content_provider.ContentProvider)
&& (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] == double)))
&& (Parameters!T.length < 2 || (ParameterStorageClassTuple!T[1] == ParameterStorageClass.none && is(Parameters!T[1] == double)))
&& (Parameters!T.length < 3 || (ParameterStorageClassTuple!T[2] == ParameterStorageClass.none && is(Parameters!T[2] : gtk.drag_source.DragSource)))
&& Parameters!T.length < 4)Connect to `Prepare` signal.T actions(gdk.types.DragAction propval)Set `actions` property. Params: propval = The actions that are supported by drag operations from the source.T content(gdk.content_provider.ContentProvider propval)Set `content` property. Params: propval = The data that is offered by drag operations from this source. Returns: Builder instance for fluent chainingFluent builder for [gtk.drag_source.DragSource]