Debian Codegraph @

examples/book/drawingarea/clock/clock.cc

raw
1 /* gtkmm example Copyright (C) 2002 gtkmm development team
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include <ctime>
18 #include <cmath>
19 #include <cairomm/context.h>
20 #include <glibmm/main.h>
21 #include "clock.h"
22
23 Clock::Clock()
24 : m_radius(0.42), m_line_width(0.05)
25 {
26 Glib::signal_timeout().connect( sigc::mem_fun(*this, &Clock::on_timeout), 1000 );
27 set_draw_func(sigc::mem_fun(*this, &Clock::on_draw));
28 }
29
30 Clock::~Clock()
31 {
32 }
33
34 void Clock::on_draw(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height)
35 {
36 // scale to unit square and translate (0, 0) to be (0.5, 0.5), i.e.
37 // the center of the window
38 cr->scale(width, height);
39 cr->translate(0.5, 0.5);
40 cr->set_line_width(m_line_width);
41
42 cr->save();
43 cr->set_source_rgba(0.337, 0.612, 0.117, 0.9); // green
44 cr->paint();
45 cr->restore();
46 cr->arc(0, 0, m_radius, 0, 2 * M_PI);
47 cr->save();
48 cr->set_source_rgba(1.0, 1.0, 1.0, 0.8);
49 cr->fill_preserve();
50 cr->restore();
51 cr->stroke_preserve();
52 cr->clip();
53
54 //clock ticks
55 for (int i = 0; i < 12; i++)
56 {
57 double inset = 0.05;
58
59 cr->save();
60 cr->set_line_cap(Cairo::Context::LineCap::ROUND);
61
62 if(i % 3 != 0)
63 {
64 inset *= 0.8;
65 cr->set_line_width(0.03);
66 }
67
68 cr->move_to(
69 (m_radius - inset) * cos (i * M_PI / 6),
70 (m_radius - inset) * sin (i * M_PI / 6));
71 cr->line_to (
72 m_radius * cos (i * M_PI / 6),
73 m_radius * sin (i * M_PI / 6));
74 cr->stroke();
75 cr->restore(); /* stack-pen-size */
76 }
77
78 // store the current time
79 time_t rawtime;
80 time(&rawtime);
81 struct tm * timeinfo = localtime (&rawtime);
82
83 // compute the angles of the indicators of our clock
84 double minutes = timeinfo->tm_min * M_PI / 30;
85 double hours = timeinfo->tm_hour * M_PI / 6;
86 double seconds= timeinfo->tm_sec * M_PI / 30;
87
88 cr->save();
89 cr->set_line_cap(Cairo::Context::LineCap::ROUND);
90
91 // draw the seconds hand
92 cr->save();
93 cr->set_line_width(m_line_width / 3);
94 cr->set_source_rgba(0.7, 0.7, 0.7, 0.8); // gray
95 cr->move_to(0, 0);
96 cr->line_to(sin(seconds) * (m_radius * 0.9),
97 -cos(seconds) * (m_radius * 0.9));
98 cr->stroke();
99 cr->restore();
100
101 // draw the minutes hand
102 cr->set_source_rgba(0.117, 0.337, 0.612, 0.9); // blue
103 cr->move_to(0, 0);
104 cr->line_to(sin(minutes + seconds / 60) * (m_radius * 0.8),
105 -cos(minutes + seconds / 60) * (m_radius * 0.8));
106 cr->stroke();
107
108 // draw the hours hand
109 cr->set_source_rgba(0.337, 0.612, 0.117, 0.9); // green
110 cr->move_to(0, 0);
111 cr->line_to(sin(hours + minutes / 12.0) * (m_radius * 0.5),
112 -cos(hours + minutes / 12.0) * (m_radius * 0.5));
113 cr->stroke();
114 cr->restore();
115
116 // draw a little dot in the middle
117 cr->arc(0, 0, m_line_width / 3.0, 0, 2 * M_PI);
118 cr->fill();
119 }
120
121 bool Clock::on_timeout()
122 {
123 // force our program to redraw the entire clock.
124 queue_draw();
125 return true;
126 }