ClientLauncher.UISettings.SetupMainForm C# (CSharp) Method

SetupMainForm() public method

public SetupMainForm ( System theBackgroundPath, Grid grdTitle, Grid grdWindowButtons, Grid grdStatusArea, System.Windows.Controls.Border brdServerControls, string strCurrentPallette, ScaleTransform formScale, System.Windows.Controls.Border brdMessageBox ) : List
theBackgroundPath System
grdTitle System.Windows.Controls.Grid
grdWindowButtons System.Windows.Controls.Grid
grdStatusArea System.Windows.Controls.Grid
brdServerControls System.Windows.Controls.Border
strCurrentPallette string
formScale System.Windows.Media.ScaleTransform
brdMessageBox System.Windows.Controls.Border
return List
        public List<string> SetupMainForm(System.Windows.Shapes.Path theBackgroundPath, Grid grdTitle, Grid grdWindowButtons, Grid grdStatusArea, Border brdServerControls, string strCurrentPallette, ScaleTransform formScale, Border brdMessageBox)
        {
            List<string> lstPaths = new List<string>();
            if (theXmlSkin != null)
            {
                //try and read the MainForm node
                XElement mainForm = theXmlSkin.Element("MainForm");

                if (mainForm == null)
                {
                    MessageBox.Show("Settings file is missing it's MainForm element\r\nUnable to continue");
                    Application.Current.Shutdown();
                }
                else
                {

                    //scale it
                    if (mainForm.Element("FormZoom") != null)
                    {
                        formScale.ScaleX = Convert.ToDouble(mainForm.Element("FormZoom").Element("x").Value, ni);
                        formScale.ScaleY = Convert.ToDouble(mainForm.Element("FormZoom").Element("y").Value, ni);
                    }

                    //set up the corner radiuseseses
                    if (mainForm.Element("RoundedCornerRadius") != null)
                    {
                        double dblCornerBase = Convert.ToDouble(mainForm.Element("RoundedCornerRadius").Value, ni);
                        App.Current.Resources["BigCornerRadius"] = new CornerRadius(dblCornerBase);
                        App.Current.Resources["SmallCornerRadius"] = new CornerRadius(dblCornerBase / 2.0);
                        App.Current.Resources["TinyCornerRadius"] = new CornerRadius(dblCornerBase / 5.0);
                        App.Current.Resources["PrefsBorder"] = new CornerRadius(0, dblCornerBase, dblCornerBase, dblCornerBase);
                        App.Current.Resources["PrefsTabBorder"] = new CornerRadius(dblCornerBase / 5.0, dblCornerBase / 5.0, 0, 0);
                        App.Current.Resources["RectangleRadius"] = dblCornerBase;
                    }
                    else
                    {
                        //reset
                        CornerRadius cnrBase = new CornerRadius(0);
                        App.Current.Resources["BigCornerRadius"] = cnrBase;
                        App.Current.Resources["SmallCornerRadius"] = cnrBase;
                        App.Current.Resources["TinyCornerRadius"] = cnrBase;
                        App.Current.Resources["PrefsBorder"] = cnrBase;
                        App.Current.Resources["PrefsTabBorder"] = cnrBase;
                        App.Current.Resources["RectangleRadius"] = 0;
                    }

                    //make a ColorConverter to manage the string-to-color handling
                    ColorConverter cv = new ColorConverter();

                    //What's the default palette?
                    string strDefaultPalette = "Standard";

                    XElement defaultPalette = mainForm.Element("DefaultPalette");
                    if (defaultPalette != null)
                    {
                        strDefaultPalette = defaultPalette.Value;
                    }

                    //do we have one from the cache maybe?
                    if (!String.IsNullOrEmpty(strCurrentPallette))
                    {
                        strDefaultPalette = strCurrentPallette;
                    }

                    //what type of server selector?
                    if (App.Current.Resources["ServerType"] == null)
                    {
                        App.Current.Resources.Add("ServerType", "Textual");
                    }
                    else
                    {
                        App.Current.Resources["ServerType"] = "Textual";
                    }
                    if (mainForm.Element("ServerType") != null)
                    {
                        App.Current.Resources["ServerType"] = mainForm.Element("ServerType").Value;
                    }

                    //Load it from the list
                    List<Pallette> lstPalletes = App.Current.Resources["UIPalettes"] as List<Pallette>;
                    var somePalettes = lstPalletes.Where(pal => pal.Name.Equals(strDefaultPalette, StringComparison.InvariantCultureIgnoreCase));
                    if (somePalettes.Count() > 0)
                    {
                        Pallette theDefaultPalette = somePalettes.First();
                        SetNewPallette(theDefaultPalette);
                    }

                    //and work out what type of MainForm is required
                    XElement formType = mainForm.Element("BackgroundType");

                    if (formType == null)
                    {
                        MessageBox.Show("Can't determine the BackgroundType\r\nUnable to continue");
                    }
                    else
                    {
                        switch (formType.Value.ToLowerInvariant())
                        {
                            case "image":

                                //here's a default one in case it all goes wrong
                                BitmapImage bmImage = new BitmapImage();
                                bmImage.BeginInit();
                                bmImage.UriSource = new Uri("pack://application:,,,/Images/background.jpg", UriKind.RelativeOrAbsolute);
                                bmImage.EndInit();
                                //do we have more than one image?
                                if (mainForm.Elements("BackgroundImage").Count() > 1)
                                {
                                    var imagePaths = from bi in mainForm.Elements("BackgroundImage")
                                                     select bi.Value;

                                    List<string> lstTempPaths = new List<string>();

                                    lstTempPaths = imagePaths.ToList();

                                    //do they all exists?
                                    foreach (string strImage in lstTempPaths)
                                    {
                                        if (File.Exists(strRootPath + strImage))
                                        {
                                            lstPaths.Add(strImage);
                                        }
                                    }

                                    //Load the first image
                                    ImageBrush bgImage = new ImageBrush();
                                    if (lstPaths.Count > 0)
                                    {
                                        bgImage.ImageSource = new BitmapImage(new Uri(strRootPath + lstPaths[0], UriKind.Relative));
                                    }
                                    else
                                    {
                                        bgImage.ImageSource = bmImage;
                                    }

                                    theBackgroundPath.Fill = bgImage;
                                }
                                else
                                {
                                    //Load the specified image
                                    ImageBrush bgImage = new ImageBrush();
                                    if (File.Exists(strRootPath + mainForm.Element("BackgroundImage").Value))
                                    {
                                        bgImage.ImageSource = new BitmapImage(new Uri(strRootPath + mainForm.Element("BackgroundImage").Value, UriKind.Relative));

                                    }
                                    else
                                    {
                                        bgImage.ImageSource = bmImage;
                                    }

                                    theBackgroundPath.Fill = bgImage;
                                }
                                break;
                            case "path":
                                //load the path geometry
                                theBackgroundPath.Data = Geometry.Parse(mainForm.Element("PathData").Value);
                                brdMessageBox.Clip = Geometry.Parse(mainForm.Element("MsgBoxPathData").Value);
                                if (mainForm.Element("BorderThickness") != null)
                                {
                                    theBackgroundPath.StrokeThickness = Convert.ToDouble(mainForm.Element("BorderThickness").Value, ni);
                                }
                                if (mainForm.Element("BackgroundColor") != null)
                                {
                                    theBackgroundPath.Fill = new SolidColorBrush((Color)cv.ConvertFrom(mainForm.Element("BackgroundColor").Value));
                                }

                                if (mainForm.Element("TitlePath") != null)
                                {
                                    grdTitle.Clip = Geometry.Parse(mainForm.Element("TitlePath").Value);
                                }

                                if (mainForm.Element("WindowControlsPath") != null)
                                {
                                    grdWindowButtons.Clip = Geometry.Parse(mainForm.Element("WindowControlsPath").Value);
                                    grdWindowButtons.Margin = new Thickness(grdWindowButtons.Margin.Left, grdTitle.ActualHeight + theBackgroundPath.StrokeThickness, grdWindowButtons.Margin.Right, grdWindowButtons.Margin.Bottom);
                                }

                                if (mainForm.Element("StatusPath") != null)
                                {
                                    grdStatusArea.Clip = Geometry.Parse(mainForm.Element("StatusPath").Value);
                                }

                                if (mainForm.Element("StatusBorderThickness") != null)
                                {
                                    //make a new thickness
                                    Thickness thck = new Thickness();

                                    if (mainForm.Element("StatusBorderThickness").Element("Top") != null)
                                    {
                                        thck.Top = Convert.ToInt32(mainForm.Element("StatusBorderThickness").Element("Top").Value, ni);
                                    }

                                    if (mainForm.Element("StatusBorderThickness").Element("Bottom") != null)
                                    {
                                        thck.Bottom = Convert.ToInt32(mainForm.Element("StatusBorderThickness").Element("Bottom").Value, ni);
                                    }

                                    if (mainForm.Element("StatusBorderThickness").Element("Right") != null)
                                    {
                                        thck.Right = Convert.ToInt32(mainForm.Element("StatusBorderThickness").Element("Right").Value, ni);
                                    }

                                    if (mainForm.Element("StatusBorderThickness").Element("Left") != null)
                                    {
                                        thck.Left = Convert.ToInt32(mainForm.Element("StatusBorderThickness").Element("Left").Value, ni);
                                    }

                                    brdServerControls.BorderThickness = thck;
                                }
                                break;
                        }
                    }
                }
            }

            return lstPaths;
        }

Usage Example

        private void ResetSettings()
        {
            //Stop the animation
            sbFadeIn.Stop();
            sbFadeOut.Stop();

            //rest for to original values
            ResetForm();

            //make the setting obejct
            mySettings = new UISettings(myUserPrefs.CurrentSkin);

            List <string> lstPaths = mySettings.SetupMainForm(pthBorder, grdTitle, grdWindowControls, grdStatusBar, brdServerControls, null, formScale, brdMessageBox);

            if (lstPaths.Count > 0)
            {
                SetAnimation(lstPaths);
            }
        }