class Gtk::DropTarget

Overview

Gtk::DropTarget is an event controller to receive Drag-and-Drop operations.

The most basic way to use a Gtk::DropTarget to receive drops on a widget is to create it via Gtk::DropTarget.new, passing in the GType of the data you want to receive and connect to the Gtk::DropTarget::#drop signal to receive the data:

WARNING ⚠️ The following code is in c ⚠️

static gboolean
on_drop (Gtk::DropTarget *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)
{
  Gtk::DropTarget *target =
    gtk_drop_target_new (G_TYPE_INVALID, GDK_ACTION_COPY);

  // This widget accepts two types of drop types: GFile objects
  // and Gdk::Pixbuf 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));
}

Gtk::DropTarget supports more options, such as:

However, Gtk::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 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 Gtk::DropControllerMotion.

Defined in:

lib/gi-crystal/src/auto/gtk-4.0/drop_target.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from class Gtk::EventController

==(other : self) ==, current_event : Gdk::Event | Nil current_event, current_event_device : Gdk::Device | Nil current_event_device, current_event_state : Gdk::ModifierType current_event_state, current_event_time : UInt32 current_event_time, hash(hasher) hash, name : String | Nil name, name=(value : String) : String
name=(value : Nil) : Nil
name=(name : String | Nil) : Nil
name=
, name? : String | Nil name?, propagation_limit : Gtk::PropagationLimit propagation_limit, propagation_limit=(limit : Gtk::PropagationLimit) : Nil propagation_limit=, propagation_phase : Gtk::PropagationPhase propagation_phase, propagation_phase=(phase : Gtk::PropagationPhase) : Nil propagation_phase=, reset : Nil reset, widget : Gtk::Widget widget

Constructor methods inherited from class Gtk::EventController

new
new(*, name : String | Nil = nil, propagation_limit : Gtk::PropagationLimit | Nil = nil, propagation_phase : Gtk::PropagationPhase | Nil = nil, widget : Gtk::Widget | Nil = nil)
new

Class methods inherited from class Gtk::EventController

g_type : UInt64 g_type

Instance methods inherited from class GObject::Object

==(other : self) ==, bind_property(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags) : GObject::Binding bind_property, bind_property_full(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags, transform_to : GObject::Closure, transform_from : GObject::Closure) : GObject::Binding bind_property_full, data(key : String) : Pointer(Void) | Nil data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, hash(hasher) hash, notify(property_name : String) : Nil notify, notify_by_pspec(pspec : GObject::ParamSpec) : Nil notify_by_pspec, notify_signal notify_signal, property(property_name : String, value : _) : Nil property, qdata(quark : UInt32) : Pointer(Void) | Nil qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void) | Nil) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void) | Nil steal_data, steal_qdata(quark : UInt32) : Pointer(Void) | Nil steal_qdata, thaw_notify : Nil thaw_notify, to_unsafe : Pointer(Void) to_unsafe, watch_closure(closure : GObject::Closure) : Nil watch_closure

Constructor methods inherited from class GObject::Object

cast(obj : GObject::Object) : self cast, new(pointer : Pointer(Void), transfer : GICrystal::Transfer)
new
new
, newv(object_type : UInt64, parameters : Enumerable(GObject::Parameter)) : self newv

Class methods inherited from class GObject::Object

cast?(obj : GObject::Object) : self | Nil cast?, compat_control(what : UInt64, data : Pointer(Void) | Nil) : UInt64 compat_control, g_type : UInt64 g_type, interface_find_property(g_iface : GObject::TypeInterface, property_name : String) : GObject::ParamSpec interface_find_property, interface_list_properties(g_iface : GObject::TypeInterface) : Enumerable(GObject::ParamSpec) interface_list_properties

Macros inherited from class GObject::Object

previous_vfunc(*args) previous_vfunc, previous_vfunc!(*args) previous_vfunc!, signal(signature) signal

Constructor Detail

def self.new(type : UInt64, actions : Gdk::DragAction) : self #

Creates a new Gtk::DropTarget object.

If the drop target should support more than 1 type, pass %G_TYPE_INVALID for type and then call Gtk::DropTarget#gtypes=.


[View source]
def self.new #

Initialize a new DropTarget.


[View source]
def self.new(*, actions : Gdk::DragAction | Nil = nil, current_drop : Gdk::Drop | Nil = nil, formats : Gdk::ContentFormats | Nil = nil, name : String | Nil = nil, preload : Bool | Nil = nil, propagation_limit : Gtk::PropagationLimit | Nil = nil, propagation_phase : Gtk::PropagationPhase | Nil = nil, value : GObject::Value | Nil = nil, widget : Gtk::Widget | Nil = nil) #

[View source]

Class Method Detail

def self.g_type : UInt64 #

Returns the type id (GType) registered in GLib type system.


[View source]

Instance Method Detail

def ==(other : self) #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.


def accept_signal #

[View source]
def actions : Gdk::DragAction #

Gets the actions that this drop target supports.


[View source]
def actions=(actions : Gdk::DragAction) : Nil #

Sets the actions that this drop target supports.


[View source]
def current_drop : Gdk::Drop | Nil #

Gets the currently handled drop operation.

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


[View source]
def drop : Gdk::Drop | Nil #

Gets the currently handled drop operation.

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

DEPRECATED


[View source]
def drop_signal #

[View source]
def enter_signal #

[View source]
def formats : Gdk::ContentFormats | Nil #

Gets the data formats that this drop target accepts.

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


[View source]
def formats=(value : Gdk::ContentFormats | Nil) : Gdk::ContentFormats | Nil #

[View source]
def gtypes : Enumerable(UInt64) | Nil #

Gets the list of supported GTypes that can be dropped on the target.

If no types have been set, NULL will be returned.


[View source]
def gtypes=(types : Enumerable(UInt64) | Nil) : Nil #

Sets the supported GTypes for this drop target.


[View source]
def hash(hasher) #
Description copied from class Reference

See Object#hash(hasher)


def leave_signal #

[View source]
def motion_signal #

[View source]
def preload : Bool #

Gets whether data should be preloaded on hover.


[View source]
def preload=(preload : Bool) : Nil #

Sets whether data should be preloaded on hover.


[View source]
def preload? : Bool #

[View source]
def reject : Nil #

Rejects the ongoing drop operation.

If no drop operation is ongoing, i.e when [property@Gtk.DropTarget:current-drop] is nil, 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.


[View source]
def value : GObject::Value | Nil #

Gets the current drop data, as a GValue.


[View source]