using System;
using System.Drawing;

using MindFusion.Diagramming;
using MindFusion.Diagramming.Layout;


public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
		if (!IsPostBack)
		{
			Diagram diagram = diagView.Diagram;

			ShapeNode root = diagram.Factory.CreateShapeNode(10, 10, 24, 14);
			root.Shape = Shapes.RoundRect;
			root.Brush = new MindFusion.Drawing.SolidBrush(Color.White);
			root.Text = "root";
		}
    }

	protected void btnLayout_Click(object sender, EventArgs e)
	{
		Diagram diagram = diagView.Diagram;

		// apply radial tree layout
		TreeLayout layout = new TreeLayout();
		layout.Type = TreeLayoutType.Radial;
		layout.LayoutNode = new LayoutNode((node, oldBounds) =>
			{
				var depth = GetDepth(node);
				node.Brush = new MindFusion.Drawing.SolidBrush(Color.FromArgb(
					Math.Max(0, 255 - depth * 34),
					Math.Max(0, 255 - depth * 18),
					Math.Max(0, 255 - depth * 6)));
			});
		layout.Arrange(diagram);

		// make the diagram big enough to contain the items
		RectangleF bounds = diagram.Bounds;
		diagram.ResizeToFitItems(5);
		diagram.Bounds = RectangleF.Union(bounds, diagram.Bounds);
	}

	private int GetDepth(DiagramNode node)
	{
		if (node.IncomingLinks.Count == 0)
			return 0;

		return 1 + GetDepth(node.IncomingLinks[0].Origin);
	}

	protected void btnExportPdf_Click(object sender, EventArgs e)
	{
		MindFusion.Diagramming.Export.PdfExporter pdf =
			new MindFusion.Diagramming.Export.PdfExporter();

		string path = Server.MapPath(@"~\PDF\") + Session.SessionID + ".pdf";
		pdf.Export(diagView.Diagram, path);

		pdfURL.Value = "PDF/" + Session.SessionID + ".pdf";
	}
}