Struct gtk4::DropTarget [−][src]
pub struct DropTarget(_);
Expand description
DropTarget
is an event controller to receive Drag-and-Drop operations.
The most basic way to use a DropTarget
to receive drops on a
widget is to create it via new()
, passing in the
GType
of the data you want to receive and connect to the
signal::DropTarget::drop
signal to receive the data:
⚠️ The following code is in 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);
gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (target));
}
DropTarget
supports more options, such as:
- rejecting potential drops via the
signal::DropTarget::accept
signal and thereject()
function to let other drop targets handle the drop - tracking an ongoing drag operation before the drop via the
signal::DropTarget::enter
,signal::DropTarget::motion
andsignal::DropTarget::leave
signals - configuring how to receive data by setting the
property::DropTarget::preload
property and listening for its availability via theproperty::DropTarget::value
property
However, DropTarget
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
.
Implements
Implementations
Creates a new DropTarget
object.
If the drop target should support more than 1 type, pass
G_TYPE_INVALID
for type_
and then call
set_types()
.
type_
The supported type or G_TYPE_INVALID
actions
the supported actions
Returns
the new DropTarget
Creates a new builder-pattern struct instance to construct DropTarget
objects.
This method returns an instance of DropTargetBuilder
which can be used to create DropTarget
objects.
This is supported on crate feature v4_4
only.
v4_4
only.Gets the currently handled drop operation.
If no drop operation is going on, None
is returned.
Deprecated since 4.4
Use current_drop()
instead
Returns
The current drop
Rejects the ongoing drop operation.
If no drop operation is ongoing, i.e when property::DropTarget::current-drop
is None
, 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.
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. If true
is returned, the drop is accepted for now
but may be rejected later via a call to reject()
or ultimately by returning false
from a signal::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
, the
property::DropTarget::preload
property should be set and the value
should be inspected via the ::notify:value signal, calling
reject()
if required.
drop
the gdk::Drop
Returns
true
if drop
is accepted
pub fn connect_enter<F: Fn(&Self, f64, f64) -> DragAction + 'static>(
&self,
f: F
) -> SignalHandlerId
pub fn connect_enter<F: Fn(&Self, f64, f64) -> DragAction + 'static>(
&self,
f: F
) -> SignalHandlerId
Emitted on the drop site when the pointer enters the widget.
It can be used to set up custom highlighting.
x
the x coordinate of the current pointer position
y
the y coordinate of the current pointer position
Returns
Preferred action for this drag operation or 0 if
dropping is not supported at the current x
,y
location.
Emitted on the drop site when the pointer leaves the widget.
Its main purpose it to undo things done in
signal::DropTarget::enter
.
pub fn connect_motion<F: Fn(&Self, f64, f64) -> DragAction + 'static>(
&self,
f: F
) -> SignalHandlerId
pub fn connect_motion<F: Fn(&Self, f64, f64) -> DragAction + 'static>(
&self,
f: F
) -> SignalHandlerId
v4_4
only.Sets the supported GTypes
for this drop target.
types
all supported GType
s
that can be dropped on the target
Gets the list of supported GType
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
pub fn connect_drop<F: Fn(&DropTarget, &Value, f64, f64) -> bool + 'static>(
&self,
f: F
) -> SignalHandlerId
pub fn connect_drop<F: Fn(&DropTarget, &Value, f64, f64) -> bool + 'static>(
&self,
f: F
) -> SignalHandlerId
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 given value
and performing the drop operation.
value
the GValue
being dropped
x
the x coordinate of the current pointer position
y
the y coordinate of the current pointer position
Returns
whether the drop was accepted at the given pointer position
Trait Implementations
type Parent = EventController
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
Returns the type identifier of Self
.
Auto Trait Implementations
impl RefUnwindSafe for DropTarget
impl !Send for DropTarget
impl !Sync for DropTarget
impl Unpin for DropTarget
impl UnwindSafe for DropTarget
Blanket Implementations
Mutably borrows from an owned value. Read more
Upcasts an object to a superclass or interface T
. Read more
Upcasts an object to a reference of its superclass or interface T
. Read more
Tries to downcast to a subclass or interface implementor T
. Read more
Tries to downcast to a reference of its subclass or interface implementor T
. Read more
Tries to cast to an object of type T
. This handles upcasting, downcasting
and casting between interface and interface implementors. All checks are performed at
runtime, while downcast
and upcast
will do many checks at compile-time already. Read more
Tries to cast to reference to an object of type T
. This handles upcasting, downcasting
and casting between interface and interface implementors. All checks are performed at
runtime, while downcast
and upcast
will do many checks at compile-time already. Read more
Casts to T
unconditionally. Read more
Casts to &T
unconditionally. Read more
Returns true
if the object is an instance of (can be cast to) T
.
pub fn set_property<'a, N, V>(
&self,
property_name: N,
value: V
) -> Result<(), BoolError> where
N: Into<&'a str>,
V: ToValue,
pub fn set_property_from_value<'a, N>(
&self,
property_name: N,
value: &Value
) -> Result<(), BoolError> where
N: Into<&'a str>,
pub fn set_properties_from_value(
&self,
property_values: &[(&str, Value)]
) -> Result<(), BoolError>
pub fn has_property<'a, N>(&self, property_name: N, type_: Option<Type>) -> bool where
N: Into<&'a str>,
pub fn find_property<'a, N>(&self, property_name: N) -> Option<ParamSpec> where
N: Into<&'a str>,
Safety Read more
Safety Read more
Safety Read more
Safety Read more
pub fn connect<'a, N, F>(
&self,
signal_name: N,
after: bool,
callback: F
) -> Result<SignalHandlerId, BoolError> where
N: Into<&'a str>,
F: 'static + Fn(&[Value]) -> Option<Value> + Send + Sync,
Same as connect
but takes a SignalId
instead of a signal name.
pub fn connect_local<'a, N, F>(
&self,
signal_name: N,
after: bool,
callback: F
) -> Result<SignalHandlerId, BoolError> where
N: Into<&'a str>,
F: 'static + Fn(&[Value]) -> Option<Value>,
Same as connect_local
but takes a SignalId
instead of a signal name.
pub unsafe fn connect_unsafe<'a, N, F>(
&self,
signal_name: N,
after: bool,
callback: F
) -> Result<SignalHandlerId, BoolError> where
N: Into<&'a str>,
F: Fn(&[Value]) -> Option<Value>,
Same as connect_unsafe
but takes a SignalId
instead of a signal name.
Emit signal by signal id.
Same as emit
but takes Value
for the arguments.
Emit signal by its name.
Same as emit_by_name
but takes Value
for the arguments.
Emit signal with details by signal id.
Same as emit_with_details
but takes Value
for the arguments.
pub fn connect_notify<F>(&self, name: Option<&str>, f: F) -> SignalHandlerId where
F: 'static + Fn(&T, &ParamSpec) + Send + Sync,
pub fn connect_notify_local<F>(
&self,
name: Option<&str>,
f: F
) -> SignalHandlerId where
F: 'static + Fn(&T, &ParamSpec),
pub unsafe fn connect_notify_unsafe<F>(
&self,
name: Option<&str>,
f: F
) -> SignalHandlerId where
F: Fn(&T, &ParamSpec),
pub fn bind_property<'a, O, N, M>(
&'a self,
source_property: N,
target: &'a O,
target_property: M
) -> BindingBuilder<'a> where
O: ObjectType,
N: Into<&'a str>,
M: Into<&'a str>,
Returns a SendValue
clone of self
.