-
Notifications
You must be signed in to change notification settings - Fork 8
/
TreeViewExUnit.pas
350 lines (318 loc) · 9.5 KB
/
TreeViewExUnit.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
unit TreeViewExUnit;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.StdCtrls, Vcl.ComCtrls, ConstUnit, Data.Db,
Vcl.Dialogs, Vcl.ExtCtrls;
type
TTreeNodeEx = class(TTreeNode)
private
FDataSource: string;
FTableList: TStringList;
public
constructor Create(AOwner: TTreeNodes); override;
destructor Destroy; override;
property DataSource: string read FDataSource write FDataSource;
end;
TTreeViewEx = class(TCustomTreeView)
private
FBdType: TBdType;
FDarkMode: boolean;
FSelectedColor: TColor;
procedure TreeViewCompare(Sender: TObject; Node1, Node2: TTreeNode;
Data: Integer; var Compare: Integer);
procedure WMRButtonDown(var Message: TWMLButtonDblClk); message WM_RBUTTONDOWN;
function Destroying: boolean;
procedure SetBdType(const Value: TBdType);
protected
function IsCustomDrawn(Target: TCustomDrawTarget; Stage: TCustomDrawStage): Boolean; override;
function CustomDrawItem(Node: TTreeNode; State: TCustomDrawState;
Stage: TCustomDrawStage; var PaintImages: Boolean): Boolean; override;
procedure TreeViewExGetImageIndex(Sender: TObject; Node: TTreeNode);
procedure TreeViewExGetSelectedIndex(Sender: TObject; Node: TTreeNode);
procedure TreeViewCreateNodeClass(Sender: TCustomTreeView;
var NodeClass: TTreeNodeClass);
public
constructor Create(AOwner: TComponent); override;
function NodeFoundByDataSource(TreeView: TTreeViewEx; DataSource: string): TTreeNode;
procedure AddServer(const Data: TCommand; Login: string; Bases: TDataset);
property BdType: TBdType read FBdType write SetBdType;
property DarkMode: boolean read FDarkMode write FDarkMode;
property SelectedColor: TColor read FSelectedColor write FSelectedColor;
published
property Align;
property Anchors;
property AutoExpand;
property BevelEdges;
property BevelInner;
property BevelOuter;
property BevelKind default bkNone;
property BevelWidth;
property BiDiMode;
property BorderStyle;
property BorderWidth;
property ChangeDelay;
property Color;
property Ctl3D;
property Constraints;
property DoubleBuffered;
property DragKind;
property DragCursor;
property DragMode;
property Enabled;
property Font;
property HideSelection;
property HotTrack;
property Images;
property Indent;
property MultiSelect;
property MultiSelectStyle;
property ParentBiDiMode;
property ParentColor default False;
property ParentCtl3D;
property ParentDoubleBuffered;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly;
property RightClickSelect;
property RowSelect;
property ShowButtons;
property ShowHint;
property ShowLines;
property ShowRoot;
property SortType;
property StateImages;
property TabOrder;
property TabStop default True;
property ToolTips;
property Touch;
property Visible;
property StyleElements;
property OnAddition;
property OnAdvancedCustomDraw;
property OnAdvancedCustomDrawItem;
property OnCancelEdit;
property OnChange;
property OnChanging;
property OnClick;
property OnCollapsed;
property OnCollapsing;
property OnCompare;
property OnContextPopup;
property OnCreateNodeClass;
property OnCustomDraw;
property OnCustomDrawItem;
property OnDblClick;
property OnDeletion;
property OnDragDrop;
property OnDragOver;
property OnEdited;
property OnEditing;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnExpanding;
property OnExpanded;
property OnGesture;
property OnGetImageIndex;
property OnGetSelectedIndex;
property OnHint;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseActivate;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
{ Items must be published after OnGetImageIndex and OnGetSelectedIndex }
property Items;
end;
TTreeNodeHelper = class helper for TTreeNode
private
function GetItemType: TItemType;
procedure SetItemType(const Value: TItemType);
function GetTableList: TStringList;
public
property ItemType: TItemType read GetItemType write SetItemType;
property TableList: TStringList read GetTableList;
end;
implementation
{ TTreeNodeHelper }
function TTreeNodeHelper.GetItemType: TItemType;
begin
Result := TItemType(Self.Data);
end;
function TTreeNodeHelper.GetTableList: TStringList;
begin
Result := TTreeNodeEx(Self).FTableList;
end;
procedure TTreeNodeHelper.SetItemType(const Value: TItemType);
begin
Self.Data := Pointer(Integer(Value));
end;
{ TTreeViewEx }
constructor TTreeViewEx.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
OnCompare := TreeViewCompare;
ReadOnly := True;
SortType := stText;
Align := alClient;
HideSelection := False;
ShowRoot := False;
BevelOuter := bvNone;
BevelInner := bvNone;
BorderStyle := bsNone;
BorderWidth := 4;
OnGetImageIndex := TreeViewExGetImageIndex;
OnGetSelectedIndex := TreeViewExGetSelectedIndex;
OnCreateNodeClass := TreeViewCreateNodeClass;
end;
function TTreeViewEx.CustomDrawItem(Node: TTreeNode; State: TCustomDrawState;
Stage: TCustomDrawStage; var PaintImages: Boolean): Boolean;
var
NodeRect: TRect;
begin
Result := inherited CustomDrawItem(Node,State,Stage,PaintImages);
if (Stage = cdPrePaint) and PaintImages then
begin
if Node.ItemType = itLogin then
Canvas.Font.Style := Canvas.Font.Style + [fsBold]
else
begin
Canvas.Font.Style := Canvas.Font.Style - [fsBold];
if (State = [cdsSelected]) and FDarkMode then
begin
Canvas.Brush.Color := FSelectedColor; //RGB(120,120,120); // RGB(34,61,84);
NodeRect := Node.DisplayRect(True);
NodeRect.Left := NodeRect.Left - Images.Width - 5;
NodeRect.Right := NodeRect.Right + 2;
Canvas.FillRect(NodeRect);
Canvas.Font.Color := clBlack;
end;
end;
end;
end;
function TTreeViewEx.IsCustomDrawn(Target: TCustomDrawTarget;
Stage: TCustomDrawStage): Boolean;
begin
if (Stage = cdPrePaint) and (Target = dtItem) then
Result := True
else
Result := inherited IsCustomDrawn(Target, Stage);
end;
procedure TTreeViewEx.SetBdType(const Value: TBdType);
begin
FBdType := Value;
end;
procedure TTreeViewEx.TreeViewCompare(Sender: TObject; Node1, Node2: TTreeNode;
Data: Integer; var Compare: Integer);
begin
if TTreeView(Sender).Visible then
begin
if Node1.ItemType = itLogin then
begin
Compare := -1;
Exit;
end;
if Node2.ItemType = itLogin then
begin
Compare := 1;
Exit;
end;
Compare := lstrcmp(PChar(Node1.Text), PChar(Node2.Text))
end;
end;
procedure TTreeViewEx.TreeViewCreateNodeClass(Sender: TCustomTreeView;
var NodeClass: TTreeNodeClass);
begin
NodeClass := TTreeNodeEx;
end;
procedure TTreeViewEx.TreeViewExGetImageIndex(Sender: TObject; Node: TTreeNode);
begin
if Node.HasChildren then
Node.ImageIndex := 0
else
case Node.ItemType of
itBase : Node.ImageIndex := 1;
itBaseRTI : Node.ImageIndex := 2;
itLogin : Node.ImageIndex := 3;
end;
end;
procedure TTreeViewEx.TreeViewExGetSelectedIndex(Sender: TObject; Node: TTreeNode);
begin
Node.SelectedIndex := Node.ImageIndex;
end;
function TTreeViewEx.Destroying: boolean;
begin
Result := (csDestroying in ComponentState) or Application.Terminated;
end;
procedure TTreeViewEx.WMRButtonDown(var Message: TWMLButtonDblClk);
var
Node: TTreeNode;
begin
if Destroying then Exit;
Node := TTreeNode(GetNodeAt(TWMMouse(Message).XPos, TWMMouse(Message).YPos));
if Assigned(Node) then
begin
Self.Selected := Node;
end;
end;
procedure TTreeViewEx.AddServer(const Data: TCommand; Login: string;
Bases: TDataset);
var
TreeNode,Node: TTreeNode;
begin
TreeNode := Items.AddChild(nil,Data.Substring(0,Data.IndexOf('|')));
TreeNode.Data := Pointer(Self.BdType); //Ïåðåäàåì òèï ñåðâåðà
TTreeNodeEx(TreeNode).DataSource := Data;
if not Login.IsEmpty then
begin
Node := Items.AddChild(TreeNode,Login);
Node.Data := Pointer(Integer(itLogin));
end;
if Assigned(Bases) and Bases.Active then
begin
Bases.First;
while not Bases.Eof do
begin
Node := Items.AddChild(TreeNode,Bases.Fields[0].AsString);
Node.Data := Pointer(Integer(itBase));
Bases.Next;
end;
Bases.Close;
end;
TreeNode.Expand(True);
end;
function TTreeViewEx.NodeFoundByDataSource(TreeView: TTreeViewEx;
DataSource: string): TTreeNode;
var
i: Integer;
begin
Result := nil;
for i := 0 to TreeView.Items.Count-1 do
if TreeView.Items[i].Parent = nil then
if TTreeNodeEx(TreeView.Items[i]).DataSource = DataSource then
begin
Result := TreeView.Items[i];
break;
end;
end;
{ TTreeNodeEx }
constructor TTreeNodeEx.Create(AOwner: TTreeNodes);
begin
inherited;
FTableList := TStringList.Create;
end;
destructor TTreeNodeEx.Destroy;
begin
FTableList.Free;
inherited;
end;
end.