Struct gtk4::DropTarget

source ·
#[repr(transparent)]
pub struct DropTarget { /* private fields */ }
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);

  g_signal_connect (target, "drop", G_CALLBACK (on_drop), self);
  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 the reject() 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 and signal::DropTarget::leave signals
  • configuring how to receive data by setting the property::DropTarget::preload property and listening for its availability via the property::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

EventControllerExt, glib::ObjectExt

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.

Gets the actions that this drop target supports.

Returns

the actions that this drop target supports

Available on crate feature v4_4 only.

Gets the currently handled drop operation.

If no drop operation is going on, None is returned.

Returns

The current drop

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

Gets the data formats that this drop target accepts.

If the result is None, all formats are expected to be supported.

Returns

the supported data formats

Gets whether data should be preloaded on hover.

Returns

true if drop data should be preloaded

Gets the current drop data, as a GValue.

Returns

The current drop data

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.

Sets the actions that this drop target supports.

actions

the supported actions

Sets whether data should be preloaded on hover.

preload

true to preload drop 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

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.

Emitted while the pointer is moving over the drop target.

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.

Available on crate feature v4_4 only.

Sets the supported GTypes for this drop target.

types

all supported GTypes that can be dropped on the target

Similar to Self::value but panics if the value is of a different type.

Gets the list of supported GTypes 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

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§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Formats the value using the given formatter. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Returns the type identifier of Self.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
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 the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Returns true if the object is an instance of (can be cast to) T.
Returns the type of the object.
Returns the ObjectClass of the object. Read more
Returns the class of the object.
Returns the class of the object in the given type T. Read more
Returns the interface T of the object. Read more
Sets the property property_name of the object to value value. Read more
Sets the property property_name of the object to value value. Read more
Sets multiple properties of the object at once. Read more
Sets multiple properties of the object at once. Read more
Gets the property property_name of the object and cast it to the type V. Read more
Gets the property property_name of the object. Read more
Check if the object has a property property_name of the given type_. Read more
Get the type of the property property_name of this object. Read more
Get the ParamSpec of the property property_name of this object.
Return all ParamSpec of the properties of this object.
Freeze all property notifications until the return guard object is dropped. Read more
Set arbitrary data on this object with the given key. Read more
Return previously set arbitrary data of this object with the given key. Read more
Retrieve previously set arbitrary data of this object with the given key. Read more
Set arbitrary data on this object with the given key. Read more
Return previously set arbitrary data of this object with the given key. Read more
Retrieve previously set arbitrary data of this object with the given key. Read more
Block a given signal handler. Read more
Unblock a given signal handler.
Stop emission of the currently emitted signal.
Stop emission of the currently emitted signal by the (possibly detailed) signal name.
Connect to the signal signal_name on this object. Read more
Connect to the signal signal_id on this object. Read more
Connect to the signal signal_name on this object. Read more
Connect to the signal signal_id on this object. Read more
Connect to the signal signal_name on this object. Read more
Connect to the signal signal_id on this object. Read more
Connect a closure to the signal signal_name on this object. Read more
Connect a closure to the signal signal_id on this object. Read more
Limits the lifetime of closure to the lifetime of the object. When the object’s reference count drops to zero, the closure will be invalidated. An invalidated closure will ignore any calls to invoke_with_values, or invoke when using Rust closures.
Emit signal by signal id. Read more
Same as Self::emit but takes Value for the arguments.
Emit signal by its name. Read more
Emit signal by its name. Read more
Emit signal by its name with details. Read more
Emit signal by its name with details. Read more
Emit signal by signal id with details. Read more
Emit signal by signal id with details. Read more
Disconnect a previously connected signal handler.
Connect to the notify signal of the object. Read more
Connect to the notify signal of the object. Read more
Connect to the notify signal of the object. Read more
Notify that the given property has changed its value. Read more
Notify that the given property has changed its value. Read more
Downgrade this object to a weak reference.
Add a callback to be notified when the Object is disposed.
Add a callback to be notified when the Object is disposed. Read more
Bind property source_property on this object to the target_property on the target object. Read more
Returns the strong reference count of this object.
Runs the dispose mechanism of the object. Read more
Ensures that the type has been registered with the type system.
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.