Bouton radio

De même que les cases à cocher, les boutons radio héritent aussi des propriétés des Gtk::ToggleButton, mais les boutons radio fonctionnent en groupe et, dans un groupe, un seul et unique bouton radio peut être sélectionné à un moment donné.

V.IV.I. Groupes

Il y a deux façons de définir un groupe de boutons radio. La première est de créer les boutons et de définir leur groupement après-coup. Seuls les deux premiers constructeurs sont utilisés dans ce cas. Dans l'exemple suivant, nous créons une nouvelle classe de fenêtre nommée RadioButtons dans laquelle nous plaçons trois boutons radio :

class RadioButtons : public Gtk::Window
{
public:
  RadioButtons();

protected:
  Gtk::RadioButton m_rb1, m_rb2, m_rb3;
};

RadioButtons::RadioButtons()
: m_rb1("button1"),
  m_rb2("button2"),
  m_rb3("button3")
{
  m_rb2.join_group(m_rb1);
  m_rb3.join_group(m_rb1);
}

We told gtkmm to put all three RadioButtons in the same group by using join_group() to tell the other RadioButtons to share group with the first RadioButton.

Note that you can't do

m_rb2.set_group(m_rb1.get_group()); //doesn't work
because get_group() returns a RadioButton::Group which is modified by set_group() and therefore is non-const.

La deuxième façon pour grouper des boutons radio consiste à créer un groupement au préalable, puis y intégrer les boutons. Voici un exemple :

class RadioButtons : public Gtk::Window
{
public:
  RadioButtons();
};

RadioButtons::RadioButtons()
{
  Gtk::RadioButton::Group group;
  Gtk::RadioButton* m_rb1 = Gtk::manage(new Gtk::RadioButton(group, "button1"));
  Gtk::RadioButton* m_rb2 = Gtk::manage(new Gtk::RadioButton(group, "button2"));
  Gtk::RadioButton* m_rb3 = Gtk::manage(new Gtk::RadioButton(group, "button3"));
}

Nous générons un nouveau groupe par une simple déclaration de variable, group, du type Gtk::RadioButton::Group, puis nous créons trois boutons radio avec le constructeur indiquant qu'ils font partie du groupe group.

V.IV.II. Fonctions membres

À leur création, les objets de la classe RadioButtons sont « inactifs », donc, lors de la création du groupe, tous les boutons sont désactivés. N'oubliez donc pas d'en activer un avec set_active() :

Reference

V.IV.III. Exemple

L'exemple suivant montre l'utilisation de la classe RadioButton :

Figure V.3 Bouton radio

Source Code

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

#ifndef GTKMM_EXAMPLE_RADIOBUTTONS_H
#define GTKMM_EXAMPLE_RADIOBUTTONS_H

#include <gtkmm/box.h>
#include <gtkmm/window.h>
#include <gtkmm/radiobutton.h>
#include <gtkmm/separator.h>

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

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

  //Child widgets:
  Gtk::Box m_Box_Top, m_Box1, m_Box2;
  Gtk::RadioButton m_RadioButton1, m_RadioButton2, m_RadioButton3;
  Gtk::Separator m_Separator;
  Gtk::Button m_Button_Close;
};

#endif //GTKMM_EXAMPLE_RADIOBUTTONS_H

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

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

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

  RadioButtons buttons;

  //Shows the window and returns when it is closed.
  return app->run(buttons);
}

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

#include "radiobuttons.h"


RadioButtons::RadioButtons() :
  m_Box_Top(Gtk::ORIENTATION_VERTICAL),
  m_Box1(Gtk::ORIENTATION_VERTICAL, 10),
  m_Box2(Gtk::ORIENTATION_VERTICAL, 10),
  m_RadioButton1("button1"),
  m_RadioButton2("button2"),
  m_RadioButton3("button3"),
  m_Button_Close("close")
{
  // Set title and border of the window
  set_title("radio buttons");
  set_border_width(0);

  // Put radio buttons 2 and 3 in the same group as 1:
  m_RadioButton2.join_group(m_RadioButton1);
  m_RadioButton3.join_group(m_RadioButton1);

  // Add outer box to the window (because the window
  // can only contain a single widget)
  add(m_Box_Top);

  //Put the inner boxes and the separator in the outer box:
  m_Box_Top.pack_start(m_Box1);
  m_Box_Top.pack_start(m_Separator);
  m_Box_Top.pack_start(m_Box2);

  // Set the inner boxes' borders
  m_Box2.set_border_width(10);
  m_Box1.set_border_width(10);

  // Put the radio buttons in Box1:
  m_Box1.pack_start(m_RadioButton1);
  m_Box1.pack_start(m_RadioButton2);
  m_Box1.pack_start(m_RadioButton3);

  // Set the second button active
  m_RadioButton2.set_active();

  // Put Close button in Box2:
  m_Box2.pack_start(m_Button_Close);

  // Make the button the default widget
  m_Button_Close.set_can_default();
  m_Button_Close.grab_default();

  // Connect the clicked signal of the button to
  // RadioButtons::on_button_clicked()
  m_Button_Close.signal_clicked().connect(sigc::mem_fun(*this,
              &RadioButtons::on_button_clicked) );

  // Show all children of the window
  show_all_children();
}

RadioButtons::~RadioButtons()
{
}

void RadioButtons::on_button_clicked()
{
  hide(); //to close the application.
}