home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 25: Programming / pc_actual_25.iso / Delphi / CompositeComponentsPack / SOURCE / BoxExpt.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-02-12  |  13.1 KB  |  514 lines

  1. {*******************************************************}
  2. {                                                       }
  3. {       Delphi Visual Component Library                 }
  4. {       Composite Components Pack (CCPack)              }
  5. {                                                       }
  6. {       Copyright (c) 1997-99 Sergey Orlik              }
  7. {                                                       }
  8. {     Written by:                                       }
  9. {       Sergey Orlik                                    }
  10. {       product manager                                 }
  11. {       Russia, C.I.S. and Baltic States (former USSR)  }
  12. {       Inprise Moscow office                           }
  13. {       Internet:  sorlik@inprise.ru                    }
  14. {       www.geocities.com/SiliconValley/Way/9006/       }
  15. {                                                       }
  16. {*******************************************************}
  17. {$I BOXDEF.INC}
  18.  
  19. {$IFDEF VER_CB}
  20.   {$ObjExportAll On}
  21. {$ENDIF}
  22.  
  23. {$Warnings Off}
  24.  
  25. unit BoxExpt;
  26.  
  27. interface
  28. uses
  29.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Menus,
  30.   ComCtrls, ExtCtrls, StdCtrls,
  31.   DsgnIntf, ExptIntf, ToolIntf, EditIntf, VirtIntf, TypInfo, Boxes;
  32.  
  33. type
  34.   { TBoxExpert }
  35.  
  36.   TBoxExpert = class(TIExpert)
  37.   public
  38.     function GetName: string; override;
  39.     function GetComment: string; override;
  40.     function GetGlyph: HICON; override;
  41.     function GetStyle: TExpertStyle; override;
  42.     function GetState: TExpertState; override;
  43.     function GetIDString: string; override;
  44.     function GetAuthor: string; override;
  45.     function GetPage: string; override;
  46.     procedure Execute; override;
  47.   end;
  48.  
  49.   TNewBoxDlg = class(TForm)
  50.     Bevel1: TBevel;
  51.     BtnCancel: TButton;
  52.     BtnCreate: TButton;
  53.     Label1: TLabel;
  54.     Label2: TLabel;
  55.     EdClass: TEdit;
  56.     EdPage: TComboBox;
  57.     Label3: TLabel;
  58.     EdAncestor: TComboBox;
  59.     procedure FormCreate(Sender: TObject);
  60.     procedure ClassChange(Sender: TObject);
  61.   end;
  62.  
  63. var
  64.   NewBoxDlg: TNewBoxDlg;
  65.  
  66. procedure Register;
  67.  
  68. implementation
  69.  
  70. {$R *.DFM}
  71. {$R *.RES}
  72.  
  73. const
  74.   CRLF = #13#10;
  75.   CRLF2 = #13#10#13#10;
  76.   DefaultModuleFlags = [cmShowSource, cmShowForm, cmMarkModified, cmUnNamed];
  77.  
  78.   { kind of box}
  79.   bkBox = 0;
  80.   bkControlGroupBox = 1;
  81.   bkControlScrollBox = 2;
  82.   bkToolBarBox = 3;
  83.  
  84.   BoxKind : array[0..3] of string
  85.           = ('Box','ControlGroupBox','ControlScrollBox',
  86.              'ToolBarBox');
  87.  
  88.   { initial size parameters for boxes }
  89.   isBoxWidth : integer = 300;
  90.   isBoxHeight : integer = 200;
  91.   isToolBarBoxWidth : integer = 500;   
  92.   isToolBarBoxHeight : integer = 52; 
  93.  
  94. resourcestring
  95.   sBoxExpertAuthor = 'Sergey Orlik';
  96.   sBoxExpertName   = 'Composite Component';
  97.   sBoxExpertDesc   = 'Creates a new composite component';
  98.  
  99. { TBoxModuleCreator }
  100.  
  101. type
  102.   {$IFDEF VER_VCL4}
  103.   TBoxModuleCreator = class(TIModuleCreatorEx)
  104.   {$ELSE}
  105.   TBoxModuleCreator = class(TIModuleCreator)
  106.   {$ENDIF}
  107.   private
  108.     FClass   : string;
  109.     FPage    : string;
  110.     FBoxKind : integer;
  111.   public
  112.     function Existing: Boolean; override;
  113.     function GetFileName: string; override;
  114.     function GetFileSystem: string; override;
  115.     function GetFormName: string; override;
  116.     function GetAncestorName: string; override;
  117.   {$IFNDEF VER100}
  118.     {$IFDEF VER_CB}
  119.     function GetIntfName: string; override;
  120.     function NewIntfSource(const UnitIdent, FormIdent,
  121.       AncestorIdent: string): string; override;
  122.     {$ENDIF}
  123.     function NewModuleSource(const UnitIdent, FormIdent,
  124.       AncestorIdent: string): string; override;
  125.   {$ELSE}
  126.     function NewModuleSource(UnitIdent, FormIdent,
  127.       AncestorIdent: string): string; override;
  128.   {$ENDIF}
  129.     procedure FormCreated(Form: TIFormInterface); override;
  130.   end;
  131.  
  132. function TBoxModuleCreator.Existing: boolean;
  133. begin
  134.   Result:=False;
  135. end;
  136.  
  137. function TBoxModuleCreator.GetFileName:string;
  138. begin
  139.   Result:='';
  140. end;
  141.  
  142. function TBoxModuleCreator.GetFileSystem:string;
  143. begin
  144.   Result:='';
  145. end;
  146.  
  147. function TBoxModuleCreator.GetFormName:string;
  148. var
  149.   s : string;
  150. begin
  151.   s:=FClass;
  152.   if s<>EmptyStr then
  153.     System.Delete(s,1,1);
  154.   Result:=s;
  155. end;
  156.  
  157. function TBoxModuleCreator.GetAncestorName:string;
  158. begin
  159.   Result:=BoxKind[FBoxKind];
  160. end;
  161.  
  162. {$IFDEF VER_CB}
  163. function UnitName2Namespace(const Value:string):string;
  164. var
  165.   s1,s2 : string;
  166. begin
  167.   s1:=Value[1];
  168.   s2:=LowerCase(Value);
  169.   System.Delete(s2,1,1);
  170.   Result:=UpperCase(s1)+s2;
  171. end;
  172.  
  173. function TBoxModuleCreator.GetIntfName: string;
  174. begin
  175.   Result:='';
  176. end;
  177.  
  178. function TBoxModuleCreator.NewIntfSource(const UnitIdent, FormIdent,
  179.       AncestorIdent: string): string;
  180. begin
  181.   Result:='//---------------------------------------------------------------------------'+
  182.       CRLF+
  183.       '#ifndef '+UnitIdent+'H'+CRLF+
  184.       '#define '+UnitIdent+'H'+CRLF+
  185.       '//---------------------------------------------------------------------------'+
  186.       CRLF+
  187.       '#include <SysUtils.hpp>'+CRLF+
  188.       '#include <Classes.hpp>'+CRLF+
  189.       '#include <Controls.hpp>'+CRLF+
  190.       '#include <StdCtrls.hpp>'+CRLF+
  191.       '#include <Forms.hpp>'+CRLF+
  192.       '#include "Boxes.hpp"'+CRLF+
  193.       '//---------------------------------------------------------------------------'+
  194.       CRLF;
  195.  
  196.   if FClass<>EmptyStr then
  197.     Result:=Result+
  198.       'class '+FClass
  199.   else
  200.     Result:=Result+
  201.       'class T'+FormIdent;
  202.  
  203.     Result:=Result+' : public T'+BoxKind[FBoxKind]+CRLF+
  204.       '{'+CRLF+
  205.       '__published:     // IDE-managed Components'+CRLF;
  206.  
  207.     Result:=Result+
  208.       'private:         // User declarations'+CRLF+
  209.       'protected:       // User declarations'+CRLF+
  210.       'public:          // User declarations'+CRLF;
  211.  
  212.   if FClass<>EmptyStr then
  213.     Result:=Result+
  214.       '        __fastcall '+FClass+'(TComponent* Owner);'+CRLF
  215.   else
  216.     Result:=Result+
  217.       '        __fastcall T'+FormIdent+'(TComponent* Owner);'+CRLF;
  218.  
  219.     Result:=Result+
  220.       '__published:     // User declarations'+CRLF;
  221.       
  222.   if not (FBoxKind in [bkToolBarBox]) then
  223.     Result:=Result+
  224.       '    __property Align;'+CRLF;
  225.  
  226.     Result:=Result+
  227.       '};'+CRLF+
  228.       '//---------------------------------------------------------------------------'+
  229.       CRLF+
  230.       '#endif';
  231. end;
  232.  
  233. function TBoxModuleCreator.NewModuleSource(const UnitIdent, FormIdent,
  234.       AncestorIdent: string): string;
  235. begin
  236.   Result:=
  237.      '//---------------------------------------------------------------------------'+
  238.      CRLF+
  239.      '#include <vcl.h>'+CRLF+
  240.      '#pragma hdrstop'+CRLF2+
  241.      '#include "'+UnitIdent+'.h"'+CRLF+
  242.      '//---------------------------------------------------------------------------'+
  243.      CRLF+
  244.      '#pragma package(smart_init)'+CRLF+
  245.      '#pragma resource "*.dfm"'+CRLF+
  246.      '//---------------------------------------------------------------------------'+
  247.      CRLF+
  248.      '// ValidCtrCheck is used to assure that the components created do not have'+CRLF+
  249.      '// any pure virtual functions.'+CRLF+
  250.      '//'+CRLF+
  251.      'static inline void ValidCtrCheck(T'+FormIdent+' *)'+CRLF+
  252.      '{'+CRLF+
  253.      '        new T'+FormIdent+'(NULL);'+CRLF+
  254.      '}'+CRLF+
  255.      '//---------------------------------------------------------------------------'+
  256.      CRLF+
  257.      '__fastcall T'+FormIdent+'::T'+FormIdent+'(TComponent* Owner)'+CRLF+
  258.      '        : T'+BoxKind[FBoxKind]+'(Owner)'+CRLF+
  259.      '{'+CRLF+
  260.      '}'+CRLF+
  261.      '//---------------------------------------------------------------------------'+
  262.      CRLF;                                             
  263.  
  264.   if FPage<>EmptyStr then
  265.     Result:=Result+
  266.      'namespace '+UnitName2Namespace(UnitIdent)+CRLF+
  267.      '{'+CRLF+
  268.      '        void __fastcall PACKAGE Register()'+CRLF+
  269.      '        {'+CRLF+
  270.      '                 TComponentClass classes[1] = {__classid(T'+FormIdent+')};'+CRLF+
  271.      '                 RegisterComponents("'+FPage+'", classes, 0);'+CRLF+
  272.      '        }'+CRLF+
  273.      '}'+CRLF+
  274.      '//---------------------------------------------------------------------------'+
  275.      CRLF;
  276. end;
  277. {$ELSE}
  278.   {$IFDEF VER100}
  279. function TBoxModuleCreator.NewModuleSource(UnitIdent,FormIdent,AncestorIdent:string):string;
  280.   {$ELSE}
  281. function TBoxModuleCreator.NewModuleSource(const UnitIdent,FormIdent,AncestorIdent:string):string;
  282.   {$ENDIF}
  283. begin
  284.     Result:='unit '+UnitIdent+';'+CRLF2+
  285.       'interface'+CRLF2+
  286.       'uses'+CRLF+
  287.       '  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,'+CRLF+
  288.       '  Boxes, ExtCtrls, StdCtrls, ComCtrls;'+CRLF2+
  289.       'type'+CRLF;
  290.  
  291.   if FClass<>EmptyStr then
  292.     Result:=Result+'  '+FClass
  293.   else
  294.     Result:=Result+'  T'+FormIdent;
  295.  
  296.     Result:=Result+' = class(T'+BoxKind[FBoxKind]+')'+CRLF+
  297.       '  private'+CRLF+
  298.       '    { Private declarations }'+CRLF+
  299.       '  protected'+CRLF+
  300.       '    { Protected declarations }'+CRLF+
  301.       '  public'+CRLF+
  302.       '    { Public declarations }'+CRLF+
  303.       '  published'+CRLF+
  304.       '    { Published declarations }'+CRLF;
  305.  
  306.   if not (FBoxKind in [bkToolBarBox]) then
  307.     Result:=Result+
  308.       '    property Align;'+CRLF;
  309.  
  310.     Result:=Result+
  311.       '  end;'+CRLF2;
  312.  
  313.   if FPage<>EmptyStr then Result:=Result+
  314.       'procedure Register;'+CRLF2;
  315.  
  316.     Result:=Result+
  317.       'implementation'+CRLF2+
  318.       '{$R *.DFM}'+CRLF2;
  319.  
  320.   if FPage<>EmptyStr then
  321.   begin
  322.     Result:=Result+
  323.       'procedure Register;'+CRLF+
  324.       'begin'+CRLF+
  325.       '  RegisterComponents('''+FPage+''',[';
  326.  
  327.     if FClass<>EmptyStr then
  328.       Result:=Result+FClass
  329.     else
  330.       Result:=Result+'T'+FormIdent;
  331.  
  332.       Result:=Result+']);'+CRLF+
  333.       'end;'+CRLF2;
  334.   end;
  335.  
  336.     Result:=Result+
  337.       'end.'+CRLF;
  338. end;
  339. {$ENDIF}
  340.  
  341. procedure TBoxModuleCreator.FormCreated(Form:TIFormInterface);
  342. var
  343.   Comp: TIComponentInterface;
  344. begin
  345.   Comp:=Form.GetFormComponent;
  346.   if FBoxKind=bkToolBarBox then
  347.     begin
  348.       Comp.SetPropByName('Height',isToolBarBoxHeight);
  349.       Comp.SetPropByName('Width',isToolBarBoxWidth);
  350.     end
  351.   else
  352.     begin
  353.       Comp.SetPropByName('Height',isBoxHeight);
  354.       Comp.SetPropByName('Width',isBoxWidth);
  355.     end;  
  356.   Comp.Free;
  357.   Form.Free;
  358. end;
  359.  
  360. { HandleException }
  361.  
  362. procedure HandleException;
  363. begin
  364.   ToolServices.RaiseException(ReleaseException);
  365. end;
  366.  
  367. { BoxExpert }
  368.  
  369. procedure BoxExpert(ToolServices: TIToolServices);
  370. var
  371.   IModuleCreator : TBoxModuleCreator;
  372.   IModule : TIModuleInterface;
  373. begin
  374.   NewBoxDlg:=TNewBoxDlg.Create(Application);
  375.   if NewBoxDlg.ShowModal=mrCancel then
  376.   begin
  377.     NewBoxDlg.Free;
  378.     Exit;
  379.   end;
  380.   IModuleCreator:=TBoxModuleCreator.Create;
  381.   IModuleCreator.FBoxKind:=NewBoxDlg.EdAncestor.ItemIndex;
  382.   IModuleCreator.FClass:=NewBoxDlg.EdClass.Text;
  383.   if IModuleCreator.FClass[1]<>'T' then
  384.     IModuleCreator.FClass:='T'+IModuleCreator.FClass;
  385.   IModuleCreator.FPage:=NewBoxDlg.EdPage.Text;
  386.   try
  387.     {$IFDEF VER_CB}
  388.     IModule:=ToolServices.ModuleCreateEx(IModuleCreator,DefaultModuleFlags);
  389.     {$ELSE}
  390.     IModule:=ToolServices.ModuleCreate(IModuleCreator,DefaultModuleFlags);
  391.     {$ENDIF}
  392.     IModule.Free;
  393.   finally
  394.     IModuleCreator.Free;
  395.     NewBoxDlg.Free;
  396.   end;
  397. end;
  398.  
  399. { TBoxExpert }
  400.  
  401. function TBoxExpert.GetName: string;
  402. begin
  403.   try
  404.     Result := sBoxExpertName;
  405.   except
  406.     HandleException;
  407.   end;
  408. end;
  409.  
  410. function TBoxExpert.GetComment: string;
  411. begin
  412.   try
  413.     Result := sBoxExpertDesc;
  414.   except
  415.     HandleException;
  416.   end;
  417. end;
  418.  
  419. function TBoxExpert.GetGlyph: HICON;
  420. begin
  421.   try
  422.     Result := LoadIcon(HInstance, 'NEWBOX');
  423.   except
  424.     HandleException;
  425.   end;
  426. end;
  427.  
  428. function TBoxExpert.GetStyle: TExpertStyle;
  429. begin
  430.   try
  431.     Result := esForm;
  432.   except
  433.     HandleException;
  434.   end;
  435. end;
  436.  
  437. function TBoxExpert.GetState: TExpertState;
  438. begin
  439.   try
  440.     Result := [esEnabled];
  441.   except
  442.     HandleException;
  443.   end;
  444. end;
  445.  
  446. function TBoxExpert.GetIDString: string;
  447. begin
  448.   try
  449.     Result := 'Borland.'+sBoxExpertName;
  450.   except
  451.     HandleException;
  452.   end;
  453. end;
  454.  
  455. function TBoxExpert.GetAuthor: string;
  456. begin
  457.   try
  458.     Result := sBoxExpertAuthor;
  459.   except
  460.     HandleException;
  461.   end;
  462. end;
  463.  
  464. function TBoxExpert.GetPage: string;
  465. begin
  466.   try
  467.     Result := 'New';
  468.   except
  469.     HandleException;
  470.   end;
  471. end;
  472.  
  473. procedure TBoxExpert.Execute;
  474. begin
  475.   try
  476.     BoxExpert(ToolServices);
  477.   except
  478.     HandleException;
  479.   end;
  480. end;
  481.  
  482. procedure TNewBoxDlg.FormCreate(Sender: TObject);
  483. var
  484.   IDEMainForm : TForm;
  485.   IDEPalTabs  : TTabControl;
  486.   i : integer;
  487. begin
  488.   IDEMainForm:=TForm(Application.MainForm);
  489.   IDEPalTabs:=TTabControl(IDEMainForm.FindComponent('TabControl'));
  490.     for i:=0 to IDEPalTabs.Tabs.Count-1 do
  491.       EdPage.Items.Add(IDEPalTabs.Tabs[i]);
  492.   for i:=0 to High(BoxKind) do
  493.     EdAncestor.Items.Add(BoxKind[i]);
  494.   EdAncestor.ItemIndex:=0;
  495.   EdPage.ItemIndex:=EdPage.Items.IndexOf('Standard');
  496. end;
  497.  
  498. procedure TNewBoxDlg.ClassChange(Sender: TObject);
  499. begin
  500.   if (EdClass.Text=EmptyStr) or not IsValidIdent(EdClass.Text) then
  501.     BtnCreate.Enabled:=False
  502.   else
  503.     BtnCreate.Enabled:=True;
  504. end;
  505.  
  506. { Register }
  507.  
  508. procedure Register;
  509. begin
  510.   RegisterLibraryExpert(TBoxExpert.Create);
  511. end;
  512.  
  513. end.
  514.