How to code...

TeeTree VCL for Borland Delphi and C++ Builder.
Post Reply
Lenfors
Newbie
Newbie
Posts: 49
Joined: Thu Sep 20, 2007 12:00 am

How to code...

Post by Lenfors » Tue Nov 03, 2009 3:33 pm

Hello!

Just started with TeeTree and need some guidance how to approach the following problems:

I want to use drag and drop between nodes, but I have to be able to control wich nodes are allowed to be dropped on wich nodes! When I enable the DragAndDrop.Automatic it permitts drop on any node without fiering the OnDragOver event. Do I have to manage it all manually, and if, do you have any sample code?

In the TTree.OnClickShape event i want to add children to the clicked node and make the first one of the added children the "selected" node. I tried with TreeNodeShape.Selected := True; (TreeNodeShape beeing the added child) but it's still the clicked node that becomes the selected one.

Regards, Mikael

Yeray
Site Admin
Site Admin
Posts: 9613
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: How to code...

Post by Yeray » Thu Nov 05, 2009 4:30 pm

Hi Michael,
Lenfors wrote:I want to use drag and drop between nodes, but I have to be able to control wich nodes are allowed to be dropped on wich nodes! When I enable the DragAndDrop.Automatic it permitts drop on any node without fiering the OnDragOver event. Do I have to manage it all manually, and if, do you have any sample code?
I think you should control manually the drag and drop process. Something like following:

Code: Select all

var dragginShape: TTreeNodeShape;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Tree1.DragAndDrop.Automatic:=false;
end;

procedure TForm1.Tree1AfterDraw(Sender: TObject);
begin
  if dragginShape <> nil then
    Caption:='dragging';
end;

procedure TForm1.Tree1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  dragginShape:=Tree1.ClickedShape(X,Y);
end;

procedure TForm1.Tree1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var parentShape: TTreeNodeShape;
    connection: TTreeConnection;

    // Returns True when AShape can be a child node of AParent
  Function CanBeParentOf(AParent,AShape:TTreeNodeShape):Boolean;
  begin
    result:=False;
    if Assigned(AParent) and Assigned(AShape) and
       (AParent<>AShape) and (AParent<>AShape.Parent) then
    begin
      While Assigned(AParent.Parent) do
      if AParent.Parent=AShape then Exit
                               else AParent:=AParent.Parent;
      result:=True;
    end;
  end;
begin
  if dragginShape <> nil then
  begin
    parentShape:=Tree1.ClickedShape(X,Y);
    if parentShape <> nil then
    begin
      if CanBeParentOf(parentShape,dragginShape) then
      begin
        parentShape.AddConnection(dragginShape);
        dragginShape.Parent:=parentShape;
      end;
    end;
  end;
end;
Lenfors wrote:In the TTree.OnClickShape event i want to add children to the clicked node and make the first one of the added children the "selected" node. I tried with TreeNodeShape.Selected := True; (TreeNodeShape beeing the added child) but it's still the clicked node that becomes the selected one.
You can try using similar code as following to force a node selection at OnMouseDown event:

Code: Select all

  TreeNodeShape1.Selected:=true;
  Tree1.CancelMouse:=true;
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Post Reply