| 1 |
.. currentmodule:: gi.repository |
| 2 |
|
| 3 |
Drag and Drop |
| 4 |
============= |
| 5 |
|
| 6 |
.. seealso:: `Drag-and-Drop`_ in the GTK documentation. |
| 7 |
|
| 8 |
GTK drag-and-drop works with drag sources and drop targets. |
| 9 |
These are event controllers that can be set to any widget using |
| 10 |
:meth:`Gtk.Widget.add_controller`. The data begin moved in the operation is |
| 11 |
provided through a :class:`Gdk.ContentProvider`. |
| 12 |
|
| 13 |
|
| 14 |
Drag sources |
| 15 |
------------ |
| 16 |
:class:`Gtk.DragSource` is the event controller that allows a widget to be used |
| 17 |
as a drag source. |
| 18 |
You can set up everything needed for the drag-and-drop operation ahead of time |
| 19 |
or do it on the fly using the signals provided by :class:`Gtk.DragSource`. |
| 20 |
|
| 21 |
You can use :meth:`Gtk.DragSource.set_content` to set the |
| 22 |
:class:`Gdk.ContentProvider` that will be sent to drop targets. |
| 23 |
A content provider is usually created using |
| 24 |
:meth:`Gdk.ContentProvider.new_for_value` where you only pass the value to send. |
| 25 |
To pass different values for multiple possible targets you can use |
| 26 |
:meth:`Gdk.ContentProvider.new_union` were you can pass a list of |
| 27 |
:class:`Gdk.ContentProviders <Gdk.ContentProvider>`. |
| 28 |
|
| 29 |
:class:`Gtk.DragSource` provides signals for the different stages of the drag |
| 30 |
event. |
| 31 |
The :func:`prepare <Gtk.DragSource.signals.prepare>` signal is emitted when a |
| 32 |
drag is about to be initiated, here you should return the |
| 33 |
:class:`Gdk.ContentProvider` that will be sent, otherwise the one set with |
| 34 |
:meth:`Gtk.DragSource.set_content` will be used. |
| 35 |
The :func:`drag-begin <Gtk.DragSource.signals.drag_begin>` signal is emitted |
| 36 |
when the drag is started, here you can do things like changing the icon attached |
| 37 |
to the cursor when dragging using :meth:`Gtk.DragSource.set_icon`. |
| 38 |
Also the :func:`drag-end <Gtk.DragSource.signals.drag_end>` signal is provided, |
| 39 |
you can use it to undo things done in the previous signals. |
| 40 |
Finally :func:`drag-cancel <Gtk.DragSource.signals.drag_cancel>` allows you |
| 41 |
to do things when the operation has been cancelled. |
| 42 |
|
| 43 |
Drop targets |
| 44 |
------------ |
| 45 |
:class:`Gtk.DropTarget` is the event controller to receive drag-and-drop |
| 46 |
operations in a widget. |
| 47 |
When creating a new drop target with :meth:`Gtk.DropTarget.new` you should |
| 48 |
provide the data type and :class:`Gdk.DragAction` that your target accepts. |
| 49 |
If you want to support multiple data types you can pass :attr:`GObject.TYPE_NONE` |
| 50 |
and then use :meth:`Gtk.DropTarget.set_gtypes` where you can pass a list of |
| 51 |
types, be aware that the order of the list establish the priorities. |
| 52 |
|
| 53 |
:class:`Gtk.DropTarget` provides multiple signals for the process. These are |
| 54 |
:func:`accept <Gtk.DropTarget.signals.accept>`, |
| 55 |
:func:`drop <Gtk.DropTarget.signals.drop>`, |
| 56 |
:func:`enter <Gtk.DropTarget.signals.enter>`, |
| 57 |
:func:`leave <Gtk.DropTarget.signals.leave>`, and |
| 58 |
:func:`motion <Gtk.DropTarget.signals.motion>`. |
| 59 |
Generally connecting to :func:`drop <Gtk.DropTarget.signals.drop>` is only |
| 60 |
needed, this signal will receive the value sended by the :class:`Gtk.DragSource`. |
| 61 |
|
| 62 |
For more complex use cases checkout :class:`Gtk.DropTargetAsync`. |
| 63 |
|
| 64 |
Example |
| 65 |
------- |
| 66 |
|
| 67 |
.. image:: images/drag_and_drop.png |
| 68 |
|
| 69 |
.. literalinclude:: examples/drag_and_drop.py |
| 70 |
:linenos: |
| 71 |
|
| 72 |
|
| 73 |
.. _Drag-and-Drop: https://docs.gtk.org/gtk4/drag-and-drop.html |