Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/libccc/0.0.4/ccc/cc-circle.c
blob: 27275c1d64ee3f531d0ff2e95220b4e1555d9fd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* This file is part of libccc, criawips' cairo-based canvas
 *
 * AUTHORS
 *     Sven Herzberg  <herzi@gnome-de.org>
 *
 * Copyright (C) 2006  Sven Herzberg <herzi@gnome-de.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include "cc-circle.h"

#include <math.h>

CcItem*
cc_circle_new(void) {
	return g_object_new(CC_TYPE_CIRCLE, NULL);
}

void
cc_circle_set_anchor(CcCircle* self, gdouble x, gdouble y) {
	g_return_if_fail(CC_IS_CIRCLE(self));

	if(self->x == x && self->y == y) {
		return;
	}

	self->x = x;
	self->y = y;

	cc_item_update_bounds(CC_ITEM(self), NULL);

	g_object_notify(G_OBJECT(self), "anchor");
}

void
cc_circle_set_radius(CcCircle* self, gdouble radius) {
	CcDRect dirty;
	CcItem* item;

	g_return_if_fail(CC_IS_CIRCLE(self));

	if(self->radius == radius) {
		return;
	}

	item = CC_ITEM(self);
	dirty.x1 = self->x - MAX(radius, self->radius);
	dirty.y1 = self->y - MAX(radius, self->radius);
	dirty.x2 = self->x + MAX(radius, self->radius);
	dirty.y2 = self->y + MAX(radius, self->radius);
	self->radius = radius;

	g_object_notify(G_OBJECT(self), "radius");
	cc_item_dirty(item, NULL, dirty);
	cc_item_update_bounds(item, NULL);
}

/* GType */
G_DEFINE_TYPE(CcCircle, cc_circle, CC_TYPE_SHAPE);

enum {
	PROP_O,
	PROP_ANCHOR,
	PROP_RADIUS
};

static void
cc_circle_init(CcCircle* self) {}

static void
cc_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec) {
	CcCircle* self = CC_CIRCLE(object);

	switch(prop_id) {
	case PROP_RADIUS:
		g_value_set_double(value, self->radius);
		break;
	case PROP_ANCHOR:
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
		break;
	}
}

static void
cc_set_property(GObject* object, guint prop_id, GValue const* value, GParamSpec* pspec) {
	CcCircle* self = CC_CIRCLE(object);

	switch(prop_id) {
	case PROP_RADIUS:
		cc_circle_set_radius(self, g_value_get_double(value));
		break;
	case PROP_ANCHOR:
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
		break;
	}
}

static gdouble
cc_distance(CcItem* item, gdouble x, gdouble y, CcItem** found) {
	gdouble   distance = 0.0;
	CcCircle* self = NULL;

	// search the children first
	distance = CC_ITEM_CLASS(cc_circle_parent_class)->distance(item, x, y, found);

	if(*found) {
		return distance;
	}

	self = CC_CIRCLE(item);
	distance = sqrt(pow(x - self->x, 2.0) + pow(y - self->y, 2));

	if(distance <= self->radius + 0.5 * CC_SHAPE(self)->width) {
		distance = 0.0;
		*found = item;
	} else {
		distance -= self->radius;
	}

	return distance;
}

static void
cc_update_bounds(CcItem* item, CcView const* view, gpointer data) {
	CcCircle* self = CC_CIRCLE(item);
	gdouble w_2 = 0.5 * CC_SHAPE(self)->width;
	CcDRect new_bounds = {
		self->x - self->radius - w_2,
		self->y - self->radius - w_2,
		self->x + self->radius + w_2,
		self->y + self->radius + w_2
	};
	CcDRect* bounds = cc_hash_map_lookup(item->bounds, view);

	if(!bounds || !cc_d_rect_equal(*bounds, new_bounds)) {
		if(bounds) {
			cc_item_dirty(item, view, *bounds);
			cc_hash_map_remove(item->bounds, view);
		}

		cc_hash_map_insert(item->bounds, (gpointer)view, cc_d_rect_copy(&new_bounds));
		cc_item_dirty(item, view, new_bounds);
		cc_item_bounds_changed(item, view);
	}
}

static void
cc_path(CcShape* shape, CcView* view, cairo_t* cr) {
	CcCircle* self = CC_CIRCLE(shape);
	gdouble x = self->x,
		y = self->y,
		r = self->radius,
		tmp = 0.0;
	cc_view_world_to_window(view, &x, &y);
	cc_view_world_to_window_distance(view, &r, &tmp);
	cairo_arc(cr, x, y, r, 0.0, 2*G_PI);
}

static void
cc_circle_class_init(CcCircleClass* self_class) {
	GObjectClass* go_class;
	CcItemClass * ci_class;
	CcShapeClass* cs_class;

	/* GObjectClass */
	go_class = G_OBJECT_CLASS(self_class);
	go_class->get_property = cc_get_property;
	go_class->set_property = cc_set_property;

	g_object_class_install_property(go_class,
					PROP_ANCHOR,
					g_param_spec_pointer("anchor",
							     "Anchor",
							     "The center of the circle",
							     0));
	g_object_class_install_property(go_class,
					PROP_RADIUS,
					g_param_spec_double("radius",
							    "Radius",
							    "Radius",
							    0.0,
							    G_MAXDOUBLE,
							    0.0,
							    G_PARAM_READWRITE));

	/* CcItemClass */
	ci_class = CC_ITEM_CLASS(self_class);
	ci_class->distance      = cc_distance;
	ci_class->update_bounds = cc_update_bounds;

	/* CcShapeClass */
	cs_class = CC_SHAPE_CLASS(self_class);
	cs_class->path = cc_path;
}