Page 1 of 1

How to code...

Posted: Tue Nov 03, 2009 3:33 pm
by 10546756
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

Re: How to code...

Posted: Thu Nov 05, 2009 4:30 pm
by yeray
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;