Location Position

Having some trouble understanding the difference between Location and Position for nodes, I set up the following code :

private void drawCross(PointF position, Pen pen, PaintEventArgs e)
{
Point pos = new Point((int)position.X, (int)position.Y);
int delta = 10;
e.Graphics.DrawLine(pen, pos.X - delta, pos.Y, pos.X + delta, pos.Y);
e.Graphics.DrawLine(pen, pos.X, pos.Y - delta, pos.X, pos.Y + delta);
}
private void projectDiagramView_Paint(object sender, PaintEventArgs e)
{
foreach (GoObject obj in projectDiagramView.Document)
{
IGoNode node = obj as IGoNode;
if (node != null)
{
drawCross(node.GoObject.Position, Pens.Red, e);
drawCross(node.GoObject.Location, Pens.DarkGreen, e);
}
}
}

To my surprise, the two crosses were exactly at the same position, on the upper left corner of the bounding box. My nodes are derive from GoGeneralNode, which is derived from GoNode, and we can read in GoNode documentation:

<h1 style=“font-weight: normal; margin-left: 40px;” =“dtH1”>GoNode.Location Property

Assume the natural location of a node is the center of the GoObject.SelectionObject,
if it is different from the whole group itself.

In my case, the SelectionObjetc is correctly set to the icon itself, but the location seems unrelated to it. Does anybody has some explanation ?

That’s odd, because in Demo1, when you look at the properties for a GoGeneralNode, the Location is clearly different from the Top/Left position.
But you can override Location to implement get and set any way you want, as long as they are consistent with each other.

You’re right, that’s odd… I made the same test with demo1, and could see the green cross where I expected it on the GoGeneralNode. I do not want to overload Location in any way: The behaviour in demo1 is the one that I want, I just can’t get it in my program.

I checked my application, and on my node I have overriden the following functions :

  • LayoutChildren
    base.LayoutChildren(childchanged);
    if (this.Label != null && this.Icon != null)
    {
    this.Label.WrappingWidth = Math.Max(this.Icon.Width, 170);
    }

  • CreatePort

  • GetToolTip

Can any of those function have some impact on location ?

Best regards,

Well, LayoutChildren can certainly change both the group’s Position and its Location.
I’m wondering if the problem is that you are defining a Paint event handler on the view, and it is not updating areas where it drew crosses that are now invalid.
Try overriding GoObject.Paint instead, to draw those crosses. Remember that if you override Paint and if you paint beyond the Bounds, you also need to override ExpandPaintBounds to cover that area, so that GoView knows what area it needs to repaint.

That is why I posted the whole code of my LayoutChildren override. Anyway, I commented-out all my overrides, the problem was still present.

Then, I realized that, in opposition to demo1, I do not call Initialize, since I create my own type of icon manually. If I call Initialize in my code, everything goes back to normal, except of course that the icon is created twice.

I guess there is some kind of magic inside of Initialize that makes everything go right afterward. Is it possible to reproduce it myself without calling Initialize ?

Best regards,

If you are going to use a GoGeneralNode, you ought to Initialize it. But you could certainly override CreateIcon and CreateLabel to do what you want, including just returning null.

Ok, I did that, and it works now. Thanks !