Migrating from other containers to CtkGrid

CtkBox versus CtkGrid: packing
CtkBox versus CtkGrid: sizing
CtkBox versus CtkGrid: spacing

CtkGrid is an attempt to write a comprehensive, legacy-free, box-layout container that is flexible enough to replace CtkBox, CtkTable and the like.

The layout model of CtkGrid is to arrange its children in rows and columns. This is done by assigning positions on a two-dimentions grid that stretches arbitrarily far in all directions. Children can span multiple rows or columns, too.

CtkBox versus CtkGrid: packing

CtkBox works by arranging child widgets in a single line, either horizontally or vertically. It allows packing children from the beginning or end, using ctk_box_pack_start() and ctk_box_pack_end().

Example 53. A simple box

  box = ctk_box_new (CTK_ORIENTATION_HORIZONTAL, 0);

  ctk_box_pack_start (CTK_BOX (box), ctk_label_new ("One"), FALSE, FALSE, 0);
  ctk_box_pack_start (CTK_BOX (box), ctk_label_new ("Two"), FALSE, FALSE, 0);
    

This can be done with CtkGrid as follows:

  grid = ctk_grid_new ();

  child1 = ctk_label_new ("One");
  ctk_grid_attach (CTK_GRID (grid), child1, 0, 0, 1, 1);
  child2 = ctk_label_new ("Two");
  ctk_grid_attach_next_to (CTK_GRID (grid), child2, child1, CTK_POS_RIGHT, 1, 1);
    

And similarly for ctk_box_pack_end(). In that case, you would use CTK_POS_LEFT to place the grid children from left to right.

If you only need to pack children from the start, using ctk_container_add() is an even simpler alternative. CtkGrid places children added with ctk_container_add() in a single row or column according to its “orientation”.


One difference to keep in mind is that the ctk_box_pack_start/pack_end functions allow you to place an arbitrary number of children from either end without ever 'colliding in the middle'. With CtkGrid, you have to leave enough space between the two ends, if you want to combine packing from both ends towards the middle. In practice, this should be easy to avoid; and CtkGrid simply ignores entirely empty rows or columns for layout and spacing.

On the other hand, CtkGrid is more flexible in that its grid extends indefinitively in both directions — there is no problem with using negative numbers for the grid positions. So, if you discover that you need to place a widget before your existing arrangement, you always can.