GTK+ 1.2 Tutorial
Prev | Chapter 10. Container Widgets | Next

# Button Boxes

Button Boxes are a convenient way to quickly layout a group of buttons. They come in both horizontal and vertical flavours. You create a new Button Box with one of the following calls, which create a horizontal or vertical box, respectively:

    GtkWidget *gtk_hbutton_box_new( void );

    GtkWidget *gtk_vbutton_box_new( void );

The only attributes pertaining to button boxes effect how the buttons are laid out. You can change the spacing between the buttons with:

    void gtk_hbutton_box_set_spacing_default( gint spacing );

    void gtk_vbutton_box_set_spacing_default( gint spacing );

Similarly, the current spacing values can be queried using:

    gint gtk_hbutton_box_get_spacing_default( void );

    gint gtk_vbutton_box_get_spacing_default( void );

The second attribute that we can access effects the layout of the buttons within the box. It is set using one of:

    void gtk_hbutton_box_set_layout_default( GtkButtonBoxStyle layout );

    void gtk_vbutton_box_set_layout_default( GtkButtonBoxStyle layout );

The `layout` argument can take one of the following values:

      GTK_BUTTONBOX_DEFAULT_STYLE
      GTK_BUTTONBOX_SPREAD
      GTK_BUTTONBOX_EDGE
      GTK_BUTTONBOX_START
      GTK_BUTTONBOX_END

The current layout setting can be retrieved using:

    GtkButtonBoxStyle gtk_hbutton_box_get_layout_default( void );

    GtkButtonBoxStyle gtk_vbutton_box_get_layout_default( void );

Buttons are added to a Button Box using the usual function:

        gtk_container_add( GTK_CONTAINER(button_box), child_widget );

Here's an example that illustrates all the different layout settings for Button Boxes.

    /* example-start buttonbox buttonbox.c */

    #include <gtk/gtk.h>

    /* Create a Button Box with the specified parameters */
    GtkWidget *create_bbox( gint  horizontal,
                            char *title,
                            gint  spacing,
                            gint  child_w,
                            gint  child_h,
                            gint  layout )
    {
      GtkWidget *frame;
      GtkWidget *bbox;
      GtkWidget *button;

      frame = gtk_frame_new (title);

      if (horizontal)
        bbox = gtk_hbutton_box_new ();
      else
        bbox = gtk_vbutton_box_new ();

      gtk_container_set_border_width (GTK_CONTAINER (bbox), 5);
      gtk_container_add (GTK_CONTAINER (frame), bbox);

      /* Set the appearance of the Button Box */
      gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), layout);
      gtk_button_box_set_spacing (GTK_BUTTON_BOX (bbox), spacing);
      gtk_button_box_set_child_size (GTK_BUTTON_BOX (bbox), child_w, child_h);

      button = gtk_button_new_with_label ("OK");
      gtk_container_add (GTK_CONTAINER (bbox), button);

      button = gtk_button_new_with_label ("Cancel");
      gtk_container_add (GTK_CONTAINER (bbox), button);

      button = gtk_button_new_with_label ("Help");
      gtk_container_add (GTK_CONTAINER (bbox), button);

      return(frame);
    }

    int main( int   argc,
              char *argv[] )
    {
      static GtkWidget* window = NULL;
      GtkWidget *main_vbox;
      GtkWidget *vbox;
      GtkWidget *hbox;
      GtkWidget *frame_horz;
      GtkWidget *frame_vert;

      /* Initialize GTK */
      gtk_init( &argc, &argv );

      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_title (GTK_WINDOW (window), "Button Boxes");

      gtk_signal_connect (GTK_OBJECT (window), "destroy",
    		      GTK_SIGNAL_FUNC(gtk_main_quit),
    		      NULL);

      gtk_container_set_border_width (GTK_CONTAINER (window), 10);

      main_vbox = gtk_vbox_new (FALSE, 0);
      gtk_container_add (GTK_CONTAINER (window), main_vbox);

      frame_horz = gtk_frame_new ("Horizontal Button Boxes");
      gtk_box_pack_start (GTK_BOX (main_vbox), frame_horz, TRUE, TRUE, 10);

      vbox = gtk_vbox_new (FALSE, 0);
      gtk_container_set_border_width (GTK_CONTAINER (vbox), 10);
      gtk_container_add (GTK_CONTAINER (frame_horz), vbox);

      gtk_box_pack_start (GTK_BOX (vbox),
    	   create_bbox (TRUE, "Spread (spacing 40)", 40, 85, 20, GTK_BUTTONBOX_SPREAD),
    		      TRUE, TRUE, 0);

      gtk_box_pack_start (GTK_BOX (vbox),
    	   create_bbox (TRUE, "Edge (spacing 30)", 30, 85, 20, GTK_BUTTONBOX_EDGE),
    		      TRUE, TRUE, 5);

      gtk_box_pack_start (GTK_BOX (vbox),
               create_bbox (TRUE, "Start (spacing 20)", 20, 85, 20, GTK_BUTTONBOX_START),
    		      TRUE, TRUE, 5);

      gtk_box_pack_start (GTK_BOX (vbox),
    	   create_bbox (TRUE, "End (spacing 10)", 10, 85, 20, GTK_BUTTONBOX_END),
    		      TRUE, TRUE, 5);

      frame_vert = gtk_frame_new ("Vertical Button Boxes");
      gtk_box_pack_start (GTK_BOX (main_vbox), frame_vert, TRUE, TRUE, 10);

      hbox = gtk_hbox_new (FALSE, 0);
      gtk_container_set_border_width (GTK_CONTAINER (hbox), 10);
      gtk_container_add (GTK_CONTAINER (frame_vert), hbox);

      gtk_box_pack_start (GTK_BOX (hbox),
               create_bbox (FALSE, "Spread (spacing 5)", 5, 85, 20, GTK_BUTTONBOX_SPREAD),
    		      TRUE, TRUE, 0);

      gtk_box_pack_start (GTK_BOX (hbox),
               create_bbox (FALSE, "Edge (spacing 30)", 30, 85, 20, GTK_BUTTONBOX_EDGE),
    		      TRUE, TRUE, 5);

      gtk_box_pack_start (GTK_BOX (hbox),
               create_bbox (FALSE, "Start (spacing 20)", 20, 85, 20, GTK_BUTTONBOX_START),
    		      TRUE, TRUE, 5);

      gtk_box_pack_start (GTK_BOX (hbox),
               create_bbox (FALSE, "End (spacing 20)", 20, 85, 20, GTK_BUTTONBOX_END),
    		      TRUE, TRUE, 5);

      gtk_widget_show_all (window);

      /* Enter the event loop */
      gtk_main ();

      return(0);
    }
    /* example-end */

Prev | Home | Next
Scrolled Windows | Up | Toolbar