2006-12-03 Dimitris Glezos <dimitris@glezos.com>
[dia.git] / lib / diatransform.c
blobb5c8b5eeb077c9cf4e0b9a6e4cf7c6132b343413
1 /* Dia -- an diagram creation/manipulation program
2 * Copyright (C) 1998 Alexander Larsson
4 * diatransform.c - scaling indirection to connect display
5 * and renderers
6 * Copyright (C) 2002 Hans Breuer (refactoring)
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.
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.
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.
23 #include "diatransform.h"
25 typedef struct _DiaTransformClass DiaTransformClass;
27 struct _DiaTransform
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 */
35 struct _DiaTransformClass
37 GObjectClass parent_class;
40 static void dia_transform_class_init (DiaTransformClass *klass);
42 static gpointer parent_class = NULL;
44 GType
45 dia_transform_get_type (void)
47 static GType object_type = 0;
49 if (!object_type)
51 static const GTypeInfo object_info =
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 */
64 object_type = g_type_register_static (G_TYPE_OBJECT,
65 "DiaTransform",
66 &object_info, 0);
69 return object_type;
72 static void
73 dia_transform_finalize (GObject *object)
75 /* don't free the fields, we don't own them */
77 G_OBJECT_CLASS (parent_class)->finalize (object);
80 static void
81 dia_transform_class_init (DiaTransformClass *klass)
83 GObjectClass *object_class = G_OBJECT_CLASS (klass);
85 parent_class = g_type_class_peek_parent (klass);
87 object_class->finalize = dia_transform_finalize;
91 DiaTransform *
92 dia_transform_new (Rectangle *rect, real* zoom)
94 DiaTransform *t = g_object_new (DIA_TYPE_TRANSFORM, NULL);
95 t->visible = rect;
96 t->factor = zoom;
98 return t;
101 real
102 dia_transform_length (DiaTransform *t, real len)
104 g_return_val_if_fail (DIA_IS_TRANSFORM (t), len);
105 g_return_val_if_fail (t != NULL && *t->factor != 0.0, len);
107 return (len * *(t->factor));
110 /* Takes pixel length and returns real length */
111 real
112 dia_untransform_length(DiaTransform *t, real len)
114 g_return_val_if_fail (DIA_IS_TRANSFORM (t), len);
115 g_return_val_if_fail (t != NULL && *t->factor != 0.0, len);
117 return len / *(t->factor);
120 void
121 dia_transform_coords (DiaTransform *t,
122 coord x, coord y,
123 int *xi, int *yi)
125 g_return_if_fail (DIA_IS_TRANSFORM (t));
126 g_return_if_fail (t != NULL && t->factor != NULL);
128 *xi = ROUND ( (x - t->visible->left) * *(t->factor));
129 *yi = ROUND ( (y - t->visible->top) * *(t->factor));
132 void
133 dia_transform_coords_double (DiaTransform *t,
134 coord x, coord y,
135 double *xd, double *yd)
137 g_return_if_fail (DIA_IS_TRANSFORM (t));
138 g_return_if_fail (t != NULL && t->factor != NULL);
140 *xd = ((x - t->visible->left) * *(t->factor));
141 *yd = ((y - t->visible->top) * *(t->factor));