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

  1. {*********************************************************}
  2. {*                   ESTILE.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. {$IFNDEF Win32}
  17.   {$G+} {286 Instructions}
  18.   {$N+} {Numeric Coprocessor}
  19.   {$C MOVEABLE,DEMANDLOAD,DISCARDABLE}
  20. {$ENDIF}
  21.  
  22. unit EsTile;
  23.   {-tile component}
  24.  
  25. interface
  26.  
  27. uses
  28.   {$IFDEF Win32} Windows, {$ELSE} WinTypes, WinProcs, {$ENDIF}
  29.   Classes, Controls, Forms, Graphics, Messages,
  30.   EsConst, EsData;
  31.  
  32. type
  33.   TEsCustomTile = class(TGraphicControl)
  34.   protected {private}
  35.     {.Z+}
  36.     {property variables}
  37.     FBitmap : TBitmap;
  38.  
  39.     {internal methods}
  40.     procedure tiBitmapChange(Sender : TObject);
  41.  
  42.     {property methods}
  43.     function GetVersion : string;
  44.     procedure SetBitmap(Value : TBitmap);
  45.     procedure SetVersion(const Value : string);
  46.     {.Z-}
  47.   protected
  48.     {.Z+}
  49.     procedure Paint;
  50.       override;
  51.     {.Z-}
  52.  
  53.     property Bitmap : TBitmap
  54.       read FBitmap
  55.       write SetBitmap;
  56.  
  57.     property Version : string
  58.       read GetVersion
  59.       write SetVersion
  60.       stored False;
  61.  
  62.   public
  63.     {.Z+}
  64.     constructor Create(AComponent : TComponent);
  65.       override;
  66.     destructor Destroy;
  67.       override;
  68.     procedure PaintTo(DC : TEsHdc; R : TRect);
  69.     {.Z-}
  70.   end;
  71.  
  72.   TEsTile = class(TEsCustomTile)
  73.   published
  74.     property Align;
  75.     property Bitmap;
  76.     property Version;
  77.  
  78.     property OnClick;
  79.     property OnDblClick;
  80.     property OnDragDrop;
  81.     property OnDragOver;
  82.     property OnMouseDown;
  83.     property OnMouseMove;
  84.     property OnMouseUp;
  85.     {$IFDEF WIN32}
  86.     property OnStartDrag;
  87.     {$ENDIF WIN32}
  88.   end;
  89.  
  90.  
  91. implementation
  92.  
  93.  
  94. {$IFDEF TRIALRUN}
  95. uses
  96.   EsTrial;
  97. {$I ESTRIALF.INC}
  98. {$ENDIF}
  99.  
  100.  
  101. constructor TEsCustomTile.Create(AComponent : TComponent);
  102. {$IFDEF TRIALRUN}
  103. var
  104.   X : Integer;
  105. {$ENDIF}
  106. begin
  107.   inherited Create(AComponent);
  108.  
  109.   ControlStyle := ControlStyle + [csOpaque];                           {!!.04}
  110.  
  111.   Width       := 100;
  112.   Height      := 100;
  113.  
  114.   FBitmap := TBitmap.Create;
  115.   FBitmap.OnChange := tiBitmapChange;
  116.  
  117. {$IFDEF TRIALRUN}
  118.   X := _CC_;
  119.   if (X < ccRangeLow) or (X > ccRangeHigh) then Halt;
  120.   X := _VC_;
  121.   if (X < ccRangeLow) or (X > ccRangeHigh) then Halt;
  122. {$ENDIF}
  123. end;
  124.  
  125. destructor TEsCustomTile.Destroy;
  126. begin
  127.   FBitmap.Free;
  128.   FBitmap := nil;
  129.  
  130.   inherited Destroy;
  131. end;
  132.  
  133. function TEsCustomTile.GetVersion : string;
  134. begin
  135.   Result := EsVersionStr;
  136. end;
  137.  
  138. procedure TEsCustomTile.Paint;
  139. begin
  140.   if not Assigned(FBitmap) or FBitmap.Empty then begin
  141.     Canvas.Brush.Color := Color;
  142.     Canvas.FillRect(ClientRect);
  143.     Exit;
  144.   end;
  145.  
  146.   PaintTo(Canvas.Handle, ClientRect);
  147. end;
  148.  
  149. procedure TEsCustomTile.PaintTo(DC : TEshDC; R : TRect);
  150. var
  151.   HTiles  : Integer;
  152.   VTiles  : Integer;
  153.   X, Y    : Integer;
  154.   CW, CH  : Integer;
  155.   TmpDC   : hDC;
  156.   Bmp     : hBitmap;
  157.   OldBmp  : hBitmap;
  158.   OldPal  : hPalette;
  159.   OldPal2 : hPalette;
  160. begin
  161.   if FBitmap.Handle > 0 then {force handle creation};
  162.  
  163.   {get the width and height}
  164.   CW := R.Right-R.Left;
  165.   CH := R.Bottom-R.Top;
  166.  
  167.   OldPal := 0;
  168.   if FBitmap.Palette <> 0 then begin
  169.     {if the bitmap has a palette, use it}
  170.     OldPal := SelectPalette(DC, FBitmap.Palette, True);
  171.     RealizePalette(DC);
  172.   end;
  173.   try
  174.     {create a temporary device context}
  175.     TmpDC := CreateCompatibleDC(DC);
  176.     try
  177.       {create a bitmap to draw on}
  178.       Bmp := CreateCompatibleBitmap(DC, CW, CH);
  179.       try
  180.         {select the bitmap into the temporary DC}
  181.         OldBmp := SelectObject(TmpDC, Bmp);
  182.         try
  183.           OldPal2 := 0;
  184.           if FBitmap.Palette <> 0 then begin
  185.             OldPal2 := SelectPalette(TmpDC, FBitmap.Palette, True);
  186.             RealizePalette(TmpDC);
  187.           end;
  188.           try
  189.             {compute needed tiles}
  190.             HTiles := CW div FBitmap.Width;
  191.             if CW mod FBitmap.Width <> 0 then
  192.               Inc(HTiles);
  193.             VTiles := CH div FBitmap.Height;
  194.             if CH mod FBitmap.Height <> 0 then
  195.               Inc(VTiles);
  196.  
  197.             {paint the tiles}
  198.             for X := 0 to Pred(HTiles) do
  199.               for Y := 0 to Pred(VTiles) do
  200.                 BitBlt(TmpDC, X*FBitmap.Width, Y*FBitmap.Height,
  201.                   FBitmap.Width, FBitmap.Height, FBitmap.Canvas.Handle, 0, 0, SRCCOPY);
  202.  
  203.             {copy temporary device context to ours}
  204.             BitBlt(DC, 0, 0, CW, CH, TmpDC, 0, 0, SRCCOPY);
  205.           finally
  206.             if OldPal2 <> 0 then
  207.               SelectPalette(TmpDC, OldPal2, True);
  208.           end;
  209.         finally
  210.           SelectObject(TmpDC, OldBmp);
  211.         end;
  212.       finally
  213.         DeleteObject(Bmp);
  214.       end;
  215.     finally
  216.       DeleteDC(TmpDC);
  217.     end;
  218.   finally
  219.     if OldPal > 0 then
  220.       SelectPalette(DC, OldPal, True);
  221.   end;
  222. end;
  223.  
  224. procedure TEsCustomTile.SetBitmap(Value : TBitmap);
  225. begin
  226.   if Assigned(Value) then
  227.     FBitmap.Assign(Value)
  228.   else
  229.     FBitmap.ReleaseHandle;
  230.   Invalidate;
  231. end;
  232.  
  233. procedure TEsCustomTile.SetVersion(const Value : string);
  234. begin
  235. end;
  236.  
  237. procedure TEsCustomTile.tiBitmapChange(Sender : TObject);
  238. begin
  239.   Invalidate;
  240. end;
  241.  
  242. end.
  243.