File: sec-drawing-clock-example.html

package info (click to toggle)
gtkmm-documentation 4.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,772 kB
  • sloc: cpp: 15,541; javascript: 1,208; makefile: 1,080; python: 401; xml: 106; perl: 67; sh: 8
file content (269 lines) | stat: -rw-r--r-- 9,726 bytes parent folder | download
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
<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>Example Application: Creating a Clock with Cairo</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-drawingarea.html" title="Chapter 18. The DrawingArea Widget">
<link rel="prev" href="sec-draw-images.html" title="Drawing Images">
<link rel="next" href="chapter-draganddrop.html" title="Chapter 19. Drag and Drop">
</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">Example Application: Creating a Clock with Cairo</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-draw-images.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 18. The DrawingArea Widget</th>
<td width="20%" align="right"> <a accesskey="n" href="chapter-draganddrop.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-drawing-clock-example"></a>Example Application: Creating a Clock with Cairo</h2></div></div></div>
  

      <p>
          Now that we've covered the basics of drawing with Cairo, let's try to
          put it all together and create a simple application that actually
          does something. The following example uses Cairo to create a custom
          <code class="classname">Clock</code> widget. The clock has a second hand, a
          minute hand, and an hour hand, and updates itself every second.
      </p>
      <div class="screenshot">
          <div class="mediaobject"><img src="figures/cairo_clock.png"></div>
      </div>
<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/drawingarea/clock" target="_top">Source Code</a></p>

<p>File: <code class="filename">clock.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLE_CLOCK_H
#define GTKMM_EXAMPLE_CLOCK_H

#include &lt;gtkmm/drawingarea.h&gt;

class Clock : public Gtk::DrawingArea
{
public:
  Clock();
  virtual ~Clock();

protected:
  void on_draw(const Cairo::RefPtr&lt;Cairo::Context&gt;&amp; cr, int width, int height);

  bool on_timeout();

  double m_radius;
  double m_line_width;
};

#endif // GTKMM_EXAMPLE_CLOCK_H
</code></pre>
<p>File: <code class="filename">clock.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include &lt;ctime&gt;
#include &lt;cmath&gt;
#include &lt;cairomm/context.h&gt;
#include &lt;glibmm/main.h&gt;
#include "clock.h"

Clock::Clock()
: m_radius(0.42), m_line_width(0.05)
{
  Glib::signal_timeout().connect( sigc::mem_fun(*this, &amp;Clock::on_timeout), 1000 );
  set_draw_func(sigc::mem_fun(*this, &amp;Clock::on_draw));
}

Clock::~Clock()
{
}

void Clock::on_draw(const Cairo::RefPtr&lt;Cairo::Context&gt;&amp; cr, int width, int height)
{
  // scale to unit square and translate (0, 0) to be (0.5, 0.5), i.e.
  // the center of the window
  cr-&gt;scale(width, height);
  cr-&gt;translate(0.5, 0.5);
  cr-&gt;set_line_width(m_line_width);

  cr-&gt;save();
  cr-&gt;set_source_rgba(0.337, 0.612, 0.117, 0.9);   // green
  cr-&gt;paint();
  cr-&gt;restore();
  cr-&gt;arc(0, 0, m_radius, 0, 2 * M_PI);
  cr-&gt;save();
  cr-&gt;set_source_rgba(1.0, 1.0, 1.0, 0.8);
  cr-&gt;fill_preserve();
  cr-&gt;restore();
  cr-&gt;stroke_preserve();
  cr-&gt;clip();

  //clock ticks
  for (int i = 0; i &lt; 12; i++)
  {
    double inset = 0.05;

    cr-&gt;save();
    cr-&gt;set_line_cap(Cairo::Context::LineCap::ROUND);

    if(i % 3 != 0)
    {
      inset *= 0.8;
      cr-&gt;set_line_width(0.03);
    }

    cr-&gt;move_to(
      (m_radius - inset) * cos (i * M_PI / 6),
      (m_radius - inset) * sin (i * M_PI / 6));
    cr-&gt;line_to (
      m_radius * cos (i * M_PI / 6),
      m_radius * sin (i * M_PI / 6));
    cr-&gt;stroke();
    cr-&gt;restore(); /* stack-pen-size */
  }

  // store the current time
  time_t rawtime;
  time(&amp;rawtime);
  struct tm * timeinfo = localtime (&amp;rawtime);

  // compute the angles of the indicators of our clock
  double minutes = timeinfo-&gt;tm_min * M_PI / 30;
  double hours = timeinfo-&gt;tm_hour * M_PI / 6;
  double seconds= timeinfo-&gt;tm_sec * M_PI / 30;

  cr-&gt;save();
  cr-&gt;set_line_cap(Cairo::Context::LineCap::ROUND);

  // draw the seconds hand
  cr-&gt;save();
  cr-&gt;set_line_width(m_line_width / 3);
  cr-&gt;set_source_rgba(0.7, 0.7, 0.7, 0.8); // gray
  cr-&gt;move_to(0, 0);
  cr-&gt;line_to(sin(seconds) * (m_radius * 0.9),
    -cos(seconds) * (m_radius * 0.9));
  cr-&gt;stroke();
  cr-&gt;restore();

  // draw the minutes hand
  cr-&gt;set_source_rgba(0.117, 0.337, 0.612, 0.9);   // blue
  cr-&gt;move_to(0, 0);
  cr-&gt;line_to(sin(minutes + seconds / 60) * (m_radius * 0.8),
    -cos(minutes + seconds / 60) * (m_radius * 0.8));
  cr-&gt;stroke();

  // draw the hours hand
  cr-&gt;set_source_rgba(0.337, 0.612, 0.117, 0.9);   // green
  cr-&gt;move_to(0, 0);
  cr-&gt;line_to(sin(hours + minutes / 12.0) * (m_radius * 0.5),
    -cos(hours + minutes / 12.0) * (m_radius * 0.5));
  cr-&gt;stroke();
  cr-&gt;restore();

  // draw a little dot in the middle
  cr-&gt;arc(0, 0, m_line_width / 3.0, 0, 2 * M_PI);
  cr-&gt;fill();
}

bool Clock::on_timeout()
{
  // force our program to redraw the entire clock.
  queue_draw();
  return true;
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "clock.h"
#include &lt;gtkmm/application.h&gt;
#include &lt;gtkmm/window.h&gt;

class ExampleWindow : public Gtk::Window
{
public:
  ExampleWindow();

protected:
  Clock m_clock;
};

ExampleWindow::ExampleWindow()
{
  set_title("Cairomm Clock");
  set_child(m_clock);
}

int main(int argc, char** argv)
{
  auto app = Gtk::Application::create("org.gtkmm.example");

  return app-&gt;make_window_and_run&lt;ExampleWindow&gt;(argc, argv);
}
</code></pre>

      <p>
          As before, almost all of the interesting stuff is done in the draw
          function <code class="methodname">on_draw()</code>. Before we dig
          into the draw function, notice that the constructor for the
          <code class="classname">Clock</code> widget connects a handler function
          <code class="methodname">on_timeout()</code> to a timer with a timeout
          period of 1000 milliseconds (1 second). This means that
          <code class="methodname">on_timeout()</code> will get called once per
          second. The sole responsibility of this function is to invalidate
          the window so that <span class="application">gtkmm</span> will be forced to redraw it.
      </p>
      <p>
          Now let's take a look at the code that performs the actual drawing.
          The first section of <code class="methodname">on_draw()</code> should be
          pretty familiar by now. This example again scales the coordinate system
          to be a unit square so that it's easier to draw the clock as a
          percentage of window size so that it will automatically scale when
          the window size is adjusted. Furthermore, the coordinate system is
          scaled over and down so that the (0, 0) coordinate is in the very
          center of the window.
      </p>
      <p>
          The function <code class="methodname">Cairo::Context::paint()</code> is used here
          to set the background color of the window. This function takes no
          arguments and fills the current surface (or the clipped portion of
          the surface) with the source color currently active. After setting
          the background color of the window, we draw a circle for the clock
          outline, fill it with white, and then stroke the outline in black.
          Notice that both of these actions use the
          <code class="methodname">_preserve</code> variant to preserve the current path,
          and then this same path is clipped to make sure that our next lines
          don't go outside the outline of the clock.
      </p>
      <p>
          After drawing the outline, we go around the clock and draw ticks for
          every hour, with a larger tick at 12, 3, 6, and 9. Now we're finally
          ready to implement the time-keeping functionality of the clock, which
          simply involves getting the current values for hours, minutes and
          seconds, and drawing the hands at the correct angles.
      </p>
  </div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-draw-images.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-drawingarea.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="chapter-draganddrop.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Drawing Images </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 19. Drag and Drop</td>
</tr>
</table>
</div>
</body>
</html>