/// <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);
        }
Beispiel #2
0
        /// <summary>
        /// Restores the saved form state (if existing) for the form and
        /// returns a value that indicates if the form state has 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 form state has been encountered and successfully
        /// applied, false if no stored form state existed for the form or an error
        /// occurred</returns>
        public bool RestoreState(
            FormStateRestoreOption restoreOption = FormStateRestoreOption.Normal)
        {
            // read state from file
            T formState;

            try
            {
                formState = (T)FormStatePersistence.ReadFromXml(_fileName, typeof(T));
            }
            catch (Exception e)
            {
                _msg.Warn(
                    "Error restoring saved window state. Window state is reset to defaults.", e);

                // ignore the exception
                return(false);
            }

            if (formState == null)
            {
                return(false);
            }

            // get the current form state for rollback in case of exception
            T origFormState = GetCurrentState();

            try
            {
                ApplyFormState(formState, restoreOption);
            }
            catch (Exception e)
            {
                // roll back and ignore the exception
                try
                {
                    ApplyFormState(origFormState);
                }
                catch
                {
                    _msg.Warn("Unable to roll back form state", e);
                }
                finally
                {
                    _msg.Warn("Error applying stored form state", e);
                }

                return(false);
            }

            return(true);
        }
Beispiel #3
0
        private void ApplyFormState([NotNull] T formState,
                                    FormStateRestoreOption restoreOption)
        {
            _msg.VerboseDebugFormat("Applying form state for {0}", Form.Name);

            using (_msg.IncrementIndentation())
            {
                switch (restoreOption)
                {
                case FormStateRestoreOption.OnlyLocation:
                    ApplyLocation(formState);
                    ApplyTopMost(formState);
                    // don't apply size
                    // don't apply window state
                    ApplyInternalFormState(formState);
                    break;

                case FormStateRestoreOption.KeepLocation:
                    // don't apply location
                    ApplyTopMost(formState);
                    ApplySize(formState);
                    ApplyWindowState(formState);
                    ApplyInternalFormState(formState);
                    break;

                case FormStateRestoreOption.Normal:
                    ApplyLocation(formState);
                    ApplyTopMost(formState);
                    ApplySize(formState);
                    ApplyWindowState(formState);
                    ApplyInternalFormState(formState);
                    break;

                default:
                    throw new ArgumentException(
                              string.Format("Unknown restore option: {0}", restoreOption));
                }
            }
        }
        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));
            }
        }