home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / AddOns / Components / Essentials / SETUP.EXE / %MAINDIR% / EsEdPop.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-11-28  |  11.4 KB  |  432 lines

  1. {*********************************************************}
  2. {*                   ESEDPOP.PAS 1.05                    *}
  3. {*      Copyright (c) 1997-98 TurboPower Software Co     *}
  4. {*                 All rights reserved.                  *}
  5. {*********************************************************}
  6.  
  7. {$I ES.INC}
  8.  
  9. {$B-} {Complete Boolean Evaluation}
  10. {$I+} {Input/Output-Checking}
  11. {$P+} {Open Parameters}
  12. {$T-} {Typed @ Operator}
  13. {$W-} {Windows Stack Frame}
  14. {$X+} {Extended Syntax}
  15.  
  16. {$IFDEF WIN32}                                                         {!!.02}
  17.   {$J+} {Writable constants}                                           {!!.02}
  18. {$ENDIF}                                                               {!!.02}
  19.  
  20. {$IFNDEF Win32}
  21.   {$G+} {286 Instructions}
  22.   {$N+} {Numeric Coprocessor}
  23.   {$C MOVEABLE,DEMANDLOAD,DISCARDABLE}
  24. {$ENDIF}
  25.  
  26. unit EsEdPop;
  27.   {-base popup edit field class}
  28.  
  29. interface
  30.  
  31. uses
  32.   {$IFDEF Win32} Windows, {$ELSE} WinTypes, WinProcs, {$ENDIF}
  33.   Buttons, Classes, Controls, ExtCtrls, Forms, Graphics, Menus, Messages,
  34.   StdCtrls, SysUtils,
  35.   EsBase, EsConst, EsData, EsLabel, EsUtil;
  36.  
  37. type
  38.   {.Z+}
  39.   TEsEdButton = class(TBitBtn)
  40.   public                                                               {!!.05}
  41.      procedure Click;
  42.        override;
  43.   end;
  44.   {.Z-}
  45.  
  46.   TEsEdPopup = class(TCustomEdit)
  47.   protected {private}
  48.     {.Z+}
  49.     {property variables}
  50.     FButton      : TEsEdButton;
  51.     FEsLabel     : TEsLabelInfo;
  52.     FPopupActive : Boolean;
  53.     FShowButton  : Boolean;
  54.  
  55.     {property methods}
  56.     function GetAttachedLabel : TEsAttachedLabel;
  57.     function GetVersion : string;
  58.     procedure SetShowButton(Value : Boolean);
  59.     procedure SetVersion(const Value : string);
  60.  
  61.     {internal methods}
  62.     function GetButtonWidth : Integer;
  63.     procedure LabelChange(Sender : TObject);
  64.     procedure LabelAttach(Sender : TObject; Value : Boolean);
  65.     procedure PositionLabel;
  66.  
  67.     procedure ESAssignLabel(var Msg : TMessage);
  68.       message ES_ASSIGNLABEL;
  69.     procedure ESPositionLabel(var Msg : TMessage);
  70.       message ES_POSITIONLABEL;
  71.     procedure ESRecordLabelPosition(var Msg : TMessage);
  72.       message ES_RECORDLABELPOSITION;
  73.     {.Z-}
  74.  
  75.   protected
  76.     {.Z+}
  77.     {descendants can set the value of this variable after calling inherited }
  78.     {create to set the default location and point-of-reference (POR) for the}
  79.     {attached label. if dlpTopLeft, the default location and POR will be at }
  80.     {the top left of the control. if dlpBottomLeft, the default location and}
  81.     {POR will be at the bottom left}
  82.     DefaultLabelPosition : TEsLabelPosition;
  83.  
  84.     procedure CreateParams(var Params : TCreateParams);
  85.       override;
  86.     procedure CreateWnd;
  87.       override;
  88.     function GetButtonEnabled : Boolean;                               {!!.03}
  89.       dynamic;
  90.     procedure Notification(AComponent : TComponent; Operation: TOperation);
  91.       override;
  92.     procedure PopupClose(Sender : TObject);
  93.       dynamic;
  94.     {.Z-}
  95.  
  96.     property EsLabelInfo : TEsLabelInfo
  97.       read FEsLabel
  98.       write FEsLabel;
  99.  
  100.     property ShowButton : Boolean
  101.       read FShowButton
  102.       write SetShowButton
  103.       default True;
  104.  
  105.     property Version : string
  106.       read GetVersion
  107.       write SetVersion
  108.       stored False;
  109.  
  110.   public
  111.     {.Z+}
  112.     constructor Create(AOwner : TComponent);
  113.       override;
  114.     destructor Destroy;
  115.       override;
  116.     procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer);        {!!.05}
  117.       override;
  118.     {.Z-}
  119.  
  120.     property AttachedLabel : TEsAttachedLabel
  121.       read GetAttachedLabel;
  122.  
  123.     {moved from protected to public}
  124.     procedure PopupOpen;                                               {!!.02}
  125.       dynamic;
  126.  
  127.     {.Z+}
  128.     property PopupActive : Boolean
  129.       read FPopupActive;
  130.     {.Z-}
  131.   end;
  132.  
  133.  
  134. implementation
  135.  
  136.  
  137. {$IFDEF TRIALRUN}
  138. uses
  139.   EsTrial;
  140. {$I ESTRIALF.INC}
  141. {$ENDIF}
  142.  
  143.  
  144. {*** TEsEditBtn ***}
  145.  
  146. procedure TEsEdButton.Click;
  147. begin
  148.   TEsEdPopup(Parent).PopupOpen;
  149. end;
  150.  
  151.  
  152. {*** TEsEdPopup ***}
  153.  
  154. constructor TEsEdPopup.Create(AOwner : TComponent);
  155. begin
  156.   inherited Create(AOwner);
  157.  
  158.   ControlStyle := ControlStyle - [csSetCaption];
  159.  
  160.   FShowButton := True;
  161.   FButton := TEsEdButton.Create(Self);
  162.   FButton.Visible := True;
  163.   FButton.Parent := Self;
  164.   FButton.Caption := '';
  165.   FButton.TabStop := False;
  166.   FButton.Style := bsNew;
  167.  
  168.   {set default position and reference point}
  169.   DefaultLabelPosition := dlpTopLeft;
  170.  
  171.   FEsLabel := TEsLabelInfo.Create;
  172.   FEsLabel.OnChange := LabelChange;
  173.   FEsLabel.OnAttach := LabelAttach;
  174. end;
  175.  
  176. procedure TEsEdPopup.CreateParams(var Params : TCreateParams);
  177. begin
  178.   inherited CreateParams(Params);
  179.  
  180.   Params.Style := Params.Style or WS_CLIPCHILDREN;
  181. end;
  182.  
  183. procedure TEsEdPopup.CreateWnd;
  184. {$IFDEF TRIALRUN}
  185. var
  186.   X : Integer;
  187. {$ENDIF}
  188. begin
  189.   inherited CreateWnd;
  190.  
  191.   {force button placement}
  192.   SetBounds(Left, Top, Width, Height);
  193.  
  194.   FButton.Enabled := GetButtonEnabled;                                 {!!.03}
  195.  
  196. {$IFDEF TRIALRUN}
  197.   X := _CC_;
  198.   if (X < ccRangeLow) or (X > ccRangeHigh) then Halt;
  199.   X := _VC_;
  200.   if (X < ccRangeLow) or (X > ccRangeHigh) then Halt;
  201. {$ENDIF}
  202. end;
  203.  
  204. destructor TEsEdPopup.Destroy;
  205. begin
  206.   {detatch and destroy label, if any}
  207.   FEsLabel.Visible := False;
  208.  
  209.   {destroy label info}
  210.   FEsLabel.Free;
  211.   FEsLabel := nil;
  212.  
  213.   FButton.Free;
  214.   FButton := nil;
  215.  
  216.   inherited Destroy;
  217. end;
  218.  
  219. procedure TEsEdPopup.ESAssignLabel(var Msg : TMessage);
  220. begin
  221.   FEsLabel.ALabel := TEsAttachedLabel(Msg.lParam);
  222. end;
  223.  
  224. procedure TEsEdPopup.ESPositionLabel(var Msg : TMessage);
  225. const
  226.   DX : Integer = 0;
  227.   DY : Integer = 0;
  228. begin
  229.   if FEsLabel.Visible and Assigned(FEsLabel.ALabel) and (FEsLabel.ALabel.Parent <> nil) and
  230.      not (csLoading in ComponentState) then begin
  231.     if DefaultLabelPosition = dlpTopLeft then begin
  232.       DX := FEsLabel.ALabel.Left - Left;
  233.       DY := FEsLabel.ALabel.Top + FEsLabel.ALabel.Height - Top;
  234.     end else begin
  235.       DX := FEsLabel.ALabel.Left - Left;
  236.       DY := FEsLabel.ALabel.Top - Top - Height;
  237.     end;
  238.     if (DX <> FEsLabel.OffsetX) or (DY <> FEsLabel.OffsetY) then
  239.       PositionLabel;
  240.   end;
  241. end;
  242.  
  243. procedure TEsEdPopup.ESRecordLabelPosition(var Msg : TMessage);
  244. begin
  245.   if Assigned(FEsLabel.ALabel) and (FEsLabel.ALabel.Parent <> nil) then begin
  246.     {if the label was cut and then pasted, this will complete the reattachment}
  247.     FEsLabel.FVisible := True;
  248.  
  249.     if DefaultLabelPosition = dlpTopLeft then
  250.       FEsLabel.SetOffsets(FEsLabel.ALabel.Left - Left,
  251.                           FEsLabel.ALabel.Top + FEsLabel.ALabel.Height - Top)
  252.     else
  253.       FEsLabel.SetOffsets(FEsLabel.ALabel.Left - Left,
  254.                           FEsLabel.ALabel.Top - Top - Height);
  255.   end;
  256. end;
  257.  
  258. function TEsEdPopup.GetAttachedLabel : TEsAttachedLabel;
  259. begin
  260.   if not FEsLabel.Visible then
  261.     raise EEssentialsError.Create(StrRes[SCEsLabelNotAttached]);
  262.  
  263.   Result := FEsLabel.ALabel;
  264. end;
  265.  
  266. {!!.03}
  267. function TEsEdPopup.GetButtonEnabled : Boolean;
  268. begin
  269.   Result := not ReadOnly;
  270. end;
  271.  
  272. function TEsEdPopup.GetButtonWidth : Integer;
  273. begin
  274.   if Assigned(FButton) and FShowButton then
  275.     Result := FButton.Width
  276.   else
  277.     Result := 0;
  278. end;
  279.  
  280. function TEsEdPopup.GetVersion : string;
  281. begin
  282.   Result := EsVersionStr;
  283. end;
  284.  
  285. procedure TEsEdPopup.LabelAttach(Sender : TObject; Value : Boolean);
  286. var
  287.   PF : TForm;
  288. begin
  289.   if csLoading in ComponentState then
  290.     Exit;
  291.  
  292.   PF := TForm(GetParentForm(Self));
  293.   if Value then begin
  294.     if Assigned(PF) then begin
  295.       FEsLabel.ALabel.Free;
  296.       FEsLabel.ALabel := TEsAttachedLabel.CreateEx(PF, Self);
  297.       FEsLabel.ALabel.Parent := Parent;
  298.       FEsLabel.ALabel.Caption := Name+'Label';
  299.       FEsLabel.SetOffsets(0, 0);
  300.       PositionLabel;
  301.       FEsLabel.ALabel.BringToFront;
  302.       {turn off auto size}
  303.       TLabel(FEsLabel.ALabel).AutoSize := False;
  304.     end;
  305.   end else begin
  306.     if Assigned(PF) then begin
  307.       FEsLabel.ALabel.Free;
  308.       FEsLabel.ALabel := nil;
  309.     end;
  310.   end;
  311. end;
  312.  
  313. procedure TEsEdPopup.LabelChange(Sender : TObject);
  314. begin
  315.   if not (csLoading in ComponentState) then
  316.     PositionLabel;
  317. end;
  318.  
  319. procedure TEsEdPopup.Notification(AComponent : TComponent; Operation: TOperation);
  320. var
  321.   PF : TForm;
  322. begin
  323.   inherited Notification(AComponent, Operation);
  324.  
  325.   if Operation = opRemove then
  326.     if Assigned(FEsLabel) and (AComponent = FEsLabel.ALabel) then begin
  327.       PF := TForm(GetParentForm(Self));
  328.       if Assigned(PF) and not (csDestroying in PF.ComponentState) then begin
  329.         FEsLabel.FVisible := False;
  330.         FEsLabel.ALabel := nil;
  331.       end
  332.     end;
  333. end;
  334.  
  335. procedure TEsEdPopup.PopupClose;
  336. begin
  337.   FPopupActive := False;
  338. end;
  339.  
  340. procedure TEsEdPopup.PopupOpen;
  341. begin
  342.   FPopupActive := True;
  343. end;
  344.  
  345. procedure TEsEdPopup.PositionLabel;
  346. begin
  347.   if FEsLabel.Visible and Assigned(FEsLabel.ALabel) and (FEsLabel.ALabel.Parent <> nil) and
  348.      not (csLoading in ComponentState) then begin
  349.  
  350.     if DefaultLabelPosition = dlpTopLeft then begin
  351.       FEsLabel.ALabel.SetBounds(Left + FEsLabel.OffsetX,
  352.                          FEsLabel.OffsetY - FEsLabel.ALabel.Height + Top,
  353.                          FEsLabel.ALabel.Width, FEsLabel.ALabel.Height);
  354.     end else begin
  355.       FEsLabel.ALabel.SetBounds(Left + FEsLabel.OffsetX,
  356.                          FEsLabel.OffsetY + Top + Height,
  357.                          FEsLabel.ALabel.Width, FEsLabel.ALabel.Height);
  358.     end;
  359.   end;
  360. end;
  361.  
  362. procedure TEsEdPopup.SetBounds(ALeft, ATop, AWidth, AHeight : Integer);
  363. var
  364.   H : Integer;
  365. begin
  366.   inherited SetBounds(ALeft, ATop, AWidth, AHeight);
  367.  
  368.   if not HandleAllocated then
  369.     Exit;
  370.  
  371.   if HandleAllocated then
  372.     PostMessage(Handle, ES_POSITIONLABEL, 0, 0);
  373.  
  374.   if not FShowButton then begin
  375.     FButton.Height := 0;
  376.     FButton.Width := 0;
  377.     Exit;
  378.   end;
  379.  
  380.   H := ClientHeight;
  381.   {$IFDEF Win32}
  382.   if BorderStyle = bsNone then begin
  383.     FButton.Height := H;
  384.     FButton.Width := (FButton.Height div 4) * 3;
  385.     if Assigned(Fbutton.Glyph) then
  386.       if FButton.Width < FButton.Glyph.Width+4 then
  387.         FButton.Width := FButton.Glyph.Width+4;
  388.     FButton.Left := Width - FButton.Width;
  389.     FButton.Top := 0;
  390.   end else if Ctl3D then begin
  391.     FButton.Height := H;
  392.     FButton.Width := (FButton.Height div 4) * 3;
  393.     if Assigned(FButton.Glyph) then
  394.       if FButton.Width < FButton.Glyph.Width+4 then
  395.         FButton.Width := FButton.Glyph.Width+4;
  396.     FButton.Left := Width - FButton.Width - 4;
  397.     FButton.Top := 0;
  398.   end else begin
  399.     FButton.Height := H - 2;
  400.     FButton.Width := (FButton.Height div 4) * 3;
  401.     if Assigned(Fbutton.Glyph) then
  402.       if FButton.Width < FButton.Glyph.Width+4 then
  403.         FButton.Width := FButton.Glyph.Width+4;
  404.     FButton.Left := Width - FButton.Width - 1;
  405.     FButton.Top := 1;
  406.   end;
  407.   {$ELSE} {block revised}                                              {!!.02}
  408.   FButton.Height := H;
  409.   FButton.Width := (FButton.Height div 4) * 3;
  410.   if (FButton.Glyph <> nil) then
  411.     if FButton.Width < FButton.Glyph.Width+6 then
  412.       FButton.Width := FButton.Glyph.Width+6;
  413.   FButton.Left := Width - FButton.Width;
  414.   FButton.Top := 0;
  415.   {$ENDIF}
  416. end;
  417.  
  418. procedure TEsEdPopup.SetShowButton(Value : Boolean);
  419. begin
  420.   if Value <> FShowButton then begin
  421.     FShowButton := Value;
  422.     {force resize and redisplay of button}
  423.     SetBounds(Left, Top, Width, Height);
  424.   end;
  425. end;
  426.  
  427. procedure TEsEdPopup.SetVersion(const Value : string);
  428. begin
  429. end;
  430.  
  431. end.
  432.