Packing Widgets in Boxes

Gamma/GTK: Using Gamma™ with GTK, Version 0.9
Prev | Chapter 3. Building Applications - a Tutorial | Next

# 3.4. Packing Widgets in Boxes

    #!/usr/cogent/bin/gamma

    /* Example: Packing Widgets using Boxes.  Packing is a
     * common way to arrange widgets in GTK that allows for
     * flexible control over vertical and horizontal layout,
     * alignment, and spacing.
     */

    /* Initialize Cogent interprocess communication.*/
    init_ipc("a", "aq");

    /* Set variables TRUE and FALSE to 1 and 0 respectively, to allow our
     *  code to emulate GTK better.
     */
    TRUE = 1;
    FALSE = 0;

    /* Create a function to pack a horizontal box with several
     * buttons, labelled: "button", "expand = TRUE/FALSE",
     * "fill = TRUE/FALSE", and "padding = n".  The function applies
     * the expand, fill, and padding parameters to all of the buttons
     * in the box. The homogeneity and spacing within the box can be
     * set using the respective parameters.
     */
    function make_box (homogenous, spacing, expand, fill, padding)
    {
      /* Make a horizontal box.*/
      box1 = new(GtkHBox);
      box1.set_homogeneous(homogenous);
      box1.set_spacing(spacing);

      /* Make the buttons and pack them in the box.*/
      button = new(GtkButton);
      button.label = "button";
      box1.pack_start(button, expand, fill, padding);

      button = new(GtkButton);
      if(expand != FALSE)
        button.label = "expand = TRUE";
      else
        button.label = "expand = FALSE";
      box1.pack_start(button, expand, fill, padding);

      button = new(GtkButton);
      button.label = ((fill != FALSE) ? "fill = TRUE" : "fill = FALSE");
      box1.pack_start(button, expand, fill, padding);

      button = new(GtkButton);
      button.label = (string("padding = ", padding));
      box1.pack_start(button, expand, fill, padding);

      box1;
    }

    /* Make a vertical box to hold the horizontal boxes.*/
    box2 = new(GtkVBox);
    box2.set_homogeneous(0);
    box2.set_spacing(0);

    /* Three steps to make a label and two rows of buttons:
     *
     * 1) Make a label for the rows of buttons.
     */
    label = new(GtkLabel);
    label.set_text("Homogeneous = FALSE, Spacing = 0:");
    label.set_justify(GTK_JUSTIFY_RIGHT);

    /* 2) Pack the label and two rows of buttons into
     * the vertical box.  The buttons are created within
     * the method call, by calling the make_box function.
     */
    box2.pack_start(label, FALSE, FALSE, 0);
    box2.pack_start(make_box(0, 10, TRUE, FALSE, 0), FALSE, FALSE, 0);
    box2.pack_start(make_box(0, 10, TRUE, TRUE, 0), FALSE, FALSE, 0);

    /* 3) Make a separator and pack it in the vertical box.
     */
    separator = new(GtkHSeparator);
    box2.pack_start(separator, FALSE, TRUE, 5);

    /* Repeat the above three steps, changing variables and
     * labels as desired.
     */
    label = new(GtkLabel);
    label.set_text("Homogeneous = TRUE, Spacing = 0:");
    label.set_justify(GTK_JUSTIFY_RIGHT);
    box2.pack_start(label, FALSE, FALSE, 0);
    box2.pack_start(make_box(1, 0, TRUE, FALSE, 0), FALSE, FALSE, 0);
    box2.pack_start(make_box(1, 0, TRUE, TRUE, 0), FALSE, FALSE, 0);
    separator = new(GtkHSeparator);
    box2.pack_start(separator, FALSE, TRUE, 5);

    label = new(GtkLabel);
    label.set_text("Homogeneous = TRUE, Spacing = 20:");
    label.set_justify(GTK_JUSTIFY_RIGHT);
    box2.pack_start(label, FALSE, FALSE, 0);
    box2.pack_start(make_box(1, 20, TRUE, FALSE, 0), FALSE, FALSE, 0);
    box2.pack_start(make_box(1, 20, TRUE, TRUE, 0), FALSE, FALSE, 0);
    separator = new(GtkHSeparator);
    box2.pack_start(separator, FALSE, TRUE, 5);

    /* Note that this group packs from the end, so the first line
     * appears at the bottom of the window, with successive lines
     * each above the other.
     */
    label = new(GtkLabel);
    label.set_text("Note: pack_end is used here.
                   Homogeneous = TRUE, Spacing = 0:");
    label.set_justify(GTK_JUSTIFY_RIGHT);
    box2.pack_end(label, FALSE, FALSE, 0);
    box2.pack_end(make_box(1, 0, TRUE, FALSE, 0), FALSE, FALSE, 0);
    box2.pack_end(make_box(1, 0, TRUE, TRUE, 20), FALSE, FALSE, 0);
    separator = new(GtkHSeparator);
    box2.pack_end(separator, FALSE, TRUE, 5);

    /* Make a button to quit the program.  Like the above buttons,
     * it must be in a horizontal box to control its width.
     */
    button = new(GtkButton);
    button.label = "Quit";
    button.signal("button_release_event",#gtk_main_quit());
    quitbox = new(GtkHBox);
    quitbox.pack_start(button, TRUE, FALSE, 0);
    box2.pack_start(quitbox, FALSE, FALSE, 0);

    /* Create a GtkWindow with a title and add the vertical box to it.
     */
    window = new(GtkWindow);
    window.set_border_width(10);
    window.set_default_size(600,30);
    window.set_title("Box Packing Example");
    window.add(box2);

    /* Show everything and enter the GTK main processing loop.
     */
    window.show_all();
    gtk_main();

Prev | Home | Next
Multiple Buttons | Up | Using Tables

Copyright 1995-2002 by Cogent Real-Time Systems, Inc.