1998-03-31 23:43:49 +00:00
|
|
|
|
1998-08-11 18:27:49 +00:00
|
|
|
#include <stdio.h>
|
1999-11-13 23:06:46 +00:00
|
|
|
#include <stdlib.h>
|
1998-12-07 15:19:00 +00:00
|
|
|
#include "gtk/gtk.h"
|
1998-03-31 23:43:49 +00:00
|
|
|
|
2005-01-03 19:26:36 +00:00
|
|
|
static gboolean delete_event( GtkWidget *widget,
|
|
|
|
GdkEvent *event,
|
|
|
|
gpointer data )
|
1998-03-31 23:43:49 +00:00
|
|
|
{
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_main_quit ();
|
2002-02-16 23:52:30 +00:00
|
|
|
return FALSE;
|
1998-03-31 23:43:49 +00:00
|
|
|
}
|
|
|
|
|
2008-08-11 18:36:07 +00:00
|
|
|
/* Make a new hbox filled with button-labels. Arguments for the
|
|
|
|
* variables we're interested are passed in to this function.
|
1998-03-31 23:43:49 +00:00
|
|
|
* We do not show the box, but do show everything inside. */
|
2005-01-03 19:26:36 +00:00
|
|
|
static GtkWidget *make_box( gboolean homogeneous,
|
|
|
|
gint spacing,
|
|
|
|
gboolean expand,
|
|
|
|
gboolean fill,
|
2008-08-11 18:36:07 +00:00
|
|
|
guint padding )
|
1998-03-31 23:43:49 +00:00
|
|
|
{
|
|
|
|
GtkWidget *box;
|
|
|
|
GtkWidget *button;
|
|
|
|
char padstr[80];
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-12-07 15:19:00 +00:00
|
|
|
/* Create a new hbox with the appropriate homogeneous
|
|
|
|
* and spacing settings */
|
1998-03-31 23:43:49 +00:00
|
|
|
box = gtk_hbox_new (homogeneous, spacing);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-12-07 15:19:00 +00:00
|
|
|
/* Create a series of buttons with the appropriate settings */
|
1998-03-31 23:43:49 +00:00
|
|
|
button = gtk_button_new_with_label ("gtk_box_pack");
|
|
|
|
gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
|
|
|
|
gtk_widget_show (button);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
button = gtk_button_new_with_label ("(box,");
|
|
|
|
gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
|
|
|
|
gtk_widget_show (button);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
button = gtk_button_new_with_label ("button,");
|
|
|
|
gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
|
|
|
|
gtk_widget_show (button);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-12-07 15:19:00 +00:00
|
|
|
/* Create a button with the label depending on the value of
|
1998-03-31 23:43:49 +00:00
|
|
|
* expand. */
|
|
|
|
if (expand == TRUE)
|
|
|
|
button = gtk_button_new_with_label ("TRUE,");
|
|
|
|
else
|
|
|
|
button = gtk_button_new_with_label ("FALSE,");
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
|
|
|
|
gtk_widget_show (button);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
/* This is the same as the button creation for "expand"
|
|
|
|
* above, but uses the shorthand form. */
|
|
|
|
button = gtk_button_new_with_label (fill ? "TRUE," : "FALSE,");
|
|
|
|
gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
|
|
|
|
gtk_widget_show (button);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
sprintf (padstr, "%d);", padding);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
button = gtk_button_new_with_label (padstr);
|
|
|
|
gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
|
|
|
|
gtk_widget_show (button);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
return box;
|
|
|
|
}
|
|
|
|
|
1998-12-07 15:19:00 +00:00
|
|
|
int main( int argc,
|
2008-08-11 18:36:07 +00:00
|
|
|
char *argv[])
|
1998-03-31 23:43:49 +00:00
|
|
|
{
|
|
|
|
GtkWidget *window;
|
|
|
|
GtkWidget *button;
|
|
|
|
GtkWidget *box1;
|
|
|
|
GtkWidget *box2;
|
|
|
|
GtkWidget *separator;
|
|
|
|
GtkWidget *label;
|
|
|
|
GtkWidget *quitbox;
|
|
|
|
int which;
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
/* Our init, don't forget this! :) */
|
|
|
|
gtk_init (&argc, &argv);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
if (argc != 2) {
|
|
|
|
fprintf (stderr, "usage: packbox num, where num is 1, 2, or 3.\n");
|
1999-11-13 23:06:46 +00:00
|
|
|
/* This just does cleanup in GTK and exits with an exit status of 1. */
|
2002-02-16 23:52:30 +00:00
|
|
|
exit (1);
|
1998-03-31 23:43:49 +00:00
|
|
|
}
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
which = atoi (argv[1]);
|
|
|
|
|
|
|
|
/* Create our window */
|
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
|
|
|
|
2008-08-11 18:36:07 +00:00
|
|
|
/* You should always remember to connect the "delete-event" signal
|
1999-11-13 23:06:46 +00:00
|
|
|
* to the main window. This is very important for proper intuitive
|
1998-03-31 23:43:49 +00:00
|
|
|
* behavior */
|
2008-08-11 18:36:07 +00:00
|
|
|
g_signal_connect (window, "delete-event",
|
2002-02-19 19:47:16 +00:00
|
|
|
G_CALLBACK (delete_event), NULL);
|
1999-01-28 10:35:40 +00:00
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
/* We create a vertical box (vbox) to pack the horizontal boxes into.
|
|
|
|
* This allows us to stack the horizontal boxes filled with buttons one
|
|
|
|
* on top of the other in this vbox. */
|
|
|
|
box1 = gtk_vbox_new (FALSE, 0);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-12-07 15:19:00 +00:00
|
|
|
/* which example to show. These correspond to the pictures above. */
|
1998-03-31 23:43:49 +00:00
|
|
|
switch (which) {
|
|
|
|
case 1:
|
|
|
|
/* create a new label. */
|
|
|
|
label = gtk_label_new ("gtk_hbox_new (FALSE, 0);");
|
2008-08-11 18:36:07 +00:00
|
|
|
|
|
|
|
/* Align the label to the left side. We'll discuss this function and
|
1998-03-31 23:43:49 +00:00
|
|
|
* others in the section on Widget Attributes. */
|
|
|
|
gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
|
|
|
|
|
2008-08-11 18:36:07 +00:00
|
|
|
/* Pack the label into the vertical box (vbox box1). Remember that
|
1998-03-31 23:43:49 +00:00
|
|
|
* widgets added to a vbox will be packed one on top of the other in
|
|
|
|
* order. */
|
|
|
|
gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-12-07 15:19:00 +00:00
|
|
|
/* Show the label */
|
1998-03-31 23:43:49 +00:00
|
|
|
gtk_widget_show (label);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-12-07 15:19:00 +00:00
|
|
|
/* Call our make box function - homogeneous = FALSE, spacing = 0,
|
1998-03-31 23:43:49 +00:00
|
|
|
* expand = FALSE, fill = FALSE, padding = 0 */
|
|
|
|
box2 = make_box (FALSE, 0, FALSE, FALSE, 0);
|
|
|
|
gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
|
|
|
|
gtk_widget_show (box2);
|
|
|
|
|
1998-12-07 15:19:00 +00:00
|
|
|
/* Call our make box function - homogeneous = FALSE, spacing = 0,
|
1999-11-13 23:06:46 +00:00
|
|
|
* expand = TRUE, fill = FALSE, padding = 0 */
|
1998-03-31 23:43:49 +00:00
|
|
|
box2 = make_box (FALSE, 0, TRUE, FALSE, 0);
|
|
|
|
gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
|
|
|
|
gtk_widget_show (box2);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
/* Args are: homogeneous, spacing, expand, fill, padding */
|
|
|
|
box2 = make_box (FALSE, 0, TRUE, TRUE, 0);
|
|
|
|
gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
|
|
|
|
gtk_widget_show (box2);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
|
|
|
/* Creates a separator, we'll learn more about these later,
|
1998-03-31 23:43:49 +00:00
|
|
|
* but they are quite simple. */
|
|
|
|
separator = gtk_hseparator_new ();
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1999-11-13 23:06:46 +00:00
|
|
|
/* Pack the separator into the vbox. Remember each of these
|
|
|
|
* widgets is being packed into a vbox, so they'll be stacked
|
1998-03-31 23:43:49 +00:00
|
|
|
* vertically. */
|
|
|
|
gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
|
|
|
|
gtk_widget_show (separator);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-12-07 15:19:00 +00:00
|
|
|
/* Create another new label, and show it. */
|
1998-03-31 23:43:49 +00:00
|
|
|
label = gtk_label_new ("gtk_hbox_new (TRUE, 0);");
|
|
|
|
gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
|
|
|
|
gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
|
|
|
|
gtk_widget_show (label);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
/* Args are: homogeneous, spacing, expand, fill, padding */
|
|
|
|
box2 = make_box (TRUE, 0, TRUE, FALSE, 0);
|
|
|
|
gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
|
|
|
|
gtk_widget_show (box2);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
/* Args are: homogeneous, spacing, expand, fill, padding */
|
|
|
|
box2 = make_box (TRUE, 0, TRUE, TRUE, 0);
|
|
|
|
gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
|
|
|
|
gtk_widget_show (box2);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-12-07 15:19:00 +00:00
|
|
|
/* Another new separator. */
|
1998-03-31 23:43:49 +00:00
|
|
|
separator = gtk_hseparator_new ();
|
1999-11-13 23:06:46 +00:00
|
|
|
/* The last 3 arguments to gtk_box_pack_start are:
|
|
|
|
* expand, fill, padding. */
|
1998-03-31 23:43:49 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
|
|
|
|
gtk_widget_show (separator);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
|
2008-08-11 18:36:07 +00:00
|
|
|
/* Create a new label, remember box1 is a vbox as created
|
1998-03-31 23:43:49 +00:00
|
|
|
* near the beginning of main() */
|
|
|
|
label = gtk_label_new ("gtk_hbox_new (FALSE, 10);");
|
|
|
|
gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
|
|
|
|
gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
|
|
|
|
gtk_widget_show (label);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
/* Args are: homogeneous, spacing, expand, fill, padding */
|
|
|
|
box2 = make_box (FALSE, 10, TRUE, FALSE, 0);
|
|
|
|
gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
|
|
|
|
gtk_widget_show (box2);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
/* Args are: homogeneous, spacing, expand, fill, padding */
|
|
|
|
box2 = make_box (FALSE, 10, TRUE, TRUE, 0);
|
|
|
|
gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
|
|
|
|
gtk_widget_show (box2);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
separator = gtk_hseparator_new ();
|
1999-11-13 23:06:46 +00:00
|
|
|
/* The last 3 arguments to gtk_box_pack_start are:
|
|
|
|
* expand, fill, padding. */
|
1998-03-31 23:43:49 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
|
|
|
|
gtk_widget_show (separator);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
label = gtk_label_new ("gtk_hbox_new (FALSE, 0);");
|
|
|
|
gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
|
|
|
|
gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
|
|
|
|
gtk_widget_show (label);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
/* Args are: homogeneous, spacing, expand, fill, padding */
|
|
|
|
box2 = make_box (FALSE, 0, TRUE, FALSE, 10);
|
|
|
|
gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
|
|
|
|
gtk_widget_show (box2);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
/* Args are: homogeneous, spacing, expand, fill, padding */
|
|
|
|
box2 = make_box (FALSE, 0, TRUE, TRUE, 10);
|
|
|
|
gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
|
|
|
|
gtk_widget_show (box2);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
separator = gtk_hseparator_new ();
|
|
|
|
/* The last 3 arguments to gtk_box_pack_start are: expand, fill, padding. */
|
|
|
|
gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
|
|
|
|
gtk_widget_show (separator);
|
|
|
|
break;
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
case 3:
|
|
|
|
|
1998-12-07 15:19:00 +00:00
|
|
|
/* This demonstrates the ability to use gtk_box_pack_end() to
|
|
|
|
* right justify widgets. First, we create a new box as before. */
|
1998-03-31 23:43:49 +00:00
|
|
|
box2 = make_box (FALSE, 0, FALSE, FALSE, 0);
|
1998-12-07 15:19:00 +00:00
|
|
|
|
|
|
|
/* Create the label that will be put at the end. */
|
1998-03-31 23:43:49 +00:00
|
|
|
label = gtk_label_new ("end");
|
1998-12-07 15:19:00 +00:00
|
|
|
/* Pack it using gtk_box_pack_end(), so it is put on the right
|
|
|
|
* side of the hbox created in the make_box() call. */
|
1998-03-31 23:43:49 +00:00
|
|
|
gtk_box_pack_end (GTK_BOX (box2), label, FALSE, FALSE, 0);
|
1998-12-07 15:19:00 +00:00
|
|
|
/* Show the label. */
|
1998-03-31 23:43:49 +00:00
|
|
|
gtk_widget_show (label);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-12-07 15:19:00 +00:00
|
|
|
/* Pack box2 into box1 (the vbox remember ? :) */
|
1998-03-31 23:43:49 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
|
|
|
|
gtk_widget_show (box2);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-12-07 15:19:00 +00:00
|
|
|
/* A separator for the bottom. */
|
1998-03-31 23:43:49 +00:00
|
|
|
separator = gtk_hseparator_new ();
|
1998-12-07 15:19:00 +00:00
|
|
|
/* This explicitly sets the separator to 400 pixels wide by 5 pixels
|
|
|
|
* high. This is so the hbox we created will also be 400 pixels wide,
|
1998-03-31 23:43:49 +00:00
|
|
|
* and the "end" label will be separated from the other labels in the
|
1998-12-07 15:19:00 +00:00
|
|
|
* hbox. Otherwise, all the widgets in the hbox would be packed as
|
1998-03-31 23:43:49 +00:00
|
|
|
* close together as possible. */
|
2002-02-16 23:52:30 +00:00
|
|
|
gtk_widget_set_size_request (separator, 400, 5);
|
2008-08-11 18:36:07 +00:00
|
|
|
/* pack the separator into the vbox (box1) created near the start
|
1998-03-31 23:43:49 +00:00
|
|
|
* of main() */
|
|
|
|
gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
|
2008-08-11 18:36:07 +00:00
|
|
|
gtk_widget_show (separator);
|
1998-03-31 23:43:49 +00:00
|
|
|
}
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
/* Create another new hbox.. remember we can use as many as we need! */
|
|
|
|
quitbox = gtk_hbox_new (FALSE, 0);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
/* Our quit button. */
|
|
|
|
button = gtk_button_new_with_label ("Quit");
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1999-11-13 23:06:46 +00:00
|
|
|
/* Setup the signal to terminate the program when the button is clicked */
|
2008-08-11 18:36:07 +00:00
|
|
|
g_signal_connect_swapped (button, "clicked",
|
2002-02-19 19:47:16 +00:00
|
|
|
G_CALLBACK (gtk_main_quit),
|
2008-08-11 18:36:07 +00:00
|
|
|
window);
|
1998-12-07 15:19:00 +00:00
|
|
|
/* Pack the button into the quitbox.
|
1999-11-13 23:06:46 +00:00
|
|
|
* The last 3 arguments to gtk_box_pack_start are:
|
|
|
|
* expand, fill, padding. */
|
1998-03-31 23:43:49 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (quitbox), button, TRUE, FALSE, 0);
|
|
|
|
/* pack the quitbox into the vbox (box1) */
|
|
|
|
gtk_box_pack_start (GTK_BOX (box1), quitbox, FALSE, FALSE, 0);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-12-07 15:19:00 +00:00
|
|
|
/* Pack the vbox (box1) which now contains all our widgets, into the
|
1998-03-31 23:43:49 +00:00
|
|
|
* main window. */
|
|
|
|
gtk_container_add (GTK_CONTAINER (window), box1);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-12-07 15:19:00 +00:00
|
|
|
/* And show everything left */
|
1998-03-31 23:43:49 +00:00
|
|
|
gtk_widget_show (button);
|
|
|
|
gtk_widget_show (quitbox);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
gtk_widget_show (box1);
|
|
|
|
/* Showing the window last so everything pops up at once. */
|
|
|
|
gtk_widget_show (window);
|
2008-08-11 18:36:07 +00:00
|
|
|
|
1998-03-31 23:43:49 +00:00
|
|
|
/* And of course, our main function. */
|
|
|
|
gtk_main ();
|
|
|
|
|
2008-08-11 18:36:07 +00:00
|
|
|
/* Control returns here when gtk_main_quit() is called, but not when
|
2002-02-25 01:47:44 +00:00
|
|
|
* exit() is used. */
|
2008-08-11 18:36:07 +00:00
|
|
|
|
2002-02-16 23:52:30 +00:00
|
|
|
return 0;
|
1998-03-31 23:43:49 +00:00
|
|
|
}
|