home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / kolekce / d123456 / DFS.ZIP / DFSStickyFormReg.pas < prev    next >
Pascal/Delphi Source File  |  2001-06-27  |  10KB  |  339 lines

  1. {$I DFS.INC}  { Standard defines for all Delphi Free Stuff components }
  2.  
  3. unit DFSStickyFormReg;
  4.  
  5. {$IFNDEF DFS_WIN32}
  6.   Error!  This unit is only available for Win32.
  7. {$ENDIF}
  8.  
  9. {$IFNDEF DFS_COMPILER_3_UP}
  10.   Error! This unit is not used by Delphi 2 or C++Builder 1.  Do not install it!
  11. {$ENDIF}
  12.  
  13. interface
  14.  
  15. uses
  16.   ExptIntf, EditIntf, Windows, SysUtils, StdCtrls, DFSStickyForm, DFSAbout,
  17.   Consts;
  18.  
  19. const
  20.   { This is the name of the page in the Object Repository (File | New) that the
  21.     form expert will be created on.  I chose DFS (for Delphi Free Stuff) so
  22.     that it remained seperate from the standard items in the repository.
  23.     However, you may find it more convenient to change this string to 'Forms'
  24.     so that it shows up with all the other new types of forms you have in the
  25.     repository. }
  26.   // The page name for the Sticky Form Wizard
  27.   sStickyFormObjRepositoryPage = 'DFS';
  28.  
  29. type
  30.   {: Registers the class for use in the IDE of Delphi 3, 4, and C++Builder.
  31.      Previous versions of Delphi and C++Builder 1.0 do <B>NOT</>
  32.      support design-time access of TForm descendants.  Sorry.  Unlike a normal
  33.      component, TForm descendant classes must have an expert that creates the
  34.      custom form instance for the process to work. }
  35.   { The IDE expert that allows the class to work at design-time in the IDE }
  36.   TdfsStickyFormExpert = class(TIExpert)
  37.   public
  38.     function GetStyle: TExpertStyle; override;
  39.     function GetName: string; override;
  40.     function GetAuthor: string; override;
  41.     function GetComment: string; override;
  42.     function GetPage: string; override;
  43.     function GetGlyph: HICON; override;
  44.     function GetState: TExpertState; override;
  45.     function GetIDString: string; override;
  46.     function GetMenuText: string; override;
  47.     procedure Execute; override;
  48.   end;
  49.  
  50.   procedure Register;
  51.  
  52. implementation
  53.  
  54. uses
  55.   {$IFDEF DFS_NO_DSGNINTF}
  56.   DesignIntf,
  57.   DesignEditors,
  58.   {$ELSE}
  59.   DsgnIntf,
  60.   {$ENDIF}
  61.   ToolIntf, TypInfo;
  62.  
  63. const
  64.   CRLF = #13#10;
  65.  
  66. procedure Register;
  67. begin
  68.   RegisterCustomModule(TdfsStickyForm, TCustomModule);
  69.   RegisterLibraryExpert(TdfsStickyFormExpert.Create);
  70.   RegisterPropertyEditor(TypeInfo(string), TdfsStickyForm, 'Version',
  71.      TDFSVersionProperty);
  72. end;
  73.  
  74.  
  75. type
  76.   {$IFDEF DFS_DELPHI_3}
  77.   TdfsStickyFormModuleCreator = class(TIModuleCreator)
  78.   {$ELSE}
  79.   TdfsStickyFormModuleCreator = class(TIModuleCreatorEx)
  80.   {$ENDIF}
  81.   private
  82.     FAncestorIdent: string;
  83.     FAncestorClass: TClass;
  84.     FFormIdent: string;
  85.     FUnitIdent: string;
  86.     FFileName: string;
  87.   public
  88.     function Existing: boolean; override;
  89.     function GetFileName: string; override;
  90.     function GetFileSystem: string; override;
  91.     function GetFormName: string; override;
  92.     function GetAncestorName: string; override;
  93.     function NewModuleSource({$IFNDEF DFS_DELPHI_3} const {$ENDIF} UnitIdent,
  94.        FormIdent, AncestorIdent: string): string; override;
  95.     {$IFNDEF DFS_DELPHI_3}
  96.     function GetIntfName: string; override;
  97.     function NewIntfSource(const UnitIdent, FormIdent,
  98.        AncestorIdent: string): string; override;
  99.     {$ENDIF}
  100.     procedure FormCreated(Form: TIFormInterface); override;
  101.   end;
  102.  
  103.  
  104. { TdfsStickyFormModuleCreator }
  105.  
  106. function TdfsStickyFormModuleCreator.Existing: boolean;
  107. begin
  108.   Result := FALSE;
  109. end;
  110.  
  111. function TdfsStickyFormModuleCreator.GetFileName: string;
  112. begin
  113.   Result := '';
  114. end;
  115.  
  116. function TdfsStickyFormModuleCreator.GetFileSystem: string;
  117. begin
  118.   Result := '';
  119. end;
  120.  
  121. function TdfsStickyFormModuleCreator.GetFormName: string;
  122. begin
  123.   Result := FFormIdent;
  124. end;
  125.  
  126. function TdfsStickyFormModuleCreator.GetAncestorName: string;
  127. begin
  128.   Result := FAncestorIdent;
  129. end;
  130.  
  131. {$IFDEF DFS_CPPB}
  132.  
  133. function UnitName2NameSpace(const Value: string): string;
  134. var
  135.   s1, s2: string;
  136. begin
  137.   Result := '';
  138.   if Value <> '' then
  139.   begin
  140.     s1 := Value[1];
  141.     s2 := LowerCase(Value);
  142.     System.Delete(s2, 1, 1);
  143.     Result := UpperCase(s1)+s2;
  144.   end;
  145. end;
  146.  
  147. {$ENDIF}
  148.  
  149. function GetCustomFormUnit(const AClass: TClass): string;
  150. begin
  151.   Result := GetTypeData(PTypeInfo(AClass.ClassInfo))^.UnitName;
  152. end;
  153.  
  154. {$IFNDEF DFS_DELPHI_3}
  155.  
  156. function TdfsStickyFormModuleCreator.GetIntfName: string;
  157. begin
  158.   Result := '';
  159. end;
  160.  
  161. const
  162.   COMMENT_LINE = '//---------------------------------------------------------------------------' + CRLF;
  163.  
  164. function TdfsStickyFormModuleCreator.NewIntfSource(const UnitIdent, FormIdent,
  165.    AncestorIdent: string): string;
  166. begin
  167.   {$IFDEF DFS_CPPB}
  168.   Result := COMMENT_LINE +
  169.      '#ifndef ' + UnitIdent + 'H' + CRLF +
  170.      '#define ' + UnitIdent + 'H' + CRLF +
  171.      COMMENT_LINE +
  172.      '#include <Classes.hpp>' + CRLF +
  173.      '#include <Controls.hpp>' + CRLF +
  174.      '#include <StdCtrls.hpp>' + CRLF +
  175.      '#include <Forms.hpp>' + CRLF ;
  176.  
  177.   if (AncestorIdent <> 'Form') and (FAncestorIdent <> 'DataModule') then
  178.     Result := Result + '#include "' +
  179.        GetCustomFormUnit(FAncestorClass) + '.hpp"' + CRLF;
  180.  
  181.   Result := Result + COMMENT_LINE +
  182.      'class T' + FormIdent + ' : public ' + FAncestorClass.ClassName + CRLF +
  183.      '{' + CRLF +
  184.       '__published: // IDE-managed Components' + CRLF +
  185.       'private: // User declarations' + CRLF +
  186.       'protected: // User declarations' + CRLF +
  187.       'public: // User declarations' + CRLF +
  188.       '  __fastcall T' + FormIdent + '(TComponent* Owner);' + CRLF +
  189.       '__published: // User declarations' + CRLF +
  190.       '};' + CRLF + COMMENT_LINE +
  191.       'extern PACKAGE T'+FormIdent+' *'+FormIdent+';' + CRLF + COMMENT_LINE +
  192.       '#endif';
  193.   {$ELSE}
  194.   Result := ''; // Delphi doesn't use this
  195.   {$ENDIF}
  196. end;
  197.  
  198. {$ENDIF}
  199.  
  200. function TdfsStickyFormModuleCreator.NewModuleSource(
  201.    {$IFNDEF DFS_DELPHI_3} const {$ENDIF} UnitIdent, FormIdent,
  202.    AncestorIdent: string): string;
  203. begin
  204.   {$IFDEF DFS_CPPB}
  205.   Result := '// The features of TdfsStickyForm require Windows 2000 or higher.'+
  206.     CRLF + '// There should be no detrimental effects of using it on a previous'+
  207.     CRLF + '// OS, the form will simply behave as a standard TForm.' + CRLF + CRLF;
  208.   Result := Result + COMMENT_LINE + '#include <vcl.h>' + CRLF +
  209.      '#pragma hdrstop' + CRLF + CRLF +
  210.      '#include "' + UnitIdent + '.h"' + CRLF + COMMENT_LINE +
  211.      '#pragma package(smart_init)' + CRLF;
  212.  
  213.   if (FAncestorIdent <> 'Form') and (FAncestorIdent <> 'DataModule') then
  214.     Result := Result + '#pragma link "' +
  215.        GetCustomFormUnit(FAncestorClass) + '"' + CRLF;
  216.  
  217.   Result := Result + '#pragma resource "*.dfm"' + CRLF +
  218.      'T' + FormIdent + ' *' + FormIdent+';' + CRLF + COMMENT_LINE +
  219.      '__fastcall T' + FormIdent + '::T' + FormIdent + '(TComponent* Owner)' + CRLF +
  220.      '        : ' + FAncestorClass.ClassName + '(Owner)' + CRLF +
  221.      '{' + CRLF +
  222.      '}' + CRLF + COMMENT_LINE;
  223.   {$ELSE}
  224.   Result := 'unit ' + FUnitIdent + ';' + CRLF + CRLF +
  225.      'interface' + CRLF + CRLF +
  226.      'uses' + CRLF +
  227.      '  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs';
  228.  
  229.   if (FAncestorIdent <> 'Form') and (FAncestorIdent <> 'DataModule') then
  230.     Result := Result + ',' + CRLF +
  231.        '  ' + GetCustomFormUnit(FAncestorClass);
  232.  
  233.   Result := Result + ';' + CRLF + CRLF +
  234.      '{ The features of TdfsStickyForm require Windows 2000 or higher.' + CRLF +
  235.      '  There should be no detremental effects of using it on a previous' + CRLF +
  236.      '  OS, the form will simply behave as a standard TForm. }' + CRLF + CRLF +
  237.      'type' + CRLF +
  238.      '  T' + FFormIdent + ' = class(' + FAncestorClass.ClassName + ')' + CRLF +
  239.      '  private' + CRLF +
  240.      '    { Private declarations }' + CRLF +
  241.      '  protected' + CRLF +
  242.      '    { Protected declarations }' + CRLF +
  243.      '  public' + CRLF +
  244.      '    { Public declarations }' + CRLF +
  245.      '  published' + CRLF +
  246.      '    { Published declarations }' + CRLF +
  247.      '  end;' + CRLF + CRLF +
  248.      'var' + CRLF +
  249.      '  ' + FFormIdent + ' : T' + FFormIdent + ';' + CRLF + CRLF +
  250.      'implementation' + CRLF + CRLF +
  251.      '{$R *.DFM}' + CRLF + CRLF +
  252.      'end.' + CRLF;
  253.   {$ENDIF}
  254. end;
  255.  
  256. procedure TdfsStickyFormModuleCreator.FormCreated(Form: TIFormInterface);
  257. begin
  258.   // do nothing
  259. end;
  260.  
  261. { TdfsStickyFormExpert }
  262.  
  263. function TdfsStickyFormExpert.GetStyle: TExpertStyle;
  264. begin
  265.   // Make it show up in the object repository (File | New)
  266.   Result := esForm;
  267. end;
  268.  
  269. function TdfsStickyFormExpert.GetName: String;
  270. begin
  271.   // official name
  272.   Result := 'Sticky Form'
  273. end;
  274.  
  275. function TdfsStickyFormExpert.GetAuthor: string;
  276. begin
  277.   Result := 'Bradley D. Stowers';
  278. end;
  279.  
  280. function TdfsStickyFormExpert.GetComment: String;
  281. begin
  282.   Result := 'Create a new sticky form in current project';
  283. end;
  284.  
  285. function TdfsStickyFormExpert.GetPage: string;
  286. begin
  287.   Result := sStickyFormObjRepositoryPage;
  288. end;
  289.  
  290. function TdfsStickyFormExpert.GetGlyph: HICON;
  291. begin
  292.   Result := LoadIcon(hInstance, 'TdfsStickyForm');
  293. end;
  294.  
  295. function TdfsStickyFormExpert.GetState: TExpertState;
  296. begin
  297.   // not used in a esForm expert
  298.   Result := [esEnabled];
  299. end;
  300.  
  301. function TdfsStickyFormExpert.GetIDString: String;
  302. begin
  303.   // must be unique
  304.   Result := 'DelphiFreeStuff.TdfsStickyFormWizard';
  305. end;
  306.  
  307. function TdfsStickyFormExpert.GetMenuText: string;
  308. begin
  309.   Result := ''; // not used for esForm, just here to shut up the compiler warning.
  310. end;
  311.  
  312. procedure TdfsStickyFormExpert.Execute;
  313. var
  314.   IModuleCreator : TdfsStickyFormModuleCreator;
  315.   IModule : TIModuleInterface;
  316. begin
  317.   IModuleCreator := TdfsStickyFormModuleCreator.Create;
  318.   try
  319.     IModuleCreator.FAncestorIdent := 'dfsStickyForm'; // Don't include the 'T'!!!!
  320.     IModuleCreator.FAncestorClass := TdfsStickyForm;
  321.     ToolServices.GetNewModuleAndClassName(IModuleCreator.FAncestorIdent,
  322.     IModuleCreator.FUnitIdent,IModuleCreator.FFormIdent,IModuleCreator.FFileName);
  323.     {$IFDEF DFS_DELPHI_3}
  324.     IModule := ToolServices.ModuleCreate(IModuleCreator, [cmShowSource,
  325.        cmShowForm, cmMarkModified, cmAddToProject, cmUnNamed]);
  326.     {$ELSE}
  327.     IModule:=ToolServices.ModuleCreateEx(IModuleCreator, [cmShowSource,
  328.        cmShowForm, cmMarkModified, cmAddToProject, cmUnNamed]);
  329.     {$ENDIF}
  330.     IModule.Free;
  331.   finally
  332.     IModuleCreator.Free;
  333.   end;
  334. end;
  335.        
  336.  
  337. end.
  338.  
  339.