private void RaiseChangedEvent(EditorOptionDefinition definition)
        {
            // First, send out local events, but only if the change is valid in this scope
            if (Scope == null || definition.IsApplicableToScope(Scope))
            {
                var tempEvent = OptionChanged;
                if (tempEvent != null)
                {
                    tempEvent(this, new EditorOptionChangedEventArgs(definition.Name));
                }
            }

            // Get rid of the expired refs
            DerivedEditorOptions.RemoveAll(weakref => !weakref.IsAlive);

            // Now, notify a copy of the derived options (since an event might modify the DerivedEditorOptions).
            foreach (var weakRef in new FrugalList <WeakReference>(DerivedEditorOptions))
            {
                EditorOptions derived = weakRef.Target as EditorOptions;
                if (derived != null)
                {
                    derived.OnParentOptionChanged(definition);
                }
            }
        }
        public bool ClearOptionValue(string optionId)
        {
            if (this.Parent == null)
            {
                // Can't clear options on the Global options
                return(false);
            }

            if (OptionsSetLocally.Contains(optionId))
            {
                object currentValue = OptionsSetLocally[optionId];

                OptionsSetLocally.Remove(optionId);

                EditorOptionDefinition definition = _factory.GetOptionDefinitionOrThrow(optionId);
                object inheritedValue             = this.GetOptionValue(definition);

                // See what the inherited option value was.  If it isn't changing,
                // then we don't need to raise an event.
                if (!object.Equals(currentValue, inheritedValue))
                {
                    RaiseChangedEvent(definition);
                }

                return(true);
            }

            return(false);
        }
        public void SetOptionValue(string optionId, object value)
        {
            EditorOptionDefinition definition = _factory.GetOptionDefinitionOrThrow(optionId);

            // Make sure the type of the provided value is correct
            if (!definition.ValueType.IsAssignableFrom(value.GetType()))
            {
                throw new ArgumentException("Specified option value is of an invalid type", "value");
            }
            // Make sure the option is valid, also
            else if (!definition.IsValid(ref value))
            {
                throw new ArgumentException("The supplied value failed validation for the option.", "value");
            }
            // Finally, set the option value locally
            else
            {
                object currentValue = this.GetOptionValue(definition);
                OptionsSetLocally[optionId] = value;

                if (!object.Equals(currentValue, value))
                {
                    RaiseChangedEvent(definition);
                }
            }
        }
 internal void OnParentOptionChanged(EditorOptionDefinition definition)
 {
     // We only notify if the given option isn't already set locally, since it
     // would be overriden by a parent option changing.
     if (!this.OptionsSetLocally.Contains(definition.Name))
     {
         RaiseChangedEvent(definition);
     }
 }
        private object GetOptionValue(EditorOptionDefinition definition)
        {
            object value;

            if (!TryGetOption(definition, out value))
            {
                throw new ArgumentException(string.Format("The specified option is not valid in this scope: {0}", definition.Name));
            }

            return(value);
        }
        private bool TryGetOption(EditorOptionDefinition definition, out object value)
        {
            value = null;

            if (Scope != null && !definition.IsApplicableToScope(Scope))
            {
                return(false);
            }

            value = this.GetOptionForChild(definition);
            return(true);
        }
        /// <summary>
        /// Get the given option from this (or its ancestors).  The caller should have already checked to ensure
        /// the given option is valid in the scope being requested.
        /// </summary>
        /// <param name="definition">Definition of the option to find.</param>
        /// <returns>The option's current value.</returns>
        internal object GetOptionForChild(EditorOptionDefinition definition)
        {
            if (OptionsSetLocally.Contains(definition.Name))
            {
                return(OptionsSetLocally[definition.Name]);
            }

            if (_parent == null)
            {
                return(definition.DefaultValue);
            }

            return(_parent.GetOptionForChild(definition));
        }
        public bool IsOptionDefined(string optionId, bool localScopeOnly)
        {
            if (localScopeOnly && (_parent != null))    //All options with valid definitions are set for the root.
            {
                return(OptionsSetLocally.Contains(optionId));
            }

            EditorOptionDefinition definition = _factory.GetOptionDefinition(optionId);

            if ((definition != null) &&
                (Scope == null || definition.IsApplicableToScope(Scope)))
            {
                return(true);
            }

            return(false);
        }
        public bool IsOptionDefined <T>(EditorOptionKey <T> key, bool localScopeOnly)
        {
            if (localScopeOnly && (_parent != null))    //All options with valid definitions are set for the root.
            {
                return(OptionsSetLocally.Contains(key.Name));
            }

            EditorOptionDefinition definition = _factory.GetOptionDefinition(key.Name);

            if ((definition != null) &&
                (Scope == null || definition.IsApplicableToScope(Scope)) &&
                definition.ValueType.IsEquivalentTo(typeof(T)))
            {
                return(true);
            }

            return(false);
        }
Exemple #10
0
        public bool LoadOption(IEditorOptions options, string optionName)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            EditorOptionDefinition optionDefinition = null;

            while (true)
            {
                optionDefinition = GetMatchingOption(options, optionName);
                if (optionDefinition != null)
                {
                    break;
                }

                options = options.Parent;
                if (options == null)
                {
                    return(false);       //Unable to load option.
                }
            }

            IVsSettingsManager manager = this.SettingsManagerService;
            IVsSettingsStore   store;

            Marshal.ThrowExceptionForHR(manager.GetReadOnlySettingsStore((uint)__VsSettingsScope.SettingsScope_UserSettings, out store));

            string result;

            Marshal.ThrowExceptionForHR(store.GetStringOrDefault("Text Editor", optionName, string.Empty, out result));

            if (result == string.Empty)
            {
                //No corresponding entry in the registery. Save the option to make it finable and editable.
                this.SaveOption(options, optionName);
            }
            else
            {
                try
                {
                    if (optionDefinition.ValueType == typeof(bool))
                    {
                        options.SetOptionValue(optionName, bool.Parse(result));
                        return(true);
                    }
                    else if (optionDefinition.ValueType == typeof(int))
                    {
                        options.SetOptionValue(optionName, int.Parse(result));
                        return(true);
                    }
                    else if (optionDefinition.ValueType == typeof(double))
                    {
                        options.SetOptionValue(optionName, double.Parse(result));
                        return(true);
                    }
                    else if (optionDefinition.ValueType == typeof(string))
                    {
                        options.SetOptionValue(optionName, result);
                        return(true);
                    }
                    else if (optionDefinition.ValueType == typeof(Color))
                    {
                        //Color's saved by Color.ToString() have a leading # sign ... strip that off before we parse.
                        uint argb = uint.Parse(result.Substring(1), NumberStyles.AllowHexSpecifier);
                        options.SetOptionValue(optionName, Color.FromArgb((byte)(argb >> 24), (byte)(argb >> 16), (byte)(argb >> 8), (byte)argb));
                        return(true);
                    }
                }
                catch (System.FormatException)
                {
                    //If we get a format exception, then the data for the option is invalid: overwrite it with something in the correct format.
                    this.SaveOption(options, optionName);
                }
            }

            return(false);
        }