Reputation: 1
I have successfully implemented a drag and drop operation in GTK4/Python where the user drags an item from a toolbox onto a canvas by implementing a DragSource and DropTarget To improve this I need to be able to do the following whilst dragging
Is there any way I can modify these items ion the fly?
I have looked through the GTK4 documentation and can see nothing which would allow me to do this during the drag motion phase.
Upvotes: 0
Views: 34
Reputation: 474
DnD in Gtk basically works through signals. If you have already implemented it, you are probably already using some of them.
To change the widget displayed as an icon:
1 - In the "prepare" phase, create a ContentProvider with the information on how the widget displayed in the icon will be configured (in some cases it is not necessary).
2 - In the "drag-begin" phase, you will configure the widget displayed in the icon to have the characteristics you want. These characteristics can be changed whenever you want in this phase.
3 - You can create a Gtk.DropControllerMotion()
to detect when the mouse is in the drop zone, which we can call the "drop hover" phase. Once again, here you can change the characteristics of the widget displayed in the icon, but this is also where I believe you should put the code that will make the adherence to the grid points.
To make the pointer stick to grid points:
Ok, here we start to get into a really complex area. Gtk4 has no direct control over the position of the pointer, that's a function of X11/Wayland. In theory, you would need to deal with the window manager to achieve this, but I found a trick that might work.
When the pointer is inside the canvas, you hide the real pointer and draw your own pointer connecting it to the position of the real pointer. This way you can change the position of your pseudo pointer as you wish.
This will generate a series of complications in detecting the position of the clicks, since you will always have to adjust the position of the click when the position of the pseudo pointer is not the same as the real pointer, but it is certainly less complicated than dealing with the window manager.
Upvotes: 0