home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / kolekce / d56 / TDSOFT.ZIP / TDSuperComboBox.pas < prev    next >
Pascal/Delphi Source File  |  2001-07-09  |  9KB  |  321 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 TDSuperComboBox;
  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.   TTDSuperComboBox = class(TCustomComboBox)
  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.     FToRightWinControl: TWinControl;
  39.     FToLeftWinControl: TWinControl;
  40.     FUseArrowNavigation: Boolean;
  41.     { Private declarations }
  42.     Procedure MeasureItems(Control: TWinControl; Index: Integer; var Height: Integer);
  43.     Procedure DrawItems(Control: TWinControl; Index: Integer; RectArea: TRect; State: TOwnerDrawState);
  44.     procedure SetValue(const Value: String);
  45.     function GetValue: String;
  46.     procedure SetSuperStyle(const Value: TTDStyle);
  47.     procedure SetEvenFont(const Value: TFont);
  48.     procedure SetOddFont(const Value: TFont);
  49.     procedure SetToLeftWinControl(const Value: TWinControl);
  50.     procedure SetToRightWinControl(const Value: TWinControl);
  51.     procedure WMKEYDOWN (var Msg: TMessage); message WM_KEYDOWN;    
  52.   protected
  53.     { Protected declarations }
  54.     procedure CreateParams(var Params: TCreateParams); override;
  55.     property Color;
  56.   public
  57.     { Public declarations }
  58.     constructor Create(AOwner: TComponent); override;
  59.     destructor Destroy; override;
  60.     procedure Populate(const HideString,Visible: String);
  61.     property Value: String read GetValue write SetValue;
  62.   published
  63.     //property Style; {Must be published before Items}
  64.     property Anchors;
  65.     property BiDiMode;
  66.     property Constraints;
  67.     property Ctl3D;
  68.     property DragCursor;
  69.     property DragKind;
  70.     property DragMode;
  71.     property DropDownCount;
  72.     property Enabled;
  73.     property Font;
  74.     property ImeMode;
  75.     property ImeName;
  76.     property ItemHeight;
  77.     property MaxLength;
  78.     property ParentBiDiMode;
  79.     property ParentColor;
  80.     property ParentCtl3D;
  81.     property ParentFont;
  82.     property ParentShowHint;
  83.     property PopupMenu;
  84.     property ShowHint;
  85.     property Sorted;
  86.     property TabOrder;
  87.     property TabStop;
  88.     property Text;
  89.     property Visible;
  90.     property OnChange;
  91.     property OnClick;
  92.     property OnContextPopup;
  93.     property OnDblClick;
  94.     property OnDragDrop;
  95.     property OnDragOver;
  96.     //property OnDrawItem;
  97.     property OnDropDown;
  98.     property OnEndDock;
  99.     property OnEndDrag;
  100.     property OnEnter;
  101.     property OnExit;
  102.     property OnKeyDown;
  103.     property OnKeyPress;
  104.     property OnKeyUp;
  105.     //property OnMeasureItem;
  106.     property OnStartDock;
  107.     property OnStartDrag;
  108.     property Items; { Must be published after OnMeasureItem }
  109.     property HideItems: TStringList read FHideItems write FHideItems;
  110.     //Aggiunte da me
  111.     property ImageList: TimageList read FImageList write FImageList;
  112.     property EvenBackground: TColor read FEvenBackground write FEvenBackground;
  113.     property OddBackground: TColor read FOddBackground write FOddBackground;
  114.     property EvenFont: TFont read FEvenFont write SetEvenFont;
  115.     property OddFont: TFont read FOddFont write SetOddFont;
  116.     property SuperStyle: TTDStyle read FSuperStyle write SetSuperStyle;
  117.     property ImageIndex: integer read FImageIndex write FImageIndex;
  118.     property ToLeftControl: TWinControl read FToLeftWinControl write SetToLeftWinControl;
  119.     property ToRightControl: TWinControl read FToRightWinControl write SetToRightWinControl;
  120.     property UseArrowNavigation: Boolean read FUseArrowNavigation write FUseArrowNavigation;    
  121.   end;
  122.  
  123. {.$R *.DCR}
  124. procedure Register;
  125.  
  126. implementation
  127.  
  128. procedure Register;
  129. begin
  130.   RegisterComponents('TDSoft', [TTDSuperComboBox]);
  131. end;
  132.  
  133. { TTDSuperComboBox }
  134.  
  135. constructor TTDSuperComboBox.Create(AOwner: TComponent);
  136. begin
  137.   inherited;
  138.   FEvenFont:=TFont.create;
  139.   FEvenFont.Color:=clBlack;
  140.  
  141.   FOddFont:=TFont.create;
  142.   FOddFont.Color:=clBlack;
  143.  
  144.   FEvenBackGround:=clWhite;
  145.   FOddBackGround:=clYellow;
  146.  
  147.   HideItems:=TStringList.Create;
  148.  
  149.   Color:=FEvenBackGround;
  150.   Style:=csOwnerDrawVariable;
  151.   OnMeasureItem:=MeasureItems;
  152.   OnDrawItem:=DrawItems;
  153.   FImageIndex:=-1;
  154.  
  155.   FUseArrowNavigation:=True;
  156. end;
  157.  
  158. procedure TTDSuperComboBox.CreateParams(var Params: TCreateParams);
  159. begin
  160.   inherited;
  161.   Params.Style:=Params.Style or WS_CHILD
  162.                 or CBS_OWNERDRAWVARIABLE and (not CBS_OWNERDRAWFIXED);
  163.   case FSuperStyle of
  164.     csDropDown:
  165.       Params.Style:=Params.Style or CBS_DROPDOWN or CBS_SIMPLE and (not CBS_DROPDOWNLIST);
  166.     csDropDownList:
  167.       Params.Style:=Params.Style or CBS_DROPDOWNLIST and (not CBS_DROPDOWN) and (not CBS_SIMPLE);
  168.   end;
  169.  
  170. end;
  171.  
  172. destructor TTDSuperComboBox.Destroy;
  173. begin
  174.   FEvenFont.free;
  175.   FOddFont.free;
  176.   FHideItems.Free;
  177.   inherited;
  178. end;
  179.  
  180. procedure TTDSuperComboBox.DrawItems(Control: TWinControl; Index: Integer; RectArea: TRect;
  181.   State: TOwnerDrawState);
  182. var
  183.   bmp: TBitmap;
  184.   ImageOrizOffSet: cardinal;
  185.   IndexOfImage: integer;
  186. begin
  187.   inherited;
  188.   bmp:=TBitmap.create;
  189.   try
  190.     with (Control as TCustomComboBox).canvas do
  191.     begin
  192.       if Odd(Index) then
  193.       begin
  194.         Brush.color:=FOddBackground;
  195.         (Control as TCustomComboBox).Canvas.Font:=FOddFont;
  196.       end
  197.       else
  198.       begin
  199.         Brush.Color:=FEvenBackground;
  200.         (Control as TCustomComboBox).Canvas.Font:=FEvenFont;
  201.       end;
  202.  
  203.       if Assigned(FImageList) then
  204.       begin
  205.         if FImageIndex>-1 then
  206.           IndexOfImage:=FImageIndex
  207.         else
  208.           IndexOfImage:=Index;
  209.  
  210.         if IndexOfImage < FImageList.Count then
  211.         begin
  212.           FImageList.GetBitmap(IndexOfImage,bmp);
  213.           ImageOrizOffSet:=bmp.width;
  214.         end
  215.         else
  216.         begin
  217.           bmp:=nil;
  218.           ImageOrizOffSet:=0;
  219.         end;
  220.  
  221.         //Draw a Bitmap
  222.         if bmp<>nil then Draw(RectArea.left,RectArea.top,bmp);
  223.       end
  224.       else
  225.         ImageOrizOffSet:=0;
  226.  
  227.  
  228.  
  229.       {$WARNINGS OFF}
  230.       FillRect(Rect(RectArea.left + ImageOrizOffSet,RectArea.top,RectArea.right,RectArea.bottom));
  231.       {$WARNINGS ON}
  232.  
  233.       //Draw Text Item
  234.       {$WARNINGS OFF}
  235.       TextOut(RectArea.left + ImageOrizOffSet + GetSystemMetrics(SM_CXEDGE),
  236.               RectArea.top + (itemheight div 2) - (TextHeight('I') div 2),
  237.               items[index]);
  238.       {$WARNINGS ON}
  239.             
  240.     end;
  241.  
  242.   finally
  243.     bmp.free;
  244.   end;
  245. end;
  246.  
  247. function TTDSuperComboBox.GetValue: String;
  248. begin
  249.   Result:=HideItems[ItemIndex];
  250. end;
  251.  
  252. procedure TTDSuperComboBox.MeasureItems(Control: TWinControl;
  253.   Index: Integer; var Height: Integer);
  254. begin
  255.   Height:=ItemHeight;
  256. end;
  257.  
  258. procedure TTDSuperComboBox.Populate(const HideString, Visible: String);
  259. begin
  260.   items.Add(Visible);
  261.   HideItems.Add(HideString);
  262. end;
  263.  
  264. procedure TTDSuperComboBox.SetEvenFont(const Value: TFont);
  265. begin
  266.   FEvenFont.Assign(Value);
  267. end;
  268.  
  269. procedure TTDSuperComboBox.SetOddFont(const Value: TFont);
  270. begin
  271.   FOddFont.assign(Value);
  272. end;
  273.  
  274. procedure TTDSuperComboBox.SetSuperStyle(const Value: TTDStyle);
  275. begin
  276.   FSuperStyle := Value;
  277.   RecreateWnd;
  278. end;
  279.  
  280. procedure TTDSuperComboBox.SetToLeftWinControl(const Value: TWinControl);
  281. begin
  282.   FToLeftWinControl := Value;
  283. end;
  284.  
  285. procedure TTDSuperComboBox.SetToRightWinControl(const Value: TWinControl);
  286. begin
  287.   FToRightWinControl := Value;
  288. end;
  289.  
  290. procedure TTDSuperComboBox.SetValue(const Value: String);
  291. begin
  292.   FValue := Value;
  293.   ItemIndex:=HideItems.indexOf(Value);
  294. end;
  295.  
  296. procedure TTDSuperComboBox.WMKEYDOWN(var Msg: TMessage);
  297. begin
  298.     case Msg.wParam of
  299.       VK_LEFT:
  300.             if FUseArrowNavigation then
  301.               if Assigned(ToLeftControl) then
  302.             begin
  303.                 ToLeftControl.SetFocus;
  304.               Exit;
  305.             end;
  306.  
  307.       VK_RIGHT:
  308.             if FUseArrowNavigation then
  309.               if Assigned(ToRightControl) then
  310.             begin
  311.                             ToRightControl.SetFocus;
  312.               Exit;
  313.             end;
  314.   end;
  315.   inherited;
  316.  
  317.  
  318. end;
  319.  
  320. end.
  321.