DragSource

GtkDragSource is an event controller to initiate Drag-And-Drop operations.

GtkDragSource 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 [class@Gdk.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 [method@Gtk.Widget.add_controller].

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, GtkDragSource has [signal@Gtk.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 GdkDrag object has been created, and can be used to set up the drag icon.

static void
on_drag_begin (GtkDragSource *source,
GtkDrag       *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, GtkDragSource 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_ACTION_MOVE, you need to listen for the [signal@Gtk.DragSource::drag-end] signal and delete the data after it has been transferred.

Constructors

this
this(GtkDragSource* gtkDragSource, bool ownedRef)

Sets our main struct and passes it to the parent class.

this
this()

Creates a new GtkDragSource object.

Members

Functions

addOnDragBegin
gulong addOnDragBegin(void delegate(Drag, DragSource) dlg, ConnectFlags connectFlags)

Emitted on the drag source when a drag is started.

addOnDragCancel
gulong addOnDragCancel(bool delegate(Drag, GdkDragCancelReason, DragSource) dlg, ConnectFlags connectFlags)

Emitted on the drag source when a drag has failed.

addOnDragEnd
gulong addOnDragEnd(void delegate(Drag, bool, DragSource) dlg, ConnectFlags connectFlags)

Emitted on the drag source when a drag is finished.

addOnPrepare
gulong addOnPrepare(ContentProvider delegate(double, double, DragSource) dlg, ConnectFlags connectFlags)

Emitted when a drag is about to be initiated.

dragCancel
void dragCancel()

Cancels a currently ongoing drag operation.

getActions
GdkDragAction getActions()

Gets the actions that are currently set on the GtkDragSource.

getContent
ContentProvider getContent()

Gets the current content provider of a GtkDragSource.

getDrag
Drag getDrag()

Returns the underlying GdkDrag object for an ongoing drag.

getDragSourceStruct
GtkDragSource* getDragSourceStruct(bool transferOwnership)

Get the main Gtk struct

getStruct
void* getStruct()

the main Gtk struct as a void*

setActions
void setActions(GdkDragAction actions)

Sets the actions on the GtkDragSource.

setContent
void setContent(ContentProvider content)

Sets a content provider on a GtkDragSource.

setIcon
void setIcon(PaintableIF paintable, int hotX, int hotY)

Sets a paintable to use as icon during DND operations.

Static functions

getType
GType getType()

Variables

gtkDragSource
GtkDragSource* gtkDragSource;

the main Gtk struct

Inherited Members

From GestureSingle

gtkGestureSingle
GtkGestureSingle* gtkGestureSingle;

the main Gtk struct

getGestureSingleStruct
GtkGestureSingle* getGestureSingleStruct(bool transferOwnership)

Get the main Gtk struct

getStruct
void* getStruct()

the main Gtk struct as a void*

getType
GType getType()
getButton
uint getButton()

Returns the button number @gesture listens for.

getCurrentButton
uint getCurrentButton()

Returns the button number currently interacting with @gesture, or 0 if there is none.

getCurrentSequence
GdkEventSequence* getCurrentSequence()

Returns the event sequence currently interacting with @gesture.

getExclusive
bool getExclusive()

Gets whether a gesture is exclusive.

getTouchOnly
bool getTouchOnly()

Returns %TRUE if the gesture is only triggered by touch events.

setButton
void setButton(uint button)

Sets the button number @gesture listens to.

setExclusive
void setExclusive(bool exclusive)

Sets whether @gesture is exclusive.

setTouchOnly
void setTouchOnly(bool touchOnly)

Sets whether to handle only touch events.

Meta