File: prop_pattern.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 (252 lines) | stat: -rw-r--r-- 7,476 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
/* 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) 2013 Hans Breuer
 * Property types for pattern.
 *
 * 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 <gtk/gtk.h>
#define WIDGET GtkWidget
#include "widgets.h"
#include "properties.h"
#include "propinternals.h"
#include "pattern.h"
#include "prop_pattern.h"
#include "diapatternselector.h"

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

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

  return prop;
}

static void
patternprop_free(PatternProperty *prop) 
{
  if (prop->pattern)
    g_object_unref (prop->pattern);
  g_free(prop);
} 

static PatternProperty *
patternprop_copy(PatternProperty *src) 
{
  PatternProperty *prop = 
    (PatternProperty *)src->common.ops->new_prop(src->common.descr,
                                              src->common.reason);
  if (src->pattern) /* TODO: rethink on edit - ...copy() ? */
    prop->pattern = g_object_ref (src->pattern);

  return prop;
}

DiaPattern *
data_pattern (DataNode node, DiaContext *ctx)
{
  AttributeNode attr;
  DiaPattern *pattern;
  DiaPatternType type = DIA_LINEAR_GRADIENT;
  guint flags = 0;
  Point p = {0.0, 0.0};
  real r;

  attr = composite_find_attribute(node, "gradient");
  if (attr)
    type = data_int (attribute_first_data(attr), ctx);
  attr = composite_find_attribute(node, "flags");
  if (attr)
    flags = data_int (attribute_first_data(attr), ctx);
  attr = composite_find_attribute(node, "p1");
  if (attr)
    data_point (attribute_first_data(attr), &p, ctx);
  pattern = dia_pattern_new (type, flags, p.x, p.y);
  if (pattern) {
    /* need to apply the radius first as it may limit p2 */
    attr = composite_find_attribute(node, "r");
    if (attr) {
      r = data_real (attribute_first_data(attr), ctx);
      dia_pattern_set_radius (pattern, r);
    }
    attr = composite_find_attribute(node, "p2");
    if (attr) {
      data_point (attribute_first_data(attr), &p, ctx);
      dia_pattern_set_point (pattern, p.x, p.y);
    }
    /* restore color-stops */
    attr =  composite_find_attribute(node, "data");
    if (attr) {
      DataNode data = attribute_first_data(attr);
      guint nvals = attribute_num_data(attr);
      guint i;
      real offset = 0.0;
      Color color = color_black;

      for (i=0; (i < nvals) && data; i++, data = data_next(data)) {
	attr = composite_find_attribute(data, "offset");
	if (attr)
	  offset = data_real (attribute_first_data(attr), ctx);
	attr = composite_find_attribute(data, "color");
	if (attr)
	  data_color (attribute_first_data(attr), &color, ctx);
	dia_pattern_add_color (pattern, offset, &color);
      }
    }
  } 
  return pattern;
}

static void 
patternprop_load(PatternProperty *prop, AttributeNode attr, DataNode data, DiaContext *ctx)
{
  prop->pattern = data_pattern (data, ctx);
}

typedef struct
{
  AttributeNode node;
  DiaContext   *ctx;
} StopUserData;

static gboolean
_data_add_stop (real ofs, const Color *col, gpointer user_data)
{
  StopUserData *ud = (StopUserData *)user_data;
  AttributeNode attr = ud->node;
  DiaContext *ctx = ud->ctx;
  ObjectNode composite = data_add_composite(attr, "color-stop", ctx);

  data_add_real (composite_add_attribute(composite, "offset"), ofs, ctx);
  data_add_color(composite_add_attribute(composite, "color"), col, ctx);

  return TRUE;
}

void
data_add_pattern (AttributeNode attr, DiaPattern *pattern, DiaContext *ctx)
{
  ObjectNode composite = data_add_composite(attr, "pattern", ctx);
  StopUserData ud;
  DiaPatternType type;
  guint flags;
  Point p1, p2;

  ud.node = composite_add_attribute (composite, "data");
  ud.ctx = ctx;

  dia_pattern_get_settings (pattern, &type, &flags);
  data_add_int (composite_add_attribute(composite, "gradient"), type, ctx);
  data_add_int (composite_add_attribute(composite, "flags"), flags, ctx);
  dia_pattern_get_points (pattern, &p1, &p2);
  data_add_point (composite_add_attribute(composite, "p1"), &p1, ctx);
  data_add_point (composite_add_attribute(composite, "p2"), &p2, ctx);
  if (type == DIA_RADIAL_GRADIENT) {
    real r;
    dia_pattern_get_radius (pattern, &r);
    data_add_real (composite_add_attribute(composite, "r"), r, ctx);
  }
  dia_pattern_foreach (pattern, _data_add_stop, &ud);
}

static void 
patternprop_save(PatternProperty *prop, AttributeNode attr, DiaContext *ctx) 
{
  if (prop->pattern) {
    data_add_pattern (attr, prop->pattern, ctx);
  }
}

static void 
patternprop_get_from_offset(PatternProperty *prop,
                         void *base, guint offset, guint offset2) 
{
  /* before we start editing a simple reference should be enough */
  DiaPattern *pattern = struct_member(base,offset,DiaPattern *);

  if (pattern)
    prop->pattern = g_object_ref (pattern);
  else
    prop->pattern = NULL;
}

static void 
patternprop_set_from_offset(PatternProperty *prop,
                           void *base, guint offset, guint offset2)
{
  DiaPattern *dest = struct_member(base,offset,DiaPattern *);
  if (dest)
    g_object_unref (dest);
  if (prop->pattern)
    struct_member(base,offset, DiaPattern *) = g_object_ref (prop->pattern);
  else
    struct_member(base,offset, DiaPattern *) = NULL;
}

static GtkWidget *
patternprop_get_widget (PatternProperty *prop, PropDialog *dialog) 
{ 
  GtkWidget *ret = dia_pattern_selector_new ();
  prophandler_connect(&prop->common, G_OBJECT(ret), "value_changed");
  return ret;
}

static void
patternprop_reset_widget(PatternProperty *prop, GtkWidget *widget)
{
  dia_pattern_selector_set_pattern (widget, prop->pattern);
}

static void
patternprop_set_from_widget(PatternProperty *prop, GtkWidget *widget) 
{
  DiaPattern *pat = dia_pattern_selector_get_pattern (widget);

  if (prop->pattern)
    g_object_unref (prop->pattern);
  prop->pattern = pat;
}

static const PropertyOps patternprop_ops = {
  (PropertyType_New) patternprop_new,
  (PropertyType_Free) patternprop_free,
  (PropertyType_Copy) patternprop_copy,
  (PropertyType_Load) patternprop_load,
  (PropertyType_Save) patternprop_save,
  (PropertyType_GetWidget) patternprop_get_widget,
  (PropertyType_ResetWidget) patternprop_reset_widget,
  (PropertyType_SetFromWidget) patternprop_set_from_widget,

  (PropertyType_CanMerge) noopprop_can_merge,
  (PropertyType_GetFromOffset) patternprop_get_from_offset,
  (PropertyType_SetFromOffset) patternprop_set_from_offset
};

void 
prop_patterntypes_register(void)
{
  prop_type_register(PROP_TYPE_PATTERN, &patternprop_ops);
}