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
|
<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>Idle Functions</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-chapter-timeouts.html" title="Chapter 24. Timeouts, I/O and Idle Functions">
<link rel="prev" href="sec-monitoring-io.html" title="Monitoring I/O">
<link rel="next" href="chapter-memory.html" title="Chapter 25. Memory management">
</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">Idle Functions</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-monitoring-io.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 24. Timeouts, I/O and Idle Functions </th>
<td width="20%" align="right"> <a accesskey="n" href="chapter-memory.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-idle-functions"></a>Idle Functions</h2></div></div></div>
<p>
If you want to specify a method that gets called when nothing else is happening, use the following:
</p>
<pre class="programlisting"><code class="code">sigc::connection Glib::SignalIdle::connect(const sigc::slot<bool()>& slot,
int priority = Glib::PRIORITY_DEFAULT_IDLE);
</code></pre>
<p>
This causes <span class="application">gtkmm</span> to call the specified method whenever nothing else is
happening. You can add a priority (lower numbers are higher priorities). There are two ways to remove the signal handler: calling
<code class="methodname">disconnect()</code> on the
<code class="classname">sigc::connection</code> object, or returning
<code class="literal">false</code> in the signal handler, which should be declared
as follows:
</p>
<pre class="programlisting"><code class="code">bool idleFunc();
</code></pre>
<p>
Since this is very similar to the methods above this explanation should
be sufficient to understand what's going on. However, here's a little example:
</p>
<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/idle/" target="_top">Source Code</a></p>
<p>File: <code class="filename">idleexample.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLE_IDLEEXAMPLE_H
#define GTKMM_EXAMPLE_IDLEEXAMPLE_H
#include <gtkmm.h>
#include <iostream>
class IdleExample : public Gtk::Window
{
public:
IdleExample();
protected:
// Signal Handlers:
bool on_timer();
bool on_idle();
void on_button_clicked();
// Member data:
Gtk::Box m_Box;
Gtk::Button m_ButtonQuit;
Gtk::ProgressBar m_ProgressBar_c;
Gtk::ProgressBar m_ProgressBar_d;
};
#endif // GTKMM_EXAMPLE_IDLEEXAMPLE_H
</code></pre>
<p>File: <code class="filename">idleexample.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "idleexample.h"
IdleExample::IdleExample() :
m_Box(Gtk::Orientation::VERTICAL, 5),
m_ButtonQuit("_Quit", true)
{
m_Box.set_margin(5);
// Put buttons into container
// Adding a few widgets:
set_child(m_Box);
m_Box.append(*Gtk::make_managed<Gtk::Label>("Formatting Windows drive C:"));
m_Box.append(*Gtk::make_managed<Gtk::Label>("100 MB"));
m_Box.append(m_ProgressBar_c);
m_ProgressBar_c.set_expand();
m_Box.append(*Gtk::make_managed<Gtk::Label>(""));
m_Box.append(*Gtk::make_managed<Gtk::Label>("Formatting Windows drive D:"));
m_Box.append(*Gtk::make_managed<Gtk::Label>("5000 MB"));
m_Box.append(m_ProgressBar_d);
m_ProgressBar_d.set_expand();
auto hbox = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL,10);
m_Box.append(*hbox);
hbox->append(m_ButtonQuit);
m_ButtonQuit.set_expand();
m_ButtonQuit.set_halign(Gtk::Align::END);
m_ButtonQuit.set_valign(Gtk::Align::END);
// Connect the signal handlers:
m_ButtonQuit.signal_clicked().connect( sigc::mem_fun(*this,
&IdleExample::on_button_clicked) );
// formatting drive c in timeout signal handler - called once every 50ms
Glib::signal_timeout().connect( sigc::mem_fun(*this, &IdleExample::on_timer),
50 );
// formatting drive d in idle signal handler - called as quickly as possible
Glib::signal_idle().connect( sigc::mem_fun(*this, &IdleExample::on_idle) );
}
void IdleExample::on_button_clicked()
{
set_visible(false);
}
// this timer callback function is executed once every 50ms (set in connection
// above). Use timeouts when speed is not critical. (ie periodically updating
// something).
bool IdleExample::on_timer()
{
double value = m_ProgressBar_c.get_fraction();
// Update progressbar 1/500th each time:
m_ProgressBar_c.set_fraction(value + 0.002);
return value < 0.99; // return false when done
}
// This idle callback function is executed as often as possible, hence it is
// ideal for processing intensive tasks.
bool IdleExample::on_idle()
{
double value = m_ProgressBar_d.get_fraction();
// Update progressbar 1/5000th each time:
m_ProgressBar_d.set_fraction(value + 0.0002);
return value < 0.99; // return false when done
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "idleexample.h"
#include <gtkmm/application.h>
int main (int argc, char *argv[])
{
auto app = Gtk::Application::create("org.gtkmm.example");
return app->make_window_and_run<IdleExample>(argc, argv);
}
</code></pre>
<p>
This example points out the difference of idle and timeout methods a
little. If you need methods that are called periodically, and speed
is not very important, then you want timeout methods. If
you want methods that are called as often as possible (like
calculating a fractal in background), then use idle methods.
</p>
<p>
Try executing the example and increasing the system load. The upper
progress bar will increase steadily; the lower one will slow down.
</p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-monitoring-io.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-chapter-timeouts.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="chapter-memory.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Monitoring I/O </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 25. Memory management</td>
</tr>
</table>
</div>
</body>
</html>
|