Debian Codegraph @

examples/book/drawingarea/image/myarea.cc

raw
1 /* gtkmm example Copyright (C) 2011 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 <giomm/resource.h>
20 #include <gdkmm/general.h> // set_source_pixbuf()
21 #include <glibmm/fileutils.h>
22 #include <iostream>
23
24 MyArea::MyArea()
25 {
26 try
27 {
28 // The fractal image has been created by the XaoS program.
29 // http://xaos.sourceforge.net
30 m_image = Gdk::Pixbuf::create_from_resource("/image/fractal_image.png");
31 }
32 catch(const Gio::ResourceError& ex)
33 {
34 std::cerr << "ResourceError: " << ex.what() << std::endl;
35 }
36 catch(const Gdk::PixbufError& ex)
37 {
38 std::cerr << "PixbufError: " << ex.what() << std::endl;
39 }
40
41 // Show at least a quarter of the image.
42 if (m_image)
43 {
44 set_content_width(m_image->get_width()/2);
45 set_content_height(m_image->get_height()/2);
46 }
47
48 // Set the draw function.
49 set_draw_func(sigc::mem_fun(*this, &MyArea::on_draw));
50 }
51
52 MyArea::~MyArea()
53 {
54 }
55
56 void MyArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height)
57 {
58 if (!m_image)
59 return;
60
61 // Draw the image in the middle of the drawing area, or (if the image is
62 // larger than the drawing area) draw the middle part of the image.
63 Gdk::Cairo::set_source_pixbuf(cr, m_image,
64 (width - m_image->get_width())/2, (height - m_image->get_height())/2);
65 cr->paint();
66 }