1. GoDocument derived objects should be of fixed size (think of A4 sheet of paper for drawing something)
2. GoView should display whole document (no scrollbars). If GoView is smaller than the document, it should scale it down, if larger, it should center the document display and leave some borders
3. User should not be able to move diagram elements outside the fixed boundaries of GoDocument
can you please point me to a sample code, which is doing similar task, or suggest ways of do the task.
At the moment I set Size for GoDocument and declare it FixedSize - not sure what this do, and also call GoView::RescaleToFit() when a document is loaded or view resized
I tried to solve the problem myself, and this is what I've done:
1. I set size of document and declare it FixedSize=true;
2. I call this method (SetupViewBounds) whenever window hosting goView1 (Host) is resized
protected void SetupViewBounds()
{
GoDocument doc = goView1.Document;
int HostWidth = this.ClientRectangle.Width - 2 * BorderWidth;
int HostHeight = this.ClientRectangle.Height - 2 * BorderWidth;
// goview should have the same aspect ratio as the document
float DocAspectRatio = (float)doc.Size.Width / (float)doc.Size.Height;
float HostAspectRatio = (float)HostWidth / (float)HostHeight;
if (DocAspectRatio > HostAspectRatio)
{
// this means that document shape is longer that that of host
goView1.Width = HostWidth;
goView1.Height = (int)((float)(HostWidth) / DocAspectRatio);
}
else
{
// this means that document shape is taller that that of host
goView1.Height = HostHeight;
goView1.Width = (int)((float)(HostHeight) * DocAspectRatio);
}
float newscale = (float)goView1.Width / (float)doc.Size.Width;
goView1.DocScale = newscale;
// center the GoView inside the Host client rectangle
goView1.Left = BorderWidth + (Width - goView1.Width) / 2;
goView1.Top = BorderWidth + (Height - goView1.Height) / 2;
}
public override PointF SnapPoint(PointF p, GoObject obj, GoView view) { // modified from class SwimLane. See that code if you want to avoid the margins. if (obj == null) return p;
// normalize the object's Bounds as if the Location were always the Position PointF loc = obj.Location; RectangleF b = (view.Selection.Contains(obj) ? GoDocument.ComputeBounds(view.Selection, view) : obj.Bounds); b.X += (p.X - loc.X); b.Y += (p.Y - loc.Y);
// position the object so that it fits inside the sheet RectangleF r = view.Sheet.Bounds ; if (b.Right > r.Right) p.X -= (b.Right - r.Right); if (b.Left < r.Left) p.X += (r.Left - b.Left); if (b.Bottom > r.Bottom) p.Y -= (b.Bottom - r.Bottom); if (b.Top < r.Top) p.Y += r.Top - b.Top; return p; } }