📄 jvlistbox.pas
字号:
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: JvListbox2.PAS, released on 2001-02-28.
The Initial Developer of the Original Code is S閎astien Buysse [sbuysse att buypin dott com]
Portions created by S閎astien Buysse are Copyright (C) 2001 S閎astien Buysse.
All Rights Reserved.
This unit is a merging of the original TJvListBox, JvListBox2, TJvExListBox.
Merging done 2002-06-15 by Peter Thornqvist [peter3 at sourceforge dot net]
Contributor(s):
Michael Beck [mbeck att bigfoot dott com].
Petr Vones (petr dott v att mujmail dott cz)
Peter Below <100113 dott 1101 att compuserve dott com>
MERGE NOTES:
* The Alignment property might mess things up depending on other property settings
* not very extensively tested
* TJvListBox in JvCtrls inherits from TJvCustomListbox in this unit.
Maybe TJvListBox should be moved here instead (or this code into JvCtrls)?
* TJvPlaylist now inherits from JvListBox
You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.sourceforge.net
Known Issues:
Merge notes (2002-03-21):
* (p3) merged JvMultilineListBox, JvReorderListBox, JvTextListBox, JvBMPListBox
Notes (2003-05-21) // Remko Bonte
* Removed OwnerData
* Some bug-fixes. Combinations of Multiline, Alignment, Scrollbars seem to work now.
* Did some rewrite of background-drawing. Most of it seems to work, but a lot
of flickering, best avoid it or set ScrollBars to ssNone.
* Updated drag image to use with MultiLine.
-----------------------------------------------------------------------------}
// $Id: JvListBox.pas,v 1.46 2005/02/19 14:05:52 peter3 Exp $
unit JvListBox;
{$I jvcl.inc}
{$I vclonly.inc}
interface
uses
{$IFDEF UNITVERSIONING}
JclUnitVersioning,
{$ENDIF UNITVERSIONING}
Windows, Messages, SysUtils, Classes, Graphics, StdCtrls, Controls, Forms,
JvItemsSearchs, JvDataProvider, JvDataProviderIntf, JvExStdCtrls;
type
TJvListboxFillMode = (bfmTile, bfmStretch);
TJvListBoxDataEvent = procedure(Sender: TWinControl; Index: Integer; var Text: string) of object;
TJvListboxChange = procedure(Sender: TObject; Item: string) of object;
TJvScrollEvent = procedure(Sender: TObject; const Msg: TWMScroll; var DontScroll: Boolean) of object;
TJvListBoxBackground = class(TPersistent)
private
FOnChange: TNotifyEvent;
FImage: TBitmap;
FFillMode: TJvListboxFillMode;
FVisible: Boolean;
procedure SetFillMode(const Value: TJvListboxFillMode);
procedure SetImage(const Value: TBitmap);
procedure SetVisible(const Value: Boolean);
function GetDoDraw: Boolean;
protected
procedure Change;
public
constructor Create;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
published
property DoDraw: Boolean read GetDoDraw;
property Image: TBitmap read FImage write SetImage;
property FillMode: TJvListboxFillMode read FFillMode write SetFillMode;
property Visible: Boolean read FVisible write SetVisible;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
end;
TJvCustomListBox = class;
{ This class will be used for the Items property of the list box.
If a provider is active at the list box, this list will keep the strings stored in an internal
list.
Whenever an item is added to the list the provider will be deactivated and the list will be
handled by the list box as usual. }
TJvListBoxStrings = class(TStrings)
private
FListBox: TJvCustomListBox;
FInternalList: TStringList;
FUseInternal: Boolean;
FUpdating: Boolean;
FDestroyCnt: Integer;
function GetInternalList: TStrings;
public
function Get(Index: Integer): string; override;
function GetCount: Integer; override;
function GetObject(Index: Integer): TObject; override;
procedure Put(Index: Integer; const S: string); override;
procedure PutObject(Index: Integer; AObject: TObject); override;
procedure SetUpdateState(Updating: Boolean); override;
procedure SetWndDestroying(Destroying: Boolean);
function GetListBox: TJvCustomListBox;
procedure SetListBox(Value: TJvCustomListBox);
property ListBox: TJvCustomListBox read GetListBox write SetListBox;
property InternalList: TStrings read GetInternalList;
property UseInternal: Boolean read FUseInternal write FUseInternal;
property Updating: Boolean read FUpdating;
property DestroyCount: Integer read FDestroyCnt;
public
constructor Create;
destructor Destroy; override;
function Add(const S: string): Integer; override;
procedure Clear; override;
procedure Delete(Index: Integer); override;
function IndexOf(const S: string): Integer; override;
procedure Insert(Index: Integer; const S: string); override;
procedure Move(CurIndex, NewIndex: Integer); override;
procedure MakeListInternal; virtual;
procedure ActivateInternal; virtual;
end;
TJvListBoxStringsClass = class of TJvListBoxStrings;
TJvCustomListBox = class(TJvExCustomListBox)
private
FHotTrack: Boolean;
FAlignment: TAlignment;
FMaxWidth: Integer;
FScrollBars: TScrollStyle;
FSorted: Boolean;
FOnGetText: TJvListBoxDataEvent;
FOnSelectCancel: TNotifyEvent;
FOnDeleteString: TJvListboxChange;
FOnAddString: TJvListboxChange;
FOnChange: TNotifyEvent;
FOnHorizontalScroll: TJvScrollEvent;
FOnVerticalScroll: TJvScrollEvent;
FDragIndex: Integer;
FDragImage: TDragImageList;
FMultiline: Boolean;
FShowFocusRect: Boolean;
FSelectedTextColor: TColor;
FSelectedColor: TColor;
FDisabledTextColor: TColor;
FBackground: TJvListBoxBackground;
FLeftPosition: Integer;
FConsumerSvc: TJvDataConsumer;
FConsumerStrings: TJvConsumerStrings;
FProviderIsActive: Boolean;
FProviderToggle: Boolean;
procedure WMVScroll(var Msg: TWMVScroll); message WM_VSCROLL;
procedure WMHScroll(var Msg: TWMHScroll); message WM_HSCROLL;
procedure CNKeyDown(var Msg: TWMKeyDown); message CN_KEYDOWN;
procedure DrawBackGround(ADC: HDC; const DoOffSet: Boolean);
procedure UpdateStyle;
{ Handle messages that insert or delete strings from the listbox to
manage the horizontal scrollbar if FMutliline is false. }
procedure LBAddString(var Msg: TMessage); message LB_ADDSTRING;
procedure LBInsertString(var Msg: TMessage); message LB_INSERTSTRING;
procedure LBDeleteString(var Msg: TMessage); message LB_DELETESTRING;
{ Override CN_DRAWITEM handling to be able to switch off focus rect. }
procedure CNDrawItem(var Msg: TWMDrawItem); message CN_DRAWITEM;
procedure SetAlignment(const Value: TAlignment);
procedure SetMultiline(const Value: Boolean);
procedure SetSelectedColor(const Value: TColor);
procedure SetSelectedTextColor(const Value: TColor);
procedure SetShowFocusRect(const Value: Boolean);
procedure SetDisabledTextColor(const Value: TColor);
procedure SetMaxWidth(const Value: Integer);
procedure SetScrollBars(const Value: TScrollStyle);
procedure SetSorted(const Value: Boolean);
procedure SetHotTrack(const Value: Boolean);
procedure SetBackground(const Value: TJvListBoxBackground);
function GetLimitToClientWidth: Boolean;
protected
procedure FontChanged; override;
function GetItemsClass: TJvListBoxStringsClass; virtual;
procedure BeginRedraw;
procedure EndRedraw;
procedure SetConsumerService(Value: TJvDataConsumer);
procedure ConsumerServiceChanging(Sender: TJvDataConsumer; Reason: TJvDataConsumerChangeReason);
procedure ConsumerServiceChanged(Sender: TJvDataConsumer; Reason: TJvDataConsumerChangeReason);
procedure ConsumerSubServiceCreated(Sender: TJvDataConsumer;
SubSvc: TJvDataConsumerAggregatedObject);
function IsProviderSelected: Boolean;
function IsProviderToggle: Boolean;
procedure DeselectProvider;
procedure UpdateItemCount;
property Provider: TJvDataConsumer read FConsumerSvc write SetConsumerService;
property ConsumerStrings: TJvConsumerStrings read FConsumerStrings;
procedure LBFindString(var Msg: TMessage); message LB_FINDSTRING;
procedure LBFindStringExact(var Msg: TMessage); message LB_FINDSTRINGEXACT;
procedure LBSelectString(var Msg: TMessage); message LB_SELECTSTRING;
procedure LBGetText(var Msg: TMessage); message LB_GETTEXT;
procedure LBGetTextLen(var Msg: TMessage); message LB_GETTEXTLEN;
procedure DoStartDrag(var DragObject: TDragObject); override;
procedure DragOver(Source: TObject; X, Y: Integer; State: TDragState;
var Accept: Boolean); override;
function DoEraseBackground(Canvas: TCanvas; Param: Integer): Boolean; override;
procedure MouseEnter(Control: TControl); override;
procedure MouseLeave(Control: TControl); override;
procedure SelectCancel(var Msg: TMessage); message LBN_SELCANCEL;
procedure Changed; virtual;
procedure DrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState); override;
procedure MeasureItem(Index: Integer; var Height: Integer); override;
procedure RemeasureAll;
procedure DoBackgroundChange(Sender: TObject);
procedure Loaded; override;
procedure DrawProviderItem(Canvas: TCanvas; Rect: TRect; Index: Integer;
State: TOwnerDrawState);
procedure DoGetText(Index: Integer; var AText: string); virtual;
property LimitToClientWidth: Boolean read GetLimitToClientWidth;
property MaxWidth: Integer read FMaxWidth write SetMaxWidth;
property ScrollBars: TScrollStyle read FScrollBars write SetScrollBars default ssBoth;
property Sorted: Boolean read FSorted write SetSorted default False;
property OnGetText: TJvListBoxDataEvent read FOnGetText write FOnGetText;
property Alignment: TAlignment read FAlignment write SetAlignment
default taLeftJustify;
property HotTrack: Boolean read FHotTrack write SetHotTrack default False;
property OnSelectCancel: TNotifyEvent read FOnSelectCancel write FOnSelectCancel;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
property OnDeleteString: TJvListboxChange read FOnDeleteString write FOnDeleteString;
property OnAddString: TJvListboxChange read FOnAddString write FOnAddString;
property OnVerticalScroll: TJvScrollEvent read FOnVerticalScroll write FOnVerticalScroll;
property OnHorizontalScroll: TJvScrollEvent read FOnHorizontalScroll write FOnHorizontalScroll;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function ItemRect(Index: Integer): TRect;
function ItemsShowing: TStrings; virtual;
procedure MeasureProviderItem(Index, WidthAvail: Integer; var ASize: TSize);
procedure MeasureString(const S: string; WidthAvail: Integer; var ASize: TSize);
procedure DefaultDrawItem(Index: Integer; ARect: TRect;
State: TOwnerDrawState); virtual;
procedure DefaultDragOver(Source: TObject; X, Y: Integer; State: TDragState;
var Accept: Boolean); virtual;
procedure DefaultStartDrag(var DragObject: TDragObject); virtual;
procedure DefaultDragDrop(Source: TObject; X, Y: Integer); virtual;
procedure CreateDragImage(const S: string);
procedure DragDrop(Source: TObject; X, Y: Integer); override;
function GetDragImages: TDragImageList; override;
property DragIndex: Integer read FDragIndex;
property DragImages: TDragImageList read GetDragImages;
procedure WndProc(var Msg: TMessage); override;
procedure CreateParams(var Params: TCreateParams); override;
procedure CreateWnd; override;
procedure DestroyWnd; override;
procedure UpdateHorizontalExtent;
function SearchExactString(const Value: string; CaseSensitive: Boolean = True;
StartIndex: Integer = -1): Integer;
function SearchPrefix(const Value: string; CaseSensitive: Boolean = True;
StartIndex: Integer = -1): Integer;
function SearchSubString(const Value: string; CaseSensitive: Boolean = True;
StartIndex: Integer = -1): Integer;
function DeleteExactString(const Value: string; All: Boolean;
CaseSensitive: Boolean = True): Integer;
procedure SelectAll; {$IFDEF COMPILER6_UP} override; {$ENDIF}
procedure UnselectAll;
procedure InvertSelection;
procedure MoveSelectedUp; virtual;
procedure MoveSelectedDown; virtual;
procedure DeleteSelected; {$IFDEF COMPILER6_UP} override; {$ENDIF}
procedure DeleteAllButSelected;
procedure SetBounds(ALeft: Integer; ATop: Integer; AWidth: Integer;
AHeight: Integer); override;
protected
property MultiLine: Boolean read FMultiline write SetMultiline default False;
property SelectedColor: TColor read FSelectedColor write SetSelectedColor default clHighlight;
property SelectedTextColor: TColor read FSelectedTextColor write SetSelectedTextColor default clHighlightText;
property DisabledTextColor: TColor read FDisabledTextColor write SetDisabledTextColor default clGrayText;
property ShowFocusRect: Boolean read FShowFocusRect write SetShowFocusRect default True;
property Background: TJvListBoxBackground read FBackground write SetBackground;
end;
TJvListBox = class(TJvCustomListBox)
public
{$IFDEF COMPILER6_UP}
property Count;
{$ENDIF COMPILER6_UP}
published
property Align;
property Anchors;
property BiDiMode;
property BorderStyle;
property Color;
property Columns;
property Constraints;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property ExtendedSelect;
property Font;
property ImeMode;
property ImeName;
property IntegralHeight;
property ItemHeight;
property Items;
property MultiLine;
property SelectedColor;
property SelectedTextColor;
property DisabledTextColor;
property ShowFocusRect;
property Background;
property MultiSelect;
property ParentBiDiMode;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property Provider;
property ScrollBars;
property ShowHint;
property Sorted;
property Style;
property TabOrder;
property TabStop;
property TabWidth;
property Visible;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnDrawItem;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnGetText;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMeasureItem;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
property Alignment;
property HotTrack;
property HintColor;
property OnMouseEnter;
property OnMouseLeave;
property OnParentColorChange;
property OnSelectCancel;
property OnChange;
property OnDeleteString;
property OnAddString;
property OnVerticalScroll;
property OnHorizontalScroll;
end;
{$IFDEF UNITVERSIONING}
const
UnitVersioning: TUnitVersionInfo = (
RCSfile: '$RCSfile: JvListBox.pas,v $';
Revision: '$Revision: 1.46 $';
Date: '$Date: 2005/02/19 14:05:52 $';
LogPath: 'JVCL\run'
);
{$ENDIF UNITVERSIONING}
implementation
uses
Consts, TypInfo,
{$IFDEF HAS_UNIT_RTLCONSTS}
RTLConsts,
{$ENDIF HAS_UNIT_RTLCONSTS}
JclBase,
JvJVCLUtils, JvConsts, JvCtrls, JvResources;
const
AlignFlags: array [TAlignment] of DWORD = (DT_LEFT, DT_RIGHT, DT_CENTER);
type
PStrings = ^TStrings;
//=== { TJvListBoxStrings } ==================================================
constructor TJvListBoxStrings.Create;
begin
inherited Create;
FInternalList := TStringList.Create;
end;
destructor TJvListBoxStrings.Destroy;
begin
FreeAndNil(FInternalList);
inherited Destroy;
end;
function TJvListBoxStrings.Get(Index: Integer): string;
var
Len: Integer;
begin
if UseInternal then
Result := InternalList[Index]
else
{$IFDEF COMPILER6_UP}
if ListBox.Style in [lbVirtual, lbVirtualOwnerDraw] then
Result := ListBox.DoGetData(Index)
else
{$ENDIF COMPILER6_UP}
begin
Len := SendMessage(ListBox.Handle, LB_GETTEXTLEN, Index, 0);
if Len = LB_ERR then
Error(SListIndexError, Index);
SetLength(Result, Len);
if Len <> 0 then
begin
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -