forked from groehner/JavaEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUStructogramform.pas
More file actions
2585 lines (2437 loc) · 74.5 KB
/
UStructogramform.pas
File metadata and controls
2585 lines (2437 loc) · 74.5 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: UStructogramForm
Author: Gerhard Röhner
Based on: NSD-Editor by Marcel Kalt
Date: August 2013
Purpose: structogram editor
------------------------------------------------------------------------------- }
unit UStructogramForm;
interface
uses
Windows,
Classes,
Controls,
ExtCtrls,
Forms,
Graphics,
ComCtrls,
Menus,
StdCtrls,
System.ImageList,
Vcl.ImgList,
Vcl.ToolWin,
Vcl.BaseImageCollection,
Vcl.VirtualImageList,
Vcl.VirtualImage,
SVGIconImageCollection,
TB2Item,
SpTBXItem,
UTypes,
UBaseForm;
type
TFStructogram = class(TFForm)
ScrollBox: TScrollBox;
PanelLeft: TPanel;
ToolBarStructogram: TToolBar;
TBClose: TToolButton;
TBStatement: TToolButton;
TBIf: TToolButton;
TBSwitch: TToolButton;
TBwhile: TToolButton;
TBDoWhile: TToolButton;
TBFor: TToolButton;
TBSubProgram: TToolButton;
TBBreak: TToolButton;
TBAlgorithm: TToolButton;
TBGenerateJava: TToolButton;
TBZoomOut: TToolButton;
TBZoomIn: TToolButton;
TBPuzzleMode: TToolButton;
icStructogram: TSVGIconImageCollection;
vilToolbarLight: TVirtualImageList;
vilToolbarDark: TVirtualImageList;
TrashImage: TVirtualImage;
vilPopupMenuLight: TVirtualImageList;
vilPopupMenuDark: TVirtualImageList;
StructoPopupMenu: TSpTBXPopupMenu;
MIAddCase: TSpTBXItem;
MISwitchWithCaseLine: TSpTBXItem;
MICopy: TSpTBXItem;
MIDelete: TSpTBXItem;
MIDeleteCase: TSpTBXItem;
SpTBXSeparatorItem1: TSpTBXSeparatorItem;
MIGenerateMethod: TSpTBXItem;
MIGenerateProgram: TSpTBXItem;
MIDataType: TSpTBXSubmenuItem;
SpTBXSeparatorItem2: TSpTBXSeparatorItem;
MICopyAsPicture: TSpTBXItem;
MIPuzzleMode: TSpTBXItem;
MIFont: TSpTBXItem;
MIConfiguration: TSpTBXItem;
MIBoolean: TSpTBXItem;
MIString: TSpTBXItem;
MIInt: TSpTBXItem;
MIFloat: TSpTBXItem;
MIDouble: TSpTBXItem;
MIChar: TSpTBXItem;
procedure FormCreate(Sender: TObject);
procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormKeyPress(Sender: TObject; var Key: Char);
procedure FormClose(Sender: TObject; var Action: TCloseAction); override;
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
// used by buttons
procedure StrElementMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ImageDblClick(Sender: TObject);
procedure ImageMouseMove(Sender: TObject; Shift: TShiftState;
XPos, YPos: Integer);
procedure ImageMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; XPos, YPos: Integer);
procedure ImageMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; XPos, YPos: Integer);
procedure ScrollBoxClick(Sender: TObject);
procedure ScrollBoxMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
procedure MICopyAsPictureClick(Sender: TObject);
procedure MIDatatypeClick(Sender: TObject);
procedure StructoPopupMenuPopup(Sender: TObject);
procedure EditMemoChange(Sender: TObject);
procedure MIGenerateMethodClick(Sender: TObject);
procedure MIGenerateProgramClick(Sender: TObject);
procedure MIFontClick(Sender: TObject);
procedure MISwitchWithCaseLineClick(Sender: TObject);
procedure MIAddCaseClick(Sender: TObject);
procedure MIDeleteCaseClick(Sender: TObject);
procedure MIDeleteClick(Sender: TObject);
procedure MIPuzzleClick(Sender: TObject);
procedure MICopyClick(Sender: TObject);
procedure BBGenerateJavaClick(Sender: TObject);
procedure BBCloseClick(Sender: TObject);
procedure BBZoomOutClick(Sender: TObject);
procedure BBZoomInClick(Sender: TObject);
procedure BBPuzzleClick(Sender: TObject);
procedure MIConfigurationClick(Sender: TObject);
private
FEditMemo: TMemo;
FOldShape: TRect;
FOldCanvas: TCanvas;
FCurElement: TStrElement;
FCurList: TStrList;
FEditMemoElement: TStrElement;
FEditMemoBeginText: string;
FCurInsert: Integer;
FIsMoving: Boolean;
FReadOnly: Boolean;
FMousePos: TPoint;
FScreenMousePos: TPoint;
FDataType: string;
FVariables: string;
FIgnoreNextMouseDown: Boolean;
FIgnoreNextMouseUp: Boolean;
FSeparating: Integer;
FPuzzleMode: Integer;
FSolution: string;
FVersion: Byte;
procedure ShowShape;
procedure HideShape;
procedure AddParameter(List: TStrList; ParamList: TStringList);
procedure AddVariable(AText: string; List: TStringList);
procedure StrElementToJava(Element: TStrElement;
PList, VariablesList: TStringList; const Indent: string);
function FindElement(Current: TStrElement; X, Y: Integer): TStrElement;
function FindVisibleElement(Current: TStrElement; X, Y: Integer)
: TStrElement;
function BeforeOrAfter(Current: TStrElement): Boolean;
function GetParentElement(Element: TStrElement): TStrElement;
function GetToken(var Str: string; var Token: string): Integer;
procedure CalculateInsertionShape(DestList, InsertList: TStrList;
YPos: Integer);
procedure InsertElement(DestList, InsertList: TStrList;
ACurElement: TStrElement);
procedure SetEvents(Image: TListImage);
procedure DoEdit(StrElement: TStrElement; const Str: string);
procedure CloseEdit;
procedure SetLeftBorderForEditMemo;
function GetAlgorithmParameter(ParamList: TStringList): string;
procedure JavaProgram(List: TStrList; ProgList: TStringList;
const AName: string);
function GetAlgorithm: TStrAlgorithm;
function GetList: TStrList;
function GetListAtScreenPos(Point: TPoint): TStrList;
function GetCurList: Boolean;
function GetCurListAndCurElement: Boolean;
function GetStructogramAsBitmap: TBitmap;
function GetName(StrList: TStrList): string;
procedure PaintAll;
function FitsIn(ACurList: TStrList; ACurElement: TStrElement): Boolean;
procedure SetPuzzleMode(Mode: Integer);
procedure MakeVeryHard;
function StringFromStream(Stream: TStream): string;
function IntegerFromStream(Stream: TStream): Integer;
protected
function LoadFromFile(const FileName: string): Boolean;
function LoadFromFileOld(const FileName: string): Boolean;
procedure SaveToFile(const FileName: string);
procedure UpdateState; override;
public
constructor Create(AOwner: TComponent); override;
procedure New;
function Open(const Pathname: string): Boolean;
procedure Save(WithBackup: Boolean); override;
procedure Print; override;
procedure GenerateFromText(const Text: string);
procedure RenewFromText(const Text: string);
function GetFormType: string; override;
function GetSaveAsName: string; override;
procedure SetOptions; override;
procedure CutToClipboard; override;
procedure CopyToClipboard; override;
procedure PasteFromClipboard; override;
procedure SetFont(AFont: TFont); override;
procedure Enter(Sender: TObject); override;
procedure DoExport; override;
procedure ChangeStyle; override;
procedure DPIChanged; override;
end;
implementation
{$R *.DFM}
uses
Messages,
Buttons,
SysUtils,
Math,
Clipbrd,
Dialogs,
Themes,
Types,
UITypes,
IOUtils,
StrUtils,
JvGnugettext,
UStringRessources,
UJava,
UConfiguration,
UUtils,
UGenerateStructogram,
UTemplates;
constructor TFStructogram.Create(AOwner: TComponent);
begin
inherited;
FormTag := 11;
end;
procedure TFStructogram.FormCreate(Sender: TObject);
begin
TranslateComponent(Self);
ToMainPanel;
FOldShape := Rect(-1, -1, -1, -1);
FEditMemo := TMemo.Create(Self);
FEditMemo.Parent := Self;
FEditMemo.SetBounds(136, 156, 99, 37);
FEditMemo.Color := StyleServices.GetSystemColor(clSkyBlue);
FEditMemo.OnChange := EditMemoChange;
FEditMemo.Visible := False;
FEditMemo.WordWrap := False;
FEditMemo.BevelInner := bvNone;
OnMouseActivate := FormMouseActivate;
Font.Assign(FConfiguration.StructogramFont);
ScrollBox.DoubleBuffered := True;
FSeparating := 0;
FPuzzleMode := 0;
TBPuzzleMode.Visible := False;
ToolBarStructogram.Height := 308 - 22;
FVersion := $0E;
FCurList := nil;
FCurElement := nil;
UpdateState;
SetOptions;
end;
procedure TFStructogram.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
if Modified and not AlreadySavedAs then
begin
FJava.DoSave(Self, True);
AlreadySavedAs := True;
end;
CanClose := True;
end;
procedure TFStructogram.FormClose(Sender: TObject; var Action: TCloseAction);
begin
inherited;
for var I := ScrollBox.ControlCount - 1 downto 0 do
TListImage(ScrollBox.Controls[I]).StrList.Destroy;
Action := caFree;
end;
procedure TFStructogram.New;
var Elem: TStrElement; StrList: TStrAlgorithm;
begin
if Pathname = '' then
Pathname := FJava.GetFilename('.jsg');
StrList := TStrAlgorithm.Create(ScrollBox, FPuzzleMode, Font);
StrList.Text := FConfiguration.Algorithm + ' ' +
ChangeFileExt(ExtractFileName(Pathname), '');
Elem := TStrStatement.Create(StrList);
StrList.Insert(StrList, Elem);
SetEvents(StrList.ListImage);
StrList.ResizeAll;
StrList.Paint;
Modified := False;
Enter(Self); // must stay!
if CanFocus then
SetFocus;
Caption := Pathname;
FJava.AddToWindowMenuAndTabBar(Number, OpenWindow, Self);
end;
procedure TFStructogram.GenerateFromText(const Text: string);
var Generator: TGenerateStructogram; StrList: TStrAlgorithm;
begin
if Pathname = '' then
Pathname := FJava.GetFilename('.jsg');
StrList := TStrAlgorithm.Create(ScrollBox, FPuzzleMode, Font);
StrList.Text := FConfiguration.Algorithm + ' ';
Generator := TGenerateStructogram.Create(True);
Generator.GenerateStructogram(Text, StrList);
FreeAndNil(Generator);
SetEvents(StrList.ListImage);
StrList.ResizeAll;
StrList.Paint;
Save(False);
Enter(Self); // must stay!
if CanFocus then
SetFocus;
Caption := Pathname;
FJava.AddToWindowMenuAndTabBar(Number, OpenWindow, Self);
end;
procedure TFStructogram.RenewFromText(const Text: string);
var Generator: TGenerateStructogram; StrList: TStrList; Algorithm: string;
begin
for var I := ScrollBox.ControlCount - 1 downto 0 do
begin
StrList := TListImage(ScrollBox.Controls[I]).StrList;
if StrList is TStrAlgorithm then
Algorithm := StrList.Text;
FreeAndNil(StrList);
end;
StrList := TStrAlgorithm.Create(ScrollBox, FPuzzleMode, Font);
StrList.Text := Algorithm;
SetEvents(StrList.ListImage);
StrList.Text := FConfiguration.Algorithm + ' ';
Generator := TGenerateStructogram.Create(True);
Generator.GenerateStructogram(Text, StrList);
FreeAndNil(Generator);
StrList.ResizeAll;
StrList.Paint;
Save(False);
Enter(Self); // must stay!
if CanFocus then
SetFocus;
end;
function TFStructogram.Open(const Pathname: string): Boolean;
begin
CloseEdit;
Result := LoadFromFile(Pathname);
if Result then
begin
Self.Pathname := Pathname;
Caption := Pathname;
FJava.AddToWindowMenuAndTabBar(Number, OpenWindow, Self);
Modified := False;
Enter(Self); // must stay!
if CanFocus then
SetFocus;
FReadOnly := IsWriteProtected(Pathname);
UpdateState;
end;
end;
procedure TFStructogram.Enter(Sender: TObject);
begin
inherited;
if ToolBarStructogram.Visible and ToolBarStructogram.CanFocus then
ToolBarStructogram.SetFocus;
end;
function TFStructogram.LoadFromFile(const FileName: string): Boolean;
var StrList: TStrList; SwitchWithCaseLine: Boolean; Reader: TStringListReader;
procedure Init(List: TStrList);
begin
List.SwitchWithCaseLine := SwitchWithCaseLine;
List.SetPuzzleMode(FPuzzleMode);
List.SetFont(Font);
List.SetLineHeight;
SetEvents(List.ListImage);
List.ResizeAll;
List.Paint;
end;
begin
Result := True;
Reader := TStringListReader.Create(FileName);
try
try
if Reader.HasOldFormat then
Result:= LoadFromFileOld(FileName)
else
begin
repeat
Reader.ReadLine;
if Reader.Key = 'FontName' then
Font.Name := Reader.Val
else if Reader.Key = 'FontSize' then
Font.Size := PPIScale(StrToInt(Reader.Val))
else if Reader.Key = 'FontColor' then
Font.Color := StrToInt(Reader.Val)
else if Reader.Key = 'FontBold' then
if Reader.Val = 'true' then
Font.Style := Font.Style + [fsBold]
else
Font.Style := Font.Style - [fsBold]
else if Reader.Key = 'FontItalic' then
if Reader.Val = 'true' then
Font.Style := Font.Style + [fsItalic]
else
Font.Style := Font.Style - [fsItalic]
else if Reader.Key = 'PuzzleMode' then
FPuzzleMode := StrToInt(Reader.Val)
else if Reader.Key = 'Solution' then
FSolution := UnHideCrLf(Reader.Val);
until (Reader.Key = '- Kind') or (Reader.Key = '');
while (Reader.Key = '- Kind') and Result do
begin
if Reader.Val = 'Algorithm' then
StrList := TStrAlgorithm.Create(ScrollBox, FPuzzleMode, Font)
else
StrList := TStrList.Create(ScrollBox, FPuzzleMode, Font);
Reader.ReadLine;
if Reader.Key = 'SwitchWithCaseLine' then
SwitchWithCaseLine := (Reader.Val = 'true')
else
Reader.LineBack;
StrList.LoadFromReader(Reader);
Init(StrList);
Result := Result and not StrList.LoadError;
Reader.ReadLine;
end;
Result := True;
end;
except
on E: Exception do
ErrorMsg(E.Message);
end;
finally
FreeAndNil(Reader);
end;
FVersion := $0F;
end;
function TFStructogram.LoadFromFileOld(const FileName: string): Boolean;
var SigName: ShortString; FontName: ShortString; Style: Byte;
Stream: TFileStream; LeftMargin, TopMargin, Zoomfactor: SmallInt; Kind: Byte;
StrList: TStrList; StructogramAligned: Boolean; SwitchWithCaseLine: Boolean;
procedure Init(List: TStrList);
begin
List.SwitchWithCaseLine := SwitchWithCaseLine;
List.ListImage.Left := LeftMargin;
List.ListImage.Top := TopMargin;
List.SetPuzzleMode(FPuzzleMode);
List.SetFont(Font);
List.SetLineHeight;
SetEvents(List.ListImage);
List.ResizeAll;
List.Paint;
end;
begin
Result := False;
Stream := TFileStream.Create(FileName, fmOpenRead);
try
try
Stream.Read(SigName, 4); { read signature }
if not((SigName = 'JSG') or (SigName = 'NSD')) then
ErrorMsg(_('Invalid file format!'))
else
begin
Stream.Read(FVersion, 1); { read FVersion }
if $0E <= FVersion then
begin
Stream.Read(FPuzzleMode, 1);
SetPuzzleMode(FPuzzleMode);
FSolution := StringFromStream(Stream);
Font.Name := StringFromStream(Stream);
if Font.Name = 'Arial' then
Font.Name := 'Segoe UI';
end;
if $0C = FVersion then
begin
Stream.Read(StructogramAligned, 1);
Stream.Read(SwitchWithCaseLine, 1);
end;
if ($0B <= FVersion) and (FVersion <= $0C) then
begin
Stream.Read(LeftMargin, 2); { read left margin }
Stream.Read(TopMargin, 2); { read top margin }
Stream.Read(Zoomfactor, 2); { read zoom factor }
end;
if ($0A <= FVersion) and (FVersion < $0E) then
begin
Stream.Read(FontName[0], 1);
Stream.Read(FontName[1], Ord(FontName[0])); { read font name }
Font.Name := string(FontName);
if Font.Name = 'Arial' then
Font.Name := 'Segoe UI';
end;
if ($0A <= FVersion) then
begin
Font.Size := IntegerFromStream(Stream);
Stream.Read(Style, 1);
Font.Style := [];
if (Style and $01) > 0 then
Font.Style := Font.Style + [fsBold];
if (Style and $02) > 0 then
Font.Style := Font.Style + [fsItalic];
Font.Color := IntegerFromStream(Stream);
if FVersion < $0D then
begin
StrList := TStrAlgorithm.Create(ScrollBox, FPuzzleMode, Font);
StrList.LoadFromStream(Stream, FVersion);
Init(StrList);
Result := Result and not StrList.LoadError;
end
else
while (Stream.Read(Kind, 1) = 1) and Result do
begin
if Kind = Byte(nsAlgorithm) then
StrList := TStrAlgorithm.Create(ScrollBox, FPuzzleMode, Font)
else
StrList := TStrList.Create(ScrollBox, FPuzzleMode, Font);
Stream.Read(SwitchWithCaseLine, 1);
Stream.Read(LeftMargin, 2);
Stream.Read(TopMargin, 2);
StrList.LoadFromStream(Stream, FVersion);
Init(StrList);
Result := Result and not StrList.LoadError;
end;
end;
Result := True;
end;
except
on E: Exception do
ErrorMsg(E.Message);
end;
finally
FreeAndNil(Stream);
end;
FVersion := $0E;
end;
procedure TFStructogram.SaveToFile(const FileName: string);
var Lines: TStringList; AList: TStrList;
begin
Lines := TStringList.Create;
try
try
Lines.Add('JSG: true');
if Font.Name = 'Arial' then
Font.Name := 'Segoe UI';
Lines.Add('FontName: ' + Font.Name);
Lines.Add('FontSize: ' + IntToStr(PPIUnScale(Font.Size)));
if fsBold in Font.Style then
Lines.Add('FontBold: true');
if fsItalic in Font.Style then
Lines.Add('FontItalic: true');
Lines.Add('FontColor: ' + IntToStr(Font.Color));
if FPuzzleMode > 0 then
begin
Lines.Add('PuzzleMode: ' + IntToStr(FPuzzleMode));
Lines.Add('Solution: ' + HideCrLf(FSolution));
end;
for var I := 0 to ScrollBox.ControlCount - 1 do
begin
AList := TListImage(ScrollBox.Controls[I]).StrList;
Lines.Add('- Kind: ' + AList.GetKind);
Lines.Add(' SwitchWithCaseLine: ' +
BoolToStr(AList.SwitchWithCaseLine, True));
Lines.Add(' RectPos' + AList.GetRectPos);
Lines.Add(AList.GetText(' '));
end;
Lines.SaveToFile(FileName, TEncoding.UTF8);
except
on E: Exception do
ErrorMsg(E.Message);
end;
finally
Lines.Free;
end;
end;
function TFStructogram.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 TFStructogram.IntegerFromStream(Stream: TStream): Integer;
begin
Stream.Read(Result, SizeOf(Integer));
end;
procedure TFStructogram.ShowShape;
begin
with FOldShape do
begin
FOldCanvas.Pen.Color := clRed;
if Top = Bottom then
begin
FOldCanvas.MoveTo(Left + 1, Top - 2);
FOldCanvas.LineTo(Right, Top - 2);
FOldCanvas.MoveTo(Left + 1, Top - 1);
FOldCanvas.LineTo(Right, Top - 1);
FOldCanvas.MoveTo(Left + 1, Top + 1);
FOldCanvas.LineTo(Right, Top + 1);
FOldCanvas.MoveTo(Left + 1, Top + 2);
FOldCanvas.LineTo(Right, Top + 2);
end
else
begin
FOldCanvas.MoveTo(Left, Top);
FOldCanvas.LineTo(Right, Top);
FOldCanvas.LineTo(Right, Bottom);
FOldCanvas.LineTo(Left, Bottom);
FOldCanvas.LineTo(Left, Top);
FOldCanvas.MoveTo(Left + 1, Top + 1);
FOldCanvas.LineTo(Right - 1, Top + 1);
FOldCanvas.LineTo(Right - 1, Bottom - 1);
FOldCanvas.LineTo(Left + 1, Bottom - 1);
FOldCanvas.LineTo(Left + 1, Top + 1);
end;
end;
end;
procedure TFStructogram.HideShape;
begin
if FOldShape.Top > -1 then
begin
with FOldShape do
begin
FOldCanvas.Pen.Color := StyleServices.GetStyleColor(scPanel);
if Top = Bottom then
begin
FOldCanvas.MoveTo(Left + 1, Top - 2);
FOldCanvas.LineTo(Right, Top - 2);
FOldCanvas.MoveTo(Left + 1, Top - 1);
FOldCanvas.LineTo(Right, Top - 1);
FOldCanvas.MoveTo(Left + 1, Top + 1);
FOldCanvas.LineTo(Right, Top + 1);
FOldCanvas.MoveTo(Left + 1, Top + 2);
FOldCanvas.LineTo(Right, Top + 2);
end
else
begin
FOldCanvas.MoveTo(Left, Top);
FOldCanvas.LineTo(Right, Top);
FOldCanvas.LineTo(Right, Bottom);
FOldCanvas.LineTo(Left, Bottom);
FOldCanvas.LineTo(Left, Top);
FOldCanvas.MoveTo(Left + 1, Top + 1);
FOldCanvas.LineTo(Right - 1, Top + 1);
FOldCanvas.LineTo(Right - 1, Bottom - 1);
FOldCanvas.LineTo(Left + 1, Bottom - 1);
FOldCanvas.LineTo(Left + 1, Top + 1);
end;
end;
FOldShape := Rect(-1, -1, -1, -1);
PaintAll;
end;
end;
// used by buttons
procedure TFStructogram.StrElementMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var StrList: TStrList; Element: TStrElement; PtScreen, PtClient: TPoint;
begin
if Button = mbLeft then
begin
CloseEdit;
if TSpeedButton(Sender).Tag = 0 then
StrList := TStrAlgorithm.Create(ScrollBox, FPuzzleMode, Font)
else
StrList := TStrList.Create(ScrollBox, FPuzzleMode, Font);
Element := nil;
case TSpeedButton(Sender).Tag of
Ord(nsAlgorithm):
begin
Element := TStrStatement.Create(StrList);
StrList.Text := FConfiguration.Algorithm + ' ';
end;
Ord(nsStatement):
Element := TStrStatement.Create(StrList);
Ord(nsIf):
Element := TStrIf.Create(StrList);
Ord(nsWhile):
begin
Element := TStrWhile.Create(StrList);
Element.Text := FConfiguration._While + ' ';
end;
Ord(nsDoWhile):
begin
Element := TStrDoWhile.Create(StrList);
Element.Text := FConfiguration.DoWhile + ' ';
end;
Ord(nsFor):
begin
Element := TStrFor.Create(StrList);
Element.Text := ReplaceStr(ReplaceStr(FConfiguration._For, '[', ''),
']', '') + ' ';
end;
Ord(nsSwitch):
Element := TStrSwitch.Create(StrList);
Ord(nsSubProgram):
Element := TStrSubprogram.Create(StrList);
Ord(nsBreak):
Element := TStrBreak.Create(StrList);
end;
StrList.Insert(StrList, Element);
StrList.SetFont(Font);
StrList.ResizeAll;
PtScreen := TToolButton(Sender).ClientToScreen(Point(X, Y));
PtClient := ToolBarStructogram.ScreenToClient(PtScreen);
StrList.ListImage.SetBounds(PtClient.X - StrList.RctList.Width div 2,
PtClient.Y - StrList.LineHeight div 2, StrList.RctList.Width,
StrList.RctList.Height);
SetEvents(StrList.ListImage);
StrList.Paint;
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
if TSpeedButton(Sender).Tag = Ord(nsDoWhile) then
SetCursorPos(Mouse.CursorPos.X + 30, Mouse.CursorPos.Y +
StrList.LineHeight)
else
SetCursorPos(Mouse.CursorPos.X + 30, Mouse.CursorPos.Y);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
end;
end;
procedure TFStructogram.ImageDblClick(Sender: TObject);
begin
FCurList := TListImage(Sender).StrList;
FCurElement := FindVisibleElement(FCurList, FMousePos.X, FMousePos.Y);
DoEdit(FCurElement, '');
FIgnoreNextMouseDown := True;
end;
procedure TFStructogram.ImageMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; XPos, YPos: Integer);
begin
if FIgnoreNextMouseDown then
begin
FIgnoreNextMouseDown := False;
Exit;
end;
if FIsMoving then
Exit;
var
Image := TListImage(Sender);
Image.BringToFront;
FCurList := Image.StrList;
FMousePos := Point(XPos, YPos);
FScreenMousePos := Image.ClientToScreen(Point(XPos, YPos));
if FEditMemo.Visible then
CloseEdit;
FCurElement := FindVisibleElement(FCurList, FMousePos.X, FMousePos.Y);
if (FSeparating = 0) and Assigned(FCurElement) and Assigned(FCurElement.Prev)
then
if (FCurElement.Prev is TStrAlgorithm) or
not((FCurElement.Prev is TStrList) or (FCurElement is TStrCase)) then
FSeparating := 1;
UpdateState;
end;
// mouse move over a structogram
procedure TFStructogram.ImageMouseMove(Sender: TObject; Shift: TShiftState;
XPos, YPos: Integer);
var ScrollBoxPt, Image2Pt, ScreenPt: TPoint;
DeltaX, DeltaY, DeltaX1, DeltaY1: Integer; Image, Image2: TListImage;
StrList: TStrList; StrStatement: TStrStatement; ARect: TRect;
begin
HideShape;
if FEditMemo.Visible then
Exit;
if ssLeft in Shift then
begin
Image := TListImage(Sender);
FCurList := Image.StrList;
// start moving
ScreenPt := Image.ClientToScreen(Point(XPos, YPos));
ScrollBoxPt := ScrollBox.ScreenToClient(ScreenPt);
DeltaX := ScreenPt.X - FScreenMousePos.X;
DeltaY := ScreenPt.Y - FScreenMousePos.Y;
if (DeltaX = 0) and (DeltaY = 0) then
Exit;
if (Abs(DeltaX) + Abs(DeltaY) > 5) or FIsMoving then
begin // move Image
FIsMoving := True;
Modified := True;
FScreenMousePos := ScreenPt;
FCurElement := FindVisibleElement(FCurList, XPos - DeltaX, YPos - DeltaY);
if Assigned(FCurElement) and (FSeparating = 1) then
begin
DeltaX1 := XPos - FCurElement.Rct.Left;
DeltaY1 := YPos - FCurElement.Rct.Top;
if FCurElement.Prev = nil then
Exit;
ARect := FCurElement.Rct;
if (FCurElement.Prev is TStrListHead) or (FCurElement.Prev is TStrCase)
or (FPuzzleMode = 1) then
begin
StrStatement := TStrStatement.Create(FCurList);
StrStatement.Rct := FCurElement.Rct;
FCurList.Insert(FCurElement.Prev, StrStatement);
end;
if FPuzzleMode = 1 then
begin // handle single statements, no lists
FCurElement.Prev.Next := FCurElement.Next;
if Assigned(FCurElement.Next) then
FCurElement.Next.Prev := FCurElement.Prev;
FCurElement.Prev := nil;
FCurElement.Next := nil;
end
else
begin
FCurElement.Prev.Next := nil;
FCurElement.Prev := nil;
end;
FCurList.ResizeAll;
FCurList.Paint;
// create new List
StrList := TStrList.Create(ScrollBox, FPuzzleMode, Font);
StrList.SetFont(Font);
StrList.Insert(StrList, FCurElement);
StrList.SetList(StrList);
SetEvents(StrList.ListImage);
StrList.ResizeAll;
var
Rect := StrList.RctList;
Rect.Right := Max(ARect.Right - ARect.Left, StrList.RctList.Right);
Rect.Bottom := Max(ARect.Bottom - ARect.Top, StrList.RctList.Bottom);
StrList.RctList := Rect;
StrList.Rct := StrList.RctList;
StrList.ListImage.Left := Image.Left + ARect.Left + DeltaY;
StrList.ListImage.Top := Image.Top + ARect.Top + DeltaX;
StrList.Paint;
FSeparating := 2;
FCurList.DontMove := True;
FIgnoreNextMouseUp := True;
FIsMoving := False;
ScreenPt := StrList.ListImage.ClientToScreen(Point(DeltaX1, DeltaY1));
SetCursorPos(ScreenPt.X, ScreenPt.Y);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
FCurList := StrList;
end
else
begin
// crucial for switching mouse to new image
Image.SetBounds(Image.Left + DeltaX, Image.Top + DeltaY, Image.Width,
Image.Height);
if FCurList.DontMove then
begin
Image.SetBounds(Image.Left - DeltaX, Image.Top - DeltaY, Image.Width,
Image.Height);
FCurList.DontMove := False;
end;
if FCurList.Kind <> Byte(nsAlgorithm) then
begin
for var I := ScrollBox.ControlCount - 1 downto 0 do
begin
Image2 := TListImage(ScrollBox.Controls[I]);
if Image2 <> Image then
begin
ARect := Image2.BoundsRect;
ARect.Inflate(20, 20);
if ARect.Contains(ScrollBoxPt) then
begin
Image2Pt := Image2.ScreenToClient(ScreenPt);
FCurElement := FindElement(Image2.StrList, Image2Pt.X,
Image2Pt.Y);
CalculateInsertionShape(Image2.StrList, FCurList, Image2Pt.Y);
end;
end;
end;
end;
end;
end;
end;
end;
procedure TFStructogram.CalculateInsertionShape(DestList, InsertList: TStrList;
YPos: Integer);
begin
// FCurElement.debug()
if not Assigned(FCurElement) then
Exit;
with FCurElement do
if not EqualRect(FOldShape, Rect(Rct.Bottom, Rct.Left, Rct.Bottom,
Rct.Right)) then
begin
if BeforeOrAfter(FCurElement) then
begin
if YPos < FCurElement.Rct.Top + FCurElement.GetMaxDelta then
begin
FOldShape := Rect(Rct.Left, Rct.Top, Rct.Right, Rct.Top);
FCurInsert := -1; // Insert before
end
else if YPos > FCurElement.Rct.Bottom - FCurElement.GetMaxDelta then
begin
if FCurElement = List then
FOldShape := Rect(Rct.Left, Rct.Bottom, List.RctList.Right,
Rct.Bottom)
else
FOldShape := Rect(Rct.Left, Rct.Bottom, Rct.Right, Rct.Bottom);
FCurInsert := +1; // Insert after
end;
end
else
begin
var
DeltaY := (FCurElement.Rct.Bottom - FCurElement.Rct.Top) div 4;
if YPos < FCurElement.Rct.Top + DeltaY then
begin
FOldShape := Rect(Rct.Left, Rct.Top, Rct.Right, Rct.Top);
FCurInsert := -1;
end
else if YPos < FCurElement.Rct.Bottom - DeltaY then
begin
FOldShape := Rect(Rct.Left + 1, Rct.Top + 1, Rct.Right - 1,
Rct.Bottom - 1);
FCurInsert := 0;
end
else
begin
FOldShape := Rect(Rct.Left, Rct.Bottom, Rct.Right, Rct.Bottom);
FCurInsert := +1;
end;
end;
FOldCanvas := DestList.ListImage.Canvas;
if FitsIn(InsertList, FCurElement) then
begin
ShowShape;
DestList.Dirty := True;
end;
end;
end;
procedure TFStructogram.ImageMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; XPos, YPos: Integer);
var ScreenPt, QPoint, ScrollBoxPt, Image2Pt: TPoint; Image, Image2: TListImage;
StrList: TStrList; ARect: TRect;
begin
FIsMoving := False;
Image := TListImage(Sender);
StrList := Image.StrList;
if FIgnoreNextMouseUp then
begin
FIgnoreNextMouseUp := False;
Exit;
end;
ARect := StrList.RctList;
StrList.ResizeAll;
if ARect <> StrList.RctList then
StrList.Paint;
if StrList.Kind in [Byte(nsAlgorithm), Byte(nsList)] then
begin
ScreenPt := Image.ClientToScreen(Point(XPos, YPos));
QPoint := Self.ScreenToClient(ScreenPt);
if QPoint.X < ScrollBox.Left then
begin
FreeAndNil(StrList);
Exit;
end;
if QPoint.Y < ScrollBox.Top then
Image.Top := 0;
end;
if StrList.Kind = Byte(nsList) then
begin
ScrollBoxPt := ScrollBox.ScreenToClient(ScreenPt);
for var I := ScrollBox.ControlCount - 1 downto 0 do
begin
Image2 := TListImage(ScrollBox.Controls[I]);
ARect := Image2.BoundsRect;
ARect.Inflate(30, 30);
if (Image2 <> Image) and ARect.Contains(ScrollBoxPt) then
begin
Image2Pt := Image2.ScreenToClient(ScreenPt);
FCurElement := FindElement(Image2.StrList, Image2Pt.X, Image2Pt.Y);
if Assigned(FCurElement) and FitsIn(StrList, FCurElement) then
InsertElement(Image2.StrList, StrList, FCurElement);