Why did margin property disappeared in GTK4?


Probably was asked before, but I am out of thinking why there is no “margin” property anymore.

I am 100% sure that writing:

g_object_set ( toggle_button,
               "margin-start",  50,
               "margin-top",    50,
               "margin-end",    50,
               "margin-bottom", 50,
               NULL );

it is not easier to be written than:

g_object_set ( toggle_button,
               "margin",  50,
               NULL );

If one reads Migrating from GTK3 to GTK4 at the Stop using GtkBox padding, fill and expand child properties Section there is a part where it is explained about “margin” which does not help me at all:

GTK 4 removes these GtkBox child properties, so you should stop using them. You can replace GtkBox:padding using the “margin” properties on your GtkBox child widgets.

Could someone please explain me whats the story with in “margin” property in GTK4?

1 Like

The margin property was removed because we can’t have a shorthand version, like CSS does; in other words, we cannot have:

// 20px on every direction
g_object_set (widget, "margin", 20, NULL);

// 20px vertical direction, 30px horizontal direction
g_object_set (widget, "margin", 20, 30, NULL);

// different margins for each directions
g_object_set (widget, "margin", 10, 20, 30, 40, NULL);

An alternative would be to have margin-{start|end|top|bottom} properties, and a margin property that set them all to the same value; but the margin property would need to be write-only, as it wouldn’t make sense to read it.

In practice, margins are really more commonly set using GtkBuilder XML files; and even there, it’s less often the case of same margin in all directions. For instance, the padding in an horizontal Box would be set by the two margin-start and margin-end properties, whereas the padding in a vertical Box would be set by the two margin-top and margin-bottom properties.

Finally: margin properties are hard coded, and part of your layout. If you want to leave it to the theme, for instance to increase affordances for accessibility, you should be using CSS.

1 Like

I have recently finally added a setMargin() procedure manually for the Nim bindings:

proc setMargin*(w: Widget; m: int) =
  setMarginStart(w, m)
  setMarginEnd(w, m)
  setMarginTop(w, m)
  setMarginBottom(w, m)

Because I had to set margins in some of the examples in the GTK4 Nim book to make widgets and their borders better visible, and I was not willing to waste four lines for that.


This is exactly the same thing I was thinking about too, having a function which calls all 4:

static void set_margin ( GObject *const object, const gint margin )
    g_object_set ( object,
                   "margin-start",  margin,
                   "margin-top",    margin,
                   "margin-end",    margin,
                   "margin-bottom", margin,
                   NULL );

and using it something like this:
set_margin ( G_OBJECT( toggle_button ), 50 );

1 Like

I use it all the time, but I am preparing GTK4 tutorials for my Channel and needed an explanation.
Thank you

Seems similar to our thinking here for the Fortran bindings:

1 Like

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