Quantcast
Channel: Planet Object Pascal
Viewing all articles
Browse latest Browse all 1725

Žarko Gajić: Quick Tip: Locate / ScrollIntoView Focused Node In Virtual Tree View

$
0
0

virrtualtreeview-locatefocusedI’m a huge fan of one big gem in the Delphi third party controls arena: Virtual Tree View.

Whatever issue I had to solve in my applications harvesting the power of the TVirtualStringTree, the component had the answer, either through vast amount of properties or through nicely exposed events.

The only solution I was not able to find straight forward, or out of the box, is how to have a visual indication that the currently focused node is not visible in the tree. Not “not visible” as hidden but not visible as currently not in view.

The Virtual TreeView, initially being developed by Mike Lischke and now being maintained as an open source project on Google Code is a must-use control if you are up to working with whatever you could call “nodes”.

Again, the solution is rather simple, one only needs to know what combination of tree event + tree method + node property is required to accomplish the above.

Therefore, here’s a simple tree having a few nodes. At one time several nodes can be selected (if allowed using the corresponding property), but only one node at one time can be focused.

Having one node focused I needed to have a button “Locate Focused Node” made visible or hidden: visible if the focused node is “out of the view”.

To make this work, handle the OnScroll event as (the name of the TVirtualStringtree control on my form is “tree”):

//handles OnScroll event
procedure TvstForm.treeScroll(
  Sender: TBaseVirtualTree; 
  DeltaX, DeltaY: Integer);
var
  nr : TRect;
begin
  inherited;

  if Assigned(sender.FocusedNode) then
    nr := sender.GetDisplayRect(sender.FocusedNode, 0, false);

  btnLocateFocused.Enabled := (nr.Top < 0) OR
                              (nr.Bottom > sender.ClientHeight);
end;

The above code will make my “locate focused node” button enabled or disabled depending on the visibility of the focused node.

When the focused node goes out of the view area of the tree, the button gets enabled.

Clicking the button scrolls the tree so that the given node is “back” in the client area.

procedure TvstForm.btnLocateFocusedClick(Sender: TObject);
begin
  //get the focused node in the center of the tree!
  tree.ScrollIntoView(tree.FocusedNode, true);
end;

Note that if the node was made invisible using tree.IsVisible[node], the GetDisplayRect method would return a zero height/witdh rect structure and the button will be disabled.

That’s it.


Viewing all articles
Browse latest Browse all 1725

Trending Articles