Tree Nodes and Persistence (HTreeEngine unit)

Logical tree nodes, searches, sorting, selection, and XML/JSON persistence

ThTreeNode

An in-memory tree node. A node may exist without a visual control. Create a root with ParentNode = nil; the second constructor argument enables BOM support for it and its descendants.
ThTreeNode = Class(TObject);
Root := ThTreeNode.Create(nil, False);
try
  Child := Root.Add_Child_Node('Parts', '');
  // Use Root.Child(0) to retrieve the first child.
finally
  Root.Free;
end;

Create

Creates a node. Pass nil for an independent root. Children are normally created through Add_Child_Node so that the parent list is updated. Destroying a node frees its descendants and BOM object.
Constructor ThTreeNode.Create(ParentNode: ThTreeNode; Bill_Of_Material_Support: Boolean);

Node fields

Public node data. Runtime tags can hold application data; rt_Obj1 is an unmanaged object reference.
DeclarationMeaning
ThTreeNode.Caption: String;
Node text; may contain multiple lines.
ThTreeNode.Bmp_Icon: String;
Icon identifier.
ThTreeNode.Hidden: Boolean;
Refresh hides this node and all descendants.
ThTreeNode.Selected: Boolean;
Checkbox selection state.
ThTreeNode.Padding_Top: Byte;
Extra top spacing for a visual row; intended values 0..3.
ThTreeNode.rt_Type: Byte;
Application type tag.
ThTreeNode.rt_STag: Array[1..4] OF String;
String tags.
ThTreeNode.rt_DTag: Array[1..4] OF Double;
Numeric tags.
ThTreeNode.rt_BTag: Array[1..4] OF Boolean;
Boolean tags.
ThTreeNode.rt_ITag: Array[1..8] OF Integer;
Integer tags.
ThTreeNode.rt_Obj1: TObject;
Runtime object reference; neither copied nor persisted.

Clear

Resets node data and deletes all child nodes. It does not reset the associated BOM object; call its Clear separately if needed.
Procedure ThTreeNode.Clear;

Clear_Child_Nodes

Deletes direct children and their subtrees. False preserves direct children whose BOM.Inserted_By_End_User is True; without BOM support the method deletes all children.
Procedure ThTreeNode.Clear_Child_Nodes(Including_Inserted_By_End_User: Boolean = True);

ChildCount

Number of immediate children.
Function ThTreeNode.ChildCount: Cardinal;

Child

Returns the child at zero-based Index, or nil if Index is outside the list.
Function ThTreeNode.Child(CONST Index: Integer): ThTreeNode;

LastChild

Returns the last direct child, or nil when no children exist.
Function ThTreeNode.LastChild: ThTreeNode;

Add_Child_Node

Appends a child, inheriting BOM support from the parent. Returns nil if the parent is deeper than level 65530; check the result before using it.
Function ThTreeNode.Add_Child_Node(CONST _Caption: String; CONST _Icon: String; CONST _Hidden: Boolean = False;
  CONST rt_Type: Byte = 0; CONST Padding_Top: Byte = 0): ThTreeNode;
N := Root.Add_Child_Node('Fasteners', 'bolt', False, 2, 0);
if N <> nil then
  N.rt_ITag[1] := 42;

Expand_Or_Collapse_All_Child_Nodes

Sets Expanded on this node and all descendants. Call ThVisualTree.Refresh to rebuild the visible rows.
Procedure ThTreeNode.Expand_Or_Collapse_All_Child_Nodes(CONST Expand: Boolean);

IS_Node_Exists

True for this node or any descendant; false for nil or an unrelated node.
Function ThTreeNode.IS_Node_Exists(CONST Node: ThTreeNode): Boolean;

ThTreeNodeFindAlgo

Matching options. The first three use HAPI string helpers after removing caption attribute tags. he_FastCompare compares the raw strings directly.
ThTreeNodeFindAlgo = (he_SameString, he_ContString, he_StartingWith, he_FastCompare);

Find_Node_By_Caption

Finds the first matching node. With Recurse=False it searches direct children only. With Recurse=True it also checks Self and deeper descendants. Returns nil when absent.
Function ThTreeNode.Find_Node_By_Caption(FindCaption: String; CONST Algo: ThTreeNodeFindAlgo;
  CONST Recurse: Boolean): ThTreeNode;
Found := Root.Find_Node_By_Caption('Fasteners', he_FastCompare, True);

Find_Node_ITag

Searches integer tags. Index 1..8 selects rt_ITag; Index 0 selects rt_Type. With Recurse=False it checks Self and direct children; with True it traverses descendants. Invalid Index returns nil.
Function ThTreeNode.Find_Node_ITag(CONST Index, FindValue: Integer; CONST Recurse: Boolean): ThTreeNode;

Find_Node_STag

Recursively searches Self and descendants by rt_STag[Index]. Index must be 1..4; otherwise the result is nil.
Function ThTreeNode.Find_Node_STag(CONST Index: Integer; CONST FindValue: String;
  CONST Algo: ThTreeNodeFindAlgo = he_FastCompare): ThTreeNode;

ThTreeNodeSortAlgo

Sort order for direct children: case-insensitive caption, callback, first integer tag, or type then caption.
ThTreeNodeSortAlgo = (he_CaptionString, he_ExtEvent, he_Node_ITag1, he_Type_Then_Caption);

ThTreeNode_SortEvent

Comparison callback. Set the Boolean result to True when Node1 must precede Node2.
ThTreeNode_SortEvent = Procedure (Node1, Node2: ThTreeNode;
  VAR Node1_Before_Node2: Boolean) OF Object;

Sort_Childs

Sorts immediate children in place. For he_ExtEvent pass _Compare, or attach the node to a visual tree with Sort_Compare assigned. Refresh the visual tree after changing order.
Procedure ThTreeNode.Sort_Childs(CONST SortWay: ThTreeNodeSortAlgo;
  CONST _Compare: ThTreeNode_SortEvent = NIL);

Move_Node_As_Child_Of_This_Node

Removes MoveNode from its old parent, appends it here, and updates descendant levels. Returns False when MoveNode has no parent or removal fails. The implementation does not check for ancestry cycles; never move a node under itself or its descendant.
Function ThTreeNode.Move_Node_As_Child_Of_This_Node(CONST MoveNode: ThTreeNode): Boolean;

Reset_Selection_Recurse

Sets Selected on Self and every descendant.
Procedure ThTreeNode.Reset_Selection_Recurse(NewValue: Boolean = False);

IS_Node_Selected_Anyhow

True if Self or any descendant is selected.
Function ThTreeNode.IS_Node_Selected_Anyhow: Boolean;

Get_Selected_Nodes_Into_Array_Of_Pointers

Returns selected node references, including Self when selected. With Only_First_Level=True, traversal stops below a selected node; unselected branches are still searched. ThNodes_List is Array OF ThTreeNode.
Function ThTreeNode.Get_Selected_Nodes_Into_Array_Of_Pointers(
  CONST Only_First_Level: Boolean): ThNodes_List;
SelectedNodes := Root.Get_Selected_Nodes_Into_Array_Of_Pointers(False);
for N in SelectedNodes do
  Write_Log_File(N.Caption);

Get_Parent_Nodes_Into_Array_Of_Pointers

Returns ancestors starting with the immediate parent and ending at the root. The current node is excluded.
Function ThTreeNode.Get_Parent_Nodes_Into_Array_Of_Pointers: ThNodes_List;

Copy_Tree_Data_To_This_Node

Copies selected node fields when Override_Self=True, then appends non-hidden source children recursively when Copy_Children=True. It does not erase existing children or copy BOM data, Selected, Expanded, or rt_Obj1.
Procedure ThTreeNode.Copy_Tree_Data_To_This_Node(CONST Source: ThTreeNode;
  CONST Override_Self: Boolean = True; CONST Copy_Children: Boolean = True);

Copy_Children_Recurse_Filter

Appends only visible source children whose captions match the word or substring filter, then searches below those matches. An empty Filter copies the visible subtree. Existing destination children remain.
Procedure ThTreeNode.Copy_Children_Recurse_Filter(CONST Source: ThTreeNode;
  CONST Filter: String);

Write_Json_To_MS

Appends a JSON representation to MS; it does not clear the stream. A positive size limit can truncate output, so do not treat a limited stream as a complete saved tree.
Procedure ThTreeNode.Write_Json_To_MS(CONST MS: ThMemoryStream; Limit_MS_Size: Integer = 0);

Debug_Write_To_Log

Writes compact JSON for this subtree to the application log.
Procedure ThTreeNode.Debug_Write_To_Log;

Node properties

Relationships and visual state. The root Parent is nil and Level is 0.
DeclarationMeaning
Property ThTreeNode.Child_Index: Integer;
Zero-based index within the parent; writable.
Property ThTreeNode.Parent: ThTreeNode;
Parent node or nil.
Property ThTreeNode.Level: Word;
Depth from root.
Property ThTreeNode.BOM: ThBillOfMaterial;
BOM data object, or nil without BOM support.
Property ThTreeNode.Expanded: Boolean;
Visual expansion flag; call Refresh after changing it.

TagStr_To_ThNumeric

Legacy XML numeric-tag decoder. Accepts a fixed-width hexadecimal byte string, or falls back to Save_Real_Filter for other lengths. Invalid hexadecimal input returns 0.
Function TagStr_To_ThNumeric(TagValue: String): Double;

Load_Tree_From_File

Loads the legacy XML format into Base. A valid call clears Base before decoding and collapses descendants afterward. False means missing file, invalid Base, or decoding failure; Base may already have been cleared.
Function Load_Tree_From_File(Base: ThTreeNode; FileName: String): Boolean;

Save_Tree_To_File

Saves Base and descendants in the legacy XML format. Passing nil or an empty filename has no effect. The routine collapses the source tree before writing.
Procedure Save_Tree_To_File(Base: ThTreeNode; FileName: String);

Copy_Tree_Into_Clipboard

Copies Base and descendants as legacy XML text to the clipboard. It collapses the source tree before serializing.
Procedure Copy_Tree_Into_Clipboard(Base: ThTreeNode);

Paste_Tree_From_Clipboard

Decodes legacy XML text from the clipboard into Base, replacing its previous data and children. Returns False if decoding fails; it does not append a new child.
Function Paste_Tree_From_Clipboard(Base: ThTreeNode): Boolean;

Load_Tree_From_Json

Reads a JSON tree from MS into Base. It clears Base before decoding and collapses descendants after success. Requires Base, MS, and a stream of at least 10 bytes.
Function Load_Tree_From_Json(Base: ThTreeNode; MS: ThMemoryStream): Boolean;

Load_Tree_From_JsonFile

Loads a JSON tree from an existing file into Base; returns False if the file is missing or decoding fails.
Function Load_Tree_From_JsonFile(Base: ThTreeNode; CONST Filename: String): Boolean;

Save_Tree_To_JsonFile

Writes the JSON tree to a UTF-8 stream and saves it to Filename. Returns the stream save result.
Function Save_Tree_To_JsonFile(Base: ThTreeNode; CONST Filename: String): Boolean;