home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 March / Chip_1998-03_cd.bin / zkuste / delphi / WhiteAnts / CMPNAMES.ZIP / DsgnWrap.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-02-14  |  9.9 KB  |  305 lines

  1. {
  2. +----------------------------------------------------------------------------+
  3. |                                      ⌐  ⌐                                  |
  4. |                                    ⌐⌐ ⌐ ⌐ ⌐                                |
  5. |                                 ⌐⌐⌐ ⌐   ⌐  ⌐                               |
  6. |                                 ⌐⌐    ⌐ ⌐   ⌐                              |
  7. |                  ⌐             ⌐⌐     ⌐  ⌐                                 |
  8. |                 ⌐ ⌐            ⌐⌐⌐    ⌐⌐  ⌐                                |
  9. |             ⌐⌐  ⌐  ⌐      ⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐  ⌐                                    |
  10. |            ⌐  ⌐⌐  ⌐⌐      ⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐                                  |
  11. |            ⌐ ⌐⌐⌐ ⌐⌐⌐⌐⌐⌐ ⌐⌐⌐ ⌐   ⌐⌐⌐⌐⌐⌐⌐⌐                                   |
  12. |           ⌐ ⌐⌐ ⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐ ⌐   ⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐      Copyright ⌐ 1996-1997 by:  |
  13. |           ⌐ ⌐⌐⌐⌐⌐⌐ ⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐⌐  ⌐ ⌐⌐⌐⌐⌐ ⌐⌐                                 |
  14. |          ⌐ ⌐⌐⌐⌐⌐⌐⌐   ⌐⌐⌐⌐⌐ ⌐⌐⌐⌐    ⌐⌐ ⌐⌐ ⌐      WHITE ANTS SYSTEMHOUSE BV  |
  15. |         ⌐  ⌐⌐⌐⌐⌐⌐⌐ ⌐⌐⌐ ⌐⌐⌐ ⌐⌐ ⌐       ⌐⌐⌐⌐      Geleen 12                  |
  16. |         ⌐ ⌐⌐⌐⌐⌐⌐⌐    ⌐   ⌐⌐   ⌐⌐⌐       ⌐       8032 GB Zwolle             |
  17. |           ⌐⌐⌐⌐⌐⌐     ⌐            ⌐ ⌐           Netherlands                |
  18. |      ⌐⌐⌐  ⌐⌐⌐⌐⌐      ⌐     ⌐⌐     ⌐  ⌐                                     |
  19. |            ⌐⌐       ⌐              ⌐  ⌐⌐⌐ ⌐     Tel. +31 38 453 86 31      |
  20. |      ⌐              ⌐              ⌐            Fax. +31 38 453 41 22      |
  21. |      ⌐             ⌐               ⌐⌐                                      |
  22. |    ⌐              ⌐                  ⌐⌐         www.whiteants.com          |
  23. |  ⌐⌐              ⌐                     ⌐ ⌐      support@whiteants.com      |
  24. |                 ⌐                                                          |
  25. +----------------------------------------------------------------------------+
  26.   file     : DsgnWrap
  27.   version  : 1.01
  28.   comment  : 
  29.   date     : 14-02-1997
  30.   time     : 13:58:00
  31.   author   : G.Beuze, R.Post
  32.   compiler : Delphi 1.0
  33. +----------------------------------------------------------------------------+
  34. | DISCLAIMER:                                                                |
  35. | THIS SOURCE IS FREEWARE. YOU ARE ALLOWED TO USE IT IN YOUR OWN PROJECTS    |
  36. | WITHOUT ANY RESTRICTIONS, BUT YOU ARE NOT ALLOWED TO SELL IT IN ANY WAY.   |
  37. | THERE IS NO WARRANTY AT ALL - YOU USE IT ON YOUR OWN RISC. WHITE ANTS DOES |
  38. | NOT ASSUME ANY RESPONSIBILITY FOR ANY DAMAGE OR ANY LOOSE OF TIME OR MONEY |
  39. | DUE THE USE OF ANY PART OF THIS SOURCE CODE.                               |
  40. +----------------------------------------------------------------------------+
  41.  
  42. Description:
  43. }
  44.  
  45. unit DsgnWrap;
  46. {MMWIN:ENDEXPAND}
  47.  
  48. interface
  49.  
  50. uses WinTypes, WinProcs, Messages, Classes, SysUtils, Controls, Forms, 
  51.      TypInfo, DsgnIntf;
  52.  
  53. type
  54.   TNotificationEvent = procedure (Sender: TObject; AComponent: TComponent; Operation: 
  55.       TOperation) of object;
  56.  
  57.   TValidateRenameEvent = procedure (Sender: TObject; AComponent: TComponent; const 
  58.       CurName, NewName: string) of object;
  59.  
  60.   TDesignerDecorator = class (TFormDesigner)
  61.   private
  62.     FDesigner: TFormDesigner;
  63.     FOnNotification: TNotificationEvent;
  64.     FOnUnhookError: TNotifyEvent;
  65.     FOnValidateRename: TValidateRenameEvent;
  66.     FOwnsDesigner: Boolean;
  67.   protected
  68.     function GetDesigner: TFormDesigner;
  69.     function GetIsControl: Boolean;
  70.     procedure SetDesigner(Value: TFormDesigner);
  71.     procedure SetIsControl(Value: Boolean);
  72.     procedure UnhookError;
  73.   public
  74.     constructor Create;
  75.     destructor Destroy; override;
  76.     function CreateMethod(const Name:string;TypeData:PTypeData): TMethod; override;
  77.     function GetMethodName(const Method: TMethod): string; override;
  78.     procedure GetMethods(TypeData: PTypeData; Proc: TGetStrProc); override;
  79.     function GetPrivateDirectory: string; override;
  80.     procedure Hook(aForm: TForm); dynamic;
  81.     function IsDesignMsg(Sender: TControl; var Message: TMessage): Boolean; override;
  82.     function MethodExists(const AName: string): Boolean; override;
  83.     procedure Modified; override;
  84.     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  85.     procedure PaintGrid; override;
  86.     procedure RenameMethod(const CurName, NewName: string); override;
  87.     procedure ShowMethod(const Name: string); override;
  88.     procedure Unhook; dynamic;
  89.     procedure ValidateRename(AComponent: TComponent; const CurName, NewName: string); 
  90.         override;
  91.     property Designer: TFormDesigner read GetDesigner write SetDesigner;
  92.     property IsControl read GetIsControl write SetIsControl;
  93.     property OnNotification: TNotificationEvent read FOnNotification write 
  94.         FOnNotification;
  95.     property OnUnhookError: TNotifyEvent read FOnUnhookError write FOnUnhookError;
  96.     property OnValidateRename: TValidateRenameEvent read FOnValidateRename write 
  97.         FOnValidateRename;
  98.     property OwnsDesigner: Boolean read FOwnsDesigner write FOwnsDesigner;
  99.   end;
  100.  
  101.  
  102. implementation
  103.  
  104. {
  105. *********************************** TDesignerDecorator ***********************************
  106. }
  107. constructor TDesignerDecorator.Create;
  108. begin
  109.   inherited Create;
  110. end;
  111.  
  112. destructor TDesignerDecorator.Destroy;
  113. begin
  114.   Unhook;
  115.   Designer := nil;
  116.   inherited Destroy;
  117. end;
  118.  
  119. function TDesignerDecorator.CreateMethod(const Name:string;TypeData:PTypeData): TMethod;
  120. begin
  121.   if (Designer = nil) then
  122.     FillChar(Result, SizeOf(Result), 0)
  123.   else
  124.     Result := Designer.CreateMethod(Name, TypeData);
  125. end;
  126.  
  127. function TDesignerDecorator.GetDesigner: TFormDesigner;
  128. begin
  129.   Result := FDesigner;
  130. end;
  131.  
  132. function TDesignerDecorator.GetIsControl: Boolean;
  133. begin
  134.   if (Designer = nil) then
  135.     Result := False
  136.   else
  137.     Result := Designer.IsControl;
  138. end;
  139.  
  140. function TDesignerDecorator.GetMethodName(const Method: TMethod): string;
  141. begin
  142.   if (Designer = nil) then
  143.     Result := ''
  144.   else
  145.     Result := Designer.GetMethodName(Method);
  146. end;
  147.  
  148. procedure TDesignerDecorator.GetMethods(TypeData: PTypeData; Proc: TGetStrProc);
  149. begin
  150.   if (Designer <> nil) then
  151.     Designer.GetMethods(TypeData, Proc);
  152. end;
  153.  
  154. function TDesignerDecorator.GetPrivateDirectory: string;
  155. begin
  156.   if (Designer = nil) then
  157.     Result := ''
  158.   else
  159.     Result := Designer.GetPrivateDirectory;
  160. end;
  161.  
  162. procedure TDesignerDecorator.Hook(aForm: TForm);
  163. begin
  164.   if aForm <> Form then
  165.   begin
  166.     { always unhook first }
  167.     Unhook;
  168.     Form := aForm;
  169.     if Form <> nil then
  170.     begin
  171.       Designer := Form.Designer as TFormDesigner;
  172.       Form.Designer := Self;
  173.     end
  174.     else
  175.       Designer := nil;
  176.   end;
  177. end;
  178.  
  179. function TDesignerDecorator.IsDesignMsg(Sender: TControl; var Message: TMessage): Boolean;
  180. begin
  181.   if (Designer = nil) then
  182.     Result := False
  183.   else
  184.     Result := Designer.IsDesignMsg(Sender, Message);
  185. end;
  186.  
  187. function TDesignerDecorator.MethodExists(const AName: string): Boolean;
  188. begin
  189.   if (Designer = nil) then
  190.     Result := False
  191.   else
  192.     Result := Designer.MethodExists(AName);
  193. end;
  194.  
  195. procedure TDesignerDecorator.Modified;
  196. begin
  197.   if (Designer <> nil) then
  198.     Designer.Modified;
  199. end;
  200.  
  201. procedure TDesignerDecorator.Notification(AComponent: TComponent; Operation: TOperation);
  202. begin
  203.   if (Designer <> nil) then
  204.     Designer.Notification(AComponent, Operation);
  205.   if Assigned(FOnNotification) then
  206.     FOnNotification(Self, AComponent, Operation);
  207. end;
  208.  
  209. procedure TDesignerDecorator.PaintGrid;
  210. begin
  211.   if (Designer <> nil) then
  212.     Designer.PaintGrid;
  213. end;
  214.  
  215. procedure TDesignerDecorator.RenameMethod(const CurName, NewName: string);
  216. begin
  217.   if (Designer <> nil) then
  218.     Designer.RenameMethod(CurName, NewName);
  219. end;
  220.  
  221. procedure TDesignerDecorator.SetDesigner(Value: TFormDesigner);
  222. begin
  223.   if Value <> FDesigner then
  224.   begin
  225.     if OwnsDesigner then FDesigner.Free;
  226.     FDesigner := Value;
  227.   end;
  228. end;
  229.  
  230. procedure TDesignerDecorator.SetIsControl(Value: Boolean);
  231. begin
  232.   if Designer <> nil then Designer.IsControl := Value
  233. end;
  234.  
  235. procedure TDesignerDecorator.ShowMethod(const Name: string);
  236. begin
  237.   if (Designer <> nil) then
  238.     Designer.ShowMethod(Name);
  239. end;
  240.  
  241. procedure TDesignerDecorator.Unhook;
  242. var
  243.   D: TDesigner;
  244. begin
  245.   { The old form might not be alife anymore: so protect with try except }
  246.   try
  247.     D := nil;
  248.     try
  249.       { Is this decorator linked at all? Test: (Form = nil) <=> (not linked) }
  250.       if (Form <> nil) and (Form.Designer <> nil) then
  251.       begin
  252.         if Form.Designer = Self then { Hooked at first position (=Form) }
  253.           if Designer = nil then
  254.             Form.Designer := nil
  255.           else
  256.             Form.Designer := Designer as TFormDesigner
  257.         else
  258.         { Not at first position (form): iterate along designers }
  259.         begin
  260.           D := Form.Designer;
  261.           while Assigned(D) do
  262.           begin
  263.             if D is TDesignerDecorator then
  264.               if TDesignerDecorator(D).Designer = Self then
  265.               begin
  266.                 { Yes! it' us: perform actual unhooking }
  267.                 TDesignerDecorator(D).Designer := Designer;
  268.                 D := nil;
  269.               end
  270.               else { hook position not found yet, so iterate to the next designer }
  271.                 D := TDesignerDecorator(D).Designer
  272.             else { not a decorator designer: so don't know how to iterate further }
  273.               Break;
  274.           end;
  275.         end;
  276.       end;
  277.     finally
  278.       Form := nil;
  279.       Designer := nil;
  280.     end;
  281.     if D <> nil then UnhookError;
  282.   except
  283.     { ignore exceptions }
  284.   end;
  285. end;
  286.  
  287. procedure TDesignerDecorator.UnhookError;
  288. begin
  289.   if Assigned(FOnUnhookError) then FOnUnhookError(Self);
  290. end;
  291.  
  292. procedure TDesignerDecorator.ValidateRename(AComponent: TComponent; const CurName, 
  293.     NewName: string);
  294. begin
  295.   if (Designer <> nil) then
  296.     Designer.ValidateRename(AComponent, CurName, NewName);
  297.   if Assigned(FOnValidateRename) then
  298.     FOnValidateRename(Self, AComponent, CurName, NewName);
  299. end;
  300.  
  301.  
  302. initialization
  303. end.
  304.  
  305.