1.47 <GtkDragSource>

Class: <GtkDragSource>

It derives from <GtkGestureSingle>.

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 <GdkContentProvider>, 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 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 drag-source:prepare and drag-source: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,
               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, <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 move, you need to listen for the drag-source:drag-end signal and delete the data after it has been transferred.

Properties

Property: drag-source:actions self

The actions that are supported by drag operations from the source.

Note that you must handle the drag-source:drag-end signal if the actions include move.

Its value is a <GdkDragAction> (or, to set it, a list of its symbols, or one of them).

Set it with (set! (drag-source:actions self) value).

Property: drag-source:content self

The data that is offered by drag operations from this source.

Its value is a <GdkContentProvider>, or #f.

Set it with (set! (drag-source:content self) value).

Procedures

Method: drag-source:drag-cancel self
Method: drag-cancel self

Cancels a currently ongoing drag operation.

self

Passed: a <GtkDragSource>.

a <GtkDragSource>

Method: drag-source:get-actions self ⇒ %return
Method: get-actions self ⇒ %return

Gets the actions that are currently set on the <GtkDragSource>.

self

Passed: a <GtkDragSource>.

a <GtkDragSource>

%return

Returned: a <GdkDragAction>.

the actions set on self

Method: drag-source:get-content self ⇒ %return
Method: get-content self ⇒ %return

Gets the current content provider of a <GtkDragSource>.

self

Passed: a <GtkDragSource>.

a <GtkDragSource>

%return

Returned: a <GdkContentProvider>, or #f.

the <GdkContentProvider> of self

Method: drag-source:get-drag self ⇒ %return
Method: get-drag self ⇒ %return

Returns the underlying <GdkDrag> object for an ongoing drag.

self

Passed: a <GtkDragSource>.

a <GtkDragSource>

%return

Returned: a <GdkDrag>, or #f.

the <GdkDrag> of the current drag operation

Procedure: drag-source:new ⇒ %return

Creates a new <GtkDragSource> object.

%return

Returned: a <GtkDragSource>.

the new <GtkDragSource>

Method: drag-source:set-actions self actions
Method: set-actions self actions

Sets the actions on the <GtkDragSource>.

During a DND operation, the actions are offered to potential drop targets. If actions include move, you need to listen to the drag-source:drag-end signal and handle delete-data being #t.

This function can be called before a drag is started, or in a handler for the drag-source:prepare signal.

self

Passed: a <GtkDragSource>.

a <GtkDragSource>

actions

Passed: a <GdkDragAction>, or a list of its symbols, or one of them.

the actions to offer

Method: drag-source:set-content self [content]
Method: set-content self [content]

Sets a content provider on a <GtkDragSource>.

When the data is requested in the cause of a DND operation, it will be obtained from the content provider.

This function can be called before a drag is started, or in a handler for the drag-source:prepare signal.

You may consider setting the content provider back to #f in a drag-source:drag-end signal handler.

self

Passed: a <GtkDragSource>.

a <GtkDragSource>

content

Passed, or left out: a <GdkContentProvider>, or #f.

a <GdkContentProvider>

Method: drag-source:set-icon self paintable hot-x hot-y
Method: set-icon self paintable hot-x hot-y

Sets a paintable to use as icon during DND operations.

The hotspot coordinates determine the point on the icon that gets aligned with the hotspot of the cursor.

If paintable is #f, a default icon is used.

This function can be called before a drag is started, or in a drag-source:prepare or drag-source:drag-begin signal handler.

self

Passed: a <GtkDragSource>.

a <GtkDragSource>

paintable

Passed: a <GdkPaintable>, or #f.

the <GdkPaintable> to use as icon

hot-x

Passed: a 32-bit integer.

the hotspot X coordinate on the icon

hot-y

Passed: a 32-bit integer.

the hotspot Y coordinate on the icon

Signals

Signal: drag-source:drag-begin

Connect a handler with (connect object drag-source:drag-begin handler). It is called as (handler self drag).

Emitted on the drag source when a drag is started.

It can be used to e.g. set a custom drag icon with drag-source:set-icon.

self

Passed: a <GtkDragSource>.

drag

Passed: a <GdkDrag>.

the <GdkDrag> object

Signal: drag-source:drag-cancel

Connect a handler with (connect object drag-source:drag-cancel handler). It is called as (handler self drag reason) ⇒ %return.

Emitted on the drag source when a drag has failed.

The signal handler may handle a failed drag operation based on the type of error. It should return #t if the failure has been handled and the default "drag operation failed" animation should not be shown.

self

Passed: a <GtkDragSource>.

drag

Passed: a <GdkDrag>.

the <GdkDrag> object

reason

Passed: a <GdkDragCancelReason>.

information on why the drag failed

%return

Returned: a boolean.

#t if the failed drag operation has been already handled

Signal: drag-source:drag-end

Connect a handler with (connect object drag-source:drag-end handler). It is called as (handler self drag delete-data).

Emitted on the drag source when a drag is finished.

A typical reason to connect to this signal is to undo things done in drag-source:prepare or drag-source:drag-begin handlers.

self

Passed: a <GtkDragSource>.

drag

Passed: a <GdkDrag>.

the <GdkDrag> object

delete-data

Passed: a boolean.

#t if the drag was performing move, and the data should be deleted

Signal: drag-source:prepare

Connect a handler with (connect object drag-source:prepare handler). It is called as (handler self x y) ⇒ %return.

Emitted when a drag is about to be initiated.

It returns the <GdkContentProvider> to use for the drag that is about to start. The default handler for this signal returns the value of the drag-source:content property, so if you set up that property ahead of time, you don’t need to connect to this signal.

self

Passed: a <GtkDragSource>.

x

Passed: a real number.

the X coordinate of the drag starting point

y

Passed: a real number.

the Y coordinate of the drag starting point

%return

Returned: a <GdkContentProvider>, or #f.

a <GdkContentProvider>