home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 November / Chip_2003-11_cd1.bin / program / delphi / kompon / DIHtmlLabel.exe / Demos / DIHtmlLabel / CategCh.pas next >
Encoding:
Pascal/Delphi Source File  |  2003-01-24  |  9.1 KB  |  329 lines

  1. {-----------------------------------------------------------------------------
  2. The contents of this file are subject to the Mozilla Public License
  3. Version 1.1 (the "License"); you may not use this file except in compliance
  4. with the License. You may obtain a copy of the License at
  5. http://www.mozilla.org/MPL/
  6.  
  7. Software distributed under the License is distributed on an "AS IS" basis,
  8. WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
  9. the specific language governing rights and limitations under the License.
  10.  
  11. The Original Code is: CategCh.pas, released 2002-01-06.
  12.  
  13. The Initial Developer of the Original Code is David Polberger <dpol@swipnet.se>
  14. Portions created by David Polberger are Copyright (C) 2002 David Polberger.
  15. All Rights Reserved.
  16.  
  17. Contributor(s): ______________________________________.
  18.  
  19. Last Modified: 2002-01-06;
  20. Current Version: 1.00
  21.  
  22. You may retrieve the latest version of this file at the Project JEDI home page,
  23. located at http://www.delphi-jedi.org
  24.  
  25. Known Issues:
  26.   None.
  27. Description:
  28.   TCategoryChooser displays an attractive list of categories that users can
  29.   choose from. TJvLinkLabel's demo project makes use of this component, by
  30.   creating instances of it at run-time.
  31.  
  32.   It is not intended to be a part of the JEDI VCL, as it does one specific thing
  33.   well, but not much else. Having said that, it's licened under the MPL license,
  34.   just like TJvLinkLabel, so you're free to use it in your own projects, if you
  35.   like. Simply add it to the package of your choice, and install it into the
  36.   IDE.
  37. -----------------------------------------------------------------------------}
  38.  
  39. unit CategCh;
  40.  
  41. interface
  42.  
  43. uses
  44.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms;
  45.  
  46. type
  47.   TCategoryChooser = class(TGraphicControl)
  48.   private
  49.     FCatList: TStringList;
  50.     FBackgroundColor: TColor;
  51.     FActiveColor: TColor;
  52.     FCatHeight: Integer;
  53.     FActiveCat: Integer;
  54.     FSelectedCat: Integer;
  55.     FLastOutOfBounds: Boolean;
  56.  
  57.     FCatChange: TNotifyEvent;
  58.     procedure SetCatList(const Value: TStringList);
  59.     function GetLineColor: TColor;
  60.     procedure SetLineColor(const Value: TColor);
  61.     function IsCursorWithinBounds: Boolean;
  62.     procedure DrawCat(Index: Integer; Color: TColor);
  63.     function GetCatAtPos(y: Integer): Integer;
  64.     procedure MouseLeave;
  65.     procedure SetBackgroundColor(const Value: TColor);
  66.     procedure SetActiveColor(const Value: TColor);
  67.     procedure SetCatHeight(const Value: Integer);
  68.     procedure SetSelectedCat(const Value: Integer);
  69.     procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
  70.   protected
  71.     procedure Paint; override;
  72.     procedure MouseMove(Shift: TShiftState; x, y: Integer); override;
  73.     procedure MouseUp(Button: TMouseButton; Shift: TShiftState; x, y: Integer); override;
  74.     procedure DoCatChange; virtual;
  75.   public
  76.     constructor Create(AOwner: TComponent); override;
  77.     destructor Destroy; override;
  78.     property ActiveCat: Integer read FActiveCat;
  79.     property Canvas;
  80.   published
  81.     property CatList: TStringList read FCatList write SetCatList;
  82.     property BackgroundColor: TColor read FBackgroundColor write SetBackgroundColor;
  83.     property ActiveColor: TColor read FActiveColor write SetActiveColor;
  84.     property LineColor: TColor read GetLineColor write SetLineColor;
  85.     property Font;
  86.     property ParentFont;
  87.     property CatHeight: Integer read FCatHeight write SetCatHeight;
  88.     property SelectedCat: Integer read FSelectedCat write SetSelectedCat;
  89.     property OnCatChange: TNotifyEvent read FCatChange write FCatChange;
  90.   end;
  91.  
  92. procedure Register;
  93.  
  94. implementation
  95.  
  96. procedure Register;
  97. begin
  98.   RegisterComponents('The Delphi Inspiration', [TCategoryChooser]);
  99. end;
  100.  
  101. { TCategoryChooser }
  102.  
  103. constructor TCategoryChooser.Create(AOwner: TComponent);
  104. begin
  105.   inherited Create(AOwner);
  106.   FCatList := TStringList.Create;
  107.  
  108.   // Set default values
  109.   FBackgroundColor := $00F0CAA6;
  110.   FActiveColor := clWhite;
  111.   FCatHeight := 24;
  112.   FSelectedCat := 0;
  113.   FLastOutOfBounds := False;
  114.  
  115.   Width := 100;
  116.   Height := 200;
  117. end;
  118.  
  119. { ---------------------------------------------------------------------------- }
  120.  
  121. destructor TCategoryChooser.Destroy;
  122. begin
  123.   inherited Destroy;
  124.   FCatList.Free;
  125. end;
  126.  
  127. { ---------------------------------------------------------------------------- }
  128.  
  129. procedure TCategoryChooser.CMMouseLeave(var Message: TMessage);
  130. begin
  131.   inherited;
  132.   MouseLeave;
  133. end;
  134.  
  135. { ---------------------------------------------------------------------------- }
  136.  
  137. function TCategoryChooser.IsCursorWithinBounds: Boolean;
  138. var
  139.   p: TPoint;
  140. begin
  141.   p := ScreenToClient(Mouse.CursorPos);
  142.   Result :=
  143.     (p.x >= 0) and (p.x <= Width) and
  144.     (p.y >= 0) and (p.y <= FCatList.Count * FCatHeight - 1);
  145. end;
  146.  
  147. { ---------------------------------------------------------------------------- }
  148.  
  149. procedure TCategoryChooser.DoCatChange;
  150. begin
  151.   if Assigned(FCatChange) then FCatChange(Self);
  152. end;
  153.  
  154. { ---------------------------------------------------------------------------- }
  155.  
  156. procedure TCategoryChooser.DrawCat(Index: Integer; Color: TColor);
  157.  
  158.   function ValueToAddToTop: Integer;
  159.   begin
  160.     { The first category does not have a line drawn at the top; thus we fill
  161.       the entire rectangle in this case. }
  162.     if Index = 0 then
  163.       Result := 0
  164.     else
  165.       Result := 1;
  166.   end;
  167.  
  168. begin
  169.   if (Index >= 0) and (Index < FCatList.Count) then
  170.     with Canvas do
  171.       begin
  172.         Brush.Color := Color;
  173.         FillRect(Rect(0, Index * FCatHeight + ValueToAddToTop, Width,
  174.           (Index + 1) * FCatHeight));
  175.  
  176.         TextOut(4, (FCatHeight div 2 - (Abs(Font.Height) div 2)) +
  177.           Index * FCatHeight - 2, FCatList[Index]);
  178.  
  179.         MoveTo(0, (Index + 1) * FCatHeight);
  180.         LineTo(Width + 1, (Index + 1) * FCatHeight);
  181.       end;
  182. end;
  183.  
  184. { ---------------------------------------------------------------------------- }
  185.  
  186. function TCategoryChooser.GetCatAtPos(y: Integer): Integer;
  187. begin
  188.   Result := y div FCatHeight;
  189.   if Result >= FCatList.Count then
  190.     Result := -1;
  191. end;
  192.  
  193. { ---------------------------------------------------------------------------- }
  194.  
  195. function TCategoryChooser.GetLineColor: TColor;
  196. begin
  197.   Result := Canvas.Pen.Color;
  198. end;
  199.  
  200. procedure TCategoryChooser.MouseLeave;
  201. begin
  202.   DrawCat(FActiveCat, FBackgroundColor);
  203.   FActiveCat := FSelectedCat;
  204.   DrawCat(FActiveCat, FActiveColor);
  205. end;
  206.  
  207. procedure TCategoryChooser.MouseMove(Shift: TShiftState; x, y: Integer);
  208. var
  209.   CatIndex: Integer;
  210. begin
  211.   inherited MouseMove(Shift, x, y);
  212.  
  213.   if IsCursorWithinBounds then
  214.     Cursor := crHandPoint
  215.   else
  216.     Cursor := crDefault;
  217.  
  218.   CatIndex := GetCatAtPos(y);
  219.  
  220.   if CatIndex <> FActiveCat then
  221.     if IsCursorWithinBounds then
  222.       begin
  223.         // Let's remove the highlight from the last active category
  224.         DrawCat(FActiveCat, FBackgroundColor);
  225.  
  226.         FActiveCat := CatIndex;
  227.         DrawCat(FActiveCat, FActiveColor);
  228.       end else
  229.       if not FLastOutOfBounds then MouseLeave;
  230.  
  231.   FLastOutOfBounds := not IsCursorWithinBounds;
  232. end;
  233.  
  234. procedure TCategoryChooser.MouseUp(Button: TMouseButton;
  235.   Shift: TShiftState; x, y: Integer);
  236. var
  237.   OldSelectedCat: Integer;
  238. begin
  239.   inherited MouseUp(Button, Shift, x, y);
  240.  
  241.   if GetCatAtPos(y) <> -1 then
  242.     begin
  243.       OldSelectedCat := FSelectedCat;
  244.       FSelectedCat := GetCatAtPos(y);
  245.  
  246.       if OldSelectedCat <> FSelectedCat then
  247.         DoCatChange;
  248.     end;
  249. end;
  250.  
  251. procedure TCategoryChooser.Paint;
  252. var
  253.   i: Integer;
  254.  
  255.   function GetColor: TColor;
  256.   begin
  257.     if i = FActiveCat then
  258.       Result := FActiveColor
  259.     else
  260.       Result := FBackgroundColor;
  261.   end;
  262.  
  263. begin
  264.   // inherited Paint; // Does nothing anyway.
  265.  
  266.   with Canvas do
  267.     begin
  268.       Font := Self.Font;
  269.       Brush.Color := FBackgroundColor;
  270.       FillRect(Rect(0, 0, Width, Height));
  271.  
  272.       for i := 0 to FCatList.Count - 1 do
  273.         DrawCat(i, GetColor);
  274.     end;
  275. end;
  276.  
  277. { ---------------------------------------------------------------------------- }
  278.  
  279. procedure TCategoryChooser.SetActiveColor(const Value: TColor);
  280. begin
  281.   FActiveColor := Value;
  282.   Paint;
  283. end;
  284.  
  285. { ---------------------------------------------------------------------------- }
  286.  
  287. procedure TCategoryChooser.SetBackgroundColor(const Value: TColor);
  288. begin
  289.   FBackgroundColor := Value;
  290.   Paint;
  291. end;
  292.  
  293. { ---------------------------------------------------------------------------- }
  294.  
  295. procedure TCategoryChooser.SetCatHeight(const Value: Integer);
  296. begin
  297.   FCatHeight := Value;
  298.   Paint;
  299. end;
  300.  
  301. { ---------------------------------------------------------------------------- }
  302.  
  303. procedure TCategoryChooser.SetCatList(const Value: TStringList);
  304. begin
  305.   FCatList.Assign(Value);
  306.   Paint;
  307. end;
  308.  
  309. { ---------------------------------------------------------------------------- }
  310.  
  311. procedure TCategoryChooser.SetLineColor(const Value: TColor);
  312. begin
  313.   Canvas.Pen.Color := Value;
  314. end;
  315.  
  316. { ---------------------------------------------------------------------------- }
  317.  
  318. procedure TCategoryChooser.SetSelectedCat(const Value: Integer);
  319. begin
  320.   FSelectedCat := Value;
  321.   FActiveCat := Value;
  322.  
  323.   DoCatChange;
  324.   Paint;
  325. end;
  326.  
  327. end.
  328.  
  329.