📄 teetree.pas
字号:
{**********************************************}
{ TTree Component Library }
{ Copyright (c) 1998-2005 by David Berneda }
{**********************************************}
{$I TeeDefs.inc}
unit TeeTree;
{$A-,B-} // <-- mandatory compiler options
(*
This unit implements a visual component: TTree
A Tree control maintains a network-hierarchy of "nodes", very similar
to Microsoft TreeView control.
But, every node in TeeTree is a full standalone component with properties,
methods and events.
Properties include color, border, brush, font, text, gradient, etc.
Every node in the tree can optionally be linked to one or more nodes
using "connection" components.
"Connections" are also components that have properties, methods and events,
which display lines from one node to another.
Also, every node can optionally be associated to a single "parent" node.
When assigned, the "parent" node will be the responsible to
expand / collapse its children nodes and also to free (destroy) its
children nodes when it is destroyed.
When a node has a "parent" node, and the parent node is moved
(changed to a new XY location), the children nodes are also moved.
This only happens when children nodes have set their "AutoPosition"
property to True.
The way parent and children nodes are arranged on screen (layout)
is determined by the "Tree.GlobalFormat.ChildManager" object,
when nodes are Auto-Positioned.
For example, the classic "Windows Explorer" layout of nodes is maintained
by the TTreeExplorerAlignChild class in this unit.
*)
interface
Uses {$IFNDEF LINUX}
Windows, Messages,
{$ENDIF}
{$IFDEF CLX}
Qt, QControls, QGraphics, QForms, QExtCtrls, QMenus, QDialogs, QStdCtrls,
QPrinters, QImgList,
{$ELSE}
Controls, Graphics, Forms, ExtCtrls, Menus, Printers, Dialogs, StdCtrls,
ImgList,
{$ENDIF}
{$IFDEF D6}
Types,
{$ENDIF}
{$IFDEF CLR}
System.ComponentModel,
{$ENDIF}
Classes, TeeProcs, TeCanvas;
{ Warning. These config-constants may not exist in future versions }
Const TeeDefaultBoxSize = 4;
TeeDefaultArrowSize = 4;
TeeDefaultGridStep = 10;
TeeDefaultMaxScroll = 1000;
TeeDefaultMinScroll = 0;
// Key codes
TeeTree_EditKey = TeeKey_F2;
TeeTree_EscapeKey = TeeKey_Escape;
TeeTree_HomeKey = {$IFDEF CLX}Key_Home{$ELSE}VK_HOME{$ENDIF};
TeeTree_InsertKey = TeeKey_Insert;
TeeTree_UpKey = TeeKey_Up;
TeeTree_DownKey = TeeKey_Down;
TeeTree_PriorKey = {$IFDEF CLX}Key_Prior{$ELSE}VK_PRIOR{$ENDIF};
TeeTree_NextKey = {$IFDEF CLX}Key_Next{$ELSE}VK_NEXT{$ENDIF};
TeeTree_RightKey = TeeKey_Right;
TeeTree_LeftKey = TeeKey_Left;
TeeTree_EndKey = {$IFDEF CLX}Key_End{$ELSE}VK_END{$ENDIF};
TeeTree_DeleteKey = TeeKey_Delete;
TeeTree_ReturnKey = TeeKey_Return;
TeeTree_SpaceKey = TeeKey_Space;
TeeTree_F11Key = {$IFDEF CLX}Key_F11{$ELSE}VK_F11{$ENDIF};
// Default size for "FolderOpenClose" image (optimization)
TreeDefaultImageHeight = 13;
TreeDefaultImageWidth = 15;
// Special cursors used in TGridShape class.
crArrowRight = TCursor(2021); { Right arrow cursor }
crArrowDown = TCursor(2022); { Down arrow cursor }
// Child manager constants.
// See TTreeExplorerAlignChild properties if you want to change them.
TreeHorizMarginDefault = 19;
TreeVertMarginDefault = 1;
TeeCrossBoxHorizMargin = 5;
TeeTree_DefaultYPosition = 2;
TeeHighLight={$IFDEF CLX}clNavy{$ELSE}clHighLight{$ENDIF};
TeeHighLightUnfocused={$IFDEF CLX}clGray{$ELSE}clInactiveCaption{$ENDIF};
TeeHighLightText={$IFDEF CLX}clWhite{$ELSE}clHighLightText{$ENDIF};
// Global variables. Warning: they might be removed / replaced in future versions.
var TeeTreeAnimatedScroll : Boolean = True;
TeeLineClickTolerance : Integer = 3; // pixels of tolerance when clicking nodes
TeeConnectionCursor : TCursor = crHandPoint; // default connection cursor
TeeConnectionPointCursor : TCursor = crCross; // default connection point cursor
TeePictureHorizMargin : Integer = 3;
TeePictureVertMargin : Integer = 2;
TeeTreeArrowMargin : Integer = 2;
TeeTextHorizMargin : Integer = 3;
TeeTextVertMargin : Integer = 3; //tom:01/10/2002
TeeTreeZ : Integer = 0; // default Z position
TreePageScrollQuantity : Integer = 20;
TeeTreeDeleteKey : Integer = TeeTree_DeleteKey; // key to delete nodes
// default Tree lists Capacity.
// Set both to 0 (zero) to minimize memory used.
// Set to a bigger number (ie: 1000 or 10000) for a little more speed
// when adding many nodes.
TreeListCapacity : Integer = 100;
TreeShapeListCapacity : Integer = 0;
type
TCustomTree=class;
TCustomTreePanel=TCustomTree;
TTreeClick=procedure( Sender:TCustomTreePanel;
Button:TMouseButton;
Shift: TShiftState;
X, Y: Integer) of object;
TCustomTreeElement=class;
// Node and Connection Text alignment constants
THorizTextAlign =(htaCenter,htaLeft,htaRight);
TVertTextAlign =(vtaCenter,vtaTop,vtaBottom);
TTreeStrings=class(TStringList)
private
FAngle : Integer; // 0..360 degree. Rotation angle of text.
FClipText : Boolean; // when true, text outside node is clipped
FHorizAlign : THorizTextAlign;
FHorizOffset : Integer;
FTransparency : TTeeTransparency;
FVertAlign : TVertTextAlign;
FVertOffset : Integer;
IOwner : TCustomTreeElement;
procedure SetAngle(const Value: Integer);
procedure SetClipText(const Value: Boolean);
procedure SetHorizAlign(const Value:THorizTextAlign);
procedure SetHorizOffset(const Value:Integer);
procedure SetTransparency(const Value: TTeeTransparency);
procedure SetVertAlign(const Value:TVertTextAlign);
procedure SetVertOffset(const Value:Integer);
public
procedure Assign(Source: TPersistent); override;
published
property Angle:Integer read FAngle write SetAngle default 0;
property ClipText:Boolean read FClipText write SetClipText default False;
property HorizAlign: THorizTextAlign read FHorizAlign
write SetHorizAlign default htaCenter;
property HorizOffset: Integer read FHorizOffset
write SetHorizOffset default 0;
property Transparency: TTeeTransparency read FTransparency
write SetTransparency default 0;
property VertAlign: TVertTextAlign read FVertAlign
write SetVertAlign default vtaCenter;
property VertOffset: Integer read FVertOffset
write SetVertOffset default 0;
end;
// This class is derived in TreeNodeShape and TreeConnection classes.
// It is the shared base class both for nodes and connections.
{$IFDEF CLR}
[ToolBoxItem(False)]
{$ENDIF}
TCustomTreeElement=class(TComponent)
private
FCursor : TCursor;
FData : {$IFDEF CLR}TObject{$ELSE}Pointer{$ENDIF};
FFont : TTeeFont;
FTextString : String;
Function GetFont:TTeeFont;
Function GetText:TTreeStrings;
Function InternalFont:TTeeFont;
procedure SetCursor(Value:TCursor);
procedure SetFont(Value: TTeeFont);
procedure SetHorizTextAlign(Value:THorizTextAlign);
procedure SetVertTextAlign(Value:TVertTextAlign);
function GetSimpleText: String;
procedure SetSimpleText(const Value: String);
function IsFontStored: Boolean;
function GetHorizTextAlign: THorizTextAlign;
function GetVertTextAlign: TVertTextAlign;
procedure ReadHorizAlign(Reader: TReader);
procedure ReadVertAlign(Reader: TReader);
protected
FText : TTreeStrings; // protected. Allows replacing.
FTree : TCustomTree;
procedure CanvasChanged(Sender:TObject); virtual;
Procedure DefineProperties(Filer:TFiler); override;
Function GetOwner:TPersistent; override;
Procedure InternalDrawHandles;
procedure ReadState(Reader: TReader); override;
Procedure SetBooleanProperty(Var Variable:Boolean; Const Value:Boolean);
Procedure SetColorProperty(Var Variable:TColor; Const Value:TColor);
Procedure SetDoubleProperty(Var Variable:Double; Const Value:Double);
Procedure SetIntegerProperty(Var Variable:Integer; Const Value:Integer);
{$IFNDEF CLR}
procedure SetParentComponent(AParent: TComponent); override;
{$ENDIF}
procedure SetText(Value : TTreeStrings);
Procedure SetTree(Value:TCustomTree); virtual;
Function TextLinesCount:Integer;
public
Destructor Destroy; override;
Procedure Assign(Source:TPersistent); override;
Procedure DrawHandles; dynamic;
function GetParentComponent: TComponent; override;
function HasParent: Boolean; override;
procedure Repaint;
{$IFDEF CLR}
procedure SetParentComponent(AParent: TComponent); override;
{$ENDIF}
property SimpleText: String read GetSimpleText write SetSimpleText;
property Cursor: TCursor read FCursor write SetCursor default crDefault;
property Data: {$IFDEF CLR}TObject{$ELSE}Pointer{$ENDIF} read FData write FData;
property Font: TTeeFont read GetFont write SetFont stored IsFontStored;
property HorizTextAlign: THorizTextAlign read GetHorizTextAlign
write SetHorizTextAlign;
property Text: TTreeStrings read GetText write SetText;
property Tree: TCustomTree read FTree write SetTree stored False;
property VertTextAlign: TVertTextAlign read GetVertTextAlign
write SetVertTextAlign;
end;
// Used when node Style is RoundRectangle.
TRoundRectanglePoint=Array[0..15] of TPoint;
TShapeGradient=TChartGradient; // alias
TTreePen=TChartPen; // alias
// Determines the AutoPosition of a node.
// By default, Left and Top are True.
// The Tree.GlobalFormat.ChildManager class is the responsible to
// calculate the Left and Top node positions.
TTreeShapeAutoPosition=class(TPersistent)
private
FNoLeft : Boolean;
FNoTop : Boolean;
IOnChange : TNotifyEvent;
Function GetLeft:Boolean;
Function GetTop:Boolean;
procedure SetLeft(Value:Boolean);
procedure SetTop(Value:Boolean);
public
Procedure Assign(Source:TPersistent); override;
published
property Left:Boolean read GetLeft write SetLeft stored False;
property Top:Boolean read GetTop write SetTop stored False;
end;
TTreeConnection=class; // forward declaration
TTreeConnectionClass=class of TTreeConnection;
TTreeBrush=TChartBrush; // alias
// Default White color brush for nodes.
TTreeShapeBrush=class(TTreeBrush)
public
Constructor Create(Changed:TNotifyEvent); override;
published
property Color default clWhite;
end;
TShapePoints=Array[0..100] of TPoint; // max 100 points for shape bounding.
TTreeNodeShape=class; { forward }
TTreeNodeShapeClass=class of TTreeNodeShape;
// Callback for Node "ForEach" method.
TNodeListForEachProc=Procedure(Sender:TTreeNodeShape) of object;
// Internal "TList" class.
// Copied from Borland's VCL and optimized for speed.
{$IFDEF CLR}
TTreeList=TList;
{$ELSE}
PPointerList = ^TPointerList;
TPointerList = array[0..MaxListSize - 1] of Pointer;
TTreeList=class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -