File: dia-canvas-connection.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 (375 lines) | stat: -rw-r--r-- 11,184 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
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
/* dia-canvas-connection.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-connection.h"
//#include "dia-canvas.h"
#include "dia-canvas-group.h"

#define _(s) (s)

/* properties */
enum {
  PROP_NONE,
  PROP_X,
  PROP_Y,
  PROP_FILL_COLOR,
  PROP_TYPE,
  PROP_LAST
};

static void dia_canvas_connection_init       (DiaCanvasConnection *connection);
static void dia_canvas_connection_class_init (DiaCanvasConnectionClass *klass);
static void dia_canvas_connection_finalize   (GObject *object);

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

static void     dia_canvas_connection_real_render    (DiaCanvasItem *item,
						      DiaRenderer *renderer,
						      DiaRectangle *rectangle);
static void     dia_canvas_connection_real_update    (DiaCanvasItem *item);
static gdouble  dia_canvas_connection_real_distance  (DiaCanvasItem *item,
						      gdouble x, gdouble y);
static void     dia_canvas_connection_real_move      (DiaCanvasItem *item,
						      gdouble dx, gdouble dy);
static void    dia_canvas_connection_real_connect    (DiaCanvasItem *item,
						      DiaCanvasConnection *connection);
static void    dia_canvas_connection_real_connection (DiaCanvasItem *item);

static GObjectClass *parent_class = NULL;


GType
dia_canvas_connection_get_type (void)
{
  static GtkType canvas_connection_type = 0;

  if (!canvas_connection_type)
    {
      static const GTypeInfo canvas_connection_info =
      {
        sizeof (DiaCanvasConnectionClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) dia_canvas_connection_class_init,
        (GClassFinalizeFunc) NULL,
        NULL,
        
        sizeof (DiaCanvasConnection),
        0, /* n_preallocs */
        (GInstanceInitFunc) dia_canvas_connection_init,
      };

      canvas_connection_type = g_type_register_static (DIA_TYPE_CANVAS_ITEM,
						       "DiaCanvasConnection",
						       &canvas_connection_info,
						       0);
    }
  return canvas_connection_type;
}

static void
dia_canvas_connection_class_init (DiaCanvasConnectionClass *klass)
{
  GObjectClass *object_class;
  DiaCanvasItemClass *item_class;

  object_class = G_OBJECT_CLASS(klass);
  item_class = DIA_CANVAS_ITEM_CLASS(klass);
  parent_class = g_type_class_ref(DIA_TYPE_CANVAS_ITEM);

  object_class->set_property = dia_canvas_connection_set_property;
  object_class->get_property = dia_canvas_connection_get_property;
  object_class->finalize     = dia_canvas_connection_finalize;

  item_class->render           = dia_canvas_connection_real_render;
  item_class->update           = dia_canvas_connection_real_update;
  item_class->distance         = dia_canvas_connection_real_distance;
  item_class->move             = dia_canvas_connection_real_move;
  item_class->connect          = dia_canvas_connection_real_connect;
  item_class->connection       = dia_canvas_connection_real_connection;

  g_object_class_install_property (object_class,
				   PROP_X,
				   g_param_spec_double ("x", _("Position"),
							_("The position of the connection point"),
							-G_MAXDOUBLE,
							G_MAXDOUBLE,
							0.0,
							G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_Y,
				   g_param_spec_double ("y", _("Position"),
							_("The position of the connection point"),
							-G_MAXDOUBLE,
							G_MAXDOUBLE,
							0,
							G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_FILL_COLOR,
				   g_param_spec_boxed ("fill_color", _("Fill color"),
						       _("The fill color of the connection (or NULL for empty)"),
						       GDK_TYPE_COLOR,
						       G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_TYPE,
				   g_param_spec_int ("type", _("Type"),
						     _("Type of the connection point"),
						     -G_MAXINT,
						     G_MAXINT,
						     0,
						     G_PARAM_READWRITE));
}

static void
dia_canvas_connection_init (DiaCanvasConnection *connection)
{
  DiaCanvasItem *item = DIA_CANVAS_ITEM(connection);

  item->independant_move  = FALSE;
  connection->connections = NULL;
  connection->type        = UNKNOWN;
}

static void
dia_canvas_connection_finalize(GObject *object)
{
  DiaCanvasConnection *connection;
  GList *tmp, *next;

  g_return_if_fail(object != NULL);
  g_return_if_fail(DIA_IS_CANVAS_CONNECTION(object));

  connection = DIA_CANVAS_CONNECTION(object);

  tmp = connection->connections;
  while( tmp != NULL ) {
    next = tmp->next;
    ((DiaCanvasConnection*)tmp->data)->connections = g_list_remove(((DiaCanvasConnection*)tmp->data)->connections,connection);
    connection->connections = g_list_remove(connection->connections,((DiaCanvasConnection*)tmp->data));
    tmp = next;
  }

  if (parent_class->finalize)
    (* parent_class->finalize) (object);
}

static void
dia_canvas_connection_set_property (GObject *object, guint property_id,
				    const GValue *value, GParamSpec *pspec)
{
  DiaCanvasConnection *connection = DIA_CANVAS_CONNECTION(object);
  gdouble val;
  GdkColor *color;

  switch (property_id)
    {

    case PROP_X:
      val = g_value_get_double(value);
      if (val != connection->pos.x) {
	connection->pos.x = val;
	dia_canvas_item_request_update(DIA_CANVAS_ITEM(connection));
      }
      break;

    case PROP_Y:
      val = g_value_get_double(value);
      if (val != connection->pos.y) {
	connection->pos.y = val;
	dia_canvas_item_request_update(DIA_CANVAS_ITEM(connection));
      }
      break;

    case PROP_FILL_COLOR:
      color = g_value_get_boxed(value);
      connection->fill_color = *color;
      dia_canvas_item_request_update(DIA_CANVAS_ITEM(connection));
      break;

    case PROP_TYPE:
      connection->type = g_value_get_int(value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
dia_canvas_connection_get_property (GObject *object, guint property_id,
				    GValue *value, GParamSpec *pspec)
{
  DiaCanvasConnection *connection = DIA_CANVAS_CONNECTION(object);

  switch (property_id)
    {
    case PROP_X:
      g_value_set_double(value, connection->pos.x);
      break;
    case PROP_Y:
      g_value_set_double(value, connection->pos.y);
      break;
    case PROP_FILL_COLOR:
      g_value_set_boxed(value, &connection->fill_color);
      break;
    case PROP_TYPE:
      g_value_set_int(value, connection->type);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

/*****************************************************************************
 ******************************* S I G N A L S *******************************
 *****************************************************************************/

static void
dia_canvas_connection_real_render (DiaCanvasItem *item,
				   DiaRenderer *renderer,
				   DiaRectangle *rectangle)
{
  DiaCanvasConnection *connection = DIA_CANVAS_CONNECTION(item);
  DiaPoint p1,p2;

  p1.x = connection->pos.x - 5;
  p1.y = connection->pos.y - 5;
  p2.x = connection->pos.x + 5;
  p2.y = connection->pos.y + 5;
  dia_renderer_set_color(renderer, &connection->fill_color);
  dia_renderer_draw_rectangle(renderer, TRUE, &p1, &p2);
}

static void
dia_canvas_connection_real_update (DiaCanvasItem *item)
{
  DiaCanvasConnection *connection = DIA_CANVAS_CONNECTION(item);

  /* the connection may have been moved, so dirty its former location */
  dia_canvas_dirty_region(item->canvas, &item->bounds);

  item->bounds.left   = connection->pos.x - 5;
  item->bounds.top    = connection->pos.y - 5;
  item->bounds.right  = connection->pos.x + 5;
  item->bounds.bottom = connection->pos.y + 5;

  /* dirty the new location to redraw ... */
  dia_canvas_dirty_region(item->canvas, &item->bounds);
}

static gdouble
dia_canvas_connection_real_distance (DiaCanvasItem *item,
				     gdouble x,gdouble y)
{
  //DiaCanvasConnection *connection = DIA_CANVAS_CONNECTION(item);
  double dx = 0.0, dy = 0.0;

  /* not the exact distance, but close enough for our checks */
  if (x < item->bounds.left)
    dx = item->bounds.left - x;
  else if (x > item->bounds.right)
    dx = x - item->bounds.right;

  if (y < item->bounds.top)
    dy = item->bounds.top - y;
  else if (y > item->bounds.bottom)
    dy = y - item->bounds.bottom;

  return dx*dx + dy*dy;
}

static void
dia_canvas_connection_real_move (DiaCanvasItem *item,
				 gdouble dx, gdouble dy)
{
  static guint connection_moved_signal_id = 0;
  DiaCanvasConnection *connection = DIA_CANVAS_CONNECTION(item);
  GList *tmp;

  connection->pos.x += dx;
  connection->pos.y += dy;

  dia_canvas_item_request_update(DIA_CANVAS_ITEM(connection));

  if( !item->connection_moved ) {
    if (connection_moved_signal_id == 0)
      connection_moved_signal_id = gtk_signal_lookup("connection_moved", DIA_TYPE_CANVAS_ITEM);

    tmp = connection->connections;
    while( tmp != NULL ) {
      DiaCanvasConnection *save;

      save = tmp->data;
      tmp = tmp->next;
      DIA_CANVAS_ITEM(save)->connection_moved = TRUE;
      g_signal_emit(DIA_CANVAS_ITEM(save), connection_moved_signal_id, 0, connection, dx, dy);
      DIA_CANVAS_ITEM(save)->connection_moved = FALSE;
    }
  }
}

static void
dia_canvas_connection_real_connect (DiaCanvasItem *item,
				    DiaCanvasConnection *connection)
{
  DiaCanvasConnection *this;
  GList *tmp;

  g_return_if_fail( item != NULL );
  g_return_if_fail( connection != NULL );

  if( item == DIA_CANVAS_ITEM(connection) )
    return;

  this = DIA_CANVAS_CONNECTION(item);

  if( (this->pos.x == connection->pos.x) &&
      (this->pos.y == connection->pos.y) ) {

    /*
     * Check that the connection doesn't already exists.
     */
    tmp = this->connections;
    while( tmp != NULL ) {
      if( tmp->data == connection )
	return;
      tmp = tmp->next;
    }

    this->connections       = g_list_append(this->connections,connection);
    connection->connections = g_list_append(connection->connections,this);
  }
}

static void
dia_canvas_connection_real_connection (DiaCanvasItem *item)
{
  g_return_if_fail( item != NULL );

  dia_canvas_connect( item->canvas, DIA_CANVAS_CONNECTION(item) );
}