ラジオボタン

チェックボックスと同様にラジオボタンもGtk::ToggleButtonを継承しています。しかし、これはグループで動作し、一つのグループにラジオボタンが一つしかないときはボタンは一度しか選択することができません。

グループ

ラジオボタンをグループ内に設置するには2つ方法があります。最初の方法はボタンを生成し、その後でグループに設置するものです。最初の2つのコンストラクタだけが使われます。次の例ではRadioButtonsという名の新しいウィンドウクラスを作り、それから3つのラジオボタンをその中に置きます:

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")
{
    Gtk::RadioButton::Group group = m_rb1.get_group();
    m_rb2.set_group(group);
    m_rb3.set_group(group);
}

ここではgtkmmに3つ全てのRadioButtonを同じグループに配置させています。get_group()メソッドでグループを取得し、それを共有させたい他のRadioButtonset_group()を使って設定します。

覚えていて欲しいのは単に次のようにするだけでは動作しないことです

m_rb2.set_group(m_rb1.get_group()); //doesn't work

なぜなら、グループはset_group()で変更されるので const ではないからです。

ふたつ目の方法はまずグループを作り、ラジオボタンをそこに加えるものです。次がその例です:

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 = manage(
      new Gtk::RadioButton(group,"button2"));
      Gtk::RadioButton *m_rb3 = manage(
        new Gtk::RadioButton(group,"button3"));
}

ここでは新しいグループを簡単にGtk::RadioButton::Group型の変数、groupとして宣言しています。それから三つのラジオボタンを作り、コンストラクタを使って、groupの中へ納めていきます。

メソッド

RadioButtonは生成された段階では"オフ"の状態です。つまり、ボタンを納めたグループを作ると、それらは全てオフの状態になります。そのうちの一つをset_active()でオンにするのを忘れないでください。

Reference

次の例ではRadioButtonのデモをしています:

Figure 4.3. Radio Button

Radio Button

Source Code

File: radiobuttons.h

#ifndef GTKMM_EXAMPLE_RADIOBUTTONS_H
#define GTKMM_EXAMPLE_RADIOBUTTONS_H

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

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

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

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

#endif //GTKMM_EXAMPLE_RADIOBUTTONS_H

File: main.cc

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

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

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

  return 0;
}

File: radiobuttons.cc

#include "radiobuttons.h"


RadioButtons::RadioButtons() :
  m_Box1(false, 10),
  m_Box2(false, 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:
  Gtk::RadioButton::Group group = m_RadioButton1.get_group();
  m_RadioButton2.set_group(group);
  m_RadioButton3.set_group(group);

  // 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_flags(Gtk::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.
}