Menu

[r3166]: / trunk / Src / FrCheckedTV.pas  Maximize  Restore  History

Download this file

749 lines (688 with data), 24.2 kB

  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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2006-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements a frame containing a treeview that has check boxes associated with
* tree nodes and updates parent check boxes according to state of child nodes.
}
unit FrCheckedTV;
interface
uses
// Delphi
ImgList, Controls, Classes, ComCtrls, StdCtrls, Forms,
// Project
UKeysHelper, UTVCheckBoxes;
type
{
TCheckedTreeNode:
Custom tree node class that has an associated check box that can appear
checked, unchecked and greyed in either hot or normal state.
}
TCheckedTreeNode = class(TTreeNode)
strict private
fHot: Boolean; // Value of Hot property
procedure SetImage(const ImgIdx: Integer);
{Sets node's image. ImageIndex and SelectedImageIndex properties are set
to same value.
@param ImgIdx [in] Required image index.
}
function GetCheck: TCheckBoxState;
{Read accessor for Check property. Calculates value from ImageIndex.
@return Current property value.
}
procedure SetCheck(const Value: TCheckBoxState);
{Write accessor for Check property. Updates node's image.
@param Value [in] New property value.
}
procedure SetHot(const Value: Boolean);
{Write accessor for Hot property. Updates node's image.
@param Value [in] New value of property.
}
function GetParent: TCheckedTreeNode;
{Read accessor for Parent property.
@return Value of Parent property.
}
function GetTopLevelParent: TCheckedTreeNode;
{Read accessor for TopLevelParent property.
@return Reference to top level parent node or Self if node has no parent.
}
public
function IsChecked: Boolean;
{Checks if node is checked.
@return True if node is checked.
}
function IsUnchecked: Boolean;
{Checks if node is unchecked.
@return True if node is unchecked.
}
function HasParent: Boolean;
{Checks if node has a parent node.
@return True if node has parent.
}
function GetNextSibling: TCheckedTreeNode;
{Gets reference to node's next sibling.
@return Reference to next sibling or nil if no more siblings.
}
function GetFirstChild: TCheckedTreeNode;
{Gets reference to a node's first child node.
@return Reference to child node or nil if no child nodes.
}
procedure ToggleCheck;
{Toggles node's check box off if currently checked or greyed or on if
currently unchecked.
}
property Parent: TCheckedTreeNode read GetParent;
{Node's parent or nil if node has no parent. This is a re-implementation
of inherited property}
property TopLevelParent: TCheckedTreeNode read GetTopLevelParent;
{Top level parent of node. Self if node is a top level node}
property Check: TCheckBoxState read GetCheck write SetCheck;
{State of node's associated checkbox image}
property Hot: Boolean read fHot write SetHot;
{Whether node's associated checkbox is "hot" or not}
end;
{
TCheckedTVFrame:
Abstract frame base class containing a treeview that has check boxes
associated with tree nodes and updates parent check boxes according to state
of child nodes.
}
TCheckedTVFrame = class(TFrame)
ilChecks: TImageList;
tvChecked: TTreeView;
procedure FrameEnter(Sender: TObject);
procedure tvCheckedCollapsing(Sender: TObject; Node: TTreeNode;
var AllowCollapse: Boolean);
procedure tvCheckedKeyPress(Sender: TObject; var Key: Char);
procedure tvCheckedMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure tvCheckedMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure tvCheckedCreateNodeClass(Sender: TCustomTreeView;
var NodeClass: TTreeNodeClass);
strict private
fOnChange: TNotifyEvent; // Event handler for OnChange event
fCanCollapse: Boolean; // Value of CanCollapse property
fTVChecks: TTVCheckBoxes; // Object that manages check box image lists
fHotNode: TCheckedTreeNode; // Tree node currently under mouse cursor
procedure SetCanCollapse(Flag: Boolean);
{Write access method for CanCollapse property.
@param Flag [in] New property value.
}
procedure MakeHotNode(const Node: TCheckedTreeNode);
{Makes a node's check box appear "hot". Sets any previous hot check box
back to normal state.
@param Node [in] Node whose check box to appear hot.
}
procedure SetParentStatesFromChildren(Node: TCheckedTreeNode);
{Sets state of a node parent, grandparent etc nodes according to state of
node and its siblings. Parent nodes are checked if all children are
checked, unchecked if all children unchecked and greyed if children have
mixed states.
@param Node [in] Node whose parent states are to be updated.
}
procedure SetParentStates;
{Sets state of all parent nodes according to state of their child nodes.
Parent nodes are checked if all children are checked, unchecked if all
children unchecked and greyed if children have mixed states.
}
procedure CopyParentStateToChildren(ParentNode: TCheckedTreeNode);
{Copies state of check box of parent node to all its child nodes and
children's child nodes.
@param ParentNode [in] Parent node whose state is to be copied.
}
procedure PopulateTreeView;
{Populates treeview with required nodes.
}
procedure ToggleNode(const Node: TCheckedTreeNode);
{Toggles checked state of a node and updates parent or child nodes
accordingly. Changed are recorded and OnChange event triggered.
@param Node [in] Node whose check state is to be toggled.
}
procedure CheckBoxChangeHandler(Sender: TObject);
{Handles TTVCheckBoxes.OnChange event triggered when appearance of check
boxes changes. Refreshes tree view.
@param Sender [in] Not used.
}
strict protected
procedure AddNodes; virtual; abstract;
{Called when tree view is to be populated with nodes.
}
procedure SetLeafNodeState(const Node: TCheckedTreeNode); virtual; abstract;
{Called to get checked state of a leaf node when tree view's underlying
data changes. Implementor should set node's check state.
@param Node [in] Node to have check state set.
}
procedure RecordChanges; virtual; abstract;
{Called when state of treeview changes to enable tree view's underlying
data to be changed.
}
function AddNode(const Parent: TCheckedTreeNode; const Text: string;
const Data: Pointer): TCheckedTreeNode;
{Adds new unchecked tree node. This method can be called from descendant
classes to add new nodes to tree from their implementation of the AddNodes
method.
@param Parent [in] Parent of new node or nil if new node is top level.
@param Text [in] Text to display in new node.
@param Data [in] User defined data assoicated with new node.
@return Reference to new node.
}
function FirstNode: TCheckedTreeNode;
{Gets reference to first top level node in tree view.
@return Node reference.
}
procedure UpdateTreeViewState;
{Updates check boxes of each node in treeview. Child nodes state is
requested and parent node are set according to state of child nodes.
}
procedure DataChanged;
{Notifies that state of tree view's underlying data has changed. Leaf node
state is requested and parent node states are set accroding to state of
child nodes.
}
procedure DoChanged;
{Triggers OnChange event.
}
public
constructor Create(AOwner: TComponent); override;
{Class constructor. Sets up object and populates tree view.
@param AOwner [in] Component that owns this frame.
}
destructor Destroy; override;
{Class destructor. Tears down object.
}
procedure ExpandTree;
procedure CollapseTree;
property CanCollapse: Boolean
read fCanCollapse write SetCanCollapse default False;
{Determines whether tree nodes can be collapsed. Also switches on buttons
for user to use to make nodes expand and collapse}
property OnChange: TNotifyEvent
read fOnChange write fOnChange;
{Event triggered when state of a tree node changes}
end;
implementation
uses
// Delphi
SysUtils;
{$R *.dfm}
{ TCheckedTVFrame }
function TCheckedTVFrame.AddNode(const Parent: TCheckedTreeNode;
const Text: string; const Data: Pointer): TCheckedTreeNode;
{Adds new unchecked tree node. This method can be called from descendant
classes to add new nodes to tree from their implementation of the AddNodes
method.
@param Parent [in] Parent of new node or nil if new node is top level.
@param Text [in] Text to display in new node.
@param Data [in] User defined data assoicated with new node.
@return Reference to new node.
}
begin
Result := tvChecked.Items.AddChild(Parent, Text) as TCheckedTreeNode;
Result.Data := Data;
Result.Check := cbUnchecked;
end;
procedure TCheckedTVFrame.CheckBoxChangeHandler(Sender: TObject);
{Handles TTVCheckBoxes.OnChange event triggered when appearance of check boxes
changes. Refreshes tree view.
@param Sender [in] Not used.
}
begin
tvChecked.Refresh;
end;
procedure TCheckedTVFrame.CollapseTree;
begin
if not fCanCollapse then
Exit;
tvChecked.Items.BeginUpdate;
try
tvChecked.FullCollapse;
if Assigned(tvChecked.Selected) then
tvChecked.Selected.MakeVisible;
finally
tvChecked.Items.EndUpdate;
end;
end;
procedure TCheckedTVFrame.CopyParentStateToChildren(
ParentNode: TCheckedTreeNode);
{Copies state of check box of parent node to all its child nodes and
children's child nodes.
@param ParentNode [in] Parent node whose state is to be copied.
}
var
ChildNode: TCheckedTreeNode; // reference to each of parent's child nodes
begin
ChildNode := ParentNode.GetFirstChild;
while Assigned(ChildNode) do
begin
ChildNode.Check := ParentNode.Check;
CopyParentStateToChildren(ChildNode);
ChildNode := ChildNode.GetNextSibling;
end;
end;
constructor TCheckedTVFrame.Create(AOwner: TComponent);
{Class constructor. Sets up object and populates tree view.
@param AOwner [in] Component that owns this frame.
}
begin
inherited;
fTVChecks := TTVCheckBoxes.Create(ilChecks);
fTVChecks.OnChange := CheckBoxChangeHandler;
PopulateTreeView;
end;
procedure TCheckedTVFrame.DataChanged;
{Notifies that state of tree view's underlying data has changed. Leaf node
state is requested and parent node states are set accroding to state of child
nodes.
}
// ---------------------------------------------------------------------------
procedure SetLeafNodeStates(const Node: TCheckedTreeNode);
{Recursively sets state of leaf nodes.
@param Node [in] Node to process. If Node is leaf node its state is
requested. If Node is a parent we set state of its child nodes
recursively.
}
var
ChildNode: TCheckedTreeNode; // reference to each child of node
begin
if Node.HasChildren then
begin
ChildNode := Node.GetFirstChild;
while Assigned(ChildNode) do
begin
SetLeafNodeStates(ChildNode);
ChildNode := ChildNode.GetNextSibling;
end;
end
else
SetLeafNodeState(Node);
end;
// ---------------------------------------------------------------------------
var
Node: TCheckedTreeNode; // reference to each top level node
begin
// Loop through all top level nodes setting their leaf node states
Node := FirstNode;
while Assigned(Node) do
begin
SetLeafNodeStates(Node);
Node := Node.GetNextSibling;
end;
// Set state of parent nodes based on state of leaf node
SetParentStates;
// Notify that tree view has (may have) changed
DoChanged;
end;
destructor TCheckedTVFrame.Destroy;
{Class destructor. Tears down object.
}
begin
FreeAndNil(fTVChecks);
inherited;
end;
procedure TCheckedTVFrame.DoChanged;
{Triggers OnChange event.
}
begin
if Assigned(fOnChange) then
fOnChange(Self);
end;
procedure TCheckedTVFrame.ExpandTree;
begin
if not fCanCollapse then
Exit;
tvChecked.Items.BeginUpdate;
try
tvChecked.FullExpand;
if Assigned(tvChecked.Selected) then
tvChecked.Selected.MakeVisible;
finally
tvChecked.Items.EndUpdate;
end;
end;
function TCheckedTVFrame.FirstNode: TCheckedTreeNode;
{Gets reference to first top level node in tree view.
@return Node reference.
}
begin
Result := tvChecked.Items.GetFirstNode as TCheckedTreeNode;
end;
procedure TCheckedTVFrame.FrameEnter(Sender: TObject);
{Handles event triggered when frame is entered. Moves focus to contained tree
view.
@param Sender [in] Not used.
}
begin
if Assigned(tvChecked) then
tvChecked.SetFocus;
end;
procedure TCheckedTVFrame.MakeHotNode(const Node: TCheckedTreeNode);
{Makes a node's check box appear "hot". Sets any previous hot check box back
to normal state.
@param Node [in] Node whose check box to appear hot.
}
begin
if Node <> fHotNode then
begin
if Assigned(fHotNode) then
fHotNode.Hot := False;
if Assigned(Node) then
Node.Hot := True;
fHotNode := Node;
end;
end;
procedure TCheckedTVFrame.PopulateTreeView;
{Populates treeview with required nodes.
}
begin
// Get descendant to add nodes to tree view
AddNodes;
// Expand treeview and display top of tree
tvChecked.FullExpand;
if FirstNode <> nil then
FirstNode.MakeVisible;
end;
procedure TCheckedTVFrame.SetCanCollapse(Flag: Boolean);
begin
if fCanCollapse = Flag then
Exit;
tvChecked.ShowRoot := Flag;
tvChecked.ShowButtons := Flag;
fCanCollapse := Flag;
end;
procedure TCheckedTVFrame.SetParentStates;
{Sets state of all parent nodes according to state of their child nodes.
Parent nodes are checked if all children are checked, unchecked if all
children unchecked and greyed if children have mixed states.
}
var
Node: TCheckedTreeNode; // references each of top level tree nodes
begin
Node := FirstNode;
while Assigned(Node) do
begin
SetParentStatesFromChildren(Node);
Node := Node.GetNextSibling;
end;
end;
procedure TCheckedTVFrame.SetParentStatesFromChildren(
Node: TCheckedTreeNode);
{Sets state of a node parent, grandparent etc nodes according to state of node
and its siblings. Parent nodes are checked if all children are checked,
unchecked if all children unchecked and greyed if children have mixed states.
@param Node [in] Node whose parent states are to be updated.
}
var
ChildNode: TCheckedTreeNode; // references child nodes of node
CheckedChildCount: Integer; // count of checked child nodes
UnCheckedChildCount: Integer; // count of unchecked child nodes
begin
// If child node do nothing
if not Node.HasChildren then
Exit;
// We have parent node
// count nodes checked and unchecked children (some may be grey)
CheckedChildCount := 0;
UnCheckedChildCount := 0;
ChildNode := Node.GetFirstChild;
while Assigned(ChildNode) do
begin
if ChildNode.HasChildren then
// child is itself a parent: update its check state recursively
SetParentStatesFromChildren(ChildNode);
if ChildNode.IsChecked then
Inc(CheckedChildCount)
else if ChildNode.IsUnchecked then
Inc(UncheckedChildCount);
ChildNode := ChildNode.GetNextSibling;
end;
// Set state of node according to checked / unchecked count
if UncheckedChildCount = Node.Count then
Node.Check := cbUnchecked
else if CheckedChildCount = Node.Count then
Node.Check := cbChecked
else
Node.Check := cbGrayed;
end;
procedure TCheckedTVFrame.ToggleNode(const Node: TCheckedTreeNode);
{Toggles checked state of a node and updates parent or child nodes
accordingly. Changed are recorded and OnChange event triggered.
@param Node [in] Node whose check state is to be toggled.
}
begin
Assert(Assigned(Node), ClassName + '.ToggleNode: Node is nil');
Node.ToggleCheck;
if Node.HasChildren then
CopyParentStateToChildren(Node);
if Node.HasParent then
SetParentStatesFromChildren(Node.TopLevelParent);
// Refresh tree view display to display revised glyphs
tvChecked.Refresh;
// Update data from selections
RecordChanges;
// Trigger OnChange event
DoChanged;
end;
procedure TCheckedTVFrame.tvCheckedCollapsing(Sender: TObject;
Node: TTreeNode; var AllowCollapse: Boolean);
{Handler for tree view's OnCollapsing event. Prevents tree from being
collapsed.
@param Sender [in] Not used.
@param Node [in] Not used.
@param AllowCollapse [in/out] Whether collapsing allowed. Set to false.
}
begin
AllowCollapse := fCanCollapse;
end;
procedure TCheckedTVFrame.tvCheckedCreateNodeClass(
Sender: TCustomTreeView; var NodeClass: TTreeNodeClass);
{Handler for tree view's OnCreateNodeClass event. Passes back custom tree node
class to use when creating tree nodes.
@param Sender [in] Not used.
@param NodeClass [in/out] Set to required tree node class type.
}
begin
NodeClass := TCheckedTreeNode;
end;
procedure TCheckedTVFrame.tvCheckedKeyPress(Sender: TObject;
var Key: Char);
{Handles key presses on tree view. If space is pressed check state of selected
node is toggled. All other keypressed are inhibited to prevent initial
characters from changing selection.
@param Sender [in] Not used.
@param Key [in/out] Stores pressed key when called. Set to #0 to inhibit
further processing.
}
begin
if (Key = ' ') and Assigned(tvChecked.Selected) then
ToggleNode(tvChecked.Selected as TCheckedTreeNode);
Key := #0; // inhibits selection of entries when intial letter pressed
end;
procedure TCheckedTVFrame.tvCheckedMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
{Handles mouse clicks on treeview. Unmodified mouse down events are used to
toggle check state of tree nodes. Clicks outside nodes are ignored.
@param Sender [in] Not used.
@param Button [in] Mouse button pressed. We only accept left button.
@param Shift [in] Shift and button state. We only accept unmodified clicks.
@param X [in] X co-ordinate of mouse click.
@param Y [in] Y co-ordinate of mouse click.
}
var
SelNode: TCheckedTreeNode; // reference to currently selected tree node
HitTestInfo: THitTests; // info about where in treeview mouse pressed
begin
SelNode := tvChecked.GetNodeAt(X, Y) as TCheckedTreeNode;
HitTestInfo := tvChecked.GetHitTestInfoAt(X, Y);
if (htOnIcon in HitTestInfo) and (Button = mbLeft)
and not HasShiftKeys(Shift) and Assigned(SelNode) then
ToggleNode(SelNode);
end;
procedure TCheckedTVFrame.tvCheckedMouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
{Handler for tree view's OnMouseMove event. Sets check box of any node under
the mouse to hot state.
@param Sender [in] Not used.
@param Shift [in] Not used.
@param X [in] X coordinate of mouse.
@param Y [in] Y coordinate of mouse.
}
var
SelNode: TCheckedTreeNode; // reference to currently selected tree node
HitTestInfo: THitTests; // info about where in treeview mouse pressed
begin
HitTestInfo := tvChecked.GetHitTestInfoAt(X, Y);
if (htOnItem in HitTestInfo) then
SelNode := tvChecked.GetNodeAt(X, Y) as TCheckedTreeNode
else
SelNode := nil;
MakeHotNode(SelNode);
end;
procedure TCheckedTVFrame.UpdateTreeViewState;
{Updates check boxes of each node in treeview. Child nodes state is requested
and parent node are set according to state of child nodes. Should be called
when state of data represented in tree view changes.
}
procedure SetLeafNodeStates(const Node: TCheckedTreeNode);
{Recursively sets state of leaf nodes.
@param Node [in] Node to process. If Node is leaf node its state is
requested. If Node is a parent we set state of its child nodes
recursively.
}
var
ChildNode: TCheckedTreeNode; // reference to each child of node
begin
if Node.HasChildren then
begin
ChildNode := Node.GetFirstChild;
while Assigned(ChildNode) do
begin
SetLeafNodeStates(ChildNode);
ChildNode := ChildNode.GetNextSibling;
end;
end
else
SetLeafNodeState(Node);
end;
var
Node: TCheckedTreeNode; // reference to each top level node
begin
// Loop through all top level nodes setting their leaf node states
Node := FirstNode;
while Assigned(Node) do
begin
SetLeafNodeStates(Node);
Node := Node.GetNextSibling as TCheckedTreeNode;
end;
// Set state of parent nodes based on state of leaf node
SetParentStates;
// Notify that tree view has (may have) changed
DoChanged;
end;
{ TCheckedTreeNode }
function TCheckedTreeNode.GetCheck: TCheckBoxState;
{Read accessor for Check property. Calculates value from ImageIndex.
@return Current property value.
}
begin
Result := TTVCheckBoxes.ImageIdxToCheckState(Self.ImageIndex);
end;
function TCheckedTreeNode.GetFirstChild: TCheckedTreeNode;
{Gets reference to a node's first child node.
@return Reference to child node or nil if no child nodes.
}
begin
// This method simply casts first child from TTreeNode to TCheckedTreeNode
Result := inherited GetFirstChild as TCheckedTreeNode;
end;
function TCheckedTreeNode.GetNextSibling: TCheckedTreeNode;
{Gets reference to node's next sibling.
@return Reference to next sibling or nil if no more siblings.
}
begin
// This method simply casts next sibling from TTreeNode to TCheckedTreeNode
Result := inherited GetNextSibling as TCheckedTreeNode;
end;
function TCheckedTreeNode.GetParent: TCheckedTreeNode;
{Read accessor for Parent property.
@return Value of parent property.
}
begin
// This method simply converts Parent from TTreeNode to TCheckedTreeNode
Result := inherited Parent as TCheckedTreeNode;
end;
function TCheckedTreeNode.GetTopLevelParent: TCheckedTreeNode;
{Read accessor for TopLevelParent property.
@return Reference to top level parent node or Self if node has no parent.
}
begin
if not HasParent then
Result := Self
else
Result := Parent.TopLevelParent;
end;
function TCheckedTreeNode.HasParent: Boolean;
{Checks if node has a parent node.
@return True if node has parent.
}
begin
Result := Assigned(Parent);
end;
function TCheckedTreeNode.IsChecked: Boolean;
{Checks if node is checked.
@return True if node is checked.
}
begin
Result := Check = cbChecked;
end;
function TCheckedTreeNode.IsUnchecked: Boolean;
{Checks if node is unchecked.
@return True if node is unchecked.
}
begin
Result := Check = cbUnchecked;
end;
procedure TCheckedTreeNode.SetCheck(const Value: TCheckBoxState);
{Write accessor for Check property. Updates node's image.
@param Value [in] New property value.
}
begin
SetImage(TTVCheckBoxes.CheckImageIdx(Value, Hot));
end;
procedure TCheckedTreeNode.SetHot(const Value: Boolean);
{Write accessor for Hot property. Updates node's image.
@param Value [in] New value of property.
}
begin
if fHot <> Value then
begin
fHot := Value;
SetImage(TTVCheckBoxes.CheckImageIdx(GetCheck, Value));
end;
end;
procedure TCheckedTreeNode.SetImage(const ImgIdx: Integer);
{Sets node's image. ImageIndex and SelectedImageIndex properties are set to
same value.
@param ImgIdx [in] Required image index.
}
begin
ImageIndex := ImgIdx;
SelectedIndex := ImgIdx;
end;
procedure TCheckedTreeNode.ToggleCheck;
{Toggles node's check box off if currently checked or greyed or on if
currently unchecked.
}
begin
case Check of
cbChecked, cbGrayed: Check := cbUnchecked;
cbUnchecked: Check := cbChecked;
end;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.