public virtual bool SetupConfiguredDimensions()
        {
            WPFDoEvents.AssertThisCodeIsRunningInTheUIThread();

            // Format: Name=X|Y|W|H|M::...
            // Exception to the rule for backwards compatibility: first record is for main window and has no name.
            string        cfg          = Configuration.ConfigurationManager.Instance.ConfigurationRecord.GUI_RestoreLocationAtStartup_Position ?? "";
            List <string> cfgarr       = new List <string>(cfg.Split(new string[] { "::" }, StringSplitOptions.None));
            string        position     = String.Empty;
            string        name_to_find = Name;

            if (String.IsNullOrEmpty(name_to_find))
            {
                return(false);
            }
            if (name_to_find == "QiqqaMainWindow")
            {
                name_to_find = "!";
            }

            for (int i = 0; i < cfgarr.Count; i++)
            {
                string[] wincfg = cfgarr[i].Split('=');
                if (0 == i)
                {
                    wincfg = new string[] { "!" /* Main */, wincfg[0] };
                }

                if (wincfg[0] == name_to_find)
                {
                    position = wincfg[1];
                    break;
                }
            }

            // https://stackoverflow.com/questions/16100/convert-a-string-to-an-enum-in-c-sharp
            if (String.IsNullOrEmpty(position))
            {
                Logging.Warn("Can't restore screen position for window {0} from empty remembered location.", Name);
                return(false);
            }

            try
            {
                StoredLocation loc = new StoredLocation(position);

                Logging.Info("Window {5}: Screen position and window size restoring to x={0},y={1},w={2},h={3},mode={4}", loc.position.X, loc.position.Y, loc.position.Width, loc.position.Height, loc.state, Name);

                Rect pos = loc.position;

                bool done = false;

                // when we have a valid corner coordinate, do we restore window *position*
                // (and possibly also *size*):
                if (!Double.IsNaN(pos.X) && !Double.IsNaN(pos.Y))
                {
                    Left = pos.X;
                    Top  = pos.Y;

                    if (!Double.IsNaN(pos.Width) && !Double.IsNaN(pos.Height))
                    {
                        Width  = pos.Width;
                        Height = pos.Height;
                    }
                    done = true;

                    // only restore WindowState when the (restore-)coordinates are valid too!
                    WindowState = loc.state;
                }

                return(done);
            }
            catch (Exception ex)
            {
                Logging.Warn(ex, "There was a problem restoring screen position to {0} for window {1}", position, Name);
                return(false);
            }
        }
 public StoredLocation(double left, double top, double width, double height, WindowState s)
 {
     position = StoredLocation.ClipBoundsToScreen(left, top, width, height);
     state    = s;
 }
        public virtual bool SetupConfiguredDimensions(Size preferred_dimensions)
        {
            WPFDoEvents.AssertThisCodeIsRunningInTheUIThread();

            string name_to_find = Name;
            bool   done         = false;

            if (!String.IsNullOrEmpty(name_to_find))
            {
                if (Configuration.ConfigurationManager.IsInitialized)
                {
                    if (Configuration.ConfigurationManager.Instance.ConfigurationRecord.GUI_RestoreLocationAtStartup)
                    {
                        // Format: Name=X|Y|W|H|M::...
                        // Exception to the rule for backwards compatibility: first record is for main window and has no name.
                        string        cfg      = Configuration.ConfigurationManager.Instance.ConfigurationRecord.GUI_RestoreLocationAtStartup_Position ?? "";
                        List <string> cfgarr   = new List <string>(cfg.Split(new string[] { "::" }, StringSplitOptions.None));
                        string        position = String.Empty;

                        for (int i = 0; i < cfgarr.Count; i++)
                        {
                            string[] wincfg = cfgarr[i].Split('=');

                            if (wincfg[0] == name_to_find)
                            {
                                position = wincfg[1];
                                break;
                            }
                        }

                        // https://stackoverflow.com/questions/16100/convert-a-string-to-an-enum-in-c-sharp
                        if (String.IsNullOrEmpty(position))
                        {
                            Logging.Warn("Can't restore screen position for window {0} from empty remembered location.", Name);
                        }
                        else
                        {
                            try
                            {
                                StoredLocation loc = new StoredLocation(position);

                                Logging.Info("Window {5}: Screen position and window size restoring to x={0},y={1},w={2},h={3},mode={4}", loc.position.X, loc.position.Y, loc.position.Width, loc.position.Height, loc.state, Name);

                                Rect pos = loc.position;

                                // when we have a valid corner coordinate, do we restore window *position*
                                // (and possibly also *size*):
                                if (!Double.IsNaN(pos.X) && !Double.IsNaN(pos.Y))
                                {
                                    Left = pos.X;
                                    Top  = pos.Y;

                                    if (!Double.IsNaN(pos.Width) && !Double.IsNaN(pos.Height))
                                    {
                                        Width  = pos.Width;
                                        Height = pos.Height;
                                    }
                                    done = true;

                                    // only restore WindowState when the (restore-)coordinates are valid too!
                                    WindowState = loc.state;
                                }
                            }
                            catch (Exception ex)
                            {
                                Logging.Warn(ex, "There was a problem restoring screen position to {0} for window {1}", position, Name);
                                done = false;
                            }
                        }
                    }
                }
            }

            // Check that we actually are fitting on the user's display area
            //
            // https://stackoverflow.com/questions/1317235/c-get-complete-desktop-size
            // https://stackoverflow.com/questions/2704887/is-there-a-wpf-equaivalent-to-system-windows-forms-screen/2707176#2707176
            // https://stackoverflow.com/questions/1918877/how-can-i-get-the-dpi-in-wpf
            // https://github.com/micdenny/WpfScreenHelper
            List <Screen> scrns  = Screen.AllScreens;
            Rect          bounds = scrns[0].Bounds;

            scrns.RemoveAt(0);

            foreach (Screen screen in scrns)
            {
                bounds = Rect.Union(bounds, screen.Bounds);
            }

            if (Width > SystemParameters.FullPrimaryScreenWidth)
            {
                Width = SystemParameters.FullPrimaryScreenWidth;
            }
            if (Height > SystemParameters.FullPrimaryScreenHeight)
            {
                Height = SystemParameters.FullPrimaryScreenHeight;
            }

            // may be PARTLY positioned outside the visible area:
            double MINIMUM_VISIBLE_PART = Math.Min(100, Math.Min(Width, Height));

            if (Left + MINIMUM_VISIBLE_PART > bounds.Width)
            {
                Left = bounds.Width - MINIMUM_VISIBLE_PART;
            }
            if (Left + Width - MINIMUM_VISIBLE_PART < 0)
            {
                Left = MINIMUM_VISIBLE_PART - Width;
            }
            if (Top + MINIMUM_VISIBLE_PART > bounds.Height)
            {
                Top = bounds.Height - MINIMUM_VISIBLE_PART;
            }
            if (Top + Height - MINIMUM_VISIBLE_PART < 0)
            {
                Top = MINIMUM_VISIBLE_PART - Height;
            }

            return(done);
        }