Recent

Author Topic: Scroll the TreeView While Dragging and Dropping a Node?  (Read 6469 times)

NelsonN

  • Jr. Member
  • **
  • Posts: 69
    • Beam Me Up!
Scroll the TreeView While Dragging and Dropping a Node?
« on: April 02, 2011, 04:26:53 am »
Anyone know how to scroll the TreeView while dragging and dropping a node?
Lazarus trunk / FPC 3.0.4 / 32-bit and 64-bit with Windows 10.

NelsonN

  • Jr. Member
  • **
  • Posts: 69
    • Beam Me Up!
Re: Scroll the TreeView While Dragging and Dropping a Node?
« Reply #1 on: April 02, 2011, 07:31:41 pm »
For those of you who might not know how to get this done, as I did, I came up with this after finding some Delphi code using handlers, I removed the handlers and used MakeVisible as was suggested for other components on this forum.

I tested this on Windows, Linux (GTK2), and MAC OS X (Carbon). The MAC version doesn't do so well on the scroll down. Also, for Windows and Linux, on the scroll down, I had to reverse the operator "<", to a greater than ">", otherwise it wouldn't even move.

If anyone has a better way of doing this, please let me know.


Code: [Select]
procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
var
  cNode, sNode : TTreeNode;
begin
  { Some Other Code I Have Here ... }

  //Memo1.Clear;
  //Memo1.Lines.Append('X, Y : ' + IntToStr(X) + ', ' + IntToStr(Y));

  { I Added This For Scrolling While Dragging }

  //Memo1.Lines.Add(IntToStr(TTreeView(Sender).Height - y));

  { On the upper edge - should scroll up }
  if (y < TTreeView(Sender).DefaultItemHeight) then begin
    cNode := TTreeView(Sender).GetNodeAt(x,y);
    if cNode <> nil then begin
      sNode := cNode.GetPrev;
      if sNode <> nil then sNode.MakeVisible;
    end;
  end
  { On the lower edge - should scroll down }
  else
  {$ifdef Darwin}
    if (TTreeView(Sender).Height - y < TTreeView(Sender).DefaultItemHeight) then begin
  {$else}
    if (TTreeView(Sender).Height - y > TTreeView(Sender).DefaultItemHeight) then begin
  {$endif}
    cNode := TTreeView(Sender).GetNodeAt(x,y);
    if cNode <> nil then begin
      if cNode <> TTreeView(Sender).Items[TTreeView(Sender).Items.Count-1] then begin
        sNode := cNode.GetNext;
        if sNode <> nil then sNode.MakeVisible;
      end;
    end;
  end;
end;
Lazarus trunk / FPC 3.0.4 / 32-bit and 64-bit with Windows 10.

 

TinyPortal © 2005-2018