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.

9.2.1. Empaquetado

Probablemente ya haya notado que las ventanas de gtkmm parecen «elásticas»: normalmente pueden estirarse de muchas maneras diferentes. Esto es así por el sistema de empaquetado de widgets.

Muchos conjuntos de herramientas de la IGU le requieren ubicar precisamente widgets en una ventana, utilizando posicionamiento absoluto, a menudo usando un editor visual. Esto lleva a muchos problemas:

  • Los widgets no se reordenar cuando la ventana se redimensiona. Algunos se esconden cuando las ventanas se hacen más pequeñas, y aparece un montón de espacio sin utilizar cuando la ventana se agranda.
  • Es imposible predecir la cantidad de espacio necesaria para texto después de que se ha traducido a otros idiomas, o se ha mostrado en otra tipografía. En Unix, también es imposible anticipar los efectos de cada tema y gestor de ventanas.
  • Cambiar la disposición de una ventana «al vuelo» para, por ejemplo, hacer que algunos widgets adicionales aparezcan, es complejo. Requiere un cálculo tedioso de la posición de cada widget.

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 flavours of containers: single-child containers and multiple-child containers. Most container widgets in gtkmm are single-child containers, including Gtk::Window.

Sí, es correcto: una ventana sólo puede contener un widget. Entonces, ¿cómo puede usar una ventana para algo útil? Poniendo un contenedor de múltiples hijos en la ventana. Los widgets contenedores más útiles son Gtk::Grid y 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.

Hay muchos más contenedores, de los que también se hablará.

Si nunca ha usado un kit de herramientas de empaquetado antes, puede llevar algo de tiempo acostumbrarse a él. Probablemente encuentre, sin embargo, que no necesita editores de formularios visuales tanto como con otros kits de herramientas.

9.2.2. Un «Hola mundo» mejorado

Eche un vistazo a un helloworld ligeramente mejorado, mostrando lo que ha aprendido.

Figura 9-5Hola mundo 2

Código fuente

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: 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);
}

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::show() 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;
}

After building and running this program, try resizing the window to see the behaviour. Also, try playing with set_expand(), set_hexpand(), set_vexpand(), set_halign() and set_valign() while reading the Boxes section.

9.2.3. Cajas

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.

9.2.3.1. Añadir widgets

9.2.3.1.1. Opciones de empaquetado por hijo

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 glade GUI designer to see what is possible. You might even decide to use the Gtk::Builder API to load your GUI at runtime.

Básicamente, hay cinco estilos diferentes, como se muestra en esta imagen.

Figura 9-6Caja de empaquetado 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.

Referencia

9.2.3.1.2. Opciones de empaquetado por contenedor

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.

Figura 9-7Caja de empaquetado 2

9.2.3.2. Gtk::Application y opciones de línea de comandos

El siguiente programa de ejemplo requiere una opción de línea de comandos. El código fuente muestra dos maneras de manejarla, en combinación con 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.

    Debe establecer el parámetro opcional after = false en la llamada a signal_command_line().connect(), porque su gestor de señales debe llamarse antes que el gestor predeterminado. También debe llamar a Gio::Application::activate() en este, a menos que quiere que su aplicación se cierre sin mostrar su ventana principal. (Gio::Application es una clase base de Gtk::Application).

9.2.3.3. Ejemplo

Aquí está el código fuente del ejemplo que produjo las capturas de pantalla anteriores. Cuando ejecute este ejemplo, proporcione un número entre 1 y 3 como opción de línea de comandos, para ver las diferentes opciones de empaquetado en acción.

Código fuente

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: 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(sigc::bind(sigc::ptr_fun(&on_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: 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()
{
  hide();
}

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);
  }
}

9.2.4. Grid

Un Grid establece dinámicamente widgets hijos en filas y columnas. No es necesario especificar las dimensiones de la red en el constructor.

Los widgets hijos pueden abarcar múltiples filas o columnas, usando attach(), o añadirse a un widget existente dentro de la cuadrícula con attach_next_to(). Se puede establecer que las filas o columnas individuales de la cuadrícula tengan una altura o ancho uniforme con set_row_homogeneous() y set_column_homogeneous().

Puede establecer las propiedades margin y expand de los Widget hijos para controlar su espaciado y comportamiento cuando se redimensiona la cuadrícula.

Referencia

9.2.4.1. Ejemplo

Este ejemplo crea una ventana con tres botones en una cuadrícula. Los dos primeros botones están en la fila superior, de izquierda a derecha. Se ha añadido un tercer botón bajo el primer botón, en una nueva fila más abajo, abarcando dos columnas.

Figura 9-8Grid

Código fuente

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: 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);
}

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()
{
  hide();
}

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

9.2.5. Cuaderno

Un Notebook tiene un conjunto de páginas apiladas, cada una de ellas contiene widgets. Las pestañas etiquetadas permiten al usuario seleccionar las páginas. Los Notebook permiten colocar varios conjuntos de widgets en un espacio reducido, mostrando sólo una página a la vez. Por ejemplo, se utilizan a menudo en los diálogos de preferencias.

Use los métodos append_page(), prepend_page() e insert_page() para añadir páginas con pestañas al Notebook, proporcionándoles el widget hijo y el nombre de la pestaña.

Para descubrir la página visible actual, use el método get_current_page(). Esto devuelve el número de página. Después llame a get_nth_page() con ese número le dará un puntero al widget hijo en sí.

Para cambiar la página seleccionada mediante programación, use el método set_current_page().

Referencia

9.2.5.1. Ejemplo

Figura 9-9Cuaderno

Código fuente

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: 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);
}

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()
{
  hide();
}

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.
}

9.2.6. Asistente

Un Assistant divide una operación compleja en pasos. Cada paso es una página, conteniendo una cabecera, un widget hijo, y un área de acción. El área de acción del asistente tiene botones de navegación que se actualizan automáticamente dependiendo del tipo de la página, establecido con set_page_type().

Use los métodos append_page(), prepend_page e insert_page() para añadirle páginas al Assistant, proporcionándole el widget hijo por cada página.

Para determinar la página actualmente visible, use el método get_current_page() y pásele el resultado a get_nth_page(), que le devuelve un puntero al widget en sí. Para cambiar mediante programación la página actual, use el método set_current_page().

To set the title of a page, use the set_page_title() method.

Para añadir widgets al área de acción, use el método add_action_widget(). Serán empaquetados junto a los botones predeterminados. Use el método remove_action_widget() para borrar los widgets.

Referencia

9.2.6.1. Ejemplo

Figura 9-10Asistente

Código fuente

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.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: 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);
}

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.show();
}

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();
  hide();
}

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

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;
}

9.2.7. 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

Source Code, IconView