class Gtk::DropTarget
- Gtk::DropTarget
- Gtk::EventController
- GObject::Object
- Reference
- Object
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:
- rejecting potential drops via the
Gtk::DropTarget::#accept
signal and theGtk::DropTarget#reject
function to let other drop targets handle the drop - tracking an ongoing drag operation before the drop via the
Gtk::DropTarget::#enter
,Gtk::DropTarget::#motion
andGtk::DropTarget::#leave
signals - configuring how to receive data by setting the
Gtk::DropTarget#preload
property and listening for its availability via theGtk::DropTarget#value
property
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.crConstructors
-
.new(type : UInt64, actions : Gdk::DragAction) : self
Creates a new
Gtk::DropTarget
object. -
.new
Initialize a new
DropTarget
. - .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)
Class Method Summary
-
.g_type : UInt64
Returns the type id (GType) registered in GLib type system.
Instance Method Summary
-
#==(other : self)
Returns
true
if this reference is the same as other. - #accept_signal
-
#actions : Gdk::DragAction
Gets the actions that this drop target supports.
-
#actions=(actions : Gdk::DragAction) : Nil
Sets the actions that this drop target supports.
-
#current_drop : Gdk::Drop | Nil
Gets the currently handled drop operation.
-
#drop : Gdk::Drop | Nil
Gets the currently handled drop operation.
DEPRECATED
- #drop_signal
- #enter_signal
-
#formats : Gdk::ContentFormats | Nil
Gets the data formats that this drop target accepts.
- #formats=(value : Gdk::ContentFormats | Nil) : Gdk::ContentFormats | Nil
-
#gtypes : Enumerable(UInt64) | Nil
Gets the list of supported
GType
s that can be dropped on the target. -
#gtypes=(types : Enumerable(UInt64) | Nil) : Nil
Sets the supported
GTypes
for this drop target. -
#hash(hasher)
See
Object#hash(hasher)
- #leave_signal
- #motion_signal
-
#preload : Bool
Gets whether data should be preloaded on hover.
-
#preload=(preload : Bool) : Nil
Sets whether data should be preloaded on hover.
- #preload? : Bool
-
#reject : Nil
Rejects the ongoing drop operation.
-
#value : GObject::Value | Nil
Gets the current drop data, as a
GValue
.
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) : Stringname=(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
newnew(*, 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
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=
.
Class Method Detail
Returns the type id (GType) registered in GLib type system.
Instance Method Detail
Returns true
if this reference is the same as other. Invokes same?
.
Gets the currently handled drop operation.
If no drop operation is going on, nil
is returned.
Gets the currently handled drop operation.
If no drop operation is going on, nil
is returned.
DEPRECATED
Gets the data formats that this drop target accepts.
If the result is nil
, all formats are expected to be supported.
Gets the list of supported GType
s that can be dropped on the target.
If no types have been set, NULL
will be returned.
Sets the supported GTypes
for this drop target.
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.