]> Pileus Git - ~andy/gtk/blob - examples/packbox/packbox.c
Fixes #136082 and #135265, patch by Morten Welinder.
[~andy/gtk] / examples / packbox / packbox.c
1
2 #include <config.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include "gtk/gtk.h"
6
7 gint delete_event( GtkWidget *widget,
8                    GdkEvent  *event,
9                    gpointer   data )
10 {
11     gtk_main_quit ();
12     return FALSE;
13 }
14
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,
19                      gint     spacing,
20                      gboolean expand,
21                      gboolean fill,
22                      guint    padding ) 
23 {
24     GtkWidget *box;
25     GtkWidget *button;
26     char padstr[80];
27     
28     /* Create a new hbox with the appropriate homogeneous
29      * and spacing settings */
30     box = gtk_hbox_new (homogeneous, spacing);
31     
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);
36     
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);
40     
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);
44     
45     /* Create a button with the label depending on the value of
46      * expand. */
47     if (expand == TRUE)
48             button = gtk_button_new_with_label ("TRUE,");
49     else
50             button = gtk_button_new_with_label ("FALSE,");
51     
52     gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
53     gtk_widget_show (button);
54     
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);
60     
61     sprintf (padstr, "%d);", padding);
62     
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);
66     
67     return box;
68 }
69
70 int main( int   argc,
71           char *argv[]) 
72 {
73     GtkWidget *window;
74     GtkWidget *button;
75     GtkWidget *box1;
76     GtkWidget *box2;
77     GtkWidget *separator;
78     GtkWidget *label;
79     GtkWidget *quitbox;
80     int which;
81     
82     /* Our init, don't forget this! :) */
83     gtk_init (&argc, &argv);
84     
85     if (argc != 2) {
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. */
88         exit (1);
89     }
90     
91     which = atoi (argv[1]);
92
93     /* Create our window */
94     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
95
96     /* You should always remember to connect the delete_event signal
97      * to the main window. This is very important for proper intuitive
98      * behavior */
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);
102     
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);
107     
108     /* which example to show. These correspond to the pictures above. */
109     switch (which) {
110     case 1:
111         /* create a new label. */
112         label = gtk_label_new ("gtk_hbox_new (FALSE, 0);");
113         
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);
117
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
120          * order. */
121         gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
122         
123         /* Show the label */
124         gtk_widget_show (label);
125         
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);
131
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);
137         
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);
142         
143         /* Creates a separator, we'll learn more about these later, 
144          * but they are quite simple. */
145         separator = gtk_hseparator_new ();
146         
147         /* Pack the separator into the vbox. Remember each of these
148          * widgets is being packed into a vbox, so they'll be stacked
149          * vertically. */
150         gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
151         gtk_widget_show (separator);
152         
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);
158         
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);
163         
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);
168         
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);
175         
176         break;
177
178     case 2:
179
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);
186         
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);
191         
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);
196         
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);
202         
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);
207         
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);
212         
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);
217         
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);
222         break;
223     
224     case 3:
225
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);
229
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);
237         
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);
241         
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 
251          * of main() */
252         gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
253         gtk_widget_show (separator);    
254     }
255     
256     /* Create another new hbox.. remember we can use as many as we need! */
257     quitbox = gtk_hbox_new (FALSE, 0);
258     
259     /* Our quit button. */
260     button = gtk_button_new_with_label ("Quit");
261     
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),
265                               G_OBJECT (window));
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);
272     
273     /* Pack the vbox (box1) which now contains all our widgets, into the
274      * main window. */
275     gtk_container_add (GTK_CONTAINER (window), box1);
276     
277     /* And show everything left */
278     gtk_widget_show (button);
279     gtk_widget_show (quitbox);
280     
281     gtk_widget_show (box1);
282     /* Showing the window last so everything pops up at once. */
283     gtk_widget_show (window);
284     
285     /* And of course, our main function. */
286     gtk_main ();
287
288     /* Control returns here when gtk_main_quit() is called, but not when 
289      * exit() is used. */
290     
291     return 0;
292 }