File: prop_matrix.c

package info (click to toggle)
dia 0.97.3%2Bgit20160930-9
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 54,372 kB
  • sloc: ansic: 155,065; xml: 16,326; python: 6,641; cpp: 4,935; makefile: 3,833; sh: 540; perl: 137; sed: 19
file content (281 lines) | stat: -rw-r--r-- 7,962 bytes parent folder | download | duplicates (3)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* Dia -- a diagram creation/manipulation program -*- c -*-
 * Copyright (C) 1998 Alexander Larsson
 *
 * Property system for dia objects/shapes.
 * Copyright (C) 2000 James Henstridge
 * Copyright (C) 2001 Cyrille Chepelov
 *
 * Copyright (C) 2010 Hans Breuer
 * Property type for affine transformation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU 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 <config.h>

#include <math.h>
#include <gtk/gtk.h>
#define WIDGET GtkWidget
#include "widgets.h"
#include "properties.h"
#include "propinternals.h"
#include "message.h"

static MatrixProperty *
matrixprop_new(const PropDescription *pdesc, PropDescToPropPredicate reason)
{
  MatrixProperty *prop = g_new0(MatrixProperty,1);

  initialize_property(&prop->common, pdesc, reason);
  /* empty by default (=identity) */
  prop->matrix = NULL;

  return prop;
}

static void
matrixprop_free(MatrixProperty *prop) 
{
  g_free (prop->matrix);
  g_free(prop);
} 

static MatrixProperty *
matrixprop_copy(MatrixProperty *src) 
{
  MatrixProperty *prop = 
    (MatrixProperty *)src->common.ops->new_prop(src->common.descr,
                                              src->common.reason);

  prop->matrix = g_memdup (src->matrix, sizeof(DiaMatrix));
					      
  return prop;
}

static real
_matrix_xml_get_value (DataNode data, const char* name, real defval)
{
  xmlChar *val;
  real retval;

  val = xmlGetProp(data, (const xmlChar *)name);
  if (!val)
    return defval;
  retval = g_ascii_strtod((char *)val, NULL);
  xmlFree(val);
  return retval;
}
DiaMatrix *
data_matrix (DataNode data)
{
  DiaMatrix *matrix;

  matrix = g_new (DiaMatrix, 1);
  matrix->xx = _matrix_xml_get_value (data, "xx", 1.0);
  matrix->xy = _matrix_xml_get_value (data, "xy", 0.0);
  matrix->yx = _matrix_xml_get_value (data, "yx", 0.0);
  matrix->yy = _matrix_xml_get_value (data, "yy", 1.0);
  matrix->x0 = _matrix_xml_get_value (data, "x0", 0.0);
  matrix->y0 = _matrix_xml_get_value (data, "y0", 0.0);

  /* identity? */
  if (matrix->xx == 1.0 && matrix->yx == 0.0 &&
      matrix->xy == 0.0 && matrix->yy == 1.0 &&
      matrix->x0 == 0.0 && matrix->y0 == 0.0) {
    g_free (matrix);
    return NULL;
  }
  /* TODO: check it's invertible? */

  return matrix;
}

static void 
matrixprop_load(MatrixProperty *prop, AttributeNode attr, DataNode data, DiaContext *ctx)
{
  prop->matrix = data_matrix (data);
}

static void
_matrix_xml_add_val (DataNode data_node, const char* name, real val)
{
  gchar buf[G_ASCII_DTOSTR_BUF_SIZE];

  g_ascii_formatd(buf, sizeof(buf), "%g", val);
  xmlSetProp(data_node, (const xmlChar *)name, (xmlChar *)buf);
}
void
data_add_matrix (AttributeNode attr, DiaMatrix *matrix, DiaContext *ctx)
{
  DataNode data_node;
  
  data_node = xmlNewChild(attr, NULL, (const xmlChar *)"matrix", NULL);

  if (matrix) {
    _matrix_xml_add_val (data_node, "xx", matrix->xx);
    _matrix_xml_add_val (data_node, "xy", matrix->xy);
    _matrix_xml_add_val (data_node, "yx", matrix->yx);
    _matrix_xml_add_val (data_node, "yy", matrix->yy);
    _matrix_xml_add_val (data_node, "x0", matrix->x0);
    _matrix_xml_add_val (data_node, "y0", matrix->y0);
  }
}

static void 
matrixprop_save(MatrixProperty *prop, AttributeNode attr, DiaContext *ctx) 
{
  if (prop->matrix) {
    data_add_matrix (attr, prop->matrix, ctx);
  }
}

static void 
matrixprop_get_from_offset(MatrixProperty *prop,
                         void *base, guint offset, guint offset2) 
{
  DiaMatrix *matrix = struct_member(base,offset,DiaMatrix *);

  prop->matrix = g_memdup (matrix, sizeof (DiaMatrix));
}

static void 
matrixprop_set_from_offset(MatrixProperty *prop,
                           void *base, guint offset, guint offset2)
{
  DiaMatrix *dest = struct_member(base,offset,DiaMatrix *);
  if (dest)
    g_free (dest);

  struct_member(base,offset, DiaMatrix *) = g_memdup (prop->matrix, sizeof (DiaMatrix));
}

/* GUI stuff - just the angle for now
 */
static GtkWidget *
matrixprop_get_widget (MatrixProperty *prop, PropDialog *dialog) 
{ 
  GtkAdjustment *adj;
  GtkWidget *ret, *sb;
  int i;

  ret = gtk_hbox_new (FALSE,0);
  /* angle */
  adj = GTK_ADJUSTMENT (gtk_adjustment_new(0.0, -180.0, 180.0, 1.0, 15.0, 0));
  sb = gtk_spin_button_new(adj, 1.0, 2);
  gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(sb),TRUE);
  prophandler_connect(&prop->common, G_OBJECT(sb), "changed");
  gtk_widget_show(sb);
  gtk_box_pack_start(GTK_BOX(ret), sb, TRUE, TRUE, 0);
  /* sx, sy */
  for (i = 0; i < 2; ++i) {
    adj = GTK_ADJUSTMENT (gtk_adjustment_new(0.0, 0.01, 100.0, 0.01, 1.0, 0));
    sb = gtk_spin_button_new(GTK_ADJUSTMENT (adj), 1.0, 2);
    gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(sb),TRUE);
    prophandler_connect(&prop->common, G_OBJECT(sb), "changed");
    gtk_widget_show(sb);
    gtk_box_pack_start(GTK_BOX(ret), sb, TRUE, TRUE, 0);
  }
  
  return ret;
}

static void 
matrixprop_reset_widget(MatrixProperty *prop, GtkWidget *widget)
{
  GList *children, *child;
  GtkWidget *sb;
  real angle, sx, sy;
  int i = 0;

  if (!prop->matrix) {
    angle = 0;
    sx = sy = 1.0;
  } else {
    real a;

    dia_matrix_get_angle_and_scales (prop->matrix, &a, &sx, &sy);

    angle = -a*180/G_PI;
  }

  children = gtk_container_get_children (GTK_CONTAINER (widget));
  for (child = children; child != NULL; child = g_list_next (child)) {
    GtkAdjustment *adj;
    sb = child->data;
    adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON(sb));
    if (i == 0)
      gtk_adjustment_configure (adj, angle, -180.0, 180.0, 1.0, 15.0, 0);
    else if (i == 1)
      gtk_adjustment_configure (adj, sx, 0.01, 100.0, 0.1, 1.0, 0);
    else if (i == 2)
      gtk_adjustment_configure (adj, sy, 0.01, 100.0, 0.1, 1.0, 0);
    else
      g_assert_not_reached ();
    ++i;
  }
}

static void 
matrixprop_set_from_widget(MatrixProperty *prop, GtkWidget *widget) 
{
  GList *children, *child;
  GtkWidget *sb;
  real angle = 0.0, sx = 1.0, sy = 1.0;
  int i = 0;

  children = gtk_container_get_children (GTK_CONTAINER (widget));
  for (child = children; child != NULL; child = g_list_next (child)) {
    sb = child->data;
    if (i == 0)
      angle = gtk_spin_button_get_value(GTK_SPIN_BUTTON(sb));
    else if (i == 1)
      sx = gtk_spin_button_get_value(GTK_SPIN_BUTTON(sb));
    else if (i == 2)
      sy = gtk_spin_button_get_value(GTK_SPIN_BUTTON(sb));
    else
      g_assert_not_reached ();
    ++i;
  }

  if (angle != 0.0 || sx != 1.0 || sy != 1.0) {
    if (!prop->matrix) {
      prop->matrix = g_new0 (DiaMatrix, 1);
    }
    dia_matrix_set_angle_and_scales (prop->matrix, -angle/180.0*G_PI, sx, sy);
  } else {
    g_free (prop->matrix);
    prop->matrix = NULL;
  }
}

static const PropertyOps matrixprop_ops = {
  (PropertyType_New) matrixprop_new,
  (PropertyType_Free) matrixprop_free,
  (PropertyType_Copy) matrixprop_copy,
  (PropertyType_Load) matrixprop_load,
  (PropertyType_Save) matrixprop_save,
  (PropertyType_GetWidget) matrixprop_get_widget,
  (PropertyType_ResetWidget) matrixprop_reset_widget,
  (PropertyType_SetFromWidget) matrixprop_set_from_widget,

  (PropertyType_CanMerge) noopprop_can_merge,
  (PropertyType_GetFromOffset) matrixprop_get_from_offset,
  (PropertyType_SetFromOffset) matrixprop_set_from_offset
};

void 
prop_matrix_register(void)
{
  prop_type_register(PROP_TYPE_MATRIX, &matrixprop_ops);
}