forked from rochus-keller/OberonSystem3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Figures.Mod
1435 lines (1301 loc) · 44.5 KB
/
Figures.Mod
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
MODULE Figures; (** eos **)
IMPORT
Files, Display, Math, Objects, Oberon, Gadgets, GfxMatrix, GfxMaps, GfxRegions, Gfx, Solver;
CONST
(** units **)
Inch* = 91.44; cm* = Inch/2.54;
A4W* = 21.0 * cm; A4H* = 29.7 * cm;
LetterW* = 8.5 * Inch; LetterH* = 11.0 * Inch;
(** shape message ids **)
Get* = 0;
Set* = 3; Reset* = 4;
Consume* = 6; Remove* = 7;
Up* = 8; Down* = 9; Top* = 21; Bottom* = 22; Reverse* = 10;
Inside* = 11; Overlap* = 12; Project* = 13;
Full* = 14; Print* = 15;
Apply* = 16; Notify* = 17; Suggest* = 20;
BorderWidth* = 12; (** number of extra units in default context coordinates available for rendering selection hints **)
MaxCommands = 20;
TYPE
(** graphical objects **)
Shape* = POINTER TO ShapeDesc;
ShapeDesc* = RECORD (Objects.ObjDesc)
cont*: Shape; (** shape containing this shape **)
llx*, lly*, urx*, ury*: REAL; (** exact bounding box **)
bw*: REAL; (** width of border around shape affected by rendering **)
sel*: BOOLEAN; (** set if shape or one of its components is selected **)
END;
(** lists of references to shapes **)
Reference* = POINTER TO ReferenceDesc;
ReferenceDesc* = RECORD
shape*: Shape; (** referenced shape **)
next*, prev*: Reference; (** links to next and previous shape reference within list **)
END;
(** shapes containing others **)
Container* = POINTER TO ContainerDesc;
ContainerDesc* = RECORD (ShapeDesc)
first*, last*: Reference; (** first (bottommost) and last (topmost) element in list of components **)
END;
(** collection of shapes **)
Command = POINTER TO CommandDesc;
Figure* = POINTER TO FigureDesc;
FigureDesc* = RECORD (Objects.ObjDesc)
root*: Container; (** container holding all shapes **)
width*, height*: REAL; (** figure dimensions **)
selTime*: LONGINT; (** time of current selection (<0 if no shapes selected) **)
damage: GfxRegions.Region; (* accumulated area that should be updated *)
border: REAL; (* maximum border width within damage region *)
lockLevel: INTEGER; (* enables updates if not positive *)
cmdLevel: INTEGER; (* number of nested commands *)
maxCmds, nofCmds: INTEGER; (* maximal and current number of undoable commands retained *)
curCmd: Command; (* current command *)
firstCmd: Command; (* first command that can still be undone *)
undoCmd: Command; (* command to undo next *)
END;
(** undoable commands **)
Action* = POINTER TO ActionDesc;
ActionProc* = PROCEDURE (fig: Figure; act: Action; stamp: LONGINT; undo: BOOLEAN);
ActionDesc* = RECORD
do: ActionProc; (* handler for executing and undoing actions *)
link: Action; (* link to next action *)
END;
CommandDesc = RECORD
next, prev: Command; (* links to next and previous command *)
actions, done, last: Action; (* list of actions *)
END;
ControlAction* = POINTER TO ControlActionDesc;
ControlActionDesc* = RECORD (ActionDesc)
id: INTEGER;
prev, first, last, next: Reference;
cont: Container;
END;
AttrAction = POINTER TO AttrActionDesc;
AttrActionDesc = RECORD (ActionDesc)
obj: Objects.Object;
attr: Solver.AttrNode;
END;
(** broadcast of model changes **)
UpdateMsg* = RECORD (Display.FrameMsg)
fig*: Figure; (** affected figure **)
reg*: GfxRegions.Region; (** affected area **)
bw*: INTEGER; (** border to add around region **)
END;
(** shape message base type **)
ShapeMsg* = RECORD (Objects.ObjMsg)
fig*: Figure; (** containing figure **)
END;
(** message for changing or querying the selection state of shapes **)
SelectMsg* = RECORD (ShapeMsg)
id*: INTEGER; (** Get/Set/Reset **)
shapes*: Shape; (** linked list of shapes which handled Get **)
END;
(** message for locating shapes **)
LocateMsg* = RECORD (ShapeMsg)
id*: INTEGER; (** Inside/Overlap/Project **)
llx*, lly*, urx*, ury*: REAL; (** rectangle within which to locate **)
shapes*: Shape; (** linked list of shapes which handled message (in top-down order) **)
px*, py*: REAL; (** projected coordinates **)
END;
(** message asking a shape to validate its bounding box and update the containing figure **)
ValidateMsg* = RECORD (ShapeMsg)
mat*: GfxMatrix.Matrix; (** local to global transformation **)
END;
(** message for rendering shapes on a graphics context **)
DrawMsg* = RECORD (ShapeMsg)
id*: INTEGER; (** Full/Print **)
all*: BOOLEAN; (** set if all shapes should render themselves, otherwise only stamped shapes should **)
ctxt*: Gfx.Context; (** associated context **)
mat*: GfxMatrix.Matrix; (** transformation from default to current context space **)
llx*, lly*, urx*, ury*: REAL; (** clip rectangle **)
END;
(** message for changing the shape hierarchy **)
ControlMsg* = RECORD (ShapeMsg)
id*: INTEGER; (** Consume/Remove **)
dest*: Shape; (** shape that should handle the message **)
shapes*: Shape; (** shape(s) parameter **)
END;
(** message for reordering selected shapes **)
PriorityMsg* = RECORD (ShapeMsg)
id*: INTEGER; (** Up/Down/Top/Bottom/Reverse **)
END;
(** message for applying transformations to shapes or querying a local transformation **)
TransformMsg* = RECORD (ShapeMsg)
id*: INTEGER; (** Get/Apply/Notify/Suggest **)
mat*: GfxMatrix.Matrix; (** transformation matrix **)
x0*, y0*, x1*, y1*: REAL; (** original and moved point coordinates for transformation suggestion **)
END;
VAR
Dummy*: Shape; (** replacement for shapes with missing generator **)
(**--- Basic Object Functionality ---**)
PROCEDURE Forward* (from, to: Objects.Object; VAR msg: Objects.ObjMsg);
VAR dlink, mlink: Objects.Object;
BEGIN
dlink := from.dlink; mlink := msg.dlink;
from.dlink := mlink; msg.dlink := from;
to.handle(to, msg);
from.dlink := dlink; msg.dlink := mlink
END Forward;
PROCEDURE HandleGenAttr* (VAR msg: Objects.AttrMsg; s: ARRAY OF CHAR);
BEGIN
IF (msg.id = Objects.get) & (msg.name = "Gen") & (msg.res < 0) THEN
msg.class := Objects.String; msg.s := s; msg.res := 0
END
END HandleGenAttr;
PROCEDURE HandleStringAttr* (VAR msg: Objects.AttrMsg; name: ARRAY OF CHAR; VAR s: ARRAY OF CHAR);
BEGIN
IF msg.id = Objects.enum THEN
msg.Enum(name)
ELSIF (msg.res < 0) & (msg.name = name) THEN
IF msg.id = Objects.get THEN
msg.class := Objects.String; msg.s := s; msg.res := 0
ELSIF (msg.id = Objects.set) & (msg.class = Objects.String) THEN
s := msg.s; msg.res := 0
END
END
END HandleStringAttr;
PROCEDURE HandleIntAttr* (VAR msg: Objects.AttrMsg; name: ARRAY OF CHAR; VAR i: INTEGER; lo, hi: INTEGER);
BEGIN
IF msg.id = Objects.enum THEN
msg.Enum(name)
ELSIF (msg.res < 0) & (msg.name = name) THEN
IF msg.id = Objects.get THEN
msg.class := Objects.Int; msg.i := i; msg.res := 0
ELSIF (msg.id = Objects.set) & (msg.class = Objects.Int) THEN
IF msg.i < lo THEN i := lo
ELSIF hi < msg.i THEN i := hi
ELSE i := SHORT(msg.i)
END;
msg.res := 0
END
END
END HandleIntAttr;
PROCEDURE HandleLongIntAttr* (VAR msg: Objects.AttrMsg; name: ARRAY OF CHAR; VAR l: LONGINT; lo, hi: LONGINT);
BEGIN
IF msg.id = Objects.enum THEN
msg.Enum(name)
ELSIF (msg.res < 0) & (msg.name = name) THEN
IF msg.id = Objects.get THEN
msg.class := Objects.Int; msg.i := l; msg.res := 0
ELSIF (msg.id = Objects.set) & (msg.class = Objects.Int) THEN
IF msg.i < lo THEN l := lo
ELSIF hi < msg.i THEN l := hi
ELSE l := msg.i
END;
msg.res := 0
END
END
END HandleLongIntAttr;
PROCEDURE HandleRealAttr* (VAR msg: Objects.AttrMsg; name: ARRAY OF CHAR; VAR r: REAL);
BEGIN
IF msg.id = Objects.enum THEN
msg.Enum(name)
ELSIF (msg.res < 0) & (msg.name = name) THEN
IF msg.id = Objects.get THEN
msg.class := Objects.Real; msg.x := r; msg.res := 0
ELSIF msg.id = Objects.set THEN
IF msg.class = Objects.Int THEN
r := msg.i; msg.res := 0
ELSIF msg.class = Objects.Real THEN
r := msg.x; msg.res := 0
ELSIF msg.class = Objects.LongReal THEN
r := SHORT(msg.y); msg.res := 0
END
END
END
END HandleRealAttr;
PROCEDURE HandleBoolAttr* (VAR msg: Objects.AttrMsg; name: ARRAY OF CHAR; VAR b: BOOLEAN);
BEGIN
IF msg.id = Objects.enum THEN
msg.Enum(name)
ELSIF (msg.res < 0) & (msg.name = name) THEN
IF msg.id = Objects.get THEN
msg.class := Objects.Bool; msg.b := b; msg.res := 0
ELSIF (msg.id = Objects.set) & (msg.class = Objects.Bool) THEN
b := msg.b; msg.res := 0
END
END
END HandleBoolAttr;
PROCEDURE HandleColorAttr* (VAR msg: Objects.AttrMsg; name: ARRAY OF CHAR; VAR col: Gfx.Color);
BEGIN
IF msg.id = Objects.enum THEN
msg.Enum(name)
ELSIF (msg.res < 0) & (msg.name = name) THEN
IF msg.id = Objects.get THEN
msg.class := Objects.Int; msg.i := GfxMaps.BestIndex(GfxMaps.DisplayPal, col.r, col.g, col.b); msg.res := 0
ELSIF (msg.id = Objects.set) & (msg.class = Objects.Int) & (0 <= msg.i) & (msg.i < GfxMaps.DisplayPal.used) THEN
col := GfxMaps.DisplayPal.col[msg.i]; msg.res := 0
END
END
END HandleColorAttr;
(**--- Shape Bounds ---**)
PROCEDURE GetShapeBox* (shape: Shape; VAR llx, lly, urx, ury, bw: REAL);
VAR tm: TransformMsg;
BEGIN
llx := shape.llx; lly := shape.lly; urx := shape.urx; ury := shape.ury; bw := 0;
IF (urx >= llx) & (ury >= lly) THEN
tm.fig := NIL; tm.id := Get;
shape.handle(shape, tm);
GfxMatrix.ApplyToRect(tm.mat, llx, lly, urx, ury, llx, lly, urx, ury);
GfxMatrix.ApplyToDist(tm.mat, shape.bw, bw)
END
END GetShapeBox;
(**--- Figure Updates ---**)
PROCEDURE UpdateFigure (fig: Figure);
VAR um: UpdateMsg;
BEGIN
um.F := NIL; um.fig := fig; um.reg := fig.damage; um.bw := -SHORT(ENTIER(fig.border));
Display.Broadcast(um);
GfxRegions.Clear(fig.damage);
fig.border := 0
END UpdateFigure;
PROCEDURE DisableUpdate* (fig: Figure);
BEGIN
INC(fig.lockLevel)
END DisableUpdate;
PROCEDURE EnableUpdate* (fig: Figure);
BEGIN
DEC(fig.lockLevel);
IF (fig.lockLevel = 0) & ~GfxRegions.Empty(fig.damage) THEN
UpdateFigure(fig)
END
END EnableUpdate;
PROCEDURE UpdateRect* (fig: Figure; llx, lly, urx, ury, bw: REAL);
VAR llu, llv, uru, urv: INTEGER;
BEGIN
llu := SHORT(ENTIER(llx)); llv := SHORT(ENTIER(lly));
uru := -SHORT(ENTIER(-urx)); urv := -SHORT(ENTIER(-ury));
IF fig.lockLevel > 0 THEN
GfxRegions.AddRect(fig.damage, llu, llv, uru, urv);
IF bw > fig.border THEN fig.border := bw END
ELSE
GfxRegions.SetToRect(fig.damage, llu, llv, uru, urv);
fig.border := bw;
UpdateFigure(fig)
END
END UpdateRect;
(**--- Selection ---**)
PROCEDURE GetSelection* (fig: Figure; VAR shapes: Shape);
VAR sm: SelectMsg;
BEGIN
Objects.Stamp(sm); sm.fig := fig; sm.id := Get; sm.shapes := NIL;
Forward(fig, fig.root, sm);
shapes := sm.shapes
END GetSelection;
PROCEDURE Select* (fig: Figure; shapes: Shape);
VAR sm: SelectMsg; obj: Objects.Object;
BEGIN
DisableUpdate(fig);
sm.fig := fig; sm.id := Set;
obj := shapes;
WHILE obj # NIL DO
obj.handle(obj, sm);
obj := obj.slink
END;
fig.selTime := Oberon.Time();
EnableUpdate(fig)
END Select;
PROCEDURE Deselect* (fig: Figure; shapes: Shape);
VAR sm: SelectMsg; obj: Objects.Object;
BEGIN
DisableUpdate(fig);
sm.fig := fig; sm.id := Reset;
obj := shapes;
WHILE obj # NIL DO
obj.handle(obj, sm);
obj := obj.slink
END;
fig.selTime := Oberon.Time();
EnableUpdate(fig)
END Deselect;
PROCEDURE ClearSelection* (fig: Figure);
VAR sm: SelectMsg;
BEGIN
DisableUpdate(fig);
sm.fig := fig; sm.id := Reset;
fig.root.handle(fig.root, sm);
EnableUpdate(fig)
END ClearSelection;
(**--- Undoable Commands ---**)
PROCEDURE InvertActions (cmd: Command);
VAR cur, link: Action;
BEGIN
cur := cmd.actions; cmd.actions := NIL;
WHILE cur # NIL DO
link := cur.link; cur.link := cmd.actions; cmd.actions := cur; cur := link
END
END InvertActions;
PROCEDURE CallActions (fig: Figure; cmd: Command; undo: BOOLEAN);
VAR vm: ValidateMsg; cur: Action;
BEGIN
InvertActions(cmd);
DisableUpdate(fig);
ClearSelection(fig);
Objects.Stamp(vm);
cur := cmd.actions;
WHILE cur # NIL DO
cur.do(fig, cur, vm.stamp, undo);
cur := cur.link
END;
vm.fig := fig;
Forward(fig, fig.root, vm);
EnableUpdate(fig)
END CallActions;
PROCEDURE AddAction* (fig: Figure; action: Action; do: ActionProc);
VAR cmd: Command;
BEGIN
ASSERT(fig.cmdLevel > 0);
action.do := do; action.link := NIL;
cmd := fig.curCmd;
IF cmd.last = NIL THEN cmd.actions := action ELSE cmd.last.link := action END;
cmd.last := action
END AddAction;
PROCEDURE BeginCommand* (fig: Figure);
VAR cmd: Command;
BEGIN
INC(fig.cmdLevel);
IF (fig.cmdLevel = 1) & (fig.curCmd = NIL) THEN
NEW(fig.curCmd)
END
END BeginCommand;
PROCEDURE CancelCommand* (fig: Figure);
VAR cmd: Command;
BEGIN
cmd := fig.curCmd;
IF cmd.actions # NIL THEN
IF cmd.done # NIL THEN
cmd.done.link := NIL; cmd.last := cmd.done
ELSE
cmd.actions := NIL; cmd.last := NIL
END
END;
IF (fig.cmdLevel = 1) & (cmd.actions # NIL) THEN
CallActions(fig, cmd, TRUE);
cmd.actions := NIL; cmd.done := NIL; cmd.last := NIL
END;
DEC(fig.cmdLevel)
END CancelCommand;
PROCEDURE EndCommand* (fig: Figure);
VAR cmd: Command; vm: ValidateMsg; cur: Action;
BEGIN
cmd := fig.curCmd;
IF cmd.actions # NIL THEN
DisableUpdate(fig);
Objects.Stamp(vm);
IF cmd.done # NIL THEN cur := cmd.done.link ELSE cur := cmd.actions END;
WHILE cur # NIL DO
cur.do(fig, cur, vm.stamp, FALSE);
cur := cur.link
END;
cmd.done := cmd.last;
fig.selTime := Oberon.Time();
vm.fig := fig;
Forward(fig, fig.root, vm);
IF fig.cmdLevel = 1 THEN
fig.undoCmd.next := cmd; cmd.prev := fig.undoCmd; cmd.next := NIL;
fig.undoCmd := cmd; fig.curCmd := NIL;
IF fig.nofCmds = fig.maxCmds THEN fig.firstCmd := fig.firstCmd.next; fig.firstCmd.prev := NIL
ELSE INC(fig.nofCmds)
END
END;
EnableUpdate(fig)
END;
DEC(fig.cmdLevel)
END EndCommand;
PROCEDURE Undo* (fig: Figure);
VAR cmd: Command;
BEGIN
cmd := fig.undoCmd;
IF (fig.cmdLevel = 0) & (cmd.prev # NIL) THEN
fig.undoCmd := cmd.prev;
CallActions(fig, cmd, TRUE);
DEC(fig.nofCmds)
END
END Undo;
PROCEDURE Redo* (fig: Figure);
VAR cmd: Command;
BEGIN
cmd := fig.undoCmd.next;
IF (fig.cmdLevel = 0) & (cmd # NIL) THEN
fig.undoCmd := cmd;
CallActions(fig, cmd, FALSE);
INC(fig.nofCmds)
END
END Redo;
(**--- Locating ---**)
PROCEDURE ProjectToHandles* (shape: Shape; VAR lm: LocateMsg; llx, lly, urx, ury: REAL);
VAR mx, my: REAL;
BEGIN
mx := 0.5*(llx + urx); my := 0.5*(lly + ury);
IF (lm.llx <= mx) & (mx <= lm.urx) THEN
IF (lm.lly <= my) & (my <= lm.ury) THEN lm.px := mx; lm.py := my; lm.shapes := shape
ELSIF (lm.lly <= lly) & (lly <= lm.ury) THEN lm.px := mx; lm.py := lly; lm.shapes := shape
ELSIF (lm.lly <= ury) & (ury <= lm.ury) THEN lm.px := mx; lm.py := ury; lm.shapes := shape
END
ELSIF (lm.llx <= llx) & (llx <= lm.urx) THEN
IF (lm.lly <= my) & (my <= lm.ury) THEN lm.px := llx; lm.py := my; lm.shapes := shape
ELSIF (lm.lly <= lly) & (lly <= lm.ury) THEN lm.px := llx; lm.py := lly; lm.shapes := shape
ELSIF (lm.lly <= ury) & (ury <= lm.ury) THEN lm.px := llx; lm.py := ury; lm.shapes := shape
END
ELSIF (lm.llx <= urx) & (urx <= lm.urx) THEN
IF (lm.lly <= my) & (my <= lm.ury) THEN lm.px := urx; lm.py := my; lm.shapes := shape
ELSIF (lm.lly <= lly) & (lly <= lm.ury) THEN lm.px := urx; lm.py := lly; lm.shapes := shape
ELSIF (lm.lly <= ury) & (ury <= lm.ury) THEN lm.px := urx; lm.py := ury; lm.shapes := shape
END
END
END ProjectToHandles;
(**--- Rendering ---**)
(** render figure on given context **)
PROCEDURE Render* (fig: Figure; id: INTEGER; ctxt: Gfx.Context);
VAR dm: DrawMsg; ctm, inv: GfxMatrix.Matrix;
BEGIN
Objects.Stamp(dm); dm.dlink := NIL; dm.fig := fig; dm.id := id; dm.all := TRUE; dm.ctxt := ctxt;
ctm := ctxt.ctm;
Gfx.ResetCTM(ctxt);
GfxMatrix.Invert(ctm, inv);
GfxMatrix.Concat(inv, ctxt.ctm, dm.mat);
Gfx.SetCTM(ctxt, ctm);
Forward(fig, fig.root, dm)
END Render;
PROCEDURE DrawHandles* (VAR dm: DrawMsg; llx, lly, urx, ury: REAL);
VAR ctm, mat: GfxMatrix.Matrix; col: Gfx.Color; mx, my: REAL;
PROCEDURE draw (x, y: REAL);
BEGIN
GfxMatrix.Apply(mat, x, y, x, y);
Gfx.DrawRect(dm.ctxt, x-2, y-2, x+2, y+2, {Gfx.Fill})
END draw;
BEGIN
ctm := dm.ctxt.ctm; col := dm.ctxt.fillCol;
Gfx.ResetCTM(dm.ctxt);
GfxMatrix.Invert(dm.ctxt.ctm, mat);
GfxMatrix.Concat(ctm, mat, mat); (* mat now maps from local to default context coords *)
Gfx.SetFillColor(dm.ctxt, Gfx.DGrey);
mx := 0.5*(llx + urx); my := 0.5*(lly + ury);
draw(llx, ury); draw(mx, ury); draw(urx, ury);
draw(llx, my); draw(urx, my);
draw(llx, lly); draw(mx, lly); draw(urx, lly);
Gfx.SetFillColor(dm.ctxt, col);
Gfx.SetCTM(dm.ctxt, ctm)
END DrawHandles;
(**--- Control ---**)
PROCEDURE Integrate* (fig: Figure; target, shapes: Shape);
VAR cm: ControlMsg;
BEGIN
cm.fig := fig; cm.id := Consume; cm.dest := target; cm.shapes := shapes;
BeginCommand(fig);
Forward(fig, fig.root, cm);
EndCommand(fig)
END Integrate;
PROCEDURE Delete* (fig: Figure; shapes: Shape);
VAR cm: ControlMsg; obj: Objects.Object;
BEGIN
Objects.Stamp(cm); cm.fig := fig; cm.id := Remove;
obj := shapes;
WHILE obj # NIL DO
obj.stamp := cm.stamp; obj := obj.slink
END;
BeginCommand(fig);
Forward(fig, fig.root, cm);
EndCommand(fig)
END Delete;
(**--- Transformations ---**)
PROCEDURE SuggestHandleTrafo* (VAR tm: TransformMsg; llx, lly, urx, ury: REAL);
CONST tol = 0.1;
VAR x0, y0: REAL;
BEGIN
IF (llx < urx) & (lly < ury) THEN
x0 := (tm.x0 - llx)/(urx - llx); y0 := (tm.y0 - lly)/(ury - lly);
IF (-tol <= x0) & (x0 <= tol) THEN
IF (-tol <= y0) & (y0 <= tol) THEN
GfxMatrix.ScaleAt(GfxMatrix.Identity, urx, ury, (tm.x1 - urx)/(tm.x0 - urx), (tm.y1 - ury)/(tm.y0 - ury), tm.mat)
ELSIF (1-tol <= y0) & (y0 <= 1+tol) THEN
GfxMatrix.ScaleAt(GfxMatrix.Identity, urx, lly, (tm.x1 - urx)/(tm.x0 - urx), (tm.y1 - lly)/(tm.y0 - lly), tm.mat)
ELSIF (0.5-tol <= y0) & (y0 <= 0.5+tol) THEN
GfxMatrix.ScaleAt(GfxMatrix.Identity, urx, 0.5*(lly + ury), (tm.x1 - urx)/(tm.x0 - urx), 1, tm.mat)
ELSE
GfxMatrix.Translate(GfxMatrix.Identity, tm.x1 - tm.x0, tm.y1 - tm.y0, tm.mat)
END
ELSIF (1-tol <= x0) & (x0 <= 1+tol) THEN
IF (-tol <= y0) & (y0 <= tol) THEN
GfxMatrix.ScaleAt(GfxMatrix.Identity, llx, ury, (tm.x1 - llx)/(tm.x0 - llx), (tm.y1 - ury)/(tm.y0 - ury), tm.mat)
ELSIF (1-tol <= y0) & (y0 <= 1+tol) THEN
GfxMatrix.ScaleAt(GfxMatrix.Identity, llx, lly, (tm.x1 - llx)/(tm.x0 - llx), (tm.y1 - lly)/(tm.y0 - lly), tm.mat)
ELSIF (0.5-tol <= y0) & (y0 <= 0.5+tol) THEN
GfxMatrix.ScaleAt(GfxMatrix.Identity, llx, 0.5*(lly + ury), (tm.x1 - llx)/(tm.x0 - llx), 1, tm.mat)
ELSE
GfxMatrix.Translate(GfxMatrix.Identity, tm.x1 - tm.x0, tm.y1 - tm.y0, tm.mat)
END
ELSIF (0.5-tol <= x0) & (x0 <= 0.5+tol) THEN
IF (-tol <= y0) & (y0 <= tol) THEN
GfxMatrix.ScaleAt(GfxMatrix.Identity, 0.5*(llx + urx), ury, 1, (tm.y1 - ury)/(tm.y0 - ury), tm.mat)
ELSIF (1-tol <= y0) & (y0 <= 1+tol) THEN
GfxMatrix.ScaleAt(GfxMatrix.Identity, 0.5*(llx + urx), lly, 1, (tm.y1 - lly)/(tm.y0 - lly), tm.mat)
ELSE
GfxMatrix.Translate(GfxMatrix.Identity, tm.x1 - tm.x0, tm.y1 - tm.y0, tm.mat)
END
ELSE GfxMatrix.Translate(GfxMatrix.Identity, tm.x1 - tm.x0, tm.y1 - tm.y0, tm.mat)
END
END
END SuggestHandleTrafo;
PROCEDURE ComputeTrafo* (fig: Figure; shapes: Shape; VAR mat: GfxMatrix.Matrix);
VAR tm: TransformMsg; obj: Objects.Object;
BEGIN
Solver.Init;
Objects.Stamp(tm); tm.fig := fig; tm.id := Apply; tm.mat := mat;
obj := shapes;
WHILE obj # NIL DO
obj.stamp := tm.stamp; obj := obj.slink
END;
fig.handle(fig, tm);
Solver.Solve
END ComputeTrafo;
PROCEDURE DoAttrAction (fig: Figure; act: Action; stamp: LONGINT; undo: BOOLEAN);
VAR obj: Objects.Object; sm: SelectMsg;
BEGIN
obj := act(AttrAction).obj;
IF obj IS Shape THEN
Solver.ApplyAttr(obj, act(AttrAction).attr);
obj.stamp := stamp; (* make sure that new position is updated during validate phase *)
sm.fig := fig; sm.id := Set;
obj.handle(obj, sm)
END
END DoAttrAction;
PROCEDURE CommitTrafo* (fig: Figure);
VAR id: LONGINT; obj: Objects.Object; attr: Solver.AttrNode; act: AttrAction;
BEGIN
BeginCommand(fig);
id := 0; Solver.GetObject(0, obj, attr);
WHILE obj # NIL DO
NEW(act); act.obj := obj; act.attr := attr;
AddAction(fig, act, DoAttrAction);
INC(id); Solver.GetObject(id, obj, attr)
END;
EndCommand(fig)
END CommitTrafo;
PROCEDURE Transform* (fig: Figure; shapes: Shape; VAR mat: GfxMatrix.Matrix);
BEGIN
ComputeTrafo(fig, shapes, mat);
CommitTrafo(fig)
END Transform;
(**--- Shapes ---**)
(** return whether shape is present in a linked list of shapes **)
PROCEDURE InList* (shape, list: Shape): BOOLEAN;
VAR obj: Objects.Object;
BEGIN
obj := list;
WHILE obj # NIL DO
IF shape = obj THEN RETURN TRUE END;
obj := obj.slink
END;
RETURN FALSE
END InList;
(** update figure within shape box **)
PROCEDURE UpdateShapeRect* (shape: Shape; fig: Figure; VAR mat: GfxMatrix.Matrix);
VAR llx, lly, urx, ury, bw: REAL;
BEGIN
GfxMatrix.ApplyToRect(mat, shape.llx, shape.lly, shape.urx, shape.ury, llx, lly, urx, ury);
GfxMatrix.ApplyToDist(mat, shape.bw, bw);
UpdateRect(fig, llx, lly, urx, ury, bw)
END UpdateShapeRect;
(** copy shape contents **)
PROCEDURE CopyShape* (VAR msg: Objects.CopyMsg; from, to: Shape);
BEGIN
to.handle := from.handle;
to.llx := from.llx; to.lly := from.lly; to.urx := from.urx; to.ury := from.ury;
to.bw := from.bw
END CopyShape;
(** default message handler for shapes **)
PROCEDURE HandleShape* (obj: Objects.Object; VAR msg: Objects.ObjMsg);
VAR shape: Shape; llx, lly, urx, ury, bw, rdummy: REAL; ver: LONGINT; idummy: INTEGER;
BEGIN
shape := obj(Shape);
IF msg IS ShapeMsg THEN
WITH msg: ShapeMsg DO
IF msg IS SelectMsg THEN
WITH msg: SelectMsg DO
IF (msg.id = Get) & shape.sel & (msg.stamp # shape.stamp) THEN
shape.slink := msg.shapes; msg.shapes := shape; shape.stamp := msg.stamp
ELSIF (msg.id = Set) & ~shape.sel THEN
shape.sel := TRUE;
GetShapeBox(shape, llx, lly, urx, ury, bw);
UpdateRect(msg.fig, llx, lly, urx, ury, bw);
Forward(shape, shape.cont, msg)
ELSIF (msg.id = Reset) & shape.sel THEN
shape.sel := FALSE;
GetShapeBox(shape, llx, lly, urx, ury, bw);
UpdateRect(msg.fig, llx, lly, urx, ury, bw)
END
END
ELSIF (msg IS ValidateMsg) & (msg.stamp = shape.stamp) THEN
WITH msg: ValidateMsg DO
UpdateShapeRect(shape, msg.fig, msg.mat);
IF shape.cont # NIL THEN shape.cont.stamp := msg.stamp END
END
ELSIF (msg IS TransformMsg) & (msg(TransformMsg).id = Get) THEN
shape.cont.handle(shape.cont, msg)
END
END
ELSIF msg IS Objects.AttrMsg THEN
HandleBoolAttr(msg(Objects.AttrMsg), "Selected", shape.sel)
ELSIF msg IS Objects.BindMsg THEN
Gadgets.BindObj(shape, msg(Objects.BindMsg).lib)
ELSIF msg IS Objects.FileMsg THEN
WITH msg: Objects.FileMsg DO
IF msg.id = Objects.store THEN
Files.WriteNum(msg.R, 2)
ELSIF msg.id = Objects.load THEN
Files.ReadNum(msg.R, ver);
IF ver = 1 THEN
Files.ReadInt(msg.R, idummy); Files.ReadInt(msg.R, idummy);
Files.ReadInt(msg.R, idummy); Files.ReadInt(msg.R, idummy);
Files.ReadReal(msg.R, rdummy)
ELSIF ver = 2 THEN
(* nothing stored *)
END
END
END
END
END HandleShape;
(** initialize shape **)
PROCEDURE InitShape* (shape: Shape);
BEGIN
shape.handle := HandleShape; shape.cont := NIL; shape.sel := FALSE;
shape.llx := MAX(REAL); shape.lly := MAX(REAL); shape.urx := MIN(REAL); shape.ury := MIN(REAL);
shape.bw := 0
END InitShape;
(** copy linked list of shapes **)
PROCEDURE CopyShapes* (shapes: Shape; VAR copies: Shape);
VAR obj, cur: Objects.Object; cm: Objects.CopyMsg;
BEGIN
obj := shapes; copies := NIL;
IF obj # NIL THEN
Objects.Stamp(cm); cm.dlink := NIL; cm.id := Objects.shallow; cm.obj := NIL;
obj.handle(obj, cm);
copies := cm.obj(Shape); cur := copies;
WHILE obj.slink # NIL DO
obj := obj.slink;
cm.obj := NIL;
obj.handle(obj, cm);
cur.slink := cm.obj;
cur := cur.slink
END;
cur.slink := NIL
END
END CopyShapes;
(**--- References ---**)
(** build list of references from linked list of shapes **)
PROCEDURE MakeRefs* (shapes: Shape; VAR first, last: Reference);
VAR obj: Objects.Object;
BEGIN
IF shapes = NIL THEN
first := NIL; last := NIL
ELSE
NEW(first); last := first; last.shape := shapes;
obj := shapes.slink;
WHILE obj # NIL DO
NEW(last.next); last.next.prev := last; last := last.next;
last.shape := obj(Shape);
obj := obj.slink
END
END
END MakeRefs;
(** store list of references to file **)
PROCEDURE WriteRefs* (VAR r: Files.Rider; lib: Objects.Library; first: Reference);
BEGIN
WHILE first # NIL DO
Gadgets.WriteRef(r, lib, first.shape);
first := first.next
END;
Gadgets.WriteRef(r, lib, NIL)
END WriteRefs;
(** load list of references from file **)
PROCEDURE ReadRefs* (VAR r: Files.Rider; lib: Objects.Library; VAR first, last: Reference);
VAR obj: Objects.Object; cur: Reference;
BEGIN
first := NIL; last := NIL;
Gadgets.ReadRef(r, lib, obj);
IF obj # NIL THEN
NEW(first); cur := first;
REPEAT
IF (obj # NIL) & (obj IS Shape) THEN cur.shape := obj(Shape) ELSE cur.shape := Dummy END;
Gadgets.ReadRef(r, lib, obj);
IF obj # NIL THEN
NEW(cur.next); cur.next.prev := cur;
cur := cur.next
END
UNTIL obj = NIL;
last := cur
END
END ReadRefs;
(** copy a list of shape references **)
PROCEDURE CopyRefs* (VAR msg: Objects.CopyMsg; from: Reference; VAR to, last: Reference);
VAR cm: Objects.CopyMsg; fref, tref: Reference;
BEGIN
to := NIL; last := NIL;
IF from # NIL THEN
cm.stamp := msg.stamp; cm.dlink := NIL; cm.id := msg.id;
fref := from;
NEW(to); tref := to;
LOOP
fref.shape.handle(fref.shape, cm);
tref.shape := cm.obj(Shape);
fref := fref.next;
IF fref = NIL THEN EXIT END;
NEW(tref.next); tref.next.prev := tref;
tref := tref.next
END;
last := tref
END
END CopyRefs;
(** forward message to list of shape references, starting with first reference **)
PROCEDURE HandleRefs* (cont: Container; VAR msg: Objects.ObjMsg);
VAR clink, mlink: Objects.Object; ref: Reference;
BEGIN
clink := cont.dlink; mlink := msg.dlink;
cont.dlink := mlink; msg.dlink := cont;
ref := cont.first;
WHILE ref # NIL DO
ref.shape.handle(ref.shape, msg);
ref := ref.next
END;
cont.dlink := clink; msg.dlink := mlink
END HandleRefs;
(**--- Containers ---**)
PROCEDURE ValidateContainer* (cont: Container);
VAR ref: Reference; s: Shape;
BEGIN
cont.llx := MAX(REAL); cont.lly := MAX(REAL); cont.urx := MIN(REAL); cont.ury := MIN(REAL); cont.bw := 0;
ref := cont.first;
WHILE ref # NIL DO
s := ref.shape;
IF s.llx < cont.llx THEN cont.llx := s.llx END;
IF s.lly < cont.lly THEN cont.lly := s.lly END;
IF s.urx > cont.urx THEN cont.urx := s.urx END;
IF s.ury > cont.ury THEN cont.ury := s.ury END;
IF s.bw > cont.bw THEN cont.bw := s.bw END;
ref := ref.next
END
END ValidateContainer;
PROCEDURE SelectContainer (cont: Container; VAR sm: SelectMsg);
VAR shapes: Shape; ref: Reference;
BEGIN
IF (sm.id = Get) & cont.sel & (sm.stamp # cont.stamp) THEN
cont.stamp := sm.stamp;
shapes := sm.shapes;
ref := cont.last;
WHILE ref # NIL DO (* handle shapes top-to-bottom to get correct order *)
Forward(cont, ref.shape, sm);
ref := ref.prev
END;
IF sm.shapes = shapes THEN (* no component is selected *)
cont.slink := shapes; sm.shapes := cont
END
ELSIF (sm.id = Reset) & cont.sel THEN
HandleRefs(cont, sm);
HandleShape(cont, sm)
ELSE
HandleShape(cont, sm)
END
END SelectContainer;
PROCEDURE LocateContainer (cont: Container; VAR lm: LocateMsg);
VAR shapes: Shape;
BEGIN
IF (lm.llx <= cont.urx) & (cont.llx <= lm.urx) & (lm.lly <= cont.ury) & (cont.lly <= lm.ury) & (lm.stamp # cont.stamp) THEN
cont.stamp := lm.stamp;
IF lm.id = Inside THEN
IF (lm.llx <= cont.llx) & (cont.urx <= lm.urx) & (lm.lly <= cont.lly) & (cont.ury <= lm.ury) THEN
cont.slink := lm.shapes; lm.shapes := cont
ELSE
HandleRefs(cont, lm)
END
ELSIF lm.id = Overlap THEN
shapes := lm.shapes;
HandleRefs(cont, lm);
IF (lm.shapes # shapes) & ~cont.sel THEN (* can only locate components if container is selected *)
lm.shapes := cont; cont.slink := NIL
END
ELSIF lm.id = Project THEN
ProjectToHandles(cont, lm, cont.llx, cont.lly, cont.urx, cont.ury);
HandleRefs(cont, lm)
END
ELSE
HandleRefs(cont, lm)
END
END LocateContainer;
PROCEDURE DrawContainer (cont: Container; VAR dm: DrawMsg);
BEGIN
IF ~dm.all OR (dm.stamp # cont.stamp) THEN
IF dm.all THEN cont.stamp := dm.stamp END;
IF (dm.llx <= cont.urx + cont.bw) & (cont.llx - cont.bw <= dm.urx) &
(dm.lly <= cont.ury + cont.bw) & (cont.lly - cont.bw <= dm.ury)
THEN
IF (dm.id = Full) & cont.sel & (dm.stamp = cont.stamp) THEN
DrawHandles(dm, cont.llx, cont.lly, cont.urx, cont.ury)
END;
HandleRefs(cont, dm)
END
END
END DrawContainer;
PROCEDURE DoControlAction* (fig: Figure; act: Action; stamp: LONGINT; undo: BOOLEAN);
VAR ctrl: ControlAction; ref: Reference; sm: SelectMsg; llx, lly, urx, ury, bw: REAL;
BEGIN
ctrl := act(ControlAction);
IF ~undo & (ctrl.id = Consume) OR undo & (ctrl.id = Remove) THEN
ctrl.first.prev := ctrl.prev; ctrl.last.next := ctrl.next;
IF ctrl.prev # NIL THEN ctrl.prev.next := ctrl.first ELSE ctrl.cont.first := ctrl.first END;
IF ctrl.next # NIL THEN ctrl.next.prev := ctrl.last ELSE ctrl.cont.last := ctrl.last END;
ref := ctrl.first; sm.fig := fig; sm.id := Set;
REPEAT
ref.shape.cont := ctrl.cont; ref.shape.stamp := stamp;
ref.shape.handle(ref.shape, sm);
ref := ref.next
UNTIL ref = ctrl.next
ELSIF ~undo & (ctrl.id = Remove) OR undo & (ctrl.id = Consume) THEN
ctrl.first.prev := NIL; ctrl.last.next := NIL;
IF ctrl.prev # NIL THEN ctrl.prev.next := ctrl.next ELSE ctrl.cont.first := ctrl.next END;
IF ctrl.next # NIL THEN ctrl.next.prev := ctrl.prev ELSE ctrl.cont.last := ctrl.prev END;
ref := ctrl.first; sm.fig := fig; sm.id := Reset;
REPEAT
IF ref.shape.cont # NIL THEN (* may be NIL if already removed by previous action *)
IF ref.shape.sel THEN
ref.shape.handle(ref.shape, sm)
ELSE
GetShapeBox(ref.shape, llx, lly, urx, ury, bw);
UpdateRect(fig, llx, lly, urx, ury, bw)
END
END;
ref.shape.cont := NIL; ref := ref.next
UNTIL ref = NIL;
sm.fig := fig; sm.id := Set;
ctrl.cont.handle(ctrl.cont, sm);
ctrl.cont.stamp := stamp
END
END DoControlAction;
PROCEDURE InitControlAction* (act: ControlAction; id: INTEGER; prev, first, last, next: Reference; cont: Container);
BEGIN
act.id := id;
act.prev := prev; act.first := first; act.last := last; act.next := next;
act.cont := cont
END InitControlAction;
PROCEDURE AddControlAction (fig: Figure; id: INTEGER; prev, first, last, next: Reference; cont: Container);
VAR act: ControlAction;
BEGIN
NEW(act); InitControlAction(act, id, prev, first, last, next, cont);
AddAction(fig, act, DoControlAction)
END AddControlAction;
PROCEDURE ControlContainer (cont: Container; VAR cm: ControlMsg);
VAR first, last, ref: Reference;
BEGIN
IF cm.id = Consume THEN
IF cont = cm.dest THEN
MakeRefs(cm.shapes, first, last);
ref := first;
WHILE (ref # NIL) & (ref.shape.cont = NIL) DO ref := ref.next END;
IF ref # NIL THEN