/// <summary>
 /// Zooms the map element out by 10%
 /// </summary>
 public static void ZoomOutMap(LayoutMap lm)
 {
     double tenPerWidth = (lm.Envelope.Maximum.X - lm.Envelope.Minimum.X) / 20;
     double tenPerHeight = (lm.Envelope.Maximum.Y - lm.Envelope.Minimum.Y) / 20;
     Geometries.Envelope envl = new Geometries.Envelope();
     envl.Minimum.X = lm.Envelope.Minimum.X - tenPerWidth;
     envl.Minimum.Y = lm.Envelope.Minimum.Y - tenPerHeight;
     envl.Maximum.X = lm.Envelope.Maximum.X + tenPerWidth;
     envl.Maximum.Y = lm.Envelope.Maximum.Y + tenPerWidth;
     lm.Envelope = envl;
 }
        /// <summary>
        /// Loads the selected layoutfile
        /// </summary>
        /// <param name="filename">The layout file to load</param>
        public void LoadLayout(string filename)
        {
            //Open the model xml document
            XmlDocument layoutXmlDoc = new XmlDocument();
            layoutXmlDoc.Load(filename);
            XmlElement root = layoutXmlDoc.DocumentElement;

            List<LayoutElement> loadList = new List<LayoutElement>();

            if (root != null)
            {
                if (root.Name == "MapWindowLayout")
                {
                    //This creates instances of all the elements 
                    XmlNode child = root.LastChild;
                    while (child != null)
                    {
                        LayoutElement newLe = null;
                        switch (child.ChildNodes[0].Name)
                        {
                            case "Bitmap":
                                newLe = new LayoutBitmap();
                                break;
                            case "Legend":
                                newLe = CreateLegendElement();
                                break;
                            case "Map":
                                newLe = CreateMapElement();
                                break;
                            case "NorthArrow":
                                newLe = new LayoutNorthArrow();
                                break;
                            case "Rectangle":
                                newLe = new LayoutRectangle();
                                break;
                            case "ScaleBar":
                                newLe = CreateScaleBarElement();
                                break;
                            case "Text":
                                newLe = new LayoutText();
                                break;
                        }
                        if (newLe != null)
                        {
                            newLe.Name = child.Attributes["Name"].Value;
                            newLe.Invalidated += LeInvalidated;
                            newLe.Rectangle = new RectangleF(float.Parse(child.Attributes["RectangleX"].Value, System.Globalization.CultureInfo.InvariantCulture), float.Parse(child.Attributes["RectangleY"].Value, System.Globalization.CultureInfo.InvariantCulture), float.Parse(child.Attributes["RectangleWidth"].Value, System.Globalization.CultureInfo.InvariantCulture), float.Parse(child.Attributes["RectangleHeight"].Value, System.Globalization.CultureInfo.InvariantCulture));
                            newLe.ResizeStyle = (ResizeStyle)Enum.Parse(typeof(ResizeStyle), child.Attributes["ResizeStyle"].Value);
                            loadList.Insert(0,newLe);
                        }
                        child = child.PreviousSibling;
                    }

                    //Since some of the elements may be dependant on elements already being added we add their other properties after we add them all                
                    child = root.LastChild;
                    for (int i = loadList.Count - 1; i >= 0; i--)
                    {
                        if (child != null)
                        {
                            XmlNode innerChild = child.ChildNodes[0];
                            if (loadList[i] is LayoutBitmap)
                            {
                                LayoutBitmap lb = loadList[i] as LayoutBitmap;
                                if (lb != null)
                                {
                                    lb.Filename = innerChild.Attributes["Filename"].Value;
                                    lb.PreserveAspectRatio = Convert.ToBoolean(innerChild.Attributes["PreserveAspectRatio"].Value);
                                    lb.Draft = Convert.ToBoolean(innerChild.Attributes["Draft"].Value);
                                }
                            }
                            else if (loadList[i] is LayoutLegend)
                            {
                                LayoutLegend ll = loadList[i] as LayoutLegend;
                                if (ll != null)
                                {
                                    ll.LayoutControl = this;
                                    ll.TextHint = (System.Drawing.Text.TextRenderingHint)Enum.Parse(typeof(System.Drawing.Text.TextRenderingHint), innerChild.Attributes["TextHint"].Value);
                                    ll.Color = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromInvariantString(innerChild.Attributes["Color"].Value);
                                    ll.Font = (Font)TypeDescriptor.GetConverter(typeof(Font)).ConvertFromString(innerChild.Attributes["Font"].Value);
                                }
                                int mapIndex = Convert.ToInt32(innerChild.Attributes["Map"].Value);
                                if (mapIndex >= 0)
                                    if (ll != null) ll.Map = loadList[mapIndex] as LayoutMap;
                                string layStr = innerChild.Attributes["Layers"].Value;
                                List<int> layers = new List<int>();
                                while (layStr.EndsWith("|"))
                                {
                                    layStr = layStr.TrimEnd("|".ToCharArray());
                                    layers.Add((int)TypeDescriptor.GetConverter(typeof(int)).ConvertFromString(layStr.Substring(layStr.LastIndexOf("|") + 1)));
                                    layStr = layStr.Substring(0, layStr.LastIndexOf("|") + 1);
                                }
                                if (ll != null)
                                {
                                    ll.NumColumns = (int)TypeDescriptor.GetConverter(typeof(int)).ConvertFromString(innerChild.Attributes["NumColumns"].Value);
                                    ll.Layers = layers;
                                }
                            }
                            else if (loadList[i] is LayoutMap)
                            {
                                LayoutMap lm = loadList[i] as LayoutMap;
                                Geometries.Envelope env = new Geometries.Envelope();
                                env.Minimum.X = (double)TypeDescriptor.GetConverter(typeof(double)).ConvertFromInvariantString(innerChild.Attributes["EnvelopeXmin"].Value);
                                env.Minimum.Y = (double)TypeDescriptor.GetConverter(typeof(double)).ConvertFromInvariantString(innerChild.Attributes["EnvelopeYmin"].Value);
                                env.Maximum.X = (double)TypeDescriptor.GetConverter(typeof(double)).ConvertFromInvariantString(innerChild.Attributes["EnvelopeXmax"].Value);
                                env.Maximum.Y = (double)TypeDescriptor.GetConverter(typeof(double)).ConvertFromInvariantString(innerChild.Attributes["EnvelopeYmax"].Value);
                                if (lm != null) lm.Envelope = env;
                            }
                            else if (loadList[i] is LayoutNorthArrow)
                            {
                                LayoutNorthArrow na = loadList[i] as LayoutNorthArrow;
                                if (na != null)
                                {
                                    na.Color = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromInvariantString(innerChild.Attributes["Color"].Value);
                                    na.NorthArrowStyle = (NorthArrowStyle)Enum.Parse(typeof(NorthArrowStyle), innerChild.Attributes["Style"].Value);
                                    if (innerChild.Attributes["Rotation"] != null)
                                    {
                                        na.Rotation = (float)TypeDescriptor.GetConverter(typeof(float)).ConvertFromInvariantString(innerChild.Attributes["Rotation"].Value);
                                    }
                                }
                            }
                            else if (loadList[i] is LayoutRectangle)
                            {
                                LayoutRectangle lr = loadList[i] as LayoutRectangle;
                                if (lr != null)
                                {
                                    lr.Color = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString(innerChild.Attributes["Color"].Value);
                                    lr.BackColor = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString(innerChild.Attributes["BackColor"].Value);
                                    lr.OutlineWidth = Convert.ToInt32(innerChild.Attributes["OutlineWidth"].Value);
                                }
                            }
                            else if (loadList[i] is LayoutScaleBar)
                            {
                                LayoutScaleBar lsc = loadList[i] as LayoutScaleBar;
                                if (lsc != null)
                                {
                                    lsc.LayoutControl = this;
                                    lsc.TextHint = (System.Drawing.Text.TextRenderingHint)Enum.Parse(typeof(System.Drawing.Text.TextRenderingHint), innerChild.Attributes["TextHint"].Value);
                                    lsc.Color = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString(innerChild.Attributes["Color"].Value);
                                    lsc.Font = (Font)TypeDescriptor.GetConverter(typeof(Font)).ConvertFromString(innerChild.Attributes["Font"].Value);
                                    lsc.BreakBeforeZero = Convert.ToBoolean(innerChild.Attributes["BreakBeforeZero"].Value);
                                    lsc.NumberOfBreaks = Convert.ToInt32(innerChild.Attributes["NumberOfBreaks"].Value);
                                    lsc.Unit = (ScaleBarUnits)Enum.Parse(typeof(ScaleBarUnits), innerChild.Attributes["Unit"].Value);
                                    lsc.UnitText = innerChild.Attributes["UnitText"].Value;
                                }
                                int mapIndex = Convert.ToInt32(innerChild.Attributes["Map"].Value);
                                if (mapIndex >= 0)
                                    if (lsc != null) lsc.Map = loadList[mapIndex] as LayoutMap;
                            }
                            else if (loadList[i] is LayoutText)
                            {
                                LayoutText lt = loadList[i] as LayoutText;
                                if (lt != null)
                                {
                                    lt.TextHint = (System.Drawing.Text.TextRenderingHint)Enum.Parse(typeof(System.Drawing.Text.TextRenderingHint), innerChild.Attributes["TextHint"].Value);
                                    lt.Color = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString(innerChild.Attributes["Color"].Value);
                                    lt.Font = (Font)TypeDescriptor.GetConverter(typeof(Font)).ConvertFromString(innerChild.Attributes["Font"].Value);
                                    lt.ContentAlignment = (ContentAlignment)TypeDescriptor.GetConverter(typeof(ContentAlignment)).ConvertFromString(innerChild.Attributes["ContentAlignment"].Value);
                                    lt.Text = innerChild.Attributes["Text"].Value;
                                }
                            }
                        }
                        if (child != null) child = child.PreviousSibling;
                    }
                    _layoutElements.Clear();
                    _selectedLayoutElements.Clear();
                    _layoutElements.InsertRange(0, loadList);
                    Filename = filename;
                    Invalidate();
                    OnElementsChanged(null);
                }
            }
        }