]> Pileus Git - ~andy/gtk/blob - examples/packbox/packbox.c
381fa2b2e4f9380a8ad2ecb65d81388e762548db
[~andy/gtk] / examples / packbox / packbox.c
1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "gtk/gtk.h"
5
6 static gboolean delete_event( GtkWidget *widget,
7                               GdkEvent  *event,
8                               gpointer   data )
9 {
10     gtk_main_quit ();
11     return FALSE;
12 }
13
14 /* Make a new hbox filled with button-labels. Arguments for the
15  * variables we're interested are passed in to this function.
16  * We do not show the box, but do show everything inside. */
17 static GtkWidget *make_box( gboolean homogeneous,
18                             gint     spacing,
19                             gboolean expand,
20                             gboolean fill,
21                             guint    padding )
22 {
23     GtkWidget *box;
24     GtkWidget *button;
25     char padstr[80];
26
27     /* Create a new hbox with the appropriate homogeneous
28      * and spacing settings */
29     box = gtk_hbox_new (homogeneous, spacing);
30
31     /* Create a series of buttons with the appropriate settings */
32     button = gtk_button_new_with_label ("gtk_box_pack");
33     gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
34     gtk_widget_show (button);
35
36     button = gtk_button_new_with_label ("(box,");
37     gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
38     gtk_widget_show (button);
39
40     button = gtk_button_new_with_label ("button,");
41     gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
42     gtk_widget_show (button);
43
44     /* Create a button with the label depending on the value of
45      * expand. */
46     if (expand == TRUE)
47             button = gtk_button_new_with_label ("TRUE,");
48     else
49             button = gtk_button_new_with_label ("FALSE,");
50
51     gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
52     gtk_widget_show (button);
53
54     /* This is the same as the button creation for "expand"
55      * above, but uses the shorthand form. */
56     button = gtk_button_new_with_label (fill ? "TRUE," : "FALSE,");
57     gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
58     gtk_widget_show (button);
59
60     sprintf (padstr, "%d);", padding);
61
62     button = gtk_button_new_with_label (padstr);
63     gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
64     gtk_widget_show (button);
65
66     return box;
67 }
68
69 int main( int   argc,
70           char *argv[])
71 {
72     GtkWidget *window;
73     GtkWidget *button;
74     GtkWidget *box1;
75     GtkWidget *box2;
76     GtkWidget *separator;
77     GtkWidget *label;
78     GtkWidget *quitbox;
79     int which;
80
81     /* Our init, don't forget this! :) */
82     gtk_init (&argc, &argv);
83
84     if (argc != 2) {
85         fprintf (stderr, "usage: packbox num, where num is 1, 2, or 3.\n");
86         /* This just does cleanup in GTK and exits with an exit status of 1. */
87         exit (1);
88     }
89
90     which = atoi (argv[1]);
91
92     /* Create our window */
93     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
94
95     /* You should always remember to connect the "delete-event" signal
96      * to the main window. This is very important for proper intuitive
97      * behavior */
98     g_signal_connect (window, "delete-event",
99                       G_CALLBACK (delete_event), NULL);
100     gtk_container_set_border_width (GTK_CONTAINER (window), 10);
101
102     /* We create a vertical box (vbox) to pack the horizontal boxes into.
103      * This allows us to stack the horizontal boxes filled with buttons one
104      * on top of the other in this vbox. */
105     box1 = gtk_vbox_new (FALSE, 0);
106
107     /* which example to show. These correspond to the pictures above. */
108     switch (which) {
109     case 1:
110         /* create a new label. */
111         label = gtk_label_new ("gtk_hbox_new (FALSE, 0);");
112
113         /* Align the label to the left side.  We'll discuss this function and
114          * others in the section on Widget Attributes. */
115         gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
116
117         /* Pack the label into the vertical box (vbox box1).  Remember that
118          * widgets added to a vbox will be packed one on top of the other in
119          * order. */
120         gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
121
122         /* Show the label */
123         gtk_widget_show (label);
124
125         /* Call our make box function - homogeneous = FALSE, spacing = 0,
126          * expand = FALSE, fill = FALSE, padding = 0 */
127         box2 = make_box (FALSE, 0, FALSE, FALSE, 0);
128         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
129         gtk_widget_show (box2);
130
131         /* Call our make box function - homogeneous = FALSE, spacing = 0,
132          * expand = TRUE, fill = FALSE, padding = 0 */
133         box2 = make_box (FALSE, 0, TRUE, FALSE, 0);
134         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
135         gtk_widget_show (box2);
136
137         /* Args are: homogeneous, spacing, expand, fill, padding */
138         box2 = make_box (FALSE, 0, TRUE, TRUE, 0);
139         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
140         gtk_widget_show (box2);
141
142         /* Creates a separator, we'll learn more about these later,
143          * but they are quite simple. */
144         separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
145
146         /* Pack the separator into the vbox. Remember each of these
147          * widgets is being packed into a vbox, so they'll be stacked
148          * vertically. */
149         gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
150         gtk_widget_show (separator);
151
152         /* Create another new label, and show it. */
153         label = gtk_label_new ("gtk_hbox_new (TRUE, 0);");
154         gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
155         gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
156         gtk_widget_show (label);
157
158         /* Args are: homogeneous, spacing, expand, fill, padding */
159         box2 = make_box (TRUE, 0, TRUE, FALSE, 0);
160         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
161         gtk_widget_show (box2);
162
163         /* Args are: homogeneous, spacing, expand, fill, padding */
164         box2 = make_box (TRUE, 0, TRUE, TRUE, 0);
165         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
166         gtk_widget_show (box2);
167
168         /* Another new separator. */
169         separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
170         /* The last 3 arguments to gtk_box_pack_start are:
171          * expand, fill, padding. */
172         gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
173         gtk_widget_show (separator);
174
175         break;
176
177     case 2:
178
179         /* Create a new label, remember box1 is a vbox as created
180          * near the beginning of main() */
181         label = gtk_label_new ("gtk_hbox_new (FALSE, 10);");
182         gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
183         gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
184         gtk_widget_show (label);
185
186         /* Args are: homogeneous, spacing, expand, fill, padding */
187         box2 = make_box (FALSE, 10, TRUE, FALSE, 0);
188         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
189         gtk_widget_show (box2);
190
191         /* Args are: homogeneous, spacing, expand, fill, padding */
192         box2 = make_box (FALSE, 10, TRUE, TRUE, 0);
193         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
194         gtk_widget_show (box2);
195
196         separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
197         /* The last 3 arguments to gtk_box_pack_start are:
198          * expand, fill, padding. */
199         gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
200         gtk_widget_show (separator);
201
202         label = gtk_label_new ("gtk_hbox_new (FALSE, 0);");
203         gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
204         gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
205         gtk_widget_show (label);
206
207         /* Args are: homogeneous, spacing, expand, fill, padding */
208         box2 = make_box (FALSE, 0, TRUE, FALSE, 10);
209         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
210         gtk_widget_show (box2);
211
212         /* Args are: homogeneous, spacing, expand, fill, padding */
213         box2 = make_box (FALSE, 0, TRUE, TRUE, 10);
214         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
215         gtk_widget_show (box2);
216
217         separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
218         /* The last 3 arguments to gtk_box_pack_start are: expand, fill, padding. */
219         gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
220         gtk_widget_show (separator);
221         break;
222
223     case 3:
224
225         /* This demonstrates the ability to use gtk_box_pack_end() to
226          * right justify widgets. First, we create a new box as before. */
227         box2 = make_box (FALSE, 0, FALSE, FALSE, 0);
228
229         /* Create the label that will be put at the end. */
230         label = gtk_label_new ("end");
231         /* Pack it using gtk_box_pack_end(), so it is put on the right
232          * side of the hbox created in the make_box() call. */
233         gtk_box_pack_end (GTK_BOX (box2), label, FALSE, FALSE, 0);
234         /* Show the label. */
235         gtk_widget_show (label);
236
237         /* Pack box2 into box1 (the vbox remember ? :) */
238         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
239         gtk_widget_show (box2);
240
241         /* A separator for the bottom. */
242         separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
243         /* This explicitly sets the separator to 400 pixels wide by 5 pixels
244          * high. This is so the hbox we created will also be 400 pixels wide,
245          * and the "end" label will be separated from the other labels in the
246          * hbox. Otherwise, all the widgets in the hbox would be packed as
247          * close together as possible. */
248         gtk_widget_set_size_request (separator, 400, 5);
249         /* pack the separator into the vbox (box1) created near the start
250          * of main() */
251         gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
252         gtk_widget_show (separator);
253     }
254
255     /* Create another new hbox.. remember we can use as many as we need! */
256     quitbox = gtk_hbox_new (FALSE, 0);
257
258     /* Our quit button. */
259     button = gtk_button_new_with_label ("Quit");
260
261     /* Setup the signal to terminate the program when the button is clicked */
262     g_signal_connect_swapped (button, "clicked",
263                               G_CALLBACK (gtk_main_quit),
264                               window);
265     /* Pack the button into the quitbox.
266      * The last 3 arguments to gtk_box_pack_start are:
267      * expand, fill, padding. */
268     gtk_box_pack_start (GTK_BOX (quitbox), button, TRUE, FALSE, 0);
269     /* pack the quitbox into the vbox (box1) */
270     gtk_box_pack_start (GTK_BOX (box1), quitbox, FALSE, FALSE, 0);
271
272     /* Pack the vbox (box1) which now contains all our widgets, into the
273      * main window. */
274     gtk_container_add (GTK_CONTAINER (window), box1);
275
276     /* And show everything left */
277     gtk_widget_show (button);
278     gtk_widget_show (quitbox);
279
280     gtk_widget_show (box1);
281     /* Showing the window last so everything pops up at once. */
282     gtk_widget_show (window);
283
284     /* And of course, our main function. */
285     gtk_main ();
286
287     /* Control returns here when gtk_main_quit() is called, but not when
288      * exit() is used. */
289
290     return 0;
291 }