Class DropTarget
- All Implemented Interfaces:
Proxy
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 DropTarget(org.gnome.glib.Type, org.gnome.gdk.DragAction...)
, passing in the
GType
of the data you want to receive and connect to the
Gtk.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, (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:
- rejecting potential drops via the
Gtk.DropTarget::accept
signal and thereject()
function to let other drop targets handle the drop - tracking an ongoing drag operation before the drop via the
Gtk.DropTarget::enter
,Gtk.DropTarget::motion
andGtk.DropTarget::leave
signals - configuring how to receive data by setting the
Gtk.DropTarget:preload
property and listening for its availability via theGtk.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 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
StateFlags.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
.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Functional interface declaration of theAcceptCallback
callback.static class
DropTarget.Builder<B extends DropTarget.Builder<B>>
Inner class implementing a builder pattern to construct a GObject with properties.static interface
Functional interface declaration of theDropCallback
callback.static class
static interface
Functional interface declaration of theEnterCallback
callback.static interface
Functional interface declaration of theLeaveCallback
callback.static interface
Functional interface declaration of theMotionCallback
callback.Nested classes/interfaces inherited from class org.gnome.gtk.EventController
EventController.EventControllerClass, EventController.EventControllerImpl
Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass
-
Constructor Summary
ConstructorDescriptionDropTarget
(MemorySegment address) Create a DropTarget proxy instance for the provided memory address.DropTarget
(Type type, Set<DragAction> actions) Creates a newGtkDropTarget
object.DropTarget
(Type type, DragAction... actions) Creates a newGtkDropTarget
object. -
Method Summary
Modifier and TypeMethodDescriptionprotected DropTarget
asParent()
Returns this instance as if it were its parent type.static DropTarget.Builder
<? extends DropTarget.Builder> builder()
ADropTarget.Builder
object constructs aDropTarget
with the specified properties.boolean
emitAccept
(Drop drop) Emits the "accept" signal.boolean
Emits the "drop" signal.emitEnter
(double x, double y) Emits the "enter" signal.void
Emits the "leave" signal.emitMotion
(double x, double y) Emits the "motion" signal.Gets the actions that this drop target supports.Gets the currently handled drop operation.getDrop()
Deprecated.Gets the data formats that this drop target accepts.Type[]
Gets the list of supportedGType
s that can be dropped on the target.boolean
Gets whether data should be preloaded on hover.static Type
getType()
Get the GType of the DropTarget classgetValue()
Gets the current drop data, as aGValue
.onAccept
(DropTarget.AcceptCallback handler) Emitted on the drop site when a drop operation is about to begin.onDrop
(DropTarget.DropCallback handler) Emitted on the drop site when the user drops the data onto the widget.onEnter
(DropTarget.EnterCallback handler) Emitted on the drop site when the pointer enters the widget.onLeave
(DropTarget.LeaveCallback handler) Emitted on the drop site when the pointer leaves the widget.onMotion
(DropTarget.MotionCallback handler) Emitted while the pointer is moving over the drop target.void
reject()
Rejects the ongoing drop operation.void
setActions
(Set<DragAction> actions) Sets the actions that this drop target supports.void
setActions
(DragAction... actions) Sets the actions that this drop target supports.void
Sets the supportedGType
s for this drop target.void
setPreload
(boolean preload) Sets whether data should be preloaded on hover.Methods inherited from class org.gnome.gtk.EventController
getCurrentEvent, getCurrentEventDevice, getCurrentEventState, getCurrentEventTime, getName, getPropagationLimit, getPropagationPhase, getWidget, reset, setName, setPropagationLimit, setPropagationPhase, setStaticName
Methods inherited from class org.gnome.gobject.GObject
addToggleRef, addWeakPointer, bindProperty, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, connect, constructed, disconnect, dispatchPropertiesChanged, dispose, dupData, dupQdata, emit, emitNotify, finalize_, forceFloating, freezeNotify, get, getData, getMemoryLayout, getProperty, getProperty, getProperty, getQdata, getv, interfaceFindProperty, interfaceInstallProperty, interfaceListProperties, isFloating, newInstance, newInstance, newInstance, newInstance, newv, notify_, notify_, notifyByPspec, onNotify, ref, refSink, removeToggleRef, removeWeakPointer, replaceData, replaceQdata, runDispose, set, setData, setDataFull, setProperty, setProperty, setProperty, setQdata, setQdataFull, setv, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref, withProperties
Methods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, getPrivate, readGClass, writeGClass
Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
DropTarget
Create a DropTarget proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
DropTarget
Creates a newGtkDropTarget
object.If the drop target should support more than 1 type, pass
G_TYPE_INVALID
fortype
and then callsetGtypes(org.gnome.glib.Type[])
.- Parameters:
type
- The supported type orG_TYPE_INVALID
actions
- the supported actions
-
DropTarget
Creates a newGtkDropTarget
object.If the drop target should support more than 1 type, pass
G_TYPE_INVALID
fortype
and then callsetGtypes(org.gnome.glib.Type[])
.- Parameters:
type
- The supported type orG_TYPE_INVALID
actions
- the supported actions
-
-
Method Details
-
getType
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuper
keyword, but will set the native typeclass function pointers to the parent type. When overriding a native virtual method in Java, "chaining up" withsuper.methodName()
doesn't work, because it invokes the overridden function pointer again. To chain up, callasParent().methodName()
. This will call the native function pointer of this virtual method in the typeclass of the parent type.- Overrides:
asParent
in classEventController
-
getActions
Gets the actions that this drop target supports.- Returns:
- the actions that this drop target supports
-
getCurrentDrop
Gets the currently handled drop operation.If no drop operation is going on,
null
is returned.- Returns:
- The current drop
-
getDrop
Deprecated.UsegetCurrentDrop()
insteadGets the currently handled drop operation.If no drop operation is going on,
null
is returned.- Returns:
- The current drop
-
getFormats
Gets the data formats that this drop target accepts.If the result is
null
, all formats are expected to be supported.- Returns:
- the supported data formats
-
getGtypes
Gets the list of supportedGType
s that can be dropped on the target.If no types have been set,
NULL
will be returned.- Returns:
- the
G_TYPE_INVALID
-terminated array of types included in formats
-
getPreload
public boolean getPreload()Gets whether data should be preloaded on hover.- Returns:
true
if drop data should be preloaded
-
getValue
-
reject
public void reject()Rejects the ongoing drop operation.If no drop operation is ongoing, i.e when
Gtk.DropTarget:current-drop
isnull
, 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.
-
setActions
Sets the actions that this drop target supports.- Parameters:
actions
- the supported actions
-
setActions
Sets the actions that this drop target supports.- Parameters:
actions
- the supported actions
-
setGtypes
Sets the supportedGType
s for this drop target.- Parameters:
types
- all supportedGType
s that can be dropped on the target
-
setPreload
public void setPreload(boolean preload) Sets whether data should be preloaded on hover.- Parameters:
preload
-true
to preload drop data
-
onAccept
Emitted on the drop site when a drop operation is about to begin.If the drop is not accepted,
false
will be returned and the drop target will ignore the drop. Iftrue
is returned, the drop is accepted for now but may be rejected later via a call toreject()
or ultimately by returningfalse
from aGtk.DropTarget::drop
handler.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
, theGtk.DropTarget:preload
property should be set and the value should be inspected via the ::notify:value signal, callingreject()
if required.- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitAccept
Emits the "accept" signal. SeeonAccept(org.gnome.gtk.DropTarget.AcceptCallback)
. -
onDrop
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
false
and 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 givenvalue
and performing the drop operation.- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitDrop
Emits the "drop" signal. SeeonDrop(org.gnome.gtk.DropTarget.DropCallback)
. -
onEnter
Emitted on the drop site when the pointer enters the widget.It can be used to set up custom highlighting.
- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitEnter
Emits the "enter" signal. SeeonEnter(org.gnome.gtk.DropTarget.EnterCallback)
. -
onLeave
Emitted on the drop site when the pointer leaves the widget.Its main purpose it to undo things done in
Gtk.DropTarget::enter
.- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitLeave
public void emitLeave()Emits the "leave" signal. SeeonLeave(org.gnome.gtk.DropTarget.LeaveCallback)
. -
onMotion
Emitted while the pointer is moving over the drop target.- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitMotion
Emits the "motion" signal. SeeonMotion(org.gnome.gtk.DropTarget.MotionCallback)
. -
builder
ADropTarget.Builder
object constructs aDropTarget
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withDropTarget.Builder.build()
.
-
getCurrentDrop()
instead