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

current_event : Gdk::Event? current_event, current_event_device : Gdk::Device? current_event_device, current_event_state : Gdk::ModifierType current_event_state, current_event_time : UInt32 current_event_time, name : String? name, name=(value : String) : String
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, static_name=(name : String?) : Nil static_name=, widget : Gtk::Widget widget

Constructor methods inherited from class Gtk::EventController

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

Class methods inherited from class Gtk::EventController

g_type : UInt64 g_type

Instance methods inherited from class GObject::Object

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)? data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, 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)? qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void)?) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void)? steal_data, steal_qdata(quark : UInt32) : Pointer(Void)? 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, 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

compat_control(what : UInt64, data : Pointer(Void)?) : 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

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


def self.new #

Initialize a new DropTarget.


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

Class Method Detail

def self.g_type : UInt64 #

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


Instance Method Detail

def accept_signal #

def actions : Gdk::DragAction #

Gets the actions that this drop target supports.


def actions=(actions : Gdk::DragAction) : Nil #

Sets the actions that this drop target supports.


def current_drop : Gdk::Drop? #

Gets the currently handled drop operation.

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


def drop : Gdk::Drop? #

Gets the currently handled drop operation.

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


def drop_signal #

def enter_signal #

def formats : Gdk::ContentFormats? #

Gets the data formats that this drop target accepts.

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


def formats=(value : Gdk::ContentFormats?) : Gdk::ContentFormats? #

def gtypes : Enumerable(UInt64)? #

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

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


def gtypes=(types : Enumerable(UInt64)?) : Nil #

Sets the supported GTypes for this drop target.


def leave_signal #

def motion_signal #

def preload : Bool #

Gets whether data should be preloaded on hover.


def preload=(preload : Bool) : Nil #

Sets whether data should be preloaded on hover.


def preload? : Bool #

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.


def value : GObject::Value? #

Gets the current drop data, as a GValue.