home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 December / Chip_2001-12_cd1.bin / zkuste / delphi / kolekce / d345 / JWTOOL.ZIP / jwtool / JwPopBtn.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-09-29  |  12.9 KB  |  481 lines

  1. unit JwPopbtn;
  2.  
  3. {
  4.         **   VERSION History   **
  5.    Version     Date     Notes
  6.     v1.00  - 01APR99    Original Release
  7. }
  8.  
  9. {
  10.      Three stage buttons:
  11.            The idea is that you want one "look" in an "unfocused" state, another
  12.      when you are in a "mouseover" state, and a third when you are clicking.  This
  13.      idea is taken in the FlashClick, FlashPanel, and PopButton.
  14.  
  15.           The JwPopButton was the first of the three stage buttons. I can't say
  16.      how many times I have seen installer programs that had a really neat button
  17.      on the surface that acted differently if you even hovered over it.   This
  18.      of course uses three different bitmaps to create the effect.
  19.  
  20. }
  21.  
  22. //  Created By:
  23. //    Joseph Wilcock
  24. //    Coockoo@hotmail.com
  25. //    http://msnhomepages.talkcity.com/RedmondAve/coockoo/
  26.  
  27. interface
  28.  
  29. uses {$IFDEF WIN32} Windows, {$ELSE} WinProcs, WinTypes, {$ENDIF}
  30.      Messages, SysUtils, Classes, Controls, Forms, Graphics, Extctrls;
  31.  
  32. type
  33.   TActiveModeState = ( amNone, amDown, amUp, amJump );
  34.  
  35.   TJwPopButton = class(TCustomPanel)
  36.     private
  37.       { Private fields of TJwPopButton }
  38.  
  39.         {Bitmap Info}
  40.         FActive : TBitmap;
  41.         FJump : TBitmap;
  42.         FDeactive : TBitmap;
  43.         FBitOffsetX : Integer; {Offset of the Bitmap from top left corner, in logical points}
  44.         FBitOffsetY : Integer;
  45.  
  46.         {General Attributes}
  47.         FDrawBitmap: Boolean;
  48.         FWordWrap: Boolean;
  49.  
  50.         {button colors, if normal drawing}
  51.         FButtonBevelWidth: Integer;
  52.         FButtonFace: TColor;
  53.         FHighLight: TColor;
  54.         FButtonShadow: TColor;
  55.         FWindowFrame: TColor;
  56.  
  57.         FTotalFrameColor: TColor;
  58.         FActMode: TActiveModeState;
  59.  
  60.         procedure SetFActive(Value : TBitmap);
  61.         procedure SetFDeactive(Value : TBitmap);
  62.         procedure SetFJump( Value : TBitmap );
  63.         Procedure SetBevelWidth( Value: Integer );
  64.         procedure SetButtonFace( Value: TColor );
  65.         procedure SetHightLight( Value: TColor );
  66.         procedure SetButtonShadow( Value: TColor );
  67.         procedure SetWindowFrame( Value: TColor );
  68.         Procedure SetDrawBitmap( Value: Boolean );
  69.         procedure SetWordWrap( Value: Boolean );
  70.         procedure SetTotalFrameColor( Value: TColor );
  71.     protected
  72.       { Protected fields of TJwPopButton }
  73.  
  74.       { Protected methods of TJwPopButton }
  75.         procedure Click; override;
  76.         procedure DoEnter; override;
  77.         procedure DoExit; override;
  78.         procedure KeyPress(var Key : Char); override;
  79.         procedure Loaded; override;
  80.         procedure Paint; override;
  81.  
  82.         function DrawButtonFace( const Client: TRect; BevelWidth: Integer ): TRect;
  83.         procedure DoDrawText( var Rect: TRect; Flags: Word );
  84.         procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;
  85.         procedure WMLButtonUp(var Message: TWMLButtonUp); message WM_LBUTTONUP;
  86.         procedure WMSize(var Message: TWMSize); message WM_SIZE;
  87.         procedure CMMouseEnter(var msg: TMessage); message CM_MOUSEENTER;
  88.         procedure CMMouseLeave(var msg: TMessage); message CM_MOUSELEAVE;
  89.     public
  90.       { Public fields and properties of TJwPopButton }
  91.  
  92.       { Public methods of TJwPopButton }
  93.         constructor Create(AOwner: TComponent); override;
  94.         destructor Destroy; override;
  95.  
  96.     published
  97.       { Published properties of TJwPopButton }
  98.         property OnClick;
  99.         property OnDblClick;
  100.         property OnDragDrop;
  101.         property OnEnter;
  102.         property OnExit;
  103.         property OnKeyDown;
  104.         property OnKeyPress;
  105.         property OnKeyUp;
  106.         property OnMouseDown;
  107.         property OnMouseMove;
  108.         property OnMouseUp;
  109.         property OnResize;
  110.         { Picture for active occurances... }
  111.         property Active : TBitmap read FActive write SetFActive;
  112.         property Jump : TBitmap read FJump write SetFJump;
  113.         property Deactive : TBitmap read FDeactive write SetFDeactive;
  114.         property BitOffsetX : Integer read FBitOffsetX write FBitOffsetX default 0;
  115.         property BitOffsetY : Integer read FBitOffsetY write FBitOffsetY default 0;
  116.         Property Anchors;
  117.         property Caption;
  118.         property Font;
  119.         property Align;
  120.         property Enabled;
  121.         property BorderWidth;
  122.         property BorderStyle;
  123.         property Locked;
  124.         property Alignment;
  125.         property DrawBitmap: Boolean Read FDrawBitmap Write SetDrawBitmap
  126.                  default True;
  127.         property WordWrap: Boolean Read FWordWrap Write SetWordWrap
  128.                  default False;
  129.  
  130.         property ButtonBevelWidth: Integer Read FButtonBevelWidth Write SetBevelWidth
  131.                  default 2;
  132.         property ButtonFace: TColor Read FButtonFace Write SetButtonFace
  133.                  default clbtnFace;
  134.         property HighLight: TColor Read FHighLight Write SetHightLight
  135.                  default clBtnHighlight;
  136.         property ButtonShadow: TColor Read FButtonShadow Write SetButtonShadow
  137.                  default clBtnShadow;
  138.         property WindowFrame: TColor Read FWindowFrame Write SetWindowFrame
  139.                  default clWindowFrame;
  140.         property TotalFrameColor: TColor Read FTotalFrameColor Write SetTotalFrameColor
  141.                  default clBtnFace;
  142.   end;
  143.  
  144. procedure Register;
  145.  
  146. implementation
  147.  
  148. procedure Register;
  149. begin
  150.   { Register TJwPopButton with Standard as its
  151.     default page on the Delphi component palette }
  152.   RegisterComponents('JwTools', [TJwPopButton]);
  153. end;
  154.  
  155. procedure TJwPopButton.SetWordWrap( Value: Boolean );
  156. begin
  157.   if Value <> FWordWrap then
  158.     begin
  159.       FWordWrap := Value;
  160.       InValidate;
  161.     end;
  162. end;
  163.  
  164. procedure TJwPopButton.SetButtonFace( Value: TColor );
  165. begin
  166.   if Value <> FButtonFace then
  167.   begin
  168.     FButtonFace := Value;
  169.     Invalidate;
  170.   end;
  171. end;
  172.  
  173. procedure TJwPopButton.SetHightLight( Value: TColor );
  174. begin
  175.   if Value <> FHighLight then
  176.   begin
  177.     FHighLight := Value;
  178.     Invalidate;
  179.   end;
  180. end;
  181.  
  182. procedure TJwPopButton.SetTotalFrameColor( Value: TColor );
  183. begin
  184.   if Value <> FTotalFrameColor then
  185.   begin
  186.     FTotalFrameColor := Value;
  187.     Invalidate;
  188.   end;
  189. end;
  190.  
  191. procedure TJwPopButton.SetButtonShadow( Value: TColor );
  192. begin
  193.   if Value <> FButtonShadow then
  194.   begin
  195.     FButtonShadow := Value;
  196.     Invalidate;
  197.   end;
  198. end;
  199.  
  200. procedure TJwPopButton.SetWindowFrame( Value: TColor );
  201. begin
  202.   if Value <> FWindowFrame then
  203.   begin
  204.     FWindowFrame := Value;
  205.     Invalidate;
  206.   end;
  207. end;
  208.  
  209. Procedure TJwPopButton.SetDrawBitmap( Value: Boolean );
  210. begin
  211.   if Value <> FDrawBitmap then
  212.     begin
  213.       FDrawBitmap := Value;
  214.       InValidate;
  215.     end;
  216. end;
  217.  
  218. Procedure TJwPopButton.SetBevelWidth( Value: Integer );
  219. begin
  220.   if Value <> FButtonBevelWidth then
  221.     begin
  222.       FButtonBevelWidth := Value;
  223.       InValidate;
  224.     end;
  225. end;
  226.  
  227. { Write method for property FActive }
  228. procedure TJwPopButton.SetFActive( Value : TBitmap );
  229. begin
  230.   { Use Assign method because TBitmap is an object type }
  231.   FActive.Assign(Value);
  232.   if FActMode = amUp then
  233.     Invalidate;
  234. end;
  235.  
  236. procedure TJwPopButton.SetFJump( Value : TBitmap );
  237. begin
  238.   { Use Assign method because TBitmap is an object type }
  239.   FJump.Assign(Value);
  240.   if FActMode = amJump then
  241.     Invalidate;
  242. end;
  243.  
  244. { Write method for property FDeactive }
  245. procedure TJwPopButton.SetFDeactive( Value : TBitmap );
  246. begin
  247.   { Use Assign method because TBitmap is an object type }
  248.   FDeactive.Assign( Value );
  249.   if FActMode = amDown then
  250.     Invalidate;
  251. end;
  252.  
  253. { Override OnClick handler from TCustomPanel }
  254. procedure TJwPopButton.Click;
  255. {var
  256.   OldMode: Byte;}
  257. begin
  258.   { Activate click behavior of parent }
  259. {  OldMode := FActMode;
  260.   try
  261.     FActMode := 2;
  262.     Invalidate;}
  263.     inherited Click;
  264.   {finally
  265.     FActMode := OldMode;
  266.     Invalidate;
  267.   end;}
  268. end;
  269.  
  270. { Override OnEnter handler from TCustomPanel }
  271. procedure TJwPopButton.DoEnter;
  272. begin
  273.   inherited DoEnter;
  274.   FActMode := amUP;
  275.   InValidate;
  276. end;
  277.  
  278. { Override OnExit handler from TCustomPanel }
  279. procedure TJwPopButton.DoExit;
  280. begin
  281.   inherited DoExit;
  282.   FActMode := amDown;
  283.   InValidate;
  284. end;
  285.  
  286. { Override OnKeyPress handler from TCustomPanel }
  287. procedure TJwPopButton.KeyPress(var Key : Char);
  288. const
  289.   TabKey = Char( VK_TAB );
  290.   EnterKey = Char( VK_RETURN );
  291.   SpaceKey = Char( VK_SPACE );
  292. begin
  293.   { Key contains the character produced by the keypress.
  294.     It can be tested or assigned a new value before the
  295.     call to the inherited KeyPress method.  Setting Key
  296.     to #0 before call to the inherited KeyPress method
  297.     terminates any further processing of the character. }
  298.  
  299.   { Activate KeyPress behavior of parent }
  300.   if Key = EnterKey then
  301.     Self.Click;
  302.   if Key = SpaceKey then
  303.     Self.Click;
  304.   inherited KeyPress(Key);
  305.  
  306.   { Code to execute after KeyPress behavior of parent }
  307.  
  308. end;
  309.  
  310. constructor TJwPopButton.Create(AOwner: TComponent);
  311. begin
  312.   { Call the Create method of the container's parent class       }
  313.   inherited Create(AOwner);
  314.  
  315.   FActive := TBitmap.Create;
  316.   FDeactive := TBitmap.Create;
  317.   FJump := TBitMap.Create;
  318.   FActMode := amDown;
  319.   FButtonFace := clBtnFace;
  320.   FHighLight := clBtnHighLight;
  321.   FButtonShadow := clBtnShadow;
  322.   FWindowFrame := clBtnFace;
  323.   FTotalFrameColor := clBtnFace;
  324.   FDrawBitmap := True;
  325.   FWordWrap := False;
  326.   BorderWidth := 0;
  327.  
  328.         {button colors, if normal drawing}
  329.   FButtonBevelWidth := 5;
  330.   Width := 50;
  331.   Height := 20;
  332.  
  333. end;
  334.  
  335. destructor TJwPopButton.Destroy;
  336. begin
  337.   FActive.Free;
  338.   FDeactive.Free;
  339.   FJump.Free;
  340.   inherited Destroy;
  341. end;
  342.  
  343. procedure TJwPopButton.Loaded;
  344. begin
  345.   inherited Loaded;
  346.  
  347.   { Perform any component setup that depends on the property
  348.     values having been set }
  349.  
  350. end;
  351.  
  352. procedure TJwPopButton.Paint;
  353. const
  354.   Alignments: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
  355. var
  356.   PaintRect: TRect;
  357.   TextBounds: TRect;
  358.   OldStyle: TPenStyle;
  359. begin
  360.   Canvas.Font := Self.Font;
  361.   Canvas.Brush.Color := Self.ButtonFace;
  362.   OldStyle := Canvas.Pen.Style;
  363.   try
  364.     Canvas.Pen.Style := psClear;
  365.     Canvas.Rectangle( 0, 0, Width, Height );
  366.   finally
  367.     Canvas.Pen.Style := OldStyle;
  368.   end;
  369.  
  370.   if FDrawBitmap then
  371.     begin
  372.       PaintRect := Rect( FBitOffsetX, FBitOffsetY, Width, Height );
  373.       case FActMode of
  374.         amDown: Canvas.Draw( FBitOffsetX, FBitOffsetY, FDeactive );
  375.         amUp: Canvas.Draw( FBitOffsetX, FBitOffsetY, FActive );
  376.         amJump: Canvas.Draw( FBitOffsetX, FBitOffsetY, FJump );
  377.       end;
  378.     end
  379.   else
  380.     begin
  381.       PaintRect := DrawButtonFace( Rect(0, 0, Width, Height), FButtonBevelWidth );
  382.       PaintRect.Top := PaintRect.Top + FButtonBevelWidth;
  383.       PaintRect.Left := PaintRect.Left + FButtonBevelWidth;
  384.       PaintRect.Bottom := PaintRect.Bottom - FButtonBevelWidth;
  385.       PaintRect.Right := PaintRect.Right - FButtonBevelWidth;
  386.     end;
  387.  
  388.   DoDrawText(PaintRect, (DT_EXPANDTABS or DT_WORDBREAK) or
  389.       Alignments[Self.Alignment]);
  390. end;
  391.  
  392. function TJwPopButton.DrawButtonFace( const Client: TRect; BevelWidth: Integer ): TRect;
  393. var
  394.   R: TRect;
  395. begin
  396.   R := Client;
  397.   with Canvas do
  398.   begin
  399.       Brush.Style := bsSolid;
  400.       FillRect(R);
  401.  
  402.       if FActMode = amJump then
  403.         begin
  404.           Frame3D(Canvas, R, FButtonShadow, FHighLight, 1);
  405.           Frame3D(Canvas, R, FWindowFrame, FButtonShadow, 1);
  406.         end
  407.       else if FActMode = amUP then
  408.         begin
  409.           Frame3D(Canvas, R, FButtonShadow, FWindowFrame, 1);
  410.           Frame3D(Canvas, R, FHighLight, FButtonShadow, 1);
  411.         end;
  412.   end;
  413.  
  414.   Result := Client;
  415.   InflateRect(Result, -BevelWidth, -BevelWidth);
  416.   if FActMode <> amUp then OffsetRect(Result, 2, 2);
  417. end;
  418.  
  419. procedure TJwPopButton.DoDrawText( var Rect: TRect; Flags: Word );
  420. var
  421.   Text: array[0..255] of Char;
  422. begin
  423.   GetTextBuf( Text, SizeOf(Text) );
  424.  
  425.   if FWordWrap then
  426.     Flags := Flags or dt_WordBreak;
  427.  
  428.   if (Flags and DT_CALCRECT <> 0) and ((Text[0] = #0) and
  429.     (Text[0] = '&') and (Text[1] = #0)) then StrCopy(Text, ' ');
  430.  
  431.   Canvas.Font := Font;
  432.   if not Enabled then Canvas.Font.Color := clGrayText;
  433.   DrawText(Canvas.Handle, Text, StrLen(Text), Rect, Flags);
  434. end;
  435.  
  436. procedure TJwPopButton.CMMouseEnter(var msg: TMessage);
  437. begin
  438.   FActMode := amUp;
  439.   Invalidate;
  440.   Inherited;
  441. end;
  442.  
  443. procedure TJwPopButton.CMMouseLeave(var msg: TMessage);
  444. begin
  445.   FActMode := amDown;
  446.   Invalidate;
  447.   Inherited;
  448. end;
  449.  
  450. procedure TJwPopButton.WMLButtonDown(var Message: TWMLButtonDown);
  451. begin
  452.   FActMode := amJump;
  453.   Invalidate;
  454.   Inherited;
  455. end;
  456.  
  457. procedure TJwPopButton.WMLButtonUp(var Message: TWMLButtonUp);
  458. begin
  459.   FActMode := amUp;
  460.   Invalidate;
  461.   Inherited;
  462. end;
  463.  
  464. procedure TJwPopButton.WMSize(var Message: TWMSize);
  465. var
  466.      W, H: Integer;
  467. begin
  468.      inherited;
  469.  
  470.      W := Width;
  471.      H := Height;
  472.  
  473.      if (W <> Width) or (H <> Height) then
  474.         inherited SetBounds(Left, Top, W, H);
  475.  
  476.      Message.Result := 0;
  477. end;
  478.  
  479. end.
  480.  
  481.