1 /* Dia -- an diagram creation/manipulation program
2 * Copyright (C) 1998 Alexander Larsson
3 *
4 * diatransform.c - scaling indirection to connect display
5 * and renderers
6 * Copyright (C) 2002 Hans Breuer (refactoring)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23 #include "diatransform.h"
24
25 typedef struct _DiaTransformClass DiaTransformClass;
26
27 struct _DiaTransform
28 {
29 GObject parent_instance;
30 /*< private >*/
31 Rectangle *visible; /* pointer to original rectangle for transform_coords */
32 real *factor; /* pointer to original factor for transform_length */
33 };
34
35 struct _DiaTransformClass
36 {
37 GObjectClass parent_class;
38 };
39
40 static void dia_transform_class_init (DiaTransformClass *klass);
41
42 static gpointer parent_class = NULL;
43
44 GType
dia_transform_get_type(void)45 dia_transform_get_type (void)
46 {
47 static GType object_type = 0;
48
49 if (!object_type)
50 {
51 static const GTypeInfo object_info =
52 {
53 sizeof (DiaTransformClass),
54 (GBaseInitFunc) NULL,
55 (GBaseFinalizeFunc) NULL,
56 (GClassInitFunc) dia_transform_class_init,
57 NULL, /* class_finalize */
58 NULL, /* class_data */
59 sizeof (DiaTransform),
60 0, /* n_preallocs */
61 NULL /* init */
62 };
63
64 object_type = g_type_register_static (G_TYPE_OBJECT,
65 "DiaTransform",
66 &object_info, 0);
67 }
68
69 return object_type;
70 }
71
72 static void
dia_transform_finalize(GObject * object)73 dia_transform_finalize (GObject *object)
74 {
75 /* don't free the fields, we don't own them */
76
77 G_OBJECT_CLASS (parent_class)->finalize (object);
78 }
79
80 static void
dia_transform_class_init(DiaTransformClass * klass)81 dia_transform_class_init (DiaTransformClass *klass)
82 {
83 GObjectClass *object_class = G_OBJECT_CLASS (klass);
84
85 parent_class = g_type_class_peek_parent (klass);
86
87 object_class->finalize = dia_transform_finalize;
88
89 }
90
91 DiaTransform *
dia_transform_new(Rectangle * rect,real * zoom)92 dia_transform_new (Rectangle *rect, real* zoom)
93 {
94 DiaTransform *t = g_object_new (DIA_TYPE_TRANSFORM, NULL);
95 t->visible = rect;
96 t->factor = zoom;
97
98 return t;
99 }
100
101 real
dia_transform_length(DiaTransform * t,real len)102 dia_transform_length (DiaTransform *t, real len)
103 {
104 g_return_val_if_fail (DIA_IS_TRANSFORM (t), len);
105 g_return_val_if_fail (t != NULL && *t->factor != 0.0, len);
106
107 return (len * *(t->factor));
108 }
109
110 /* Takes pixel length and returns real length */
111 real
dia_untransform_length(DiaTransform * t,real len)112 dia_untransform_length(DiaTransform *t, real len)
113 {
114 g_return_val_if_fail (DIA_IS_TRANSFORM (t), len);
115 g_return_val_if_fail (t != NULL && *t->factor != 0.0, len);
116
117 return len / *(t->factor);
118 }
119
120 void
dia_transform_coords(DiaTransform * t,coord x,coord y,int * xi,int * yi)121 dia_transform_coords (DiaTransform *t,
122 coord x, coord y,
123 int *xi, int *yi)
124 {
125 g_return_if_fail (DIA_IS_TRANSFORM (t));
126 g_return_if_fail (t != NULL && t->factor != NULL);
127
128 *xi = ROUND ( (x - t->visible->left) * *(t->factor));
129 *yi = ROUND ( (y - t->visible->top) * *(t->factor));
130 }
131
132 void
dia_transform_coords_double(DiaTransform * t,coord x,coord y,double * xd,double * yd)133 dia_transform_coords_double (DiaTransform *t,
134 coord x, coord y,
135 double *xd, double *yd)
136 {
137 g_return_if_fail (DIA_IS_TRANSFORM (t));
138 g_return_if_fail (t != NULL && t->factor != NULL);
139
140 *xd = ((x - t->visible->left) * *(t->factor));
141 *yd = ((y - t->visible->top) * *(t->factor));
142 }
143
144