I’ve restarted my cross-platform GTK4 C application from scratch. It’s an application that unfolds a volume from its 3d file and draws it flattened as a net of 2d triangles, that’s the easy part that I already made.
Next I want to add interactivity. For the previous version I did that using only a drawingArea that I redraw as needed, adding visible elements to show the user what the current selection is and so on. What do I need to do if I want the user to be able to hover a piece and have it change its border or color to reflect this hover event ? And what do I need to do for the user to be able to drag a piece a move it around the drawingArea ? (make a custom widget ? another drawingArea with only the pieces that shows on top of the big one ?) Or do I need to keep using only one big drawingArea and it’s events ? Depending on the 3d model, the big DrawingArea can contain more than 100 pages (the example shown has 15 pages). Is there somewhere an example of GTK4 C application implementing drawingArea hover and/or drag ? I think I read here that it’s just a matter of event to be able to hover on a drawArea, but I didn’t find anything about dragging, if needed it would be enough for me to be able to drag the bounding box frame of the piece, does this mean that I need to put a box over the drawingArea ?
GTK doesn’t care what you draw on the GtkDrawingArea, that’s the application business. As such, it cannot do picking for you — the application has to do some math and determine which piece is under the cursor.
Basically, you’ll have to listen for motion events on the drawing area. That can be done by adding a GtkEventControllerMotion to the drawing area, or alternatively a GtkEventControllerLegacy, which will catch all kind of events.
Subscribe to the GtkEventControllerMotion::motion signal, that will give you the
y coordinates of the mouse cursor. Then you have to determine whether the cursor is above the piece or not
Thank you, for the previous version of my application I handled piece picking, movement and rotation using events on the drawingArea, one on left click, another on right click and lots of keyboards pressed events. As I saw the gtkDemo drag example I was hoping that it was possible to implement it using a drawingArea or a custom widget, but I can simply reuse my code and improve it using an event to capture mouse moves even without a button press.
Apparently, it could be helpful for me to use gtk.snapshot for each piece I draw as I can use translate on them to move the whole piece instead of redrawing everything. Browsing the gtk demos, I found that “Simple paintable” is using snapshot, so I’m going to learn from it.
For the moment I was able to implement hover on graphical elements and it works fine. Sadly even if my app works, the way I use Cairo makes it hangs from time to time, certainly because of memory management. I tried to use GtkSnapshot but didn’t understand how to use it. It reminds me that before I started using GTK4, I made a console version of my application that only uses cairo to create a pdf file and it was able to create pdfs with hundreds pages without problem, and the first GTK4 GUI version of the app that used a GtkNotebook also was able to create and edit big pdfs, but having only one page shown at a time was not handy for edition. I hope that I will be able to implement a pagination that would allow Cairo memory management to stop hanging the application. But if pagination is mentioned several times on posts about draw area and cairo, there’s no concrete example. I first planned to use one gtkDrawArea per page, that was what I did on first version on the app with a notebook, but here pages are shown altogether. I don’t get how I could handle mouse events in such context. What is frustrating is that what I implemented, hover handling works perfectly.