Chapter 7. Container Widgets
Prev |  | Next

## Chapter 7. Container Widgets

All container widgets derive from `Gtk::Container`, not always directly. Some container widgets, such as `Gtk::Table` can hold many child widgets, so these typically have more complex interfaces. Others, such as `Gtk::Frame` contain only one child widget.

## Single-item Containers

The single-item container widgets derive from `Gtk::Bin`, which provides the `add()` and `remove()` methods for the child widget. Note that `Gtk::Button` and `Gtk::Window` are technically single-item containers, but we have discussed them already elsewhere.

We also discuss the `Gtk::Paned` widget, which allows you to divide a window into two separate "panes". This widget actually contains two child widgets, but the number is fixed so it seems appropriate.

### Frame

Frames can enclose one or a group of widgets within a box, optionally with a title. For instance, you might place a group of `RadioButton`s or `CheckButton`s in a `Frame`.

Reference

#### Example

Figure 7.1. Frame

Source Code

### Paned

Panes divide a widget into two halves, separated by a moveable divider. There are two such widgets: `Gtk::HPaned` adds a horizontal divider, and `Gtk::VPaned` adds a vertical one. Other than the names and the orientations, there's no difference between the two.

Unlike the other widgets in this chapter, pane widgets contain not one but two child widgets, one in each pane. Therefore, you should use `add1()` and `add2()` instead of the `add()` method.

You can adjust the position of the divider using the `set_position()` method, and you will probably need to do so.

Reference

#### Example

Figure 7.2. Paned

Source Code

### ScrolledWindow

`ScrolledWindow` widgets are used to create a scrollable area. You can insert any type of widget into a `ScrolledWindow` window, and it will be accessible regardless of its size by using the scrollbars. Note that `ScrolledWindow` is not a `Gtk::Window` despite the slightly misleading name.

Scrolled windows have scrollbar policies which determine whether the `Scrollbar`s will be displayed. The policies can be set with the `set_policy()` method. The policy may be one of `Gtk::POLICY_AUTOMATIC` or `Gtk::POLICY_ALWAYS`. `Gtk::POLICY_AUTOMATIC` will cause the scrolled window to display the scrollbar only if the contained widget is larger than the visible area. `Gtk::POLICY_ALWAYS` will cause the scrollbar to be displayed always.

Reference

#### Example

Here is a simple example that packs 100 toggle buttons into a ScrolledWindow. Try resizing the window to see the scrollbars react.

Figure 7.3. ScrolledWindow

Source Code

### AspectFrame

The `AspectFrame` widget looks like a `Frame` widget, but it also enforces the aspect ratio (the ratio of the width to the height) of the child widget, adding extra space if necessary. For instance, this would allow you to display a photograph without allowing the user to distort it horizontally or vertically while resizing.

Reference

#### Example

The following program uses a `Gtk::AspectFrame` to present a drawing area whose aspect ratio will always be 2:1, no matter how the user resizes the top-level window.

### Alignment

The `Alignment` widget allows you to place a widget at a position and size relative to the size of the `Alignment` widget itself. For instance, it might be used to center a widget.

You need to specify the `Alignment`'s characteristics to the constructor, or to the `set()` method. In particular, you won't notice much effect unless you specify a number other than 1.0 for the `xscale` and `yscale` parameters, because 1.0 simply means that the child widget will expand to fill all available space.

Reference

#### Example

This example right-aligns a button in a window by using an `Alignment` widget.

Figure 7.4. Alignment

Source Code

See the ProgressBar section for another example that uses an `Alignment`.

Prev | Up | Next
Tooltips | Home | Multiple-item widgets