Recent

Author Topic: How to center position of specific TreeView node?  (Read 289 times)

Alextp

  • Hero Member
  • *****
  • Posts: 1117
    • UVviewsoft
How to center position of specific TreeView node?
« on: August 14, 2020, 09:19:29 pm »
I found the reply for Delphi but it's not cross platform.
https://stackoverflow.com/questions/18490960/how-to-center-position-of-specific-treeview-node

So I need the Lazarus way for all OS'es.

jamie

  • Hero Member
  • *****
  • Posts: 3507
Re: How to center position of specific TreeView node?
« Reply #1 on: August 14, 2020, 11:01:35 pm »
I found the reply for Delphi but it's not cross platform.
https://stackoverflow.com/questions/18490960/how-to-center-position-of-specific-treeview-node

So I need the Lazarus way for all OS'es.

That example is a hack and I wouldn't rely on it..

If you are trying to center view a short list of nodes in a larger pan area, I would use a TScrollBox as the master background and place a TTreeview control in the center of that.

 So what you end up with is two controls, a Tscrollbox and a TreeView living within the TscrollBox.
Position the TreeView any where you wish in the scrollbox.

 Using no borders with the Treeview this should look natural.
The only true wisdom is knowing you know nothing

Blaazen

  • Hero Member
  • *****
  • Posts: 2902
  • POKE 54296,15
    • Eye-Candy Controls
Re: How to center position of specific TreeView node?
« Reply #2 on: August 14, 2020, 11:53:26 pm »
This works:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.TreeView1CustomDrawItem(Sender: TCustomTreeView; Node: TTreeNode; State: TCustomDrawState;
  2.   var DefaultDraw: Boolean);
  3. var aStr: string;
  4.     aSize: TSize;
  5.     aRect: TRect;
  6. begin
  7.   if Node.Text='Item1' then
  8.     begin
  9.       DefaultDraw:=False;
  10.       aStr:='My position';
  11.       aSize:=Sender.Canvas.TextExtent(aStr);
  12.       aRect:=Sender.ClientRect;
  13.       Sender.Canvas.TextOut((aRect.Left+aRect.Width-aSize.Width) div 2, 1*aSize.Height, aStr);
  14.     end;
  15. end;
Note that it is the second item, therefore 1*aSize.Height.
Lazarus 2.1.0 r63881 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

wp

  • Hero Member
  • *****
  • Posts: 7519
Re: How to center position of specific TreeView node?
« Reply #3 on: August 15, 2020, 12:23:28 am »
Blaazen, I think he is asking for vertical centering.

Alextp, I would do it similarly as in the stackexchange post, but calculate which node must be the tree's TopItem instead of sending scroll messages:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. var
  3.   Rsel: TRect;
  4.   ycenter: Integer;
  5.   node, newTopNode: TTreeNode;
  6.   h: Integer;
  7. begin
  8.   Rsel := TreeView1.Selected.DisplayRect(false);
  9.   h := Rsel.Bottom - Rsel.Top;
  10.   node := TreeView1.TopItem;
  11.   newTopNode := node;
  12.   ycenter := TreeView1.ClientHeight div 2;
  13.   if Rsel.Top > ycenter then begin
  14.     while (Rsel.Top > ycenter) and (node <> nil) do begin
  15.       newTopNode := node;
  16.       node := node.GetNextVisible;
  17.       dec(Rsel.Top, h);
  18.     end;
  19.   end else
  20.   begin
  21.     while (Rsel.Top < ycenter) and (node <> nil) do
  22.     begin
  23.       newTopNode := node;
  24.       node := node.GetPrevVisible;
  25.       inc(Rsel.Top, h);
  26.     end;
  27.   end;
  28.   TreeView1.TopItem := newTopNode;
  29. end;

Not your question, but do you know that there is a MakeSelectionVisible method which at least moves the selected node into view, although not in the center?
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

 

TinyPortal © 2005-2018