Understanding Window Sizing and Decorations on Gtk4

On gtk3 (at least at some point) frame extents was drawn by the window manager and on X11 we could use the WM _NET_FRAME_EXTENTS extension.

So, afaik it was hard to size the window to it’s whole size, accounting decorations, unless using gtk_window_set_titlebar so it was possible to know the header size (dunno about the left, right and bottom extents).

It appears that Gtk4 headed to the titlebar solution. So, the questions are:

  • Does the WM still draw the decorations? Or Gtk draws it within the Surface?
  • Same solution for X11 or Wayland, or behaviour differs?
  • How would I set the whole window size (accounting decorations) with Gtk4, using the same solution for Wayland and X11?

Thanks.

1 Like

Same as in GTK3: It depends.

  • on wayland, GTK uses client-side decorations (*)
  • on X11 when using a custom titlebar, GTK tries hard to convince the window manager to not add its own decorations
  • when not using a custom titlebar, GTK relies on the window manager to decorate the window under X11.

(*) except when running under a compositor that expresses a preference for server-side decorations. In that case the wayland backend behaves as the X11 one

Use gtk_window_set_titlebar() to use client-side decorations. As that means that decorations are part of the client window/surface, the actual window size matches GTK’s idea of the window size.

With Gtk 3.24.20 using this example below I was able to resize the window with gdk_window_resize. gtk_window_set_default_size seems to add the header bar size and shadow sizes, resulting in larger size than specified on gtk_window_set_default_size.

Is there a way to say “I want the final size to be total 800x600 (for example)”, when using header bar?

gdk_window_resize works as expected, but it’s a resize operation that needs to be done after.

#include <gtk/gtk.h>

int main(int argc, char *argv[]) {
  gtk_init(&argc, &argv);

  GtkWidget * main = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  GtkWidget *bar = gtk_header_bar_new();
  gtk_window_set_titlebar(GTK_WINDOW(main), bar); 
  gtk_window_set_title(GTK_WINDOW(main), "test");
  
  gtk_window_set_default_size(GTK_WINDOW(main), 800, 600);
  gtk_window_set_position(GTK_WINDOW(main), GTK_WIN_POS_CENTER);

  gtk_widget_show_all(main);
  gdk_window_resize(gtk_widget_get_window(main), 800, 600);
  
  g_signal_connect(main, "destroy", G_CALLBACK(gtk_main_quit), NULL);

  gtk_main();

  return 0;
}

Oh, indeed. I had only tested with GTK4 before, so it looks like that behavior changed.

Yeah, just checked with Gtk4, it changed. Nice, best gtk ever!

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