File: newidewnddlg.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (257 lines) | stat: -rw-r--r-- 7,686 bytes parent folder | download | duplicates (6)
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
unit NewIDEWndDlg;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils,
  // LazUtils
  LazFileUtils, LazLoggerBase, LazUTF8,
  // LCL
  Forms, Controls, Graphics, Dialogs, StdCtrls,
  // IdeIntf
  ProjectIntf, MacroIntf, IDEDialogs,
  // CodeTools
  CodeCache, CodeToolManager,
  // Cody
  CodyStrConsts;

type

  { TFileDescIDEDockableWindow }

  TFileDescIDEDockableWindow = class(TFileDescPascalUnit)
  private
    FMenuItemCaption: string;
    FTemplateImplementation: string;
    FTemplateInterface: string;
    FTemplateLFM: string;
    FTemplateUsesSection: string;
    FTemplateLoaded: boolean;
    function ExtractTemplate(Src, StartMarker, EndMarker: string): string;
    procedure LoadTemplate;
  public
    constructor Create; override;
    function Init(var NewFilename: string; NewOwner: TObject;
      var NewSource: string; Quiet: boolean): TModalResult; override;
    function GetLocalizedName: string; override;
    function GetLocalizedDescription: string; override;
    function GetInterfaceUsesSection: string; override;
    function GetInterfaceSource(const {%H-}Filename, {%H-}SourceName,
                                ResourceName: string): string; override;
    function GetImplementationSource(const {%H-}Filename, {%H-}SourceName,
                                     ResourceName: string): string; override;
    property TemplateUsesSection: string read FTemplateUsesSection write FTemplateUsesSection;
    property TemplateInterface: string read FTemplateInterface write FTemplateInterface;
    property TemplateImplementation: string read FTemplateImplementation write FTemplateImplementation;
    property TemplateLFM: string read FTemplateLFM write FTemplateLFM;
    property MenuItemCaption: string read FMenuItemCaption write FMenuItemCaption;
  end;


  { TNewIDEWndCfgDialog }

  TNewIDEWndCfgDialog = class(TForm)
    FormNameEdit: TEdit;
    FormNameLabel: TLabel;
    FormNameNoteLabel: TLabel;
    MenuItemCaptionEdit: TEdit;
    MenuItemCaptionLabel: TLabel;
    OkButton: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormNameEditChange(Sender: TObject);
    procedure OkButtonClick(Sender: TObject);
  private
    function IsFormNameValid: boolean;
    function IsMenuCaptionValid: boolean;
  public
  end;

implementation

{$R *.lfm}

{ TNewIDEWndCfgDialog }

procedure TNewIDEWndCfgDialog.FormCreate(Sender: TObject);
begin
  Caption:=crsConfigureNewIDEWindow;
  FormNameLabel.Caption:='Form.Name:';
  FormNameEdit.Text:='MyPkgCheetahEditor';
  FormNameNoteLabel.Caption:=
    crsNoteTheNameOfTheFormMustBeAValidPascalIdentifierAn;
  MenuItemCaptionLabel.Caption:=crsMenuItemCaption;
  MenuItemCaptionEdit.Text:=crsCheetahEditor;
  OkButton.Caption:=crsContinue;
end;

procedure TNewIDEWndCfgDialog.FormNameEditChange(Sender: TObject);
begin
  if IsFormNameValid then
    FormNameLabel.Font.Color:=clDefault
  else
    FormNameLabel.Font.Color:=clRed;
end;

procedure TNewIDEWndCfgDialog.OkButtonClick(Sender: TObject);
begin
  if not IsFormNameValid then begin
    IDEMessageDialog('Error','Invalid form name.',mtError,[mbCancel]);
    exit;
  end;
  if not IsMenuCaptionValid then begin
    IDEMessageDialog('Error','Invalid menu caption.',mtError,[mbCancel]);
    exit;
  end;
  ModalResult:=mrOK;
end;

function TNewIDEWndCfgDialog.IsFormNameValid: boolean;
begin
  Result:=IsValidIdent(FormNameEdit.Text);
end;

function TNewIDEWndCfgDialog.IsMenuCaptionValid: boolean;
var
  s: TCaption;
begin
  s:=UTF8Trim(MenuItemCaptionEdit.Text);
  Result:=(s<>'') and (length(s)<255);
end;

{ TFileDescIDEDockableWindow }

function TFileDescIDEDockableWindow.ExtractTemplate(Src, StartMarker,
  EndMarker: string): string;
var
  StartPos: SizeInt;
  EndPos: SizeInt;
begin
  StartPos:=system.Pos(StartMarker,Src);
  EndPos:=system.Pos(EndMarker,Src);
  if StartPos<1 then begin
    DebugLn(['ERROR: TFileDescIDEDockableWindow.ExtractTemplate marker "'+StartMarker+'" not found']);
    debugln('Source: ==================================================');
    debugln(Src);
    debugln('==========================================================');
    exit('');
  end;
  if EndPos<1 then begin
    debugln(['ERROR: TFileDescIDEDockableWindow.ExtractTemplate marker "'+EndMarker+'" not found']);
    exit('');
  end;
  inc(StartPos,length(StartMarker));
  Result:=UTF8Trim(copy(Src,StartPos,EndPos-StartPos));
end;

procedure TFileDescIDEDockableWindow.LoadTemplate;
var
  BaseDir: String;
  Filename: String;
  Code: TCodeBuffer;
begin
  if FTemplateLoaded then exit;

  BaseDir:='$PkgDir(Cody)';
  IDEMacros.SubstituteMacros(BaseDir);
  if (BaseDir='') or (not DirectoryExistsUTF8(BaseDir)) then begin
    debugln(['ERROR: TFileDescIDEDockableWindow.Create cody directory not found']);
    exit;
  end;

  // load unit source
  Filename:=AppendPathDelim(BaseDir)+'templateidedockablewindow.pas';
  if not FileExistsUTF8(Filename) then begin
    debugln(['ERROR: TFileDescIDEDockableWindow.Create template file not found: '+Filename]);
    exit;
  end;
  Code:=CodeToolBoss.LoadFile(Filename,true,false);
  if Code=nil then begin
    debugln(['ERROR: TFileDescIDEDockableWindow.Create unable to read file ',Filename]);
    exit;
  end;

  TemplateUsesSection:=ExtractTemplate(Code.Source,'// UsesStart','// UsesEnd');
  TemplateInterface:=ExtractTemplate(Code.Source,'// InterfaceStart','// InterfaceEnd');
  TemplateImplementation:=ExtractTemplate(Code.Source,'// ImplementationStart','// ImplementationEnd');

  // load lfm
  Filename:=AppendPathDelim(BaseDir)+'templateidedockablewindow.lfm';
  if not FileExistsUTF8(Filename) then begin
    debugln(['ERROR: TFileDescIDEDockableWindow.Create template file not found: '+Filename]);
    exit;
  end;
  Code:=CodeToolBoss.LoadFile(Filename,true,false);
  if Code=nil then begin
    debugln(['ERROR: TFileDescIDEDockableWindow.Create unable to read file ',Filename]);
    exit;
  end;

  TemplateLFM:=Code.Source;
end;

constructor TFileDescIDEDockableWindow.Create;
begin
  inherited Create;
  Name:='IDE window, dockable';
  ResourceClass:=TForm;
  UseCreateFormStatements:=false;
  RequiredPackages:='IDEIntf';
  AddToProject:=false;
  TemplateInterface:='failed loading template';
  MenuItemCaption:='Cheetah Editor';
end;

function TFileDescIDEDockableWindow.Init(var NewFilename: string;
  NewOwner: TObject; var NewSource: string; Quiet: boolean): TModalResult;
var
  Dlg: TNewIDEWndCfgDialog;
begin
  Result:=inherited Init(NewFilename, NewOwner, NewSource, Quiet);
  if not Quiet then begin
    Dlg:=TNewIDEWndCfgDialog.Create(nil);
    try
      Result:=Dlg.ShowModal;
      if Result<>mrOk then exit;
      DefaultResourceName:=Dlg.FormNameEdit.Text;
      MenuItemCaption:=UTF8Trim(Dlg.MenuItemCaptionEdit.Text);
    finally
      Dlg.Free;
    end;
  end;
end;

function TFileDescIDEDockableWindow.GetLocalizedName: string;
begin
  Result:=crsIDEWindowDockable;
end;

function TFileDescIDEDockableWindow.GetLocalizedDescription: string;
begin
  Result:=crsAWindowForTheLazarusIDEItCanBeDockedLikeTheCodeExp;
end;

function TFileDescIDEDockableWindow.GetInterfaceUsesSection: string;
begin
  LoadTemplate;
  Result:=TemplateUsesSection;
end;

function TFileDescIDEDockableWindow.GetInterfaceSource(const Filename,
  SourceName, ResourceName: string): string;
begin
  LoadTemplate;
  Result:=StringReplace(TemplateInterface,'TemplateName',ResourceName,[rfReplaceAll]);
end;

function TFileDescIDEDockableWindow.GetImplementationSource(const Filename,
  SourceName, ResourceName: string): string;
begin
  LoadTemplate;
  Result:=StringReplace(TemplateImplementation,'TemplateName',ResourceName,[rfReplaceAll])
          +LineEnding+LineEnding;
end;

end.