1 unit ProjectDescriptorTypes;
2 
3 {$mode objfpc}{$H+}
4 
5 interface
6 
7 uses
8   Classes, SysUtils,
9   // LCL
10   Controls, Forms,
11   // Codetools
12   FileProcs,
13   // LazUtils
14   LazFileUtils, LazUTF8,
15   // IdeIntf
16   CompOptsIntf, ProjectIntf, LazIDEIntf,
17   // IDE
18   frmCustomApplicationOptions, LazarusIDEStrConsts, Project, W32Manifest;
19 
20 type
21 
22   //----------------------------------------------------------------------------
23 
24   { TProjectApplicationDescriptor }
25 
26   TProjectApplicationDescriptor = class(TProjectDescriptor)
27   public
28     constructor Create; override;
GetLocalizedNamenull29     function GetLocalizedName: string; override;
GetLocalizedDescriptionnull30     function GetLocalizedDescription: string; override;
InitProjectnull31     function InitProject(AProject: TLazProject): TModalResult; override;
CreateStartFilesnull32     function CreateStartFiles({%H-}AProject: TLazProject): TModalResult; override;
33   end;
34 
35   { TProjectSimpleProgramDescriptor }
36 
37   TProjectSimpleProgramDescriptor = class(TProjectDescriptor)
38   public
39     constructor Create; override;
GetLocalizedNamenull40     function GetLocalizedName: string; override;
GetLocalizedDescriptionnull41     function GetLocalizedDescription: string; override;
InitProjectnull42     function InitProject(AProject: TLazProject): TModalResult; override;
CreateStartFilesnull43     function CreateStartFiles(AProject: TLazProject): TModalResult; override;
44   end;
45 
46   { TProjectProgramDescriptor }
47 
48   TProjectProgramDescriptor = class(TProjectDescriptor)
49   public
50     constructor Create; override;
GetLocalizedNamenull51     function GetLocalizedName: string; override;
GetLocalizedDescriptionnull52     function GetLocalizedDescription: string; override;
InitProjectnull53     function InitProject(AProject: TLazProject): TModalResult; override;
CreateStartFilesnull54     function CreateStartFiles(AProject: TLazProject): TModalResult; override;
55   end;
56 
57   { TProjectConsoleApplicationDescriptor }
58 
59   TProjectConsoleApplicationDescriptor = class(TProjectDescriptor)
60   public
61     constructor Create; override;
GetLocalizedNamenull62     function GetLocalizedName: string; override;
GetLocalizedDescriptionnull63     function GetLocalizedDescription: string; override;
InitProjectnull64     function InitProject(AProject: TLazProject): TModalResult; override;
CreateStartFilesnull65     function CreateStartFiles(AProject: TLazProject): TModalResult; override;
66   end;
67 
68   { TProjectLibraryDescriptor }
69 
70   TProjectLibraryDescriptor = class(TProjectDescriptor)
71   public
72     constructor Create; override;
GetLocalizedNamenull73     function GetLocalizedName: string; override;
GetLocalizedDescriptionnull74     function GetLocalizedDescription: string; override;
InitProjectnull75     function InitProject(AProject: TLazProject): TModalResult; override;
CreateStartFilesnull76     function CreateStartFiles(AProject: TLazProject): TModalResult; override;
77   end;
78 
79   { TProjectManualProgramDescriptor }
80 
81   TProjectManualProgramDescriptor = class(TProjectDescriptor)
82   private
83     FAddMainSource: boolean;
84   public
85     constructor Create; override;
GetLocalizedNamenull86     function GetLocalizedName: string; override;
GetLocalizedDescriptionnull87     function GetLocalizedDescription: string; override;
InitProjectnull88     function InitProject(AProject: TLazProject): TModalResult; override;
CreateStartFilesnull89     function CreateStartFiles(AProject: TLazProject): TModalResult; override;
90     property AddMainSource: boolean read FAddMainSource write FAddMainSource;
91   end;
92 
93   { TProjectEmptyProgramDescriptor }
94 
95   TProjectEmptyProgramDescriptor = class(TProjectManualProgramDescriptor)
96   public
97     constructor Create; override;
98   end;
99 
100 
101 implementation
102 
103 { TProjectApplicationDescriptor }
104 
105 constructor TProjectApplicationDescriptor.Create;
106 begin
107   inherited Create;
108   Name:=ProjDescNameApplication;
109   Flags:=Flags+[pfUseDefaultCompilerOptions];
110 end;
111 
TProjectApplicationDescriptor.GetLocalizedNamenull112 function TProjectApplicationDescriptor.GetLocalizedName: string;
113 begin
114   Result:=dlgPOApplication;
115 end;
116 
GetLocalizedDescriptionnull117 function TProjectApplicationDescriptor.GetLocalizedDescription: string;
118 begin
119   Result:=lisApplicationProgramDescriptor;
120 end;
121 
InitProjectnull122 function TProjectApplicationDescriptor.InitProject(AProject: TLazProject): TModalResult;
123 var
124   NewSource: String;
125   MainFile: TLazProjectFile;
126 begin
127   Result:=inherited InitProject(AProject);
128 
129   MainFile:=AProject.CreateProjectFile('project1.lpr');
130   MainFile.IsPartOfProject:=true;
131   AProject.AddFile(MainFile,false);
132   AProject.MainFileID:=0;
133   AProject.UseAppBundle:=true;
134   AProject.UseManifest:=true;
135   AProject.Scaled:=true;
136   (AProject as TProject).ProjResources.XPManifest.DpiAware := xmdaTrue;
137   AProject.LoadDefaultIcon;
138 
139   // create program source
140   NewSource:='program Project1;'+LineEnding
141     +LineEnding
142     +'{$mode objfpc}{$H+}'+LineEnding
143     +LineEnding
144     +'uses'+LineEnding
145     +'  {$IFDEF UNIX}'+LineEnding
146     +'  cthreads,'+LineEnding
147     +'  {$ENDIF}'+LineEnding
148     +'  {$IFDEF HASAMIGA}'+LineEnding
149     +'  athreads,'+LineEnding
150     +'  {$ENDIF}'+LineEnding
151     +'  Interfaces, // this includes the LCL widgetset'+LineEnding
152     +'  Forms'+LineEnding
153     +'  { you can add units after this };'+LineEnding
154     +LineEnding
155     +'begin'+LineEnding
156     +'  RequireDerivedFormResource:=True;'+LineEnding
157     +'  Application.Scaled:=True;'+LineEnding
158     +'  Application.Initialize;'+LineEnding
159     +'  Application.Run;'+LineEnding
160     +'end.'+LineEnding
161     +LineEnding;
162   AProject.MainFile.SetSourceText(NewSource,true);
163 
164   // add lcl pp/pas dirs to source search path
165   AProject.AddPackageDependency('LCL');
166   AProject.LazCompilerOptions.Win32GraphicApp:=true;
167   AProject.LazCompilerOptions.UnitOutputDirectory:='lib'+PathDelim+'$(TargetCPU)-$(TargetOS)';
168   AProject.LazCompilerOptions.TargetFilename:='project1';
169 end;
170 
TProjectApplicationDescriptor.CreateStartFilesnull171 function TProjectApplicationDescriptor.CreateStartFiles(AProject: TLazProject
172   ): TModalResult;
173 begin
174   Result:=LazarusIDE.DoNewEditorFile(FileDescriptorForm,'','',
175                          [nfIsPartOfProject,nfOpenInEditor,nfCreateDefaultSrc]);
176 end;
177 
178 { TProjectSimpleProgramDescriptor }
179 
180 constructor TProjectSimpleProgramDescriptor.Create;
181 begin
182   inherited Create;
183   Name:=ProjDescNameSimpleProgram;
184   Flags:=Flags-[pfMainUnitHasCreateFormStatements,pfMainUnitHasTitleStatement,pfMainUnitHasScaledStatement]
185               +[pfUseDefaultCompilerOptions];
186 end;
187 
GetLocalizedNamenull188 function TProjectSimpleProgramDescriptor.GetLocalizedName: string;
189 begin
190   Result:=lisSimpleProgram;
191 end;
192 
GetLocalizedDescriptionnull193 function TProjectSimpleProgramDescriptor.GetLocalizedDescription: string;
194 begin
195   Result:=lisSimpleProgramProgramDescriptor;
196 end;
197 
InitProjectnull198 function TProjectSimpleProgramDescriptor.InitProject(AProject: TLazProject): TModalResult;
199 var
200   NewSource: String;
201   MainFile: TLazProjectFile;
202 begin
203   Result:=inherited InitProject(AProject);
204 
205   MainFile:=AProject.CreateProjectFile('project1.lpr');
206   MainFile.IsPartOfProject:=true;
207   AProject.AddFile(MainFile,false);
208   AProject.MainFileID:=0;
209 
210   // create program source
211   NewSource:='program Project1;'+LineEnding
212     +LineEnding
213     +'begin'+LineEnding
214     +'end.'+LineEnding
215     +LineEnding;
216   AProject.MainFile.SetSourceText(NewSource,true);
217 
218   AProject.LazCompilerOptions.UnitOutputDirectory:='lib'+PathDelim+'$(TargetCPU)-$(TargetOS)';
219   AProject.LazCompilerOptions.TargetFilename:='project1';
220 end;
221 
TProjectSimpleProgramDescriptor.CreateStartFilesnull222 function TProjectSimpleProgramDescriptor.CreateStartFiles(AProject: TLazProject): TModalResult;
223 begin
224   Result:=LazarusIDE.DoOpenEditorFile(AProject.MainFile.Filename,-1,-1,
225                                       [ofProjectLoading,ofRegularFile]);
226 end;
227 
228 { TProjectProgramDescriptor }
229 
230 constructor TProjectProgramDescriptor.Create;
231 begin
232   inherited Create;
233   Name:=ProjDescNameProgram;
234   Flags:=Flags-[pfMainUnitHasCreateFormStatements,pfMainUnitHasTitleStatement,pfMainUnitHasScaledStatement]
235               +[pfUseDefaultCompilerOptions];
236 end;
237 
GetLocalizedNamenull238 function TProjectProgramDescriptor.GetLocalizedName: string;
239 begin
240   Result:=lisProgram;
241 end;
242 
GetLocalizedDescriptionnull243 function TProjectProgramDescriptor.GetLocalizedDescription: string;
244 begin
245   Result:=lisProgramProgramDescriptor;
246 end;
247 
InitProjectnull248 function TProjectProgramDescriptor.InitProject(AProject: TLazProject): TModalResult;
249 var
250   NewSource: String;
251   MainFile: TLazProjectFile;
252 begin
253   Result:=inherited InitProject(AProject);
254 
255   MainFile:=AProject.CreateProjectFile('project1.lpr');
256   MainFile.IsPartOfProject:=true;
257   AProject.AddFile(MainFile,false);
258   AProject.MainFileID:=0;
259 
260   // create program source
261   NewSource:='program Project1;'+LineEnding
262     +LineEnding
263     +'{$mode objfpc}{$H+}'+LineEnding
264     +LineEnding
265     +'uses'+LineEnding
266     +'  {$IFDEF UNIX}'+LineEnding
267     +'  cthreads,'+LineEnding
268     +'  {$ENDIF}'+LineEnding
269     +'  Classes'+LineEnding
270     +'  { you can add units after this };'+LineEnding
271     +LineEnding
272     +'begin'+LineEnding
273     +'end.'+LineEnding
274     +LineEnding;
275   AProject.MainFile.SetSourceText(NewSource,true);
276 
277   AProject.LazCompilerOptions.UnitOutputDirectory:='lib'+PathDelim+'$(TargetCPU)-$(TargetOS)';
278   AProject.LazCompilerOptions.TargetFilename:='project1';
279 end;
280 
TProjectProgramDescriptor.CreateStartFilesnull281 function TProjectProgramDescriptor.CreateStartFiles(AProject: TLazProject): TModalResult;
282 begin
283   Result:=LazarusIDE.DoOpenEditorFile(AProject.MainFile.Filename,-1,-1,
284                                       [ofProjectLoading,ofRegularFile]);
285 end;
286 
287 { TProjectManualProgramDescriptor }
288 
289 constructor TProjectManualProgramDescriptor.Create;
290 begin
291   inherited Create;
292   VisibleInNewDialog:=false;
293   Name:=ProjDescNameCustomProgram;
294   Flags:=Flags-[pfMainUnitHasUsesSectionForAllUnits,
295                 pfMainUnitHasCreateFormStatements,
296                 pfMainUnitHasTitleStatement,
297                 pfMainUnitHasScaledStatement]
298               +[pfUseDefaultCompilerOptions];
299   FAddMainSource:=true;
300 end;
301 
TProjectManualProgramDescriptor.GetLocalizedNamenull302 function TProjectManualProgramDescriptor.GetLocalizedName: string;
303 begin
304   Result:=lisCustomProgram;
305 end;
306 
TProjectManualProgramDescriptor.GetLocalizedDescriptionnull307 function TProjectManualProgramDescriptor.GetLocalizedDescription: string;
308 begin
309   Result:=lisCustomProgramProgramDescriptor;
310 end;
311 
InitProjectnull312 function TProjectManualProgramDescriptor.InitProject(AProject: TLazProject): TModalResult;
313 var
314   NewSource: String;
315   MainFile: TLazProjectFile;
316 begin
317   Result:=inherited InitProject(AProject);
318 
319   if AddMainSource then begin
320     MainFile:=AProject.CreateProjectFile('project1.pas');
321     MainFile.IsPartOfProject:=true;
322     AProject.AddFile(MainFile,false);
323     AProject.MainFileID:=0;
324 
325     // create program source
326     NewSource:='program Project1;'+LineEnding
327       +LineEnding
328       +'{$mode objfpc}{$H+}'+LineEnding
329       +LineEnding
330       +'uses'+LineEnding
331       +'  Classes, SysUtils'+LineEnding
332       +'  { you can add units after this };'+LineEnding
333       +LineEnding
334       +'begin'+LineEnding
335       +'end.'+LineEnding
336       +LineEnding;
337     AProject.MainFile.SetSourceText(NewSource,true);
338     AProject.LazCompilerOptions.Win32GraphicApp:=false;
339   end;
340 end;
341 
TProjectManualProgramDescriptor.CreateStartFilesnull342 function TProjectManualProgramDescriptor.CreateStartFiles(AProject: TLazProject
343   ): TModalResult;
344 begin
345   if AProject.MainFile<>nil then
346     Result:=LazarusIDE.DoOpenEditorFile(AProject.MainFile.Filename,-1,-1,
347                                         [ofProjectLoading,ofRegularFile])
348   else
349     Result:=mrCancel;
350 end;
351 
352 { TProjectEmptyProgramDescriptor }
353 
354 constructor TProjectEmptyProgramDescriptor.Create;
355 begin
356   inherited Create;
357   FAddMainSource:=false;
358 end;
359 
360 { TProjectConsoleApplicationDescriptor }
361 
362 constructor TProjectConsoleApplicationDescriptor.Create;
363 begin
364   inherited Create;
365   Name:=ProjDescNameConsoleApplication;
366   Flags:=Flags-[pfMainUnitHasCreateFormStatements,pfMainUnitHasTitleStatement,pfMainUnitHasScaledStatement]
367               +[pfUseDefaultCompilerOptions];
368 end;
369 
TProjectConsoleApplicationDescriptor.GetLocalizedNamenull370 function TProjectConsoleApplicationDescriptor.GetLocalizedName: string;
371 begin
372   Result:=lisConsoleApplication;
373 end;
374 
GetLocalizedDescriptionnull375 function TProjectConsoleApplicationDescriptor.GetLocalizedDescription: string;
376 begin
377   Result:=lisConsoleApplicationProgramDescriptor;
378 end;
379 
InitProjectnull380 function TProjectConsoleApplicationDescriptor.InitProject(AProject: TLazProject
381   ): TModalResult;
382 var
383   NewSource: TStringList;
384   MainFile: TLazProjectFile;
385   C, T : String;
386   CC,CD,CU,CS, CO : Boolean;
387 
388 begin
389   Result:=inherited InitProject(AProject);
390   If Result<>mrOk then
391     Exit;
392   With TCustomApplicationOptionsForm.Create(Application) do
393     try
394       Result:=ShowModal;
395       If Result<>mrOk then
396         Exit;
397       C:=Trim(AppClassName);
398       T:=StringReplace(Title,'''','''''',[rfReplaceAll]);
399       CC:=CodeConstructor;
400       CD:=CodeDestructor;
401       CU:=CodeUsage;
402       CS:=CodeStopOnError;
403       CO:=CodeCheckOptions;
404     finally
405       Free;
406     end;
407   MainFile:=AProject.CreateProjectFile('project1.lpr');
408   MainFile.IsPartOfProject:=true;
409   AProject.AddFile(MainFile,false);
410   AProject.MainFileID:=0;
411 
412   AProject.LazCompilerOptions.UnitOutputDirectory:='lib'+PathDelim+'$(TargetCPU)-$(TargetOS)';
413   AProject.LazCompilerOptions.TargetFilename:='project1';
414   AProject.LazCompilerOptions.Win32GraphicApp:=false;
415 
416   // create program source
417   NewSource:=TStringList.Create;
418   NewSource.Add('program Project1;');
419   NewSource.Add('');
420   NewSource.Add('{$mode objfpc}{$H+}');
421   NewSource.Add('');
422   NewSource.Add('uses');
423   NewSource.Add('  {$IFDEF UNIX}');
424   NewSource.Add('  cthreads,');
425   NewSource.Add('  {$ENDIF}');
426   NewSource.Add('  Classes, SysUtils, CustApp');
427   NewSource.Add('  { you can add units after this };');
428   NewSource.Add('');
429   NewSource.Add('type');
430   NewSource.Add('');
431   NewSource.Add('  { '+C+' }');
432   NewSource.Add('');
433   NewSource.Add('  '+C+' = class(TCustomApplication)');
434   NewSource.Add('  protected');
435   NewSource.Add('    procedure DoRun; override;');
436   NewSource.Add('  public');
437   If CC or CS then
438     NewSource.Add('    constructor Create(TheOwner: TComponent); override;');
439   if CD then
440     NewSource.Add('    destructor Destroy; override;');
441   if CU then
442     NewSource.Add('    procedure WriteHelp; virtual;');
443   NewSource.Add('  end;');
444   NewSource.Add('');
445   NewSource.Add('{ '+C+' }');
446   NewSource.Add('');
447   NewSource.Add('procedure '+C+'.DoRun;');
448   NewSource.Add('var');
449   NewSource.Add('  ErrorMsg: String;');
450   NewSource.Add('begin');
451   if CO then
452     begin
453     NewSource.Add('  // quick check parameters');
454     NewSource.Add('  ErrorMsg:=CheckOptions(''h'',''help'');');
455     NewSource.Add('  if ErrorMsg<>'''' then begin');
456     NewSource.Add('    ShowException(Exception.Create(ErrorMsg));');
457     NewSource.Add('    Terminate;');
458     NewSource.Add('    Exit;');
459     NewSource.Add('  end;');
460     NewSource.Add('');
461     end;
462   If CU then
463     begin
464     NewSource.Add('  // parse parameters');
465     NewSource.Add('  if HasOption(''h'',''help'') then begin');
466     NewSource.Add('    WriteHelp;');
467     NewSource.Add('    Terminate;');
468     NewSource.Add('    Exit;');
469     NewSource.Add('  end;');
470     end;
471   NewSource.Add('');
472   NewSource.Add('  { add your program here }');
473   NewSource.Add('');
474   NewSource.Add('  // stop program loop');
475   NewSource.Add('  Terminate;');
476   NewSource.Add('end;');
477   NewSource.Add('');
478   If CC or CS then
479     begin
480     NewSource.Add('constructor '+C+'.Create(TheOwner: TComponent);');
481     NewSource.Add('begin');
482     NewSource.Add('  inherited Create(TheOwner);');
483     If CS then
484     NewSource.Add('  StopOnException:=True;');
485     NewSource.Add('end;');
486     NewSource.Add('');
487     end;
488   If CD then
489     begin
490     NewSource.Add('destructor '+C+'.Destroy;');
491     NewSource.Add('begin');
492     NewSource.Add('  inherited Destroy;');
493     NewSource.Add('end;');
494     NewSource.Add('');
495     end;
496   If CU then
497     begin
498     NewSource.Add('procedure '+C+'.WriteHelp;');
499     NewSource.Add('begin');
500     NewSource.Add('  { add your help code here }');
501     NewSource.Add('  writeln(''Usage: '',ExeName,'' -h'');');
502     NewSource.Add('end;');
503     NewSource.Add('');
504     end;
505   NewSource.Add('var');
506   NewSource.Add('  Application: '+C+';');
507   NewSource.Add('begin');
508   NewSource.Add('  Application:='+C+'.Create(nil);');
509   If (T<>'') then
510     begin
511     AProject.Flags:=AProject.Flags+[pfMainUnitHasTitleStatement];
512     AProject.Title:=T;
513     NewSource.Add('  Application.Title:='''+T+''';');
514     end;
515   NewSource.Add('  Application.Run;');
516   NewSource.Add('  Application.Free;');
517   NewSource.Add('end.');
518   NewSource.Add('');
519   AProject.MainFile.SetSourceText(NewSource.Text,true);
520   NewSource.Free;
521 end;
522 
TProjectConsoleApplicationDescriptor.CreateStartFilesnull523 function TProjectConsoleApplicationDescriptor.CreateStartFiles(
524   AProject: TLazProject): TModalResult;
525 begin
526   Result:=LazarusIDE.DoOpenEditorFile(AProject.MainFile.Filename,-1,-1,
527                                       [ofProjectLoading,ofRegularFile]);
528 end;
529 
530 { TProjectLibraryDescriptor }
531 
532 constructor TProjectLibraryDescriptor.Create;
533 begin
534   inherited Create;
535   Name:=ProjDescNameLibrary;
536   Flags:=Flags-[pfMainUnitHasCreateFormStatements,pfMainUnitHasTitleStatement,pfMainUnitHasScaledStatement]
537               +[pfUseDefaultCompilerOptions];
538 end;
539 
TProjectLibraryDescriptor.GetLocalizedNamenull540 function TProjectLibraryDescriptor.GetLocalizedName: string;
541 begin
542   Result:=lisPckOptsLibrary;
543 end;
544 
GetLocalizedDescriptionnull545 function TProjectLibraryDescriptor.GetLocalizedDescription: string;
546 begin
547   Result:=lisLibraryProgramDescriptor;
548 end;
549 
InitProjectnull550 function TProjectLibraryDescriptor.InitProject(AProject: TLazProject): TModalResult;
551 var
552   NewSource: String;
553   MainFile: TLazProjectFile;
554 begin
555   Result:=inherited InitProject(AProject);
556 
557   MainFile:=AProject.CreateProjectFile('project1.lpr');
558   MainFile.IsPartOfProject:=true;
559   AProject.AddFile(MainFile,false);
560   AProject.MainFileID:=0;
561   AProject.LazCompilerOptions.ExecutableType:=cetLibrary;
562 
563   // create program source
564   NewSource:='library Project1;'+LineEnding
565     +LineEnding
566     +'{$mode objfpc}{$H+}'+LineEnding
567     +LineEnding
568     +'uses'+LineEnding
569     +'  Classes'+LineEnding
570     +'  { you can add units after this };'+LineEnding
571     +LineEnding
572     +'begin'+LineEnding
573     +'end.'+LineEnding
574     +LineEnding;
575   AProject.MainFile.SetSourceText(NewSource,true);
576 
577   AProject.LazCompilerOptions.UnitOutputDirectory:='lib'+PathDelim+'$(TargetCPU)-$(TargetOS)';
578   AProject.LazCompilerOptions.TargetFilename:='project1';
579   AProject.LazCompilerOptions.Win32GraphicApp:=false;
580   AProject.LazCompilerOptions.RelocatableUnit:=true;
581 end;
582 
TProjectLibraryDescriptor.CreateStartFilesnull583 function TProjectLibraryDescriptor.CreateStartFiles(AProject: TLazProject): TModalResult;
584 begin
585   Result:=LazarusIDE.DoOpenEditorFile(AProject.MainFile.Filename,-1,-1,
586                                       [ofProjectLoading,ofRegularFile]);
587 end;
588 
589 end.
590 
591