7 gint delete_event( GtkWidget *widget,
15 /* Make a new hbox filled with button-labels. Arguments for the
16 * variables we're interested are passed in to this function.
17 * We do not show the box, but do show everything inside. */
18 GtkWidget *make_box( gboolean homogeneous,
28 /* Create a new hbox with the appropriate homogeneous
29 * and spacing settings */
30 box = gtk_hbox_new (homogeneous, spacing);
32 /* Create a series of buttons with the appropriate settings */
33 button = gtk_button_new_with_label ("gtk_box_pack");
34 gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
35 gtk_widget_show (button);
37 button = gtk_button_new_with_label ("(box,");
38 gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
39 gtk_widget_show (button);
41 button = gtk_button_new_with_label ("button,");
42 gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
43 gtk_widget_show (button);
45 /* Create a button with the label depending on the value of
48 button = gtk_button_new_with_label ("TRUE,");
50 button = gtk_button_new_with_label ("FALSE,");
52 gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
53 gtk_widget_show (button);
55 /* This is the same as the button creation for "expand"
56 * above, but uses the shorthand form. */
57 button = gtk_button_new_with_label (fill ? "TRUE," : "FALSE,");
58 gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
59 gtk_widget_show (button);
61 sprintf (padstr, "%d);", padding);
63 button = gtk_button_new_with_label (padstr);
64 gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
65 gtk_widget_show (button);
82 /* Our init, don't forget this! :) */
83 gtk_init (&argc, &argv);
86 fprintf (stderr, "usage: packbox num, where num is 1, 2, or 3.\n");
87 /* This just does cleanup in GTK and exits with an exit status of 1. */
91 which = atoi (argv[1]);
93 /* Create our window */
94 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
96 /* You should always remember to connect the delete_event signal
97 * to the main window. This is very important for proper intuitive
99 g_signal_connect (G_OBJECT (window), "delete_event",
100 G_CALLBACK (delete_event), NULL);
101 gtk_container_set_border_width (GTK_CONTAINER (window), 10);
103 /* We create a vertical box (vbox) to pack the horizontal boxes into.
104 * This allows us to stack the horizontal boxes filled with buttons one
105 * on top of the other in this vbox. */
106 box1 = gtk_vbox_new (FALSE, 0);
108 /* which example to show. These correspond to the pictures above. */
111 /* create a new label. */
112 label = gtk_label_new ("gtk_hbox_new (FALSE, 0);");
114 /* Align the label to the left side. We'll discuss this function and
115 * others in the section on Widget Attributes. */
116 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
118 /* Pack the label into the vertical box (vbox box1). Remember that
119 * widgets added to a vbox will be packed one on top of the other in
121 gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
124 gtk_widget_show (label);
126 /* Call our make box function - homogeneous = FALSE, spacing = 0,
127 * expand = FALSE, fill = FALSE, padding = 0 */
128 box2 = make_box (FALSE, 0, FALSE, FALSE, 0);
129 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
130 gtk_widget_show (box2);
132 /* Call our make box function - homogeneous = FALSE, spacing = 0,
133 * expand = TRUE, fill = FALSE, padding = 0 */
134 box2 = make_box (FALSE, 0, TRUE, FALSE, 0);
135 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
136 gtk_widget_show (box2);
138 /* Args are: homogeneous, spacing, expand, fill, padding */
139 box2 = make_box (FALSE, 0, TRUE, TRUE, 0);
140 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
141 gtk_widget_show (box2);
143 /* Creates a separator, we'll learn more about these later,
144 * but they are quite simple. */
145 separator = gtk_hseparator_new ();
147 /* Pack the separator into the vbox. Remember each of these
148 * widgets is being packed into a vbox, so they'll be stacked
150 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
151 gtk_widget_show (separator);
153 /* Create another new label, and show it. */
154 label = gtk_label_new ("gtk_hbox_new (TRUE, 0);");
155 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
156 gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
157 gtk_widget_show (label);
159 /* Args are: homogeneous, spacing, expand, fill, padding */
160 box2 = make_box (TRUE, 0, TRUE, FALSE, 0);
161 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
162 gtk_widget_show (box2);
164 /* Args are: homogeneous, spacing, expand, fill, padding */
165 box2 = make_box (TRUE, 0, TRUE, TRUE, 0);
166 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
167 gtk_widget_show (box2);
169 /* Another new separator. */
170 separator = gtk_hseparator_new ();
171 /* The last 3 arguments to gtk_box_pack_start are:
172 * expand, fill, padding. */
173 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
174 gtk_widget_show (separator);
180 /* Create a new label, remember box1 is a vbox as created
181 * near the beginning of main() */
182 label = gtk_label_new ("gtk_hbox_new (FALSE, 10);");
183 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
184 gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
185 gtk_widget_show (label);
187 /* Args are: homogeneous, spacing, expand, fill, padding */
188 box2 = make_box (FALSE, 10, TRUE, FALSE, 0);
189 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
190 gtk_widget_show (box2);
192 /* Args are: homogeneous, spacing, expand, fill, padding */
193 box2 = make_box (FALSE, 10, TRUE, TRUE, 0);
194 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
195 gtk_widget_show (box2);
197 separator = gtk_hseparator_new ();
198 /* The last 3 arguments to gtk_box_pack_start are:
199 * expand, fill, padding. */
200 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
201 gtk_widget_show (separator);
203 label = gtk_label_new ("gtk_hbox_new (FALSE, 0);");
204 gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
205 gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
206 gtk_widget_show (label);
208 /* Args are: homogeneous, spacing, expand, fill, padding */
209 box2 = make_box (FALSE, 0, TRUE, FALSE, 10);
210 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
211 gtk_widget_show (box2);
213 /* Args are: homogeneous, spacing, expand, fill, padding */
214 box2 = make_box (FALSE, 0, TRUE, TRUE, 10);
215 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
216 gtk_widget_show (box2);
218 separator = gtk_hseparator_new ();
219 /* The last 3 arguments to gtk_box_pack_start are: expand, fill, padding. */
220 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
221 gtk_widget_show (separator);
226 /* This demonstrates the ability to use gtk_box_pack_end() to
227 * right justify widgets. First, we create a new box as before. */
228 box2 = make_box (FALSE, 0, FALSE, FALSE, 0);
230 /* Create the label that will be put at the end. */
231 label = gtk_label_new ("end");
232 /* Pack it using gtk_box_pack_end(), so it is put on the right
233 * side of the hbox created in the make_box() call. */
234 gtk_box_pack_end (GTK_BOX (box2), label, FALSE, FALSE, 0);
235 /* Show the label. */
236 gtk_widget_show (label);
238 /* Pack box2 into box1 (the vbox remember ? :) */
239 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
240 gtk_widget_show (box2);
242 /* A separator for the bottom. */
243 separator = gtk_hseparator_new ();
244 /* This explicitly sets the separator to 400 pixels wide by 5 pixels
245 * high. This is so the hbox we created will also be 400 pixels wide,
246 * and the "end" label will be separated from the other labels in the
247 * hbox. Otherwise, all the widgets in the hbox would be packed as
248 * close together as possible. */
249 gtk_widget_set_size_request (separator, 400, 5);
250 /* pack the separator into the vbox (box1) created near the start
252 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
253 gtk_widget_show (separator);
256 /* Create another new hbox.. remember we can use as many as we need! */
257 quitbox = gtk_hbox_new (FALSE, 0);
259 /* Our quit button. */
260 button = gtk_button_new_with_label ("Quit");
262 /* Setup the signal to terminate the program when the button is clicked */
263 g_signal_connect_swapped (G_OBJECT (button), "clicked",
264 G_CALLBACK (gtk_main_quit),
266 /* Pack the button into the quitbox.
267 * The last 3 arguments to gtk_box_pack_start are:
268 * expand, fill, padding. */
269 gtk_box_pack_start (GTK_BOX (quitbox), button, TRUE, FALSE, 0);
270 /* pack the quitbox into the vbox (box1) */
271 gtk_box_pack_start (GTK_BOX (box1), quitbox, FALSE, FALSE, 0);
273 /* Pack the vbox (box1) which now contains all our widgets, into the
275 gtk_container_add (GTK_CONTAINER (window), box1);
277 /* And show everything left */
278 gtk_widget_show (button);
279 gtk_widget_show (quitbox);
281 gtk_widget_show (box1);
282 /* Showing the window last so everything pops up at once. */
283 gtk_widget_show (window);
285 /* And of course, our main function. */
288 /* Control returns here when gtk_main_quit() is called, but not when