1#include <gtk/gtk.h>
2
3static void
4print_hello (GtkWidget *widget,
5 gpointer data)
6{
7 g_print (format: "Hello World\n");
8}
9
10static void
11activate (GtkApplication *app,
12 gpointer user_data)
13{
14 GtkWidget *window;
15 GtkWidget *grid;
16 GtkWidget *button;
17
18 /* create a new window, and set its title */
19 window = gtk_application_window_new (application: app);
20 gtk_window_set_title (GTK_WINDOW (window), title: "Window");
21
22 /* Here we construct the container that is going pack our buttons */
23 grid = gtk_grid_new ();
24
25 /* Pack the container in the window */
26 gtk_window_set_child (GTK_WINDOW (window), child: grid);
27
28 button = gtk_button_new_with_label (label: "Button 1");
29 g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
30
31 /* Place the first button in the grid cell (0, 0), and make it fill
32 * just 1 cell horizontally and vertically (ie no spanning)
33 */
34 gtk_grid_attach (GTK_GRID (grid), child: button, column: 0, row: 0, width: 1, height: 1);
35
36 button = gtk_button_new_with_label (label: "Button 2");
37 g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
38
39 /* Place the second button in the grid cell (1, 0), and make it fill
40 * just 1 cell horizontally and vertically (ie no spanning)
41 */
42 gtk_grid_attach (GTK_GRID (grid), child: button, column: 1, row: 0, width: 1, height: 1);
43
44 button = gtk_button_new_with_label (label: "Quit");
45 g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_window_destroy), window);
46
47 /* Place the Quit button in the grid cell (0, 1), and make it
48 * span 2 columns.
49 */
50 gtk_grid_attach (GTK_GRID (grid), child: button, column: 0, row: 1, width: 2, height: 1);
51
52 gtk_widget_show (widget: window);
53
54}
55
56int
57main (int argc,
58 char **argv)
59{
60 GtkApplication *app;
61 int status;
62
63 app = gtk_application_new (application_id: "org.gtk.example", flags: G_APPLICATION_FLAGS_NONE);
64 g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
65 status = g_application_run (G_APPLICATION (app), argc, argv);
66 g_object_unref (object: app);
67
68 return status;
69}
70

source code of gtk/examples/grid-packing.c