[Solved] Found a Bug advice needed.. lazarus / 31-LCL ---------------------------------------------------------------------- ArtLogi · 2016-06-20 14:21 Hello I found pretty severe bug on the TTreeNode function, .GetTheLastSubChild to be precise. It seems that the function is not a general solution, but effected on the tree structure, which on my eyes makes it useless at current state. The source code indicates that it should give the absolute last node of selected object, but this will not happen unfortunately. Here is a picture of the (wrong) behaviour. Same image attached to the post. (http://i.imgur.com/EVM8iJE.png) Current code is as far as I know (noob), this one: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TTreeNode.GetLastSubChild: TTreeNode;var  Node: TTreeNode;begin  Result:=GetLastChild;  if Result<>nil then begin    Node:=Result.GetLastSubChild;    if Node<>nil then      Result:=Node;  end;end; It seems to me that it do lack of a siblings loop for a self call pile. How I file a proper Bug report? Where and how and what information I should give etc. Also how I would correct it to my current compilers LCL?? So I can continue my "work" with my project. ---------------------------------------------------------------------- Ondrej Pokorny · 2016-06-20 14:43 IMO the function result is correct. It finds the last node not the deepest node. If you need the deepest node and there is no function to get it, you have to write your own. If you think it's general purpose, you can make a patch and send it to mantis so that it will be included into LCL. ---------------------------------------------------------------------- Thaddy · 2016-06-20 14:50 Yes, indeed. It is correct. You have to write a deepest node function yourself if you need it. The function is referring to last node, not deepest node. No bug. ---------------------------------------------------------------------- ArtLogi · 2016-06-20 15:04 Ok, thank you both I start  to see the logic of it when looking at it as Last <> Deepest. It would have been clear if you speak LastSubNode LowestSubNode not the LastSubChild as levels or bloodline. Well I need to brew something then. Unfortunately my knowledge level of LCL, Laz etc. is on the state that I can not share a version on mantis. Mmm.. Reminds me why I newer liked these "O"s prefixes on languages in the past. Here is a quick and dirty function to get the branches deepest node from the tree, if someone in the future is strugling with the same thing and happens to find this thread. If someone want to make a patch to the mantis (what ever it does mean actually) then feel free to do so, function is free as free beer without strings attached nor guarantee given. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Function GetBranchesDeepestNode(Node: TTreeNode): TTreenode ;  var    WorkNode : TTreeNode;begin  (*In case of double or nth branch ends with same deepnes the "Last" aka lowest node on those equal nodes will be picked*) Result := node; WorkNode:= Node.GetLastSubChild; While (WorkNode <> nil) and (WorkNode <> Node) Do Begin    If Result.level < WorkNode.level Then Begin       Result := WorkNode;       End;    WorkNode := WorkNode.GetPrev;    end;end; ---------------------------------------------------------------------- molly · 2016-06-20 19:18 Quote from: ArtLogi on June 20, 2016, 05:04:17 pmIf someone want to make a patch to the mantis (what ever it does mean actually) then feel free to do so, function is free as free beer without strings attached nor guarantee given. I realize that the subject might perhaps be a little too much for your (current) taste (in which case, please ignore) but, that sounds like a perfect job for a class helper. Please consider the following: MainUnit: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+}{$DEFINE USEHELPERS} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,  StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    Label1: TLabel;    Label2: TLabel;    TreeView1: TTreeView;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);  private    { private declarations }  public    { public declarations }  end; var  Form1: TForm1; implementation {$R *.lfm} uses  {$IFNDEF USEHELPERS}  DOM, xmlread,  {$ENDIF}  MyTreeHelpers; const  XMLFileName = 'project1.lpi';  { TForm1 }  {$IFNDEF USEHELPERS}procedure TForm1.Button1Click(Sender: TObject);var  XML  : TXMLDocument;begin  ReadXMLFile(XML, XMLFileName);  XML2Tree(XML, TreeView1);end; procedure TForm1.Button2Click(Sender: TObject);var  Node : TTreenode;begin  if assigned(TreeView1.Selected)  then Node := TreeView1.Selected  else Node := TreeView1.Items[0];   Node := GetBranchesDeepestNode(Node);  Label1.Caption := IntToStr(Node.AbsoluteIndex);  Label2.Caption := Node.GetTextPath;end; {$ELSE} procedure TForm1.Button1Click(Sender: TObject);begin  TreeView1.LoadFromXMLFile(XMLFileName);end;  procedure TForm1.Button2Click(Sender: TObject);var  Node : TTreenode;begin  if assigned(TreeView1.Selected)  then Node := TreeView1.Selected  else Node := TreeView1.Items[0];   Node := Node.GetBranchesDeepestNode;   Label1.Caption := IntToStr(Node.AbsoluteIndex);  Label2.Caption := Node.GetTextPath;end; {$ENDIF}end.  And the helper unit: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit MyTreeHelpers; {$mode objfpc}{$H+} {$DEFINE USEHELPERS} interface uses  DOM, xmlread, Comctrls;   {$IFDEF USEHELPERS}  Type   { TTreeViewHelper }   TTreeViewHelper = class helper for TTreeView    Procedure LoadFromXMLFile(Filename: String);  end;   { TTreeNodeHelper }   TTreeNodeHelper = class helper for TTreeNode    Function GetBranchesDeepestNode: TTreeNode;  end;   {$ELSE}   procedure XML2Tree(XMLDoc:TXMLDocument; TreeView:TTreeView);  Function  GetBranchesDeepestNode(Node: TTreeNode): TTreenode ;   {$ENDIF}  implementation uses  SysUtils;  // http://wiki.freepascal.org/XML_Tutorial#Populating_a_TreeView_with_XML  procedure XML2Tree(XMLDoc:TXMLDocument; TreeView:TTreeView);  // Local function that outputs all node attributes as a string  function GetNodeAttributesAsString(pNode: TDOMNode):string;  var i: integer;  begin    Result:='';    if pNode.HasAttributes then      for i := 0 to pNode.Attributes.Length -1 do        with pNode.Attributes[i] do        Result := Result + format(' %s="%s"', [NodeName, NodeValue]);    // Remove leading and trailing spaces    Result:= Trim(Result);  end;   // Recursive function to process a node and all its child nodes  procedure ParseXML(Node:TDOMNode; TreeNode: TTreeNode);  begin    // Exit procedure if no more nodes to process    if Node = nil then Exit;     // Add node to TreeView    TreeNode := TreeView.Items.AddChild    (      TreeNode,      Trim(Node.NodeName + ' ' + GetNodeAttributesAsString(Node) + Node.NodeValue)    );     // Process all child nodes    Node := Node.FirstChild;    while Node <> Nil do    begin      ParseXML(Node, TreeNode);      Node := Node.NextSibling;    end;  end;begin  TreeView.Items.Clear;  ParseXML(XMLDoc.DocumentElement,nil);end;  // http://forum.lazarus.fre epascal.org/index.php/topic,33065.msg213548.html#msg213548  function GetBranchesDeepestNode(Node: TTreeNode): TTreenode;var  WorkNode : TTreeNode;begin  (*In case of double or nth branch ends with same deepnes the "Last" aka lowest node on those equal nodes will be picked*)  Result := node;  WorkNode:= Node.GetLastSubChild;   While (WorkNode <> nil) and (WorkNode <> Node) Do  Begin    If Result.level < WorkNode.level Then    Begin      Result := WorkNode;    End;    WorkNode := WorkNode.GetPrev;  end;end; {$IFDEF USEHELPERS} { TTreeNodeHelper } function TTreeNodeHelper.GetBranchesDeepestNode: TTreeNode;var  Node: TTreeNode;begin  Node := Self;  result := MyTreeHelpers.GetBranchesDeepestNode(Node);end; { TTreeViewHelper } procedure TTreeViewHelper.LoadFromXMLFile(Filename: String);var  XML  : TXMLDocument;begin  ReadXMLFile(XML, FileName);  XML2Tree(XML, Self);end; {$ENDIF}end.  Just to make sure: i am aware this is bad coding style but this example is meant to show (in parallel) how easy it can be to create your own class helper by using existing routines such as OP presented. The stand alone functions should really be part of the class helper itself (as a private function). With the method as shown you are able to 'extend' existing classes the way you want them to without the need to bother someone else with your (explicit) wishes. Because of the helper class it looks like your newly added functionality is part of the original class itself  ;-) You can comment/uncomment the DEFINE (in both units at same time) in order to switch back 'n' forth between using the class helpers or using the stand-alone functions. ---------------------------------------------------------------------- ArtLogi · 2016-06-20 22:15 Thx molly, I'll look that up tomorrow. It looks extremely usefull.