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
|
/* Dia -- an diagram creation/manipulation program
* Copyright (C) 1998 Alexander Larsson
*
* GRAFCET charts support for Dia
* Copyright (C) 2000 Cyrille Chepelov
*
* This file is derived from objects/standard/zigzag.c
*
* 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 "object.h"
#include "orth_conn.h"
#include "connectionpoint.h"
#include "diarenderer.h"
#include "attributes.h"
#include "properties.h"
#include "grafcet.h"
#include "pixmaps/vector.xpm"
#define ARC_LINE_WIDTH (GRAFCET_GENERAL_LINE_WIDTH)
#define ARC_ARROW_LENGTH .8
#define ARC_ARROW_WIDTH .6
#define ARC_ARROW_TYPE ARROW_FILLED_TRIANGLE
#define HANDLE_MIDDLE HANDLE_CUSTOM1
typedef struct _Arc {
OrthConn orth;
gboolean uparrow;
} Arc;
static DiaObjectChange *arc_move_handle (Arc *arc,
Handle *handle,
Point *to,
ConnectionPoint *cp,
HandleMoveReason reason,
ModifierKeys modifiers);
static DiaObjectChange *arc_move (Arc *arc,
Point *to);
static void arc_select(Arc *arc, Point *clicked_point,
DiaRenderer *interactive_renderer);
static void arc_draw(Arc *arc, DiaRenderer *renderer);
static DiaObject *arc_create(Point *startpoint,
void *user_data,
Handle **handle1,
Handle **handle2);
static real arc_distance_from(Arc *arc, Point *point);
static void arc_update_data(Arc *arc);
static void arc_destroy(Arc *arc);
static DiaMenu *arc_get_object_menu(Arc *arc,
Point *clickedpoint);
static DiaObject *arc_load(ObjectNode obj_node, int version,DiaContext *ctx);
static PropDescription *arc_describe_props(Arc *arc);
static void arc_get_props(Arc *arc,
GPtrArray *props);
static void arc_set_props(Arc *arc,
GPtrArray *props);
static ObjectTypeOps arc_type_ops =
{
(CreateFunc)arc_create, /* create */
(LoadFunc) arc_load,/* using_properties */ /* load */
(SaveFunc) object_save_using_properties,
(GetDefaultsFunc) NULL,
(ApplyDefaultsFunc) NULL,
};
DiaObjectType old_arc_type =
{
"GRAFCET - Vector", /* name */
/* Version 0 had no autorouting and so shouldn't have it set by default. */
1, /* version */
vector_xpm, /* pixmap */
&arc_type_ops /* ops */
};
DiaObjectType grafcet_arc_type =
{
"GRAFCET - Arc", /* name */
0, /* version */
vector_xpm, /* pixmap */
&arc_type_ops /* ops */
};
static ObjectOps arc_ops = {
(DestroyFunc) arc_destroy,
(DrawFunc) arc_draw,
(DistanceFunc) arc_distance_from,
(SelectFunc) arc_select,
(CopyFunc) object_copy_using_properties,
(MoveFunc) arc_move,
(MoveHandleFunc) arc_move_handle,
(GetPropertiesFunc) object_create_props_dialog,
(ApplyPropertiesDialogFunc) object_apply_props_from_dialog,
(ObjectMenuFunc) arc_get_object_menu,
(DescribePropsFunc) arc_describe_props,
(GetPropsFunc) arc_get_props,
(SetPropsFunc) arc_set_props,
(TextEditFunc) 0,
(ApplyPropertiesListFunc) object_apply_props,
};
static PropDescription arc_props[] = {
ORTHCONN_COMMON_PROPERTIES,
{ "uparrow",PROP_TYPE_BOOL,PROP_FLAG_VISIBLE,
N_("Draw arrow heads on upward arcs:"),NULL},
PROP_DESC_END
};
static PropDescription *
arc_describe_props(Arc *arc)
{
if (arc_props[0].quark == 0) {
prop_desc_list_calculate_quarks(arc_props);
}
return arc_props;
}
static PropOffset arc_offsets[] = {
ORTHCONN_COMMON_PROPERTIES_OFFSETS,
{ "uparrow",PROP_TYPE_BOOL,offsetof(Arc,uparrow)},
{ NULL,0,0 }
};
static void
arc_get_props(Arc *arc, GPtrArray *props)
{
object_get_props_from_offsets(&arc->orth.object,
arc_offsets,props);
}
static void
arc_set_props(Arc *arc, GPtrArray *props)
{
object_set_props_from_offsets(&arc->orth.object,
arc_offsets,props);
arc_update_data(arc);
}
static real
arc_distance_from(Arc *arc, Point *point)
{
OrthConn *orth = &arc->orth;
return orthconn_distance_from(orth, point, ARC_LINE_WIDTH);
}
static void
arc_select(Arc *arc, Point *clicked_point,
DiaRenderer *interactive_renderer)
{
orthconn_update_data(&arc->orth);
}
static DiaObjectChange *
arc_move_handle (Arc *arc,
Handle *handle,
Point *to,
ConnectionPoint *cp,
HandleMoveReason reason,
ModifierKeys modifiers)
{
DiaObjectChange *change;
change = orthconn_move_handle (&arc->orth, handle, to, cp, reason, modifiers);
arc_update_data (arc);
return change;
}
static DiaObjectChange *
arc_move (Arc *arc, Point *to)
{
DiaObjectChange *change;
change = orthconn_move (&arc->orth, to);
arc_update_data (arc);
return change;
}
static void
arc_draw (Arc *arc, DiaRenderer *renderer)
{
OrthConn *orth = &arc->orth;
Point *points;
int n,i;
Arrow arrow = { ARC_ARROW_TYPE, ARC_ARROW_LENGTH, ARC_ARROW_WIDTH };
points = &orth->points[0];
n = orth->numpoints;
dia_renderer_set_linewidth (renderer, ARC_LINE_WIDTH);
dia_renderer_set_linestyle (renderer, DIA_LINE_STYLE_SOLID, 0.0);
dia_renderer_set_linejoin (renderer, DIA_LINE_JOIN_MITER);
dia_renderer_set_linecaps (renderer, DIA_LINE_CAPS_BUTT);
dia_renderer_draw_polyline (renderer, points, n, &color_black);
if (arc->uparrow) {
for (i=0;i<n-1; i++) {
if ((points[i].y > points[i+1].y) &&
(ABS (points[i+1].y-points[i].y) > 5 * ARC_ARROW_LENGTH)) {
/* Draw an arrow on the middle of the line */
Point m;
m.x = points[i].x; /* == points[i+1].x */
m.y = .5 * (points[i].y + points[i+1].y) - (.5 * ARC_ARROW_LENGTH);
dia_arrow_draw (&arrow,
renderer,
&m,
&points[i],
ARC_LINE_WIDTH,
&color_black,
&color_white);
}
}
}
}
static DiaObject *
arc_create(Point *startpoint,
void *user_data,
Handle **handle1,
Handle **handle2)
{
Arc *arc;
OrthConn *orth;
DiaObject *obj;
arc = g_new0 (Arc, 1);
orth = &arc->orth;
obj = &orth->object;
obj->type = &grafcet_arc_type;
obj->ops = &arc_ops;
orthconn_init(orth, startpoint);
arc->uparrow = TRUE;
arc_update_data(arc);
*handle1 = orth->handles[0];
*handle2 = orth->handles[orth->numhandles-1];
return &arc->orth.object;
}
static void
arc_destroy(Arc *arc)
{
orthconn_destroy(&arc->orth);
}
static void
arc_update_data(Arc *arc)
{
OrthConn *orth = &arc->orth;
PolyBBExtras *extra = &orth->extra_spacing;
orthconn_update_data(&arc->orth);
extra->start_trans =
extra->start_long =
extra->end_long =
extra->end_trans = ARC_LINE_WIDTH/2.0;
if (arc->uparrow) {
extra->middle_trans = (ARC_LINE_WIDTH + ARC_ARROW_WIDTH)/2.0;
} else {
extra->middle_trans = ARC_LINE_WIDTH/2.0;
}
orthconn_update_boundingbox(orth);
}
static DiaObjectChange *
arc_add_segment_callback (DiaObject *obj, Point *clicked, gpointer data)
{
DiaObjectChange *change;
change = orthconn_add_segment ((OrthConn *) obj, clicked);
arc_update_data ((Arc *) obj);
return change;
}
static DiaObjectChange *
arc_delete_segment_callback (DiaObject *obj, Point *clicked, gpointer data)
{
DiaObjectChange *change;
change = orthconn_delete_segment ((OrthConn *) obj, clicked);
arc_update_data ((Arc *) obj);
return change;
}
static DiaMenuItem object_menu_items[] = {
{ N_("Add segment"), arc_add_segment_callback, NULL, 1 },
{ N_("Delete segment"), arc_delete_segment_callback, NULL, 1 },
ORTHCONN_COMMON_MENUS,
};
static DiaMenu object_menu = {
"Arc",
sizeof(object_menu_items)/sizeof(DiaMenuItem),
object_menu_items,
NULL
};
static DiaMenu *
arc_get_object_menu(Arc *arc, Point *clickedpoint)
{
OrthConn *orth;
orth = &arc->orth;
/* Set entries sensitive/selected etc here */
object_menu_items[0].active = orthconn_can_add_segment(orth, clickedpoint);
object_menu_items[1].active = orthconn_can_delete_segment(orth, clickedpoint);
orthconn_update_object_menu(orth, clickedpoint, &object_menu_items[2]);
return &object_menu;
}
static DiaObject *
arc_load(ObjectNode obj_node, int version,DiaContext *ctx)
{
DiaObject *obj = object_load_using_properties(&grafcet_arc_type,
obj_node,version,ctx);
if (version == 0) {
AttributeNode attr;
/* In old objects with no autorouting, set it to false. */
attr = object_find_attribute(obj_node, "autorouting");
if (attr == NULL)
((OrthConn*)obj)->autorouting = FALSE;
}
return obj;
}
|