home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / kolekce / d56 / TDSOFT.ZIP / TDSuperListBox.pas < prev    next >
Pascal/Delphi Source File  |  2001-04-27  |  8KB  |  294 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       TDSoft Visual Component Library                 }
  5. {                                                       }
  6. {       Copyright (c) 2001 Daniele Teti                 }
  7. {                                                       }
  8. {-------------------------------------------------------}
  9. {       For suggest, request, bug or only for sport:    }
  10. {       Daniele_Teti@hotmail.com                        }
  11. {-------------------------------------------------------}
  12. {                                                       }
  13. {                                                       }
  14. {*******************************************************}
  15.  
  16. unit TDSuperListBox;
  17.  
  18. interface
  19.  
  20. uses
  21.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  22.   StdCtrls;
  23.  
  24. type
  25.   TTDStyle=(csDropDown, csDropDownList);
  26.  
  27.   TTDSuperListBox = class(TCustomListBox)
  28.   private
  29.     FImageList: TimageList;
  30.     FOddBackground: TColor;
  31.     FEvenBackground: TColor;
  32.     FOddFont: TFont;
  33.     FEvenFont: TFont;
  34.     FHideItems: TStringList;
  35.     FValue: String;
  36.   //FSuperStyle: TTDStyle;
  37.     FImageIndex: integer;
  38.     { Private declarations }
  39.     Procedure MeasureItems(Control: TWinControl; Index: Integer; var Height: Integer);
  40.     Procedure DrawItems(Control: TWinControl; Index: Integer; RectArea: TRect; State: TOwnerDrawState);
  41.     procedure SetValue(const Value: String);
  42.     function GetValue: String;
  43.     //procedure SetSuperStyle(const Value: TTDStyle);
  44.     procedure SetEvenFont(const Value: TFont);
  45.     procedure SetOddFont(const Value: TFont);
  46.   protected
  47.     { Protected declarations }
  48.     procedure CreateParams(var Params: TCreateParams); override;
  49.     property Color;    
  50.   public
  51.     { Public declarations }
  52.     constructor Create(AOwner: TComponent); override;
  53.     destructor Destroy; override;
  54.     procedure Populate(const HideString,Visible: String);
  55.     property Value: String read GetValue write SetValue;
  56.   published
  57.     property Align;
  58.     property Anchors;
  59.     property BiDiMode;
  60.     property BorderStyle;
  61.     property Columns;
  62.     property Constraints;
  63.     property Ctl3D;
  64.     property DragCursor;
  65.     property DragKind;
  66.     property DragMode;
  67.     property Enabled;
  68.     property ExtendedSelect;
  69.     property Font;
  70.     property ImeMode;
  71.     property ImeName;
  72.     property IntegralHeight;
  73.     property ItemHeight;
  74.     property Items;
  75.     property MultiSelect;
  76.     property ParentBiDiMode;
  77.     property ParentColor;
  78.     property ParentCtl3D;
  79.     property ParentFont;
  80.     property ParentShowHint;
  81.     property PopupMenu;
  82.     property ShowHint;
  83.     //property Sorted;
  84.     //property Style;
  85.     property TabOrder;
  86.     property TabStop;
  87.     property TabWidth;
  88.     property Visible;
  89.     property OnClick;
  90.     property OnContextPopup;
  91.     property OnDblClick;
  92.     property OnDragDrop;
  93.     property OnDragOver;
  94.     property OnDrawItem;
  95.     property OnEndDock;
  96.     property OnEndDrag;
  97.     property OnEnter;
  98.     property OnExit;
  99.     property OnKeyDown;
  100.     property OnKeyPress;
  101.     property OnKeyUp;
  102.     property OnMeasureItem;
  103.     property OnMouseDown;
  104.     property OnMouseMove;
  105.     property OnMouseUp;
  106.     property OnStartDock;
  107.     property OnStartDrag;
  108.     property HideItems: TStringList read FHideItems write FHideItems;
  109.     //Aggiunte da me
  110.     property ImageList: TimageList read FImageList write FImageList;
  111.     property EvenBackground: TColor read FEvenBackground write FEvenBackground;
  112.     property OddBackground: TColor read FOddBackground write FOddBackground;
  113.     property EvenFont: TFont read FEvenFont write SetEvenFont;
  114.     property OddFont: TFont read FOddFont write SetOddFont;
  115.     //property SuperStyle: TTDStyle read FSuperStyle write SetSuperStyle;
  116.     property ImageIndex: integer read FImageIndex write FImageIndex;
  117.   end;
  118.  
  119. {.$R *.DCR}
  120. procedure Register;
  121.  
  122. implementation
  123.  
  124. procedure Register;
  125. begin
  126.   RegisterComponents('TDSoft', [TTDSuperListBox]);
  127. end;
  128.  
  129. { TTDSuperListBox }
  130.  
  131. constructor TTDSuperListBox.Create(AOwner: TComponent);
  132. begin
  133.   inherited;
  134.   FEvenFont:=TFont.create;
  135.   FEvenFont.Color:=clBlack;
  136.   Sorted:=False;
  137.   FOddFont:=TFont.create;
  138.   FOddFont.Color:=clBlack;
  139.  
  140.   FEvenBackGround:=clWhite;
  141.   FOddBackGround:=clYellow;
  142.  
  143.   HideItems:=TStringList.Create;
  144.  
  145.   Color:=FEvenBackGround;
  146.   //Style:=csOwnerDrawVariable;
  147.   OnMeasureItem:=MeasureItems;
  148.   OnDrawItem:=DrawItems;
  149.   FImageIndex:=-1;
  150. end;
  151.  
  152. procedure TTDSuperListBox.CreateParams(var Params: TCreateParams);
  153. begin
  154.   inherited;
  155. //  Params.Style:=Params.Style or WS_CHILD or WS_HSCROLL
  156. //                or LBS_OWNERDRAWVARIABLE and (not LBS_OWNERDRAWFIXED)
  157. //                and (not LBS_SORT);
  158.   Params.Style:=Params.Style or WS_CHILD or WS_HSCROLL
  159.                 or LBS_OWNERDRAWVARIABLE
  160.                 and (not LBS_SORT);
  161.                 //and (not CBS_SORT);
  162.  
  163.  {
  164.   case FSuperStyle of
  165.     csDropDown:
  166.       Params.Style:=Params.Style or LBS_DROPDOWN;
  167.     csDropDownList:
  168.       Params.Style:=Params.Style or LBS_DROPDOWNLIST;
  169.   end;
  170.  }
  171.  
  172. end;
  173.  
  174. destructor TTDSuperListBox.Destroy;
  175. begin
  176.   inherited;
  177.   FEvenFont.free;
  178.   FOddFont.free;
  179.   FHideItems.Free;
  180. end;
  181.  
  182. procedure TTDSuperListBox.DrawItems(Control: TWinControl; Index: Integer; RectArea: TRect;
  183.   State: TOwnerDrawState);
  184. var
  185.   bmp: TBitmap;
  186.   ImageOrizOffSet: cardinal;
  187.   IndexOfImage: integer;
  188. begin
  189.   inherited;
  190.   bmp:=TBitmap.create;
  191.   try
  192.     with (Control as TCustomListBox).canvas do
  193.     begin
  194.       if Odd(Index) then
  195.       begin
  196.         Brush.color:=FOddBackground;
  197.         (Control as TCustomListBox).Canvas.Font:=FOddFont;
  198.       end
  199.       else
  200.       begin
  201.         Brush.Color:=FEvenBackground;
  202.         (Control as TCustomListBox).Canvas.Font:=FEvenFont;
  203.       end;
  204.  
  205.       //if Index=ItemIndex then Brush.Color:=clRed;
  206.  
  207.       if Assigned(FImageList) then
  208.       begin
  209.         if FImageIndex>-1 then
  210.           IndexOfImage:=FImageIndex
  211.         else
  212.           IndexOfImage:=Index;
  213.  
  214.         if IndexOfImage < FImageList.Count then
  215.         begin
  216.           FImageList.GetBitmap(IndexOfImage,bmp);
  217.           ImageOrizOffSet:=bmp.width;
  218.         end
  219.         else
  220.         begin
  221.           bmp:=nil;
  222.           ImageOrizOffSet:=0;
  223.         end;
  224.  
  225.         //Draw a Bitmap
  226.         if bmp<>nil then Draw(RectArea.left,RectArea.top,bmp);
  227.       end
  228.       else
  229.         ImageOrizOffSet:=0;
  230.  
  231.  
  232.  
  233.       {$WARNINGS OFF}
  234.       FillRect(Rect(RectArea.left + ImageOrizOffSet,RectArea.top,RectArea.right,RectArea.bottom));
  235.       {$WARNINGS ON}
  236.  
  237.       //Draw Text Item
  238.       {$WARNINGS OFF}
  239.       TextOut(RectArea.left + ImageOrizOffSet + GetSystemMetrics(SM_CXEDGE),
  240.               RectArea.top + (itemheight div 2) - (TextHeight('I') div 2),
  241.               items[index]);
  242.       {$WARNINGS ON}
  243.  
  244.     end;
  245.  
  246.   finally
  247.     bmp.free;
  248.   end;
  249.  
  250.   //InvalidateRect(Handle,@ClientRect,True);
  251. end;
  252.  
  253. function TTDSuperListBox.GetValue: String;
  254. begin
  255.   Result:=HideItems[ItemIndex];
  256. end;
  257.  
  258. procedure TTDSuperListBox.MeasureItems(Control: TWinControl;
  259.   Index: Integer; var Height: Integer);
  260. begin
  261.   Height:=ItemHeight;
  262. end;
  263.  
  264. procedure TTDSuperListBox.Populate(const HideString, Visible: String);
  265. begin
  266.   items.Add(Visible);
  267.   HideItems.Add(HideString);
  268. end;
  269.  
  270. procedure TTDSuperListBox.SetEvenFont(const Value: TFont);
  271. begin
  272.   FEvenFont.Assign(Value);
  273. end;
  274.  
  275. procedure TTDSuperListBox.SetOddFont(const Value: TFont);
  276. begin
  277.   FOddFont.assign(Value);
  278. end;
  279.  
  280. {
  281. procedure TTDSuperListBox.SetSuperStyle(const Value: TTDStyle);
  282. begin
  283.   FSuperStyle := Value;
  284.   RecreateWnd;
  285. end;
  286. }
  287. procedure TTDSuperListBox.SetValue(const Value: String);
  288. begin
  289.   FValue := Value;
  290.   ItemIndex:=HideItems.indexOf(Value);
  291. end;
  292.  
  293. end.
  294.