Container Widgets
All container widgets derive from Gtk::Container, not always directly. Some container widgets, such as Gtk::Grid 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 RadioButtons or CheckButtons in a Frame.
Reference

Example

Frame
Source Code

Paned
Panes divide a widget into two halves, separated by a moveable divider. The two halves (panes) can be oriented either horizontally (side by side) or vertically (one above the other).
Unlike the other widgets in this section, 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

Paned
Source Code

ScrolledWindow
ScrolledWindow widgets 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 Scrollbars 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.

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.

AspectFrame
Source Code

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.

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

Multiple-item widgets
Multiple-item widgets inherit from Gtk::Container; just as with Gtk::Bin, you use the add() and remove() methods to add and remove contained widgets. Unlike Gtk::Bin::remove(), however, the remove() method for Gtk::Container takes an argument, specifiying which widget to remove.

Packing
You've probably noticed that gtkmm windows seem "elastic" - they can usually be stretched in many different ways. This is due to the widget packing system.
Many GUI toolkits require you to precisely place widgets in a window, using absolute positioning, often using a visual editor. This leads to several problems:
The widgets don't rearrange themselves when the window is resized. Some widgets are hidden when the window is made smaller, and lots of useless space appears when the window is made larger.
It's impossible to predict the amount of space necessary for text after it has been translated to other languages, or displayed in a different font. On Unix it is also impossible to anticipate the effects of every theme and window manager.
Changing the layout of a window "on the fly", to make some extra widgets appear, for instance, is complex. It requires tedious recalculation of every widget's position.
gtkmm uses the packing system to solve these problems. Rather than specifying the position and size of each widget in the window, you can arrange your widgets in rows, columns, and/or grids. gtkmm can size your window automatically, based on the sizes of the widgets it contains. And the sizes of the widgets are, in turn, determined by the amount of text they contain, or the minimum and maximum sizes that you specify, and/or how you have requested that the available space should be shared between sets of widgets. You can perfect your layout by specifying padding distance and centering values for each of your widgets. gtkmm then uses all this information to resize and reposition everything sensibly and smoothly when the user manipulates the window.
gtkmm arranges widgets hierarchically, using containers. A Container widget contains other widgets. Most gtkmm widgets are containers. Windows, Notebook tabs, and Buttons are all container widgets. There are two flavours of containers: single-child containers, which are all descendants of Gtk::Bin, and multiple-child containers, which are descendants of Gtk::Container. Most widgets in gtkmm are descendants of Gtk::Bin, including Gtk::Window.
Yes, that's correct: a Window can contain at most one widget. How, then, can we use a window for anything useful? By placing a multiple-child container in the window. The most useful container widgets are Gtk::Grid and Gtk::Box.
Gtk::Grid arranges its child widgets in rows and columns. Use attach(), attach_next_to() and add() to insert child widgets.
Gtk::Box arranges its child widgets vertically or horizontally. Use pack_start() and pack_end() to insert child widgets.
There are several other containers, which we will also discuss.
If you've never used a packing toolkit before, it can take some getting used to. You'll probably find, however, that you don't need to rely on visual form editors quite as much as you might with other toolkits.

An improved Hello World
Let's take a look at a slightly improved helloworld, showing what we've learnt.

Hello World 2
Source Code
After building and running this program, try resizing the window to see the behaviour. Also, try playing with the options to pack_start() while reading the Boxes section.

Boxes
Most packing uses boxes as in the above example. These are invisible containers into which we can pack our widgets. When packing widgets into a horizontal box, the objects are inserted horizontally from left to right or right to left depending on whether pack_start() or pack_end() is used. In a vertical box, widgets are packed from top to bottom or vice versa. You may use any combination of boxes inside or beside other boxes to create the desired effect.

Adding widgets

Per-child packing options
The pack_start() and pack_end() methods place widgets inside these containers. The pack_start() method will start at the top and work its way down in a Box with vertical orientation, or pack left to right in a Box with horizontal orientation. pack_end() will do the opposite, packing from bottom to top or from right to left. Using these methods allows us to right justify or left justify our widgets. We will use pack_start() in most of our examples.
There are several options governing how widgets are to be packed, and this can be confusing at first. If you have difficulties then it is sometimes a good idea to play with the glade GUI designer to see what is possible. You might even decide to use the Gtk::Builder API to load your GUI at runtime.
There are basically five different styles, as shown in this picture:

Box Packing 1
Each line contains one horizontal Box with several buttons. Each of the buttons on a line is packed into the Box with the same arguments to the pack_start() method.
This is the declaration of the pack_start() method:

void pack_start(Gtk::Widget& child,
                Gtk::PackOptions options = Gtk::PACK_EXPAND_WIDGET,
                guint padding = 0);

The first argument is the widget you're packing. In our example these are all Buttons.
The options argument can take one of these three options:
Gtk::PACK_SHRINK: Space is contracted to the child widget size. The widget will take up just-enough space and never expand.
Gtk::PACK_EXPAND_PADDING: Extra space is filled with padding. The widgets will be spaced out evenly, but their sizes won't change - there will be empty space between the widgets instead.
Gtk::PACK_EXPAND_WIDGET: Extra space is taken up by increasing the child widget size, without changing the amount of space between widgets.
The padding argument specifies the width of an extra border area to leave around the packed widget.
Reference

Per-container packing options
Here's the constructor for the Box widget, and methods that set per-container packing options:

Gtk::Box(Gtk::Orientation orientation = Gtk::ORIENTATION_HORIZONTAL, int spacing = 0);
void set_spacing(int spacing);
void set_homogeneous(bool homogeneous = true);

Passing true to set_homogeneous() will cause all of the contained widgets to be the same size. spacing is a (minimum) number of pixels to leave between each widget.
What's the difference between spacing (set when the box is created) and padding (set when elements are packed)? Spacing is added between objects, and padding is added on either side of a widget. The following figure should make it clearer:

Box Packing 2

Gtk::Application and command-line options
The following example program requires a command-line option. The source code shows two ways of handling command-line options in combination with Gtk::Application.
Handle the options in main() and hide them from Gtk::Application by setting argc = 1 in the call to Gtk::Application::create().
Give all command-line options to Gtk::Application::create() and add the flag Gio::APPLICATION_HANDLES_COMMAND_LINE. Connect a signal handler to the command_line signal, and handle the command-line options in the signal handler.
You must set the optional parameter after = false in the call to signal_command_line().connect(), because your signal handler must be called before the default signal handler. You must also call Gio::Application::activate() in the signal handler, unless you want your application to exit without showing its main window. (Gio::Application is a base class of Gtk::Application.)

Example
Here is the source code for the example that produced the screenshots above. When you run this example, provide a number between 1 and 3 as a command-line option, to see different packing options in use.
Source Code

ButtonBoxes
Button boxes are a convenient way to quickly arrange a group of buttons. Their orientation can be either horizontal or vertical.
ButtonBoxes help to make applications appear consistent because they use standard settings, such as inter-button spacing and packing.
Buttons are added to a ButtonBox with the add() method.
Button boxes support several layout styles. The style can be retrieved and changed using get_layout() and set_layout().
Reference

Example

ButtonBox
Source Code

Grid
A Grid dynamically lays out child widgets in rows and columns. The dimensions of the grid do not need to be specified in the constructor.
Child widgets can span multiple rows or columns, using attach(), or added next to an existing widget inside the grid with attach_next_to(). Individual rows and columns of the grid can be set to have uniform height or width with set_row_homogeneous() and set_column_homogeneous().
You can set the margin and expand properties of the child Widgets to control their spacing and their behaviour when the Grid is resized.
Reference

Example
This example creates a window with three buttons in a grid. The first two buttons are in the upper row, from left to right. A third button is attached underneath the first button, in a new lower row, spanning two columns.

Grid
Source Code

Table
Gtk::Table allows us to place widgets in a grid, similar to Gtk::Grid.
Gtk::Table is deprecated from gtkmm version 3.4 and should not be used in newly-written code. Use Gtk::Grid instead.

Notebook
A Notebook has a set of stacked pages, each of which contains widgets. Labelled tabs allow the user to select the pages. Notebooks allow several sets of widgets to be placed in a small space, by only showing one page at a time. For instance, they are often used in preferences dialogs.
Use the append_page(), prepend_page() and insert_page() methods to add tabbed pages to the Notebook, supplying the child widget and the name for the tab.
To discover the currently visible page, use the get_current_page() method. This returns the page number, and then calling get_nth_page() with that number will give you a pointer to the actual child widget.
To programmatically change the selected page, use the set_current_page() method.
Reference

Example

Notebook
Source Code

Assistant
An Assistant splits a complex operation into steps. Each step is a page, containing a header, a child widget and an action area. The Assistant's action area has navigation buttons which update automatically depending on the type of the page, set with set_page_type().
Use the append_page(), prepend_page and insert_page() methods to add pages to the Assistant, supplying the child widget for each page.
To determine the currently-visible page, use the get_current_page() method, and pass the result to get_nth_page(), which returns a pointer to the actual widget. To programmatically change the current page, use the set_current_page() method.
To set the title of a page, use the set_page_title() method. The header and side images of a page can be set with the set_page_header_image() and set_page_side_image() methods.
To add widgets to the action area, use the add_action_widget() method. They will be packed alongside the default buttons. Use the remove_action_widget() method to remove widgets.
Reference

Example

Assistant
Source Code