1 // -*- C++ -*-
2 
3 /*
4  * Gnome Chemistry Utils
5  * gccv/path.cc
6  *
7  * Copyright (C) 2009-2010 Jean Bréfort <jean.brefort@normalesup.org>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 3 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
22  * USA
23  */
24 
25 #include "config.h"
26 #include "path.h"
27 
28 namespace gccv {
29 
Path(Canvas * canvas,GOPath * path)30 Path::Path (Canvas *canvas, GOPath *path):
31 	FillItem (canvas),
32 	m_Path (path)
33 {
34 }
35 
Path(Group * parent,GOPath * path,ItemClient * client)36 Path::Path (Group *parent, GOPath *path, ItemClient *client):
37 	FillItem (parent, client),
38 	m_Path (path)
39 {
40 }
41 
~Path()42 Path::~Path ()
43 {
44 	go_path_free (m_Path);
45 }
46 
Distance(double x,double y,Item ** item) const47 double Path::Distance (double x, double y, Item **item) const
48 {
49 	// use cairo_in_fill to detect if the point is inside
50 	// size for the surface is probably unimportant
51 	if (item)
52 		*item = const_cast <Path *> (this);
53 	cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
54 	cairo_t *cr = cairo_create (surface);
55 	cairo_surface_destroy (surface);
56 	cairo_set_line_width (cr, GetLineWidth ());
57 	go_path_to_cairo (m_Path, GO_PATH_DIRECTION_FORWARD, cr);
58 	// FIXME, take colors into account
59 	if (cairo_in_fill (cr, x, y)) {
60 		cairo_destroy (cr);
61 		return 0.;
62 	}
63 	if (cairo_in_stroke (cr, x, y)) {
64 		cairo_destroy (cr);
65 		return 0.;
66 	}
67 	cairo_destroy (cr);
68 	return G_MAXDOUBLE; // FIXME
69 }
70 
Draw(cairo_t * cr,G_GNUC_UNUSED bool is_vector) const71 void Path::Draw (cairo_t *cr, G_GNUC_UNUSED bool is_vector) const
72 {
73 	GOColor fill_color = GetFillColor (), line_color = GetEffectiveLineColor ();
74 	go_path_to_cairo (m_Path, GO_PATH_DIRECTION_FORWARD, cr);
75 	if (fill_color != 0) {
76 		cairo_set_source_rgba (cr, GO_COLOR_TO_CAIRO (fill_color));
77 		if (line_color != 0)
78 			cairo_fill_preserve (cr);
79 		else
80 			cairo_fill (cr);
81 	}
82 	if (ApplyLine (cr))
83 		cairo_stroke (cr);
84 	cairo_restore (cr);
85 }
86 
UpdateBounds()87 void Path::UpdateBounds ()
88 {
89 	cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
90 	cairo_t *cr = cairo_create (surface);
91 	cairo_surface_destroy (surface);
92 	cairo_set_line_width (cr, GetLineWidth ());
93 	go_path_to_cairo (m_Path, GO_PATH_DIRECTION_FORWARD, cr);
94 	cairo_stroke_extents (cr, &m_x0, &m_y0, &m_x1, &m_y1);
95 	cairo_destroy (cr);
96 	Item::UpdateBounds ();
97 }
98 
99 }	//	namespace gccv
100