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 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

Gets the currently handled drop operation.

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

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::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.

Sets the supported GTypes for this drop target.

types

all supported GTypes that can be dropped

Gets the list of supported GTypes for self.

If no type have been set, None will be returned.

Returns

G_TYPE_INVALID-terminated array of types included in formats or None if none.

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 rading the given value and performing the drop operation.

value

the glib::Value 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

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 ==. Read more

This method tests for !=.

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

Performs the conversion.

Performs the conversion.

Returns true if the object is an instance of (can be cast to) T.

Safety Read more

Safety Read more

Safety Read more

Safety Read more

Safety Read more

Safety Read more

Same as connect but takes a SignalId instead of a signal name.

Same as connect_local but takes a SignalId instead of a signal name.

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.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Returns a SendValue clone of self.

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.