Reading through the GTK Book, there are lots of things to clean up when making sure to learn GTK3-focused skills. One is tables versus grids. My question is about the set_spacing
functions for those two widgets.
The GTK docs say the second parameter for gtk_table_set_row_spacings()
is measured in pixels. However, the docs for gtk_grid_set_row_spacing()
don't give a unit of measure for the second parameter. The same is true for setting column spacing for both widgets.
The only reason this is of any concern (aside from explicit being better?) is that you have to use very different numbers to create the same aesthetic between tables and grids. So, if grids aren't spaced by pixels, what are they spaced by?
EDIT:
This is the tutorial code as I've typed it out.
#include <gtk/gtk.h>
int main(int argc, char *argv[])
{
GtkWidget *window, *table, *label, *label2, *name;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Tables");
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
g_signal_connect(window, "destroy", gtk_main_quit, NULL);
//table = gtk_table_new(2, 2, TRUE);
table = gtk_grid_new();
label = gtk_label_new("Enter the following information...");
label2 = gtk_label_new("Name: ");
name = gtk_entry_new();
//gtk_table_attach(GTK_TABLE(table), label, 0, 2, 0, 1, GTK_EXPAND, GTK_SHRINK, 0, 0);
//gtk_table_attach(GTK_TABLE(table), label2, 0, 1, 1, 2, GTK_EXPAND, GTK_SHRINK, 0, 0);
//gtk_table_attach(GTK_TABLE(table), name, 1, 2, 1, 2, GTK_EXPAND, GTK_SHRINK, 0, 0);
gtk_grid_attach(GTK_GRID(table), label, 0, 0, 2, 1);
gtk_grid_attach(GTK_GRID(table), label2, 0, 1, 1, 1);
gtk_grid_attach(GTK_GRID(table), name, 1, 1, 1, 1);
//gtk_table_set_row_spacings(GTK_TABLE(table), 5);
//gtk_table_set_col_spacings(GTK_TABLE(table), 5);
gtk_grid_set_column_spacing(GTK_GRID(table), 5);
gtk_grid_set_row_spacing(GTK_GRID(table), 5);
gtk_container_add(GTK_CONTAINER(window), table);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
The two produce quite different results. My only guess is, if both grids and tables are pixel-padded, then I've misunderstood how grids and tables are laid out whilst converting this code.
For reference, this is compiled with the following Makefile:
BINS = $(basename $(wildcard *.c))
CC = gcc
CFLAGS += -Wall -Wextra -std=c11
GTK_DFLAGS = -DGTK_DISABLE_SINGLE_INCLUDES -DGDK_DISABLE_DEPRECATED -DGSEAL_ENABLE
GTK_CFLAGS := $(shell pkg-config --cflags gtk+-3.0)
GTK_LDFLAGS := $(shell pkg-config --libs gtk+-3.0)
all:
$(MAKE) $(BINS)
%: %.c
$(CC) $(CFLAGS) $(GTK_DFLAGS) $(GTK_CFLAGS) $(GTK_LDFLAGS) -o $@ $@.c
.PHONY: clean
clean:
rm -f $(BINS) *~
I comment out the definition of $GTK_DFLAGS
for the tables version to compile.
The columns and rows are still spaced by pixels, but there is also extra space being added because of the columns being homogeneous. (Check your call to gtk_table_new()
- TRUE
is being passed as the homogeneous
argument.)
That means that every column's width and height is equal to the largest width and height of any column. This messes up your spacing even without any row or column spacing whatsoever. So, the grid looks like this in ASCII-art:
+----------------------------------+
|Enter the following information...|
+-----+----------------------------+
|Name:|[ ]|
+-----+----------------------------+
(note that the Entry is set to expand horizontally, let's say that its minimum size is 20 spaces in ASCII-art)
But the table is homogeneous, so every column takes the width of the widest column. The widest widget is the top Label, but that's stretched over two columns; so the Entry wins. The table becomes:
+-----------------------------------------+
| Enter the following information... |
+--------------------+--------------------+
| Name: |[ ]|
+--------------------+--------------------+
(the Labels get centered in their cells, producing very funny spacing indeed.)