⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wwimagecombo.pas

📁 InfoPower_Studio 2007 v5.0.1.3 banben
💻 PAS
📖 第 1 页 / 共 3 页
字号:
unit wwimagecombo;
{
//
// Components : TwwImagecombo
//
// Copyright (c) 2007 by Woll2Woll Software
}

interface

uses
  Messages, Windows, SysUtils, CommCtrl, Classes, Controls, Forms, Menus,
  Graphics, StdCtrls, ImgList, ExtCtrls, DB, comctrls, dbctrls, wwdbcomb, listActns,
  wwtypes, wwcommon, wwframe;

type

//  TCustomComboBoxEx = class;

  TwwComboExItem = class;
  TwwComboExItems = class;
  TwwComboItemsSortType = (icstNone, icstDisplayedText, icstStoredText);
  TwwComboItemsCompareEvent = function(List: TwwComboExItems;
    Item1, Item2: TwwComboExItem): Integer of object;
  TwwComboItemsCompare = function(List: TwwComboExItems;
    Index1, Index2: Integer): Integer;


  TwwPopupImageListBox = class(TwwPopupListbox)
    protected
      procedure DrawItem(Index: Integer; ARect: TRect;
        State: TOwnerDrawState); override;
    public
      property Style;
  end;

  TwwComboExItem = class(TCollectionItem)
  private
    FDisplayedText: String;
    FStoredText: String;
    FImageIndex: TImageIndex;
    FSelectedImageIndex: TImageIndex;
//    FOverlayImageIndex: TImageIndex;
    FIndent: Integer;
  protected
//    procedure SetOverlayImageIndex(const Value: TImageIndex); virtual;
    procedure SetSelectedImageIndex(const Value: TImageIndex); virtual;
    procedure SetDisplayedText(const Value: String); virtual;
    procedure SetStoredText(const Value: String); virtual;
    procedure SetImageIndex(const Value: TImageIndex); virtual;
    procedure SetIndex(Value: Integer); override;
    function GetDisplayName: string; override;
  public
    procedure Assign(Source: TPersistent); override;
  published
    property DisplayedText: string read FDisplayedText write FDisplayedText;
    property StoredText : string read FStoredText write FStoredText;
    property ImageIndex: TImageIndex read FImageIndex write SetImageIndex default -1;
    property Indent: Integer read FIndent write FIndent default -1;
//    property OverlayImageIndex: TImageIndex read FOverlayImageIndex
//      write SetOverlayImageIndex default -1;
    property SelectedImageIndex: TImageIndex read FSelectedImageIndex
      write SetSelectedImageIndex default -1;
  end;

  TwwComboExItems = class(TOwnedCollection)
  private
    FSortType:  TwwComboItemsSortType;
    FOnCompare: TwwComboItemsCompareEvent;
    FCaseSensitive: boolean;
    function GetComboItem(const Index: Integer): TwwComboExItem;
    procedure SetSortType(const Value: TwwComboItemsSortType);
    procedure ExchangeItems(Index1, Index2: Integer);
    procedure QuickSort(L, R: Integer; SCompare: TwwComboItemsCompare);
    procedure CustomSort(Compare: TwwComboItemsCompare);

  protected
    procedure Notify(Item: TCollectionItem;
      Action: TCollectionNotification); override;
    function CompareItems(I1, I2: TwwComboExItem): Integer; virtual;

//    procedure SetItem(const Index: Integer); virtual;
  public
    function Add: TwwComboExItem;
    procedure Sort; virtual;
    constructor Create(AOwner: TPersistent; ItemClass: TCollectionItemClass);

//    function AddItem(const Caption: String; const ImageIndex, SelectedImageIndex,
//      OverlayImageIndex, Indent: Integer; Data: Pointer): TwwComboExItem;
    function Insert(Index: Integer): TwwComboExItem;
    property Items[const Index: Integer]: TwwComboExItem read GetComboItem; default;
//    property Items[const Index: Integer]: TListControlItem read GetListItem; default;
  published
    property SortType: TwwComboItemsSortType read FSortType write SetSortType default icstNone;
    property OnCompare: TwwComboItemsCompareEvent read FOnCompare write FOnCompare;
    property CaseSensitive:  boolean read FCaseSensitive write FCaseSensitive default false;
  end;

  TwwImageCombo = class(TwwDBCustomComboBox)
  private
    FItemsEx: TwwComboExItems;
    FImages: TCustomImageList;
    FValue: string;
    FNullTextDisplay: string;
    procedure SetImages(Value: TCustomImageList);
    procedure SetItemsEx(const Value: TwwComboExItems);
    function GetValue: string; virtual;

  protected
    procedure SetValue(value: string); override;
    Function CustomDraw: boolean; override;
    procedure Notification(AComponent: TComponent;
      Operation: TOperation); override;
    function CreateListBox: TwwPopupListBox; override;
    Procedure ShowText(ACanvas: TCanvas;
          ARect: TRect; indentLeft, indentTop: integer; AText: string; transparent: boolean = false); override;
    procedure Loaded; override;
    procedure DataChange(Sender: TObject); override;
//    Function GetStoredText: string; override;  { Map Text to stored value }
    procedure CloseUp(Accept: Boolean); override;
    procedure SetItemIndex(val: integer); override;
    function GetItemIndexFromText(DisplayedText: string): integer; virtual;    
    procedure SetEditRect; override;
    procedure Paint; override; // inherited Paint;
    function GetImageRect: TRect; virtual;
    procedure UpdateData(Sender: TObject); override;
    procedure DrawFocusRect(ACanvas: TCanvas; ARect: TRect); override;
    function CalcItemHeight: integer; override;
//    function IsItemsExStored: Boolean;
  public
    constructor Create(AOwner:tcomponent); override;
    destructor Destroy; override;
    procedure DropDown; override;
    procedure ApplyList; override;
    procedure RefreshDisplay;
    Function GetComboValue(DisplayText: string): string; override;
    Function GetComboDisplay(Value: string): string; override;    

  published
    property Controller;
    property BevelEdges;
    property BevelInner;
    property BevelKind default bkNone;
    property BevelOuter;

    property DisableThemes;
    property Anchors;
    property BiDiMode;
    property Constraints;
    property ParentBiDiMode;
    property ShowButton;  { Publish before Style property for SetStyle code }
    property Style;
    property MapList;

    property AllowClearKey;
    property AutoDropDown;
    property ShowMatchText;
    property AutoFillDate;
    property AutoSelect;
    property AutoSize;
    property BorderStyle;
    property ButtonStyle default cbsDownArrow;
    property CharCase;
    property Color;
    property Column1Width;
    property Ctl3D;
    property DataField;
    property DataSource;
    property DisableDropDownList;
    property DragMode;
    property DragCursor;
    property DropDownCount;
    property DropDownWidth;
    property Enabled;
    property Font;
    property ButtonEffects;
    property Frame;
    property ButtonWidth;
    property ButtonGlyph;
//    property HistoryList;
    property ImeMode;
    property ImeName;
//    property LimitEditRect;
    property MaxLength;
    property ParentColor;
    property ParentCtl3D;
    property ParentFont;
    property ParentShowHint;
    property Picture;
    property PopupMenu;
    property ReadOnly;
    property ShowHint;
//    property Sorted;
    property TabOrder;
    property TabStop;
    property UnboundDataType;
    property UsePictureMask;
    property Visible;
    property UnboundAlignment;

    property Images: TCustomImageList read FImages write SetImages;
    property ItemsEx: TwwComboExItems read FItemsEx write SetItemsEx;// stored IsItemsExStored;
    property ItemHeight;
    property Value :String read GetValue write SetValue;
    property NullTextDisplay: string read FNullTextDisplay write FNullTextDisplay;

    property OnAddHistoryItem;
    property OnChange;
    property OnCheckValue;
    property OnClick;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnDropDown;
    property OnCloseUp;
    property OnDrawItem;
    property OnMeasureItem;
    property OnEndDrag;
    property OnEnter;
    property OnExit;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;

  end;

 (*
  TwwImageCombo2 = class(TCustomComboBoxEx)
  private
    FDataLink: TFieldDataLink;

    function GetDataField: string;
    procedure SetDataField(const Value: string);
    function GetDataSource: TDataSource;
    procedure SetDataSource(Value: TDataSource);

    procedure DataChange(Sender: TObject);
    procedure UpdateData(Sender: TObject);
    procedure EditingChange(Sender: TObject); virtual;

    procedure CNNotify(var Message: TWMNotify); message CN_NOTIFY;

  protected
    procedure BeginEdit; virtual;
    procedure EndEdit; virtual;
  public
    property DataLink: TFieldDataLink read FDataLink;

    constructor Create(AOwner:tcomponent); override;
    destructor Destroy; override;

  published
    property Align;
    property AutoCompleteOptions default [acoAutoAppend];
    property ItemsEx;
    property Style; {Must be published before Items}
    property StyleEx;
    property Action;
    property Anchors;
    property BiDiMode;
    property Color;
    property Constraints;
    property Ctl3D;
    property DragCursor;
    property DragKind;
    property DragMode;
    property Enabled;
    property Font;
    property ImeMode;
    property ImeName;
    property ItemHeight;
    property MaxLength;
    property ParentBiDiMode;
    property ParentColor;
    property ParentCtl3D;
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property ShowHint;
    property TabOrder;
    property TabStop;
    property Text;
    property Visible;
    property OnBeginEdit;
    property OnChange;
    property OnClick;
    property OnContextPopup;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnDropDown;
    property OnEndEdit;
    property OnEndDock;
    property OnEndDrag;
    property OnEnter;
    property OnExit;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property OnMouseMove;
    property OnSelect;
    property OnStartDock;
    property OnStartDrag;
//    property Items;
    property Images;
    property DropDownCount;

    property DataField: string read GetDataField write SetDataField;
    property DataSource: TDataSource read GetDataSource write SetDataSource;
  end;
*)

implementation

procedure TwwPopupImageListBox.DrawItem(Index: Integer; ARect: TRect;
      State: TOwnerDrawState);
var combo: TwwImageCombo;
    comboItem: TwwComboExItem;
    imageRect, textRect: TRect;
    imageIndex: integer;
    RowRect: TRect;
    DoDefault: boolean;
begin
  Combo:= owner as TwwImageCombo;
  RowRect:= ARect;
  RowRect.Left:= 0;

  DoDefault:= True;
  if Assigned(Combo.OnDrawItem) then begin
     if not (odSelected in State) then begin
        Canvas.Brush.Color:= Color;
        Canvas.Font.Color:= Font.Color;
        ARect.Left:=0;
        Canvas.TextRect(RowRect, RowRect.Left, RowRect.Top, '');
     end;

     try
       Combo.OnDrawItem(Combo, Index, RowRect, State)
     except
       DoDefault:= False;
     end
  end;

  if not DoDefault then exit;

  comboItem:= combo.ItemsEx.Items[Index] as TwwComboExItem;
  if (comboItem.Indent<>-1) then
     ARect.Left:= ARect.Left + comboItem.Indent;

  if Combo.Images<>nil then
  begin
     if (odSelected in State) then begin
        Canvas.Brush.Color:= clHighlight;
        Canvas.Font.Color:= clHighlightText;
        Canvas.FillRect(RowRect);
        imageIndex:= comboItem.SelectedImageIndex;
     end
     else begin
        Canvas.Brush.Color:= Color;
        Canvas.Font.Color:= Font.Color;
        Canvas.FillRect(RowRect);
        imageIndex:= comboItem.ImageIndex;
     end;
     textRect:= ARect;
     textRect.Left:= textRect.Left + Combo.Images.Width + 5;
     textRect.Top:= textRect.Top + 1 + (textRect.Bottom-textRect.Top-Canvas.TextHeight('W')) div 2;
     Canvas.TextRect(textRect, textRect.Left, textRect.Top, comboItem.DisplayedText);

     imageRect:= ARect;
     imageRect.Left:= imageRect.Left + 2;
     imageRect.Right:= ARect.Left + Combo.Images.Width;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -