Sophie

Sophie

distrib > Mageia > 7 > i586 > by-pkgid > e40b0b2b839eccdb3c85993a7b738115 > files > 214

gtkmm-documentation-3.24.0-1.mga7.noarch.rpm

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>RadioButton</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="Programming with gtkmm 3">
<link rel="up" href="chapter-button-widget.html" title="Chapter 5. Buttons">
<link rel="prev" href="sec-checkboxes.html" title="CheckButton">
<link rel="next" href="chapter-range-widgets.html" title="Chapter 6. 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 5. 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">
<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">
<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")
{
  m_rb2.join_group(m_rb1);
  m_rb3.join_group(m_rb1);
}</pre>
<p>
We told <span class="application">gtkmm</span> to put all three <code class="classname">RadioButton</code>s in the
same group by using <code class="methodname">join_group()</code> to tell the other
<code class="classname">RadioButton</code>s to share group with the first
<code class="classname">RadioButton</code>.
</p>
<p>
Note that you can't do
</p>
<pre class="programlisting">m_rb2.set_group(m_rb1.get_group()); //doesn't work</pre>
<p>
because <code class="methodname">get_group()</code> returns a <code class="classname">RadioButton::Group</code>
which is modified by <code class="methodname">set_group()</code> and therefore is 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::make_managed&lt;Gtk::RadioButton&gt;(group, "button1");
  Gtk::RadioButton* m_rb2 = Gtk::make_managed&lt;Gtk::RadioButton&gt;(group, "button2");
  Gtk::RadioButton* m_rb3 = Gtk::make_managed&lt;Gtk::RadioButton&gt;(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">
<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://developer.gnome.org/gtkmm/3.24/classGtk_1_1RadioButton.html" target="_top">Reference</a></p>
</div>
<div class="sect2">
<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 5.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/browse/gtkmm-documentation/tree/examples/book/buttons/radiobutton?h=gtkmm-3-24" target="_top">Source Code</a></p>
<p>File: <code class="filename">radiobuttons.h</code> (For use with gtkmm 3, not gtkmm 2)
</p>
<pre class="programlisting">
#ifndef GTKMM_EXAMPLE_RADIOBUTTONS_H
#define GTKMM_EXAMPLE_RADIOBUTTONS_H

#include &lt;gtkmm/box.h&gt;
#include &lt;gtkmm/window.h&gt;
#include &lt;gtkmm/radiobutton.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::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
</pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 3, not gtkmm 2)
</p>
<pre class="programlisting">
#include "radiobuttons.h"
#include &lt;gtkmm/application.h&gt;

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-&gt;run(buttons);
}
</pre>
<p>File: <code class="filename">radiobuttons.cc</code> (For use with gtkmm 3, not gtkmm 2)
</p>
<pre class="programlisting">
#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,
              &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>
</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 6. Range Widgets</td>
</tr>
</table>
</div>
</body>
</html>