Class DropTarget
- All Implemented Interfaces:
Proxy
The most basic way to use a GtkDropTarget to receive drops on a
widget is to create it via DropTarget(), 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::acceptsignal 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::motionandGtk.DropTarget::leavesignals - configuring how to receive data by setting the
Gtk.DropTarget:preloadproperty and listening for its availability via theGtk.DropTarget:valueproperty
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
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFunctional interface declaration of theAcceptCallbackcallback.static classDropTarget.Builder<B extends DropTarget.Builder<B>>Inner class implementing a builder pattern to construct a GObject with properties.static interfaceFunctional interface declaration of theDropCallbackcallback.static classstatic interfaceFunctional interface declaration of theEnterCallbackcallback.static interfaceFunctional interface declaration of theLeaveCallbackcallback.static interfaceFunctional interface declaration of theMotionCallbackcallback.Nested classes/interfaces inherited from class org.gnome.gtk.EventController
EventController.EventController$Impl, EventController.EventControllerClassNested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new DropTarget.DropTarget(MemorySegment address) Create a DropTarget proxy instance for the provided memory address.DropTarget(Type type, Set<DragAction> actions) Creates a newGtkDropTargetobject.DropTarget(Type type, DragAction... actions) Creates a newGtkDropTargetobject. -
Method Summary
Modifier and TypeMethodDescriptionprotected DropTargetasParent()Returns this instance as if it were its parent type.static DropTarget.Builder<? extends DropTarget.Builder> builder()ADropTarget.Builderobject constructs aDropTargetwith the specified properties.booleanemitAccept(@Nullable Drop drop) Emits the "accept" signal.booleanEmits the "drop" signal.emitEnter(double x, double y) Emits the "enter" signal.voidEmits the "leave" signal.emitMotion(double x, double y) Emits the "motion" signal.Gets the actions that this drop target supports.@Nullable DropGets the currently handled drop operation.@Nullable DropgetDrop()Deprecated.@Nullable ContentFormatsGets the data formats that this drop target accepts.@Nullable Type @Nullable []Gets the list of supportedGTypes that can be dropped on the target.booleanGets whether data should be preloaded on hover.static @Nullable TypegetType()Get the GType of the DropTarget class@Nullable ValuegetValue()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.voidreject()Rejects the ongoing drop operation.voidsetActions(Set<DragAction> actions) Sets the actions that this drop target supports.voidsetActions(DragAction... actions) Sets the actions that this drop target supports.voidSets the supportedGTypes for this drop target.voidsetPreload(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, setStaticNameMethods 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, 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, withPropertiesMethods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, cast, getPrivate, readGClass, writeGClassMethods inherited from class org.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 newGtkDropTargetobject.If the drop target should support more than 1 type, pass
G_TYPE_INVALIDfortypeand then callsetGtypes(org.gnome.glib.Type[]).- Parameters:
type- The supported type orG_TYPE_INVALIDactions- the supported actions
-
DropTarget
Creates a newGtkDropTargetobject.If the drop target should support more than 1 type, pass
G_TYPE_INVALIDfortypeand then callsetGtypes(org.gnome.glib.Type[]).- Parameters:
type- The supported type orG_TYPE_INVALIDactions- the supported actions
-
DropTarget
public DropTarget()Creates a new DropTarget.
-
-
Method Details
-
getType
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuperkeyword, 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:
asParentin 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,
nullis returned.- Returns:
- The current drop
- Since:
- 4.4
-
getDrop
Deprecated.UsegetCurrentDrop()insteadGets the currently handled drop operation.If no drop operation is going on,
nullis 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 supportedGTypes that can be dropped on the target.If no types have been set,
NULLwill 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:
trueif drop data should be preloaded
-
getValue
Gets the current drop data, as aGValue.- Returns:
- The current drop data
-
reject
public void reject()Rejects the ongoing drop operation.If no drop operation is ongoing, i.e when
Gtk.DropTarget:current-dropisnull, 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 supportedGTypes for this drop target.- Parameters:
types- all supportedGTypes that can be dropped on the target
-
setPreload
public void setPreload(boolean preload) Sets whether data should be preloaded on hover.- Parameters:
preload-trueto preload drop data
-
onAccept
Emitted on the drop site when a drop operation is about to begin.If the drop is not accepted,
falsewill be returned and the drop target will ignore the drop. Iftrueis returned, the drop is accepted for now but may be rejected later via a call toreject()or ultimately by returningfalsefrom aGtk.DropTarget::drophandler.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:preloadproperty 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
falseand 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 givenvalueand 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.Builderobject constructs aDropTargetwith the specified properties. Use the variousset...()methods to set properties, and finish construction withDropTarget.Builder.build().- Returns:
- the builder object
-
getCurrentDrop()instead