File: dia-canvas-grid.c

package info (click to toggle)
dia-newcanvas 0.6.10-5.4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 1,900 kB
  • ctags: 1,102
  • sloc: ansic: 7,865; sh: 7,322; makefile: 254
file content (277 lines) | stat: -rw-r--r-- 7,607 bytes parent folder | download
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
/* dia-canvas-grid.c
 * Copyright (C) 2001  Xavier Ordoquy
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "dia-canvas-grid.h"
#include "dia-marshal.h"
#include <math.h>
#include <string.h>

#define _(s) (s)

/* properties */
enum {
  PROP_NONE,
  PROP_SPACING,
  PROP_COLOR,
  PROP_SNAPPING,
  PROP_LAST
};

static void dia_canvas_grid_init           (DiaCanvasGrid *rect);
static void dia_canvas_grid_class_init     (DiaCanvasGridClass *klass);

static void   dia_canvas_grid_set_property (GObject *object,
					    guint property_id,
					    const GValue *value,
					    GParamSpec *pspec);
static void   dia_canvas_grid_get_property (GObject *object,
					    guint property_id,
					    GValue *value,
					    GParamSpec *pspec);

static void   dia_canvas_grid_real_render  (DiaCanvasItem *item,
					    DiaRenderer *renderer,
					    DiaRectangle *rect);
static gdouble dia_canvas_grid_real_attract  (DiaCanvasItem *item,
					      gdouble *x, gdouble *y);
static void    dia_canvas_grid_real_connection_moved (DiaCanvasItem *item,
 						      DiaCanvasConnection *connection,
 						      gdouble dx, gdouble dy);


static DiaCanvasItemClass *grand_parent_class = NULL;
static DiaCanvasGroupClass *parent_class = NULL;

GType
dia_canvas_grid_get_type (void)
{
  static GtkType canvas_grid_type = 0;

  if (!canvas_grid_type)
    {
      static const GTypeInfo canvas_grid_info =
      {
        sizeof (DiaCanvasGridClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) dia_canvas_grid_class_init,
        (GClassFinalizeFunc) NULL,
        NULL,
        
        sizeof (DiaCanvasGrid),
        0, /* n_preallocs */
        (GInstanceInitFunc) dia_canvas_grid_init,
      };

      canvas_grid_type = g_type_register_static (DIA_TYPE_CANVAS_GROUP,
						 "DiaCanvasGrid",
						 &canvas_grid_info,
						 0);
    }
  return canvas_grid_type;
}

static void
dia_canvas_grid_class_init (DiaCanvasGridClass *klass)
{
  GObjectClass *object_class;
  DiaCanvasItemClass *item_class;

  object_class = G_OBJECT_CLASS(klass);
  item_class = DIA_CANVAS_ITEM_CLASS(klass);

  grand_parent_class = g_type_class_ref(DIA_TYPE_CANVAS_ITEM);
  parent_class = g_type_class_ref(DIA_TYPE_CANVAS_GROUP);

  object_class->set_property = dia_canvas_grid_set_property;
  object_class->get_property = dia_canvas_grid_get_property;

  item_class->render           = dia_canvas_grid_real_render;
  item_class->attract          = dia_canvas_grid_real_attract;
  item_class->connection_moved = dia_canvas_grid_real_connection_moved;

  g_object_class_install_property (object_class,
				   PROP_SPACING,
				   g_param_spec_double ("spacing", _("Grid spacing"),
							_("The grid spacing between dots"),
							-G_MAXDOUBLE,
							G_MAXDOUBLE,
							0.0,
							G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_COLOR,
				   g_param_spec_boxed ("color", _("Drawing color"),
						       _("The color used to draw the dot"),
						       GDK_TYPE_COLOR,
						       G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_SNAPPING,
				   g_param_spec_boolean ("snapping", _("Snapping on/off"),
							 _("Turn the snapping mode on or off"),
							 TRUE,
							 G_PARAM_READWRITE));
}

static void
dia_canvas_grid_init (DiaCanvasGrid *rect)
{
  GdkColor color = { 0, 0x0000, 0x0000, 0x00000 };

  memcpy(&(rect->color),&color,sizeof(GdkColor));
  rect->spacing = 50;
  rect->snapping = TRUE;
}

static void
dia_canvas_grid_set_property (GObject *object, guint property_id,
				   const GValue *value, GParamSpec *pspec)
{
  DiaCanvasGrid *rect = DIA_CANVAS_GRID(object);
  gdouble val;
  //GdkColor *color;

  switch (property_id)
    {
    case PROP_SPACING:
      val = g_value_get_double(value);
      if (val != rect->spacing) {
	rect->spacing = val;
	dia_canvas_item_request_update(DIA_CANVAS_ITEM(rect));
      }
      break;
    case PROP_COLOR:
      g_warning("dia_canvas_grid_set_property: implement PROP_COLOR");
      break;
    case PROP_SNAPPING:
      val = g_value_get_boolean(value);
      rect->snapping = val;
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
dia_canvas_grid_get_property (GObject *object, guint property_id,
				   GValue *value, GParamSpec *pspec)
{
  DiaCanvasGrid *rect = DIA_CANVAS_GRID(object);

  switch (property_id)
    {
    case PROP_SPACING:
      g_value_set_double(value, rect->spacing);
      break;
    case PROP_COLOR:
      g_warning("dia_canvas_grid_get_property: PROP_COLOR");
      break;
    case PROP_SNAPPING:
      g_value_set_boolean(value, rect->snapping);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
dia_canvas_grid_real_render (DiaCanvasItem *item, DiaRenderer *renderer,
			     DiaRectangle *rectangle)
{
  gint i, j, x, y;
  DiaCanvasGrid *grid = DIA_CANVAS_GRID(item);
  DiaPoint pt;

  if (rectangle == NULL) {
    g_warning("RECTANGLE NULL");
    return;
  }

  dia_renderer_set_color(renderer, &(grid->color));
  dia_renderer_set_line_width(renderer, 1);
  x = floor(rectangle->left/grid->spacing);
  y = floor(rectangle->top/grid->spacing);

  for( i=x; (i*grid->spacing)<rectangle->right; i++ ) {
    pt.x = i*grid->spacing;
    for( j=y; (j*grid->spacing)<rectangle->bottom; j++ ) {
      pt.y = j*grid->spacing;
      dia_renderer_draw_point(renderer,&pt);
    }
  }
  parent_class->parent_class.render(item,renderer,rectangle);
}

static gdouble
dia_canvas_grid_real_attract(DiaCanvasItem *item,
			     gdouble *x, gdouble *y)
{
  gdouble ddx,ddy;
  DiaCanvasGrid *grid = DIA_CANVAS_GRID(item);
  GList *tmp, *best;
  gdouble x2, y2;
  gdouble signaled, result;
  static guint event_signal_id = 0;
  DiaCanvasGroup *group = DIA_CANVAS_GROUP(item);

  if (event_signal_id == 0)
    event_signal_id = gtk_signal_lookup("attract", DIA_TYPE_CANVAS_ITEM);

  result = G_MAXDOUBLE;
  best = NULL;

  for (tmp = group->children; tmp != NULL; tmp = tmp->next) {
    DiaCanvasItem *child = tmp->data;
    x2 = *x;
    y2 = *y;
    g_signal_emit(child, event_signal_id, 0, &x2, &y2, &signaled);
    if( result >= signaled ) {
      best = tmp;
      result = signaled;
    }
  }

  if( grid->snapping ) {
    ddx = rint((*x)/grid->spacing)*grid->spacing;
    ddy = rint((*y)/grid->spacing)*grid->spacing;
    ddx = ddx - *x;
    ddy = ddy - *y;
  } else {
    ddx = 0;
    ddy = 0;
  }
  signaled = ((ddx)*(ddx)+(ddy)*(ddy));

  if( (result < 400) && !(signaled < result) ) {
    g_signal_emit(best->data, event_signal_id, 0, x, y, &signaled);
  } else {
    *x += ddx;
    *y += ddy;
  }

  return signaled;
}

static void
dia_canvas_grid_real_connection_moved (DiaCanvasItem *item,
				       DiaCanvasConnection *connection,
				       gdouble dx, gdouble dy)
{
}