Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 345aa895e80053137c21f8693106c3a0 > files > 175

gtkmm2.4-documentation-2.17.4-1mdv2010.0.noarch.rpm

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>RadioButton</title>
<link rel="stylesheet" href="style.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.75.1">
<link rel="home" href="index.html" title="Programming with gtkmm">
<link rel="up" href="chapter-button-widget.html" title="Chapter 4. Buttons">
<link rel="prev" href="sec-checkboxes.html" title="CheckButton">
<link rel="next" href="chapter-range-widgets.html" title="Chapter 5. Range Widgets">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">RadioButton</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-checkboxes.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 4. Buttons</th>
<td width="20%" align="right"> <a accesskey="n" href="chapter-range-widgets.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="RadioButton">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-radio-buttons"></a>RadioButton</h2></div></div></div>
<p>
Like checkboxes, radio buttons also inherit from
<code class="classname">Gtk::ToggleButton</code>, but these work in groups, and only
one RadioButton in a group can be selected at any one time.
</p>
<div class="sect2" title="Groups">
<div class="titlepage"><div><div><h3 class="title">
<a name="radiobutton-groups"></a>Groups</h3></div></div></div>
<p>
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 first two constructors are used.  In the following example, we
make a new window class called <code class="classname">RadioButtons</code>, and then
put three radio buttons in it:
</p>
<pre class="programlisting">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);
}</pre>
<p>
We told <span class="application">gtkmm</span> to put all three <code class="classname">RadioButton</code>s in the
same group by obtaining the group with <code class="methodname">get_group()</code> and using
<code class="methodname">set_group()</code> to tell the other
<code class="classname">RadioButton</code>s to share that group.
</p>
<p>
Note that you can't just do
</p>
<pre class="programlisting">m_rb2.set_group(m_rb1.get_group()); //doesn't work</pre>
<p>
because the group is modified by <code class="methodname">set_group()</code> and therefore
non-const.
</p>
<p>
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:
</p>
<pre class="programlisting">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"));
}</pre>
<p>
We made a new group by simply declaring a variable, <code class="literal">group</code>,
of type <code class="classname">Gtk::RadioButton::Group</code>.  Then we made three radio
buttons, using a constructor to make each of them part of
<code class="literal">group</code>.
</p>
</div>
<div class="sect2" title="Methods">
<div class="titlepage"><div><div><h3 class="title">
<a name="radiobutton-methods"></a>Methods</h3></div></div></div>
<p>
<code class="classname">RadioButtons</code> 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 <code class="methodname">set_active()</code>:
</p>
<p><a class="ulink" href="http://library.gnome.org/devel/gtkmm/unstable/classGtk_1_1RadioButton.html" target="_top">Reference</a></p>
</div>
<div class="sect2" title="Example">
<div class="titlepage"><div><div><h3 class="title">
<a name="radiobutton-example"></a>Example</h3></div></div></div>
<p>
The following example demonstrates the use of
<code class="classname">RadioButton</code>s:
</p>
<div class="figure">
<a name="figure-radiobutton"></a><p class="title"><b>Figure 4.3. RadioButton</b></p>
<div class="figure-contents"><div class="screenshot"><div><img src="figures/radiobuttons.png" alt="RadioButton"></div></div></div>
</div>
<br class="figure-break"><p><a class="ulink" href="http://git.gnome.org/cgit/gtkmm-documentation/tree/examples/book/buttons/radiobutton" target="_top">Source Code</a></p>
<p>File: <code class="filename">radiobuttons.h</code>
</p>
<pre class="programlisting">
#ifndef GTKMM_EXAMPLE_RADIOBUTTONS_H
#define GTKMM_EXAMPLE_RADIOBUTTONS_H

#include &lt;gtkmm/window.h&gt;
#include &lt;gtkmm/radiobutton.h&gt;
#include &lt;gtkmm/box.h&gt;
#include &lt;gtkmm/separator.h&gt;

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

protected:
  //Signal handlers:
  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
</pre>
<p>File: <code class="filename">radiobuttons.cc</code>
</p>
<pre class="programlisting">
#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,
              &amp;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.
}
</pre>
<p>File: <code class="filename">main.cc</code>
</p>
<pre class="programlisting">
#include &lt;gtkmm/main.h&gt;
#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;
}
</pre>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-checkboxes.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-button-widget.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="chapter-range-widgets.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">CheckButton </td>
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td>
<td width="40%" align="right" valign="top"> Chapter 5. Range Widgets</td>
</tr>
</table>
</div>
</body>
</html>