Class DragSource
- All Implemented Interfaces:
Proxy
GtkDragSource can be set up with the necessary
ingredients for a DND operation ahead of time. This includes
the source for the data that is being transferred, in the form
of a ContentProvider, the desired action, and the icon to
use during the drag operation. After setting it up, the drag
source must be added to a widget as an event controller, using
Widget.addController(org.gnome.gtk.EventController).
static void
my_widget_init (MyWidget *self)
{
GtkDragSource *drag_source = gtk_drag_source_new ();
g_signal_connect (drag_source, "prepare", G_CALLBACK (on_drag_prepare), self);
g_signal_connect (drag_source, "drag-begin", G_CALLBACK (on_drag_begin), self);
gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (drag_source));
}
Setting up the content provider and icon ahead of time only makes
sense when the data does not change. More commonly, you will want
to set them up just in time. To do so, GtkDragSource has
Gtk.DragSource::prepare and Gtk.DragSource::drag-begin
signals.
The ::prepare signal is emitted before a drag is started, and can be used to set the content provider and actions that the drag should be started with.
static GdkContentProvider *
on_drag_prepare (GtkDragSource *source,
double x,
double y,
MyWidget *self)
{
// This widget supports two types of content: GFile objects
// and GdkPixbuf objects; GTK will handle the serialization
// of these types automatically
GFile *file = my_widget_get_file (self);
GdkPixbuf *pixbuf = my_widget_get_pixbuf (self);
return gdk_content_provider_new_union ((GdkContentProvider *[2]) {
gdk_content_provider_new_typed (G_TYPE_FILE, file),
gdk_content_provider_new_typed (GDK_TYPE_PIXBUF, pixbuf),
}, 2);
}
The ::drag-begin signal is emitted after the GdkDrag object has
been created, and can be used to set up the drag icon.
static void
on_drag_begin (GtkDragSource *source,
GdkDrag *drag,
MyWidget *self)
{
// Set the widget as the drag icon
GdkPaintable *paintable = gtk_widget_paintable_new (GTK_WIDGET (self));
gtk_drag_source_set_icon (source, paintable, 0, 0);
g_object_unref (paintable);
}
During the DND operation, GtkDragSource emits signals that
can be used to obtain updates about the status of the operation,
but it is not normally necessary to connect to any signals,
except for one case: when the supported actions include
DragAction.MOVE, you need to listen for the
Gtk.DragSource::drag-end signal and delete the
data after it has been transferred.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classDragSource.Builder<B extends DragSource.Builder<B>>Inner class implementing a builder pattern to construct a GObject with properties.static interfaceFunctional interface declaration of theDragBeginCallbackcallback.static interfaceFunctional interface declaration of theDragCancelCallbackcallback.static interfaceFunctional interface declaration of theDragEndCallbackcallback.static classstatic interfaceFunctional interface declaration of thePrepareCallbackcallback.Nested classes/interfaces inherited from class org.gnome.gtk.GestureSingle
GestureSingle.GestureSingleClassNested classes/interfaces inherited from class org.gnome.gtk.Gesture
Gesture.BeginCallback, Gesture.CancelCallback, Gesture.EndCallback, Gesture.Gesture$Impl, Gesture.GestureClass, Gesture.SequenceStateChangedCallback, Gesture.UpdateCallbackNested classes/interfaces inherited from class org.gnome.gtk.EventController
EventController.EventController$Impl, EventController.EventControllerClassNested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new DragSource.DragSource(MemorySegment address) Create a DragSource proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionprotected DragSourceasParent()Returns this instance as if it were its parent type.static DragSource.Builder<? extends DragSource.Builder> builder()ADragSource.Builderobject constructs aDragSourcewith the specified properties.voidCancels a currently ongoing drag operation.voidemitDragBegin(@Nullable Drag drag) Emits the "drag-begin" signal.booleanemitDragCancel(@Nullable Drag drag, DragCancelReason reason) Emits the "drag-cancel" signal.voidemitDragEnd(@Nullable Drag drag, boolean deleteData) Emits the "drag-end" signal.emitPrepare(double x, double y) Emits the "prepare" signal.Gets the actions that are currently set on theGtkDragSource.@Nullable ContentProviderGets the current content provider of aGtkDragSource.@Nullable DraggetDrag()Returns the underlyingGdkDragobject for an ongoing drag.static @Nullable TypegetType()Get the GType of the DragSource classonDragBegin(DragSource.DragBeginCallback handler) Emitted on the drag source when a drag is started.Emitted on the drag source when a drag has failed.onDragEnd(DragSource.DragEndCallback handler) Emitted on the drag source when a drag is finished.onPrepare(DragSource.PrepareCallback handler) Emitted when a drag is about to be initiated.voidsetActions(Set<DragAction> actions) Sets the actions on theGtkDragSource.voidsetActions(DragAction... actions) Sets the actions on theGtkDragSource.voidsetContent(@Nullable ContentProvider content) Sets a content provider on aGtkDragSource.voidSets a paintable to use as icon during DND operations.Methods inherited from class org.gnome.gtk.GestureSingle
getButton, getCurrentButton, getCurrentSequence, getExclusive, getTouchOnly, setButton, setExclusive, setTouchOnlyMethods inherited from class org.gnome.gtk.Gesture
emitBegin, emitCancel, emitEnd, emitSequenceStateChanged, emitUpdate, getBoundingBox, getBoundingBoxCenter, getDevice, getGroup, getLastEvent, getLastUpdatedSequence, getPoint, getSequences, getSequenceState, group, handlesSequence, isActive, isGroupedWith, isRecognized, onBegin, onCancel, onEnd, onSequenceStateChanged, onUpdate, setSequenceState, setState, ungroupMethods inherited from class org.gnome.gtk.EventController
getCurrentEvent, getCurrentEventDevice, getCurrentEventState, getCurrentEventTime, getName, getPropagationLimit, getPropagationPhase, getWidget, reset, setName, setPropagationLimit, setPropagationPhase, setStaticNameMethods inherited from class org.gnome.gobject.GObject
addToggleRef, addWeakPointer, bindProperty, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, connect, constructed, disconnect, dispatchPropertiesChanged, dispose, dupData, dupQdata, emit, emitNotify, finalize_, forceFloating, freezeNotify, get, getData, getMemoryLayout, getProperty, getProperty, getProperty, getQdata, getv, interfaceFindProperty, interfaceInstallProperty, interfaceListProperties, isFloating, newInstance, newInstance, newv, notify, notify, notifyByPspec, onNotify, ref, refSink, removeToggleRef, removeWeakPointer, replaceData, replaceQdata, runDispose, set, setData, setDataFull, setProperty, setProperty, setProperty, setQdata, setQdataFull, setv, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref, withPropertiesMethods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, cast, getPrivate, readGClass, writeGClassMethods inherited from class org.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
DragSource
Create a DragSource proxy instance for the provided memory address.- Parameters:
address- the memory address of the native object
-
DragSource
public DragSource()Creates a new DragSource.
-
-
Method Details
-
getType
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuperkeyword, but will set the native typeclass function pointers to the parent type. When overriding a native virtual method in Java, "chaining up" withsuper.methodName()doesn't work, because it invokes the overridden function pointer again. To chain up, callasParent().methodName(). This will call the native function pointer of this virtual method in the typeclass of the parent type.- Overrides:
asParentin classGestureSingle
-
dragCancel
public void dragCancel()Cancels a currently ongoing drag operation. -
getActions
Gets the actions that are currently set on theGtkDragSource.- Returns:
- the actions set on this DragSource
-
getContent
Gets the current content provider of aGtkDragSource.- Returns:
- the
GdkContentProviderof this DragSource
-
getDrag
Returns the underlyingGdkDragobject for an ongoing drag.- Returns:
- the
GdkDragof the current drag operation
-
setActions
Sets the actions on theGtkDragSource.During a DND operation, the actions are offered to potential drop targets. If
actionsincludeDragAction.MOVE, you need to listen to theGtk.DragSource::drag-endsignal and handledeleteDatabeingtrue.This function can be called before a drag is started, or in a handler for the
Gtk.DragSource::preparesignal.- Parameters:
actions- the actions to offer
-
setActions
Sets the actions on theGtkDragSource.During a DND operation, the actions are offered to potential drop targets. If
actionsincludeDragAction.MOVE, you need to listen to theGtk.DragSource::drag-endsignal and handledeleteDatabeingtrue.This function can be called before a drag is started, or in a handler for the
Gtk.DragSource::preparesignal.- Parameters:
actions- the actions to offer
-
setContent
Sets a content provider on aGtkDragSource.When the data is requested in the cause of a DND operation, it will be obtained from the content provider.
This function can be called before a drag is started, or in a handler for the
Gtk.DragSource::preparesignal.You may consider setting the content provider back to
nullin aGtk.DragSource::drag-endsignal handler.- Parameters:
content- aGdkContentProvider
-
setIcon
Sets a paintable to use as icon during DND operations.The hotspot coordinates determine the point on the icon that gets aligned with the hotspot of the cursor.
If
paintableisnull, a default icon is used.This function can be called before a drag is started, or in a
Gtk.DragSource::prepareorGtk.DragSource::drag-beginsignal handler.- Parameters:
paintable- theGdkPaintableto use as iconhotX- the hotspot X coordinate on the iconhotY- the hotspot Y coordinate on the icon
-
onDragBegin
public SignalConnection<DragSource.DragBeginCallback> onDragBegin(DragSource.DragBeginCallback handler) Emitted on the drag source when a drag is started.It can be used to e.g. set a custom drag icon with
setIcon(org.gnome.gdk.Paintable, int, int).- Parameters:
handler- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitDragBegin
Emits the "drag-begin" signal. SeeonDragBegin(org.gnome.gtk.DragSource.DragBeginCallback). -
onDragCancel
public SignalConnection<DragSource.DragCancelCallback> onDragCancel(DragSource.DragCancelCallback handler) Emitted on the drag source when a drag has failed.The signal handler may handle a failed drag operation based on the type of error. It should return
trueif the failure has been handled and the default "drag operation failed" animation should not be shown.- Parameters:
handler- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitDragCancel
Emits the "drag-cancel" signal. SeeonDragCancel(org.gnome.gtk.DragSource.DragCancelCallback). -
onDragEnd
Emitted on the drag source when a drag is finished.A typical reason to connect to this signal is to undo things done in
Gtk.DragSource::prepareorGtk.DragSource::drag-beginhandlers.- Parameters:
handler- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitDragEnd
Emits the "drag-end" signal. SeeonDragEnd(org.gnome.gtk.DragSource.DragEndCallback). -
onPrepare
Emitted when a drag is about to be initiated.It returns the
GdkContentProviderto use for the drag that is about to start. The default handler for this signal returns the value of theGtk.DragSource:contentproperty, so if you set up that property ahead of time, you don't need to connect to this signal.- Parameters:
handler- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitPrepare
Emits the "prepare" signal. SeeonPrepare(org.gnome.gtk.DragSource.PrepareCallback). -
builder
ADragSource.Builderobject constructs aDragSourcewith the specified properties. Use the variousset...()methods to set properties, and finish construction withDragSource.Builder.build().- Returns:
- the builder object
-