forked from groehner/JavaEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUTypes.pas
More file actions
3317 lines (3020 loc) · 85.6 KB
/
UTypes.pas
File metadata and controls
3317 lines (3020 loc) · 85.6 KB
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
{ -------------------------------------------------------------------------------
Unit: UTypes
Author: Gerhard Röhner
Based on: NSD-Editor by Marcel Kalt
Date: August 2013
Purpose: elements of a structogram
------------------------------------------------------------------------------- }
unit UTypes;
interface
uses
Windows,
Classes,
Graphics,
ExtCtrls,
Forms;
const
LEFT_RIGHT = 10; { Default Left/Right margin of element }
var
CF_NSDDATA: Word; { ClipBoard-Format }
type
TStringListReader = class
private
FARect: TRect;
FIndentAsInt: Integer;
FKey: string;
FKind: Byte;
FNum: Integer;
FPoint: TPoint;
FStringList: TStringList;
FText: string;
FVal: string;
function NextLineIndent: Integer;
function GetKind(const Kind: string): Byte;
public
constructor Create(const Filename: string);
destructor Destroy; override;
function HasOldFormat: Boolean;
procedure LineBack;
procedure ReadLine;
property ARect: TRect read FARect;
property IndentAsInt: Integer read FIndentAsInt;
property Key: string read FKey;
property Kind: Byte read FKind;
property Num: Integer read FNum;
property Point: TPoint read FPoint;
property StringList: TStringList read FStringList;
property Text: string read FText;
property Val: string read FVal;
end;
TStrType = (nsAlgorithm, nsStatement, nsIf, nsWhile, nsDoWhile, nsFor,
nsSwitch, nsSubProgram, nsListHead, nsCase, nsBreak, nsList);
TStrList = class;
TStrElement = class
private
FKind: Byte;
FList: TStrList;
FNext: TStrElement;
FPrev: TStrElement;
FRct: TRect;
FText: string;
FTextPos: TPoint;
procedure Draw; virtual;
procedure DrawRightBottom; virtual;
procedure DrawLeftBottom; virtual;
procedure DrawCircle(XPos, YPos, Height: Integer);
procedure DrawLines(XPos, YPos, LineHeight: Integer);
procedure DrawLinesRight(XPos, YPos, LineHeight: Integer);
procedure DrawCenteredLines(Center, YPos: Integer; Percent: Real);
procedure DrawContent(XPos, YPos: Integer); virtual;
procedure MoveRight(Width: Integer); virtual;
procedure MoveRightList(Width: Integer); virtual;
procedure MoveDown(Height: Integer); virtual;
procedure MoveDownList(Height: Integer); virtual;
procedure Resize(XPos, YPos: Integer); virtual;
procedure SetRight(Right: Integer); virtual;
procedure SetBottomList(Bottom: Integer); virtual;
procedure SetBottom(Bottom: Integer); virtual;
procedure LoadFromStream(Stream: TStream; Version: Byte); virtual;
procedure LoadFromReader(Reader: TStringListReader); virtual;
procedure SaveToStream(Stream: TStream); virtual;
function GetText(const Indent: string): string; virtual;
procedure RectToStream(Stream: TStream; Rect: TRect);
function GetRectPosAsText(Rect: TRect; Point: TPoint): string;
function RectFromStream(Stream: TStream): TRect;
procedure IntegerToStream(Stream: TStream; Int: Integer);
function IntegerFromStream(Stream: TStream): Integer;
procedure StringToStream(Stream: TStream; const Str: string);
function StringFromStream(Stream: TStream): string;
function AppendToClipboard: string; virtual;
procedure CreateFromClipboard(var ClipboardStr: string); virtual;
procedure SetRctList(X1Pos, Y1Pos, X2Pos, Y2Pos: Integer); virtual;
procedure SetList(List: TStrList); virtual;
public
constructor Create(List: TStrList);
function GetHeadHeight: Integer; virtual;
procedure SetRct(X1Pos, Y1Pos, X2Pos, Y2Pos: Integer);
procedure ResizeList(XPos, YPos: Integer);
procedure SetRightList(Right: Integer);
function GetStatementFromKind(Kind: Byte; const List: TStrList;
Parent: TStrElement): TStrElement;
function GetStatementFromReader(Reader: TStringListReader;
const List: TStrList; Parent: TStrElement): TStrElement;
function GetLineHeight: Integer;
function GetDefaultRectWidth: Integer;
function GetLines: Integer;
function AsString: string; virtual;
function GetKind: string;
function GetMaxDelta: Integer;
procedure Debug; virtual;
procedure Debug1;
procedure Collapse; virtual;
procedure CollapseCase; virtual;
property Kind: Byte read FKind;
property List: TStrList read FList;
property Next: TStrElement read FNext write FNext;
property Prev: TStrElement read FPrev write FPrev;
property Rct: TRect read FRct write FRct;
property Text: string read FText write FText;
property TextPos: TPoint read FTextPos;
end;
TStrStatement = class(TStrElement)
constructor Create(List: TStrList);
destructor Destroy; override;
function AsString: string; override;
procedure LoadFromReader(Reader: TStringListReader); override;
procedure Debug; override;
end;
TStrIf = class(TStrElement)
private
FElseElem: TStrElement;
FThenElem: TStrElement;
FPercent: Real; // then-width to else-width
procedure Draw; override;
procedure DrawContent(XPos, YPos: Integer); override;
procedure MoveRight(Width: Integer); override;
procedure MoveDown(Height: Integer); override;
procedure Resize(XPos, YPos: Integer); override;
procedure SetRight(Right: Integer); override;
procedure SetBottom(Bottom: Integer); override;
procedure LoadFromStream(Stream: TStream; Version: Byte); override;
procedure LoadFromReader(Reader: TStringListReader); override;
procedure SaveToStream(Stream: TStream); override;
function GetText(const Indent: string): string; override;
function AppendToClipboard: string; override;
procedure CreateFromClipboard(var ClipboardStr: string); override;
procedure SetTextPos;
procedure SetList(List: TStrList); override;
public
constructor Create(List: TStrList);
constructor CreateStructogram(List: TStrList);
destructor Destroy; override;
function GetHeadHeight: Integer; override;
function AsString: string; override;
procedure Debug; override;
procedure Collapse; override;
property ElseElem: TStrElement read FElseElem;
property ThenElem: TStrElement read FThenElem;
end;
TStrWhile = class(TStrElement)
private
FDoElem: TStrElement;
procedure Draw; override;
procedure DrawContent(XPos, YPos: Integer); override;
procedure MoveRight(Width: Integer); override;
procedure MoveDown(Height: Integer); override;
procedure Resize(XPos, YPos: Integer); override;
procedure SetRight(Right: Integer); override;
procedure SetBottom(Bottom: Integer); override;
procedure LoadFromStream(Stream: TStream; Version: Byte); override;
procedure LoadFromReader(Reader: TStringListReader); override;
procedure SaveToStream(Stream: TStream); override;
function GetText(const Indent: string): string; override;
function AppendToClipboard: string; override;
procedure CreateFromClipboard(var ClipboardStr: string); override;
procedure SetList(List: TStrList); override;
public
constructor Create(List: TStrList);
constructor CreateStructogram(List: TStrList);
destructor Destroy; override;
function GetHeadHeight: Integer; override;
function AsString: string; override;
procedure Debug; override;
procedure Collapse; override;
property DoElem: TStrElement read FDoElem;
end;
TStrDoWhile = class(TStrWhile)
private
procedure Draw; override;
procedure DrawContent(XPos, YPos: Integer); override;
procedure Resize(XPos, YPos: Integer); override;
public
constructor Create(List: TStrList);
constructor CreateStructogram(List: TStrList);
procedure SetBottom(Bottom: Integer); override;
function GetHeadHeight: Integer; override;
function AsString: string; override;
procedure Debug; override;
end;
TStrFor = class(TStrWhile)
public
constructor Create(List: TStrList);
constructor CreateStructogram(List: TStrList);
function AsString: string; override;
procedure Debug; override;
end;
TStrCase = class(TStrElement)
private
procedure Draw; override;
procedure DrawRightBottom; override;
procedure DrawLeftBottom; override;
public
constructor Create(List: TStrList);
function AsString: string; override;
procedure Debug; override;
end;
TCaseArray = array of TStrElement;
TStrSwitch = class(TStrElement)
private
FCaseElems: TCaseArray;
FPercent: Real; // then-width to else-width
procedure Draw; override;
procedure DrawContent(XPos, YPos: Integer); override;
procedure MoveRight(Width: Integer); override;
procedure MoveDown(Height: Integer); override;
procedure Resize(XPos, YPos: Integer); override;
procedure SetRight(Right: Integer); override;
procedure SetBottom(Bottom: Integer); override;
procedure LoadFromStream(Stream: TStream; Version: Byte); override;
procedure LoadFromReader(Reader: TStringListReader); override;
procedure SaveToStream(Stream: TStream); override;
function GetText(const Indent: string): string; override;
function AppendToClipboard: string; override;
procedure CreateFromClipboard(var ClipboardStr: string); override;
procedure SetTextPos;
procedure SetList(List: TStrList); override;
procedure AdjustCaseElems(Count: Integer);
public
constructor Create(List: TStrList);
constructor CreateStructogram(List: TStrList);
destructor Destroy; override;
function GetHeadHeight: Integer; override;
function AsString: string; override;
procedure Debug; override;
procedure Collapse; override;
property CaseElems: TCaseArray read FCaseElems write FCaseElems;
end;
TStrSubprogram = class(TStrElement)
private
procedure Draw; override;
procedure Resize(XPos, YPos: Integer); override;
function GetText(const Indent: string): string; override;
public
constructor Create(List: TStrList);
function GetHeadHeight: Integer; override;
function AsString: string; override;
procedure Debug; override;
end;
TStrBreak = class(TStrElement)
private
procedure Draw; override;
procedure Resize(XPos, YPos: Integer); override;
function GetText(const Indent: string): string; override;
public
constructor Create(List: TStrList);
function GetHeadHeight: Integer; override;
function AsString: string; override;
procedure Debug; override;
end;
// head of a FList of statements as part of a statement, used in in If/While/Do/Switch...
TStrListHead = class(TStrElement)
private
FParent: TStrElement;
procedure SetRctList(X1Pos, Y1Pos, X2Pos, Y2Pos: Integer); override;
procedure SetList(List: TStrList); override;
public
constructor Create(List: TStrList; Parent: TStrElement);
constructor CreateStructogram(List: TStrList; Parent: TStrElement);
procedure Resize(XPos, YPos: Integer); override;
destructor Destroy; override;
function AsString: string; override;
procedure Debug; override;
procedure Collapse; override;
procedure CollapseCase; override;
property Parent: TStrElement read FParent;
end;
TListImage = class;
// a whole structure without Algorithm
TStrList = class(TStrElement)
private
FCanvas: TCanvas;
FDirty: Boolean;
FDontMove: Boolean;
FLineHeight: Integer;
FListImage: TListImage;
FLoadError: Boolean;
FPuzzleMode: Integer;
FRctList: TRect;
FSwitchWithCaseLine: Boolean;
procedure PaintShadow;
procedure Draw; override;
procedure SetColors(BlackAndWhite: Boolean);
procedure Resize(XPos, YPos: Integer); override;
procedure SetRctList(X1Pos, Y1Pos, X2Pos, Y2Pos: Integer); override;
public
constructor Create(ScrollBox: TScrollBox; Mode: Integer; Font: TFont);
destructor Destroy; override;
procedure Paint(BlackAndWhite: Boolean = False);
procedure Print;
procedure DrawTo(Canvas: TCanvas);
procedure ResizeAll;
procedure LoadFromStream(Stream: TStream; Version: Byte); override;
procedure LoadFromReader(Reader: TStringListReader); override;
procedure SaveToStream(Stream: TStream); override;
function GetText(const Indent: string): string; override;
function GetRectPos: string;
procedure DeleteElem(Elem: TStrElement);
procedure Insert(Position, Elem: TStrElement);
function GetWidth: Integer;
function GetHeight: Integer;
procedure GetWidthHeigthOfText(const Text: string;
var Width, Height: Integer);
function GetWidthOfOneLine(const Text: string): Integer;
function GetWidthOfLines(const Text: string): Integer;
procedure SetLineHeight;
function AsString: string; override;
procedure Debug; override;
procedure SetList(List: TStrList); override;
procedure SetPuzzleMode(Mode: Integer);
procedure SetFont(Font: TFont);
procedure Collapse; override;
property Canvas: TCanvas read FCanvas;
property Dirty: Boolean read FDirty write FDirty;
property DontMove: Boolean read FDontMove write FDontMove;
property LineHeight: Integer read FLineHeight;
property ListImage: TListImage read FListImage;
property LoadError: Boolean read FLoadError;
property PuzzleMode: Integer read FPuzzleMode;
property RctList: TRect read FRctList write FRctList;
property SwitchWithCaseLine: Boolean read FSwitchWithCaseLine
write FSwitchWithCaseLine;
end;
// a whole structure, inherited form TStrList
TStrAlgorithm = class(TStrList)
private
procedure Resize(XPos, YPos: Integer); override;
public
constructor Create(ScrollBox: TScrollBox; Mode: Integer; Font: TFont);
function GetAlgorithmName: string;
function AsString: string; override;
procedure Debug; override;
end;
TListImage = class(TImage)
private
FStrList: TStrList;
public
// the constructor in TImage is not overload
constructor Create(ScrollBox: TScrollBox; List: TStrList); reintroduce;
property StrList: TStrList read FStrList;
end;
implementation
uses
Math,
Types,
Themes,
UITypes,
SysUtils,
Printers,
UUtils,
UConfiguration,
UMessages;
const
DO_LEFT = 22; { Default Left margin of do-element }
DO_BREAK = 15;
DO_SUB = 16;
SEP = #254; { Separator of commands }
TERM = #255; { Terminator of commands }
RECT_WIDTH = 75; { Minimal width of a rectangle }
TOP_BOTTOM = 4; { Default top/bottom margin of element }
constructor TStringListReader.Create(const Filename: string);
begin
FNum := 0;
FStringList := TStringList.Create;
try
FStringList.LoadFromFile(Filename);
except
on E: Exception do
ErrorMsg(E.Message);
end;
end;
function TStringListReader.HasOldFormat: Boolean;
begin
Result := (FStringList.Count > 0) and (FStringList[0] <> 'JSG: true');
end;
function TStringListReader.NextLineIndent: Integer;
var
Str: string;
Posi: Integer;
begin
if FNum < FStringList.Count - 1 then
begin
Str := FStringList[FNum + 1];
Posi := 1;
if Str <> '' then
while Str[Posi] = ' ' do
Inc(Posi);
Result := Posi - 1;
end
else
Result := 0;
end;
procedure TStringListReader.ReadLine;
var
Line: string;
Pos1, Pos2: Integer;
function getInt: Integer;
var
Pos1: Integer;
begin
Pos1 := Pos(' ', FVal);
if Pos1 > 1 then
begin
Result := StrToInt(Copy(FVal, 1, Pos1 - 1));
Delete(FVal, 1, Pos1);
end
else
Result := 0;
end;
begin
FKind := Ord(nsStatement);
FText := '';
FARect := Rect(0, 0, 0, 0);
FPoint.X := 0;
FPoint.Y := 0;
Line := '';
FIndentAsInt := 0;
FKey := '';
FVal := '';
Inc(FNum);
while (FNum < FStringList.Count) and (Trim(FStringList[FNum]) = '') do
Inc(FNum);
if FNum < FStringList.Count then
begin
Line := FStringList[FNum];
Pos2 := 1;
if Line <> '' then
while Line[Pos2] = ' ' do
Inc(Pos2);
FIndentAsInt := Pos2 - 1;
Line := UnHideCrLf(Trim(Line));
end;
Pos1 := Length(Line);
while (Pos1 > 0) and (Line[Pos1] <> '|') do
Dec(Pos1);
if Pos1 > 0 then
begin
FVal := Copy(Line, Pos1 + 1, Length(Line)) + ' ';
FARect.Left := getInt;
FARect.Top := getInt;
FARect.Right := getInt;
FARect.Bottom := getInt;
FPoint.X := getInt;
FPoint.Y := getInt;
end;
FText := Copy(Line, 1, Pos1 - 1);
if FText = '' then
begin
Pos2 := Pos(':', Line);
if Pos2 > 0 then
begin
FKey := Copy(Line, 1, Pos2 - 1);
FVal := Copy(Line, Pos2 + 2, Length(Line));
if (FKey <> '') and (FVal = '') then
FKind := GetKind(FKey);
end;
end
else if FText[1] = '|' then
Delete(FText, 1, 1);
end;
function TStringListReader.GetKind(const Kind: string): Byte;
begin
if Kind = 'Algorithm' then
Result := Ord(nsAlgorithm)
else if Kind = 'if' then
Result := Ord(nsIf)
else if Kind = 'while' then
Result := Ord(nsWhile)
else if Kind = 'do while' then
Result := Ord(nsDoWhile)
else if Kind = 'for' then
Result := Ord(nsFor)
else if Kind = 'switch' then
Result := Ord(nsSwitch)
else if Kind = 'subprogram' then
Result := Ord(nsSubProgram)
else if Kind = 'list head' then
Result := Ord(nsListHead)
else if Kind = 'case' then
Result := Ord(nsCase)
else if Kind = 'break' then
Result := Ord(nsBreak)
else if Kind = 'List' then
Result := Ord(nsList)
else
Result := Ord(nsStatement);
end;
procedure TStringListReader.LineBack;
begin
Dec(FNum);
end;
destructor TStringListReader.Destroy;
begin
FreeAndNil(FStringList);
inherited;
end;
(* ******************************************************************************* *)
(* ELEMENT *)
(* ******************************************************************************* *)
constructor TStrElement.Create(List: TStrList);
begin
FKind := 0;
FText := '';
FNext := nil;
FPrev := nil;
Self.FList := List;
end;
procedure TStrElement.DrawCircle(XPos, YPos, Height: Integer);
begin
with FList.Canvas do
begin
Ellipse(XPos - Height, YPos - Height, XPos + Height, YPos + Height);
MoveTo(XPos - Height, YPos + Height);
LineTo(XPos + Height, YPos - Height);
end;
end;
procedure TStrElement.DrawLines(XPos, YPos, LineHeight: Integer);
var
Str, Str1: string;
Posi: Integer;
BrushColor: TColor;
begin
BrushColor := FList.Canvas.Brush.Color;
FList.Canvas.Brush.Style := bsClear;
Str := FText;
Posi := Pos(#13#10, Str);
while Posi > 0 do
begin
Str1 := Copy(Str, 1, Posi - 1);
Delete(Str, 1, Posi + 1);
FList.Canvas.TextOut(XPos, YPos, Str1);
YPos := YPos + LineHeight;
Posi := Pos(#13#10, Str);
end;
if Str <> '' then
FList.Canvas.TextOut(XPos, YPos, Str);
FList.Canvas.Brush.Style := bsSolid;
FList.Canvas.Brush.Color := BrushColor;
end;
procedure TStrElement.DrawLinesRight(XPos, YPos, LineHeight: Integer);
var
Str, Str1: string;
Posi: Integer;
BrushColor: TColor;
begin
BrushColor := FList.Canvas.Brush.Color;
FList.Canvas.Brush.Style := bsClear;
Str := FText;
Posi := Pos(#13#10, Str);
while Posi > 0 do
begin
Str1 := Copy(Str, 1, Posi - 1);
Delete(Str, 1, Posi + 1);
XPos := FRct.Right - FList.Canvas.TextWidth(Str1) - LEFT_RIGHT div 2;
FList.Canvas.TextOut(XPos, YPos, Str1);
YPos := YPos + LineHeight;
Posi := Pos(#13#10, Str);
end;
if Str <> '' then
begin
XPos := FRct.Right - FList.Canvas.TextWidth(Str) - LEFT_RIGHT div 2;
FList.Canvas.TextOut(XPos, YPos, Str);
end;
FList.Canvas.Brush.Style := bsSolid;
FList.Canvas.Brush.Color := BrushColor;
end;
procedure TStrElement.DrawCenteredLines(Center, YPos: Integer; Percent: Real);
var
Str, Str1: string;
XPos, Width, Posi: Integer;
BrushColor: TColor;
begin
FList.Canvas.Rectangle(FRct.Left, FRct.Top, FRct.Right + 1, FRct.Bottom + 1);
BrushColor := FList.Canvas.Brush.Color;
FList.Canvas.Brush.Style := bsClear;
Str := FText;
YPos := FTextPos.Y;
Posi := Pos(#13#10, Str);
while Posi > 0 do
begin
Str1 := Copy(Str, 1, Posi - 1);
Delete(Str, 1, Posi + 1);
Width := Round(Percent * FList.GetWidthOfOneLine(Str1));
XPos := Center - Width;
FList.Canvas.TextOut(XPos, YPos, Str1);
YPos := YPos + FList.LineHeight;
Posi := Pos(#13#10, Str);
end;
if Str <> '' then
begin
Width := Round(Percent * FList.GetWidthOfOneLine(Str));
XPos := Center - Width;
FList.Canvas.TextOut(XPos, YPos, Str);
end;
FList.Canvas.Brush.Style := bsSolid;
FList.Canvas.Brush.Color := BrushColor;
end;
procedure TStrElement.DrawContent(XPos, YPos: Integer);
begin
if FText = '' then
begin
XPos := (FRct.Left + FRct.Right + 1) div 2;
YPos := (FRct.Top + FRct.Bottom + 1) div 2;
DrawCircle(XPos, YPos, (GetLineHeight - 10) div 2);
end
else
DrawLines(XPos, YPos, FList.LineHeight);
end;
procedure TStrElement.Draw;
begin
FList.Canvas.Rectangle(FRct.Left, FRct.Top, FRct.Right + 1, FRct.Bottom + 1);
DrawContent(FTextPos.X, FTextPos.Y);
end;
procedure TStrElement.DrawRightBottom;
begin
end;
procedure TStrElement.DrawLeftBottom;
begin
end;
procedure TStrElement.MoveRight(Width: Integer);
begin
FRct.Left := FRct.Left + Width;
FRct.Right := FRct.Right + Width;
FTextPos.X := FTextPos.X + Width;
end;
procedure TStrElement.MoveDown(Height: Integer);
begin
FRct.Top := FRct.Top + Height;
FRct.Bottom := FRct.Bottom + Height;
FTextPos.Y := FTextPos.Y + Height;
end;
procedure TStrElement.MoveRightList(Width: Integer);
begin
MoveRight(Width);
var
Tmp := FNext;
while Assigned(Tmp) do
begin
Tmp.MoveRight(Width);
Tmp := Tmp.FNext;
end;
end;
procedure TStrElement.MoveDownList(Height: Integer);
begin
MoveDown(Height);
var
Tmp := FNext;
while Assigned(Tmp) do
begin
Tmp.MoveDown(Height);
Tmp := Tmp.FNext;
end;
end;
procedure TStrElement.Resize(XPos, YPos: Integer);
var
Width, Height: Integer;
begin
FList.GetWidthHeigthOfText(FText, Width, Height);
if FList.PuzzleMode = 1 then
SetRct(XPos, YPos, XPos + FRct.Right - FRct.Left,
YPos + FRct.Bottom - FRct.Top)
else if FList.PuzzleMode = 2 then
SetRct(XPos, YPos, XPos + FRct.Right - FRct.Left, YPos + Height)
else
SetRct(XPos, YPos, XPos + Width, YPos + Height);
end;
procedure TStrElement.SetRight(Right: Integer);
begin
FRct.Right := Right;
end;
procedure TStrElement.SetBottomList(Bottom: Integer);
begin
SetBottom(Bottom);
var
Tmp := FNext;
if Assigned(Tmp) then
begin
while Assigned(Tmp.FNext) do
Tmp := Tmp.FNext;
Tmp.SetBottom(Bottom);
end;
end;
procedure TStrElement.SetBottom(Bottom: Integer);
begin
FRct.Bottom := Bottom;
end;
procedure TStrElement.ResizeList(XPos, YPos: Integer);
var
X1Pos, Y1Pos: Integer;
Tmp: TStrElement;
AKind: Byte;
begin
// calculate width
Resize(XPos, YPos); // Listhead or StrList
AKind := Ord(nsAlgorithm);
if FKind = AKind then
X1Pos := GetDefaultRectWidth + FList.Canvas.Font.Size + 2 * LEFT_RIGHT
else
X1Pos := Self.FRct.Right;
Y1Pos := Self.FRct.Bottom;
Tmp := FNext;
while Assigned(Tmp) do
begin
Tmp.Resize(XPos, Y1Pos);
X1Pos := Max(X1Pos, Tmp.FRct.Right);
Y1Pos := Tmp.FRct.Bottom;
Tmp := Tmp.FNext;
end;
// set width
SetRightList(X1Pos);
SetRctList(XPos, YPos, X1Pos, Y1Pos);
end;
procedure TStrElement.SetRightList(Right: Integer);
begin
SetRight(Right);
var
Tmp := FNext;
while Assigned(Tmp) do
begin
Tmp.SetRight(Right);
Tmp := Tmp.FNext;
end;
FRct.Right := Right;
end;
function TStrElement.GetLineHeight: Integer;
begin
Result := FList.LineHeight;
end;
function TStrElement.GetDefaultRectWidth: Integer;
begin
Result := Round(RECT_WIDTH * (FList.Canvas.Font.Size / 12.0));
end;
function TStrElement.GetLines: Integer;
var
Posi, Num: Integer;
Str: string;
begin
Num := 0;
Str := FText;
Posi := Pos(#13#10, Str);
while Posi > 0 do
begin
Delete(Str, 1, Posi + 1);
Inc(Num);
Posi := Pos(#13#10, Str);
end;
if Str <> '' then
Inc(Num);
if Num = 0 then
Num := 1;
Result := Num;
end;
function TStrElement.GetHeadHeight: Integer;
begin
Result := GetLineHeight;
end;
procedure TStrElement.LoadFromStream(Stream: TStream; Version: Byte);
var
Bye: Byte;
Str: ShortString;
begin
if Version >= $0E then
begin
FText := StringFromStream(Stream);
FRct := RectFromStream(Stream);
FTextPos.X := IntegerFromStream(Stream);
FTextPos.Y := IntegerFromStream(Stream);
end
else
begin
Stream.Read(Bye, 1);
SetLength(Str, Bye);
Stream.Read(Str[1], Bye);
FText := string(Str);
end;
end;
procedure TStrElement.LoadFromReader(Reader: TStringListReader);
begin
Reader.ReadLine;
FText := Reader.FText;
FRct := Reader.FARect;
FTextPos := Reader.FPoint;
end;
procedure TStrElement.SaveToStream(Stream: TStream);
begin
Stream.Write(FKind, 1);
StringToStream(Stream, FText);
RectToStream(Stream, FRct);
IntegerToStream(Stream, FTextPos.X);
IntegerToStream(Stream, FTextPos.Y);
end;
function TStrElement.GetText(const Indent: string): string;
begin
var
Str := HideCrLf(FText);
if (Str <> '') and (Str[1] = ' ') then
Str := '|' + Str;
Result := Indent + Str + GetRectPosAsText(FRct, FTextPos) + CrLf;
end;
procedure TStrElement.RectToStream(Stream: TStream; Rect: TRect);
begin
IntegerToStream(Stream, Rect.Left);
IntegerToStream(Stream, Rect.Top);
IntegerToStream(Stream, Rect.Right);
IntegerToStream(Stream, Rect.Bottom);
end;
function TStrElement.GetRectPosAsText(Rect: TRect; Point: TPoint): string;
begin
Result := '|' + IntToStr(Rect.Left) + ' ' + IntToStr(Rect.Top) + ' ' +
IntToStr(Rect.Right) + ' ' + IntToStr(Rect.Bottom) + ' ' + IntToStr(Point.X)
+ ' ' + IntToStr(Point.Y);
end;
function TStrElement.RectFromStream(Stream: TStream): TRect;
begin
Result.Left := IntegerFromStream(Stream);
Result.Top := IntegerFromStream(Stream);
Result.Right := IntegerFromStream(Stream);
Result.Bottom := IntegerFromStream(Stream);
end;
procedure TStrElement.IntegerToStream(Stream: TStream; Int: Integer);
var
Small: SmallInt;
begin
Small := SmallInt(Int);
Stream.Write(Small, 2);
end;
function TStrElement.IntegerFromStream(Stream: TStream): Integer;
begin
Result := 0;
Stream.Read(Result, 2);
end;
procedure TStrElement.StringToStream(Stream: TStream; const Str: string);
var
Size: LongInt;
begin
Size := Length(Str) * SizeOf(Char);
Stream.Write(Size, SizeOf(Size));
Stream.Write(Pointer(Str)^, Size);
end;
function TStrElement.StringFromStream(Stream: TStream): string;
var
Size: LongInt;
begin
Stream.Read(Size, SizeOf(Size));
SetLength(Result, Size div SizeOf(Char));
Stream.Read(Pointer(Result)^, Size);
end;
function TStrElement.AppendToClipboard: string;
begin
Result := Chr(FKind) + FText + SEP;
end;
procedure TStrElement.CreateFromClipboard(var ClipboardStr: string);
var
Index: Byte;
begin
Index := Pos(SEP, ClipboardStr);
FText := Copy(ClipboardStr, 2, Index - 2);
Delete(ClipboardStr, 1, Index);
end;
procedure TStrElement.SetRct(X1Pos, Y1Pos, X2Pos, Y2Pos: Integer);
begin
FRct.Left := X1Pos;
FRct.Top := Y1Pos;
FRct.Right := X2Pos;
FRct.Bottom := Y2Pos;
FTextPos.X := FRct.Left + LEFT_RIGHT div 2;
FTextPos.Y := FRct.Top + TOP_BOTTOM div 2;
end;
procedure TStrElement.SetRctList(X1Pos, Y1Pos, X2Pos, Y2Pos: Integer);
begin
end;
procedure TStrElement.SetList(List: TStrList);
begin
Self.FList := List;
end;
function TStrElement.GetStatementFromKind(Kind: Byte; const List: TStrList;
Parent: TStrElement): TStrElement;
begin
case Kind of
Ord(nsAlgorithm):
Result := nil;
Ord(nsStatement):
Result := TStrStatement.Create(List);
Ord(nsIf):
Result := TStrIf.Create(List);
Ord(nsWhile):
Result := TStrWhile.Create(List);
Ord(nsDoWhile):
Result := TStrDoWhile.Create(List);
Ord(nsFor):
Result := TStrFor.Create(List);
Ord(nsSwitch):
Result := TStrSwitch.Create(List);
Ord(nsSubProgram):
Result := TStrSubprogram.Create(List);
Ord(nsListHead):