cxfiltercontrol.pas

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

PAS
2,067
字号
{********************************************************************}
{                                                                    }
{       Developer Express Visual Component Library                   }
{       ExpressFilterControl                                         }
{                                                                    }
{       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 cxFilterControl;

{$I cxVer.inc}

interface

uses
  Windows, Messages,
{$IFDEF DELPHI6}
  Types,
  Variants,
{$ENDIF}
  Classes, SysUtils, Controls, Forms, Graphics, cxFilter, cxEdit,
  cxClasses, cxContainer, cxControls, cxGraphics, cxLookAndFeels,
  cxLookAndFeelPainters, cxDropDownEdit, Menus, cxTextEdit, StdCtrls,
  cxFilterControlUtils, cxDataStorage, cxFormats;

const
  cxFilterControlMaxDropDownRows = 12;

type
  IcxFilterControl = interface
  ['{B9890E09-5400-428D-8F72-1FF8FD15937C}']
    function GetCaption(Index: Integer): string;
    function GetCount: Integer;
    function GetCriteria: TcxFilterCriteria;
    function GetItemLink(Index: Integer): TObject;
    function GetItemLinkID(Index: Integer): Integer;
    function GetItemLinkName(Index: Integer): string;
    function GetFieldName(Index: Integer): string;
    function GetProperties(Index: Integer): TcxCustomEditProperties;
    function GetValueType(Index: Integer): TcxValueTypeClass;

    property Captions[Index: Integer]: string read GetCaption;
    property Count: Integer read GetCount;
    property Criteria: TcxFilterCriteria read GetCriteria;
    property ItemLinkNames[Index: Integer]: string read GetItemLinkName;
    property ItemLinkIDs[Index: Integer]: Integer read GetItemLinkID;
    property ItemLinks[Index: Integer]: TObject read GetItemLink;
    property FieldNames[Index: Integer]: string read GetFieldName;
    property Properties[Index: Integer]: TcxCustomEditProperties read GetProperties;
    property ValueTypes[Index: Integer]: TcxValueTypeClass read GetValueType;
  end;

  IcxFilterControlDialog = interface
  ['{D2369F8D-3B22-41A8-881E-B01BEB624B7D}']
    procedure SetDialogLinkComponent(ALink: TComponent);
  end;

  TcxFilterControlCriteria = class;
  TcxCustomFilterControl = class;
  TcxCustomFilterControlClass = class of TcxCustomFilterControl;
  TcxFilterControlViewInfo = class;
  TcxFilterControlViewInfoClass = class of TcxFilterControlViewInfo;

  { TcxFilterControlCriteriaItem }

  TcxFilterControlCriteriaItem = class(TcxFilterCriteriaItem)
  private
    function GetFilterControlCriteria: TcxFilterControlCriteria;
    function GetFilter: IcxFilterControl;
    function GetItemIndex: Integer;
    function ValidItem: Boolean;
  protected
    function GetDataValue(AData: TObject): Variant; override;
    function GetFieldCaption: string; override;
    function GetFieldName: string; override;
    function GetFilterOperatorClass: TcxFilterOperatorClass; override;
    property ItemIndex: Integer read GetItemIndex;
  public
    property Filter: IcxFilterControl read GetFilter;
    property Criteria: TcxFilterControlCriteria read GetFilterControlCriteria;
  end;

  { TcxFilterControlCriteria }

  TcxFilterControlCriteria = class(TcxFilterCriteria)
  private
    FControl: TcxCustomFilterControl;
  protected
    function GetIDByItemLink(AItemLink: TObject): Integer; override;
    function GetItemClass: TcxFilterCriteriaItemClass; override;
    function GetItemLinkByID(AID: Integer): TObject; override;
    //ver. 3
    function GetNameByItemLink(AItemLink: TObject): string; override;
    function GetItemLinkByName(const AName: string): TObject; override;

    property Control: TcxCustomFilterControl read FControl;
  public
    constructor Create(AOwner: TcxCustomFilterControl); virtual;
    procedure AssignEvents(Source: TPersistent); override;
  end;

  TcxFilterControlCriteriaClass = class of TcxFilterControlCriteria;

  TcxCustomRowViewInfo = class;
  TcxGroupViewInfo = class;
  TcxConditionViewInfo = class;

  TcxFilterControlHitTest = (fhtNone, fhtButton, fhtBoolOperator, fhtItem,
    fhtOperator, fhtValue, fhtAddCondition, fhtAddValue);

  TcxFilterControlHitTestInfo = record
    HitTest: TcxFilterControlHitTest;
    Mouse: TPoint;
    Shift: TShiftState;
    Row: TcxCustomRowViewInfo;
    ValueIndex: Integer;
  end;

  { TcxCustomRowViewInfo }

  TcxCustomRowViewInfo = class
  private
    FButtonText: string;
    FButtonRect: TRect;
    FButtonState: TcxButtonState;
    FControl: TcxCustomFilterControl;
    FCriteriaItem: TcxCustomFilterCriteriaItem;
    FLevel: Integer;
    FIndent: Integer;
    FParent: TcxCustomRowViewInfo;
    FRowRect: TRect;
    function GetCondition: TcxConditionViewInfo;
    function GetFocused: Boolean;
    function GetGroup: TcxGroupViewInfo;
    procedure GetInternal;
  protected
    function GetWidth: Integer; virtual;
    function IsLast: Boolean;
  public
    constructor Create(AControl: TcxCustomFilterControl;
      AParent: TcxCustomRowViewInfo;
      ACriteriaItem: TcxCustomFilterCriteriaItem); virtual;
    destructor Destroy; override;
    procedure Calc(const ARowRect: TRect); virtual;
    procedure GetHitTestInfo(const P: TPoint; var HitInfo: TcxFilterControlHitTestInfo); virtual;
    function Ready: Boolean; virtual;

    property ButtonRect: TRect read FButtonRect write FButtonRect;
    property ButtonState: TcxButtonState read FButtonState write FButtonState;
    property ButtonText: string read FButtonText write FButtonText;
    property Condition: TcxConditionViewInfo read GetCondition;
    property Control: TcxCustomFilterControl read FControl;
    property CriteriaItem: TcxCustomFilterCriteriaItem read FCriteriaItem;
    property Focused: Boolean read GetFocused;
    property Group: TcxGroupViewInfo read GetGroup;
    property Indent: Integer read FIndent;
    property Level: Integer read FLevel;
    property Parent: TcxCustomRowViewInfo read FParent;
    property RowRect: TRect read FRowRect write FRowRect;
    property Width: Integer read GetWidth;
  end;

  { TcxGroupViewInfo }

  TcxGroupViewInfo = class(TcxCustomRowViewInfo)
  private
    FBoolOperator: TcxFilterBoolOperatorKind;
    FBoolOperatorText: string;
    FBoolOperatorRect: TRect;
    FCaption: string;
    FCaptionRect: TRect;
    FRows: TList;
    function GetRow(Index: Integer): TcxCustomRowViewInfo;
    function GetRowCount: Integer;
    procedure SetRow(Index: Integer; const Value: TcxCustomRowViewInfo);
  protected
    function GetWidth: Integer; override;
  public
    constructor Create(AControl: TcxCustomFilterControl;
      AParent: TcxCustomRowViewInfo;
      ACriteriaItem: TcxCustomFilterCriteriaItem); override;
    destructor Destroy; override;
    procedure Add(ARow: TcxCustomRowViewInfo);
    procedure Remove(ARow: TcxCustomRowViewInfo);
    procedure Calc(const ARowRect: TRect); override;
    procedure GetHitTestInfo(const P: TPoint; var HitInfo: TcxFilterControlHitTestInfo); override;
    property BoolOperator: TcxFilterBoolOperatorKind read FBoolOperator write FBoolOperator;
    property BoolOperatorText: string read FBoolOperatorText write FBoolOperatorText;
    property BoolOperatorRect: TRect read FBoolOperatorRect;
    property Caption: string read FCaption write FCaption;
    property CaptionRect: TRect read FCaptionRect;
    property RowCount: Integer read GetRowCount;
    property Rows[Index: Integer]: TcxCustomRowViewInfo read GetRow write SetRow;
  end;

  { TcxValuesViewInfo }

  TcxValueInfo = class
  private
    FValue: TcxEditValue;
    FValueText: TCaption;
    FValueRect: TRect;
    FValueViewInfo: TcxCustomEditViewInfo;
    procedure SetValueViewInfo(const Value: TcxCustomEditViewInfo);
  public
    constructor Create;
    destructor Destroy; override;
    property Value: TcxEditValue read FValue write FValue;
    property ValueText: TCaption read FValueText write FValueText;
    property ValueRect: TRect read FValueRect write FValueRect;
    property ValueViewInfo: TcxCustomEditViewInfo
      read FValueViewInfo write SetValueViewInfo;
  end;

  { TcxValuesViewInfo }

  TcxValuesViewInfo = class
  private
    FAddButtonRect: TRect;
    FAddButtonState: TcxButtonState;
    FCondition: TcxConditionViewInfo;
    FList: TList;
    FSeparator: string;
    function GetControl: TcxCustomFilterControl;
    function GetValue(Index: Integer): TcxValueInfo;
    function GetWidth: Integer;
  protected
    function GetCount: Integer;
    procedure UpdateEditorStyle(AStyle: TcxCustomEditStyle; 
      AHightlited, AEnabled: Boolean);
  public
    constructor Create(ACondition: TcxConditionViewInfo);
    destructor Destroy; override;
    procedure AddValue;
    procedure Calc;
    procedure Clear;
    procedure GetHitTestInfo(const P: TPoint; var HitInfo: TcxFilterControlHitTestInfo); virtual;
    procedure RemoveValue(AIndex: Integer);
    property AddButtonRect: TRect read FAddButtonRect;
    property AddButtonState: TcxButtonState read FAddButtonState;
    property Condition: TcxConditionViewInfo read FCondition;
    property Control: TcxCustomFilterControl read GetControl;
    property Count: Integer read GetCount;
    property Separator: string read FSeparator;
    property Values[Index: Integer]: TcxValueInfo read GetValue; default;
    property Width: Integer read GetWidth;
  end;

  { TcxConditionViewInfo }

  TcxConditionViewInfo = class(TcxCustomRowViewInfo)
  private
    FOperator: TcxFilterControlOperator;
    FOperatorRect: TRect;
    FOperatorText: string;
    FItemIndex: Integer;
    FItemLink: TObject;
    FItemRect: TRect;
    FItemText: string;
    FEditorHelper: TcxCustomFilterEditHelperClass;
    FEditorProperties: TcxCustomEditProperties;
    FSupportedOperators: TcxFilterControlOperators;
    FValueType: TcxValueTypeClass;
    FValues: TcxValuesViewInfo;
  private
    function GetItemIndex: Integer;
  protected
    ValueEditorData: TcxCustomEditData;
    procedure AddValue;
    function GetWidth: Integer; override;
    function HasDisplayValues: Boolean; virtual;
    procedure InitValues(ASaveValue: Boolean);
    procedure InternalInit; virtual;
    procedure SetItem(AIndex: Integer);
    procedure ValidateConditions;
  public
    constructor Create(AControl: TcxCustomFilterControl;
      AParent: TcxCustomRowViewInfo;
      ACriteriaItem: TcxCustomFilterCriteriaItem); override;
    destructor Destroy; override;
    procedure Calc(const ARowRect: TRect); override;
    procedure GetHitTestInfo(const P: TPoint; var HitInfo: TcxFilterControlHitTestInfo); override;
    function GetProperties: TcxCustomEditProperties;
    function Ready: Boolean; override;
    property EditorHelper: TcxCustomFilterEditHelperClass read FEditorHelper;
    property EditorProperties: TcxCustomEditProperties read FEditorProperties;
    property ItemLink: TObject read FItemLink;
    property ItemIndex: Integer read FItemIndex;
    property ItemRect: TRect read FItemRect;
    property ItemText: string read FItemText write FItemText;
    property Operator: TcxFilterControlOperator read FOperator write FOperator;
    property OperatorRect: TRect read FOperatorRect;
    property OperatorText: string read FOperatorText write FOperatorText;
    property SupportedOperators: TcxFilterControlOperators read FSupportedOperators;
    property ValueType: TcxValueTypeClass read FValueType;
    property Values: TcxValuesViewInfo read FValues;
  end;

  { TcxCustomFilterControl }

  TFilterControlState = (fcsNormal, fcsSelectingAction, fcsSelectingItem,
    fcsSelectingBoolOperator, fcsSelectingCondition, fcsSelectingValue);

  TcxFilterControlFont = (fcfBoolOperator, fcfItem, fcfCondition, fcfValue);
  TcxActivateValueEditKind = (aveEnter, aveKey, aveMouse);

  TcxCustomFilterControl = class(TcxControl, IcxMouseTrackingCaller,
    IcxFormatControllerListener, IdxSkinSupport)
  private
    FActionMenu: TPopupMenu;
    FComboBox: TcxComboBox;
    FCriteria: TcxFilterControlCriteria;
    FFocusedInfo: TcxFilterControlHitTestInfo;
    FFonts: array[TcxFilterControlFont] of TFont;
    FHotTrack: TcxFilterControlHitTestInfo;
    FInplaceEditors: TcxInplaceEditList;
    FLeftOffset: Integer;
    FLockCount: Integer;
    FRoot: TcxCustomRowViewInfo;
    FRows: TList;
    FSortItems: Boolean;
    FState: TFilterControlState;
    FTextEditProperties: TcxTextEditProperties;
    FTopVisibleRow: Integer;

    FValueEditor: TcxCustomEdit;
    FValueEditorStyle: TcxCustomEditStyle;

    FViewInfo: TcxFilterControlViewInfo;
    FHotTrackOnUnfocused: Boolean;
    FNullString: string;
    FShowLevelLines: Boolean;
    FWantTabs: Boolean;
    FWasError: Boolean;
    FOnApplyFilter: TNotifyEvent;
    procedure CreateFonts;
    procedure DoFontChanged(Sender: TObject);
    function GetFont(Index: Integer): TFont;
    function IsFontStored(Index: Integer): Boolean;
    procedure SetFont(Index: Integer; const Value: TFont);

    function FocusedRowIndex: Integer;
    function GetRow(Index: Integer): TcxCustomRowViewInfo;
    function GetRowCount: Integer;
    function GetFocusedRow: TcxCustomRowViewInfo;

    procedure ComboBoxCloseUp(Sender: TObject);
    procedure ComboBoxExit(Sender: TObject);
    procedure ComboBoxKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure ComboBoxKeyPress(Sender: TObject; var Key: Char);
    procedure ComboBoxPopup(Sender: TObject);
    procedure DoComboDropDown(const R: TRect; const AText: string);
    procedure SetFocusedRow(ARow: TcxCustomRowViewInfo);
    procedure PopupMenuClick(Sender: TObject);
    function IsNullStringStored: Boolean;
    procedure ProcessHitTest(AHitTest: TcxFilterControlHitTest; AByMouse: Boolean);
    procedure ReadData(AStream: TStream);
    procedure RecalcRows;
    procedure RefreshMenuCaptions;
    procedure SetLeftOffset(Value: Integer);
    procedure SetNullString(const Value: string);
    procedure SetTopVisibleRow(Value: Integer);
    procedure SetShowLevelLines(const Value: Boolean);
    procedure SetWantTabs(const Value: Boolean);
    procedure ValueEditorInit;
    // value editor events
    procedure ValueEditorAfterKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure ValueEditorExit(Sender: TObject);
    procedure ValueEditorKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);

    procedure WriteData(AStream: TStream);
  protected
    procedure CheckInplaceControlsColor; virtual;
    // overrided VCL
    procedure DefineProperties(Filer: TFiler); override;
    procedure KeyDown(var Key: Word; Shift: TShiftState); override;
    procedure KeyPress(var Key: Char); override;
    procedure Loaded; override;
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
    procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
    procedure Paint; override;
    procedure SetEnabled(Value: Boolean); override;
    procedure SetParent(AParent: TWinControl); override;
    // overrided cxControl
    procedure BoundsChanged; override;
    procedure DoLayoutChange; virtual;
    procedure FocusChanged; override;
    procedure FontChanged; override;
    function GetBorderSize: Integer; override;

⌨️ 快捷键说明

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