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
Existem duas maneiras de definir um grupo de botões de opção. A primeira é criar os botões e configurar os seus grupos em seguida. Somente os dois primeiros construtores são usados. No exemplo a seguir, fazemos uma nova classe janela chamada RadioButtons, e então colocamos três botões nela:
class RadioButtons : public Gtk::Window { public: RadioButtons(); protected: Gtk::RadioButton m_rb1, m_rb2, m_rb3; }; RadioButtons::RadioButtons() : m_rb1("botão1"), m_rb2("botão2"), m_rb3("botão3") { Gtk::RadioButton::Group grupo = m_rb1.get_group(); m_rb2.set_group(grupo); m_rb3.set_group(grupo); }
We told gtkmm to put all three RadioButtons in the same group by obtaining the group with get_group() and using set_group() to tell the other RadioButtons to share that group.
Note that you can't just do
m_rb2.set_group(m_rb1.get_group()); //doesn't work
The second way to set up radio buttons is to make a group first, and then add radio buttons to it. Here's an example:
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")); }
We made a new group by simply declaring a variable, group, of type Gtk::RadioButton::Group. Then we made three radio buttons, using a constructor to make each of them part of group.
5.4.2 Methods
RadioButtons are "off" when created; this means that when you first make a group of them, they will all be off. Don't forget to turn one of them on using set_active():
5.4.3 Exemplo
The following example demonstrates the use of RadioButtons:
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: 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: 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_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. }
File: main.cc (For use with gtkmm 3, not gtkmm 2)
#include "radiobuttons.h" #include <gtkmm/main.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; }