File: frmmain.pp

package info (click to toggle)
lazarus 2.0.10%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 219,188 kB
  • sloc: pascal: 1,867,962; xml: 265,716; cpp: 56,595; sh: 3,005; java: 609; makefile: 568; perl: 297; sql: 222; ansic: 137
file content (1092 lines) | stat: -rw-r--r-- 28,903 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
{
 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code is distributed in the hope that it will be useful, but      *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.   *
 *                                                                         *
 ***************************************************************************

  Author: Michael Van Canneyt
}
unit FrmMain;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LCLProc, Forms, Controls, Graphics, Dialogs,
  Menus, ActnList, ExtCtrls, ComCtrls, pgeditor,
  LazFileUtils, LazUTF8, process, UTF8Process, StdActns, fpdeutil;

type
  TNodeType = (ntFile,ntPackage,ntModule,ntElement,ntTopic);
  { TMainForm }

  TMainForm = class(TForm)
    AInsertNumberedList: TAction;
    AInsertBulletedList: TAction;
    AInsertQuickLink: TAction;
    AInsertPrintShort: TAction;
    AExtraBuild: TAction;
    AExtraOptions: TAction;
    AHelpAbout: TAction;
    AFormatFile: TAction;
    AFormatBold: TAction;
    AFormatItalic: TAction;
    AFormatUnderline: TAction;
    AFormatRemark: TAction;
    AFormatVariable: TAction;
    AFormatCode: TAction;
    AFormatParagraph: TAction;
    AInsertTable: TAction;
    AInsertPackage: TAction;
    AInsertModule: TAction;
    AInsertElement: TAction;
    AInsertTopic: TAction;
    AInsertLink: TAction;
    AExit: TAction;
    ANew: TAction;
    ANewFromFile: TAction;
    AOpen: TAction;
    ApplicationProperties1: TApplicationProperties;
    ASave: TAction;
    ASaveAs: TAction;
    AClose: TAction;
    ALMain: TActionList;
    EditCopy1: TEditCopy;
    EditCut1: TEditCut;
    EditDelete1: TEditDelete;
    EditPaste1: TEditPaste;
    EditSelectAll1: TEditSelectAll;
    EditUndo1: TEditUndo;
    ILElements: TImageList;
    ILMain: TImageList;
    LIBuild: TMenuItem;
    MenuItem1: TMenuItem;
    MenuItem10: TMenuItem;
    MenuItem11: TMenuItem;
    MenuItem12: TMenuItem;
    MenuItem13: TMenuItem;
    MenuItem14: TMenuItem;
    MenuItem15: TMenuItem;
    MenuItem16: TMenuItem;
    MenuItem17: TMenuItem;
    MenuItem18: TMenuItem;
    MenuItem19: TMenuItem;
    MenuItem20: TMenuItem;
    MenuItem21: TMenuItem;
    Lists: TMenuItem;
    BulletedList: TMenuItem;
    NumberedList: TMenuItem;
    MenuItem4: TMenuItem;
    MenuItem5: TMenuItem;
    MenuItem6: TMenuItem;
    MenuItem7: TMenuItem;
    MenuItem8: TMenuItem;
    MenuItem9: TMenuItem;
    PopupMenu1: TPopupMenu;
    QuickLink: TMenuItem;
    MISeparate: TMenuItem;
    MISaveAs: TMenuItem;
    MISave: TMenuItem;
    MIAbout: TMenuItem;
    MIHelp: TMenuItem;
    MIExtraOptions: TMenuItem;
    MIExtra: TMenuItem;
    MIInsertPackage: TMenuItem;
    MIInsertModule: TMenuItem;
    MIInsertElement: TMenuItem;
    MIInsertLink: TMenuItem;
    MIInsertTopic: TMenuItem;
    MIInsertTable: TMenuItem;
    MIInsert: TMenuItem;
    MIClose: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    MIRecent: TMenuItem;
    MINewFromFile: TMenuItem;
    MIFileOpen: TMenuItem;
    MIFileNew: TMenuItem;
    MFile: TMenuItem;
    MMain: TMainMenu;
    ODMain: TOpenDialog;
    PCFiles: TPageControl;
    SDMain: TSaveDialog;
    Splitter1: TSplitter;
    StatusBar1: TStatusBar;
    TBMain: TToolBar;
    TBNewModule: TToolButton;
    TBNewPackage: TToolButton;
    TBNew: TToolButton;
    TBNewTopic: TToolButton;
    TBNewElement: TToolButton;
    ToolButton1: TToolButton;
    ToolButton10: TToolButton;
    TBSaveAs: TToolButton;
    TBSave: TToolButton;
    ToolButton11: TToolButton;
    ToolButton12: TToolButton;
    ToolButton13: TToolButton;
    ToolButton14: TToolButton;
    TBOpen: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    ToolButton9: TToolButton;
    procedure ACloseExecute(Sender: TObject);
    procedure AExitExecute(Sender: TObject);
    procedure AExtraBuildExecute(Sender: TObject);
    procedure AExtraOptionsExecute(Sender: TObject);
    procedure AFormatParagraphHint(var HintStr: string; var CanShow: Boolean);
    procedure AHelpAboutExecute(Sender: TObject);
    procedure AInsertNumberedListExecute(Sender: TObject);
    procedure AInsertBulletedListExecute(Sender: TObject);
    procedure ApplicationProperties1Hint(Sender: TObject);
    procedure ASaveUpdate(Sender: TObject);
    procedure CanFormat(Sender: TObject);
    procedure AInsertLinkExecute(Sender: TObject);
    procedure AInsertLinkUpdate(Sender: TObject);
    procedure AInsertTableExecute(Sender: TObject);
    procedure AInsertTableUpdate(Sender: TObject);
    procedure AInsertShortPrintLinkExecute(Sender: TObject);
    procedure AInsertShortPrintLinkUpdate(Sender: TObject);
    procedure ANewExecute(Sender: TObject);
    procedure ANewFromFileExecute(Sender: TObject);
    procedure AOpenExecute(Sender: TObject);
    procedure ASaveAsExecute(Sender: TObject);
    procedure ASaveExecute(Sender: TObject);
    procedure DoFormat(Sender: TObject);
    procedure HaveEditor(Sender: TObject);
    procedure HaveEditorAndModule(Sender: TObject);
    procedure HaveEditorAndPackage(Sender: TObject);
    procedure HaveEditorAndTopicOrSomething(Sender: TObject);
    procedure InsertStructure(Sender: TObject);
    procedure MainFormCloseQuery(Sender: TObject; var CanClose: boolean);
    procedure MainFormCreate(Sender: TObject);
    procedure MainFormDestroy(Sender: TObject);
    procedure QuickLinkClick(Sender: TObject);
  private
    FRecent : TStringList;
    FRecentBuildSettingsFile: String;
    // Editor functions.
    procedure BuildReopenList;
    Procedure AddTorecent(FN : String);
    procedure FormLocalization;
    Procedure OpenFile(FN : String; Const AStartNode : String = '');
    Procedure SaveEditorAs(E : TEditorPage);
    Procedure SaveEditor(E : TEditorPage);
    Function  CloseEditor(E : TEditorPage) : Boolean;
    Procedure FileReopen(Sender: TObject);
    Procedure NewFile;
    Procedure NewFromFile;
    Procedure LoadCommandLine;
    Procedure LoadRecent;
    Procedure SaveRecent;
    Function  CreatePage : TEditorPage;
    Function  Currenteditor : TEditorPage;
    Function  AllowClose : Boolean;
    Procedure InsertNode (NT : TNodeType);
    Procedure InsertLink;
    Procedure InsertTable;
    procedure InsertPrintShortLink;
    procedure InsertEnumerateList;
    procedure InsertItemizeList;
    Procedure ShowAbout;
    Procedure GetCurrentFiles(List : TStrings);
    Procedure ApplyOptions;
  public
    { public declarations }
  end; 

Const
  NodeNames : Array[TNodeType] of String
            = ('file','package','module','element','topic');
var
  MainForm: TMainForm;

implementation

uses
 lazdeopts, lazdemsg, inifiles, frmmakeskel, frmOptions, frmNewNode,
 frmLink, frmTable, frmAbout, frmBuild, frmlists;

{$R *.lfm}

{ Fixes & additions to LCL}

Const
  mbYesNo = [mbYes, mbNo];

function MessageDlg(Fmt: string; Args : Array of const; DlgType: TMsgDlgType;
            Buttons: TMsgDlgButtons; HelpCtx: Longint): Integer;
begin
  Result:=Dialogs.MessageDlg(Format(Fmt,Args),DlgType,Buttons,HelpCtx);
end;

function MessageDlg(Fmt: string; Args : Array of const; DlgType: TMsgDlgType;
            Buttons: TMsgDlgButtons): Integer;
begin
  Result:=Dialogs.MessageDlg(Format(Fmt,Args),DlgType,Buttons,0);
end;

function MessageDlg(Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons): Integer;
begin
  Result:=Dialogs.MessageDlg(Msg,DlgType,Buttons,0);
end;


{ TMainForm }

Type
  TRecentMenuItem = Class (TMenuItem)
  public
    FileName : String;
  end;

procedure TMainForm.FileReopen(Sender: TObject);
begin
  OpenFile((Sender as TRecentMenuItem).FileName);
end;

procedure TMainForm.MainFormCreate(Sender: TObject);
begin
  FormLocalization;
  //
  FRecent:=TStringList.Create;
  LoadCommandLine;
  LoadOptions;
  LoadRecent;
  if StartMaximized then
    WindowState := wsMaximized;
  if ReopenLast and (FRecent.Count>0) then
    OpenFile(FRecent[0]);
  ApplyOptions;
end;

procedure TMainForm.AOpenExecute(Sender: TObject);
var
  i: SizeInt;
begin
  if ODMain.Execute then
  begin
    for i := 0 to ODMain.Files.Count - 1 do
      OpenFile(ODMain.Files.Strings[i]);
  end;
end;

procedure TMainForm.AExitExecute(Sender: TObject);
begin
  Close;
end;

procedure TMainForm.AExtraBuildExecute(Sender: TObject);
var r: Integer;
begin
  if Currenteditor.Modified then
  begin
    r := MessageDlg(sSaveBeforeBuildQuestion,[Currenteditor.FileName, sLineBreak],
      mtConfirmation,mbYesNoCancel,0);
    if r = mrYes then
    begin
      SaveEditor(Currenteditor);
      Currenteditor.Update;
    end
    else if r = mrCancel then Exit;
  end;

  with TBuildForm.Create(Self) do
  try
    FileName := FRecentBuildSettingsFile;
    OnGetList:=@Self.GetCurrentFiles;
    ShowModal;
    FRecentBuildSettingsFile := FileName;
  finally
    Free;
  end;
end;

procedure TMainForm.ACloseExecute(Sender: TObject);
begin
  CloseEditor(CurrentEditor);
end;

procedure TMainForm.AExtraOptionsExecute(Sender: TObject);
var
  OptionsForm: TOptionsForm;
begin
  OptionsForm:=TOptionsForm.Create(Self);
  try
    if OptionsForm.ShowModal=mrOk then
      ApplyOptions;
  finally
    OptionsForm.Free;
  end;
end;

procedure TMainForm.AFormatParagraphHint(var HintStr: string;
  var CanShow: Boolean);
begin
  if (HintStr='') then ;
  if CanShow then ;
end;

procedure TMainForm.AHelpAboutExecute(Sender: TObject);
begin
  ShowAbout;
end;

procedure TMainForm.AInsertNumberedListExecute(Sender: TObject);
begin
  InsertEnumerateList;
end;

procedure TMainForm.AInsertBulletedListExecute(Sender: TObject);
begin
  InsertItemizeList;
end;

procedure TMainForm.ApplicationProperties1Hint(Sender: TObject);
begin
  StatusBar1.SimpleText:=Application.Hint;
end;

procedure TMainForm.ASaveUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled:=(CurrentEditor <> nil) and (CurrentEditor.Modified);
end;

procedure TMainForm.CanFormat(Sender: TObject);
Var
  B : Boolean;
begin
  B:=(CurrentEditor<>Nil) and
      CurrentEditor.CanInsertTag(TTagType((Sender as TAction).Tag));
  (Sender as Taction).Enabled:=B;
end;

procedure TMainForm.AInsertLinkExecute(Sender: TObject);
begin
  InsertLink;
end;

procedure TMainForm.AInsertLinkUpdate(Sender: TObject);
var
  B : Boolean;
begin
  B:=(CurrentEditor<>Nil) and CurrentEditor.CanInsertTag(ttLink);
  (Sender as TAction).Enabled:=B;
end;

procedure TMainForm.AInsertTableExecute(Sender: TObject);
begin
  InsertTable;
end;

procedure TMainForm.AInsertTableUpdate(Sender: TObject);
var
  B : Boolean;
begin
  B:=(CurrentEditor<>Nil) and CurrentEditor.CanInsertTag(ttTable);
  (Sender as TAction).Enabled:=B;
end;


procedure TMainForm.AInsertShortPrintLinkExecute(Sender: TObject);
begin
  InsertPrintShortLink;
end;


procedure TMainForm.AInsertShortPrintLinkUpdate(Sender: TObject);
var
  B: Boolean;
begin
  B := (CurrentEditor <> nil) and CurrentEditor.CanInsertTag(ttPrintShort);
  (Sender as TAction).Enabled := B;
end;


procedure TMainForm.ANewExecute(Sender: TObject);
begin
  NewFile;
end;


procedure TMainForm.ANewFromFileExecute(Sender: TObject);
begin
  NewFromFile;
end;

procedure TMainForm.ASaveAsExecute(Sender: TObject);
Var
  E : TEditorPage;
begin
  E:=CurrentEditor;
  if (E<>Nil) then
    SaveEditorAs(E);
end;

procedure TMainForm.ASaveExecute(Sender: TObject);
var
  E : TEditorPage;
begin
  E:=CurrentEditor;
  If (E<>Nil) then
  begin
    SaveEditor(E);
    Currenteditor.Update;
  end;
end;

procedure TMainForm.DoFormat(Sender: TObject);
begin
  if Assigned(CurrentEditor) then
    CurrentEditor.InsertTag(TTagType((Sender as TAction).Tag));
end;

procedure TMainForm.HaveEditor(Sender: TObject);
begin
  (Sender as TAction).Enabled:=(CurrentEditor <> nil);
end;

procedure TMainForm.HaveEditorAndModule(Sender: TObject);
begin
  (Sender as TAction).Enabled:=(CurrentEditor <> nil) and
                               (Currenteditor.CurrentModule <> nil);
end;

procedure TMainForm.HaveEditorAndPackage(Sender: TObject);
begin
  (Sender as TAction).Enabled:=(CurrentEditor <> nil) and
                               (Currenteditor.CurrentPackage <> nil);
end;

procedure TMainForm.HaveEditorAndTopicOrSomething(Sender: TObject);
begin
  (Sender as TAction).Enabled:=(CurrentEditor <> nil) and
    (  (Currenteditor.CurrentTopic <> nil)
    or (Currenteditor.CurrentModule <> nil)
    or (Currenteditor.CurrentPackage <> nil) );
end;

procedure TMainForm.InsertStructure(Sender: TObject);
begin
  InsertNode(TNodeType((Sender as TAction).Tag));
end;

procedure TMainForm.MainFormCloseQuery(Sender: TObject; var CanClose: boolean);
begin
  CanClose:=AllowClose;
end;

procedure TMainForm.MainFormDestroy(Sender: TObject);
begin
  SaveOptions;
  SaveRecent;
  FreeAndNil(FRecent);
end;

procedure TMainForm.QuickLinkClick(Sender: TObject);
var
  Selection : string;
begin
  if Assigned(CurrentEditor) then
  begin
    Selection := CurrentEditor.CurrentSelection;
    if Selection <> '' then
      CurrentEditor.InsertLink(CurrentEditor.CurrentElement['name'] + '.' + Selection, Selection)
    else
      ShowMessage(sSelectSomeText);
  end;
end;

procedure TMainForm.BuildReopenList;

  Function NewRecentMenuItem (Nr : Integer;AFileName : string) : TRecentMenuItem;

  begin
    Result:=TRecentMenuItem.Create(Self);
    If Nr<10 then
      Result.Caption:='&'+IntToStr(Nr)+' '+AFileName
    else
      Result.Caption:=AFileName;
    Result.FileName:=AFileName;
    Result.OnCLick:=@FileReopen;
  end;

var I : integer;
    mi : TRecentMenuItem;
begin
  with MIRecent do
    begin
    CLear;
    for I:=FRecent.count-1 downto 0 do
      begin
      mi:=NewRecentMenuItem (I,FRecent[I]);
      Add(mi);
      end;
    end;
end;

procedure TMainForm.AddTorecent(FN: String);
var
  Index : Integer;
begin
  FN:=ExpandFileNameUTF8(FN);
  With FRecent do
    begin
    Index:=IndexOf(FN);
    If Index<>-1 then
      Delete(Index);
    Insert(0,FN);
    While Count>MaxRecentUsed do
      Delete(Count-1);
    end;
  BuildReopenList;
end;

procedure TMainForm.FormLocalization;
begin
  //Localization
  Caption:=sLazDocEditor;
  //File menu
  MFile.Caption:=SMenuFile;
  ANew.Caption:=SMenuFileNew;
  AOpen.Caption:=SMenuFileOpen;
  ANewFromFile.Caption:=SMenuFileNewFromFile;
  ASave.Caption:=SMenuFileSave;
  ASaveAs.Caption:=SMenuFileSaveAs;
  AClose.Caption:=SMenuFileClose;
  AExit.Caption:=SMenuFileQuit;
  MIRecent.Caption:=SMenuFileRecent;

  ANew.Hint:=SHintFileNew;
  AOpen.Hint:=SHintFileOpen;
  ANewFromFile.Hint:=SHintMenuNewFromFile;
  ASave.Hint:=SHintFileSave;
  ASaveAs.Hint:=SHintFileSaveAs;
  AClose.Hint:=SHintFileClose;
  AExit.Hint:=SHintFileExit;

  //Format menu
  AFormatParagraph.Caption:=SMenuFormatParaGraph;
  AFormatBold.Caption:=SMenuFormatBold;
  AFormatItalic.Caption:=SMenuFormatItalics;
  AFormatUnderline.Caption:=SMenuFormatUnderLine;
  AFormatRemark.Caption:=SMenuFormatRemark;
  AFormatVariable.Caption:=SMenuformatVariable;
  AFormatCode.Caption:=SMenuFormatCode;
  AFormatFile.Caption:=SMenuFormatFile;

  AFormatBold.Hint:=SHintFormatBold;
  AFormatItalic.Hint:=SHintFormatItalics;
  AFormatUnderline.Hint:=SHintFormatUnderLine;
  AFormatRemark.Hint:=SHintFormatRemark;
  AFormatVariable.Hint:=SHintFormatVariable;
  AFormatCode.Hint:=SHintFormatCode;
  AFormatFile.Hint:=SHintFormatFile;

  //Insert menu
  MIInsert.Caption:=SMenuInsert;
  AInsertPackage.Caption:=SMenuInsertPackage;
  AInsertModule.Caption:=SMenuInsertModule;
  AInsertElement.Caption:=SMenuInsertElement;
  AInsertTopic.Caption:=SMenuInsertTopic;
  AInsertLink.Caption:=SMenuInsertLink;
  AInsertTable.Caption:=SMenuInsertTable;
  AInsertPrintShort.Caption:=SMenuInsertShortDescLink;
  AInsertQuickLink.Caption:=SMenuInsertQuickLink;
  Lists.Caption := SMenuInsertList;
  AInsertBulletedList.Caption:=SMenuInsertBulletedList;
  AInsertNumberedList.Caption:=SMenuInsertNumberedList;

  AInsertPackage.Hint:=SHintInsertPackage;
  AInsertModule.Hint:=SHintInsertModule;
  AInsertElement.Hint:=SHintInsertElement;
  AInsertTopic.Hint:=SHintInsertTopic;
  AInsertLink.Hint:=SHintInsertLink;
  AInsertTable.Hint:=ShintInsertTable;
  AInsertPrintShort.Hint:=SHintInsertPrintShortLink;
  AInsertBulletedList.Hint:=SHintInsertBulletedList;
  AInsertNumberedList.Hint:=SHintInsertNumberedList;
  //Extra menu
  MIExtra.Caption:=SMenuExtra;
  AExtraOptions.Caption:=SMenuExtraOptions;
  AExtraBuild.Caption:=SMenuExtraBuild;
  AExtraBuild.Hint:=SHMenuExtraOptions;


  //Help menu
  MIHelp.Caption:=SMenuHelp;
  AHelpAbout.Caption:=sMenuHelpAbout;
  AHelpAbout.Hint:=SHMenuHelpAbout;


end;

procedure TMainForm.NewFile;
const
  template = '<?xml version="1.0" encoding="ISO-8859-1"?>'+LineEnding+
             '<fpdoc-descriptions>'+LineEnding+
             '</fpdoc-descriptions>'+LineEnding;
var
  S : TStringStream;
begin
  with CreatePage do
  begin
    if FileExistsUTF8(SFileTemplate) then
      LoadFromFile(UTF8ToSys(SFileTemplate))
    else
    begin
      S:=TStringStream.Create(Template);
      try
        LoadFromStream(S)
      finally
        S.Free;
      end;
    end;
  end;
end;

procedure TMainForm.OpenFile(FN: String; Const AStartNode : String = '');
begin
  if (FN<>'') then
    begin
    If FileExistsUTF8(FN) then
      With CreatePage do
        begin
        LoadFromFile(UTF8ToSys(FN),AStartNode);
        AddToRecent(Fn);
        end;
    end;
end;

procedure TMainForm.SaveEditorAs(E: TEditorPage);
var
  Fn : String;
begin
  with SDMain do
    begin
    FileName:=E.FileName;
    if execute then
      begin
      FN:=FileName;
      If ExtractFileExt(FN)='' then
        FN:=FN+DefaultExtension;
      E.SaveToFile(FN);
      AddToRecent(FN);
      end;
    end;
end;

procedure TMainForm.SaveEditor(E: TEditorPage);

begin
  With E do
    begin
    if (FileName=SNewDocument) then
      SaveEditorAs(E)
    else
      SaveToFile(FileName);
    end;
end;

function TMainForm.CloseEditor(E: TEditorPage): Boolean;
begin
  Result:=Not E.Modified;
  If Not Result then
    Case MessageDlg(SFileModified,[E.FileName],mtConfirmation,mbYesNoCancel,0) of
      mrYes : begin
              SaveEditor(E);
              E.Free;
              Result:=True;
              end;
      mrNo  : begin
              E.Free;
              Result:=True;
              end;
    end
  else
    E.Free;
end;

procedure TMainForm.LoadCommandLine;
var
  I : Integer;
  N : string;
begin
  I:=1;
  While I<=ParamCount do
    begin
    if (Copy(ParamStrUTF8(i),1,10)='--element=') then
      begin
      N:=ParamStrUTF8(i);
      Delete(N,1,10);
      end
    else
      begin
      If FileExistsUTF8(ParamStrUTF8(i)) then
        OpenFile(ParamStrUTF8(I),N);
      N:='';
      end;
    Inc(I);
    end;
end;

procedure TMainForm.LoadRecent;
var
  I,Count : Integer;
  S : String;
begin
  FRecent.Clear;
  With TInifile.Create(UTF8ToSys(GetoptionFileName)) do begin
    Count:=ReadInteger('Recent','Count',0);
    For I:=1 to Count do begin
      S:=ReadString('Recent','File'+IntToStr(i),'');
      If S<>'' then
        FRecent.Add(S);
    end;
    Free;
  end;
  BuildReopenList;
end;

procedure TMainForm.SaveRecent;
var
  I : Integer;
begin
  With TInifile.Create(UTF8ToSys(GetoptionFileName)) do
    try
      EraseSection('Recent');
      WriteInteger('Recent','Count',FRecent.Count);
      For I:=1 to FRecent.Count do
        WriteString('Recent','File'+IntToStr(i),FRecent[i-1]);
      UpdateFile;
    Finally
      Free;
    end;
end;


function TMainForm.CreatePage: TEditorPage;
begin
  Result:=TEditorPage.Create(self);
  Result.Caption:=SNewDocument;
  Result.PageControl:=PCFiles;
  // This should go when it's fixed in the LCL.
  Result.Parent:=PCFiles;
  PCFiles.ActivePage:=Result;
end;

function TMainForm.Currenteditor: TEditorPage;
begin
  If PCFiles.PageCount=0 then
    Result:=Nil
  else
    Result:=PCFiles.ActivePage as TEditorPage;
end;

function TMainForm.AllowClose: Boolean;
begin
  Result:=True;
  While (PCFiles.PageCount>0) and Result do
    Result:=CloseEditor(PCFiles.Pages[PCFiles.PageCount-1] as TEditorPage);
end;

Type
  TSkeletonData = Record
    InputFile,
    OutputFile,
    PackageName,
    AdditionalOptions : String;
    DisableArguments,
    DisableResults,
    DisablePrivate,
    DisableProtected,
    DisableSeeAlso,
    DisableErrors : Boolean;
  end;


Function CreateSkeletonFile(Const S : TSkeletonData) : Boolean;
var
  Cmd : String;
begin
  With S do
    begin
    cmd:=cmdMakeSkel+' ';
    cmd:=cmd+format('"--input=%s %s"',[Inputfile,Additionaloptions]);
    cmd:=cmd+' --output='+OutputFile;
    cmd:=cmd+' --package='+PackageName;
    If DisableErrors then
      cmd:=cmd+' --disable-errors';
    If DisableSeeAlso then
      cmd:=cmd+' --disable-seealso';
    If DisableProtected then
      cmd:=cmd+' --disable-protected'
    else if DisablePrivate then
      cmd:=cmd+' --disable-private';
    If DisableResults then
      cmd:=cmd+' --disable-function-results';
    If DisableArguments then
      cmd:=cmd+' --disable-arguments';
    DebugLn(cmd);
    With TProcessUTF8.Create(Nil) do
      try
        CommandLine:=cmd;
        options:=[poWaitOnExit];
        Execute;
        If (ExitStatus<>0) then
          begin
          DebugLn('error detected ',DbgS(ExitStatus));
          If FileExistsUTF8(OutputFile) then
            Result:=MessageDlg(SSkelErrorWithFile,[ExitStatus],mtWarning,mbYesNo,0)=mrYes
          else
            begin
            MessageDlg(SSkelErrorWithoutFile,[ExitStatus],mtError,[mbOk],0);
            Result:=False;
            end;
          end
        else
          Result:=FileExistsUTF8(OutputFile);
      finally
        Free;
      end;
    end;
end;

Procedure TMainForm.NewFromFile;
var
  SkeletonData : TSkeletonData;
  OK : Boolean;
begin
  With TMakeSkelform.Create(Self) do
    try
      Caption:=SMakeSkelFromSource;
      OK:=ShowModal=mrOK;
      If OK Then
        With SkeletonData do
          begin
          InputFile:=FEInputFile.Text;
          OutputFile:=FEOutputFile.Text;
          PackageName:=EPackage.Text;
          AdditionalOptions:=EAdditionalOptions.Text;
          DisableArguments:=CBDisableArguments.Checked;
          DisableResults:=CBDisableResults.Checked;
          DisablePrivate:=CBDisablePrivate.Checked;
          DisableProtected:=CBDisableProtected.Checked;
          DisableSeeAlso:=CBDisableSeeAlso.Checked;
          DisableErrors:=CBDisableErrors.Checked;
          end;
    Finally
      Free;
    end;
  If OK and CreateSkeletonFile(SkeletonData) then
    OpenFile(SkeletonData.OutPutFile)
end;

Procedure TMainForm.InsertNode(NT : TNodeType) ;

Var
  S: AnsiString;
begin
  If (CurrentEditor<>Nil) then
    With TNewNodeForm.Create(Self) do
      Try
        Case nt of
          ntPackage: S:=sNewPackage+SForFile+ExtractFileName(CurrentEditor.FileName);
          ntModule: If (CurrentEditor.CurrentPackage<>Nil) then
                       S:=sNewModule+SForPackage+CurrentEditor.CurrentPackage['name'];
          ntElement: begin
                     If (CurrentEditor.CurrentModule<>Nil) then
                        S:=sNewElement+SForModule+CurrentEditor.CurrentModule['name'];
                     If Assigned(CurrentEditor.CurrentElement) and
                       (CurrentEditor.CurrentElement <> CurrentEditor.CurrentModule) then
                       begin
                         ENodeName.Text:=CurrentEditor.CurrentElement['name'] + '.' + 'NewElement';
                         ENodeName.SelStart:= Length(CurrentEditor.CurrentElement['name']) + 1;
                         ENodeName.SelLength := Length('NewElement');
                       end
                       else
                         ENodeName.SelText:= 'NewElement';
                     end;
          ntTopic : begin
                    if (CurrentEditor.CurrentTopic<>Nil) then
                        S:=sNewTopic+SForTopic+CurrentEditor.CurrentPackage['name']
                    else if (CurrentEditor.CurrentModule<>Nil) then
                       S:=sNewTopic+SForModule+CurrentEditor.CurrentModule['name']
                    else if (CurrentEditor.CurrentPackage<>Nil) then
                        S:=sNewTopic+SForPackage+CurrentEditor.CurrentPackage['name']
                    end;
        end;
        Caption:=S;
        LENodeName.Caption := sNodeName;
        S:='';
        If ShowModal=mrOK Then
          begin
          S:=ENodeName.Text;
          Case nt of
            ntPackage : CurrentEditor.NewPackage(S);
            ntModule  : CurrentEditor.NewModule(S);
            ntElement : CurrentEditor.NewElement(S);
            ntTopic   : CurrentEditor.NewTopic(S);
          end;
          end;
      finally
        Free;
      end;
end;

procedure TMainForm.InsertLink;
begin
  if Assigned(CurrentEditor) then
    With TLinkForm.Create(Self) do
      Try
        Caption:=SInsertLink;
        LLinkTarget.Caption := SLinkTarget;
        LELinkText.Caption := SLinkText;
        Link:=CurrentEditor.CurrentSelection;
        Links.BeginUpdate;
        Try
          CurrentEditor.GetElementList(Links);
        Finally
          Links.EndUpdate;
        end;
        If ShowModal=mrOK Then
          CurrentEditor.InsertLink(Link,LinkText);
      Finally
        free;
      end;
end;

Procedure TMainForm.InsertTable;
var
  R,C : Integer;
begin
  with TTableForm.Create(Self) do
    try
      Caption:=SInsertTable;
      LSERows.Caption := STableRows;
      LSEColumns.Caption := STableCols;
      CBUseHeaderRow.Caption := STableHeader;
      SERows.Text:=IntToStr(3);
      SEColumns.Text:=IntToStr(3);
      If ShowModal=mrOK Then
        begin
        R:=StrToIntDef(SERows.Text,3);
        C:=StrToIntDef(SEColumns.Text,3);
        CurrentEditor.InsertTable(C,R,CBUseHeaderRow.Checked);
        end;
    Finally
      Free;
    end;
end;


procedure TMainForm.InsertPrintShortLink;
begin
  if Assigned(CurrentEditor) then
  begin
    with TLinkForm.Create(Self) do
      try
        Caption             := SInsertPrintShortLink;
        LELinkText.Caption  := SLinkText;
        LLinkTarget.Caption := SLinkTarget;
        ELinkText.Visible   := False;
        ELinkText.Enabled   := False;
        LELinkText.Visible  := False;
        CBTarget.Items.BeginUpdate;
        try
          CurrentEditor.GetElementList(CBTarget.Items);
          { Find & Select current item. It will be the most used option anyway }
          CBTarget.ItemIndex :=
              CBTarget.Items.IndexOf(CurrentEditor.CurrentElement['name']);
        finally
          CBTarget.Items.EndUpdate;
        end;
        if ShowModal = mrOK then
          CurrentEditor.InsertShortPrintLink(CBTarget.Text);
      finally
        free;
      end;
  end;
end;

procedure TMainForm.InsertEnumerateList;
begin
  with TListForm.Create(Self) do
  try
    Caption:=SInsertNumberedList;
    lblItemsCount.Caption:=sItemsCount;
    speItemsCount.Value:=3;
    If ShowModal=mrOK Then
      CurrentEditor.InsertEnumerateList(speItemsCount.Value);
  Finally
    Free;
  end;
end;

procedure TMainForm.InsertItemizeList;
begin
  with TListForm.Create(Self) do
  try
    Caption:=SInsertBulletedList;
    lblItemsCount.Caption:=sItemsCount;
    speItemsCount.Value:=3;
    If ShowModal=mrOK Then
      CurrentEditor.InsertItemizeList(speItemsCount.Value);
  Finally
    Free;
  end;
end;


procedure TMainForm.ShowAbout;
begin
  with TAboutForm.Create(Self) do
  try
    ShowModal
  finally
    Free;
  end;
end;

procedure TMainForm.GetCurrentFiles(List: TStrings);
var
  I : Integer;
begin
  for I:=0 to PCFiles.PageCount-1 do
    List.Add(TEditorPage(PCFiles.Pages[i]).FileName);
end;

procedure TMainForm.ApplyOptions;
var
  i: Integer;
begin
  ShowHint := ShowHelpHints;
  for i:=0 to ComponentCount-1 do
    if Components[i] is TToolButton then
      TToolButton(Components[i]).ShowHint:=ShowHelpHints;
end;

end.