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

  1. unit JwFshCl;
  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 FlashClick shares a lot of code with the flash panel.  The difference is
  16.      that the FlashClick is inherited from a TLabel, and therefore does not have a
  17.      canvas of it's own, which can save on memory.  Since there isn't a default MouseExit
  18.      and MouseEnter events (I got use to those programming in XWindows...) I had to make
  19.      my own.  But in essence, you now have a lowmemory Web-like button.
  20.  
  21. }
  22.  
  23. //  Created By:
  24. //    Joseph Wilcock
  25. //    Coockoo@hotmail.com
  26. //    http://msnhomepages.talkcity.com/RedmondAve/coockoo/
  27.  
  28.  
  29. interface
  30.  
  31. uses {$IFDEF WIN32} Windows, {$ELSE} WinProcs, WinTypes, {$ENDIF} Messages,
  32.      SysUtils, Classes, Controls, Forms, Graphics, Stdctrls;
  33.  
  34. type
  35. {Windows Messaging is rather nasty.  Since I want to add user-defined methods to
  36. something that delphi hasn't already defined, namely OnMouseExit and OnMouseEnter,
  37. I will have to do it myself.  First, I need to redefine the message record.  In
  38. this case I literally took it from the TWMMouse message, repeated twice under two
  39. names.  (I Might want to change that later).}
  40.  
  41.   TWMMouseExit = record
  42.     Msg: Cardinal;
  43.     Keys: Longint;
  44.     case Integer of
  45.       0: (
  46.         XPos: Smallint;
  47.         YPos: Smallint);
  48.       1: (
  49.         Pos: TSmallPoint;
  50.         Result: Longint);
  51.   end;
  52.  
  53.   TWMMouseEnter = record
  54.     Msg: Cardinal;
  55.     Keys: Longint;
  56.     case Integer of
  57.       0: (
  58.         XPos: Smallint;
  59.         YPos: Smallint);
  60.       1: (
  61.         Pos: TSmallPoint;
  62.         Result: Longint);
  63.   end;
  64.  
  65.  
  66.   {The next thing to do is define a new custom "TNotify" event.  Once again,
  67.   even though I am repeating myself, I'm going to use the same thing twice
  68.   just in case I want to change things.}
  69.   TMouseExit = procedure(Sender: TObject; Button: TMouseButton;
  70.     Shift: TShiftState; X, Y: Integer) of object;
  71.   TMouseEnter = procedure(Sender: TObject; Button: TMouseButton;
  72.     Shift: TShiftState; X, Y: Integer) of object;
  73.  
  74.   TJwFlashClick = class( TLabel )
  75.     private
  76.       { Private fields of TJwFlashClick }
  77.  
  78.         {The TNotifyEvent objects to use on the specific events}
  79.         FOnEnter: TMouseEnter;
  80.         FOnExit: TMouseExit;
  81.  
  82.       { Private methods of TJwFlashClick }
  83.         procedure WMSize(var Message: TWMSize); message WM_SIZE;
  84.         procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;
  85.         procedure WMLButtonUp(var Message: TWMLButtonUp); message WM_LBUTTONUP;
  86.  
  87.         {FINALLY.. we have a 3-stage event.  The CMMouseEnter and CMMouseLeave are the functions
  88.         that actually look for the CM_MOUSEENTER and CM_MOUSELEAVE in the WinMain command loop.
  89.         Once it hits, it will call the corresponding "Do" method which will call the "Mouse..."
  90.         and that will call the TNotfy event if it is assigned.}
  91.         procedure CMMouseEnter({var msg: TMessage}var Message: TWMMouseEnter); message CM_MOUSEENTER;
  92.         procedure CMMouseLeave({var msg: TMessage}var Message: TWMMouseExit); message CM_MOUSELEAVE;
  93.         procedure MouseEnter(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  94.         procedure DoMouseEnter(var Message: TWMMouseEnter; Button: TMouseButton;
  95.                     Shift: TShiftState);
  96.         procedure DoMouseExit(var Message: TWMMouseExit; Button: TMouseButton;
  97.                     Shift: TShiftState);
  98.         procedure MouseExit(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  99.  
  100.         Procedure SetFlareFont( Value: TFont );
  101.         Procedure SetBaseFont( Value: TFont );
  102.         Procedure SetClickFont( Value: TFont );
  103.         Procedure SetCommonFont( Value: TFont );
  104.         Procedure SetCheckedValue( Value: Boolean );
  105.         Function GetCheckValue: Boolean;
  106.     protected
  107.       { Protected fields of TJwFlashClick }
  108.         FFlareMode: Byte;
  109.         FBaseFont: TFont;
  110.         FFlareFont: TFont;
  111.         FClickFont: TFont;
  112.  
  113.         FCheckEffect: Boolean;
  114.       { Protected methods of TJwFlashClick }
  115.         procedure Click; override;
  116.         procedure Loaded; override;
  117.         procedure Paint; override;
  118.  
  119.     public
  120.       { Public fields and properties of TJwFlashClick }
  121.  
  122.       { Public methods of TJwFlashClick }
  123.         constructor Create(AOwner: TComponent); override;
  124.         destructor Destroy; override;
  125.  
  126.     published
  127.       { Published properties of TJwFlashClick }
  128.         Property Anchors;
  129.         Property BaseFont: TFont Read FBaseFont Write SetBaseFont;
  130.         Property FlareFont: TFont Read FFlareFont Write SetFlareFont;
  131.         Property ClickFont: TFont Read FClickFont Write SetClickFont;
  132.         Property CommonFont: TFont Read FBaseFont Write SetCommonFont;
  133.  
  134.         Property CheckEffect: Boolean Read FCheckEffect Write FCheckEffect;
  135.  
  136.         Property OnEnter: TMouseEnter Read FOnEnter Write FOnEnter;
  137.         Property OnExit: TMouseExit Read FOnExit Write FOnExit;
  138.  
  139.         property OnClick;
  140.         property OnDblClick;
  141.         property OnDragDrop;
  142.         property OnMouseDown;
  143.         property OnMouseMove;
  144.         property OnMouseUp;
  145.   end;
  146.  
  147. procedure Register;
  148.  
  149. implementation
  150.  
  151. procedure Register;
  152. begin
  153.      { Register TJwFlashClick with Standard as its
  154.        default page on the Delphi component palette }
  155.      RegisterComponents('JwTools', [TJwFlashClick]);
  156. end;
  157.  
  158. Procedure TJwFlashClick.SetCheckedValue( Value: Boolean );
  159. begin
  160.   if ( FCheckEffect ) then
  161.     begin
  162.       if Value then
  163.         FFlareMode := 3
  164.       else
  165.         FFlareMode := 0;
  166.       InValidate;
  167.     end;
  168. end;
  169.  
  170. Function TJwFlashClick.GetCheckValue: Boolean;
  171. begin
  172.   Result := ( FFlareMode = 3 );
  173. end;
  174.  
  175. Procedure TJwFlashClick.SetCommonFont( Value: TFont );
  176. begin
  177.   {if Value <> FClickFont then
  178.     begin}
  179.       FClickFont.Assign( Value );
  180.       FBaseFont.Assign( Value );
  181.       FFlareFont.Assign( Value );
  182.       Invalidate;
  183.     {end;}
  184. end;
  185.  
  186. Procedure TJwFlashClick.SetClickFont( Value: TFont );
  187. begin
  188.   {if Value <> FClickFont then
  189.     begin}
  190.       FClickFont.Assign( Value );
  191.       Invalidate;
  192.     {end;}
  193. end;
  194.  
  195. Procedure TJwFlashClick.SetBaseFont( Value: TFont );
  196. begin
  197.   {if Value <> FBaseFont then
  198.     begin}
  199.       FBaseFont.Assign( Value );
  200.       Invalidate;
  201.     {end;}
  202. end;
  203.  
  204. Procedure TJwFlashClick.SetFlareFont( Value: TFont );
  205. begin
  206.   {if Value <> FFlareFont then
  207.     begin}
  208.       FFlareFont.Assign( Value );
  209.       Invalidate;
  210.     {end;}
  211. end;
  212.  
  213.  
  214. { Override OnClick handler from TLabel }
  215. procedure TJwFlashClick.Click;
  216. begin
  217.      { Code to execute before activating click
  218.        behavior of component's parent class }
  219.  
  220.      { Activate click behavior of parent }
  221.      inherited Click;
  222.  
  223.      { Code to execute after click behavior
  224.        of parent }
  225.  
  226. end;
  227.  
  228.  
  229. constructor TJwFlashClick.Create(AOwner: TComponent);
  230. begin
  231.   { Call the Create method of the parent class }
  232.   inherited Create(AOwner);
  233.  
  234.   FBaseFont := TFont.Create;
  235.   FFlareFont := TFont.Create;
  236.   FClickFont := TFont.Create;
  237.  
  238.   FFlareMode := 0;
  239.   FBaseFont.Assign( Self.Font );
  240.   FBaseFont.Color := clWhite;
  241.   FFlareFont.Assign( Self.Font );
  242.   FFlareFont.Size := FFlareFont.Size + 2;
  243.   FFlareFont.Color := clYellow;
  244.   FClickFont.Assign( Self.Font );
  245.   FClickFont.Size := ClickFont.Size + 2;
  246.   FClickFont.Color := clRed;
  247.  
  248.   FCheckEffect := False;
  249. end;
  250.  
  251. destructor TJwFlashClick.Destroy;
  252. begin
  253.   FBaseFont.Free;
  254.   FFlareFont.Free;
  255.   FClickFont.Free;
  256.   inherited Destroy;
  257. end;
  258.  
  259. procedure TJwFlashClick.Loaded;
  260. begin
  261.      inherited Loaded;
  262.  
  263.      { Perform any component setup that depends on the property
  264.        values having been set }
  265.  
  266. end;
  267.  
  268. procedure TJwFlashClick.Paint;
  269. begin
  270.      { Make this component look like its parent component by calling
  271.        its parent's Paint method. }
  272.      case FFlareMode of
  273.        0: begin
  274.             Self.Font := FBaseFont;
  275.           end;
  276.        1: begin
  277.             Self.Font := FFlareFont;
  278.           end;
  279.      2,3: begin
  280.             Self.Font := FClickFont;
  281.           end;
  282.      else
  283.        Self.Font := FBaseFont;
  284.      end;
  285.  
  286.      inherited Paint;
  287. end;
  288.  
  289. procedure TJwFlashClick.WMSize(var Message: TWMSize);
  290. var
  291.      W, H: Integer;
  292. begin
  293.      inherited;
  294.  
  295.      W := Width;
  296.      H := Height;
  297.  
  298.      if (W <> Width) or (H <> Height) then
  299.         inherited SetBounds(Left, Top, W, H);
  300.  
  301.      Message.Result := 0;
  302. end;
  303.  
  304. Procedure MsgToKey( Keys: Word; var MB: TMouseButton );
  305. begin
  306.   if Keys and MK_LBUTTON = MK_LBUTTON then
  307.     MB := mbLeft
  308.   else if Keys and MK_MBUTTON = MK_MBUTTON then
  309.     MB := mbMiddle
  310.   else if Keys and MK_RBUTTON = MK_RBUTTON then
  311.     MB := mbRight;
  312. end;
  313.  
  314. procedure TJwFlashClick.CMMouseEnter(var Message: TWMMouseEnter);
  315. var
  316.   MB: TMouseButton;
  317.   SS: TShiftState;
  318. begin
  319.   if ( FFlareMode <> 3 ) then
  320.     begin
  321.       FFlareMode := 1;
  322.       Invalidate;
  323.     end;
  324.   Inherited;
  325.   // This information is processes twice.
  326.   SS := Forms.KeysToShiftState( Message.Keys );
  327.   MsgToKey( Message.Keys, MB );
  328.   DoMouseEnter(Message, MB, SS);
  329. end;
  330.  
  331. procedure TJwFlashClick.CMMouseLeave(var Message: TWMMouseExit);
  332. var
  333.   MB: TMouseButton;
  334.   SS: TShiftState;
  335. begin
  336.   if ( FFlareMode <> 3 ) then
  337.     begin
  338.       FFlareMode := 0;
  339.       Invalidate;
  340.     end;
  341.   Inherited;
  342.  
  343.   // This information is processes twice.
  344.   SS := Forms.KeysToShiftState( Message.Keys );
  345.   MsgToKey( Message.Keys, MB );
  346.   DoMouseExit(Message, MB, SS);
  347. end;
  348.  
  349. procedure TJwFlashClick.WMLButtonDown(var Message: TWMLButtonDown);
  350. begin
  351.   if ( FCheckEffect ) and ( FFlareMode <> 3 ) then
  352.     FFlareMode := 3
  353.   else
  354.     FFlareMode := 2;
  355.   Invalidate;
  356.   Inherited;
  357. end;
  358.  
  359. procedure TJwFlashClick.WMLButtonUp(var Message: TWMLButtonUp);
  360. begin
  361.   if ( FFlareMode <> 3 ) then
  362.     begin
  363.       FFlareMode := 1;
  364.       Invalidate;
  365.     end;
  366.   Inherited;
  367. end;
  368.  
  369. procedure TJwFlashClick.DoMouseEnter(var Message: TWMMouseEnter; Button: TMouseButton;
  370.   Shift: TShiftState);
  371. begin
  372.   with Message do
  373.     MouseEnter(Button, {KeysToShiftState(Keys) +} Shift, XPos, YPos);
  374. end;
  375.  
  376. procedure TJwFlashClick.MouseEnter(Button: TMouseButton;
  377.   Shift: TShiftState; X, Y: Integer);
  378. begin
  379.   if Assigned(FOnEnter) then FOnEnter(Self, Button, Shift, X, Y);
  380. end;
  381.  
  382. procedure TJwFlashClick.DoMouseExit(var Message: TWMMouseExit; Button: TMouseButton;
  383.   Shift: TShiftState);
  384. begin
  385.   with Message do
  386.     MouseExit(Button, {KeysToShiftState(Keys) +} Shift, XPos, YPos);
  387. end;
  388.  
  389. procedure TJwFlashClick.MouseExit(Button: TMouseButton;
  390.   Shift: TShiftState; X, Y: Integer);
  391. begin
  392.   if Assigned(FOnExit) then FOnExit(Self, Button, Shift, X, Y);
  393. end;
  394.  
  395. end.
  396.