Custom drawing with GTK4

I need this:

Receive a paint(void *data, int width, int height) where data is in ARBG32 format and paint it to a GtkWindow. I can’t decide when paint will be called.

First try was to use a GtkDrawingArea but it decides when to paint (that got into the way). I can also call gtk_widget_queue_draw, but when it decides to paint itself I have nothing. Using the “old” buffer would not do it, because when It decides to paint the window layout probably changed.

In general, I inform the “toolkit” the layout changed and it upload pixels in the paint function.

The ideal (I think at the moment) is to have a layer positioned within the GtkWindow bounds and draw those pixels there.

This gives me some direction:
Custom widgets in GTK 4 – Drawing – GTK Development Blog.

But I still can’t formulate the “best” option.


This can be done without GtkDrawingArea, likely this is what you want to do:

  1. Store the image data in a private GdkMemoryTexture when paint is called
  2. Call gtk_widget_queue_draw at the end of paint to trigger a redraw
  3. In your snapshot vfunc, append the stored texture
  4. If you need to do something when the size changes, do it in the size_allocate vfunc

If the old buffer becomes stale then I’m not sure what you would do. Maybe you can just draw nothing if there is no buffer available? You need to either do that, or always have a texture stored because Gtk widgets can be redrawn at any time.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.