diff options
-rw-r--r-- | examples/gui/treeviewtest/treeviewtest.lpi | 9 | ||||
-rw-r--r-- | examples/gui/treeviewtest/treeviewtest.lpr | 147 | ||||
-rw-r--r-- | src/gui/fpg_tree.pas | 56 |
3 files changed, 136 insertions, 76 deletions
diff --git a/examples/gui/treeviewtest/treeviewtest.lpi b/examples/gui/treeviewtest/treeviewtest.lpi index 8941afce..5f459bb9 100644 --- a/examples/gui/treeviewtest/treeviewtest.lpi +++ b/examples/gui/treeviewtest/treeviewtest.lpi @@ -9,7 +9,6 @@ </Flags> <SessionStorage Value="InProjectDir"/> <MainUnit Value="0"/> - <IconPath Value="./"/> <TargetFileExt Value=""/> </General> <VersionInfo> @@ -41,8 +40,14 @@ </ProjectOptions> <CompilerOptions> <Version Value="8"/> + <Parsing> + <SyntaxOptions> + <CStyleOperator Value="False"/> + </SyntaxOptions> + </Parsing> <Other> - <CustomOptions Value="-FUunits"/> + <CustomOptions Value="-FUunits +"/> <CompilerPath Value="$(CompPath)"/> </Other> </CompilerOptions> diff --git a/examples/gui/treeviewtest/treeviewtest.lpr b/examples/gui/treeviewtest/treeviewtest.lpr index f47f8c37..9c57d5f9 100644 --- a/examples/gui/treeviewtest/treeviewtest.lpr +++ b/examples/gui/treeviewtest/treeviewtest.lpr @@ -13,23 +13,28 @@ uses fpg_form, fpg_tree, fpg_checkbox, + fpg_button, fpg_imagelist; type - { TMainForm } - TMainForm = class(TfpgForm) private + {@VFD_HEAD_BEGIN: MainForm} tree: TfpgTreeView; - imagelist: TfpgImageList; cbShowImages: TfpgCheckBox; cbIndentNode: TfpgCheckBox; + btnClear: TfpgButton; + {@VFD_HEAD_END: MainForm} + FImagelist: TfpgImageList; procedure cbShowImagesChange(Sender: TObject); procedure cbIndentNodeChange(Sender: TObject); + procedure btnClearClicked(Sender: TObject); + procedure PopulateTree; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; + procedure AfterCreate; override; end; { TMainForm } @@ -39,41 +44,28 @@ begin tree.IndentNodeWithNoImage := cbIndentNode.Checked; end; -procedure TMainForm.cbShowImagesChange(Sender: TObject); +procedure TMainForm.btnClearClicked(Sender: TObject); +var + n: TfpgTreeNode; begin - tree.ShowImages := cbShowImages.Checked; + if tree.Selection <> nil then + begin + n := tree.Selection; + tree.Selection.Clear; + tree.Invalidate; + tree.Selection := n; + end; end; -constructor TMainForm.Create(AOwner: TComponent); +procedure TMainForm.PopulateTree; var n: TfpgTreeNode; - n2: TfpgTreeNode; i: integer; - s: string; + n2: TfpgTreeNode; + s: TfpgString; begin - inherited Create(AOwner); - WindowTitle := 'Treeview Test'; - WindowPosition := wpUser; - SetPosition(100, 100, 300, 230); - - // create a image list - imagelist := TfpgImageList.Create; - imagelist.AddItemFromFile(SetDirSeparators('../../../images/folder_16.bmp'), 0); - imagelist.AddItemFromFile(SetDirSeparators('../../../images/menu_preferences_16.bmp'), 1); - imagelist.Item[1].Image.CreateMaskFromSample(0, 0); - imagelist.Item[1].Image.UpdateImage; - - // create a treeview - tree := TfpgTreeView.Create(self); - tree.SetPosition(8, 8, Width-16, 200-16); - tree.Anchors := [anTop, anLeft, anRight, anBottom]; -// tree.ShowColumns := True; -// tree.TreeLineStyle := lsSolid; - tree.ScrollWheelDelta := 30; - tree.ImageList := imagelist; - tree.ShowImages := True; - n := tree.RootNode.AppendText('Node 1'); + n.ImageIndex := 0; n.AppendText('Node 1.1').ImageIndex := 1; n.AppendText('Node 1.2').ImageIndex := 1; @@ -100,27 +92,96 @@ begin n2.AppendText('Child 1').AppendText('Child 2'); n2.Collapsed := False; end; - - // create a checkbox - cbShowImages := CreateCheckBox(self, 8, 204, 'Show images'); - cbShowImages.Checked := True; - cbShowImages.OnChange := @cbShowImagesChange; - cbShowImages.Anchors := [anLeft, anBottom]; - - // create a checkbox - cbIndentNode := CreateCheckBox(self, 120, 204, 'Indent node with no image'); - cbIndentNode.Checked := True; - cbIndentNode.OnChange := @cbIndentNodeChange; - cbIndentNode.Anchors := [anLeft, anBottom]; +end; + +procedure TMainForm.cbShowImagesChange(Sender: TObject); +begin + tree.ShowImages := cbShowImages.Checked; +end; + +constructor TMainForm.Create(AOwner: TComponent); +begin + inherited Create(AOwner); + // create a image list + FImagelist := TfpgImageList.Create; + FImagelist.AddItemFromFile(SetDirSeparators('../../../images/folder_16.bmp'), 0); + FImagelist.AddItemFromFile(SetDirSeparators('../../../images/menu_preferences_16.bmp'), 1); + FImagelist.Item[1].Image.CreateMaskFromSample(0, 0); + FImagelist.Item[1].Image.UpdateImage; end; destructor TMainForm.Destroy; begin tree.ImageList := nil; - imagelist.Free; + FImagelist.Free; inherited Destroy; end; +procedure TMainForm.AfterCreate; +begin + inherited AfterCreate; + {@VFD_BODY_BEGIN: MainForm} + Name := 'MainForm'; + SetPosition(294, 174, 300, 250); + WindowTitle := 'Treeview Test'; + WindowPosition := wpScreenCenter; + + tree := TfpgTreeView.Create(self); + with tree do + begin + Name := 'tree'; + SetPosition(8, 8, 284, 184); + Anchors := [anLeft,anRight,anTop,anBottom]; + FontDesc := '#Label1'; + ScrollWheelDelta := 30; + ShowImages := True; + ImageList := FImagelist; + end; + + cbShowImages := TfpgCheckBox.Create(self); + with cbShowImages do + begin + Name := 'cbShowImages'; + SetPosition(8, 200, 109, 20); + Anchors := [anLeft,anBottom]; + Checked := True; + FontDesc := '#Label1'; + TabOrder := 1; + Text := 'Show images'; + OnChange := @cbShowImagesChange; + end; + + cbIndentNode := TfpgCheckBox.Create(self); + with cbIndentNode do + begin + Name := 'cbIndentNode'; + SetPosition(120, 200, 179, 20); + Anchors := [anLeft,anBottom]; + Checked := True; + FontDesc := '#Label1'; + TabOrder := 2; + Text := 'Indent node with no image'; + OnChange := @cbIndentNodeChange; + end; + + btnClear := TfpgButton.Create(self); + with btnClear do + begin + Name := 'btnClear'; + SetPosition(8, 222, 144, 23); + Text := 'Clear Selected Node'; + FontDesc := '#Label1'; + Hint := ''; + ImageName := ''; + TabOrder := 3; + OnClick := @btnClearClicked; + end; + + {@VFD_BODY_END: MainForm} + + PopulateTree; +end; + procedure MainProc; var diff --git a/src/gui/fpg_tree.pas b/src/gui/fpg_tree.pas index 21a60f2a..351c9050 100644 --- a/src/gui/fpg_tree.pas +++ b/src/gui/fpg_tree.pas @@ -98,11 +98,11 @@ type function FindSubNode(ATreeNodeFindMethod: TfpgTreeNodeFindMethod): TfpgTreeNode; overload; function GetMaxDepth: integer; function GetMaxVisibleDepth: integer; - procedure Append(aValue: TfpgTreeNode); + procedure Append(var aValue: TfpgTreeNode); procedure Clear; // remove all nodes recursively procedure Collapse; procedure Expand; - procedure Remove(aNode: TfpgTreeNode); + procedure Remove(var aNode: TfpgTreeNode); procedure UnregisterSubNode(aNode: TfpgTreeNode); // parent color settings function ParentInactSelColor: TfpgColor; @@ -361,6 +361,7 @@ destructor TfpgTreeNode.Destroy; begin if FParent <> nil then FParent.UnregisterSubNode(self); + Clear; FData := nil; FParent := nil; FNext := nil; @@ -393,7 +394,7 @@ begin end; end; -procedure TfpgTreeNode.Append(aValue: TfpgTreeNode); +procedure TfpgTreeNode.Append(var aValue: TfpgTreeNode); begin aValue.Parent := self; aValue.Next := nil; @@ -578,7 +579,7 @@ begin result := i; end; -procedure TfpgTreeNode.Remove(aNode: TfpgTreeNode); +procedure TfpgTreeNode.Remove(var aNode: TfpgTreeNode); begin if FirstSubNode = aNode then begin @@ -587,8 +588,11 @@ begin FFirstSubNode.Prev := nil; end else + begin if aNode.prev <> nil then aNode.Prev.next := aNode.next; + end; + if LastSubNode = aNode then begin FLastSubNode := aNode.prev; @@ -596,20 +600,28 @@ begin FLastSubNode.next := nil; end else + begin if aNode.next <> nil then aNode.next.prev := aNode.prev; + end; + aNode.prev := nil; aNode.next := nil; aNode.parent := nil; end; procedure TfpgTreeNode.Clear; +var + n: TfpgTreeNode; + tn: TfpgTreeNode; begin - while FirstSubNode <> nil do + n := LastSubNode; + while n <> nil do begin - if FirstSubNode.Count > 0 then - FirstSubNode.Clear; - Remove(FirstSubNode); + tn := n; + n := n.prev; + Remove(tn); + tn.Free; end; end; @@ -1081,30 +1093,8 @@ begin end; procedure TfpgTreeView.FreeAllTreeNodes; -var - n: TfpgTreeNode; - list: TList; begin - list := TList.Create; - n := RootNode.FirstSubNode; - list.Add(n); - - while n <> nil do - begin - // todo: this only frees of the first level of nodes!!!! - n := n.next; - list.Add(n); - end; - -// writeln('NodeCount = ', list.Count); - while list.Count > 0 do - begin - n := TfpgTreeNode(list.Last); - list.Remove(n); - n.Free; - end; - list.Clear; - list.Free; + RootNode.Clear; end; procedure TfpgTreeview.HandleResize(awidth, aheight: TfpgCoord); @@ -1800,9 +1790,13 @@ end; destructor TfpgTreeView.Destroy; begin if Assigned(FColumnLeft) then + begin ClearColumnLeft; + FColumnLeft.Free; + end; FFont.Free; FreeAllTreeNodes; + FRootNode.Free; inherited Destroy; end; |