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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
<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>Chapter 6. Buttons</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="index.html" title="Programming with gtkmm 4">
<link rel="prev" href="changes-gtkmm4.html" title="Chapter 5. Changes in gtkmm-4.0 and glibmm-2.68">
<link rel="next" href="sec-toggle-buttons.html" title="ToggleButton">
</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">Chapter 6. Buttons</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="changes-gtkmm4.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center"> </th>
<td width="20%" align="right"> <a accesskey="n" href="sec-toggle-buttons.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="chapter">
<div class="titlepage"><div><div><h1 class="title">
<a name="chapter-button-widget"></a>Chapter 6. Buttons</h1></div></div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<ul class="toc">
<li><span class="section"><a href="chapter-button-widget.html#sec-pushbuttons">Button</a></span></li>
<li><span class="section"><a href="sec-toggle-buttons.html">ToggleButton</a></span></li>
<li><span class="section"><a href="sec-checkbuttons.html">CheckButton</a></span></li>
<li><span class="section"><a href="sec-radio-buttons.html">Radio Button</a></span></li>
</ul>
</div>
<p>
<span class="application">gtkmm</span> provides four basic types of buttons:
</p>
<div class="variablelist"><dl class="variablelist">
<dt><span class="term">Push buttons</span></dt>
<dd>
<p>
<a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1Button.html" target="_top"><code class="classname">Gtk::Button</code></a>. Standard buttons, usually
marked with a label or picture. Pushing one triggers an action. See the <a class="link" href="chapter-button-widget.html#sec-pushbuttons" title="Button">Button</a> section.
</p>
</dd>
<dt><span class="term">Toggle buttons</span></dt>
<dd>
<p>
<a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1ToggleButton.html" target="_top"><code class="classname">Gtk::ToggleButton</code></a>.
Unlike a normal Button, which springs back up, a ToggleButton stays down until you
press it again. It might be useful as an on/off switch. See the <a class="link" href="sec-toggle-buttons.html" title="ToggleButton">ToggleButton</a> section.
</p>
</dd>
<dt><span class="term">Check buttons</span></dt>
<dd>
<p>
<a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1CheckButton.html" target="_top"><code class="classname">Gtk::CheckButton</code></a>.
These act like ToggleButtons, but show their state in small squares,
with their label at the side. They should be used in most situations
which require an on/off setting.
See the <a class="link" href="sec-checkbuttons.html" title="CheckButton">CheckButton</a> section.
</p>
</dd>
<dt><span class="term">Radio buttons</span></dt>
<dd>
<p>
Named after the station selectors on old car
radios, these buttons are used in groups for options which are
mutually exclusive. Pressing one causes all the
others in its group to turn off. They are similar to ToggleButtons or
CheckButtons (a small widget with a label at the side), but usually
look different. There is no separate radio button class. Check buttons
and toggle buttons can act as radio buttons.
See the <a class="link" href="sec-radio-buttons.html" title="Radio Button">Radio Button</a> section.
</p>
</dd>
</dl></div>
<p>
Note that, due to GTK's theming system, the appearance of these
widgets will vary. In the case of check buttons and radio buttons, they
may vary considerably.
</p>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-pushbuttons"></a>Button</h2></div></div></div>
<p>
There are two ways to create a Button. You can specify a label
string in the <code class="classname">Gtk::Button</code> constructor,
or set it later with <code class="methodname">set_label()</code>.
</p>
<p>To define an accelerator key for keyboard navigation, place an underscore before one of the label's characters and specify <code class="literal">true</code> for the optional <code class="literal">mnemonic</code> parameter. For instance:
</p>
<pre class="programlisting"><code class="code">Gtk::Button* pButton = new Gtk::Button("_Something", true);</code></pre>
<p>
<code class="classname">Gtk::Button</code> is also
a container so you could put any other widget, such as a
<code class="classname">Gtk::Image</code> into it.
</p>
<p>
The <code class="classname">Gtk::Button</code> widget has the <code class="literal">clicked</code> signal
which is emitted when the button is pressed and released.
</p>
<p><a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1Button.html" target="_top">Reference</a></p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="pushbutton-example"></a>Example</h3></div></div></div>
<p>
This example creates a button with a picture and a label.
</p>
<div class="figure">
<a name="figure-buttons"></a><p class="title"><b>Figure 6.1. buttons example</b></p>
<div class="figure-contents">
<div class="screenshot">
<div class="mediaobject"><img src="figures/buttons.png" alt="buttons example"></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/button" target="_top">Source Code</a></p>
<p>File: <code class="filename">buttons.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLE_BUTTONS_H
#define GTKMM_EXAMPLE_BUTTONS_H
#include <gtkmm/window.h>
#include <gtkmm/button.h>
class Buttons : public Gtk::Window
{
public:
Buttons();
virtual ~Buttons();
protected:
//Signal handlers:
void on_button_clicked();
//Child widgets:
Gtk::Button m_button;
};
#endif //GTKMM_EXAMPLE_BUTTONS_H
</code></pre>
<p>File: <code class="filename">buttons.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "buttons.h"
#include <gtkmm/box.h>
#include <gtkmm/image.h>
#include <gtkmm/label.h>
#include <iostream>
Buttons::Buttons()
{
// This corresponds to Gtk::Bin::add_pixlabel("info.xpm", "cool button") in gtkmm3.
//Create Image and Label widgets:
auto pmap = Gtk::make_managed<Gtk::Image>("info.xpm");
auto label = Gtk::make_managed<Gtk::Label>("cool button");
label->set_expand(true);
//Put them in a Box:
auto hbox = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 5);
hbox->append(*pmap);
hbox->append(*label);
//And put that Box in the button:
m_button.set_child(*hbox);
set_title("Pixmap'd buttons!");
m_button.signal_clicked().connect( sigc::mem_fun(*this,
&Buttons::on_button_clicked) );
m_button.set_margin(10);
set_child(m_button);
}
Buttons::~Buttons()
{
}
void Buttons::on_button_clicked()
{
std::cout << "The Button was clicked." << std::endl;
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "buttons.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<Buttons>(argc, argv);
}
</code></pre>
</div>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="changes-gtkmm4.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"> </td>
<td width="40%" align="right"> <a accesskey="n" href="sec-toggle-buttons.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Chapter 5. Changes in <span class="application">gtkmm</span>-4.0 and <span class="application">glibmm-2.68</span> </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"> ToggleButton</td>
</tr>
</table>
</div>
</body>
</html>
|