Botão de opção

Como os de seleção, botões de opção também é uma classe filho de Gtk::ToggleButton, mas eles trabalham em grupos, e só um botão de opção em um grupo pode estar selecionado por vez.

5.4.1. Grupos

There are two ways to set up a group of radio buttons. The first way is to create the buttons, and set up their groups afterwards. Only the constructors without a Gtk::RadioButton::Group parameter are used. In the following example, we put 3 radio buttons in a group:

auto rb1 = Gtk::make_managed<Gtk::RadioButton>("button1");
auto rb2 = Gtk::make_managed<Gtk::RadioButton>("button2");
auto rb3 = Gtk::make_managed<Gtk::RadioButton>("button3");
rb2->join_group(*rb1);
rb3->join_group(*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.

The purpose of join_group() is to make it easier to express that you want a given button to use a group established by another. There is another way to do this, using get_group() and set_group(). However, this has a pitfall: Note that you can't do

rb2->set_group(rb1->get_group());
because get_group() returns a RadioButton::Group by value, and an unnamed rvalue cannot be passed as the lvalue reference needed by set_group(). Instead, you must assign the result of get_group() to a variable and pass that to set_group() as an lvalue:

auto rb1 = Gtk::make_managed<Gtk::RadioButton>("button1");
auto rb2 = Gtk::make_managed<Gtk::RadioButton>("button2");
auto rb3 = Gtk::make_managed<Gtk::RadioButton>("button3");
auto group = rb1->get_group();
rb2->set_group(group);
rb3->set_group(group);

This works because the group is really a handle and therefore can be discarded once it has been used to tell the radio buttons to group with each other. Still, as join_group() does the same thing in one less line, you might not find much use for this pattern, but it is shown for completeness.

A segunda maneira de configurar os botões de opção é criar um grupo primeiro, e depois adicionar botões de opção a ele. Aqui está um exemplo:

Gtk::RadioButton::Group group;
auto rb1 = Gtk::make_managed<Gtk::RadioButton>(group, "button1");
auto rb2 = Gtk::make_managed<Gtk::RadioButton>(group, "button2");
auto rb3 = Gtk::make_managed<Gtk::RadioButton>(group, "button3");

Criar um novo grupo, simplesmente declarando uma variável group, do tipo Gtk::RadioButton::Group. Depois criamos três botões de opção, usando um construtor para fazer cada um parte de group.

5.4.2. Métodos

RadioButtons são "desligado" quando criado; o que significa que quando você faz um grupo deles, todos eles irão estar desligado. Não esqueça de ativar um deles na utilização com set_active():

Reference

5.4.3. Exemplo

O exemplo seguinte demonstra o uso de RadioButtons:

Figure 5-3Botão de opção

Source Code

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

#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)

#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)

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