Package ch.bailu.gtk.gtk
Class DropTarget
java.lang.Object
ch.bailu.gtk.type.Type
ch.bailu.gtk.type.Pointer
ch.bailu.gtk.gobject.Object
ch.bailu.gtk.gtk.EventController
ch.bailu.gtk.gtk.DropTarget
- All Implemented Interfaces:
PointerInterface
`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 [ctor@Gtk.DropTarget.new], passing in the
`GType` of the data you want to receive and connect to the
[signal@Gtk.DropTarget::drop] signal to receive the data:
```c
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);
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 [signal@Gtk.DropTarget::accept] signal
and the [method@Gtk.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], [signal@Gtk.DropTarget::motion] and
[signal@Gtk.DropTarget::leave] signals
* configuring how to receive data by setting the
[property@Gtk.DropTarget:preload] property and listening for its
availability via the [property@Gtk.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 [class@Gtk.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 [class@Gtk.DropControllerMotion].
The most basic way to use a `GtkDropTarget` to receive drops on a
widget is to create it via [ctor@Gtk.DropTarget.new], passing in the
`GType` of the data you want to receive and connect to the
[signal@Gtk.DropTarget::drop] signal to receive the data:
```c
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);
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 [signal@Gtk.DropTarget::accept] signal
and the [method@Gtk.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], [signal@Gtk.DropTarget::motion] and
[signal@Gtk.DropTarget::leave] signals
* configuring how to receive data by setting the
[property@Gtk.DropTarget:preload] property and listening for its
availability via the [property@Gtk.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 [class@Gtk.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 [class@Gtk.DropControllerMotion].
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
static interface
static interface
static interface
static interface
Nested classes/interfaces inherited from class ch.bailu.gtk.gobject.Object
Object.OnBindingTransformFunc, Object.OnDestroyNotify, Object.OnDuplicateFunc, Object.OnNotify, Object.OnToggleNotify, Object.OnWeakNotify
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
static final String
static final String
static final String
static final String
Fields inherited from class ch.bailu.gtk.gobject.Object
SIGNAL_ON_NOTIFY
-
Constructor Summary
ConstructorDescriptionDropTarget
(long type, int actions) Creates a new `GtkDropTarget` object.DropTarget
(PointerContainer pointer) -
Method Summary
Modifier and TypeMethodDescriptionint
Gets the actions that this drop target supports.static ClassHandler
Gets the currently handled drop operation.Gets the data formats that this drop target accepts.static int
static long
static TypeSystem.TypeSize
boolean
Gets whether data should be preloaded on hover.static long
static TypeSystem.TypeSize
getValue()
Gets the current drop data, as a `GValue`.onAccept
(DropTarget.OnAccept signal) Connect to signal "accept".onDrop
(DropTarget.OnDrop signal) Connect to signal "drop".onEnter
(DropTarget.OnEnter signal) Connect to signal "enter".onLeave
(DropTarget.OnLeave signal) Connect to signal "leave".onMotion
(DropTarget.OnMotion signal) Connect to signal "motion".void
reject()
Rejects the ongoing drop operation.void
setActions
(int actions) Sets the actions that this drop target supports.void
Sets the supported `GTypes` for this drop target.void
setPreload
(boolean preload) Sets whether data should be preloaded on hover.Methods inherited from class ch.bailu.gtk.gtk.EventController
getCurrentEvent, getCurrentEventDevice, getCurrentEventState, getCurrentEventTime, getName, getPropagationLimit, getPropagationPhase, getWidget, reset, setName, setName, setPropagationLimit, setPropagationPhase, setStaticName, setStaticName
Methods inherited from class ch.bailu.gtk.gobject.Object
addToggleRef, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, disconnect, disconnect, dupData, dupData, dupQdata, forceFloating, freezeNotify, get, get, getData, getData, getProperty, getProperty, getQdata, interfaceFindProperty, interfaceInstallProperty, isFloating, notify, notify, notifyByPspec, onNotify, ref, refSink, removeToggleRef, replaceData, replaceData, replaceQdata, runDispose, set, set, setData, setData, setDataFull, setDataFull, setProperty, setProperty, setQdata, setQdataFull, stealData, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref
Methods inherited from class ch.bailu.gtk.type.Pointer
asCPointer, cast, connectSignal, disconnectSignals, disconnectSignals, equals, hashCode, throwIfNull, throwNullPointerException, toString, unregisterCallbacks, unregisterCallbacks
Methods inherited from class ch.bailu.gtk.type.Type
asCPointer, asCPointer, asCPointerNotNull, asJnaPointer, asJnaPointer, asPointer, asPointer, cast, cast, throwIfNull
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface ch.bailu.gtk.type.PointerInterface
asCPointerNotNull, asJnaPointer, asPointer, isNotNull, isNull
-
Field Details
-
SIGNAL_ON_ACCEPT
- See Also:
-
SIGNAL_ON_DROP
- See Also:
-
SIGNAL_ON_ENTER
- See Also:
-
SIGNAL_ON_LEAVE
- See Also:
-
SIGNAL_ON_MOTION
- See Also:
-
-
Constructor Details
-
DropTarget
-
DropTarget
public DropTarget(long type, int actions) Creates a new `GtkDropTarget` object.
If the drop target should support more than 1 type, pass
%G_TYPE_INVALID for @type and then call
[method@Gtk.DropTarget.set_gtypes].- Parameters:
type
- The supported type or %G_TYPE_INVALIDactions
- the supported actions
-
-
Method Details
-
getClassHandler
-
getActions
public int 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
-
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
-
getPreload
public boolean getPreload()Gets whether data should be preloaded on hover.- Returns:
- %TRUE if drop data should be preloaded
-
getValue
Gets the current drop data, as a `GValue`.- Returns:
- The current drop data
-
reject
public void reject()Rejects the ongoing drop operation.
If no drop operation is ongoing, i.e when [property@Gtk.DropTarget:current-drop]
is %NULL, 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
public void setActions(int actions) Sets the actions that this drop target supports.- Parameters:
actions
- the supported actions
-
setGtypes
Sets the supported `GTypes` for this drop target.- Parameters:
types
- all supported `GType`s that can be dropped on the targetn_types
- number of @types
-
setPreload
public void setPreload(boolean preload) Sets whether data should be preloaded on hover.- Parameters:
preload
- %TRUE to preload drop data
-
onAccept
Connect to signal "accept".
SeeDropTarget.OnAccept.onAccept(ch.bailu.gtk.gdk.Drop)
for signal description.
FieldSIGNAL_ON_ACCEPT
contains original signal name and can be used as resource reference.- Parameters:
signal
- callback function (lambda).- Returns:
SignalHandler
. Can be used to disconnect signal and to release callback function.
-
onDrop
Connect to signal "drop".
SeeDropTarget.OnDrop.onDrop(ch.bailu.gtk.gobject.Value, double, double)
for signal description.
FieldSIGNAL_ON_DROP
contains original signal name and can be used as resource reference.- Parameters:
signal
- callback function (lambda).- Returns:
SignalHandler
. Can be used to disconnect signal and to release callback function.
-
onEnter
Connect to signal "enter".
SeeDropTarget.OnEnter.onEnter(double, double)
for signal description.
FieldSIGNAL_ON_ENTER
contains original signal name and can be used as resource reference.- Parameters:
signal
- callback function (lambda).- Returns:
SignalHandler
. Can be used to disconnect signal and to release callback function.
-
onLeave
Connect to signal "leave".
SeeDropTarget.OnLeave.onLeave()
for signal description.
FieldSIGNAL_ON_LEAVE
contains original signal name and can be used as resource reference.- Parameters:
signal
- callback function (lambda).- Returns:
SignalHandler
. Can be used to disconnect signal and to release callback function.
-
onMotion
Connect to signal "motion".
SeeDropTarget.OnMotion.onMotion(double, double)
for signal description.
FieldSIGNAL_ON_MOTION
contains original signal name and can be used as resource reference.- Parameters:
signal
- callback function (lambda).- Returns:
SignalHandler
. Can be used to disconnect signal and to release callback function.
-
getTypeID
public static long getTypeID() -
getParentTypeID
public static long getParentTypeID() -
getTypeSize
-
getParentTypeSize
-
getInstanceSize
public static int getInstanceSize()
-