1.92 <GtkGrid>

Class: <GtkGrid>

It derives from <GtkWidget>.

Arranges its child widgets in rows and columns.

It supports arbitrary positions and horizontal/vertical spans.

Children are added using grid:attach. They can span multiple rows or columns. It is also possible to add a child next to an existing child, using grid:attach-next-to. To remove a child from the grid, use grid:remove.

The behaviour of <GtkGrid> when several children occupy the same grid cell is undefined.

GtkGrid as GtkBuildable

Every child in a <GtkGrid> has access to a custom <GtkBuildable> element, called <layout>. It can by used to specify a position in the grid and optionally spans. All properties that can be used in the <layout> element are implemented by <GtkGridLayoutChild>.

It is implemented by <GtkWidget> using <GtkLayoutManager>.

To showcase it, here is a simple example:

<object class="GtkGrid" id="my_grid">
  <child>
    <object class="GtkButton" id="button1">
      <property name="label">Button 1</property>
      <layout>
        <property name="column">0</property>
        <property name="row">0</property>
      </layout>
    </object>
  </child>
  <child>
    <object class="GtkButton" id="button2">
      <property name="label">Button 2</property>
      <layout>
        <property name="column">1</property>
        <property name="row">0</property>
      </layout>
    </object>
  </child>
  <child>
    <object class="GtkButton" id="button3">
      <property name="label">Button 3</property>
      <layout>
        <property name="column">2</property>
        <property name="row">0</property>
        <property name="row-span">2</property>
      </layout>
    </object>
  </child>
  <child>
    <object class="GtkButton" id="button4">
      <property name="label">Button 4</property>
      <layout>
        <property name="column">0</property>
        <property name="row">1</property>
        <property name="column-span">2</property>
      </layout>
    </object>
  </child>
</object>

It organizes the first two buttons side-by-side in one cell each. The third button is in the last column but spans across two rows. This is defined by the row-span property. The last button is located in the second row and spans across two columns, which is defined by the column-span property.

CSS nodes

<GtkGrid> uses a single CSS node with name grid.

Accessibility

Until GTK 4.10, <GtkGrid> used the Gtk.AccessibleRole.group role.

Starting from GTK 4.12, <GtkGrid> uses the Gtk.AccessibleRole.generic role.

Properties

Property: grid:baseline-row self

The row to align to the baseline when valign is using baseline alignment.

Its value is a 32-bit integer.

Set it with (set! (grid:baseline-row self) value).

Property: grid:column-homogeneous self

If #t, the columns are all the same width.

Its value is a boolean.

Set it with (set! (grid:column-homogeneous self) value).

Property: grid:column-spacing self

The amount of space between two consecutive columns.

Its value is a 32-bit integer.

Set it with (set! (grid:column-spacing self) value).

Property: grid:row-homogeneous self

If #t, the rows are all the same height.

Its value is a boolean.

Set it with (set! (grid:row-homogeneous self) value).

Property: grid:row-spacing self

The amount of space between two consecutive rows.

Its value is a 32-bit integer.

Set it with (set! (grid:row-spacing self) value).

Procedures

Method: grid:attach self child column row width height
Method: attach self child column row width height

Adds a widget to the grid.

The position of child is determined by column and row. The number of “cells” that child will occupy is determined by width and height.

self

Passed: a <GtkGrid>.

a <GtkGrid>

child

Passed: a <GtkWidget>.

the widget to add

column

Passed: a 32-bit integer.

the column number to attach the left side of child to

row

Passed: a 32-bit integer.

the row number to attach the top side of child to

width

Passed: a 32-bit integer.

the number of columns that child will span

height

Passed: a 32-bit integer.

the number of rows that child will span

Method: grid:attach-next-to self child sibling side width height
Method: attach-next-to self child sibling side width height

Adds a widget to the grid.

The widget is placed next to sibling, on the side determined by side. When sibling is #f, the widget is placed in row (for left or right placement) or column 0 (for top or bottom placement), at the end indicated by side.

Attaching widgets labeled [1], [2], [3] with @sibling == %NULL and @side == %GTK_POS_LEFT yields a layout of [3][2][1].

self

Passed: a <GtkGrid>.

a <GtkGrid>

child

Passed: a <GtkWidget>.

the widget to add

sibling

Passed: a <GtkWidget>, or #f.

the child of self that child will be placed next to, or #f to place child at the beginning or end

side

Passed: a <GtkPositionType>, or one of its symbols.

the side of sibling that child is positioned next to

width

Passed: a 32-bit integer.

the number of columns that child will span

height

Passed: a 32-bit integer.

the number of rows that child will span

Method: grid:get-baseline-row self ⇒ %return
Method: get-baseline-row self ⇒ %return

Returns which row defines the global baseline of self.

self

Passed: a <GtkGrid>.

a <GtkGrid>

%return

Returned: a 32-bit integer.

the row index defining the global baseline

Method: grid:get-child-at self column row ⇒ %return
Method: get-child-at self column row ⇒ %return

Gets the child of self whose area covers the grid cell at column, row.

self

Passed: a <GtkGrid>.

a <GtkGrid>

column

Passed: a 32-bit integer.

the left edge of the cell

row

Passed: a 32-bit integer.

the top edge of the cell

%return

Returned: a <GtkWidget>, or #f.

the child at the given position

Method: grid:get-column-homogeneous? self ⇒ %return
Method: get-column-homogeneous? self ⇒ %return

Returns whether all columns of self have the same width.

self

Passed: a <GtkGrid>.

a <GtkGrid>

%return

Returned: a boolean.

whether all columns of self have the same width.

Method: grid:get-column-spacing self ⇒ %return
Method: get-column-spacing self ⇒ %return

Returns the amount of space between the columns of self.

self

Passed: a <GtkGrid>.

a <GtkGrid>

%return

Returned: an unsigned 32-bit integer.

the column spacing of self

Method: grid:get-row-baseline-position self row ⇒ %return
Method: get-row-baseline-position self row ⇒ %return

Returns the baseline position of row.

See grid:set-row-baseline-position.

self

Passed: a <GtkGrid>.

a <GtkGrid>

row

Passed: a 32-bit integer.

a row index

%return

Returned: a <GtkBaselinePosition>.

the baseline position of row

Method: grid:get-row-homogeneous? self ⇒ %return
Method: get-row-homogeneous? self ⇒ %return

Returns whether all rows of self have the same height.

self

Passed: a <GtkGrid>.

a <GtkGrid>

%return

Returned: a boolean.

whether all rows of self have the same height.

Method: grid:get-row-spacing self ⇒ %return
Method: get-row-spacing self ⇒ %return

Returns the amount of space between the rows of self.

self

Passed: a <GtkGrid>.

a <GtkGrid>

%return

Returned: an unsigned 32-bit integer.

the row spacing of self

Method: grid:insert-column self position
Method: insert-column self position

Inserts a column at the specified position.

Children which are attached at or to the right of this position are moved one column to the right. Children which span across this position are grown to span the new column.

self

Passed: a <GtkGrid>.

a <GtkGrid>

position

Passed: a 32-bit integer.

the position to insert the column at

Method: grid:insert-next-to self sibling side
Method: insert-next-to self sibling side

Inserts a row or column at the specified position.

The new row or column is placed next to sibling, on the side determined by side. If side is top or bottom, a row is inserted. If side is left of right, a column is inserted.

self

Passed: a <GtkGrid>.

a <GtkGrid>

sibling

Passed: a <GtkWidget>.

the child of self that the new row or column will be placed next to

side

Passed: a <GtkPositionType>, or one of its symbols.

the side of sibling that child is positioned next to

Method: grid:insert-row self position
Method: insert-row self position

Inserts a row at the specified position.

Children which are attached at or below this position are moved one row down. Children which span across this position are grown to span the new row.

self

Passed: a <GtkGrid>.

a <GtkGrid>

position

Passed: a 32-bit integer.

the position to insert the row at

Procedure: grid:new ⇒ %return

Creates a new grid widget.

%return

Returned: a <GtkWidget>.

the new <GtkGrid>

Method: grid:query-child self child ⇒ column row width height
Method: query-child self child ⇒ column row width height

Queries the attach points and spans of child inside the given <GtkGrid>.

self

Passed: a <GtkGrid>.

a <GtkGrid>

child

Passed: a <GtkWidget>.

a <GtkWidget> child of self

column

Returned: a 32-bit integer.

the column used to attach the left side of child

row

Returned: a 32-bit integer.

the row used to attach the top side of child

width

Returned: a 32-bit integer.

the number of columns child spans

height

Returned: a 32-bit integer.

the number of rows child spans

Method: grid:remove self child
Method: remove self child

Removes a child from self.

The child must have been added with grid:attach or grid:attach-next-to.

self

Passed: a <GtkGrid>.

a <GtkGrid>

child

Passed: a <GtkWidget>.

the child widget to remove

Method: grid:remove-column self position
Method: remove-column self position

Removes a column from the grid.

Children that are placed in this column are removed, spanning children that overlap this column have their width reduced by one, and children after the column are moved to the left.

self

Passed: a <GtkGrid>.

a <GtkGrid>

position

Passed: a 32-bit integer.

the position of the column to remove

Method: grid:remove-row self position
Method: remove-row self position

Removes a row from the grid.

Children that are placed in this row are removed, spanning children that overlap this row have their height reduced by one, and children below the row are moved up.

self

Passed: a <GtkGrid>.

a <GtkGrid>

position

Passed: a 32-bit integer.

the position of the row to remove

Method: grid:set-baseline-row self row
Method: set-baseline-row self row

Sets which row defines the global baseline for the entire grid.

Each row in the grid can have its own local baseline, but only one of those is global, meaning it will be the baseline in the parent of the self.

self

Passed: a <GtkGrid>.

a <GtkGrid>

row

Passed: a 32-bit integer.

the row index

Method: grid:set-column-homogeneous self homogeneous
Method: set-column-homogeneous self homogeneous

Sets whether all columns of self will have the same width.

self

Passed: a <GtkGrid>.

a <GtkGrid>

homogeneous

Passed: a boolean.

#t to make columns homogeneous

Method: grid:set-column-spacing self spacing
Method: set-column-spacing self spacing

Sets the amount of space between columns of self.

self

Passed: a <GtkGrid>.

a <GtkGrid>

spacing

Passed: an unsigned 32-bit integer.

the amount of space to insert between columns

Method: grid:set-row-baseline-position self row pos
Method: set-row-baseline-position self row pos

Sets how the baseline should be positioned on row of the grid, in case that row is assigned more space than is requested.

The default baseline position is center.

self

Passed: a <GtkGrid>.

a <GtkGrid>

row

Passed: a 32-bit integer.

a row index

pos

Passed: a <GtkBaselinePosition>, or one of its symbols.

a <GtkBaselinePosition>

Method: grid:set-row-homogeneous self homogeneous
Method: set-row-homogeneous self homogeneous

Sets whether all rows of self will have the same height.

self

Passed: a <GtkGrid>.

a <GtkGrid>

homogeneous

Passed: a boolean.

#t to make rows homogeneous

Method: grid:set-row-spacing self spacing
Method: set-row-spacing self spacing

Sets the amount of space between rows of self.

self

Passed: a <GtkGrid>.

a <GtkGrid>

spacing

Passed: an unsigned 32-bit integer.

the amount of space to insert between rows