cxcolorcombobox.pas

来自「胜天进销存源码,国产优秀的进销存」· PAS 代码 · 共 1,962 行 · 第 1/5 页

PAS
1,962
字号

{********************************************************************}
{                                                                    }
{       Developer Express Visual Component Library                   }
{       ExpressEditors                                               }
{                                                                    }
{       Copyright (c) 1998-2008 Developer Express Inc.               }
{       ALL RIGHTS RESERVED                                          }
{                                                                    }
{   The entire contents of this file is protected by U.S. and        }
{   International Copyright Laws. Unauthorized reproduction,         }
{   reverse-engineering, and distribution of all or any portion of   }
{   the code contained in this file is strictly prohibited and may   }
{   result in severe civil and criminal penalties and will be        }
{   prosecuted to the maximum extent possible under the law.         }
{                                                                    }
{   RESTRICTIONS                                                     }
{                                                                    }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES            }
{   (DCU, OBJ, DLL, ETC.) ARE CONFIDENTIAL AND PROPRIETARY TRADE     }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS    }
{   LICENSED TO DISTRIBUTE THE EXPRESSEDITORS AND ALL                }
{   ACCOMPANYING VCL CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY. }
{                                                                    }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED       }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE         }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE        }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT   }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                       }
{                                                                    }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON        }
{   ADDITIONAL RESTRICTIONS.                                         }
{                                                                    }
{********************************************************************}

unit cxColorComboBox;

interface

{$I cxVer.inc}

uses
{$IFDEF DELPHI6}
  Variants,
{$ENDIF}
  Windows, Messages, TypInfo, SysUtils, Classes, Controls, Graphics, StdCtrls,
  Forms, Dialogs, cxClasses, cxControls, cxContainer, cxGraphics,
  cxCustomData, cxDataStorage, cxDataUtils, cxVariants,
  cxEdit, cxTextEdit, cxEditUtils, cxMaskEdit, cxDropDownEdit, cxImageComboBox,
  cxExtEditConsts,  cxFilterControlUtils;

type
  TcxColorBoxAlign = (cbaLeft, cbaRight);
  TcxColorNamingConvention = (cxncNone, cxncDelphi, cxncHTML4, cxncX11);
  TcxColorPrepareList = (cxplNone, cxplDelphi, cxplHTML4, cxplX11, cxplX11Ordered);
  TcxColorValueFormat = (cxcvRGB, cxcvHexadecimal, cxcvInteger);
  TcxDefaultColorStyle = (cxdcClear, cxdcColor, cxdcHatched, cxdcText, cxdcCustom);
  TcxMRUColorAction = (mcaNone, mcaMoved, mcaAdded, mcaDeleted);

type
  { TcxColorComboBoxItem }

  TcxColorComboBoxItem = class(TCollectionItem)
  private
    FColor: TColor;
    FDescription: TCaption;
    FIsCustomColor: Boolean;
    FTag: TcxTag;
    function GetDescription: TCaption;
    function IsTagStored: Boolean;
    procedure SetColor(const Value: TColor);
    procedure SetDescription(const Value: TCaption);
  public
    constructor Create(Collection: TCollection); override;
    procedure Assign(Source: TPersistent); override;
    property IsCustomColor: Boolean read FIsCustomColor;
  published
    property Color: TColor read FColor write SetColor;
    property Description: TCaption read GetDescription write SetDescription;
    property Tag: TcxTag read FTag write FTag stored IsTagStored;
  end;

  { TcxColorComboBoxItems }

  TcxCustomColorComboBoxProperties = class;  

  TcxColorComboBoxItems = class(TOwnedCollection)
  private
    FOnUpdate: TNotifyEvent;
    function GetItems(Index: Integer): TcxColorComboBoxItem;
    procedure SetItems(Index: Integer; const Value: TcxColorComboBoxItem);
  protected
    procedure Update(Item: TCollectionItem); override;
    property OnUpdate: TNotifyEvent read FOnUpdate write FOnUpdate;
  public
    function Owner: TcxCustomColorComboBoxProperties;
    property Items[Index: Integer]: TcxColorComboBoxItem read GetItems write SetItems; default;
    function FindColorItem(const AColor: TColor): TcxColorComboBoxItem; virtual;
    function GetIndexByColor(AColor: TColor): Integer;
    function GetColorByIndex(AIndex: Integer; ADefaultColor: TColor): TColor;
    function AddColor(const AColor: TColor;
      const ADescription: string): TcxColorComboBoxItem; virtual;
    function InsertColor(Index: Integer; const AColor: TColor;
      const ADescription: string): TcxColorComboBoxItem; virtual;
    function Add: TcxColorComboBoxItem;
    function Insert(Index: Integer): TcxColorComboBoxItem;
    procedure Move(CurIndex, NewIndex: Integer); virtual;
    procedure ClearCustom; virtual;    
    procedure ClearNonCustom; virtual;
  end;

  { TcxCustomColorComboBoxViewInfo }

  TcxCustomColorComboBoxViewInfo = class(TcxCustomTextEditViewInfo)
  private
    FBkColor: TColor;
    FColorBoxWidth: Integer;
    FColorBoxAlign: TcxColorBoxAlign;
    FColorBoxRect: TRect;
    FColorBoxFrameColor: TColor;
    FColorBoxColor: TColor;
    FShowDescriptions: Boolean;
    FDefaultColorStyle: TcxDefaultColorStyle;
    FFoundItem: Boolean;
  public
    property BkColor: TColor read FBkColor write FBkColor;
    property ColorBoxWidth: Integer read FColorBoxWidth write FColorBoxWidth;
    property ColorBoxAlign: TcxColorBoxAlign read FColorBoxAlign write FColorBoxAlign;
    property ColorBoxFrameColor: TColor read FColorBoxFrameColor write FColorBoxFrameColor;
    property ColorBoxColor: TColor read FColorBoxColor write FColorBoxColor;
    property ColorBoxRect: TRect read FColorBoxRect write FColorBoxRect;
    property DefaultColorStyle: TcxDefaultColorStyle read FDefaultColorStyle write FDefaultColorStyle;
    property ShowDescriptions: Boolean read FShowDescriptions write FShowDescriptions;
    property FoundItem: Boolean read FFoundItem write FFoundItem;
    procedure Paint(ACanvas: TcxCanvas); override;
    procedure Offset(DX, DY: Integer); override;
  end;

  { TcxCustomColorComboBoxViewData }

  TcxCustomColorComboBoxViewData = class(TcxCustomDropDownEditViewData)
  private
    function GetProperties: TcxCustomColorComboBoxProperties;
  protected
    procedure CalculateViewInfoProperties(AViewInfo: TcxCustomEditViewInfo); virtual;
    function InternalEditValueToDisplayText(AEditValue: TcxEditValue): string; override;
    function InternalGetEditConstantPartSize(ACanvas: TcxCanvas; AIsInplace: Boolean;
      AEditSizeProperties: TcxEditSizeProperties; var MinContentSize: TSize;
      AViewInfo: TcxCustomEditViewInfo): TSize; override;
    function IsComboBoxStyle: Boolean; override;
  public
    procedure Calculate(ACanvas: TcxCanvas; const ABounds: TRect;
      const P: TPoint; Button: TcxMouseButton; Shift: TShiftState;
      AViewInfo: TcxCustomEditViewInfo; AIsMouseEvent: Boolean); override;
    procedure DisplayValueToDrawValue(const ADisplayValue: TcxEditValue;
      AViewInfo: TcxCustomEditViewInfo); override;
    procedure EditValueToDrawValue(ACanvas: TcxCanvas; const AEditValue: TcxEditValue;
      AViewInfo: TcxCustomEditViewInfo); override;
    property Properties: TcxCustomColorComboBoxProperties read GetProperties;
  end;

  { TcxColorComboBoxListBox }

  TcxCustomColorComboBox = class;

  TcxCustomColorComboBoxListBox = class(TcxCustomComboBoxListBox)
  private
    function GetEdit: TcxCustomColorComboBox;
  protected
    procedure DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState); override;
    procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
  public
    function GetItemHeight(AIndex: Integer = -1): Integer; override;
    property Edit: TcxCustomColorComboBox read GetEdit;
  end;

  { TcxColorComboBoxLookupData }
  
  TcxColorComboBoxLookupData = class(TcxComboBoxLookupData)
  protected
    function GetListBoxClass: TcxCustomEditListBoxClass; override;
    function GetItem(Index: Integer): string; override;
    function GetItemCount: Integer; override;
    function InternalChangeCurrentMRUColorPosition(const AColor: TColor): Integer; virtual;
  public
    function GetVisualAreaPreferredSize(AMaxHeight: Integer;
      AWidth: Integer = 0): TSize; override;
    procedure TextChanged; override;
  end;

  TcxColorComboStyle = (cxccsComboList, cxccsComboEdit);
  TcxColorDialogType = (cxcdtDefault, cxcdtCustom);
  TcxSelectCustomColorEvent = procedure(Sender: TObject; var AColor: TColor;
    var AColorDescription: string; var AddToList: Boolean) of object;
  TcxNamingConventionEvent = procedure(Sender: TObject; const AColor: TColor;
    var AColorDescription: string) of object;
  TcxChangeItemIndexEvent = procedure(Sender: TObject; const AIndex: Integer) of object;
  TcxGetDefaultColorEvent = procedure(Sender: TObject; var AColor: TColor) of object;

  { TcxCustomColorComboBoxProperties }
  
  TcxCustomColorComboBoxProperties = class(TcxCustomComboBoxProperties)
  private
    FAllowSelectColor: Boolean;
    FColorComboStyle: TcxColorComboStyle;
    FColorBoxAlign: TcxColorBoxAlign;
    FColorBoxFrameColor: TColor;
    FColorBoxWidth: Integer;
    FColorDialogType: TcxColorDialogType;
    FColorValueFormat: TcxColorValueFormat;    
    FDefaultColor: TColor;
    FDefaultDescription: string;
    FDefaultColorStyle: TcxDefaultColorStyle;

    FItems: TcxColorComboBoxItems;
    FCustomColors: TcxColorComboBoxItems;
    FMRUColors: TcxColorComboBoxItems;

    FMaxMRUColors: Byte;
    FNamingConvention: TcxColorNamingConvention;
    FShowDescriptions: Boolean;
    FOnNamingConvention: TcxNamingConventionEvent;
    FOnSelectCustomColor: TcxSelectCustomColorEvent;
    FOnAddedMRUColor: TNotifyEvent;
    FOnDeletedMRUColor: TNotifyEvent;
    FOnGetDefaultColor: TcxGetDefaultColorEvent;
    FOnLoadColorList: TNotifyEvent;
    FInternalUpdate: Boolean;
    FPrepareList: TcxColorPrepareList;
    function ColorItemByIndex(AIndex: Integer): TcxColorComboBoxItem;
    procedure DeleteOverMRUColors;
    function DoConvertNaming(AIndex: Integer): string;
    function GetItems: TcxColorComboBoxItems;
    procedure InternalGetColorComboBoxDisplayValue(AItemIndex: Integer;
      const AEditValue: TcxEditValue; out AColor: TColor;
      out ADescription: string; out AColorFound: Boolean);
    procedure InternalPrepareColorList(APrepareList: TcxColorPrepareList); overload;
    procedure InternalPrepareColorList(AColorValues: array of TColor;
      AColorNames: array of string); overload;
    function IsDefaultDescriptionStored: Boolean;
    procedure ReadCustomColors(Reader: TReader);
    procedure ReadPrepareInfo(Reader: TReader);
    procedure SetAllowSelectColor(Value: Boolean);
    procedure SetColorBoxAlign(Value: TcxColorBoxAlign);
    procedure SetColorBoxFrameColor(Value: TColor);
    procedure SetColorBoxWidth(Value: Integer);
    procedure SetColorComboStyle(Value: TcxColorComboStyle);
    procedure SetColorValueFormat(Value: TcxColorValueFormat);
    procedure SetDefaultColor(Value: TColor);
    procedure SetDefaultDescription(Value: string);
    procedure SetDefaultColorStyle(Value: TcxDefaultColorStyle);

    procedure SetCustomColors(Value: TcxColorComboBoxItems);
    procedure SetItems(const Value: TcxColorComboBoxItems);
    procedure SetMaxMRUColors(Value: Byte);

    procedure SetNamingConvention(Value: TcxColorNamingConvention);
    procedure SetPrepareList(Value: TcxColorPrepareList);
    procedure SetShowDescriptions(const Value: Boolean);
    procedure SynchronizeCustomColors;
    procedure CustomColorChanged(ASender: TObject);
    procedure ValidateMRUColors;
  protected
    procedure DefineProperties(Filer: TFiler); override;
    function ShowColorBox(AColorFound: Boolean): Boolean;
    class function GetLookupDataClass: TcxInterfacedPersistentClass; override;
    class function GetPopupWindowClass: TcxCustomEditPopupWindowClass; override;
    class function GetViewDataClass: TcxCustomEditViewDataClass; override;
    function HasDisplayValue: Boolean; override;
    function IsEditValueNumeric: Boolean; override;

    function GetColorByIndex(AIndex: Integer): TColor;
    function GetIndexByColor(AColor: TColor): Integer;
    function GetDescriptionByIndex(AIndex: Integer): string;
    function IndexByValue(const AValue: TcxEditValue): Integer;
    function IsDisplayValueNumeric: Boolean; virtual;

    function AddMRUColor(const AColor: TColor): TcxMRUColorAction; virtual;
    function DelMRUColor(const AColor: TColor): TcxMRUColorAction; virtual;
    procedure ClearMRUColors; virtual;
    procedure DoGetDefaultColor(var AColor: TColor); virtual;
    procedure TranslateValues(const AEditValue: TcxEditValue;
      var AColor: TColor; var ADescription: string; ANeedDescription: Boolean = False);
  public
    constructor Create(AOwner: TPersistent); override;
    destructor Destroy; override;
    procedure Assign(Source: TPersistent); override;
    procedure Changed; override;
    function CompareDisplayValues(const AEditValue1, AEditValue2: TcxEditValue): Boolean; override;
    class function GetContainerClass: TcxContainerClass; override;
    class function GetViewInfoClass: TcxContainerViewInfoClass; override;
    function GetEditValueSource(AEditFocused: Boolean): TcxDataEditValueSource; override;
    function IsDisplayValueValid(var DisplayValue: TcxEditValue; AEditFocused: Boolean): Boolean; override;
    procedure PrepareDisplayValue(const AEditValue: TcxEditValue;
      var DisplayValue: TcxEditValue; AEditFocused: Boolean); override;
    function GetSupportedOperations: TcxEditSupportedOperations; override;
    function GetDisplayText(const AEditValue: TcxEditValue; AFullText:
      Boolean = False; AIsInplace: Boolean = True): WideString; override;
    procedure Update(AProperties: TcxCustomEditProperties); override;
    procedure GetColorComboBoxDisplayValue(const AEditValue: TcxEditValue;
      out AColor: TColor; out ADescription: string; out AColorFound: Boolean);

    procedure PrepareColorList(APrepareList: TcxColorPrepareList; ASaveCustom, ASaveMRU: Boolean);
    procedure PrepareDelphiColorList(const ASaveCustom, ASaveMRU: Boolean);
    procedure PrepareHTML4ColorList(const ASaveCustom, ASaveMRU: Boolean);
    procedure PrepareX11ColorList(const ASaveCustom, ASaveMRU: Boolean);
    procedure PrepareX11OrderedColorList(const ASaveCustom, ASaveMRU: Boolean);
    // !!!
    property AllowSelectColor: Boolean read FAllowSelectColor
      write SetAllowSelectColor default False;
    property ColorBoxAlign: TcxColorBoxAlign read FColorBoxAlign
      write SetColorBoxAlign default cbaLeft;
    property ColorBoxFrameColor: TColor read FColorBoxFrameColor
      write SetColorBoxFrameColor default clBtnShadow;
    property ColorBoxWidth: Integer read FColorBoxWidth write SetColorBoxWidth
      default 30;
    property ColorComboStyle: TcxColorComboStyle read FColorComboStyle
      write SetColorComboStyle default cxccsComboEdit;
    property ColorDialogType: TcxColorDialogType read FColorDialogType
      write FColorDialogType default cxcdtDefault;
    property ColorValueFormat: TcxColorValueFormat read FColorValueFormat
      write SetColorValueFormat default cxcvRGB;
    property DefaultColor: TColor read FDefaultColor write SetDefaultColor
      default clWindow;
    property DefaultColorStyle: TcxDefaultColorStyle read FDefaultColorStyle
      write SetDefaultColorStyle default cxdcColor;
    property DefaultDescription: string read FDefaultDescription
      write SetDefaultDescription stored IsDefaultDescriptionStored;

    property CustomColors: TcxColorComboBoxItems read FCustomColors write SetCustomColors;
    property Items: TcxColorComboBoxItems read GetItems write SetItems;
    property MaxMRUColors: Byte read FMaxMRUColors write SetMaxMRUColors
      default 10;
    property MRUColors: TcxColorComboBoxItems read FMRUColors write FMRUColors;
    
    property NamingConvention: TcxColorNamingConvention read FNamingConvention
      write SetNamingConvention default cxncDelphi;
    property PrepareList: TcxColorPrepareList read FPrepareList
      write SetPrepareList default cxplDelphi;
    property ShowDescriptions: Boolean read FShowDescriptions
      write SetShowDescriptions default True;
    property OnAddedMRUColor: TNotifyEvent read FOnAddedMRUColor
      write FOnAddedMRUColor;
    property OnDeletedMRUColor: TNotifyEvent read FOnDeletedMRUColor
      write FOnDeletedMRUColor;
    property OnGetDefaultColor: TcxGetDefaultColorEvent read FOnGetDefaultColor
      write FOnGetDefaultColor;
    property OnNamingConvention: TcxNamingConventionEvent
      read FOnNamingConvention write FOnNamingConvention;
    property OnSelectCustomColor: TcxSelectCustomColorEvent
      read FOnSelectCustomColor write FOnSelectCustomColor;
  end;

  { TcxColorComboBoxProperties }

  TcxColorComboBoxProperties = class(TcxCustomColorComboBoxProperties)
  published
    property Alignment;
    property AllowSelectColor;
    property AssignedValues;
    property BeepOnError;
    property ButtonGlyph;
    property CharCase;
    property ClearKey;
    property ColorBoxAlign;
    property ColorBoxFrameColor;
    property ColorBoxWidth;
    property ColorComboStyle;
    property ColorDialogType;
    property ColorValueFormat;
    property CustomColors;
    property DefaultColor;
    property DefaultColorStyle;
    property DefaultDescription;
    property DropDownAutoWidth;
    property DropDownRows;
    property DropDownSizeable;
    property DropDownWidth;
    property ImeMode;
    property ImeName;
    property ImmediateDropDown;
    property ImmediatePost;
    property ImmediateUpdateText;
    property MaxMRUColors;
    property NamingConvention;
    property OEMConvert;
    property PopupAlignment;
    property PostPopupValueOnTab;
    property PrepareList;
    property ReadOnly;
    property Revertable;
    property ShowDescriptions;
    property ValidateOnEnter;

⌨️ 快捷键说明

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