1/* gcc -g -Wall -O2 -o dialog-test dialog-test.c `pkg-config --cflags --libs gtk4` */
2#include <gtk/gtk.h>
3
4static GtkWidget *window;
5static GtkWidget *width_chars_spin;
6static GtkWidget *max_width_chars_spin;
7static GtkWidget *default_width_spin;
8static GtkWidget *default_height_spin;
9static GtkWidget *resizable_check;
10
11static gboolean
12set_label_idle (gpointer user_data)
13{
14 GtkLabel *label = user_data;
15 GtkNative *native = gtk_widget_get_native (GTK_WIDGET (label));
16 GdkSurface *surface = gtk_native_get_surface (self: native);
17 char *str;
18
19 str = g_strdup_printf (format: "%d x %d",
20 gdk_surface_get_width (surface),
21 gdk_surface_get_height (surface));
22 gtk_label_set_label (self: label, str);
23 g_free (mem: str);
24
25 return G_SOURCE_REMOVE;
26}
27
28static void
29layout_cb (GdkSurface *surface, int width, int height, GtkLabel *label)
30{
31 g_idle_add (function: set_label_idle, data: label);
32}
33
34static void
35show_dialog (void)
36{
37 GtkWidget *dialog;
38 GtkWidget *label;
39 int width_chars, max_width_chars, default_width, default_height;
40 gboolean resizable;
41
42 width_chars = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (width_chars_spin));
43 max_width_chars = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (max_width_chars_spin));
44 default_width = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (default_width_spin));
45 default_height = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (default_height_spin));
46 resizable = gtk_check_button_get_active (GTK_CHECK_BUTTON (resizable_check));
47
48 dialog = gtk_dialog_new_with_buttons (title: "Test", GTK_WINDOW (window),
49 flags: GTK_DIALOG_MODAL,
50 first_button_text: "_Close", GTK_RESPONSE_CANCEL,
51 NULL);
52
53 label = gtk_label_new (str: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
54 "Nulla innn urna ac dui malesuada ornare. Nullam dictum "
55 "tempor mi et tincidunt. Aliquam metus nulla, auctor "
56 "vitae pulvinar nec, egestas at mi. Class aptent taciti "
57 "sociosqu ad litora torquent per conubia nostra, per "
58 "inceptos himenaeos. Aliquam sagittis, tellus congue "
59 "cursus congue, diam massa mollis enim, sit amet gravida "
60 "magna turpis egestas sapien. Aenean vel molestie nunc. "
61 "In hac habitasse platea dictumst. Suspendisse lacinia"
62 "mi eu ipsum vestibulum in venenatis enim commodo. "
63 "Vivamus non malesuada ligula.");
64
65 gtk_label_set_wrap (GTK_LABEL (label), TRUE);
66 gtk_label_set_width_chars (GTK_LABEL (label), n_chars: width_chars);
67 gtk_label_set_max_width_chars (GTK_LABEL (label), n_chars: max_width_chars);
68 gtk_window_set_default_size (GTK_WINDOW (dialog), width: default_width, height: default_height);
69 gtk_window_set_resizable (GTK_WINDOW (dialog), resizable);
70
71
72 gtk_box_append (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
73 child: label);
74
75 label = gtk_label_new (str: "? x ?");
76
77 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), child: label, response_id: GTK_RESPONSE_HELP);
78 gtk_widget_realize (widget: dialog);
79 g_signal_connect (gtk_native_get_surface (GTK_NATIVE (dialog)), "layout",
80 G_CALLBACK (layout_cb), label);
81 g_signal_connect (dialog, "response",
82 G_CALLBACK (gtk_window_destroy),
83 NULL);
84 gtk_widget_show (widget: dialog);
85}
86
87static void
88create_window (void)
89{
90 GtkWidget *grid;
91 GtkWidget *label;
92 GtkWidget *button;
93
94 window = gtk_window_new ();
95 gtk_window_set_title (GTK_WINDOW (window), title: "Window size");
96 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
97
98 grid = gtk_grid_new ();
99 gtk_grid_set_row_spacing (GTK_GRID (grid), spacing: 12);
100 gtk_grid_set_column_spacing (GTK_GRID (grid), spacing: 12);
101 gtk_window_set_child (GTK_WINDOW (window), child: grid);
102
103 label = gtk_label_new (str: "Width chars");
104 gtk_widget_set_halign (widget: label, align: GTK_ALIGN_START);
105 width_chars_spin = gtk_spin_button_new_with_range (min: -1, max: 1000, step: 1);
106 gtk_widget_set_halign (widget: width_chars_spin, align: GTK_ALIGN_START);
107
108 gtk_grid_attach (GTK_GRID (grid), child: label, column: 0, row: 0, width: 1, height: 1);
109 gtk_grid_attach (GTK_GRID (grid), child: width_chars_spin, column: 1, row: 0, width: 1, height: 1);
110
111 label = gtk_label_new (str: "Max width chars");
112 gtk_widget_set_halign (widget: label, align: GTK_ALIGN_START);
113 max_width_chars_spin = gtk_spin_button_new_with_range (min: -1, max: 1000, step: 1);
114 gtk_widget_set_halign (widget: width_chars_spin, align: GTK_ALIGN_START);
115
116 gtk_grid_attach (GTK_GRID (grid), child: label, column: 0, row: 1, width: 1, height: 1);
117 gtk_grid_attach (GTK_GRID (grid), child: max_width_chars_spin, column: 1, row: 1, width: 1, height: 1);
118
119 label = gtk_label_new (str: "Default size");
120 gtk_widget_set_halign (widget: label, align: GTK_ALIGN_START);
121 default_width_spin = gtk_spin_button_new_with_range (min: -1, max: 1000, step: 1);
122 gtk_widget_set_halign (widget: default_width_spin, align: GTK_ALIGN_START);
123 default_height_spin = gtk_spin_button_new_with_range (min: -1, max: 1000, step: 1);
124 gtk_widget_set_halign (widget: default_height_spin, align: GTK_ALIGN_START);
125
126 gtk_grid_attach (GTK_GRID (grid), child: label, column: 0, row: 2, width: 1, height: 1);
127 gtk_grid_attach (GTK_GRID (grid), child: default_width_spin, column: 1, row: 2, width: 1, height: 1);
128 gtk_grid_attach (GTK_GRID (grid), child: default_height_spin, column: 2, row: 2, width: 1, height: 1);
129
130 label = gtk_label_new (str: "Resizable");
131 gtk_widget_set_halign (widget: label, align: GTK_ALIGN_START);
132 resizable_check = gtk_check_button_new ();
133 gtk_widget_set_halign (widget: resizable_check, align: GTK_ALIGN_START);
134
135 gtk_grid_attach (GTK_GRID (grid), child: label, column: 0, row: 3, width: 1, height: 1);
136 gtk_grid_attach (GTK_GRID (grid), child: resizable_check, column: 1, row: 3, width: 1, height: 1);
137
138 button = gtk_button_new_with_label (label: "Show");
139 g_signal_connect (button, "clicked", G_CALLBACK (show_dialog), NULL);
140 gtk_grid_attach (GTK_GRID (grid), child: button, column: 2, row: 4, width: 1, height: 1);
141
142 gtk_widget_show (widget: window);
143
144 GMainLoop *loop = g_main_loop_new (NULL, FALSE);
145
146 g_signal_connect_swapped (window, "destroy",
147 G_CALLBACK (g_main_loop_quit),
148 loop);
149
150 g_main_loop_run (loop);
151
152 g_main_loop_unref (loop);
153}
154
155int
156main (int argc, char *argv[])
157{
158 gtk_init ();
159
160 create_window ();
161
162 return 0;
163}
164

source code of gtk/tests/testwindowsize.c