File: propobject.c

package info (click to toggle)
dia 0.98%2Bgit20250827-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,064 kB
  • sloc: ansic: 155,731; xml: 14,062; python: 6,250; cpp: 3,635; sh: 447; perl: 137; makefile: 29
file content (530 lines) | stat: -rw-r--r-- 13,617 bytes parent folder | download | duplicates (2)
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/* Dia -- a diagram creation/manipulation program
 * 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. Chepelov
 *
 * Propobject.c: routines which deal with Dia Objects and affect their
 * properties.
 *
 * Most of these routines used to exist in code before the restructuration.
 * They've lost most of their meat, in favour for more modularity.
 *
 * 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 <glib/gi18n-lib.h>

#include <string.h>

#include <glib.h>
#include "properties.h"
#include "propinternals.h"
#include "object.h"
#include "dia-object-change-list.h"


const PropDescription *
object_get_prop_descriptions (const DiaObject *obj)
{
  const PropDescription *pdesc;

  pdesc = dia_object_describe_properties (DIA_OBJECT (obj)); /* Yes... */
  if (!pdesc) return NULL;

  if (pdesc[0].quark != 0) return pdesc;

  prop_desc_list_calculate_quarks ((PropDescription *) pdesc); /* Yes again... */
  return pdesc;
}

const PropDescription *
object_list_get_prop_descriptions (GList *objects, PropMergeOption option)
{
  GList *descs = NULL, *tmp;
  const PropDescription *pdesc;

  for (tmp = objects; tmp != NULL; tmp = tmp->next) {
    DiaObject *obj = tmp->data;
    const PropDescription *desc = object_get_prop_descriptions (obj);

    if (desc) descs = g_list_append (descs, (gpointer)desc);
  }

  /* use intersection for single object's list because it is more
   * complete than union. The latter does not include PROP_FLAG_DONT_MERGE */
  if (option == PROP_UNION && g_list_length(objects)!=1)
    pdesc = prop_desc_lists_union(descs);
  else
    pdesc = prop_desc_lists_intersection(descs);

  /* Important: Do not destroy the actual descriptions returned by the
     objects. We don't own them. */
  g_list_free(descs);

  return pdesc;
}

const PropDescription *
object_describe_props (DiaObject *obj)
{
  const PropDescription *props = obj->type->prop_descs;

  g_return_val_if_fail (props != NULL, NULL);

  if (props[0].quark == 0)
    prop_desc_list_calculate_quarks((PropDescription *)props);
  return props;
}

void
object_get_props(DiaObject *obj, GPtrArray *props)
{
  const PropOffset *offsets = obj->type->prop_offsets;

  g_return_if_fail (offsets != NULL);

  object_get_props_from_offsets(obj, (PropOffset *)offsets, props);
}

/* ------------------------------------------------------ */
/* Change management                                      */

/* an DiaObjectChange structure for setting of properties */
struct _DiaPropObjectChange {
  DiaObjectChange obj_change;

  DiaObject *obj;
  GPtrArray *saved_props;
};


DIA_DEFINE_OBJECT_CHANGE (DiaPropObjectChange, dia_prop_object_change)


static void
dia_prop_object_change_apply_revert (DiaPropObjectChange *change, DiaObject *obj)
{
  GPtrArray *old_props;

  old_props = prop_list_copy_empty (change->saved_props);

  dia_object_get_properties (change->obj, old_props);

  /* set saved property values */
  dia_object_set_properties (change->obj, change->saved_props);

  /* move old props to saved properties */
  prop_list_free (change->saved_props);
  change->saved_props = old_props;
}


static void
dia_prop_object_change_apply (DiaObjectChange *self, DiaObject *obj)
{
  dia_prop_object_change_apply_revert (DIA_PROP_OBJECT_CHANGE (self), obj);
}


static void
dia_prop_object_change_revert (DiaObjectChange *self, DiaObject *obj)
{
  dia_prop_object_change_apply_revert (DIA_PROP_OBJECT_CHANGE (self), obj);
}


static void
dia_prop_object_change_free (DiaObjectChange *self)
{
  DiaPropObjectChange *change = DIA_PROP_OBJECT_CHANGE (self);

  prop_list_free (change->saved_props);
}


DiaObjectChange *
object_apply_props (DiaObject *obj, GPtrArray *props)
{
  DiaPropObjectChange *change;
  GPtrArray *old_props;

  change = dia_object_change_new (DIA_TYPE_PROP_OBJECT_CHANGE);

  change->obj = obj;

  /* create new properties structure with current values */
  old_props = prop_list_copy_empty (props);

  dia_object_get_properties (obj, old_props);

  /* set saved property values */
  dia_object_set_properties (obj, props);

  change->saved_props = old_props;

  return DIA_OBJECT_CHANGE (change);
}


/**
 * object_toggle_prop:
 * @obj: the #DiaObject
 * @pname: the property name
 * @val: the new value
 *
 * Toggle a boolean property including change management
 */
DiaObjectChange *
object_toggle_prop (DiaObject *obj, const char *pname, gboolean val)
{
  Property *prop = make_new_prop (pname, PROP_TYPE_BOOL, 0);
  GPtrArray *plist = prop_list_from_single (prop);

  ((BoolProperty *)prop)->bool_data = val;

  return object_apply_props (obj, plist);
}

/* Get/Set routines */

gboolean
object_get_props_from_offsets(DiaObject *obj, PropOffset *offsets,
                              GPtrArray *props)
{
  prop_offset_list_calculate_quarks(offsets);
  do_get_props_from_offsets(obj,props,offsets);

  return TRUE;
}

gboolean
object_set_props_from_offsets(DiaObject *obj, PropOffset *offsets,
                              GPtrArray *props)
{
  prop_offset_list_calculate_quarks(offsets);
  do_set_props_from_offsets(obj,props,offsets);

  return TRUE;
}


WIDGET *
object_create_props_dialog(DiaObject *obj, gboolean is_default)
{
  GList *list = NULL;
  list = g_list_append(list, obj);
  return object_list_create_props_dialog(list, is_default);
}

WIDGET *
object_list_create_props_dialog(GList *objects, gboolean is_default)
{
  return prop_dialog_new(objects, is_default)->widget;
}


DiaObjectChange *
object_apply_props_from_dialog (DiaObject *obj, WIDGET *dialog_widget)
{
  DiaObjectChange *change;
  PropDialog *dialog = prop_dialog_from_widget (dialog_widget);
  GPtrArray *props = g_ptr_array_new ();
  guint i;

  prop_get_data_from_widgets (dialog);
  /* only stuff which is actually changed */
  for (i = 0; i < dialog->props->len; ++i) {
    Property *p = g_ptr_array_index (dialog->props, i);
    if (p->descr->flags & PROP_FLAG_WIDGET_ONLY) {
      continue;
    }
    if ((p->experience & PXP_NOTSET) == 0) {
      g_ptr_array_add (props, p);
    }
  }
  change = dia_object_apply_properties (obj, props);
  g_ptr_array_free (props, TRUE);
  return change;
}


gboolean
objects_comply_with_stdprop(GList *objects)
{
  GList *tmp = objects;

  for (; tmp != NULL; tmp = tmp->next) {
    const DiaObject *obj = (const DiaObject*)tmp->data;
    if (!object_complies_with_stdprop(obj))
      return FALSE;
  }

  return TRUE;
}

gboolean
object_complies_with_stdprop(const DiaObject *obj)
{
  if (object_get_prop_descriptions(obj) == NULL) {
    g_warning("No properties !");
    return FALSE;
  }
  return TRUE;
}

void
object_list_get_props (GList *objects, GPtrArray *props)
{
  GList *tmp = objects;

  for (; tmp != NULL; tmp = tmp->next) {
    DiaObject *obj = (DiaObject*) tmp->data;
    dia_object_get_properties (obj, props);
  }
}

static gboolean
pdtpp_do_save_no_standard_default (const PropDescription *pdesc)
{
  return pdtpp_do_save_no_standard(pdesc) && pdtpp_defaults (pdesc);
}

void
object_copy_props (DiaObject *dest, const DiaObject *src, gboolean is_default)
{
  GPtrArray *props;

  g_return_if_fail (src != NULL);
  g_return_if_fail (dest != NULL);
  g_return_if_fail (g_strcmp0 (src->type->name, dest->type->name) == 0);
  g_return_if_fail (object_complies_with_stdprop (src));
  g_return_if_fail (object_complies_with_stdprop (dest));

  props = prop_list_from_descs (object_get_prop_descriptions (src),
                                (is_default ? pdtpp_do_save_no_standard_default:
                                              pdtpp_do_save));

  dia_object_get_properties ((DiaObject *) src, props);
  dia_object_set_properties (dest, props);

  prop_list_free (props);
}

void
object_load_props(DiaObject *obj, ObjectNode obj_node, DiaContext *ctx)
{
  GPtrArray *props;

  g_return_if_fail(obj != NULL);
  g_return_if_fail(obj_node != NULL);
  g_return_if_fail(object_complies_with_stdprop(obj));

  props = prop_list_from_descs(object_get_prop_descriptions(obj),
                               pdtpp_do_load);

  if (!prop_list_load(props,obj_node, ctx)) {
    /* context already has the message */
  }

  dia_object_set_properties (obj, props);
  prop_list_free (props);
}

void
object_save_props(DiaObject *obj, ObjectNode obj_node, DiaContext *ctx)
{
  GPtrArray *props;

  g_return_if_fail(obj != NULL);
  g_return_if_fail(obj_node != NULL);
  g_return_if_fail(object_complies_with_stdprop(obj));

  props = prop_list_from_descs(object_get_prop_descriptions(obj),
                               pdtpp_do_save);

  dia_object_get_properties (obj, props);
  prop_list_save (props, obj_node, ctx);
  prop_list_free (props);
}

Property *
object_prop_by_name_type(DiaObject *obj, const char *name, const char *type)
{
  const PropDescription *pdesc;
  GQuark name_quark = g_quark_from_string(name);

  if (!object_complies_with_stdprop(obj)) return NULL;

  for (pdesc = object_get_prop_descriptions(obj);
       pdesc->name != NULL;
       pdesc++) {
    if (name_quark == 0 || (pdesc->quark == name_quark)) {
      Property *prop;
      static GPtrArray *plist = NULL;

      if (type && (0 != strcmp(pdesc->type,type))) continue;

      if (!plist) {
        plist = g_ptr_array_new();
        g_ptr_array_set_size(plist,1);
      }
      prop = pdesc->ops->new_prop (pdesc, pdtpp_from_object);
      g_ptr_array_index (plist, 0) = prop;
      dia_object_get_properties (obj, plist);
      return prop;
    }
  }
  return NULL;
}


Property *
object_prop_by_name (DiaObject *obj, const char *name)
{
  return object_prop_by_name_type (obj, name,NULL);
}


/**
 * dia_object_set_pixbuf:
 * @object: object to modify
 * @pixbuf: the pixbuf to set
 *
 * Modification of the objects 'pixbuf' property
 *
 * If the object does not have a pixbuf property nothing
 * happens. If there is a pixbuf property and the passed
 * in pixbuf is identical an empty change is returned.
 *
 * Returns: #DiaObjectChange or %NULL
 */
DiaObjectChange *
dia_object_set_pixbuf (DiaObject *object,
                       GdkPixbuf *pixbuf)
{
  DiaObjectChange *change;
  GPtrArray *props;
  PixbufProperty *pp;
  Property *prop = object_prop_by_name_type (object, "pixbuf", PROP_TYPE_PIXBUF);

  if (!prop) {
    return NULL;
  }

  pp = (PixbufProperty *) prop;
  if (pp->pixbuf == pixbuf) {
    return dia_object_change_list_new ();
  }

  g_set_object (&pp->pixbuf, pixbuf);

  props = prop_list_from_single (prop);
  change = object_apply_props (object, props);
  prop_list_free (props);
  return change;
}


/**
 * dia_object_set_pattern:
 * @object: object to modify
 * @pattern: the pattern to set
 *
 * Modification of the objects 'pattern' property
 *
 * If the object does not have a pattern property nothing
 * happens. If there is a pattern property and the passed
 * in pattern is identical an empty change is returned.
 *
 * Returns: #DiaObjectChange or %NULL
 */
DiaObjectChange *
dia_object_set_pattern (DiaObject  *object,
                        DiaPattern *pattern)
{
  DiaObjectChange *change;
  GPtrArray *props;
  PatternProperty *pp;
  Property *prop = object_prop_by_name_type (object, "pattern", PROP_TYPE_PATTERN);

  if (!prop) {
    return NULL;
  }

  pp = (PatternProperty *) prop;
  if (pp->pattern == pattern) {
    return dia_object_change_list_new ();
  }

  g_set_object (&pp->pattern, pattern);

  props = prop_list_from_single (prop);
  change = object_apply_props (object, props);
  prop_list_free (props);
  return change;
}


/**
 * dia_object_set_string:
 * @object: the object to modify
 * @name: the name of the string property (%NULL for any)
 * @value: the value to set, %NULL to delete
 *
 * Modify the objects string property
 *
 * Usually you should not pass %NULL for the name, the facility
 * was added for convenience of the unit test.
 *
 * Returns: #DiaObjectChange on success, %NULL if @name not found
 */
DiaObjectChange *
dia_object_set_string (DiaObject  *object,
                       const char *name,
                       const char *value)
{
  DiaObjectChange *change;
  GPtrArray *props = NULL;
  Property *prop = object_prop_by_name_type (object, name, PROP_TYPE_STRING);

  if (!prop) {
    prop = object_prop_by_name_type (object, name, PROP_TYPE_FILE);
  }

  if (prop) {
    StringProperty *pp = (StringProperty *)prop;
    g_clear_pointer (&pp->string_data, g_free);
    pp->string_data = g_strdup (value);
    props = prop_list_from_single (prop);
  } else if ((prop = object_prop_by_name_type (object, name, PROP_TYPE_TEXT)) != NULL) {
    TextProperty *pp = (TextProperty *)prop;
    g_clear_pointer (&pp->text_data, g_free);
    pp->text_data = g_strdup (value);
    props = prop_list_from_single (prop);
  }

  if (!props) {
    return NULL;
  }

  change = object_apply_props (object, props);
  prop_list_free (props);
  return change;
}