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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
<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>SpinButton</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-misc-widgets.html" title="Chapter 8. Miscellaneous Widgets">
<link rel="prev" href="sec-text-entry.html" title="Entry">
<link rel="next" href="sec-progressbar.html" title="ProgressBar">
</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">SpinButton</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-text-entry.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 8. Miscellaneous Widgets</th>
<td width="20%" align="right"> <a accesskey="n" href="sec-progressbar.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-spinbutton"></a>SpinButton</h2></div></div></div>
<p>
A <code class="classname">SpinButton</code> allows the user to select a value from a
range of numeric values. It has an <code class="classname">Entry</code> widget with increment and decrement buttons
at the side. Clicking the buttons causes the value to 'spin' up and down across
the range of possible values. The <code class="classname">Entry</code> widget may also
be used to enter a value directly.
</p>
<p>
The value can have an adjustable number of decimal places, and the step size is
configurable. <code class="classname">SpinButton</code>s have an 'auto-repeat' feature
as well: holding down the increment or decrement button can optionally cause the value to
change more quickly the longer the button is held down.
</p>
<p>
<code class="classname">SpinButton</code>s use an <a class="link" href="chapter-adjustment.html" title="Chapter 16. Adjustments">Adjustment</a> object to hold information about
the range of values. These Adjustment attributes are used by the Spin Button
like so:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
<p>
<code class="literal">value</code>: value for the Spin Button
</p>
</li>
<li class="listitem">
<p>
<code class="literal">lower</code>: lower range value
</p>
</li>
<li class="listitem">
<p>
<code class="literal">upper</code>: upper range value
</p>
</li>
<li class="listitem">
<p>
<code class="literal">step_increment</code>: value to increment/decrement when pressing
mouse button 1
</p>
</li>
<li class="listitem">
<p>
<code class="literal">page_increment</code>: value to increment/decrement when pressing
mouse button 2
</p>
</li>
<li class="listitem">
<p>
<code class="literal">page_size</code>: unused
</p>
</li>
</ul></div>
<p>
</p>
<p>
Additionally, mouse button 3 can be used to jump directly to the
<code class="literal">upper</code> or <code class="literal">lower</code> values.
</p>
<p>
The <code class="classname">SpinButton</code> can create a default
<code class="classname">Adjustment</code>, which you can access via the
<code class="methodname">get_adjustment()</code> method, or you can specify an existing
<code class="classname">Adjustment</code> in the constructor.
</p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="spinbutton-methods"></a>Methods</h3></div></div></div>
<p>
The number of decimal places can be altered using the
<code class="methodname">set_digits()</code> method.
</p>
<p>
You can set the spinbutton's value using the <code class="methodname">set_value()</code>
method, and retrieve it with <code class="methodname">get_value()</code>.
</p>
<p>
The <code class="methodname">spin()</code> method 'spins' the
<code class="classname">SpinButton</code>, as if its increment or decrement button had been clicked.
You need to specify a <code class="classname">Gtk::SpinType</code> to specify the
direction or new position.
</p>
<p>
To prevent the user from typing non-numeric characters into the entry box, pass
<code class="literal">true</code> to the <code class="methodname">set_numeric()</code> method.
</p>
<p>
To make the <code class="classname">SpinButton</code> 'wrap' between its upper and
lower bounds, use the <code class="methodname">set_wrap()</code> method.
</p>
<p>
To force it to snap to the nearest <code class="literal">step_increment</code>,
use <code class="methodname">set_snap_to_ticks()</code>.
</p>
<p><a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1SpinButton.html" target="_top">Reference</a></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="spinbutton-example"></a>Example</h3></div></div></div>
<p>
Here's an example of a <code class="classname">SpinButton</code> in action:
</p>
<div class="figure">
<a name="figure-spinbutton"></a><p class="title"><b>Figure 8.6. SpinButton</b></p>
<div class="figure-contents">
<div class="screenshot">
<div class="mediaobject"><img src="figures/spinbutton.png" alt="SpinButton"></div>
</div>
</div>
</div>
<br class="figure-break">
<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/spinbutton" target="_top">Source Code</a></p>
<p>File: <code class="filename">examplewindow.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
virtual ~ExampleWindow();
protected:
//Signal handlers:
void on_checkbutton_snap();
void on_checkbutton_numeric();
void on_spinbutton_digits_changed();
void on_button_close();
enum enumValueFormats
{
VALUE_FORMAT_INT,
VALUE_FORMAT_FLOAT
};
void on_button_getvalue(enumValueFormats display);
//Child widgets:
Gtk::Frame m_Frame_NotAccelerated, m_Frame_Accelerated;
Gtk::Box m_HBox_NotAccelerated, m_HBox_Accelerated,
m_HBox_Buttons;
Gtk::Box m_VBox_Main, m_VBox, m_VBox_Day, m_VBox_Month, m_VBox_Year,
m_VBox_Accelerated, m_VBox_Value, m_VBox_Digits;
Gtk::Label m_Label_Day, m_Label_Month, m_Label_Year,
m_Label_Value, m_Label_Digits,
m_Label_ShowValue;
Glib::RefPtr<Gtk::Adjustment> m_adjustment_day, m_adjustment_month, m_adjustment_year,
m_adjustment_value, m_adjustment_digits;
Gtk::SpinButton m_SpinButton_Day, m_SpinButton_Month, m_SpinButton_Year,
m_SpinButton_Value, m_SpinButton_Digits;
Gtk::CheckButton m_CheckButton_Snap, m_CheckButton_Numeric;
Gtk::Button m_Button_Int, m_Button_Float, m_Button_Close;
};
#endif //GTKMM_EXAMPLEWINDOW_H
</code></pre>
<p>File: <code class="filename">examplewindow.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
#include <iostream>
#include <cstdio>
ExampleWindow::ExampleWindow()
:
m_Frame_NotAccelerated("Not accelerated"),
m_Frame_Accelerated("Accelerated"),
m_VBox_Main(Gtk::Orientation::VERTICAL, 5),
m_VBox(Gtk::Orientation::VERTICAL),
m_VBox_Day(Gtk::Orientation::VERTICAL),
m_VBox_Month(Gtk::Orientation::VERTICAL),
m_VBox_Year(Gtk::Orientation::VERTICAL),
m_VBox_Accelerated(Gtk::Orientation::VERTICAL),
m_VBox_Value(Gtk::Orientation::VERTICAL),
m_VBox_Digits(Gtk::Orientation::VERTICAL),
m_Label_Day("Day: ", Gtk::Align::START),
m_Label_Month("Month: ", Gtk::Align::START),
m_Label_Year("Year: ", Gtk::Align::START),
m_Label_Value("Value: ", Gtk::Align::START),
m_Label_Digits("Digits: ", Gtk::Align::START),
m_adjustment_day( Gtk::Adjustment::create(1.0, 1.0, 31.0, 1.0, 5.0, 0.0) ),
m_adjustment_month( Gtk::Adjustment::create(1.0, 1.0, 12.0, 1.0, 5.0, 0.0) ),
m_adjustment_year( Gtk::Adjustment::create(2012.0, 1.0, 2200.0, 1.0, 100.0, 0.0) ),
m_adjustment_value( Gtk::Adjustment::create(0.0, -10000.0, 10000.0, 0.5, 100.0, 0.0) ),
m_adjustment_digits( Gtk::Adjustment::create(2.0, 1.0, 5.0, 1.0, 1.0, 0.0) ),
m_SpinButton_Day(m_adjustment_day),
m_SpinButton_Month(m_adjustment_month),
m_SpinButton_Year(m_adjustment_year),
m_SpinButton_Value(m_adjustment_value, 1.0, 2),
m_SpinButton_Digits(m_adjustment_digits),
m_CheckButton_Snap("Snap to 0.5-ticks"),
m_CheckButton_Numeric("Numeric only input mode"),
m_Button_Int("Value as Int"),
m_Button_Float("Value as Float"),
m_Button_Close("Close")
{
set_title("SpinButton");
m_VBox_Main.set_margin(10);
set_child(m_VBox_Main);
m_VBox_Main.append(m_Frame_NotAccelerated);
m_VBox.set_margin(5);
m_Frame_NotAccelerated.set_child(m_VBox);
/* Day, month, year spinners */
m_VBox.set_spacing(5);
m_VBox.append(m_HBox_NotAccelerated);
m_Label_Day.set_expand();
m_VBox_Day.append(m_Label_Day);
m_SpinButton_Day.set_wrap();
m_SpinButton_Day.set_expand();
m_VBox_Day.append(m_SpinButton_Day);
m_HBox_NotAccelerated.set_spacing(5);
m_HBox_NotAccelerated.append(m_VBox_Day);
m_Label_Month.set_expand();
m_VBox_Month.append(m_Label_Month);
m_SpinButton_Month.set_wrap();
m_SpinButton_Month.set_expand();
m_VBox_Month.append(m_SpinButton_Month);
m_HBox_NotAccelerated.append(m_VBox_Month);
m_Label_Year.set_expand();
m_VBox_Year.append(m_Label_Year);
m_SpinButton_Year.set_wrap();
m_SpinButton_Year.set_expand();
m_SpinButton_Year.set_size_request(55, -1);
m_VBox_Year.append(m_SpinButton_Year);
m_HBox_NotAccelerated.append(m_VBox_Year);
//Accelerated:
m_VBox_Main.append(m_Frame_Accelerated);
m_VBox_Accelerated.set_margin(5);
m_Frame_Accelerated.set_child(m_VBox_Accelerated);
m_VBox_Accelerated.set_spacing(5);
m_VBox_Accelerated.append(m_HBox_Accelerated);
m_HBox_Accelerated.append(m_VBox_Value);
m_Label_Value.set_expand();
m_VBox_Value.append(m_Label_Value);
m_SpinButton_Value.set_wrap();
m_SpinButton_Value.set_expand();
m_SpinButton_Value.set_size_request(100, -1);
m_VBox_Value.append(m_SpinButton_Value);
m_HBox_Accelerated.append(m_VBox_Digits);
m_VBox_Digits.append(m_Label_Digits);
m_Label_Digits.set_expand();
m_SpinButton_Digits.set_wrap();
m_adjustment_digits->signal_value_changed().connect( sigc::mem_fun(*this,
&ExampleWindow::on_spinbutton_digits_changed) );
m_VBox_Digits.append(m_SpinButton_Digits);
m_SpinButton_Digits.set_expand();
//CheckButtons:
m_VBox_Accelerated.append(m_CheckButton_Snap);
m_CheckButton_Snap.set_expand();
m_CheckButton_Snap.set_active();
m_CheckButton_Snap.signal_toggled().connect( sigc::mem_fun(*this,
&ExampleWindow::on_checkbutton_snap) );
m_VBox_Accelerated.append(m_CheckButton_Numeric);
m_CheckButton_Numeric.set_expand();
m_CheckButton_Numeric.set_active();
m_CheckButton_Numeric.signal_toggled().connect( sigc::mem_fun(*this,
&ExampleWindow::on_checkbutton_numeric) );
//Buttons:
m_VBox_Accelerated.append(m_HBox_Buttons);
m_Button_Int.signal_clicked().connect( sigc::bind( sigc::mem_fun(*this,
&ExampleWindow::on_button_getvalue), VALUE_FORMAT_INT) );
m_HBox_Buttons.set_spacing(5);
m_HBox_Buttons.append(m_Button_Int);
m_Button_Int.set_expand();
m_Button_Float.signal_clicked().connect( sigc::bind( sigc::mem_fun(*this,
&ExampleWindow::on_button_getvalue), VALUE_FORMAT_FLOAT) );
m_HBox_Buttons.append(m_Button_Float);
m_Button_Float.set_expand();
m_VBox_Accelerated.append(m_Label_ShowValue);
m_Label_ShowValue.set_expand();
m_Label_ShowValue.set_text("0");
//Close button:
m_Button_Close.signal_clicked().connect( sigc::mem_fun(*this,
&ExampleWindow::on_button_close) );
m_VBox_Main.append(m_Button_Close);
}
ExampleWindow::~ExampleWindow()
{
}
void ExampleWindow::on_button_close()
{
set_visible(false);
}
void ExampleWindow::on_checkbutton_snap()
{
m_SpinButton_Value.set_snap_to_ticks( m_CheckButton_Snap.get_active() );
}
void ExampleWindow::on_checkbutton_numeric()
{
m_SpinButton_Value.set_numeric( m_CheckButton_Numeric.get_active() );
}
void ExampleWindow::on_spinbutton_digits_changed()
{
m_SpinButton_Value.set_digits( m_SpinButton_Digits.get_value_as_int() );
}
void ExampleWindow::on_button_getvalue(enumValueFormats display)
{
gchar buf[32];
if (display == VALUE_FORMAT_INT)
sprintf (buf, "%d", m_SpinButton_Value.get_value_as_int());
else
sprintf (buf, "%0.*f", m_SpinButton_Value.get_digits(),
m_SpinButton_Value.get_value());
m_Label_ShowValue.set_text(buf);
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.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<ExampleWindow>(argc, argv);
}
</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-text-entry.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-misc-widgets.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="sec-progressbar.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Entry </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"> ProgressBar</td>
</tr>
</table>
</div>
</body>
</html>
|