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
|
/* Dia -- an diagram creation/manipulation program
*
* Wan Link netwrok object
* Code based on wanlink.c and bus.c
*
* Copyright (C) 1998 Alexander Larsson
* Copyright (C) 2001 Hubert Figuiere
*
* 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 <math.h>
#include "connection.h"
#include "diarenderer.h"
#include "attributes.h"
#include "properties.h"
#include "network.h"
#ifndef M_PI_2
#define M_PI_2 1.57079632679489661923
#endif
#include "pixmaps/wanlink.xpm"
#define WANLINK_POLY_LEN 6
typedef struct _WanLink {
Connection connection;
Color line_color;
Color fill_color;
real width;
Point poly[WANLINK_POLY_LEN];
} WanLink;
static DiaObject *wanlink_create(Point *startpoint,
void *user_data,
Handle **handle1,
Handle **handle2);
static void wanlink_destroy(WanLink *wanlink);
static void wanlink_draw (WanLink *wanlink, DiaRenderer *renderer);
static real wanlink_distance_from(WanLink *wanlink, Point *point);
static void wanlink_select(WanLink *wanlink, Point *clicked_point,
DiaRenderer *interactive_renderer);
static DiaObject *wanlink_copy(WanLink *wanlink);
static DiaObjectChange* wanlink_move(WanLink *wanlink, Point *to);
static DiaObjectChange* wanlink_move_handle(WanLink *wanlink, Handle *handle,
Point *to, ConnectionPoint *cp,
HandleMoveReason reason,
ModifierKeys modifiers);
static PropDescription *wanlink_describe_props(WanLink *wanlink);
static void wanlink_get_props(WanLink *wanlink, GPtrArray *props);
static void wanlink_set_props(WanLink *wanlink, GPtrArray *props);
static void wanlink_save(WanLink *wanlink, ObjectNode obj_node,
DiaContext *ctx);
static DiaObject *wanlink_load(ObjectNode obj_node, int version, DiaContext *ctx);
static void wanlink_update_data(WanLink *wanlink);
static ObjectTypeOps wanlink_type_ops =
{
(CreateFunc) wanlink_create,
(LoadFunc) wanlink_load,
(SaveFunc) wanlink_save,
(GetDefaultsFunc) NULL,
(ApplyDefaultsFunc) NULL
};
static ObjectOps wanlink_ops =
{
(DestroyFunc) wanlink_destroy,
(DrawFunc) wanlink_draw,
(DistanceFunc) wanlink_distance_from,
(SelectFunc) wanlink_select,
(CopyFunc) wanlink_copy,
(MoveFunc) wanlink_move,
(MoveHandleFunc) wanlink_move_handle,
(GetPropertiesFunc) object_create_props_dialog,
(ApplyPropertiesDialogFunc) object_apply_props_from_dialog,
(ObjectMenuFunc) NULL,
(DescribePropsFunc) wanlink_describe_props,
(GetPropsFunc) wanlink_get_props,
(SetPropsFunc) wanlink_set_props,
(TextEditFunc) 0,
(ApplyPropertiesListFunc) object_apply_props,
};
DiaObjectType wanlink_type =
{
"Network - WAN Link", /* name */
1, /* version */
(const char **) wanlink_xpm, /* pixmap */
&wanlink_type_ops /* ops */
};
static PropDescription wanlink_props[] = {
CONNECTION_COMMON_PROPERTIES,
{ "width", PROP_TYPE_REAL, PROP_FLAG_VISIBLE,
N_("Width"), NULL, NULL },
PROP_STD_LINE_COLOUR,
PROP_STD_FILL_COLOUR,
PROP_DESC_END
};
static PropDescription *
wanlink_describe_props(WanLink *wanlink)
{
if (wanlink_props[0].quark == 0)
prop_desc_list_calculate_quarks(wanlink_props);
return wanlink_props;
}
static PropOffset wanlink_offsets[] = {
CONNECTION_COMMON_PROPERTIES_OFFSETS,
{ "width", PROP_TYPE_REAL, offsetof(WanLink, width) },
{ "line_colour", PROP_TYPE_COLOUR, offsetof(WanLink, line_color) },
{ "fill_colour", PROP_TYPE_COLOUR, offsetof(WanLink, fill_color) },
{ NULL, 0, 0 }
};
static void
wanlink_get_props(WanLink *wanlink, GPtrArray *props)
{
object_get_props_from_offsets(&wanlink->connection.object, wanlink_offsets,
props);
}
static void
wanlink_set_props(WanLink *wanlink, GPtrArray *props)
{
object_set_props_from_offsets(&wanlink->connection.object, wanlink_offsets,
props);
wanlink_update_data(wanlink);
}
#define FLASH_LINE NETWORK_GENERAL_LINEWIDTH
#define FLASH_WIDTH 1.0
#define FLASH_HEIGHT 11.0
#define FLASH_BORDER 0.325
#define FLASH_BOTTOM (FLASH_HEIGHT)
static DiaObject *
wanlink_create(Point *startpoint,
void *user_data,
Handle **handle1,
Handle **handle2)
{
WanLink *wanlink;
Connection *conn;
DiaObject *obj;
int i;
Point defaultpoly = {0.0, 0.0};
Point defaultlen = { 5.0, 0.0 };
wanlink = g_new0 (WanLink, 1);
conn = &wanlink->connection;
conn->endpoints[0] = *startpoint;
conn->endpoints[1] = *startpoint;
point_add(&conn->endpoints[1], &defaultlen);
obj = (DiaObject *) wanlink;
obj->type = &wanlink_type;
obj->ops = &wanlink_ops;
connection_init(conn, 2, 0);
for (i = 0; i < WANLINK_POLY_LEN ; i++)
wanlink->poly[i] = defaultpoly;
wanlink->width = FLASH_WIDTH;
/* both colors where black at the time this was hardcoded ... */
wanlink->line_color = color_black;
wanlink->fill_color = color_black;
wanlink->line_color = attributes_get_foreground();
wanlink->fill_color = attributes_get_foreground();
wanlink_update_data(wanlink);
*handle1 = obj->handles[0];
*handle2 = obj->handles[1];
return (DiaObject *)wanlink;
}
static void
wanlink_destroy(WanLink *wanlink)
{
connection_destroy(&wanlink->connection);
}
static void
wanlink_draw (WanLink *wanlink, DiaRenderer *renderer)
{
g_return_if_fail (wanlink != NULL);
g_return_if_fail (renderer != NULL);
dia_renderer_set_linewidth (renderer, FLASH_LINE);
dia_renderer_set_linejoin (renderer, DIA_LINE_JOIN_MITER);
dia_renderer_set_linestyle (renderer, DIA_LINE_STYLE_SOLID, 0.0);
dia_renderer_draw_polygon (renderer,
wanlink->poly,
WANLINK_POLY_LEN,
&wanlink->fill_color,
&wanlink->line_color);
}
static real
wanlink_distance_from(WanLink *wanlink, Point *point)
{
return distance_polygon_point (wanlink->poly, WANLINK_POLY_LEN, wanlink->width, point);
}
static void
wanlink_select(WanLink *wanlink, Point *clicked_point,
DiaRenderer *interactive_renderer)
{
connection_update_handles(&wanlink->connection);
}
static DiaObject *
wanlink_copy(WanLink *wanlink)
{
WanLink *newwanlink;
Connection *conn, *newconn;
conn = &wanlink->connection;
newwanlink = g_new0 (WanLink, 1);
newconn = &newwanlink->connection;
connection_copy(conn, newconn);
newwanlink->width = wanlink->width;
newwanlink->line_color = wanlink->line_color;
newwanlink->fill_color = wanlink->fill_color;
return (DiaObject *)newwanlink;
}
static DiaObjectChange*
wanlink_move(WanLink *wanlink, Point *to)
{
Point delta;
Point *endpoints = &wanlink->connection.endpoints[0];
DiaObject *obj = (DiaObject *) wanlink;
int i;
delta = *to;
point_sub(&delta, &obj->position);
for (i=0;i<2;i++) {
point_add(&endpoints[i], &delta);
}
wanlink_update_data(wanlink);
return NULL;
}
static DiaObjectChange*
wanlink_move_handle(WanLink *wanlink, Handle *handle,
Point *to, ConnectionPoint *cp,
HandleMoveReason reason, ModifierKeys modifiers)
{
connection_move_handle(&wanlink->connection, handle->id, to, cp,
reason, modifiers);
connection_adjust_for_autogap(&wanlink->connection);
wanlink_update_data(wanlink);
return NULL;
}
static void
wanlink_save(WanLink *wanlink, ObjectNode obj_node,
DiaContext *ctx)
{
AttributeNode attr;
connection_save((Connection *)wanlink, obj_node, ctx);
attr = new_attribute(obj_node, "width");
data_add_real(attr, wanlink->width, ctx);
data_add_color( new_attribute(obj_node, "line_color"), &wanlink->line_color, ctx);
data_add_color( new_attribute(obj_node, "fill_color"), &wanlink->fill_color, ctx);
}
static DiaObject *
wanlink_load(ObjectNode obj_node, int version,DiaContext *ctx)
{
WanLink *wanlink;
Connection *conn;
DiaObject *obj;
AttributeNode attr;
DataNode data;
wanlink = g_new0 (WanLink, 1);
conn = &wanlink->connection;
obj = (DiaObject *) wanlink;
obj->type = &wanlink_type;
obj->ops = &wanlink_ops;
connection_load(conn, obj_node, ctx);
connection_init(conn, 2, 0);
attr = object_find_attribute(obj_node, "width");
if (attr != NULL) {
data = attribute_first_data(attr);
wanlink->width = data_real(data, ctx);
}
wanlink->line_color = color_black;
attr = object_find_attribute(obj_node, "line_color");
if (attr != NULL)
data_color(attribute_first_data(attr), &wanlink->line_color, ctx);
/* both colors where black at the time this was hardcoded ... */
wanlink->fill_color = color_black;
attr = object_find_attribute(obj_node, "fill_color");
if (attr != NULL)
data_color(attribute_first_data(attr), &wanlink->fill_color, ctx);
wanlink_update_data (wanlink);
return obj;
}
typedef real Vector[3];
typedef Vector Matrix[3];
static void
_transform_point (Matrix m, Point *src, Point *dest)
{
real xx, yy, ww;
xx = m[0][0] * src->x + m[0][1] * src->y + m[0][2];
yy = m[1][0] * src->x + m[1][1] * src->y + m[1][2];
ww = m[2][0] * src->x + m[2][1] * src->y + m[2][2];
if (!ww)
ww = 1.0;
dest->x = xx / ww;
dest->y = yy / ww;
}
static void
_identity_matrix (Matrix m)
{
int i, j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
m[i][j] = (i == j) ? 1 : 0;
}
static void
_mult_matrix (Matrix m1, Matrix m2)
{
Matrix result;
int i, j, k;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
result [i][j] = 0.0;
for (k = 0; k < 3; k++)
result [i][j] += m1 [i][k] * m2[k][j];
}
/* copy the result into matrix 2 */
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
m2 [i][j] = result [i][j];
}
static void
_rotate_matrix (Matrix m, real theta)
{
Matrix rotate;
real cos_theta, sin_theta;
cos_theta = cos (theta);
sin_theta = sin (theta);
_identity_matrix (rotate);
rotate[0][0] = cos_theta;
rotate[0][1] = -sin_theta;
rotate[1][0] = sin_theta;
rotate[1][1] = cos_theta;
_mult_matrix (rotate, m);
}
static void
wanlink_update_data(WanLink *wanlink)
{
Connection *conn = &wanlink->connection;
DiaObject *obj = (DiaObject *) wanlink;
Point v, vhat;
Point *endpoints;
real width, width_2;
real len, angle;
Point origin;
int i;
Matrix m;
width = wanlink->width;
width_2 = width / 2.0;
if (connpoint_is_autogap(conn->endpoint_handles[0].connected_to) ||
connpoint_is_autogap(conn->endpoint_handles[1].connected_to)) {
connection_adjust_for_autogap(conn);
}
endpoints = &conn->endpoints[0];
obj->position = endpoints[0];
v = endpoints[1];
point_sub(&v, &endpoints[0]);
if ((fabs(v.x) == 0.0) && (fabs(v.y)==0.0)) {
v.x += 0.01;
}
vhat = v;
point_normalize(&vhat);
connection_update_boundingbox(conn);
/** compute the polygon **/
origin = wanlink->connection.endpoints [0];
len = point_len (&v);
angle = atan2 (vhat.y, vhat.x) - M_PI_2;
/* The case of the wanlink */
wanlink->poly[0].x = (width * 0.50) - width_2;
wanlink->poly[0].y = (len * 0.00);
wanlink->poly[1].x = (width * 0.50) - width_2;
wanlink->poly[1].y = (len * 0.45);
wanlink->poly[2].x = (width * 0.94) - width_2;
wanlink->poly[2].y = (len * 0.45);
wanlink->poly[3].x = (width * 0.50) - width_2;
wanlink->poly[3].y = (len * 1.00);
wanlink->poly[4].x = (width * 0.50) - width_2;
wanlink->poly[4].y = (len * 0.55);
wanlink->poly[5].x = (width * 0.06) - width_2;
wanlink->poly[5].y = (len * 0.55);
/* rotate */
_identity_matrix (m);
_rotate_matrix (m, angle);
obj->bounding_box.top = origin.y;
obj->bounding_box.left = origin.x;
obj->bounding_box.bottom = conn->endpoints[1].y;
obj->bounding_box.right = conn->endpoints[1].x;
for (i = 0; i < WANLINK_POLY_LEN; i++)
{
Point new_pt;
_transform_point (m, &wanlink->poly[i],
&new_pt);
point_add (&new_pt, &origin);
wanlink->poly[i] = new_pt;
}
/* calculate bounding box */
{
PolyBBExtras bbex = {0, 0, wanlink->width/2, 0, 0 };
polyline_bbox (&wanlink->poly[0], WANLINK_POLY_LEN, &bbex, TRUE, &obj->bounding_box);
}
connection_update_handles(conn);
}
|