DropTarget
open class DropTarget : EventController, DropTargetProtocol
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 [ctorGtk.DropTarget.new
], passing in the
GType
of the data you want to receive and connect to the
[signalGtk.DropTarget::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, (GTypes [2]) {
G_TYPE_FILE,
GDK_TYPE_PIXBUF,
}, 2);
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 [methodGtk.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
], [signalGtk.DropTarget::motion
] and [signalGtk.DropTarget::leave
] signals - configuring how to receive data by setting the
[property
Gtk.DropTarget:preload
] property and listening for its availability via the [propertyGtk.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 [classGtk.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_STATE_FLAG_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 [classGtk.DropControllerMotion
].
The DropTarget
type acts as a reference-counted owner of an underlying GtkDropTarget
instance.
It provides the methods that can operate on this data type through DropTargetProtocol
conformance.
Use DropTarget
as a strong reference or owner of a GtkDropTarget
instance.
-
Designated initialiser from the underlying `C` data type.
This creates an instance without performing an unbalanced retain i.e., ownership is transferred to the
DropTarget
instance.Declaration
Swift
@inlinable public init(_ op: UnsafeMutablePointer<GtkDropTarget>)
Parameters
op
pointer to the underlying object
-
Designated initialiser from a constant pointer to the underlying
C
data type. This creates an instance without performing an unbalanced retain i.e., ownership is transferred to theDropTarget
instance.Declaration
Swift
@inlinable public init(_ op: UnsafePointer<GtkDropTarget>)
Parameters
op
pointer to the underlying object
-
Optional initialiser from a non-mutating
gpointer
to the underlyingC
data type. This creates an instance without performing an unbalanced retain i.e., ownership is transferred to theDropTarget
instance.Declaration
Swift
@inlinable override public init!(gpointer op: gpointer?)
Parameters
op
gpointer to the underlying object
-
Optional initialiser from a non-mutating
gconstpointer
to the underlyingC
data type. This creates an instance without performing an unbalanced retain i.e., ownership is transferred to theDropTarget
instance.Declaration
Swift
@inlinable override public init!(gconstpointer op: gconstpointer?)
Parameters
op
pointer to the underlying object
-
Optional initialiser from a constant pointer to the underlying
C
data type. This creates an instance without performing an unbalanced retain i.e., ownership is transferred to theDropTarget
instance.Declaration
Swift
@inlinable public init!(_ op: UnsafePointer<GtkDropTarget>?)
Parameters
op
pointer to the underlying object
-
Optional initialiser from the underlying
C
data type. This creates an instance without performing an unbalanced retain i.e., ownership is transferred to theDropTarget
instance.Declaration
Swift
@inlinable public init!(_ op: UnsafeMutablePointer<GtkDropTarget>?)
Parameters
op
pointer to the underlying object
-
Designated initialiser from the underlying
C
data type. Will retainGtkDropTarget
. i.e., ownership is transferred to theDropTarget
instance.Declaration
Swift
@inlinable public init(retaining op: UnsafeMutablePointer<GtkDropTarget>)
Parameters
op
pointer to the underlying object
-
Reference intialiser for a related type that implements
DropTargetProtocol
Will retainGtkDropTarget
.Declaration
Swift
@inlinable public init<T>(dropTarget other: T) where T : DropTargetProtocol
Parameters
other
an instance of a related type that implements
DropTargetProtocol
-
Unsafe typed initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
DropTargetProtocol
.Declaration
Swift
@inlinable override public init<T>(cPointer p: UnsafeMutablePointer<T>)
Parameters
cPointer
pointer to the underlying object
-
Unsafe typed, retaining initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
DropTargetProtocol
.Declaration
Swift
@inlinable override public init<T>(retainingCPointer cPointer: UnsafeMutablePointer<T>)
Parameters
cPointer
pointer to the underlying object
-
Unsafe untyped initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
DropTargetProtocol
.Declaration
Swift
@inlinable override public init(raw p: UnsafeRawPointer)
Parameters
p
raw pointer to the underlying object
-
Unsafe untyped, retaining initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
DropTargetProtocol
.Declaration
Swift
@inlinable override public init(retainingRaw raw: UnsafeRawPointer)
-
Unsafe untyped initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
DropTargetProtocol
.Declaration
Swift
@inlinable public required init(raw p: UnsafeMutableRawPointer)
Parameters
p
mutable raw pointer to the underlying object
-
Unsafe untyped, retaining initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
DropTargetProtocol
.Declaration
Swift
@inlinable required public init(retainingRaw raw: UnsafeMutableRawPointer)
Parameters
raw
mutable raw pointer to the underlying object
-
Unsafe untyped initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
DropTargetProtocol
.Declaration
Swift
@inlinable override public init(opaquePointer p: OpaquePointer)
Parameters
p
opaque pointer to the underlying object
-
Unsafe untyped, retaining initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
DropTargetProtocol
.Declaration
Swift
@inlinable override public init(retainingOpaquePointer p: OpaquePointer)
Parameters
p
opaque pointer to the underlying object
-
Creates a new
GtkDropTarget
object.If the drop target should support more than 1 type, pass
G_TYPE_INVALID
fortype
and then call [methodGtk.DropTarget.set_gtypes
].Declaration
Swift
@inlinable public init(type: GType, actions: Gdk.DragAction)