-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathProjectEditor.pas
2251 lines (2161 loc) · 74.1 KB
/
ProjectEditor.pas
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
/// Documentation Editor editor visual frame
// - this unit is part of SynProject, under GPL 3.0 license; version 1.13
unit ProjectEditor;
(*
This file is part of SynProject.
Synopse SynProject. Copyright (C) 2008-2023 Arnaud Bouchez
Synopse Informatique - https://synopse.info
SynProject is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at
your option) any later version.
SynProject is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU General Public License
along with SynProject. If not, see <http://www.gnu.org/licenses/>.
*)
interface
{$ifndef DONTUSEPARSER} // for ProjectVersion, e.g. (must be set globally)
{$define USEPARSER}
// attempt to get information from source code directly
{$define USEGDIPLUSFORIMAGES}
// if defined, GDI+ library will be used for reading jpeg and png images
// (requires Windows XP and later - or GdiPlus.dll in program folder)
{$define WITH_GRAPHVIZ}
// if defined, the WinGraphviz COM server will be used to generated diagrams
// (must be defined in ProjectParser, ProjectEditor and ProjectTypes)
{$endif}
{$define USEGDIPLUSFORIMAGES}
// must be defined by default
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, SynMemoEx, ExtCtrls, ImgList, ComCtrls, StdCtrls,
{$ifdef USEGDIPLUSFORIMAGES}
SynGdiPlus,
{$endif}
ProjectRTF,
ProjectTypes, ProjectSections, ProjectMemoExSyntax, ProjectSpellCheck,
ProjectFormSelection, ToolWin, Menus, ExtDlgs;
type
TOnDataChange = procedure(Sender: TObject; const newData: string) of object;
TFrameEditor = class(TFrame)
Memo: TMemoEx;
ToolBar: TToolBar;
BtnReadOnly: TToolButton;
ImageListEnabled: TImageList;
ImageListDisabled: TImageList;
BtnWordWrap: TToolButton;
ToolButton1: TToolButton;
ToolButton2: TToolButton;
BtnSave: TToolButton;
Panel: TPanel;
Sections: TListBox;
BtnHistoryBack: TToolButton;
BtnHistoryNext: TToolButton;
ToolButton3: TToolButton;
BtnBold: TToolButton;
BtnItalic: TToolButton;
BtnUnderline: TToolButton;
BtnUndo: TToolButton;
BtnTextAll: TToolButton;
BtnLinkSection: TToolButton;
BtnLinkPeople: TToolButton;
PopupMenuLink: TPopupMenu;
BtnLinkPicture: TToolButton;
FindDialog: TFindDialog;
BtnDocument: TToolButton;
BtnFixedFont: TToolButton;
BtnMarkProgram: TToolButton;
PopupMenuProgram: TPopupMenu;
PopupMenuProgramDelphi: TMenuItem;
PopupMenuProgramC: TMenuItem;
PopupMenuProgramCSharp: TMenuItem;
PopupMenuProgramINI: TMenuItem;
BtnAutoSections: TToolButton;
BtnAddPicture: TToolButton;
ToolButton4: TToolButton;
BtnAddTracker: TToolButton;
BtnHistory: TToolButton;
PopupMenuBtnHistory: TPopupMenu;
BtnReleaseDocument: TToolButton;
PopupMenuProgramComment: TMenuItem;
BtnWizard: TToolButton;
BtnImportTracker: TToolButton;
PopupMenuProgramModula2: TMenuItem;
BtnAddGraph: TToolButton;
BtnSpellCheck: TToolButton;
BtnAbout: TToolButton;
ToolButton5: TToolButton;
BtnLinkProgram: TToolButton;
PopupMenuProgramXML: TMenuItem;
PopupMenuProgramDFM: TMenuItem;
EditorPopup: TPopupMenu;
EditorPopupCopy: TMenuItem;
EditorPopupPaste: TMenuItem;
EditorPopupCut: TMenuItem;
EditorPopupCopyAs: TMenuItem;
EditorPopupCopyAsHtml: TMenuItem;
EditorPopupCopyAsBBCode: TMenuItem;
N1: TMenuItem;
EditorPopupSpellCheck: TMenuItem;
EditorPopupUndo: TMenuItem;
EditorPopupWordWrap: TMenuItem;
ImageList16: TImageList;
procedure BtnReadOnlyClick(Sender: TObject);
procedure BtnWordWrapClick(Sender: TObject);
procedure MemoSetCaretPos(Sender: TObject; CaretX, CaretY: Integer);
procedure SectionsClick(Sender: TObject);
procedure MemoMouseOver(Sender: TObject; WordStyle: Word;
var _Cursor: TCursor);
procedure BtnHistoryBackClick(Sender: TObject);
procedure BtnHistoryNextClick(Sender: TObject);
procedure BtnBoldItalicUnderlineClick(Sender: TObject);
procedure BtnUndoClick(Sender: TObject);
procedure MemoChange(Sender: TObject);
procedure BtnLinkSectionClick(Sender: TObject);
procedure SectionsMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FindDialogFind(Sender: TObject);
procedure MemoKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure BtnMarkProgramClick(Sender: TObject);
procedure BtnAutoSectionsClick(Sender: TObject);
procedure BtnReleaseDocumentClick(Sender: TObject);
procedure BtnWizardClick(Sender: TObject);
procedure BtnAddGraphClick(Sender: TObject);
procedure BtnSpellCheckClick(Sender: TObject);
procedure BtnAboutClick(Sender: TObject);
procedure BtnLinkProgramClick(Sender: TObject);
procedure EditorPopupCopyClick(Sender: TObject);
procedure EditorPopupPasteClick(Sender: TObject);
procedure EditorPopupCutClick(Sender: TObject);
procedure EditorPopupCopyAsHtmlClick(Sender: TObject);
procedure EditorPopupCopyAsBBCodeClick(Sender: TObject);
procedure EditorPopupPopup(Sender: TObject);
private
FParams,
FTextAll: boolean;
FLinkClickMenus: TList;
procedure UpdateSections(KeepSelected: boolean);
procedure SetTextAll(const Value: boolean);
procedure MemoWordClick(Sender: TObject; const Clicked: TWordUnderCursor);
procedure MemoInsertRtfCommand(const Prefix,Suffix: string);
function HistoryAddFromCurrent: boolean;
function HistoryAdd(const Clicked: TWordUnderCursor): boolean;
procedure SetParams(const Value: boolean);
procedure LinkMenuClick(Sender: TObject);
function CreateTempProject(aClass: TProjectWriterClass=nil): TProject;
function GetReadOnly: boolean;
procedure SetReadOnly(const Value: boolean);
procedure UpdateSectionValues(Sec: TSection);
function TitleLinkParaIndex(TitleID: integer): integer;
function OnClipboardPaste(Sender: TObject): boolean;
function InsertPicture(Pic: TPicture; const Title, PicFileName: string): boolean;
procedure AllTitles;
procedure NumberedTitles;
function ShowSelectionForm(OnlyNumberedTitles: boolean): TSelectionForm;
function FormatProAs(P: PAnsiChar; const Tags: THtmlTagsSet): AnsiString;
public
Data: TSectionsStorage;
MemoWordClickText: string;
HistoryMax,
HistoryCurrent: integer;
History: array[0..20] of TWordUnderCursor;
OnDataChange: TOnDataChange;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function UpdateDataFromTextAllIfNecessary: boolean;
procedure OnEscKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
property TextAll: boolean read FTextAll write SetTextAll;
property Params: boolean read FParams write SetParams;
property ReadOnly: boolean read GetReadOnly write SetReadOnly;
end;
procedure EnsureSingleInstance;
{ Application.Initialize;
EnsureSingleInstance;
Application.CreateForm(TMainForm, MainForm);
.... }
function CreateFormEditor(aFile: TSectionsStorage; out E: TFrameEditor;
NotPro: boolean; const Title: string): TForm;
function EditText(F: TForm; E: TFrameEditor; FreeAtClose: boolean): boolean; // true is modified
/// uncompress an image list from a .zip embedded as a .res to the executable
procedure LoadFromEmbedded(ImgList: TImageList; const ZipName: string);
resourcestring
sPictureFromFile = 'Picture from file';
sPictureFromClipboad = 'Picture from Clipboard';
sMenusParser = ',Refresh from source code,,Parse all source code again,'+
'Initialize external source code descriptions .sae file,Create external .sae file,'+
',Edit external .sae file';
sMenusGraph = 'Diagrams,Recreate all diagrams';
sMenuAllSummaryOnly = 'All Summary Sheets only';
sMenuGraphs = 'Recreate \graph diagrams';
sAllDocumentsHtml = 'All documents,Web Site Export';
sEnterPictureFileName = 'Enter Picture internal File Name (with no .png extension):';
sErrorFileExists = 'This file name already exists. Please enter a genuine one';
sEnterPictureWidth = 'Enter Picture Width percent (100%):';
sEnterPictureDescription = 'Enter Picture description:';
sCompleteSections = 'This will complete sections for the document';
sNoNewSectionFound = 'No new section found';
sAddSectionsQuery = 'Do you want to add the following sections';
sErrorDocModifiedAskSave = 'Document has been modified.'#13#13'Do you want to save changes?';
sPopupMenuText = 'Format,Mark,Link,Insert,Go to,Create Doc';
sTitlesDot = 'Titles...';
sTitlesSharp = 'Titles #';
sTitlesAll = 'All titles';
implementation
{$R *.dfm}
uses
Clipbrd,
ProjectCommons, // for UpperPCharCpy
{$ifdef WITH_GRAPHVIZ}
ProjectGraphEdit,
ProjectParser,
{$endif}
SynZip,
ProjectVersionSCR, ProjectEditorRelease, ProjectFormDocWizard,
{$ifdef USEPARSER}
ProjectEditorProgram,
{$endif}
ProjectVersionMain; // for keywords
procedure TFrameEditor.BtnReadOnlyClick(Sender: TObject);
begin
BtnReadOnly.ImageIndex := BtnReadOnly.ImageIndex xor 1;
Memo.ReadOnly := boolean(BtnReadOnly.ImageIndex);
end;
procedure TFrameEditor.BtnWordWrapClick(Sender: TObject);
var _P, _PI: integer;
Down: boolean;
begin
Down := not Memo.WordWrap;
BtnWordWrap.Down := Down; // manual tbsCheck style for Delphi 5
Memo.Lines.Index2ParaIndex(Memo.CaretY, _P, _PI);
Memo.WordWrap := Down;
EditorPopupWordWrap.Checked := Down;
if _P>=0 then // set top screen to middle of screen
Memo.SetLeftTop(0,Memo.Lines.Paragraphs[_P].FPreCount-3);
end;
procedure LoadFromEmbedded(ImgList: TImageList; const ZipName: string);
var i: integer;
Bmp: TBitmap;
Stream: TStringStream;
BW,BH,W,H: integer;
begin
with TZipRead.Create(HInstance,'Zip','ZIP') do
try
i := NameToIndex(ZipName);
if i<0 then exit;
Stream := TStringStream.Create(UnZip(i)); // uncompress
try
Bmp := TBitmap.Create;
try
Bmp.LoadFromStream(Stream);
// from multi-line (i.e. IDE export) into one-line (for AddMasked)
BW := Bmp.Width;
BH := Bmp.Height;
W := (BW div ImgList.Width);
H := (BH div ImgList.Height);
Bmp.Width := W*H*ImgList.Width;
BH := ImgList.Height;
for i := 2 to H do
Bmp.Canvas.CopyRect(Rect((i-1)*BW,0,i*BW,BH),
Bmp.Canvas,Rect(0,(i-1)*BH,BW,i*BH));
Bmp.Height := BH;
// add these images to the image list
ImgList.AddMasked(Bmp,clFuchsia);
finally
Bmp.Free;
end;
finally
Stream.Free;
end;
finally
Free;
end;
end;
procedure ImageListStretch(ImgListSource, ImgListDest: TImageList;
BkColor: TColor=clSilver);
var BmpSource, BmpDest: TBitmap;
i: integer;
Pic: TSynPicture;
RS,RD: TRect;
begin
ImgListDest.Clear;
if Gdip=nil then
Gdip := TGDIPlusFull.Create;
Pic := TSynPicture.Create;
BmpSource := TBitmap.Create;
BmpDest := TBitmap.Create;
try
RS.Left := 0;
RS.Top := 0;
RS.Right := ImgListSource.Width;
RS.Bottom := ImgListSource.Height;
BmpSource.Width := RS.Right;
BmpSource.Height := RS.Bottom;
RD.Left := 0;
RD.Top := 0;
RD.Right := ImgListDest.Width;
RD.Bottom := ImgListDest.Height;
BmpDest.Width := RD.Right;
ImgListDest.Masked := false;
BmpDest.Height := RD.Bottom;
for i := 0 to ImgListSource.Count-1 do begin
BmpSource.Canvas.Brush.Color := BkColor;
BmpSource.Canvas.Brush.Style := bsSolid;
BmpSource.Canvas.FillRect(RS);
ImgListSource.Draw(BmpSource.Canvas,0,0,i);
Pic.Assign(BmpSource);
Pic.Draw(BmpDest.Canvas,RD); // GDI+ smooth draw
ImgListDest.Add(BmpDest,nil);
end;
finally
BmpDest.Free;
BmpSource.Free;
Pic.Free;
end;
end;
destructor TFrameEditor.Destroy;
begin
FLinkClickMenus.Free;
inherited;
end;
constructor TFrameEditor.Create(AOwner: TComponent);
function New(Button: TToolButton; const Caption: string='';
Source: TMenuItem=nil; Tag: integer=0): TMenuItem;
begin
if Source=nil then
Source := EditorPopup.Items;
result := TMenuItem.Create(self);
result.Caption := Caption;
if Button<>nil then begin
if Button.ImageIndex=0 then
result.ImageIndex := Source.ImageIndex else
result.ImageIndex := Button.ImageIndex;
if Caption='' then
result.Caption := Button.Hint;
if Tag<>0 then begin
result.Tag := Tag;
FLinkClickMenus.Add(result);
end else begin
result.Tag := Button.Tag;
result.OnClick := Button.OnClick;
end;
end;
Source.Add(result);
end;
var Mark, M: TMenuItem;
i: integer;
P: PChar;
begin
inherited;
LoadFromEmbedded(ImageListEnabled,'FrameEditorEnabled.bmp');
LoadFromEmbedded(ImageListDisabled,'FrameEditorDisabled.bmp');
ImageListStretch(ImageListEnabled,ImageList16,clWhite);
BtnWordWrapClick(nil); // update Memo.WordWrap according to button
Memo.ClipPasteRtfBackSlashConvert := true; // '\' -> '\\' for paste
Memo.OnClipboardPaste := OnClipboardPaste;
if Screen.Fonts.IndexOf('Consolas')>=0 then
Memo.Font.Name := 'Consolas';
{$ifdef WITH_GRAPHVIZ}
BtnAddGraph.Visible := true;
{$endif}
FLinkClickMenus := TList.Create;
P := pointer(string(sPopupMenuText));
Mark := New(nil,GetNextItem(P));
Mark.ImageIndex := BtnItalic.ImageIndex;
New(BtnBold,'',Mark);
New(BtnItalic,'',Mark);
New(BtnUnderline,'',Mark);
New(BtnFixedFont,'',Mark);
Mark := New(nil,GetNextItem(P));
Mark.ImageIndex := BtnMarkProgram.ImageIndex;
for i := 0 to PopupMenuProgram.Items.Count-1 do
with PopupMenuProgram.Items[i] do begin
M := New(BtnMarkProgram,Caption,Mark);
M.Hint := Hint;
M.ShortCut := ShortCut;
end;
Mark := New(nil,GetNextItem(P));
Mark.ImageIndex := BtnLinkPeople.ImageIndex;
New(BtnLinkSection,'',Mark,1);
New(BtnLinkPeople,'',Mark,2);
New(BtnLinkPicture,'',Mark,3);
New(BtnLinkProgram,'',Mark);
Mark := New(nil,GetNextItem(P));
Mark.ImageIndex := BtnAddPicture.ImageIndex;
New(BtnAddPicture,'',Mark,10);
New(BtnAddGraph,'',Mark);
New(BtnLinkSection,GetNextItem(P),nil,20).ImageIndex := BtnLinkSection.ImageIndex;
New(BtnDocument,GetNextItem(P),nil,21);
end;
procedure TFrameEditor.SetTextAll(const Value: boolean);
begin
FTextAll := Value;
Sections.Visible := Value;
BtnHistoryBack.Visible := Value;
BtnHistoryNext.Visible := Value;
BtnAutoSections.Visible := Value;
BtnReleaseDocument.Visible := Value;
BtnWizard.Visible := Value;
BtnTextAll.Visible := not Value;
BtnSave.Visible := Value and Assigned(BtnSave.OnClick);
UpdateSections(false);
if Value then
Memo.OnWordClick := MemoWordClick;
end;
procedure TFrameEditor.SetParams(const Value: boolean);
begin
TextAll := false;
FParams := Value;
BtnBold.Visible := not Value;
BtnItalic.Visible := not Value;
BtnUnderline.Visible := not Value;
BtnDocument.Visible := not Value;
if Value then
Memo.OnGetLineAttr := TProjectSyntax.IniGetLineAttr else
Memo.OnGetLineAttr := TProjectSyntax.BodyGetLineAttr;
end;
procedure TFrameEditor.UpdateSections(KeepSelected: boolean);
var Select, s: string;
i: integer;
begin
if KeepSelected then begin
i := Sections.ItemIndex;
if i>=0 then
Select := Sections.Items[i] else
Select := '';
end;
Sections.Items.BeginUpdate;
Sections.Items.Clear;
with Memo.Lines do
for i := 0 to Count-1 do
with Paragraphs[i]^ do
if FCount>0 then begin
s := FStrings[0];
if (s<>'') and (s[1]='[') then begin
s := TSectionsStorage.TrimBrackets(s);
if s<>'' then
Sections.Items.Add(s);
end;
end;
if KeepSelected then
Sections.ItemIndex := Sections.Items.IndexOf(Select);
Sections.Items.EndUpdate;
end;
procedure TFrameEditor.MemoSetCaretPos(Sender: TObject; CaretX, CaretY: Integer);
var Para, ParaIndex, i: integer;
s: string;
begin // update Sections selected item from current position (fast code)
Memo.Lines.Index2ParaIndex(CaretY,Para,ParaIndex);
// TForm(Owner).Caption := format('%d,%d,Line[%d]=%s',
// [CaretX,CaretY,Para,Memo.Lines.Paragraphs[Para].FStrings[0]]);
with Memo.Lines do // find [Section] just before Caret Pos
while Para>=0 do begin
s := Paragraphs[Para].FStrings[0]; // first string of the paragraph
if (length(s)>1) and (s[1]='[') then begin
s := TSectionsStorage.TrimBrackets(s);
if s<>'' then begin
i := Sections.ItemIndex;
if (i<0) or (Sections.Items[i]<>s) then begin
i := Sections.Items.IndexOf(s);
if i<0 then begin // new or modified section name -> update list
UpdateSections(false);
i := Sections.Items.IndexOf(s);
end;
Sections.ItemIndex := i;
end;
end;
exit;
end;
dec(Para);
end;
end;
procedure TFrameEditor.SectionsClick(Sender: TObject);
// if Sender=nil -> search of MemoWordClickText+'='
var index, i, j, y: integer;
s: string;
begin
index := Sections.ItemIndex;
if index<0 then exit;
if Sender=nil then // Click from Code -> add to History
HistoryAddFromCurrent;
s := '['+UpperCase(Sections.Items[index])+']';
with Memo.Lines do
for i := 0 to Count-1 do
with Paragraphs[i]^ do // find [Section] line
if (FCount>0) and IdemPChar(pointer(FStrings[0]),pointer(s)) then begin
Memo.SetLeftTop(0,FPreCount); // top screen to [Section] beginning
Y := FPreCount;
if Sender=nil then begin // -> search of MemoWordClickText+'=' line
MemoWordClickText := UpperCase(MemoWordClickText)+'=';
for j := i+1 to Count-1 do begin
s := Paragraphs[j].FStrings[0];
if (s='') or (s[1] in ['[',':']) then break else
if IdemPChar(pointer(s),pointer(MemoWordClickText)) then begin
Y := Paragraphs[j].FPreCount;
break;
end;
end;
end;
Memo.SetCaret(0,Y); // focus this line
Memo.SetFocus;
exit;
end;
end;
procedure TFrameEditor.MemoMouseOver(Sender: TObject; WordStyle: Word;
var _Cursor: TCursor);
begin
if (WordStyle>0) and Assigned(Memo.OnWordClick) then
_Cursor := crHandPoint;
end;
function TFrameEditor.TitleLinkParaIndex(TitleID: integer): integer;
var j, value: integer;
line: string;
begin // fast find ':1 title' where TitleID=1
with Memo.Lines do
for result := 0 to Count-1 do begin
line := Paragraphs[result].FStrings[0]; // fast retrieval of beginning of line
if (line='') or (line[1]<>':') or
not (line[2] in ['1'..'9']) then continue;
value := 0;
j := 2;
while line[j] in ['0'..'9'] do begin
value := ord(line[j])-48+value*10;
inc(j);
end;
if value=TitleID then
exit;
end;
result := -1;
end;
function TFrameEditor.OnClipboardPaste(Sender: TObject): boolean;
var Dest: TPicture;
Value, Enter, Path: string;
begin
result := false;
if Clipboard.HasFormat(CF_PICTURE) then begin
Dest := TPicture.Create;
try
Dest.Assign(Clipboard);
if (Dest.Height<>0) and (Dest.Width<>0) then begin
Path := ExtractFilePath(Data.FileName);
if Path='' then
exit; // need a valid local path to store picture in
repeat
if not InputQuery(sPictureFromClipboad,sEnterPictureFileName, Enter) then
exit;
Value := ExtractFileName(ChangeFileExt(Enter,'.png'));
if FileExists(Path+Value) or (Data['Pictures'][Value]<>'') then
MessageDlg(sErrorFileExists,mtError,[mbOk],0) else
break;
until false;
if InsertPicture(Dest,sPictureFromClipboad,Value) then begin
SaveAs(Dest,Path+Value,gptPNG);
result := true; // mark don't get as text
end;
end;
finally
Dest.Free;
end;
end;
end;
procedure TFrameEditor.AllTitles;
begin
with ShowSelectionForm(false) do
try
if Selected>=0 then begin
HistoryAddFromCurrent;
Memo.SetCaretAtParaPos(integer(Lines.Objects[Selected]),0);
Memo.SetFocus;
end;
finally
Free;
end;
end;
procedure TFrameEditor.NumberedTitles;
var i: integer;
begin
with ShowSelectionForm(true) do
try
i := TitleToNumber(Selected);
if i>0 then
Memo.InsertTextAtCurrentPos('@'+IntToStr(i)+'@');
finally
Free;
end;
end;
function TFrameEditor.ShowSelectionForm(OnlyNumberedTitles: boolean): TSelectionForm;
var i,Para,ParaIndex: integer;
Value: string;
begin
result := TSelectionForm.Create(self);
result.OnlyNumberedTitles := OnlyNumberedTitles;
result.Caption := ' '+ExtractFileName(Data.FileName);
Memo.Lines.Index2ParaIndex(Memo.CaretY,Para,ParaIndex);
with Memo.Lines do
for i := 0 to Count-1 do begin
Value := Paragraphs[i].FStrings[0];
if Value='' then
continue;
case Value[1] of
'[': result.Lines.AddObject(Value,TObject(i));
':': result.Lines.AddObject(copy(Value,2,maxInt),TObject(i));
else continue;
end;
if Para>=i then
result.Selected := result.Lines.Count-1;
end;
result.ShowModal;
end;
function TFrameEditor.InsertPicture(Pic: TPicture; const Title, PicFileName: string): boolean;
var Def, Enter: string;
i, Y: integer;
Pictures: TSection;
begin
result := false;
repeat
Enter := '100';
if not InputQuery(Title,sEnterPictureWidth, Enter) then
exit;
until StrToIntDef(Enter,0)>0;
Def := Enter+'%';
Enter := '';
if not InputQuery(Title,sEnterPictureDescription, Enter) then
exit;
result := true;
Def := format('%dx%d %s,%s',[Pic.Width,Pic.Height,Def,Enter]);
Memo.Command(ecBeginLine);
Memo.InsertTextAtCurrentPos('%'+PicFileName+#13#10);
if TextAll then begin
Y := Memo.CaretY;
i := Sections.Items.IndexOf('Pictures');
Pictures := Data['Pictures'];
if Pictures=nil then begin
Pictures := Data.GetOrCreateSection('Pictures',true);
Memo.SetCaret(0,0);
Memo.InsertTextAtCurrentPos('[Pictures]'#13#10#13#10);
inc(Y,2); // #13#10#13#10 -> 2 lines down
UpdateSections(false);
Memo.Command(ecUp);
Memo.Command(ecUp);
end else begin
Sections.ItemIndex := i;
SectionsClick(nil);
end;
Memo.Command(ecDown);
Pictures[PicFileName] := Def;
Memo.InsertTextAtCurrentPos(PicFileName+'='+Def+#13#10);
Memo.SetCaret(0,Y);
end else
Data['Pictures'][PicFileName] := Def; // thats enough
end;
procedure TFrameEditor.MemoWordClick(Sender: TObject; const Clicked: TWordUnderCursor);
var DestSection: integer;
DI, SectionName, SectionNameKind, SectionNameValue, Ext: string;
i, TitleID, x,y: integer;
begin
DestSection := -1;
case Clicked.Style of
edStyleButton: begin // '@SRS@' '=[KnownIssues]'
MemoWordClickText := Clicked.Text;
while (MemoWordClickText<>'') and (MemoWordClickText[1] in ['@','=','[']) do
delete(MemoWordClickText,1,1);
while (MemoWordClickText<>'') and (MemoWordClickText[length(MemoWordClickText)] in ['@',']']) do
SetLength(MemoWordClickText,length(MemoWordClickText)-1);
if MemoWordClickText='' then exit;
DestSection := Sections.Items.IndexOf(MemoWordClickText);
if DestSection<0 then begin // if not @SRS@
Ext := ExtractFileExt(MemoWordClickText);
if PWord(MemoWordClickText)^=ord('%')+ord('%')shl 8 then
exit else // @%%graphPicture@ is ignored (not referenced in [Pictures])
if GetStringIndex(VALID_PROGRAM_EXT, Ext)>=0 then begin
{$ifdef USEPARSER}
if not SameText(Ext,'.PAS') or
(MemoWordClickText[1]<>'!') or (ssCtrl in Clicked.Shift) then begin
{$endif} // @PC\EIA\main.pas@ or Ctrl + @!PC\EIA\main.pas@ -> go to [SAD-PC] section
if MemoWordClickText[1]='!' then begin
delete(MemoWordClickText,1,1);
i := pos('!',MemoWordClickText);
if i>0 then // @!TObject!PC\EIA\main.pas@
delete(MemoWordClickText,1,i);
end;
DestSection := Sections.Items.IndexOf(TProject.GetProgramSection(MemoWordClickText));
MemoWordClickText := TProject.GetProgramName(MemoWordClickText);
{$ifdef USEPARSER}
end else begin
// left click on @!PC\EIA\main.pas@ -> edit program link
if EditProgramForm(CreateTempProject,MemoWordClickText) then begin
Memo.SelStart := Clicked.TextStart;
Memo.SelLength := length(Clicked.Text);
Memo.SelText := '@'+MemoWordClickText+'@'; // replace
end;
exit;
end;
{$endif}
end else
if GetStringIndex(VALID_PICTURES_EXT, Ext)>=0 then begin
// @picture.png@ or @%picture.png@
if MemoWordClickText[1]='%' then
delete(MemoWordClickText,1,1);
DestSection := Sections.Items.IndexOf('Pictures');
end else
if Data['People'].Lines.IndexOfName(MemoWordClickText)>=0 then
// @Michael Jackson@
DestSection := Sections.Items.IndexOf('People') else
// @1@ -> ':1 title'
if TryStrToInt(MemoWordClickText,TitleID) then begin
i := TitleLinkParaIndex(TitleID);
if i>=0 then begin
Memo.Lines.Paragraph2Caret(i,0,x,y);
HistoryAddFromCurrent;
Memo.SetCaret(x,y);
exit;
end;
end;
end;
end;
edStylePicture: begin // '%picturename.png'
MemoWordClickText := copy(Clicked.Text,2,maxInt); // search for 'picturename.png'
if (MemoWordClickText<>'') and (MemoWordClickText[1]='%') then
exit; // ignore %%GraphGenerated
DestSection := Sections.Items.IndexOf('Pictures');
end;
edStyleSection: begin // '[SDD-DI-4.1]' -> [SRS-DI-4.1]
SectionName := UpperCase(TSectionsStorage.TrimBrackets(Clicked.Text));
TSection.SplitSectionName(SectionName, SectionNameKind, SectionNameValue);
// 'SRS-DI-4.2' -> 'SRS', 'DI-4.2'
DI := Data['Project']['MainSection'];
if SectionName=DI then
SectionNameValue := 'Project' else
if SectionNameKind=DI then
SectionNameValue := DI else begin
SectionNameKind := Data[SectionNameKind]['Owner'];
if (SectionNameKind<>'') and (SectionNameKind<>DI) then
SectionNameValue := SectionNameKind+'-'+SectionNameValue;
end;
DestSection := Sections.Items.IndexOf(SectionNameValue);
end;
end;
if DestSection<0 then exit;
HistoryAdd(Clicked);
Sections.ItemIndex := DestSection;
SectionsClick(nil); // Sender=nil -> search of MemoWordClickText+'='
end;
function TFrameEditor.HistoryAdd(const Clicked: TWordUnderCursor): boolean;
begin
if HistoryCurrent>=high(History) then
result := false else begin
result := true;
if HistoryMax<>HistoryCurrent then
HistoryCurrent := 0;
inc(HistoryCurrent);
HistoryMax := HistoryCurrent;
History[HistoryCurrent] := Clicked;
BtnHistoryBack.Enabled := true;
BtnHistoryNext.Enabled := false;
end;
end;
function TFrameEditor.HistoryAddFromCurrent: boolean;
var Clicked: TWordUnderCursor;
begin
fillchar(Clicked,sizeof(Clicked),0);
Clicked.CaretX := Memo.CaretX;
Clicked.CaretY := Memo.CaretY;
result := HistoryAdd(Clicked);
end;
procedure TFrameEditor.BtnHistoryBackClick(Sender: TObject);
var X,Y: integer;
begin
if HistoryCurrent=0 then exit;
X := Memo.CaretX;
Y := Memo.CaretY;
with History[HistoryCurrent] do
Memo.SetCaret(CaretX,CaretY);
Memo.SetFocus;
dec(HistoryCurrent);
with History[HistoryCurrent] do begin
CaretX := X;
CaretY := Y;
end;
BtnHistoryBack.Enabled := HistoryCurrent>0;
BtnHistoryNext.Enabled := true;
end;
procedure TFrameEditor.BtnHistoryNextClick(Sender: TObject);
begin
if HistoryCurrent>=HistoryMax then exit;
with History[HistoryCurrent] do
Memo.SetCaret(CaretX,CaretY);
Memo.SetFocus;
inc(HistoryCurrent);
BtnHistoryBack.Enabled := true;
BtnHistoryNext.Enabled := HistoryCurrent<HistoryMax;
end;
procedure TFrameEditor.MemoInsertRtfCommand(const Prefix,Suffix: string);
var s: string;
begin
if Memo.ReadOnly then exit;
s := Memo.SelText;
if s='' then exit;
Memo.SelText := prefix+s+Suffix;
end;
procedure TFrameEditor.BtnBoldItalicUnderlineClick(Sender: TObject);
const Cmd: array[0..3] of string = ('{\b ','{\i ','{\ul ','{\f1\fs20 ');
begin
MemoInsertRtfCommand(Cmd[TComponent(Sender).Tag],'}');
end;
procedure TFrameEditor.BtnUndoClick(Sender: TObject);
begin
if Sender=EditorPopupUndo then
with Memo.UndoBuffer do
if (LastUndo<>nil) and (LastUndo.ClassName='TCaretUndo') then
Memo.Command(ecUndo); // popup right click did change the caret pos
Memo.Command(ecUndo);
end;
procedure TFrameEditor.MemoChange(Sender: TObject);
begin
BtnUndo.Enabled := not Memo.IsUndoEmpty;
EditorPopupUndo.Enabled := BtnUndo.Enabled;
end;
procedure TFrameEditor.BtnLinkSectionClick(Sender: TObject);
var Kind: integer;
function NewMenu(const Name, Hint: string; Menu: TMenuItem;
PMenuIndex: PInteger=nil): TMenuItem;
function IsChild(const aParent: string; NewMenu,ParentMenu: TMenuItem): boolean;
var i: integer;
NewFirstMenu: TMenuItem;
begin
if ParentMenu<>nil then begin
result := true;
// test if present in parent
for i := 0 to ParentMenu.Count-1 do
with ParentMenu[i] do
if Caption=aParent then begin
if Count=0 then begin
// Menu[i] will be unclickable now -> clone as first child
NewFirstMenu := TMenuItem.Create(Owner);
NewFirstMenu.Caption := aParent;
NewFirstMenu.Hint := Hint;
NewFirstMenu.Tag := Tag;
NewFirstMenu.OnClick := OnClick;
NewFirstMenu.ImageIndex := ImageIndex;
Add(NewFirstMenu);
end;
Add(NewMenu);
exit;
end;
// recursively test if present in children
for i := 0 to ParentMenu.Count-1 do
if ParentMenu[i].Count>0 then
if IsChild(aParent,NewMenu,ParentMenu[i]) then
exit; // found in children
end;
result := false;
end;
var i: integer;
begin
result := TMenuItem.Create(Owner);
result.Caption := Name;
result.Hint := Hint;
result.Tag := Kind;
result.OnClick := LinkMenuClick;
result.ImageIndex := Menu.ImageIndex;
if Kind<1000 then begin
i := length(Name);
while (i>1) and (Name[i]<>'.') do dec(i);
if i>1 then
if IsChild(copy(Name,1,i-1),result,Menu) then
exit; // avoid insert same menu twice
end;
if PMenuIndex=nil then // insert menu at default level
Menu.Add(result) else begin // group menus per 20 items
if PMenuIndex^ mod 20=0 then
NewMenu(Format('%d..',[PMenuIndex^ div 20*20+1]),'',Menu).ImageIndex := Menu.ImageIndex;
Menu.Items[Menu.Count-1].Add(result);
inc(PMenuIndex^);
end;
end;
var i,j,k: integer;
s, aName, aValue: string;
Menu, aMenu, PopupMenuLink: TMenuItem;
MenuIndex: integer;
Str: TStringList;
Sec, Test: TSection;
P: PChar;
begin
if (Data=nil) or Memo.ReadOnly then exit;
if Sender is TMenuItem then begin // from Editor popup menu
PopupMenuLink := TMenuItem(Sender);
case PopupMenuLink.Tag of
// link
1: Sender := BtnLinkSection;
2: Sender := BtnLinkPeople;
3: Sender := BtnLinkPicture;
// insert
10: Sender := BtnAddPicture;
// jump
20: Sender := Sections;
21: Sender := BtnDocument;
end;
end else begin
PopupMenuLink := self.PopupMenuLink.Items;
if Sender is TToolButton then
PopupMenuLink.ImageIndex := TToolButton(Sender).ImageIndex;
end;
PopupMenuLink.Clear;
// Sections -> menu
if (Sender=BtnLinkSection) or (Sender=Sections) then begin
if Sender=Sections then
Kind := 5 else // MenuItem.Tag=5 -> jump Section
Kind := 1; // MenuItem.Tag=1 -> @Section@
with Data.Sections do
for i := 0 to Count-1 do
with Items[i] do
if SectionName=SectionNameValue then begin
Menu := NewMenu(SectionName,Hint,PopupMenuLink);
for j := 0 to Count-1 do begin
Sec := Items[j];
if Sec.SectionNameKind=SectionName then
NewMenu(Sec.SectionName,Sec.Hint,Menu);
end;
if Menu.Count=1 then // no Child?
if Data[SectionName]['Revision']<>'' then // true doc has Revision=..
Menu.Clear else // Revision=document
with PopupMenuLink do
Delete(Count-1); // nothing to add -> delete this item
end;
Menu := nil; // ':1 Title' -> create "Titles..." submenu
MenuIndex := 0;
Str := TStringList.Create;
aName := '';
with Memo.Lines do
for i := 0 to Count-1 do begin
s := Paragraphs[i].FStrings[0]; // fast retrieval of beginning of line
if s='' then continue;
if s[1]='[' then // remember section name
aName := s else
if s[1]=':' then begin
if s[2] in ['1'..'9'] then begin
if Menu=nil then begin // create "Titles..." submenu only if necessary
NewMenu('-','',PopupMenuLink);
Menu := NewMenu(sTitlesDot,'',PopupMenuLink);
Menu.ImageIndex := BtnTextAll.ImageIndex;
end;
s[1] := ' '; // erase left ':'
kind := 0;
j := 2;