1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="highlight.min.css">
<script src="highlight.min.js"></script><script>
hljs.configure({languages: ['cpp']});
hljs.highlightAll();
</script><title>Radio Button</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Programming with gtkmm 4">
<link rel="up" href="chapter-button-widget.html" title="Chapter 6. Buttons">
<link rel="prev" href="sec-checkbuttons.html" title="CheckButton">
<link rel="next" href="chapter-range-widgets.html" title="Chapter 7. 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">Radio Button</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-checkbuttons.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 6. 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="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-radio-buttons"></a>Radio Button</h2></div></div></div>
<p>
There is no separate class for radio buttons. Check buttons and toggle buttons
act as radio buttons when they form a group. Only one button in a group can be
selected at any one time.
</p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="radiobutton-groups"></a>Groups</h3></div></div></div>
<p>
You create the buttons, and set up their group afterwards. In the following example,
we put 3 radio buttons in a group:
</p>
<pre class="programlisting"><code class="code">auto rb1 = Gtk::make_managed<Gtk::CheckButton>("button1");
auto rb2 = Gtk::make_managed<Gtk::CheckButton>("button2");
auto rb3 = Gtk::make_managed<Gtk::CheckButton>("button3");
rb2->set_group(*rb1);
rb3->set_group(*rb1);
</code></pre>
<p>
We told <span class="application">gtkmm</span> to put all three <code class="classname">CheckButton</code>s in the
same group by using <code class="methodname">set_group()</code> to tell the other
<code class="classname">CheckButton</code>s to share group with the first
<code class="classname">CheckButton</code>.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="radiobutton-methods"></a>Methods</h3></div></div></div>
<p>
<code class="classname">CheckButton</code>s and <code class="classname">ToggleButton</code>s 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="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1RadioButton.html" target="_top">Reference</a></p>
</div>
<div class="section">
<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 grouped
<code class="classname">CheckButton</code>s:
</p>
<div class="figure">
<a name="figure-radiobutton"></a><p class="title"><b>Figure 6.3. RadioButton</b></p>
<div class="figure-contents">
<div class="screenshot">
<div class="mediaobject"><img src="figures/radiobuttons.png" alt="RadioButton"></div>
</div>
</div>
</div>
<br class="figure-break">
<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/buttons/radiobutton" target="_top">Source Code</a></p>
<p>File: <code class="filename">radiobuttons.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLE_RADIOBUTTONS_H
#define GTKMM_EXAMPLE_RADIOBUTTONS_H
#include <gtkmm/box.h>
#include <gtkmm/window.h>
#include <gtkmm/button.h>
#include <gtkmm/checkbutton.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::CheckButton m_RadioButton1, m_RadioButton2, m_RadioButton3;
Gtk::Separator m_Separator;
Gtk::Button m_Button_Close;
};
#endif //GTKMM_EXAMPLE_RADIOBUTTONS_H
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "radiobuttons.h"
#include <gtkmm/application.h>
int main(int argc, char *argv[])
{
auto app = Gtk::Application::create("org.gtkmm.example");
//Shows the window and returns when it is closed.
return app->make_window_and_run<RadioButtons>(argc, argv);
}
</code></pre>
<p>File: <code class="filename">radiobuttons.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#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");
// Gtk::CheckButton and Gtk::ToggleButton have set_group() methods.
// They act as radio buttons, if they are included in a group.
// Put radio buttons 2 and 3 in the same group as 1:
m_RadioButton2.set_group(m_RadioButton1);
m_RadioButton3.set_group(m_RadioButton1);
// Add outer box to the window (because the window
// can only contain a single widget)
set_child(m_Box_Top);
//Put the inner boxes and the separator in the outer box:
m_Box_Top.append(m_Box1);
m_Box_Top.append(m_Separator);
m_Box_Top.append(m_Box2);
m_Separator.set_expand();
// Set the inner boxes' margins
m_Box1.set_margin(10);
m_Box2.set_margin(10);
// Put the radio buttons in Box1:
m_Box1.append(m_RadioButton1);
m_Box1.append(m_RadioButton2);
m_Box1.append(m_RadioButton3);
m_RadioButton1.set_expand();
m_RadioButton2.set_expand();
m_RadioButton3.set_expand();
// Set the second button active
m_RadioButton2.set_active(true);
// Put Close button in Box2:
m_Box2.append(m_Button_Close);
m_Button_Close.set_expand();
// Make the button the default widget
set_default_widget(m_Button_Close);
// Connect the toggled signal of the button to
// RadioButtons::on_button_toggled()
m_Button_Close.signal_clicked().connect(sigc::mem_fun(*this,
&RadioButtons::on_button_clicked) );
}
RadioButtons::~RadioButtons()
{
}
void RadioButtons::on_button_clicked()
{
set_visible(false); //to close the application.
}
</code></pre>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-checkbuttons.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 7. Range Widgets</td>
</tr>
</table>
</div>
</body>
</html>
|