Multiple-item Containers
| Chapter 9. Container Widgets |

## Multiple-item Containers

Multiple-item container widgets have other methods than `set_child()` and `unset_child()`. Different containers can have different methods for adding and removing child widgets. For instance, `Gtk::Box` has `append()` and `remove()` as well as other methods. The `remove()` method for multiple-item containers takes an argument, specifying 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 margins 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 flavors of containers: single-child containers and multiple-child containers. Most container widgets in gtkmm are single-child containers, 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()` and `attach_next_to()` to insert child widgets.

- `Gtk::Box` arranges its child widgets vertically or horizontally. Use `append()` 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.

Figure 9.5. Hello World 2

Source Code

File: `helloworld.h` (For use with gtkmm 4)

    #ifndef GTKMM_EXAMPLE_HELLOWORLD_H
    #define GTKMM_EXAMPLE_HELLOWORLD_H

    #include <gtkmm/box.h>
    #include <gtkmm/button.h>
    #include <gtkmm/window.h>

    class HelloWorld : public Gtk::Window
    {
    public:
      HelloWorld();
      ~HelloWorld() override;

    protected:

      // Signal handlers:
      // Our new improved on_button_clicked().
      void on_button_clicked(const Glib::ustring& data);

      // Child widgets:
      Gtk::Box m_box1;
      Gtk::Button m_button1, m_button2;
    };

    #endif // GTKMM_EXAMPLE_HELLOWORLD_H

File: `helloworld.cc` (For use with gtkmm 4)

    #include "helloworld.h"
    #include <iostream>

    HelloWorld::HelloWorld()
    : m_button1("Button 1"),
      m_button2("Button 2")
    {
      // This just sets the title of our new window.
      set_title("Hello Buttons!");

      // Sets the margin around the box.
      m_box1.set_margin(10);

      // put the box into the main window.
      set_child(m_box1);

      // Now when the button is clicked, we call the on_button_clicked() function
      // with a pointer to "button 1" as its argument.
      m_button1.signal_clicked().connect(sigc::bind(
                  sigc::mem_fun(*this, &HelloWorld::on_button_clicked), "button 1"));

      // We use Gtk::Box::append() to pack this button into the box,
      // which has been packed into the window.
      // A widget's default behaviour is not to expand if extra space is available.
      // A container widget by default expands if any of the contained widgets
      // wants to expand.
      m_box1.append(m_button1);
      m_button1.set_expand();

      // call the same signal handler with a different argument,
      // passing a pointer to "button 2" instead.
      m_button2.signal_clicked().connect(sigc::bind(
                  sigc::mem_fun(*this, &HelloWorld::on_button_clicked), "button 2"));

      m_box1.append(m_button2);
      m_button2.set_expand();

      // Gtk::Widget::set_visible(true) is seldom needed. All widgets are visible by default.
    }

    HelloWorld::~HelloWorld()
    {
    }

    // Our new improved signal handler.  The data passed to this method is
    // printed to stdout.
    void HelloWorld::on_button_clicked(const Glib::ustring& data)
    {
      std::cout << "Hello World - " << data << " was pressed" << std::endl;
    }

File: `main.cc` (For use with gtkmm 4)

    #include "helloworld.h"
    #include <gtkmm/application.h>

    int main(int argc, char* argv[])
    {
      auto app = Gtk::Application::create("org.gtkmm.example");

      //Shows the window and returns when it is closed.
      return app->make_window_and_run<HelloWorld>(argc, argv);
    }

After building and running this program, try resizing the window to see the behavior. Also, try playing with `set_expand()`, `set_hexpand()`, `set_vexpand()`, `set_halign()` and `set_valign()` 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. In a vertical box, widgets are packed from top to bottom. You may use any combination of boxes inside or beside other boxes to create the desired effect.

#### Adding widgets

##### Per-child packing options

The `append()` method places widgets inside these containers. It 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. If it's inconvenient to add widgets in this order, use `insert_child_after()` or `insert_child_at_start()`. We will use `append()` in our examples.

There are several options governing how widgets are to be packed, and this can be confusing at first. You can modify the packing by using `set_expand()`, `set_hexpand()`, `set_vexpand()`, `set_halign()`, `set_valign()` and/or `set_margin()` on the child widgets. If you have difficulties, then it is sometimes a good idea to play with the Cambalache GUI designer to see what is possible. You can then use the `Gtk::Builder` API to load your GUI at runtime.

There are basically five different styles, as shown in this picture:

Figure 9.6. 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 `set_hexpand()`, `set_halign()`, `set_margin_start()` and `set_margin_end()` methods.

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_orientation(Gtk::Orientation orientation);
    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 margins (set separately for each child widget)? Spacing is added between objects, and margins are added on one or more sides of a widget. The following figure should make it clearer. The shown margins are the left and right margins of each button in the row.

Figure 9.7. 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::run()`.

- Give all command-line options to `Gtk::Application::run()` and add the flag `Gio::Application::Flags::HANDLES_COMMAND_LINE` to `Gtk::Application::create()`. 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

File: `examplewindow.h` (For use with gtkmm 4)

    #ifndef GTKMM_EXAMPLEWINDOW_H
    #define GTKMM_EXAMPLEWINDOW_H

    #include <gtkmm.h>

    class ExampleWindow : public Gtk::Window
    {
    public:
      ExampleWindow(int which);
      virtual ~ExampleWindow();

    protected:
      //Signal handlers:
      void on_button_quit_clicked();

      //Child widgets:
      Gtk::Button m_button;
      Gtk::Box m_box1;
      Gtk::Box m_boxQuit;
      Gtk::Button m_buttonQuit;

      Gtk::Label m_Label1, m_Label2;

      Gtk::Separator m_separator1, m_separator2;
    };

    #endif //GTKMM_EXAMPLEWINDOW_H

File: `packbox.h` (For use with gtkmm 4)

    #ifndef GTKMM_EXAMPLE_PACKBOX_H
    #define GTKMM_EXAMPLE_PACKBOX_H

    #include <gtkmm.h>

    class PackBox : public Gtk::Box
    {
    public:
      PackBox(bool homogeneous = false, int spacing = 0, bool expand = false,
        Gtk::Align align = Gtk::Align::FILL, int margin = 0);

    protected:
      Gtk::Button m_buttons[4];
    };

    #endif //GTKMM_EXAMPLE_PACKBOX_H

File: `examplewindow.cc` (For use with gtkmm 4)

    #include <iostream>
    #include "examplewindow.h"
    #include "packbox.h"

    ExampleWindow::ExampleWindow(int which)
    : m_box1(Gtk::Orientation::VERTICAL),
      m_buttonQuit("Quit")
    {
      set_title("Gtk::Box example");

      m_separator1.set_margin_top(5);
      m_separator1.set_margin_bottom(5);
      m_separator2.set_margin_top(5);
      m_separator2.set_margin_bottom(5);

      switch(which)
      {
        case 1:
        {
          m_Label1.set_text("Gtk::Box(Gtk::Orientation::HORIZONTAL, 0); set_homogeneous(false);");

          // Align the label to the left side.
          m_Label1.set_halign(Gtk::Align::START);
          m_Label1.set_valign(Gtk::Align::START);

          // Pack the label into the vertical box (vbox box1).  Remember that
          // widgets added to a vbox will be packed one on top of the other in
          // order.
          m_box1.append(m_Label1);

          // Create a PackBox - homogeneous = false, spacing = 0,
          // expand = false, Gtk::Align::FILL, margin = 0
          // These are the default values.
          auto pPackBox = Gtk::make_managed<PackBox>();
          m_box1.append(*pPackBox);

          // Create a PackBox - homogeneous = false, spacing = 0,
          // expand = true, Gtk::Align::CENTER, margin = 0
          pPackBox = Gtk::make_managed<PackBox>(false, 0, true, Gtk::Align::CENTER);
          m_box1.append(*pPackBox);

          // Create a PackBox - homogeneous = false, spacing = 0,
          // expand = true, Gtk::Align::FILL, margin = 0
          pPackBox = Gtk::make_managed<PackBox>(false, 0, true);
          m_box1.append(*pPackBox);

          // pack the separator into the vbox.  Remember each of these
          // widgets are being packed into a vbox, so they'll be stacked
          // vertically.
          m_box1.append(m_separator1);

          // create another new label, and show it.
          m_Label2.set_text("Gtk::Box(Gtk::Orientation::HORIZONTAL, 0); set_homogeneous(true);");
          m_Label2.set_halign(Gtk::Align::START);
          m_Label2.set_valign(Gtk::Align::START);
          m_box1.append(m_Label2);

          // Args are: homogeneous, spacing, expand, align, margin
          pPackBox = Gtk::make_managed<PackBox>(true, 0, true, Gtk::Align::CENTER);
          m_box1.append(*pPackBox);

          // Args are: homogeneous, spacing, expand, align, margin
          pPackBox = Gtk::make_managed<PackBox>(true, 0, true);
          m_box1.append(*pPackBox);

          m_box1.append(m_separator2);

          break;
        }

        case 2:
        {
          m_Label1.set_text("Gtk::Box(Gtk::Orientation::HORIZONTAL, 10); set_homogeneous(false);");
          m_Label1.set_halign(Gtk::Align::START);
          m_Label1.set_valign(Gtk::Align::START);
          m_box1.append(m_Label1);

          auto pPackBox = Gtk::make_managed<PackBox>(false, 10, true, Gtk::Align::CENTER);
          m_box1.append(*pPackBox);

          pPackBox = Gtk::make_managed<PackBox>(false, 10, true);
          m_box1.append(*pPackBox);

          m_box1.append(m_separator1);

          m_Label2.set_text("Gtk::Box(Gtk::Orientation::HORIZONTAL, 0); set_homogeneous(false);");
          m_Label2.set_halign(Gtk::Align::START);
          m_Label2.set_valign(Gtk::Align::START);
          m_box1.append(m_Label2);

          pPackBox = Gtk::make_managed<PackBox>(false, 0, false, Gtk::Align::FILL, 10);
          m_box1.append(*pPackBox);

          pPackBox = Gtk::make_managed<PackBox>(false, 0, true, Gtk::Align::FILL, 10);
          m_box1.append(*pPackBox);

          m_box1.append(m_separator2);

          break;
        }

        case 3:
        {
          // This demonstrates the ability to use Gtk::Align::END to
          // right justify widgets.  First, we create a new box as before.
          auto pPackBox = Gtk::make_managed<PackBox>();

          // create the label that will be put at the end.
          m_Label1.set_text("end");

          // pack it using Gtk::Align::END, so it is put on the right side
          // of the PackBox.
          m_Label1.set_halign(Gtk::Align::END);
          m_Label1.set_hexpand(true);
          pPackBox->append(m_Label1);

          m_box1.append(*pPackBox);

          // This explicitly sets the separator to 700 pixels wide by 5 pixels
          // high.  This is so the hbox we created will also be 700 pixels wide,
          // and the "end" label will be separated from the other labels in the
          // hbox.  Otherwise, all the widgets in the hbox would be packed as
          // close together as possible.
          m_separator1.set_size_request(700, 5);

          // pack the separator into the vbox.
          m_box1.append(m_separator1);

          break;
        }

        default:
        {
          std::cerr << "Unexpected command-line option." << std::endl;
          break;
        }
      }

      // Connect the signal to hide the window:
      m_buttonQuit.signal_clicked().connect( sigc::mem_fun(*this,
                  &ExampleWindow::on_button_quit_clicked) );

      // pack the button into the quitbox.
      m_boxQuit.append(m_buttonQuit);
      m_buttonQuit.set_hexpand(true);
      m_buttonQuit.set_halign(Gtk::Align::CENTER);
      m_box1.append(m_boxQuit);

      // pack the vbox (box1) which now contains all our widgets, into the
      // main window.
      set_child(m_box1);
    }

    ExampleWindow::~ExampleWindow()
    {
    }

    void ExampleWindow::on_button_quit_clicked()
    {
      close();
    }

File: `main.cc` (For use with gtkmm 4)

    #include "examplewindow.h"
    #include <gtkmm/application.h>
    #include <iostream>
    #include <cstdlib>

    #define GTK_APPLICATION_RECEIVES_COMMAND_LINE_ARGUMENTS 0

    #if GTK_APPLICATION_RECEIVES_COMMAND_LINE_ARGUMENTS
    namespace
    {
    int on_command_line(const Glib::RefPtr<Gio::ApplicationCommandLine>& command_line,
                        Glib::RefPtr<Gtk::Application>& app)
    {
      int argc = 0;
      char** argv = command_line->get_arguments(argc);

      for (int i = 0; i < argc; ++i)
        std::cout << "argv[" << i << "] = " << argv[i] << std::endl;

      app->activate(); // Without activate() the window won't be shown.
      return EXIT_SUCCESS;
    }
    } // anonymous namespace
    #endif

    int main(int argc, char *argv[])
    {
      if (argc != 2)
      {
        std::cerr << "Usage: example <num>, where <num> is 1, 2, or 3." << std::endl;
        return EXIT_FAILURE;
      }

      int argc1 = argc;

    #if GTK_APPLICATION_RECEIVES_COMMAND_LINE_ARGUMENTS
      // The Gio::Application::Flags::HANDLES_COMMAND_LINE flag and the
      // on_command_line() signal handler are not necessary. This program is simpler
      // without them, and with argc = 1 in the call to Gtk::Application::make_window_and_run().
      // They are included to show a program with Gio::Application::Flags::HANDLES_COMMAND_LINE.
      // Gio::Application::Flags::NON_UNIQUE makes it possible to run several instances of
      // this application simultaneously.
      auto app = Gtk::Application::create(
        "org.gtkmm.example", Gio::Application::Flags::HANDLES_COMMAND_LINE | Gio::Application::Flags::NON_UNIQUE);

      // Note after = false.
      // Only one signal handler is invoked. This signal handler must run before
      // the default signal handler, or else it won't run at all.
      app->signal_command_line().connect(
        [&app](const auto& command_line)
        { return on_command_line(command_line, app); }, false);
    #else
      // Gio::Application::Flags::NON_UNIQUE makes it possible to run several instances of
      // this application simultaneously.
      argc1 = 1; // Don't give the command line arguments to Gtk::Application.
      auto app = Gtk::Application::create("org.gtkmm.example", Gio::Application::Flags::NON_UNIQUE);
    #endif

      // Shows the window and returns when it is closed.
      return app->make_window_and_run<ExampleWindow>(argc1, argv, std::atoi(argv[1]));
    }

File: `packbox.cc` (For use with gtkmm 4)

    #include "packbox.h"
    #include <map>

    namespace
    {
      const std::map<Gtk::Align, Glib::ustring> align_string = {
        {Gtk::Align::FILL, "Gtk::Align::FILL"},
        {Gtk::Align::START, "Gtk::Align::START"},
        {Gtk::Align::END, "Gtk::Align::END"},
        {Gtk::Align::CENTER, "Gtk::Align::CENTER"},
        {Gtk::Align::BASELINE, "Gtk::Align::BASELINE"},
      };
    }

    PackBox::PackBox(bool homogeneous, int spacing, bool expand, Gtk::Align align, int margin)
    : Gtk::Box(Gtk::Orientation::HORIZONTAL, spacing)
    {
      set_homogeneous(homogeneous);

      m_buttons[0].set_label("box.append(button);");
      m_buttons[1].set_label("expand=" + Glib::ustring(expand ? "true" : "false"));
      m_buttons[2].set_label(align_string.at(align));
      m_buttons[3].set_label("margin=" + Glib::ustring::format(margin));

      for (auto& button : m_buttons)
      {
        append(button);
        button.set_hexpand(expand);
        button.set_halign(align);
        button.set_margin_start(margin);
        button.set_margin_end(margin);
      }
    }

### 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 `Widget`s to control their spacing and their behavior 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.

Figure 9.8. Grid

Source Code

File: `examplewindow.h` (For use with gtkmm 4)

    #ifndef GTKMM_EXAMPLEWINDOW_H
    #define GTKMM_EXAMPLEWINDOW_H

    #include <gtkmm.h>

    class ExampleWindow : public Gtk::Window
    {
    public:
      ExampleWindow();
      virtual ~ExampleWindow();

    private:
      // Signal handlers:
      void on_button_quit();
      void on_button_numbered(const Glib::ustring& data);

      // Child widgets:
      Gtk::Grid m_grid;
      Gtk::Button m_button_1, m_button_2, m_button_quit;
    };

    #endif /* GTKMM_EXAMPLEWINDOW_H */

File: `examplewindow.cc` (For use with gtkmm 4)

    #include <iostream>
    #include "examplewindow.h"

    ExampleWindow::ExampleWindow()
    : m_button_1("button 1"),
      m_button_2("button 2"),
      m_button_quit("Quit")
    {
      set_title("Gtk::Grid");

      m_grid.set_margin(12);
      set_child(m_grid);

      m_grid.attach(m_button_1, 0, 0);
      m_grid.attach(m_button_2, 1, 0);
      m_grid.attach_next_to(m_button_quit, m_button_1, Gtk::PositionType::BOTTOM, 2, 1);

      m_button_1.signal_clicked().connect(
        sigc::bind( sigc::mem_fun(*this, &ExampleWindow::on_button_numbered), "button 1") );
      m_button_2.signal_clicked().connect(
        sigc::bind( sigc::mem_fun(*this, &ExampleWindow::on_button_numbered), "button 2") );

      m_button_quit.signal_clicked().connect(sigc::mem_fun(*this,
        &ExampleWindow::on_button_quit) );
    }

    ExampleWindow::~ExampleWindow()
    {
    }

    void ExampleWindow::on_button_quit()
    {
      close();
    }

    void
    ExampleWindow::on_button_numbered(const Glib::ustring& data)
    {
      std::cout << data << " was pressed" << std::endl;
    }

File: `main.cc` (For use with gtkmm 4)

    #include "examplewindow.h"
    #include <gtkmm/application.h>

    int main(int argc, char *argv[])
    {
      auto app = Gtk::Application::create("org.gtkmm.example");

      // Shows the window and returns when it is closed.
      return app->make_window_and_run<ExampleWindow>(argc, argv);
    }

### Notebook

A `Notebook` has a set of stacked `pages`, each of which contains widgets. Labelled `tabs` allow the user to select the pages. `Notebook`s 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

Figure 9.9. Notebook

Source Code

File: `examplewindow.h` (For use with gtkmm 4)

    #ifndef GTKMM_EXAMPLEWINDOW_H
    #define GTKMM_EXAMPLEWINDOW_H

    #include <gtkmm.h>

    class ExampleWindow : public Gtk::Window
    {
    public:
      ExampleWindow();
      virtual ~ExampleWindow();

    protected:
      //Signal handlers:
      void on_button_quit();
      void on_notebook_switch_page(Gtk::Widget* page, guint page_num);

      //Child widgets:
      Gtk::Box m_VBox;
      Gtk::Notebook m_Notebook;
      Gtk::Label m_Label1, m_Label2;

      Gtk::Box m_ButtonBox;
      Gtk::Button m_Button_Quit;
    };

    #endif //GTKMM_EXAMPLEWINDOW_H

File: `examplewindow.cc` (For use with gtkmm 4)

    #include <iostream>
    #include "examplewindow.h"

    ExampleWindow::ExampleWindow()
    : m_VBox(Gtk::Orientation::VERTICAL),
      m_Label1("Contents of tab 1"),
      m_Label2("Contents of tab 2"),
      m_Button_Quit("Quit")
    {
      set_title("Gtk::Notebook example");
      set_default_size(400, 200);

      m_VBox.set_margin(10);
      set_child(m_VBox);

      //Add the Notebook, with the button underneath:
      m_Notebook.set_margin(10);
      m_Notebook.set_expand();
      m_VBox.append(m_Notebook);
      m_VBox.append(m_ButtonBox);

      m_ButtonBox.append(m_Button_Quit);
      m_Button_Quit.set_hexpand(true);
      m_Button_Quit.set_halign(Gtk::Align::CENTER);
      m_Button_Quit.signal_clicked().connect(sigc::mem_fun(*this,
                  &ExampleWindow::on_button_quit) );

      //Add the Notebook pages:
      m_Notebook.append_page(m_Label1, "First");
      m_Notebook.append_page(m_Label2, "Second");

      m_Notebook.signal_switch_page().connect(sigc::mem_fun(*this,
                  &ExampleWindow::on_notebook_switch_page) );
    }

    ExampleWindow::~ExampleWindow()
    {
    }

    void ExampleWindow::on_button_quit()
    {
      close();
    }

    void ExampleWindow::on_notebook_switch_page(Gtk::Widget* /* page */, guint page_num)
    {
      std::cout << "Switched to tab with index " << page_num << std::endl;

      //You can also use m_Notebook.get_current_page() to get this index.
    }

File: `main.cc` (For use with gtkmm 4)

    #include "examplewindow.h"
    #include <gtkmm/application.h>

    int main(int argc, char *argv[])
    {
      auto app = Gtk::Application::create("org.gtkmm.example");

      //Shows the window and returns when it is closed.
      return app->make_window_and_run<ExampleWindow>(argc, argv);
    }

### Assistant

| Note
`Gtk::Assistant` is deprecated since gtkmm 4.10. There is no replacement in gtkmm. libadwaita (a C library) has replacement parts (like AdwCarousel). In some cases, a `Gtk::Notebook` might be an acceptable replacement.

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.

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

Figure 9.10. Assistant

Source Code

File: `exampleassistant.h` (For use with gtkmm 4)

    #ifndef GTKMM_EXAMPLEASSISTANT_H
    #define GTKMM_EXAMPLEASSISTANT_H

    #include <gtkmm.h>

    class ExampleAssistant : public Gtk::Assistant
    {
    public:
      ExampleAssistant();
      virtual ~ExampleAssistant();

      void get_result(bool& check_state, Glib::ustring& entry_text);

    private:
      // Signal handlers:
      void on_assistant_apply();
      void on_assistant_cancel();
      void on_assistant_close();
      void on_assistant_prepare(Gtk::Widget* widget);
      void on_entry_changed();

      // Member functions:
      void print_status();

      // Child widgets:
      Gtk::Box m_box;
      Gtk::Label m_label1, m_label2;
      Gtk::CheckButton m_check;
      Gtk::Entry m_entry;
    };

    #endif /* GTKMM_EXAMPLEASSISTANT_H */

File: `examplewindow.h` (For use with gtkmm 4)

    #ifndef GTKMM_EXAMPLEWINDOW_H
    #define GTKMM_EXAMPLEWINDOW_H

    #include "exampleassistant.h"
    #include <gtkmm.h>

    class ExampleWindow : public Gtk::Window
    {
    public:
      ExampleWindow();
      virtual ~ExampleWindow();

    private:
      // Signal handlers:
      void on_button_clicked();
      void on_assistant_apply();

      // Child widgets:
      Gtk::Grid m_grid;
      Gtk::Button m_button;
      Gtk::Label m_label1, m_label2;
      Gtk::CheckButton m_check;
      Gtk::Entry m_entry;
      ExampleAssistant m_assistant;
    };

    #endif /* GTKMM_EXAMPLEWINDOW_H */

File: `exampleassistant.cc` (For use with gtkmm 4)

    #include <iostream>
    #include "exampleassistant.h"

    ExampleAssistant::ExampleAssistant()
    : m_box(Gtk::Orientation::HORIZONTAL, 12),
      m_label1("Type text to allow the assistant to continue:"),
      m_label2("Confirmation page"),
      m_check("Optional extra information")
    {
      set_title("Gtk::Assistant example");
      set_default_size(400, 200);

      m_box.append(m_label1);
      m_box.append(m_entry);
      m_label1.set_wrap();
      m_label1.set_valign(Gtk::Align::CENTER);
      m_entry.set_valign(Gtk::Align::CENTER);

      append_page(m_box);
      append_page(m_check);
      append_page(m_label2);

      set_page_title(*get_nth_page(0), "Page 1");
      set_page_title(*get_nth_page(1), "Page 2");
      set_page_title(*get_nth_page(2), "Confirmation");

      set_page_complete(m_check, true);
      set_page_complete(m_label2, true);

      set_page_type(m_box, Gtk::AssistantPage::Type::INTRO);
      set_page_type(m_label2, Gtk::AssistantPage::Type::CONFIRM);

      signal_apply().connect(sigc::mem_fun(*this,
        &ExampleAssistant::on_assistant_apply));
      signal_cancel().connect(sigc::mem_fun(*this,
        &ExampleAssistant::on_assistant_cancel));
      signal_close().connect(sigc::mem_fun(*this,
        &ExampleAssistant::on_assistant_close));
      signal_prepare().connect(sigc::mem_fun(*this,
        &ExampleAssistant::on_assistant_prepare));

      m_entry.signal_changed().connect(sigc::mem_fun(*this,
        &ExampleAssistant::on_entry_changed));
    }

    ExampleAssistant::~ExampleAssistant()
    {
    }

    void ExampleAssistant::get_result(bool& check_state, Glib::ustring& entry_text)
    {
      check_state = m_check.get_active();
      entry_text = m_entry.get_text();
    }

    void ExampleAssistant::on_assistant_apply()
    {
      std::cout << "Apply was clicked";
      print_status();
    }

    void ExampleAssistant::on_assistant_cancel()
    {
      std::cout << "Cancel was clicked";
      print_status();
      set_visible(false);
    }

    void ExampleAssistant::on_assistant_close()
    {
      std::cout << "Assistant was closed";
      print_status();
      set_visible(false);
    }

    void ExampleAssistant::on_assistant_prepare(Gtk::Widget* /* widget */)
    {
      set_title(Glib::ustring::compose("Gtk::Assistant example (Page %1 of %2)",
        get_current_page() + 1, get_n_pages()));
    }

    void ExampleAssistant::on_entry_changed()
    {
      // The page is only complete if the entry contains text.
      if(m_entry.get_text_length())
        set_page_complete(m_box, true);
      else
        set_page_complete(m_box, false);
    }

    void ExampleAssistant::print_status()
    {
      std::cout << ", entry contents: \"" << m_entry.get_text()
        << "\", checkbutton status: " << m_check.get_active() << std::endl;
    }

File: `examplewindow.cc` (For use with gtkmm 4)

    #include "examplewindow.h"
    #include "exampleassistant.h"

    ExampleWindow::ExampleWindow()
    : m_button("Show the assistant"),
      m_label1("State of assistant checkbutton:", Gtk::Align::START, Gtk::Align::CENTER),
      m_label2("Contents of assistant entry:", Gtk::Align::START, Gtk::Align::CENTER)
    {
      set_title("Gtk::Assistant example");

      m_grid.set_row_homogeneous(true);
      m_grid.set_column_spacing(5);
      m_grid.set_margin(12);

      m_grid.attach(m_button, 0, 0, 2, 1);
      m_button.set_hexpand(true);
      m_button.set_valign(Gtk::Align::CENTER);

      m_grid.attach(m_label1, 0, 1, 1, 1);

      m_grid.attach(m_label2, 0, 2, 1, 1);

      m_grid.attach(m_check, 1, 1, 1, 1);
      m_check.set_halign(Gtk::Align::START);

      m_grid.attach(m_entry, 1, 2, 1, 1);
      m_entry.set_hexpand(true);

      set_child(m_grid);

      m_button.signal_clicked().connect(sigc::mem_fun(*this,
        &ExampleWindow::on_button_clicked));
      m_assistant.signal_apply().connect(sigc::mem_fun(*this,
        &ExampleWindow::on_assistant_apply));

      m_check.set_sensitive(false);
      m_entry.set_sensitive(false);
    }

    ExampleWindow::~ExampleWindow()
    {
    }

    void ExampleWindow::on_assistant_apply()
    {
      bool check_state;
      Glib::ustring entry_text;

      m_assistant.get_result(check_state, entry_text);
      m_check.set_active(check_state);
      m_entry.set_text(entry_text);
    }

    void ExampleWindow::on_button_clicked()
    {
      m_assistant.set_visible(true);
    }

File: `main.cc` (For use with gtkmm 4)

    #include "examplewindow.h"
    #include <gtkmm/application.h>

    int main(int argc, char *argv[])
    {
      auto app = Gtk::Application::create("org.gtkmm.example");

      // Shows the window and returns when it is closed.
      return app->make_window_and_run<ExampleWindow>(argc, argv);
    }

### Other Multi-item Containers

There are other multi-item containers. See the reference documentation for a complete list. Here are links to some example programs that show containers, which are not mentioned elsewhere in this tutorial.

Source Code, ActionBar

Source Code, FlowBox

Chapter 9. Container Widgets |  | Chapter 10. ListView, GridView, ColumnView