Debian Codegraph @

examples/book/drawingarea/thin_lines/myarea.cc

raw
1 /* gtkmm example Copyright (C) 2012 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
19 MyArea::MyArea()
20 : m_fix (0)
21 {
22 set_content_width(200);
23 set_content_height(100);
24 set_draw_func(sigc::mem_fun(*this, &MyArea::on_draw));
25 }
26
27 MyArea::~MyArea()
28 {
29 }
30
31 void MyArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height)
32 {
33 cr->set_line_width(1.0);
34
35 // draw one line, every two pixels
36 // without the 'fix', you won't notice any space between the lines,
37 // since each one will occupy two pixels (width)
38 for (int i = 0; i < width; i += 2)
39 {
40 cr->move_to(i + m_fix, 0);
41 cr->line_to(i + m_fix, height);
42 }
43
44 cr->stroke();
45 }
46
47 // Toogle between both values (0 or 0.5)
48 void MyArea::fix_lines(bool fix)
49 {
50 // to get the width right, we have to draw in the middle of the pixel
51 m_fix = fix ? 0.5 : 0.0;
52
53 // force the redraw of the image
54 queue_draw();
55 }