home *** CD-ROM | disk | FTP | other *** search
/ Freelog 11 / Freelog011.iso / BestOf / PhoenixMail / Source / comps / ComboBoxEx.pas < prev    next >
Pascal/Delphi Source File  |  1998-12-07  |  5KB  |  190 lines

  1. {*****************************************************************************
  2.  *
  3.  *  ComboBoxEx.pas - TComboBoxEx Component
  4.  *
  5.  *  Copyright (c) 1998-99 Michael Haller
  6.  *
  7.  *  Author:     Michael Haller
  8.  *  E-mail:     michael@discountdrive.com
  9.  *  Homepage:   http://www.discountdrive.com/sunrise/
  10.  *
  11.  *  This program is free software; you can redistribute it and/or
  12.  *  modify it under the terms of the GNU General Public License
  13.  *  as published by the Free Software Foundation;
  14.  *
  15.  *  This program is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *  GNU General Public License for more details.
  19.  *
  20.  *  You should have received a copy of the GNU General Public License
  21.  *  along with this program; if not, write to the Free Software
  22.  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
  23.  *
  24.  *----------------------------------------------------------------------------
  25.  *
  26.  *  Revision history:
  27.  *
  28.  *     DATE     REV                 DESCRIPTION
  29.  *  ----------- --- ----------------------------------------------------------
  30.  *
  31.  *****************************************************************************}
  32.  
  33. unit ComboBoxEx;
  34.  
  35. interface
  36.  
  37. uses
  38.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  39.   StdCtrls;
  40.  
  41. type
  42.   TComboBoxEx = class(TCustomComboBox)
  43.   private
  44.     FValues: TStringList;
  45.     FImgIndexes: TStringList;
  46.     FImages: TImageList;
  47.     FShowImages: Boolean;
  48.     procedure SetShowImages(B: Boolean);
  49.     procedure SetImages(IL: TImageList);
  50.     procedure SetImgIndexes(SL: TStringList);
  51.     procedure SetValues(SL: TStringList);
  52.     procedure SetValue(S: String);
  53.     function GetValue: String;
  54.   protected
  55.     procedure DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState); override;
  56.   public
  57.     constructor Create(AOwner: TComponent); override;
  58.     destructor Destroy; override;
  59.     property Value: String read GetValue write SetValue;
  60.   published
  61.     property Values: TStringlist read FValues write SetValues;
  62.     property ImgIndexes: TStringList read FImgIndexes write SetImgIndexes;
  63.     property Images: TImageList read FImages write SetImages;
  64.     property ShowImages: Boolean read FShowImages write SetShowImages;
  65.     property Color;
  66.     property Ctl3D;
  67.     property DragMode;
  68.     property DragCursor;
  69.     property DropDownCount;
  70.     property Enabled;
  71.     property Font;
  72.     property ImeMode;
  73.     property ImeName;
  74.     property ItemHeight;
  75.     property Items;
  76.     property MaxLength;
  77.     property ParentColor;
  78.     property ParentCtl3D;
  79.     property ParentFont;
  80.     property ParentShowHint;
  81.     property PopupMenu;
  82.     property ShowHint;
  83.     property Sorted;
  84.     property TabOrder;
  85.     property TabStop;
  86.     property Visible;
  87.     property OnChange;
  88.     property OnClick;
  89.     property OnDblClick;
  90.     property OnDragDrop;
  91.     property OnDragOver;
  92.     property OnDrawItem;
  93.     property OnDropDown;
  94.     property OnEndDrag;
  95.     property OnEnter;
  96.     property OnExit;
  97.     property OnKeyDown;
  98.     property OnKeyPress;
  99.     property OnKeyUp;
  100.     property OnMeasureItem;
  101.     property OnStartDrag;
  102.   end;
  103.  
  104. procedure Register;
  105.  
  106. implementation
  107.  
  108. constructor TComboBoxEx.Create(AOwner: TComponent);
  109. begin
  110.   inherited Create(AOwner);
  111.   FValues := TStringList.Create;
  112.   FImgIndexes := TStringList.Create;
  113.   FShowImages := False;
  114.   Style := csOwnerDrawFixed;
  115.   DropDownCount := 10;
  116. end;
  117.  
  118. destructor TComboBoxEx.Destroy;
  119. begin
  120.   Values.Free;
  121.   FImgIndexes.Free;
  122.   inherited Destroy;
  123. end;
  124.  
  125. procedure TComboBoxEx.SetShowImages(B: Boolean);
  126. begin
  127.   if FShowImages = B then Exit;
  128.   FShowImages := B;
  129.   Invalidate;
  130. end;
  131.  
  132. procedure TComboBoxEx.SetImages(IL: TImageList);
  133. begin
  134.   FImages := IL;
  135. end;
  136.  
  137. procedure TComboBoxEx.SetValues(SL: TStringList);
  138. begin
  139.   FValues.Assign(SL);
  140. end;
  141.  
  142. procedure TComboBoxEx.SetImgIndexes(SL: TStringList);
  143. begin
  144.   FImgIndexes.Assign(SL);
  145. end;
  146.  
  147. procedure TComboBoxEx.SetValue(S: String);
  148. var
  149.   I, E: Integer;
  150. begin
  151.   E := -1;
  152.   for I := 0 to Values.Count-1 do
  153.     if Values[I] = S then E := I;
  154.   ItemIndex := E;
  155. end;
  156.  
  157. function TComboBoxEx.GetValue: String;
  158. begin
  159.   Result := '';
  160.   if Values.Count > ItemIndex then
  161.     Result := Values[ItemIndex];
  162. end;
  163.  
  164. procedure TComboBoxEx.DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState);
  165. var
  166.   I: Integer;
  167. begin
  168.   if (ShowImages = False) or (Assigned(Images) = False) then begin
  169.     inherited DrawItem(Index, Rect, State);
  170.     Exit;
  171.   end;
  172.   with Canvas do begin
  173.     Brush.Color := clWindow;
  174.     if odSelected in State then Brush.Color := clActiveCaption;
  175.     FillRect(Rect);
  176.     if ImgIndexes.Count > Index then I := StrToInt(ImgIndexes.Strings[Index]) else I := 0;
  177.     Images.Draw(Canvas, Rect.Left+2, Rect.Top+0, I);
  178.     Font.Color := clWindowText;
  179.     if odSelected in State then Font.Color := clCaptionText;
  180.     TextOut(Rect.Left+23, Rect.Top+2, Items[Index]);
  181.   end;
  182. end;
  183.  
  184. procedure Register;
  185. begin
  186.   RegisterComponents('Michael Haller', [TComboBoxEx]);
  187. end;
  188.  
  189. end.
  190.