📄 jvcombolistbox.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: JvComboListBox.PAS, released on 2003-10-07.
The Initial Developer of the Original Code is Peter Thornqvist <peter3 at sourceforge.net>
Portions created by S閎astien Buysse are Copyright (C) 2003 Peter Thornqvist .
All Rights Reserved.
Contributor(s):
dejoy(dejoy att ynl dott gov dott cn)
tsoyran(tsoyran att otenet dott gr), Jan Verhoeven, Kyriakos Tasos,
Andreas Hausladen <ahuser at users dot sourceforge dot net>.
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:
Description:
A listbox that displays a combo box overlay on the selected item. Assign a
TPopupMenu to the DropdownMenu property and it will be shown when the user clicks the
combobox button.
History:
2004-07-23: Added TJvCheckedComboBox.
-----------------------------------------------------------------------------}
// $Id: JvComboListBox.pas,v 1.35 2005/02/17 10:20:02 marquardt Exp $
unit JvComboListBox;
{$I jvcl.inc}
interface
uses
{$IFDEF UNITVERSIONING}
JclUnitVersioning,
{$ENDIF UNITVERSIONING}
Windows, Messages,
Classes, Graphics, Controls, Forms, StdCtrls, Buttons,
{$IFDEF VCL}
JvListBox,
{$ENDIF VCL}
{$IFDEF VisualCLX}
Qt, JvQExStdCtrls,
{$ENDIF VisualCLX}
Menus;
type
// (p3) these types should *not* be moved to JvTypes (they are only used here)!
TJvComboListBoxDrawStyle = (dsOriginal, dsStretch, dsProportional);
TJvComboListDropDownEvent = procedure(Sender: TObject; Index: Integer;
X, Y: Integer; var AllowDrop: Boolean) of object;
TJvComboListDrawTextEvent = procedure(Sender: TObject; Index: Integer;
const AText: string; R: TRect; var DefaultDraw: Boolean) of object;
TJvComboListDrawImageEvent = procedure(Sender: TObject; Index: Integer;
const APicture: TPicture; R: TRect; var DefaultDraw: Boolean) of object;
{$IFDEF VCL}
TJvComboListBox = class(TJvCustomListBox)
{$ENDIF VCL}
{$IFDEF VisualCLX}
TJvListBoxDataEvent = procedure(Sender: TWinControl; Index: Integer; var Text: string) of object; // JvListBox
TJvComboListBox = class(TJvExCustomListBox)
{$ENDIF VisualCLX}
private
FMouseOver: Boolean;
FPushed: Boolean;
FDropdownMenu: TPopupMenu;
FDrawStyle: TJvComboListBoxDrawStyle;
FOnDrawImage: TJvComboListDrawImageEvent;
FOnDrawText: TJvComboListDrawTextEvent;
FButtonWidth: Integer;
FHotTrackCombo: Boolean;
FLastHotTrack: Integer;
FOnDropDown: TJvComboListDropDownEvent;
{$IFDEF VisualCLX}
FOnGetText: TJvListBoxDataEvent; // JvListBox
{$ENDIF VisualCLX}
procedure SetDrawStyle(const Value: TJvComboListBoxDrawStyle);
function DestRect(Picture: TPicture; ARect: TRect): TRect;
function GetOffset(OrigRect, ImageRect: TRect): TRect;
procedure SetButtonWidth(const Value: Integer);
procedure SetHotTrackCombo(const Value: Boolean);
protected
procedure InvalidateItem(Index: Integer);
procedure DrawComboArrow(Canvas: TCanvas; R: TRect; Highlight, Pushed: Boolean);
{$IFDEF VCL}
procedure DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState); override;
{$ENDIF VCL}
{$IFDEF VisualCLX}
function DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState): Boolean; override;
procedure DoGetText(Index: Integer; var Text: string); virtual; // JvListBox
{$ENDIF VisualCLX}
procedure Resize; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseLeave(Control: TControl); override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
function DoDrawImage(Index: Integer; APicture: TPicture; R: TRect): Boolean; virtual;
function DoDrawText(Index: Integer; const AText: string; R: TRect): Boolean; virtual;
function DoDropDown(Index, X, Y: Integer): Boolean; virtual;
public
constructor Create(AOwner: TComponent); override;
function AddText(const S: string): Integer;
procedure InsertText(Index: Integer; const S: string);
// helper functions: makes sure the internal TPicture object is created and freed as necessary
function AddImage(P: TPicture): Integer;
procedure InsertImage(Index: Integer; P: TPicture);
procedure Delete(Index: Integer);
published
{$IFDEF VisualCLX}
property OnGetText: TJvListBoxDataEvent read FOnGetText write FOnGetText; // JvListBox
{$ENDIF VisualCLX}
property ButtonWidth: Integer read FButtonWidth write SetButtonWidth default 20;
property HotTrackCombo: Boolean read FHotTrackCombo write SetHotTrackCombo default False;
property DropdownMenu: TPopupMenu read FDropdownMenu write FDropdownMenu;
property DrawStyle: TJvComboListBoxDrawStyle read FDrawStyle write SetDrawStyle default dsOriginal;
property OnDrawText: TJvComboListDrawTextEvent read FOnDrawText write FOnDrawText;
property OnDrawImage: TJvComboListDrawImageEvent read FOnDrawImage write FOnDrawImage;
property OnDropDown: TJvComboListDropDownEvent read FOnDropDown write FOnDropDown;
property Align;
property Anchors;
{$IFDEF VCL}
property BiDiMode;
property DragCursor;
property DragKind;
property ImeMode;
property ImeName;
property IntegralHeight;
property ParentBiDiMode;
property OnEndDock;
property OnStartDock;
property HotTrack;
property ScrollBars;
property TabWidth;
property OnGetText;
property OnSelectCancel;
property OnVerticalScroll;
property OnHorizontalScroll;
{$ENDIF VCL}
property BorderStyle;
property Color;
property Columns;
property Constraints;
property DragMode;
property Enabled;
property ExtendedSelect;
property Font;
property HintColor;
property ItemHeight default 21;
property ItemIndex default -1;
property Items;
property MultiSelect;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnDrawItem;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMeasureItem;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDrag;
property OnMouseEnter;
property OnMouseLeave;
property OnParentColorChange;
// property OnChange; // not supported for listboxes
end;
{$IFDEF UNITVERSIONING}
const
UnitVersioning: TUnitVersionInfo = (
RCSfile: '$RCSfile: JvComboListBox.pas,v $';
Revision: '$Revision: 1.35 $';
Date: '$Date: 2005/02/17 10:20:02 $';
LogPath: 'JVCL\run'
);
{$ENDIF UNITVERSIONING}
implementation
uses
Math;
constructor TJvComboListBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Style := lbOwnerDrawFixed;
{$IFDEF VCL}
ScrollBars := ssVertical;
{$ENDIF VCL}
FDrawStyle := dsOriginal;
FButtonWidth := 20;
FLastHotTrack := -1;
ItemHeight := 21;
// ControlStyle := ControlStyle + [csCaptureMouse];
end;
function TJvComboListBox.AddImage(P: TPicture): Integer;
begin
Result := Items.Count;
InsertImage(Result, P);
end;
function TJvComboListBox.AddText(const S: string): Integer;
begin
Result := Items.Add(S);
end;
procedure TJvComboListBox.MouseLeave(Control: TControl);
begin
if csDesigning in ComponentState then
Exit;
inherited MouseLeave(Control);
if FMouseOver then
begin
InvalidateItem(ItemIndex);
FMouseOver := False;
end;
if HotTrackCombo and (FLastHotTrack > -1) then
begin
InvalidateItem(FLastHotTrack);
FLastHotTrack := -1;
end;
end;
procedure TJvComboListBox.Delete(Index: Integer);
var
P: TPicture;
begin
P := TPicture(Items.Objects[Index]);
Items.Delete(Index);
P.Free;
end;
function TJvComboListBox.DestRect(Picture: TPicture; ARect: TRect): TRect;
var
W, H, CW, CH: Integer;
XYAspect: Double;
begin
W := Picture.Width;
H := Picture.Height;
CW := ARect.Right - ARect.Left;
CH := ARect.Bottom - ARect.Top;
if (DrawStyle = dsStretch) or ((DrawStyle = dsProportional) and ((W > CW) or (H > CH))) then
begin
if (DrawStyle = dsProportional) and (W > 0) and (H > 0) then
begin
XYAspect := W / H;
if W > H then
begin
W := CW;
H := Trunc(CW / XYAspect);
if H > CH then // woops, too big
begin
H := CH;
W := Trunc(CH * XYAspect);
end;
end
else
begin
H := CH;
W := Trunc(CH * XYAspect);
if W > CW then // woops, too big
begin
W := CW;
H := Trunc(CW / XYAspect);
end;
end;
end
else
begin
W := CW;
H := CH;
end;
end;
with Result do
begin
Left := 0;
Top := 0;
Right := W;
Bottom := H;
end;
OffsetRect(Result, (CW - W) div 2, (CH - H) div 2);
end;
function TJvComboListBox.DoDrawImage(Index: Integer; APicture: TPicture; R: TRect): Boolean;
begin
Result := True;
if Assigned(FOnDrawImage) then
FOnDrawImage(Self, Index, APicture, R, Result);
end;
function TJvComboListBox.DoDrawText(Index: Integer; const AText: string; R: TRect): Boolean;
begin
Result := True;
if Assigned(FOnDrawText) then
FOnDrawText(Self, Index, AText, R, Result);
end;
function TJvComboListBox.DoDropDown(Index, X, Y: Integer): Boolean;
begin
Result := True;
if Assigned(FOnDropDown) then
FOnDropDown(Self, Index, X, Y, Result);
end;
procedure TJvComboListBox.DrawComboArrow(Canvas: TCanvas; R: TRect; Highlight, Pushed: Boolean);
var
uState: Cardinal;
begin
// Canvas.Font.Style := [];
(*
Canvas.Font.Name := 'Marlett';
if ButtonWidth > Font.Size + 5 then
Canvas.Font.Size := Font.Size + 3
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -