Ejemplo n.º 1
0
 private void ApplyInternalSettings(WindowSettings settings)
 {
     // if the form implements IWindowSettingsTarget, give it a
     // chance to restore internal settings
     if (_form is IWindowSettingsTarget)
     {
         var target = (IWindowSettingsTarget)_form;
         target.RestoreSettings(settings);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Restores the saved geometry settings (if existing) for the form and
        /// returns a value that indicates if the settings have been successfully
        /// restored.
        /// </summary>
        /// <param name="restoreOption">options for the restore.</param>
        /// <remarks>
        /// All exceptions are ignored to make sure that this miscellaneous function
        /// does never break the application. However, it is guaranteed that all
        /// changes are fully rolled back.
        /// </remarks>
        /// <returns>True if stored settings have been encountered and successfully
        /// applied, false if no stored settings existed for the form or an error
        /// occurred</returns>
        public bool RestoreSettings(FormStateRestoreOption restoreOption)
        {
            // read settings from file
            WindowSettings settings;

            try
            {
                settings = (WindowSettings)FormStatePersistence.ReadFromXml(
                    _fileName, _settingsType);
            }
            catch (Exception e)
            {
                _msg.Warn("Error reading stored settings", e);

                // ignore the exception
                return(false);
            }

            if (settings == null)
            {
                return(false);
            }
            else
            {
                // get the current settings for rollback in case of exception
                WindowSettings origSettings = GetCurrentSettings();

                try
                {
                    ApplySettings(settings, restoreOption);
                }
                catch (Exception e)
                {
                    // roll back and ignore the exception
                    try
                    {
                        ApplySettings(origSettings);
                    }
                    catch
                    {
                        _msg.Warn("Unable to roll back window settings", e);
                    }
                    finally
                    {
                        _msg.Warn("Error applying stored window settings", e);
                    }

                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 3
0
        private void ApplySize(WindowSettings settings)
        {
            if (!settings.HasSize)
            {
                return;
            }

            int maxWidth  = Screen.PrimaryScreen.WorkingArea.Width;
            int maxHeight = Screen.PrimaryScreen.WorkingArea.Height;

            // try to restore location. Make sure that location is within current
            // screen dimensions
            int width = (settings.Width > maxWidth)
                                            ? maxWidth
                                            : settings.Width;
            int height = (settings.Height > maxHeight)
                                             ? maxHeight
                                             : settings.Height;

            _form.Size = new Size(width, height);
        }
Ejemplo n.º 4
0
        private void ApplySettings(WindowSettings settings,
                                   FormStateRestoreOption restoreOption)
        {
            switch (restoreOption)
            {
            case FormStateRestoreOption.OnlyLocation:
                ApplyLocation(settings);
                ApplyTopMost(settings);
                break;

            case FormStateRestoreOption.Normal:
                ApplyLocation(settings);
                ApplyTopMost(settings);
                ApplySize(settings);
                ApplyWindowState(settings);
                ApplyInternalSettings(settings);
                break;

            default:
                throw new ArgumentException(
                          string.Format("Unknown restore option: {0}", restoreOption));
            }
        }
Ejemplo n.º 5
0
 private void ApplyTopMost(WindowSettings settings)
 {
     _form.TopMost = settings.TopMost;
 }
Ejemplo n.º 6
0
 private void ApplySettings(WindowSettings settings)
 {
     ApplySettings(settings, FormStateRestoreOption.Normal);
 }