File: prop_attr.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 (504 lines) | stat: -rw-r--r-- 15,531 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/* 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
 * Major restructuration done in August 2001 by C. Chépélov
 *
 * Property types for "attribute" types (line style, arrow, colour, font)
 *
 * 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.
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gtk/gtk.h>
#include "dia_xml.h"
#include "widgets.h"
#include "properties.h"
#include "propinternals.h"
#include "diaarrowchooser.h"
#include "diafontselector.h"

/***************************/
/* The LINESTYLE property type.  */
/***************************/

static LinestyleProperty *
linestyleprop_new(const PropDescription *pdesc, 
                  PropDescToPropPredicate reason)
{
  LinestyleProperty *prop = g_new0(LinestyleProperty,1);
  initialize_property(&prop->common,pdesc,reason);
  prop->style = LINESTYLE_SOLID;
  prop->dash = 0.0;
  return prop;
}

static LinestyleProperty *
linestyleprop_copy(LinestyleProperty *src) 
{
  LinestyleProperty *prop = 
    (LinestyleProperty *)src->common.ops->new_prop(src->common.descr,
                                                    src->common.reason);
  copy_init_property(&prop->common,&src->common);
  prop->style = src->style;
  prop->dash = src->dash;
  return prop;
}

static WIDGET *
linestyleprop_get_widget(LinestyleProperty *prop, PropDialog *dialog)
{
  GtkWidget *ret = dia_line_style_selector_new();
  prophandler_connect(&prop->common, G_OBJECT(ret), "value-changed");
  return ret;
}

static void 
linestyleprop_reset_widget(LinestyleProperty *prop, WIDGET *widget)
{
  dia_line_style_selector_set_linestyle(DIALINESTYLESELECTOR(widget),
                                        prop->style,
                                        prop->dash);
}

static void 
linestyleprop_set_from_widget(LinestyleProperty *prop, WIDGET *widget) 
{
  dia_line_style_selector_get_linestyle(DIALINESTYLESELECTOR(widget),
                                        &prop->style,
                                        &prop->dash);
}

static void 
linestyleprop_load(LinestyleProperty *prop, AttributeNode attr, DataNode data, DiaContext *ctx)
{
  prop->style = data_enum(data, ctx);
  prop->dash = 1.0;
  /* don't bother checking dash length if we have a solid line. */
  if (prop->style != LINESTYLE_SOLID) {
    data = data_next(data);
    if (data)
      prop->dash = data_real(data, ctx);
    else {
      ObjectNode obj_node = attr->parent;
      /* backward compatibility */
      if ((attr = object_find_attribute(obj_node, "dashlength")) &&
          (data = attribute_first_data(attr)))
        prop->dash = data_real(data, ctx);
    }
  }
}

static void 
linestyleprop_save(LinestyleProperty *prop, AttributeNode attr, DiaContext *ctx) 
{
  data_add_enum(attr, prop->style, ctx);
  data_add_real(attr, prop->dash, ctx);
}

static void 
linestyleprop_get_from_offset(LinestyleProperty *prop,
                              void *base, guint offset, guint offset2) 
{
  prop->style = struct_member(base, offset, LineStyle);
  prop->dash = struct_member(base, offset2, real);
}

static void 
linestyleprop_set_from_offset(LinestyleProperty *prop,
                              void *base, guint offset, guint offset2)
{
  struct_member(base, offset, LineStyle) = prop->style;
  struct_member(base, offset2, real) = prop->dash;
}

static const PropertyOps linestyleprop_ops = {
  (PropertyType_New) linestyleprop_new,
  (PropertyType_Free) noopprop_free,
  (PropertyType_Copy) linestyleprop_copy,
  (PropertyType_Load) linestyleprop_load,
  (PropertyType_Save) linestyleprop_save,
  (PropertyType_GetWidget) linestyleprop_get_widget,
  (PropertyType_ResetWidget) linestyleprop_reset_widget,
  (PropertyType_SetFromWidget) linestyleprop_set_from_widget,

  (PropertyType_CanMerge) noopprop_can_merge,
  (PropertyType_GetFromOffset) linestyleprop_get_from_offset,
  (PropertyType_SetFromOffset) linestyleprop_set_from_offset
};

/***************************/
/* The ARROW property type.  */
/***************************/

static ArrowProperty *
arrowprop_new(const PropDescription *pdesc, PropDescToPropPredicate reason)
{
  ArrowProperty *prop = g_new0(ArrowProperty,1);
  initialize_property(&prop->common,pdesc,reason);
  prop->arrow_data.type = ARROW_NONE;
  prop->arrow_data.length = 0.0;
  prop->arrow_data.width = 0.0;
  return prop;
}

static ArrowProperty *
arrowprop_copy(ArrowProperty *src) 
{
  ArrowProperty *prop = 
    (ArrowProperty *)src->common.ops->new_prop(src->common.descr,
                                                src->common.reason);
  copy_init_property(&prop->common,&src->common);
  prop->arrow_data = src->arrow_data;
  return prop;
}

static WIDGET *
arrowprop_get_widget(ArrowProperty *prop, PropDialog *dialog)
{
  GtkWidget *ret = dia_arrow_selector_new();
  prophandler_connect(&prop->common, G_OBJECT(ret), "value-changed");
  return ret;
}

static void 
arrowprop_reset_widget(ArrowProperty *prop, WIDGET *widget)
{
  dia_arrow_selector_set_arrow(DIA_ARROW_SELECTOR(widget),
                               prop->arrow_data);
}

static void 
arrowprop_set_from_widget(ArrowProperty *prop, WIDGET *widget) 
{
  prop->arrow_data = dia_arrow_selector_get_arrow(DIA_ARROW_SELECTOR(widget));
}

static void 
arrowprop_load(ArrowProperty *prop, AttributeNode attr, DataNode data, DiaContext *ctx)
{
  /* Maybe it would be better to store arrows as a single composite
   * attribute rather than three seperate attributes. This would break
   * binary compatibility though.*/
  prop->arrow_data.type = data_enum(data, ctx);
  prop->arrow_data.length = DEFAULT_ARROW_SIZE;
  prop->arrow_data.width = DEFAULT_ARROW_SIZE;
  if (prop->arrow_data.type != ARROW_NONE) {
    ObjectNode obj_node = attr->parent;
    gchar *str = g_strconcat(prop->common.descr->name, "_length", NULL);
    if ((attr = object_find_attribute(obj_node, str)) &&
        (data = attribute_first_data(attr)))
      prop->arrow_data.length = data_real(data, ctx);
    g_free(str);
    str = g_strconcat(prop->common.descr->name, "_width", NULL);
    if ((attr = object_find_attribute(obj_node, str)) &&
        (data = attribute_first_data(attr)))
      prop->arrow_data.width = data_real(data, ctx);
    g_free(str);
  }
}

static void 
arrowprop_save(ArrowProperty *prop, AttributeNode attr, DiaContext *ctx) 
{
  data_add_enum(attr, prop->arrow_data.type, ctx);
  if (prop->arrow_data.type != ARROW_NONE) {
    ObjectNode obj_node = attr->parent;
    gchar *str = g_strconcat(prop->common.descr->name, "_length", NULL);
    attr = new_attribute(obj_node, str);
    g_free(str);
    data_add_real(attr, prop->arrow_data.length, ctx);
    str = g_strconcat(prop->common.descr->name, "_width", NULL);
    attr = new_attribute(obj_node, str);
    g_free(str);
    data_add_real(attr, prop->arrow_data.width, ctx);
  }
}

static void 
arrowprop_get_from_offset(ArrowProperty *prop,
                          void *base, guint offset, guint offset2) 
{
  prop->arrow_data = struct_member(base,offset,Arrow);
}

static void 
arrowprop_set_from_offset(ArrowProperty *prop,
                          void *base, guint offset, guint offset2)
{
  struct_member(base,offset,Arrow) = prop->arrow_data;
}

static const PropertyOps arrowprop_ops = {
  (PropertyType_New) arrowprop_new,
  (PropertyType_Free) noopprop_free,
  (PropertyType_Copy) arrowprop_copy,
  (PropertyType_Load) arrowprop_load,
  (PropertyType_Save) arrowprop_save,
  (PropertyType_GetWidget) arrowprop_get_widget,
  (PropertyType_ResetWidget) arrowprop_reset_widget,
  (PropertyType_SetFromWidget) arrowprop_set_from_widget,

  (PropertyType_CanMerge) noopprop_can_merge,
  (PropertyType_GetFromOffset) arrowprop_get_from_offset,
  (PropertyType_SetFromOffset) arrowprop_set_from_offset
};

/***************************/
/* The COLOR property type.  */
/***************************/

static ColorProperty *
colorprop_new(const PropDescription *pdesc, PropDescToPropPredicate reason)
{
  ColorProperty *prop = g_new0(ColorProperty,1);
  initialize_property(&prop->common,pdesc,reason);
  prop->color_data.red = 0.0;
  prop->color_data.green = 0.0;
  prop->color_data.blue = 1.0;
  prop->color_data.alpha = 1.0;
  return prop;
}

static ColorProperty *
colorprop_copy(ColorProperty *src) 
{
  ColorProperty *prop = 
    (ColorProperty *)src->common.ops->new_prop(src->common.descr,
                                                src->common.reason);
  copy_init_property(&prop->common,&src->common);
  prop->color_data = src->color_data;
  return prop;
}

static WIDGET *
colorprop_get_widget(ColorProperty *prop, PropDialog *dialog)
{ 
  GtkWidget *ret = dia_color_selector_new();
  dia_color_selector_set_use_alpha (ret, TRUE);
  prophandler_connect(&prop->common, G_OBJECT(ret), "value-changed");
  return ret;
}

static void 
colorprop_reset_widget(ColorProperty *prop, WIDGET *widget)
{
  dia_color_selector_set_color(widget,
                               &prop->color_data);
}

static void 
colorprop_set_from_widget(ColorProperty *prop, WIDGET *widget) 
{
  dia_color_selector_get_color(widget,
                               &prop->color_data);
}

static void 
colorprop_load(ColorProperty *prop, AttributeNode attr, DataNode data, DiaContext *ctx)
{
  data_color(data,&prop->color_data, ctx);
}

static void 
colorprop_save(ColorProperty *prop, AttributeNode attr, DiaContext *ctx) 
{
  data_add_color(attr,&prop->color_data, ctx);
}

static void 
colorprop_get_from_offset(ColorProperty *prop,
                          void *base, guint offset, guint offset2) 
{
  if (offset2 == 0) {
    prop->color_data = struct_member(base,offset,Color);
  } else {
    void *base2 = struct_member(base,offset,void*);
    g_return_if_fail (base2 != NULL);
    prop->color_data = struct_member(base2,offset2,Color);
  }
}

static void 
colorprop_set_from_offset(ColorProperty *prop,
                          void *base, guint offset, guint offset2)
{
  if (offset2 == 0) {
    struct_member(base,offset,Color) = prop->color_data;
  } else {
    void *base2 = struct_member(base,offset,void*);
    g_return_if_fail (base2 != NULL);
    struct_member(base2,offset2,Color) = prop->color_data;
    g_return_if_fail (offset2 == offsetof(Text, color));
    text_set_color ((Text *)base2, &prop->color_data);
  }
}

static const PropertyOps colorprop_ops = {
  (PropertyType_New) colorprop_new,
  (PropertyType_Free) noopprop_free,
  (PropertyType_Copy) colorprop_copy,
  (PropertyType_Load) colorprop_load,
  (PropertyType_Save) colorprop_save,
  (PropertyType_GetWidget) colorprop_get_widget,
  (PropertyType_ResetWidget) colorprop_reset_widget,
  (PropertyType_SetFromWidget) colorprop_set_from_widget,

  (PropertyType_CanMerge) noopprop_can_merge,
  (PropertyType_GetFromOffset) colorprop_get_from_offset,
  (PropertyType_SetFromOffset) colorprop_set_from_offset
};


/***************************/
/* The FONT property type. */
/***************************/

static FontProperty *
fontprop_new(const PropDescription *pdesc, PropDescToPropPredicate reason)
{
  FontProperty *prop = g_new0(FontProperty,1);
  initialize_property(&prop->common,pdesc,reason);
  prop->font_data = NULL;
  return prop;
}

static void
fontprop_free(FontProperty *prop)
{
  if (prop->font_data)
    dia_font_unref(prop->font_data);
  g_free(prop);
}

static FontProperty *
fontprop_copy(FontProperty *src) 
{
  FontProperty *prop = 
    (FontProperty *)src->common.ops->new_prop(src->common.descr,
                                               src->common.reason);
  copy_init_property(&prop->common,&src->common);

  if (prop->font_data)
    dia_font_unref(prop->font_data);
  prop->font_data = dia_font_ref(src->font_data);

  return prop;
}

static WIDGET *
fontprop_get_widget(FontProperty *prop, PropDialog *dialog)
{ 
  GtkWidget *ret = dia_font_selector_new();
  prophandler_connect(&prop->common, G_OBJECT(ret), "value-changed");
  return ret;
}

static void 
fontprop_reset_widget(FontProperty *prop, WIDGET *widget)
{
  dia_font_selector_set_font(DIAFONTSELECTOR(widget),
                             prop->font_data);
}

static void 
fontprop_set_from_widget(FontProperty *prop, WIDGET *widget) 
{
  prop->font_data = dia_font_selector_get_font(DIAFONTSELECTOR(widget));
}

static void 
fontprop_load(FontProperty *prop, AttributeNode attr, DataNode data, DiaContext *ctx)
{
  if (prop->font_data)
    dia_font_unref(prop->font_data);
  prop->font_data = data_font(data, ctx);
}

static void 
fontprop_save(FontProperty *prop, AttributeNode attr, DiaContext *ctx) 
{
  data_add_font(attr,prop->font_data, ctx);
}

static void 
fontprop_get_from_offset(FontProperty *prop,
                         void *base, guint offset, guint offset2) 
{
  /* if we get the same font dont unref before reuse */
  DiaFont *old_font = prop->font_data;
  if (offset2 == 0) {
    prop->font_data = dia_font_ref(struct_member(base,offset,DiaFont *));
  } else {
    void *base2 = struct_member(base,offset,void*);
    g_return_if_fail (base2 != NULL);
    prop->font_data = dia_font_ref(struct_member(base2,offset2,DiaFont *));
  }
  if (old_font)
    dia_font_unref(old_font);
}

static void 
fontprop_set_from_offset(FontProperty *prop,
                         void *base, guint offset, guint offset2)
{
  if (prop->font_data) {
    DiaFont *old_font;

    if (offset2 == 0) {
      old_font = struct_member(base,offset,DiaFont *);
      struct_member(base,offset,DiaFont *) = dia_font_ref(prop->font_data);
    } else {
      void *base2 = struct_member(base,offset,void*);
      g_return_if_fail (base2 != NULL);
      old_font = struct_member(base2,offset2,DiaFont *);
      struct_member(base2,offset2,DiaFont *) = dia_font_ref(prop->font_data);
      g_return_if_fail (offset2 == offsetof(Text, font));
      text_set_font ((Text *)base2, prop->font_data);
    }
    if (old_font)
      dia_font_unref(old_font);
  }
}

static const PropertyOps fontprop_ops = {
  (PropertyType_New) fontprop_new,
  (PropertyType_Free) fontprop_free,
  (PropertyType_Copy) fontprop_copy,
  (PropertyType_Load) fontprop_load,
  (PropertyType_Save) fontprop_save,
  (PropertyType_GetWidget) fontprop_get_widget,
  (PropertyType_ResetWidget) fontprop_reset_widget,
  (PropertyType_SetFromWidget) fontprop_set_from_widget,

  (PropertyType_CanMerge) noopprop_can_merge,
  (PropertyType_GetFromOffset) fontprop_get_from_offset,
  (PropertyType_SetFromOffset) fontprop_set_from_offset
};

/* ************************************************************** */ 

void 
prop_attr_register(void)
{
  prop_type_register(PROP_TYPE_LINESTYLE,&linestyleprop_ops);
  prop_type_register(PROP_TYPE_ARROW,&arrowprop_ops);
  prop_type_register(PROP_TYPE_COLOUR,&colorprop_ops);
  prop_type_register(PROP_TYPE_FONT,&fontprop_ops);
}