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 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
|
/* Dia -- an diagram creation/manipulation program
* Copyright (C) 1998 Alexander Larsson
*
* 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.
*
* File: compound.c
*
* Purpose: This file contains implementation of the database "compound"
* code. It is a 'spider' like object with one connection point
* and many arms going from the connnection point.
*/
#include "config.h"
#include <glib/gi18n-lib.h>
#include <gtk/gtk.h> /* because of GtkWidgket */
#include "pixmaps/compound.xpm"
#include "object.h"
#include "connectionpoint.h"
#include "diarenderer.h"
#include "attributes.h"
#include "geometry.h"
#include "propinternals.h"
#include "debug.h"
#include "database.h"
/* ------------------------------------------------------------------------ */
#define DEFAULT_NUMARMS 2 /* must be >= 2 */
#define HANDLE_MOUNT_POINT (HANDLE_CUSTOM1)
#define HANDLE_ARM (HANDLE_CUSTOM2)
#define DEFAULT_ARM_X_DISTANCE 0.5
#define DEFAULT_ARM_Y_DISTANCE 0.5
#define DEBUG_DRAW_MP_DIRECTION 0
#define DEBUG_DRAW_BOUNDING_BOX 0
/* ------------------------------------------------------------------------ */
typedef struct _Compound Compound;
typedef struct _CompoundState CompoundState;
typedef struct _ArmHandleState ArmHandleState;
typedef struct _CompoundChange CompoundChange;
typedef struct _MountPointMoveChange MountPointMoveChange;
struct _Compound {
DiaObject object; /**< inheritance */
/* there is only one connection point to this object */
ConnectionPoint mount_point;
/* the first handle is the handle at the mount_point */
Handle *handles; /* array of size is `num_arms + 1' */
gint num_arms;
real line_width;
Color line_color;
};
struct _CompoundState {
ArmHandleState * handle_states;
gint num_handles; /* num_arms = num_handles-1 */
real line_width;
Color line_color;
};
struct _ArmHandleState {
/* id, type, and connect_type are static for the arms of a
Compound. there is no need to back them up */
Point pos;
ConnectionPoint * connected_to;
};
struct _DiaDBCompoundObjectChange {
DiaObjectChange obj_change;
Compound *obj;
CompoundState *saved_state;
};
DIA_DEFINE_OBJECT_CHANGE (DiaDBCompoundObjectChange,
dia_db_compound_object_change)
struct _DiaDBCompoundMountObjectChange {
DiaObjectChange obj_change;
Compound *obj;
Point saved_pos;
};
DIA_DEFINE_OBJECT_CHANGE (DiaDBCompoundMountObjectChange,
dia_db_compound_mount_object_change)
/* ------------------------------------------------------------------------ */
static CompoundState * compound_state_new (Compound *);
static void compound_state_free (CompoundState *);
static void compound_state_set (CompoundState *, Compound *);
static DiaObjectChange *compound_change_new (Compound *,
CompoundState *);
static DiaObjectChange *mount_point_move_change_new (Compound *,
Point *);
static DiaObject * compound_create (Point *, void *, Handle **, Handle **);
static DiaObject * compound_load (ObjectNode obj_node, int version,DiaContext *ctx);
static void compound_save (Compound *, ObjectNode, DiaContext *ctx);
static void compound_destroy (Compound *);
static void compound_draw (Compound *, DiaRenderer *);
static real compound_distance_from (Compound *, Point *);
static void compound_select (Compound *, Point *, DiaRenderer *);
static DiaObject * compound_copy (Compound *);
static DiaObjectChange *compound_move (Compound *,
Point *);
static DiaObjectChange *compound_move_handle (Compound *,
Handle *,
Point *,
ConnectionPoint *,
HandleMoveReason,
ModifierKeys);
static DiaObjectChange *compound_apply_properties_dialog (Compound *,
GtkWidget *);
static PropDescription *compound_describe_props (Compound *);
static void compound_get_props (Compound *,
GPtrArray *);
static void compound_set_props (Compound *,
GPtrArray *);
static void compound_update_object (Compound *);
static DiaObjectChange *compound_flip_arms_cb (DiaObject *,
Point *,
gpointer);
static DiaObjectChange *compound_repos_mount_point_cb (DiaObject *,
Point *,
gpointer);
static DiaMenu * compound_object_menu(DiaObject *, Point *);
static gint adjust_handle_count_to (Compound *, gint);
static void setup_mount_point (ConnectionPoint *, DiaObject *, Point *);
static void setup_handle (Handle *, HandleId, HandleType, HandleConnectType);
static void compound_sanity_check (Compound *, gchar *);
static void update_mount_point_directions (Compound *);
static void compound_apply_props (Compound *, GPtrArray *, gboolean);
static void compound_update_data (Compound *);
/* these two are responsible for setting the handles positions */
static void init_positions_for_handles_beginning_at_index (Compound *, gint);
static void init_default_handle_positions (Compound *c);
/* ------------------------------------------------------------------------ */
static ObjectTypeOps compound_type_ops =
{
(CreateFunc) compound_create,
(LoadFunc) compound_load,
(SaveFunc) compound_save
};
DiaObjectType compound_type =
{
"Database - Compound", /* name */
0, /* version */
compound_xpm, /* pixmap */
&compound_type_ops /* operations */
};
static ObjectOps compound_ops =
{
(DestroyFunc) compound_destroy,
(DrawFunc) compound_draw,
(DistanceFunc) compound_distance_from,
(SelectFunc) compound_select,
(CopyFunc) compound_copy,
(MoveFunc) compound_move,
(MoveHandleFunc) compound_move_handle,
(GetPropertiesFunc) object_create_props_dialog,
(ApplyPropertiesDialogFunc) compound_apply_properties_dialog,
(ObjectMenuFunc) compound_object_menu,
(DescribePropsFunc) compound_describe_props,
(GetPropsFunc) compound_get_props,
(SetPropsFunc) compound_set_props,
(TextEditFunc) 0,
(ApplyPropertiesListFunc) object_apply_props,
};
/* actually, there is no upper limit - used 10 just for fun */
static PropNumData compound_num_arms_prop_extra_data = {2.0, 10.0, 1.0};
static PropDescription compound_props[] =
{
OBJECT_COMMON_PROPERTIES,
PROP_STD_LINE_COLOUR_OPTIONAL,
PROP_STD_LINE_WIDTH_OPTIONAL,
{ "num_arms", PROP_TYPE_INT, PROP_FLAG_VISIBLE,
N_("Number of arms"), NULL, &compound_num_arms_prop_extra_data},
PROP_DESC_END
};
static PropOffset compound_offsets[] =
{
OBJECT_COMMON_PROPERTIES_OFFSETS,
{ "line_colour", PROP_TYPE_COLOUR, offsetof(Compound, line_color) },
{ PROP_STDNAME_LINE_WIDTH, PROP_STDTYPE_LINE_WIDTH, offsetof(Compound, line_width) },
{ "num_arms", PROP_TYPE_INT, offsetof(Compound, num_arms) },
{ NULL, 0, 0 }
};
#define CENTER_BOTH 1
#define CENTER_VERTICAL 2
#define CENTER_HORIZONTAL 3
#define FLIP_VERTICAL 1
#define FLIP_HORIZONTAL 2
static DiaMenuItem compound_menu_items[] = {
{ N_("Flip arms vertically"), compound_flip_arms_cb,
GINT_TO_POINTER(1), DIAMENU_ACTIVE },
{ N_("Flip arms horizontally"), compound_flip_arms_cb,
GINT_TO_POINTER(2), DIAMENU_ACTIVE },
{ N_("Center mount point vertically"), compound_repos_mount_point_cb,
GINT_TO_POINTER (CENTER_VERTICAL), DIAMENU_ACTIVE },
{ N_("Center mount point horizontally"), compound_repos_mount_point_cb,
GINT_TO_POINTER (CENTER_HORIZONTAL), DIAMENU_ACTIVE },
{ N_("Center mount point"), compound_repos_mount_point_cb,
GINT_TO_POINTER (CENTER_BOTH), DIAMENU_ACTIVE }
};
static DiaMenu compound_menu = {
N_("Compound"),
sizeof (compound_menu_items) / sizeof (DiaMenuItem),
compound_menu_items,
NULL
};
/* ------------------------------------------------------------------------ */
static CompoundState *
compound_state_new (Compound * c)
{
CompoundState * state;
DiaObject * obj;
gint i, num_handles;
state = g_new0 (CompoundState, 1);
obj = &c->object;
num_handles = c->object.num_handles;
state->num_handles = num_handles;
state->line_width = c->line_width;
state->line_color = c->line_color;
state->handle_states = g_new (ArmHandleState, num_handles);
for (i = 0; i < num_handles; i++)
{
state->handle_states[i].pos = obj->handles[i]->pos;
state->handle_states[i].connected_to = obj->handles[i]->connected_to;
}
return state;
}
static void
compound_state_set (CompoundState * state, Compound * comp)
{
gint i, num_handles;
Handle * h;
DiaObject * obj;
ArmHandleState * ahs;
obj = &comp->object;
comp->line_width = state->line_width;
comp->line_color = state->line_color;
adjust_handle_count_to (comp, state->num_handles);
num_handles = comp->object.num_handles;
for (i = 0; i < num_handles; i++)
{
h = &comp->handles[i];
ahs = &state->handle_states[i];
h->pos = ahs->pos;
if (h->connected_to != ahs->connected_to)
{
if (h->connected_to)
object_unconnect (obj, h);
if (ahs->connected_to)
object_connect (obj, h, ahs->connected_to);
}
}
comp->mount_point.pos = comp->handles[0].pos;
compound_update_data (comp);
compound_sanity_check (comp, "Restored state");
}
static void
compound_state_free (CompoundState * state)
{
g_clear_pointer (&state->handle_states, g_free);
g_free (state);
}
static void
compound_change_apply (DiaDBCompoundObjectChange *change, DiaObject *obj)
{
CompoundState *old_state;
old_state = compound_state_new (change->obj);
compound_state_set (change->saved_state, change->obj);
compound_state_free (change->saved_state);
change->saved_state = old_state;
}
static void
dia_db_compound_object_change_apply (DiaObjectChange *self, DiaObject *obj)
{
compound_change_apply (DIA_DB_COMPOUND_OBJECT_CHANGE (self), obj);
}
static void
dia_db_compound_object_change_revert (DiaObjectChange *self, DiaObject *obj)
{
compound_change_apply (DIA_DB_COMPOUND_OBJECT_CHANGE (self), obj);
}
static void
dia_db_compound_object_change_free (DiaObjectChange *self)
{
DiaDBCompoundObjectChange *change = DIA_DB_COMPOUND_OBJECT_CHANGE (self);
compound_state_free (change->saved_state);
}
static DiaObjectChange *
compound_change_new (Compound *comp, CompoundState *state)
{
DiaDBCompoundObjectChange *change;
change = dia_object_change_new (DIA_DB_TYPE_COMPOUND_OBJECT_CHANGE);
change->obj = comp;
change->saved_state = state;
return DIA_OBJECT_CHANGE (change);
}
static void
mount_point_move_change_apply (DiaDBCompoundMountObjectChange *change, DiaObject *obj)
{
Compound *comp = change->obj;
Point old_pos = comp->handles[0].pos;
comp->handles[0].pos = change->saved_pos;
comp->mount_point.pos = change->saved_pos;
compound_update_data (comp);
change->saved_pos = old_pos;
compound_sanity_check (comp, "After applying mount point move change");
}
static void
dia_db_compound_mount_object_change_apply (DiaObjectChange *self, DiaObject *obj)
{
mount_point_move_change_apply (DIA_DB_COMPOUND_MOUNT_OBJECT_CHANGE (self),
obj);
}
static void
dia_db_compound_mount_object_change_revert (DiaObjectChange *self, DiaObject *obj)
{
mount_point_move_change_apply (DIA_DB_COMPOUND_MOUNT_OBJECT_CHANGE (self),
obj);
}
static void
dia_db_compound_mount_object_change_free (DiaObjectChange *change)
{
/* currently there is nothing to be done here */
}
static DiaObjectChange *
mount_point_move_change_new (Compound *comp, Point *pos)
{
DiaDBCompoundMountObjectChange *change;
change = dia_object_change_new (DIA_DB_TYPE_COMPOUND_MOUNT_OBJECT_CHANGE);
change->obj = comp;
change->saved_pos = *pos;
return DIA_OBJECT_CHANGE (change);
}
/* ------------------------------------------------------------------------ */
static DiaObject *
compound_create (Point * start_point, void * user_data,
Handle **handle1, Handle **handle2)
{
Compound * comp;
DiaObject * obj;
Handle * handle;
gint num_handles;
gint i;
comp = g_new0 (Compound, 1);
obj = &comp->object;
obj->type = &compound_type;
obj->ops = &compound_ops;
comp->num_arms = DEFAULT_NUMARMS;
comp->line_width = attributes_get_default_linewidth ();
comp->line_color = attributes_get_foreground ();
/* init our mount-point */
setup_mount_point (&comp->mount_point, obj, start_point);
num_handles = comp->num_arms + 1;
/* init the inherited object */
object_init (obj, num_handles, 1);
obj->connections[0] = &comp->mount_point;
comp->handles = g_new0 (Handle, num_handles);
/* init the handle on the mount-point */
obj->handles[0] = &comp->handles[0];
setup_handle (obj->handles[0], HANDLE_MOUNT_POINT,
HANDLE_MAJOR_CONTROL, HANDLE_NONCONNECTABLE);
/* now init the rest of the handles */
for (i = 1; i < num_handles; i++)
{
handle = &comp->handles[i];
obj->handles[i] = handle;
setup_handle (handle, HANDLE_ARM,
HANDLE_MINOR_CONTROL, HANDLE_CONNECTABLE_NOBREAK);
}
init_default_handle_positions (comp);
compound_update_data (comp);
compound_sanity_check (comp, "Created");
*handle1 = &comp->handles[0];
*handle2 = &comp->handles[1];
return &comp->object;
}
static DiaObject *
compound_load (ObjectNode obj_node, int version, DiaContext *ctx)
{
Compound * comp;
DiaObject * obj;
AttributeNode attr;
DataNode data;
gint i, num_handles;
comp = g_new0 (Compound, 1);
obj = &comp->object;
/* load the objects position and bounding box */
object_load (obj, obj_node, ctx);
/* init the object */
obj->type = &compound_type;
obj->ops = &compound_ops;
/* load the object's handles and its positions */
attr = object_find_attribute (obj_node, "comp_points");
g_assert (attr != NULL);
num_handles = attribute_num_data (attr);
g_assert (num_handles >= 3);
/* allocate space for object handles and connectionpoints point arrays */
object_init (obj, num_handles, 1);
data = attribute_first_data (attr);
/* init our mount_point */
setup_mount_point (&comp->mount_point, obj, NULL);
data_point (data, &comp->mount_point.pos, ctx);
obj->connections[0] = &comp->mount_point;
/* now init the handles */
comp->num_arms = num_handles-1;
comp->handles = g_new0 (Handle, num_handles);
setup_handle (&comp->handles[0], HANDLE_MOUNT_POINT,
HANDLE_MAJOR_CONTROL, HANDLE_NONCONNECTABLE);
comp->handles[0].pos = comp->mount_point.pos;
obj->handles[0] = &comp->handles[0];
data = data_next (data);
for (i = 1; i < num_handles; i++)
{
obj->handles[i] = &comp->handles[i];
setup_handle (obj->handles[i], HANDLE_ARM,
HANDLE_MINOR_CONTROL, HANDLE_CONNECTABLE_NOBREAK);
data_point (data, &obj->handles[i]->pos, ctx);
data = data_next (data);
}
/* load remainding properties */
attr = object_find_attribute (obj_node, PROP_STDTYPE_LINE_WIDTH);
if (attr == NULL)
comp->line_width = 0.1;
else
comp->line_width = data_real (attribute_first_data (attr), ctx);
attr = object_find_attribute (obj_node, "line_colour");
if (attr == NULL)
comp->line_color = color_black;
else
data_color (attribute_first_data (attr), &comp->line_color, ctx);
compound_update_data (comp);
compound_sanity_check (comp, "Loaded");
return &comp->object;
}
static void
compound_save (Compound *comp, ObjectNode obj_node, DiaContext *ctx)
{
gint i;
AttributeNode attr;
DiaObject * obj = &comp->object;
compound_sanity_check (comp, "Saving");
object_save (&comp->object, obj_node, ctx);
attr = new_attribute(obj_node, "comp_points");
for (i = 0; i < obj->num_handles; i++)
{
Handle *h = obj->handles[i];
data_add_point (attr, &h->pos, ctx);
}
attr = new_attribute (obj_node, PROP_STDNAME_LINE_WIDTH);
data_add_real (attr, comp->line_width, ctx);
attr = new_attribute (obj_node, "line_color");
data_add_color (attr, &comp->line_color, ctx);
}
static void
compound_destroy (Compound * comp)
{
compound_sanity_check (comp, "Destroying");
object_destroy (&comp->object);
g_clear_pointer (&comp->handles, g_free);
}
static void
compound_draw (Compound * comp, DiaRenderer * renderer)
{
gint num_handles;
Point * mount_point_pos;
gint i;
num_handles = comp->object.num_handles;
mount_point_pos = &comp->mount_point.pos;
dia_renderer_set_linewidth (renderer, comp->line_width);
for (i = 1; i < num_handles; i++) {
dia_renderer_draw_line (renderer,
mount_point_pos,
&comp->handles[i].pos,
&comp->line_color);
#if DEBUG_DRAW_BOUNDING_BOX
{
Point ul, br;
ul.x = comp->object.bounding_box.left;
ul.y = comp->object.bounding_box.top;
br.x = comp->object.bounding_box.right;
br.y = comp->object.bounding_box.bottom;
dia_renderer_draw_rect (renderer, &ul, &br, &comp->line_color);
}
#endif
}
#if DEBUG_DRAW_MP_DIRECTION
{
Point p = comp->mount_point.pos;
Color red = { 1.0, 0.0, 0.0, 1.0 };
gchar dirs = comp->mount_point.directions;
if (dirs & DIR_NORTH)
p.y -= 1.0;
if (dirs & DIR_SOUTH)
p.y += 1.0;
if (dirs & DIR_WEST)
p.x -= 1.0;
if (dirs & DIR_EAST)
p.x += 1.0;
dia_renderer_draw_line (renderer, &comp->mount_point.pos, &p, &red);
}
#endif
}
static real
compound_distance_from (Compound * comp, Point * point)
{
real dist = 0.0;
gint num_handles = comp->object.num_handles;
Point * mount_point_pos = &comp->mount_point.pos;
gint i;
dist = distance_line_point (mount_point_pos,
&comp->handles[1].pos,
comp->line_width,
point);
if (dist < 0.000001)
return 0.0;
for (i = 2; i < num_handles; i++)
{
dist = MIN(distance_line_point (mount_point_pos,
&comp->handles[i].pos,
comp->line_width,
point),
dist);
if (dist < 0.000001)
return 0.0;
}
return dist;
}
static void
compound_select (Compound * comp, Point * clicked,
DiaRenderer * interactive_renderer)
{
compound_update_data (comp);
}
static DiaObject *
compound_copy (Compound * comp)
{
Compound * copy;
Handle * ch, * oh;
DiaObject * copy_obj, * comp_obj;
gint i, num_handles;
comp_obj = &comp->object;
num_handles = comp->object.num_handles;
g_assert (comp->num_arms >= 2);
g_assert (comp->num_arms+1 == num_handles);
copy = g_new0 (Compound, 1);
copy_obj = ©->object;
/* copy the properties */
copy->num_arms = comp->num_arms;
copy->line_width = comp->line_width;
copy->line_color = comp->line_color;
/* this will allocate the object's pointer arrays for handles and
connection_points */
object_copy (comp_obj, copy_obj);
/* copy the handles */
copy->handles = g_new (Handle, num_handles);
for (i = 0; i < num_handles; i++)
{
ch = ©->handles[i];
oh = &comp->handles[i];
setup_handle (ch, oh->id, oh->type, oh->connect_type);
ch->pos = oh->pos;
copy_obj->handles[i] = ch;
}
/* copy the connection/mount point */
copy_obj->connections[0] = ©->mount_point;
setup_mount_point (copy_obj->connections[0],
copy_obj,
©_obj->handles[0]->pos);
compound_update_data (comp);
compound_sanity_check (copy, "Copied");
return ©->object;
}
static DiaObjectChange *
compound_move (Compound *comp, Point *to)
{
Point diff;
Handle * h;
int i, num_handles;
diff.x = to->x - comp->object.position.x;
diff.y = to->y - comp->object.position.y;
num_handles = comp->object.num_handles;
for (i = 0; i < num_handles; i++) {
h = &comp->handles[i];
point_add (&h->pos, &diff);
}
point_add (&comp->mount_point.pos, &diff);
compound_update_data (comp);
return NULL;
}
static DiaObjectChange *
compound_move_handle (Compound *comp,
Handle *handle,
Point *to,
ConnectionPoint *cp,
HandleMoveReason reason,
ModifierKeys modifiers)
{
if (handle->id == HANDLE_MOUNT_POINT)
{
g_assert (handle == &comp->handles[0]);
comp->mount_point.pos = *to;
}
else
{
/** ATTENTION: the idea is that while the handles are connected to
* an object and that object moves than this compound also moves
* (as a whole)! unfortunatelly, this method gets called for each
* handle connected and there is not method to register which
* would be called after all the handles of this object has been
* processed by this routine. see also
* app/connectionpoint_ops.c:diagram_update_connections_object
*
* as a work around we say that when the first handle is moved,
* only then we update the position of the mount_point. This
* solves the problem for compounds whose handles (all) are
* connected to some (even more than one) objects.
*/
if (reason == HANDLE_MOVE_CONNECTED
&& handle == &comp->handles[1])
{
Point diff = *to;
point_sub (&diff, &handle->pos);
point_add (&comp->handles[0].pos, &diff);
point_add (&comp->mount_point.pos, &diff);
}
}
handle->pos = *to;
compound_update_data (comp);
return NULL;
}
static DiaObjectChange *
compound_apply_properties_dialog (Compound * comp, GtkWidget * dialog_widget)
{
CompoundState * state;
PropDialog *dialog = prop_dialog_from_widget(dialog_widget);
state = compound_state_new (comp);
prop_get_data_from_widgets(dialog);
compound_apply_props (comp, dialog->props, FALSE);
return compound_change_new (comp, state);
}
static PropDescription *
compound_describe_props (Compound * comp)
{
if (compound_props[0].quark == 0)
prop_desc_list_calculate_quarks (compound_props);
return compound_props;
}
static void
compound_get_props (Compound * comp, GPtrArray * props)
{
object_get_props_from_offsets(&comp->object, compound_offsets, props);
}
/**
* Everything I know about is initialize so that this function get called
* only when the user is applying the default dialog.
*/
static void
compound_set_props (Compound * comp, GPtrArray * props)
{
compound_apply_props (comp, props, TRUE);
}
static void
compound_apply_props (Compound * comp, GPtrArray * props, gboolean is_default)
{
gint change_count;
object_set_props_from_offsets (&comp->object, compound_offsets, props);
/* comp->num_arms has already been set by
the call to object_set_props_from_offsets () */
change_count = adjust_handle_count_to (comp, comp->num_arms+1);
if (change_count)
{
/* if there has been some arms added to the default object
reinitialize all handles' position */
if (change_count > 0)
{
if (is_default)
init_default_handle_positions (comp);
else
{
gint new_index = comp->object.num_handles - change_count;
init_positions_for_handles_beginning_at_index (comp, new_index);
}
}
}
compound_update_data (comp);
compound_sanity_check (comp, "After setting properties");
}
/**
* Determine the bounding box of the compound object and store it in the
* object. Also update the object's position to the upper left corner of
* the bounding box.
*/
static void compound_update_object (Compound * comp)
{
DiaRectangle * bb;
Handle * h;
gint i;
gint num_handles = comp->object.num_handles;
h = &comp->handles[0];
bb = &comp->object.bounding_box;
bb->right = bb->left = h->pos.x;
bb->top = bb->bottom = h->pos.y;
for (i = 1; i < num_handles; i++)
{
h = &comp->handles[i];
bb->left = MIN (h->pos.x, bb->left);
bb->right = MAX(h->pos.x, bb->right);
bb->top = MIN(h->pos.y, bb->top);
bb->bottom = MAX(h->pos.y, bb->bottom);
}
comp->object.position.x = bb->left;
comp->object.position.y = bb->top;
#if DEBUG_DRAW_MP_DIRECTION
/* make the bounding box a bit larger because drawing the direction of
* the mount point can go beyond the current bounding box.
*/
bb->left -= 1.0;
bb->right += 1.0;
bb->top -= 1.0;
bb->bottom += 1.0;
#endif
}
/**
* Add or remove handles, so that comp->object.num_handles will equal to
* the specified value. When there have been no changes done 0 is
* returned, otherwise the number of handles removed (negative value) or
* the number of added handles (positive value) will be returned.
*/
static int
adjust_handle_count_to (Compound * comp, gint to)
{
DiaObject * obj = &comp->object;
int old_count = obj->num_handles;
int new_count = to;
Handle * h;
int diff = 0;
/* we require to have always at least two arms! */
g_assert (new_count >= 3);
if (new_count == old_count)
return diff; /* every thing is ok */
obj->handles = g_renew (Handle *, obj->handles, new_count);
obj->num_handles = new_count;
comp->num_arms = new_count-1;
if (new_count < old_count) {
/* removing */
for (int i = new_count; i < old_count; i++) {
object_unconnect (obj, &comp->handles[i]);
}
comp->handles = g_renew (Handle, comp->handles, new_count);
} else {
/* adding */
comp->handles = g_renew (Handle, comp->handles, new_count);
for (int i = old_count; i < new_count; i++) {
h = &comp->handles[i];
setup_handle (h, HANDLE_ARM,
HANDLE_MINOR_CONTROL, HANDLE_CONNECTABLE_NOBREAK);
}
}
for (int i = 0; i < new_count; i++) {
obj->handles[i] = &comp->handles[i];
}
return new_count - old_count;
}
static void
setup_mount_point (ConnectionPoint * mp, DiaObject * obj, Point * pos)
{
if (pos != NULL)
mp->pos = *pos;
mp->object = obj;
mp->connected = NULL;
mp->directions = DIR_ALL;
mp->flags = 0;
}
static void
setup_handle (Handle *h, HandleId id, HandleType type, HandleConnectType ctype)
{
g_assert (h != NULL);
h->id = id;
h->type = type;
h->pos.x = h->pos.y = 0.0;
h->connect_type = ctype;
h->connected_to = NULL;
}
/*
* Update the directions fields of our mount point, so that when an
* object is connected to it, let's say an OrthConn object, the
* connected object will automatically be placed at that side which
* contains no arms. If there is no free space left, DIR_ALL is assigned
* which allows new object to connect from any side.
*/
static void
update_mount_point_directions (Compound *c)
{
Point *p;
Point mp_pos;
gint i, below_index;
gchar used_dir = DIR_NONE;
below_index = c->object.num_handles;
mp_pos = c->mount_point.pos;
for (i = 1; i < below_index; i++)
{
p = &c->object.handles[i]->pos;
used_dir |= (p->x <= mp_pos.x) ? DIR_WEST : DIR_EAST;
used_dir |= (p->y <= mp_pos.y) ? DIR_NORTH : DIR_SOUTH;
}
used_dir = (used_dir ^ DIR_ALL)&DIR_ALL;
if ((used_dir&DIR_ALL) == DIR_NONE)
used_dir = DIR_ALL;
c->mount_point.directions = used_dir;
}
/**
* Reposition the handles to a default position. The handles will be on
* one vertical line equally distributed to the left of the mount point.
* The first handle will be set to the position of the mount_point! The
* mount point's position must be initialized before this function is
* called.
*/
static void
init_default_handle_positions (Compound *c)
{
Handle * h;
Point run_point;
gint i, num_handles;
num_handles = c->object.num_handles;
h = c->object.handles[0];
h->pos = c->mount_point.pos;
run_point = h->pos;
/* XXX: as you can see the code positions the points on a vertical line
to the left of the mount point; if you like to change this ...
here is the right place to do so.
*/
run_point.x -= DEFAULT_ARM_X_DISTANCE;
run_point.y -= ((num_handles-2)*DEFAULT_ARM_Y_DISTANCE)/2.0;
for (i = 1; i < num_handles; i++)
{
h = c->object.handles[i];
h->pos = run_point;
run_point.y += DEFAULT_ARM_Y_DISTANCE;
}
}
/**
* This routine will initialize the position of handles with index >=
* HINDEX.
*/
static void
init_positions_for_handles_beginning_at_index (Compound *c, gint hindex)
{
Handle * h;
Point tmp;
gint i, num_handles, num_new_gaps;
real x_inc;
real y_inc;
num_handles = c->object.num_handles;
g_assert (hindex < num_handles);
num_new_gaps = (num_handles - hindex)-1;
/* XXX: the following is too simple! there is no code for checking the
new position not colliding with any other line of this object.
however, it is sufficient for now, but feel free to improve this
code if you would like to do so.
*/
tmp = c->mount_point.pos;
switch (c->mount_point.directions)
{
case DIR_NORTH:
tmp.y -= DEFAULT_ARM_Y_DISTANCE;
tmp.x -= (DEFAULT_ARM_X_DISTANCE*num_new_gaps)/2.0;
x_inc = DEFAULT_ARM_X_DISTANCE;
y_inc = 0.0;
break;
case DIR_EAST:
tmp.x += DEFAULT_ARM_X_DISTANCE;
tmp.y -= (DEFAULT_ARM_Y_DISTANCE*num_new_gaps)/2.0;
x_inc = 0.0;
y_inc = DEFAULT_ARM_Y_DISTANCE;
break;
case DIR_SOUTH:
tmp.y += DEFAULT_ARM_Y_DISTANCE;
tmp.x -= (DEFAULT_ARM_X_DISTANCE*num_new_gaps)/2.0;
x_inc = DEFAULT_ARM_X_DISTANCE;
y_inc = 0.0;
break;
case DIR_WEST:
tmp.x -= DEFAULT_ARM_X_DISTANCE;
tmp.y -= (DEFAULT_ARM_Y_DISTANCE*num_new_gaps)/2.0;
x_inc = 0.0;
y_inc = DEFAULT_ARM_Y_DISTANCE;
break;
default:
/* XXX this could be more intelligent */
x_inc = DEFAULT_ARM_X_DISTANCE;
y_inc = DEFAULT_ARM_Y_DISTANCE;
tmp.x += x_inc;
tmp.y += y_inc;
break;
}
for (i = hindex; i < num_handles; i++)
{
h = c->object.handles[i];
h->pos = tmp;
tmp.x += x_inc;
tmp.y += y_inc;
}
}
/**
* Perform the default updating of dynamic data. In particular this is:
* - update the object's position and bounding box depending on the
* handles position.
*
* - make sure that the compound's num_arms correctly corresponds to the
* object's number of handles. If not create or remove handles.
*
* - update the mount points directions field depending on the position
* of the object's handles.
*/
static void
compound_update_data (Compound *c)
{
/* make sure the number of handles is correct */
adjust_handle_count_to (c, c->num_arms+1);
/* this will update the object's position and bounding box */
compound_update_object (c);
/* this will compute the mount point's directions field depending on
the position of the handles */
update_mount_point_directions (c);
}
static DiaMenu *
compound_object_menu(DiaObject *obj, Point *p)
{
Compound * comp = (Compound *) obj;
gchar dir = comp->mount_point.directions;
if (dir == DIR_ALL)
{
compound_menu_items[0].active = 0;
compound_menu_items[1].active = 0;
}
else
{
compound_menu_items[0].active = ((dir & DIR_NORTH) == DIR_NORTH
|| ((dir & DIR_SOUTH) == DIR_SOUTH))
? DIAMENU_ACTIVE
: 0;
compound_menu_items[1].active = ((dir & DIR_WEST) == DIR_WEST
|| (dir & DIR_EAST) == DIR_EAST)
? DIAMENU_ACTIVE
: 0;
}
return &compound_menu;
}
static DiaObjectChange *
compound_flip_arms_cb (DiaObject *obj, Point *pos, gpointer data)
{
Compound * comp = (Compound *) obj;
int direction = GPOINTER_TO_INT (data);
Point * mppos = &comp->mount_point.pos;
CompoundState * state;
Handle * h;
Point * p;
int num_handles, i;
state = compound_state_new (comp);
num_handles = obj->num_handles;
for (i = 1; i < num_handles; i++)
{
h = obj->handles[i];
object_unconnect (obj, h);
p = &h->pos;
if (direction == FLIP_VERTICAL)
{
p->y -= mppos->y;
p->y *= -1.0;
p->y += mppos->y;
}
else
{
p->x -= mppos->x;
p->x *= -1.0;
p->x += mppos->x;
}
}
compound_update_data (comp);
compound_sanity_check (comp, "After flipping sides");
return compound_change_new (comp, state);
}
static DiaObjectChange *
compound_repos_mount_point_cb (DiaObject * obj, Point *clicked, gpointer data)
{
Compound * comp = (Compound *) obj;
Handle * h;
Point old_pos;
Point pos;
int what_todo;
int i, num_handles;
old_pos = comp->mount_point.pos;
what_todo = GPOINTER_TO_INT (data);
h = comp->object.handles[1];
pos = h->pos;
num_handles = comp->object.num_handles;
for (i = 2; i < num_handles; i++)
{
h = comp->object.handles[i];
point_add (&pos, &h->pos);
}
switch (what_todo)
{
case CENTER_BOTH:
pos.x /= (real) (num_handles - 1);
pos.y /= (real) (num_handles - 1);
break;
case CENTER_VERTICAL:
pos.y /= (real) (num_handles - 1);
pos.x = comp->handles[0].pos.x;
break;
case CENTER_HORIZONTAL:
pos.x /= (real) (num_handles - 1);
pos.y = comp->handles[0].pos.y;
break;
default:
g_assert (FALSE);
}
comp->handles[0].pos = pos;
comp->mount_point.pos = pos;
compound_update_data (comp);
return mount_point_move_change_new (comp, &old_pos);
}
/**
* The only purpose is to check whether the passed compound object is in
* a consitent state. If it's not, abort the program with a message!
*/
static void
compound_sanity_check (Compound * c, gchar *msg)
{
DiaObject * obj;
Point * ph, * pc;
gint i;
obj = &c->object;
dia_object_sanity_check (obj, msg);
dia_assert_true (obj->num_connections == 1,
"%s: Compound %p has not exactly one connection but %d!\n",
msg, c, obj->num_connections);
dia_assert_true (obj->connections[0] == &c->mount_point,
"%s: Compound %p connection mismatch %p != %p!\n",
msg, c, obj->connections[0], &c->mount_point);
dia_assert_true (obj->num_handles >= 3,
"%s: Object %p has only %d handles, but at least %d are required!\n",
msg, c, obj->num_handles, 3);
dia_assert_true (obj->num_handles == (c->num_arms+1),
"%s: Compound %p has %d handles and %d arms. The number of arms must be the number of handles decreased by one!\n",
msg, c, obj->num_handles, c->num_arms);
for (i = 0; i < obj->num_handles; i++)
dia_assert_true (obj->handles[i] == &c->handles[i],
"%s: Compound %p handles mismatch at %d: %p != %p!\n",
msg, c, i, obj->handles[i], &c->handles[i]);
ph = &obj->handles[0]->pos;
pc = &c->mount_point.pos;
dia_assert_true (ph->x == pc->x && ph->y == pc->y,
"%s: Compound %p handle[0]/mount_point position mismatch: (%f, %f) != (%f, %f)!\n",
msg, c, ph->x, ph->y, pc->x, pc->y);
}
|