Conteneurs personnalisés

Quand vous dérivez de la classe Gtk::Container, vous pouvez surdéfinir les fonctions membres virtuelles suivantes :

  • get_request_mode_vfunc() : renvoie quel est l'objet Gtk::SizeRequestMode préféré par le conteneur.
  • get_preferred_width_vfunc() : calcule la largeur minimale naturelle du conteneur.
  • get_preferred_height_vfunc() : calcule la hauteur minimale naturelle du conteneur.
  • get_preferred_width_for_height_vfunc() : calcule la largeur minimale naturelle du conteneur compte tenu de la hauteur précisée.
  • get_preferred_height_for_width_vfunc() : calcule la hauteur minimale naturelle du conteneur compte tenu de la largeur précisée.
  • on_size_allocate() : place les éléments graphiques enfants, compte tenu de la hauteur et de la largeur réellement attribuées au conteneur.
  • forall_vfunc() : appelle la même fonction de rappel pour chaque enfant.
  • on_add() : ajoute un élément graphique enfant dans le conteneur.
  • on_remove() : supprime un élément graphique enfant du conteneur.
  • child_type_vfunc() : renvoie le type d'enfant pouvant être ajouté.

Les fonctions membres virtuelles get_request_mode_vfunc(), get_preferred_width_vfunc(), get_preferred_height_vfunc(), get_preferred_width_for_height_vfunc(), get_preferred_height_for_width_vfunc() et on_size_allocate() contrôlent la disposition des éléments graphiques enfants. Par exemple, si votre conteneur possède deux éléments graphiques enfants, l'un au dessus de l'autre, votre fonction membre get_preferred_width_vfunc() doit renvoyer la plus grande des largeurs des éléments graphiques enfants et get_preferred_height_for_width_vfunc() la somme des hauteurs. Si vous voulez de l'espace libre autour les éléments graphiques enfants, vous devez également l'additionner à la largeur et à la hauteur. Votre conteneur d'éléments graphiques se sert de ces résultats pour s'assurer que vos éléments graphiques ont assez de place. En examinant chaque parent de l'élément graphique, et les parents de celui-ci, cette logique détermine éventuellement la taille de la fenêtre de plus haut niveau.

Vous n'avez pas la garantie d'obtenir le Gtk::SizeRequestMode demandé. C'est pourquoi les quatre fonctions membres get_preferred_xxx_vfunc() doivent renvoyer des valeurs raisonnables.

on_size_allocate(), toutefois, reçoit les vraies hauteur et largeur que le conteneur parent a décidé de donner à l'élément graphique. Cela peut être plus que le minimum ou même plus que la taille naturelle, par exemple, si la fenêtre de haut niveau a été agrandie. Vous pouvez choisir d'ignorer l'espace supplémentaire et laisser la zone inoccupée, ou bien vous pouvez choisir d'agrandir vos éléments graphiques enfants pour remplir cet espace, ou bien encore vous pouvez choisir d'augmenter l'espace libre entre vos éléments graphiques. C'est votre conteneur, à vous de décider. N'oubliez pas d'appeler set_allocation() à l'intérieur de votre implémentation de on_size_allocate() pour utiliser effectivement l'espace alloué offert par le conteneur parent.

À moins que votre conteneur ne soit une fenêtre de haut niveau dérivée de Gtk::Window, vous devez également appeler Gtk::Widget::set_has_window(false) dans le constructeur. Cela signifie que votre conteneur ne crée pas sa propre Gdk::Window, mais utilise la fenêtre du parent. (Notez la différence entre Gtk::Window et Gdk::Window.) Si votre conteneur n'a pas besoin de sa propre Gdk::Window et ne dérive pas d'une Gtk::Window, vous devez aussi surdéfinir la fonction membre on_realize() comme cela est décrit dans la section Éléments graphiques personnalisés. Et, sauf si votre conteneur dessine directement dans la Gdk::Window sous-jacente, vous devrez probablement appeler set_redraw_on_allocate(false) pour améliorer les performances.

En surdéfinissant forall_vfunc() vous permettez aux applications d'opérer sur tous les éléments graphiques enfants du conteneur. Par exemple, show_all_children() utilise cette possibilité pour trouver tous les éléments graphiques enfants et les afficher.

Même si votre conteneur possède sa propre fonction membre pour définir ses éléments graphiques enfants, vous devez quand même fournir une implémentation des fonctions membres virtuelles on_add() et on_remove() de la classe de base de façon à ce que les fonctions membres add() et remove() fassent quelque chose d'approprié si elles sont appelées.

Votre implémentation de la fonction membre child_type_vfunc() doit renvoyer le type d'élément graphique pouvant être ajouté à votre conteneur s'il n'est pas encore rempli. Généralement, c'est à Gtk::Widget::get_type() d'indiquer quelles sont les classes dérivées de Gtk::Widget acceptées. Si le conteneur ne peut plus contenir d'autres éléments graphiques, cette fonction membre doit renvoyer la valeur G_TYPE_NONE.

XXVII.I.I. Exemple

Cet exemple implémente un conteneur avec deux éléments graphiques enfants, l'un au dessus de l'autre. Bien entendu, dans ce cas il aurait été de loin beaucoup plus simple d'utiliser Gtk::VBox.

Figure XXVII.1 Conteneur personnalisé

Code source

File: mycontainer.h (For use with gtkmm 3, not gtkmm 2)

#ifndef GTKMM_CUSTOM_CONTAINER_MYCONTAINER_H
#define GTKMM_CUSTOM_CONTAINER_MYCONTAINER_H

#include <gtkmm/container.h>

class MyContainer : public Gtk::Container
{
public:
  MyContainer();
  virtual ~MyContainer();

  void set_child_widgets(Gtk::Widget& child_one, Gtk::Widget& child_two);

protected:

  //Overrides:
  virtual Gtk::SizeRequestMode get_request_mode_vfunc() const;
  virtual void get_preferred_width_vfunc(int& minimum_width, int& natural_width) const;
  virtual void get_preferred_height_for_width_vfunc(int width, int& minimum_height, int& natural_height) const;
  virtual void get_preferred_height_vfunc(int& minimum_height, int& natural_height) const;
  virtual void get_preferred_width_for_height_vfunc(int height, int& minimum_width, int& natural_width) const;
  virtual void on_size_allocate(Gtk::Allocation& allocation);

  virtual void forall_vfunc(gboolean include_internals, GtkCallback callback, gpointer callback_data);

  virtual void on_add(Gtk::Widget* child);
  virtual void on_remove(Gtk::Widget* child);
  virtual GType child_type_vfunc() const;

  Gtk::Widget* m_child_one;
  Gtk::Widget* m_child_two;
};

#endif //GTKMM_CUSTOM_CONTAINER_MYCONTAINER_H

File: examplewindow.h (For use with gtkmm 3, not gtkmm 2)

#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

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

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

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

  //Child widgets:
  Gtk::VBox m_VBox;
  MyContainer m_MyContainer;
  Gtk::Button m_Button_One;
  Gtk::Label m_Label_Two;
  Gtk::HButtonBox m_ButtonBox;
  Gtk::Button m_Button_Quit;
};

#endif //GTKMM_EXAMPLEWINDOW_H

File: examplewindow.cc (For use with gtkmm 3, not gtkmm 2)

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

ExampleWindow::ExampleWindow()
: m_Button_One("Child One"),
  m_Label_Two("Child 2"),
  m_Button_Quit("Quit")
{
  set_title("Custom Container example");
  set_border_width(6);
  set_default_size(400, 200);

  add(m_VBox);

  //Add the child widgets to the custom container:
  m_MyContainer.set_child_widgets(m_Button_One, m_Label_Two);

  m_Label_Two.set_alignment(1.0, 0.5);

  m_VBox.pack_start(m_MyContainer, Gtk::PACK_EXPAND_WIDGET);
  m_VBox.pack_start(m_ButtonBox, Gtk::PACK_SHRINK);

  m_ButtonBox.pack_start(m_Button_Quit, Gtk::PACK_SHRINK);
  m_ButtonBox.set_border_width(6);
  m_ButtonBox.set_layout(Gtk::BUTTONBOX_END);
  m_Button_Quit.signal_clicked().connect( sigc::mem_fun(*this,
              &ExampleWindow::on_button_quit) );

  show_all_children();
}

ExampleWindow::~ExampleWindow()
{
}

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

File: main.cc (For use with gtkmm 3, not gtkmm 2)

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

int main(int argc, char *argv[])
{
  Gtk::Main kit(argc, argv);

  ExampleWindow window;
  //Shows the window and returns when it is closed.
  Gtk::Main::run(window);

  return 0;
}

File: mycontainer.cc (For use with gtkmm 3, not gtkmm 2)

#include <iostream>
#include <algorithm> // std::max
#include "mycontainer.h"

MyContainer::MyContainer()
: m_child_one(0), m_child_two(0)
{
  set_has_window(false);
  set_redraw_on_allocate(false);
}

MyContainer::~MyContainer()
{
}

void MyContainer::set_child_widgets(Gtk::Widget& child_one,
        Gtk::Widget& child_two)
{
  m_child_one = &child_one;
  m_child_two = &child_two;

  m_child_one->set_parent(*this);
  m_child_two->set_parent(*this);
}

//This example container is a simplified VBox with at most two children.
Gtk::SizeRequestMode MyContainer::get_request_mode_vfunc() const
{
  return Gtk::SIZE_REQUEST_HEIGHT_FOR_WIDTH;
}

//Discover the total amount of minimum space and natural space needed by
//this container and its children.
void MyContainer::get_preferred_width_vfunc(int& minimum_width, int& natural_width) const
{
  int child_minimum_width[2] = {0, 0};
  int child_natural_width[2] = {0, 0};

  if(m_child_one && m_child_one->get_visible())
    m_child_one->get_preferred_width(child_minimum_width[0], child_natural_width[0]);

  if(m_child_two && m_child_two->get_visible())
    m_child_two->get_preferred_width(child_minimum_width[1], child_natural_width[1]);

  //Request a width equal to the width of the widest visible child.
  minimum_width = std::max(child_minimum_width[0], child_minimum_width[1]);
  natural_width = std::max(child_natural_width[0], child_natural_width[1]);
}

void MyContainer::get_preferred_height_for_width_vfunc(int width,
   int& minimum_height, int& natural_height) const
{
  int child_minimum_height[2] = {0, 0};
  int child_natural_height[2] = {0, 0};
  int nvis_children = 0;

  if(m_child_one && m_child_one->get_visible())
  {
    ++nvis_children;
    m_child_one->get_preferred_height_for_width(width, child_minimum_height[0],
                                                child_natural_height[0]);
  }

  if(m_child_two && m_child_two->get_visible())
  {
    ++nvis_children;
    m_child_two->get_preferred_height_for_width(width, child_minimum_height[1],
                                                child_natural_height[1]);
  }

  //The allocated height will be divided equally among the visible children.
  //Request a height equal to the number of visible children times the height
  //of the highest child.
  minimum_height = nvis_children * std::max(child_minimum_height[0],
                                            child_minimum_height[1]);
  natural_height = nvis_children * std::max(child_natural_height[0],
                                            child_natural_height[1]);
}

void MyContainer::get_preferred_height_vfunc(int& minimum_height, int& natural_height) const
{
  int child_minimum_height[2] = {0, 0};
  int child_natural_height[2] = {0, 0};
  int nvis_children = 0;

  if(m_child_one && m_child_one->get_visible())
  {
    ++nvis_children;
    m_child_one->get_preferred_height(child_minimum_height[0], child_natural_height[0]);
  }

  if(m_child_two && m_child_two->get_visible())
  {
    ++nvis_children;
    m_child_two->get_preferred_height(child_minimum_height[1], child_natural_height[1]);
  }

  //The allocated height will be divided equally among the visible children.
  //Request a height equal to the number of visible children times the height
  //of the highest child.
  minimum_height = nvis_children * std::max(child_minimum_height[0],
                                            child_minimum_height[1]);
  natural_height = nvis_children * std::max(child_natural_height[0],
                                            child_natural_height[1]);
}

void MyContainer::get_preferred_width_for_height_vfunc(int height,
   int& minimum_width, int& natural_width) const
{
  int child_minimum_width[2] = {0, 0};
  int child_natural_width[2] = {0, 0};
  int nvis_children = 0;

  //Get number of visible children.
  if(m_child_one && m_child_one->get_visible())
    ++nvis_children;
  if(m_child_two && m_child_two->get_visible())
    ++nvis_children;

  if(nvis_children > 0)
  {
    //Divide the height equally among the visible children.
    const int height_per_child = height / nvis_children;

    if(m_child_one && m_child_one->get_visible())
      m_child_one->get_preferred_width_for_height(height_per_child,
                   child_minimum_width[0], child_natural_width[0]);

    if(m_child_two && m_child_two->get_visible())
      m_child_two->get_preferred_width_for_height(height_per_child,
                   child_minimum_width[1], child_natural_width[1]);
  }

  //Request a width equal to the width of the widest child.
  minimum_width = std::max(child_minimum_width[0], child_minimum_width[1]);
  natural_width = std::max(child_natural_width[0], child_natural_width[1]);
}

void MyContainer::on_size_allocate(Gtk::Allocation& allocation)
{
  //Do something with the space that we have actually been given:
  //(We will not be given heights or widths less than we have requested, though
  //we might get more.)

  //Use the offered allocation for this container:
  set_allocation(allocation);

  //Get number of visible children.
  int nvis_children = 0;
  if(m_child_one && m_child_one->get_visible())
    ++nvis_children;
  if(m_child_two && m_child_two->get_visible())
    ++nvis_children;

  if(nvis_children <= 0)
    return;

  //Assign space to the children:
  Gtk::Allocation child_allocation_one;
  Gtk::Allocation child_allocation_two;

  //Place the first child at the top-left:
  child_allocation_one.set_x( allocation.get_x() );
  child_allocation_one.set_y( allocation.get_y() );

  //Make it take up the full width available:
  child_allocation_one.set_width( allocation.get_width() );

  if(m_child_one && m_child_one->get_visible())
  {
    //Divide the height equally among the visible children.
    child_allocation_one.set_height( allocation.get_height() / nvis_children);
    m_child_one->size_allocate(child_allocation_one);
  }
  else
    child_allocation_one.set_height(0);

  //Place the second child below the first child:
  child_allocation_two.set_x( allocation.get_x() );
  child_allocation_two.set_y( allocation.get_y() +
          child_allocation_one.get_height());

  //Make it take up the full width available:
  child_allocation_two.set_width( allocation.get_width() );

  //Make it take up the remaining height:
  child_allocation_two.set_height( allocation.get_height() -
          child_allocation_one.get_height());

  if(m_child_two && m_child_two->get_visible())
    m_child_two->size_allocate(child_allocation_two);
}

void MyContainer::forall_vfunc(gboolean, GtkCallback callback, gpointer callback_data)
{
  if(m_child_one)
    callback(m_child_one->gobj(), callback_data);

  if(m_child_two)
    callback(m_child_two->gobj(), callback_data);
}

void MyContainer::on_add(Gtk::Widget* child)
{
  if(!m_child_one)
  {
    m_child_one = child;
    m_child_one->set_parent(*this);
  }
  else if(!m_child_two)
  {
    m_child_two = child;
    m_child_two->set_parent(*this);
  }
}

void MyContainer::on_remove(Gtk::Widget* child)
{
  if(child)
  {
    const bool visible = child->get_visible();
    bool found = false;

    if(child == m_child_one)
    {
      m_child_one = 0;
      found = true;
    }
    else if(child == m_child_two)
    {
      m_child_two = 0;
      found = true;
    }

    if(found)
    {
      child->unparent();

      if(visible)
        queue_resize();
    }
  }
}

GType MyContainer::child_type_vfunc() const
{
  //If there is still space for one widget, then report the type of widget that
  //may be added.
  if(!m_child_one || !m_child_two)
    return Gtk::Widget::get_type();
  else
  {
    //No more widgets may be added.
    return G_TYPE_NONE;
  }
}