Debian Codegraph @

examples/book/drawingarea/arcs/myarea.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 "myarea.h"
18 #include <cairomm/context.h>
19 #include <cmath>
20
21 MyArea::MyArea()
22 {
23 set_draw_func(sigc::mem_fun(*this, &MyArea::on_draw));
24 }
25
26 MyArea::~MyArea()
27 {
28 }
29
30 void MyArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height)
31 {
32 const int lesser = std::min(width, height);
33
34 // coordinates for the center of the window
35 const int xc = width / 2;
36 const int yc = height / 2;
37
38 cr->set_line_width(lesser * 0.02); // outline thickness changes
39 // with window size
40
41 // first draw a simple unclosed arc
42 cr->save();
43 cr->arc(width / 3.0, height / 4.0, lesser / 4.0, -(M_PI / 5.0), M_PI);
44 cr->close_path(); // line back to start point
45 cr->set_source_rgb(0.0, 0.8, 0.0);
46 cr->fill_preserve();
47 cr->restore(); // back to opaque black
48 cr->stroke(); // outline it
49
50 // now draw a circle
51 cr->save();
52 cr->arc(xc, yc, lesser / 4.0, 0.0, 2.0 * M_PI); // full circle
53 cr->set_source_rgba(0.0, 0.0, 0.8, 0.6); // partially translucent
54 cr->fill_preserve();
55 cr->restore(); // back to opaque black
56 cr->stroke();
57
58 // and finally an ellipse
59 double ex, ey, ew, eh;
60 // center of ellipse
61 ex = xc;
62 ey = 3.0 * height / 4.0;
63 // ellipse dimensions
64 ew = 3.0 * width / 4.0;
65 eh = height / 3.0;
66
67 cr->save();
68
69 cr->translate(ex, ey); // make (ex, ey) == (0, 0)
70 cr->scale(ew / 2.0, eh / 2.0); // for width: ew / 2.0 == 1.0
71 // for height: eh / 2.0 == 1.0
72
73 cr->arc(0.0, 0.0, 1.0, 0.0, 2 * M_PI); // 'circle' centered at (0, 0)
74 // with 'radius' of 1.0
75
76 cr->set_source_rgba(0.8, 0.0, 0.0, 0.7);
77 cr->fill_preserve();
78 cr->restore(); // back to opaque black
79 cr->stroke();
80 }