/// <summary> /// Returns whether this converter can convert the object to the specified type, using the /// specified context. /// </summary> /// <param name="context"> /// An <see cref="ITypeDescriptorContext"/> that provides a format context. /// </param> /// <param name="destinationType"> /// A <see cref="Type"/> that represents the type you want to convert to. /// </param> /// <returns> /// <see langword="true"/> if this converter can perform the conversion; otherwise, /// <see langword="false"/>. /// </returns> public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(string)) { if (context != null && context.Instance != null) { MultiKeyGesture instance = context.Instance as MultiKeyGesture; if (instance != null) { if (!ModifierKeysConverter.IsDefinedModifierKeys(instance.Modifiers)) { return(false); } foreach (Key key in instance.Keys) { if (!IsDefinedKey(key)) { return(false); } } return(true); } } else { return(true); } } return(false); }
private UnrestrictedKeyGesture(Key key, ModifierKeys modifiers, string displayString, bool validateGesture) { if (!ModifierKeysConverter.IsDefinedModifierKeys(modifiers)) { throw new InvalidEnumArgumentException("modifiers", (int)modifiers, typeof(ModifierKeys)); } if (!IsDefinedKey(key)) { throw new InvalidEnumArgumentException("key", (int)key, typeof(Key)); } if (displayString == null) { throw new ArgumentNullException("displayString"); } // Do not validate the gesture, can't see any reason why you'd want to limit what keys a gesture // can be composed of. Default behaviour is to require modifier keys for alpha-numeric keys // if (validateGesture && !IsValid(key, modifiers)) // { // throw new NotSupportedException(SR.Get("KeyGesture_Invalid", new object[] { modifiers, key })); // } _modifiers = modifiers; _key = key; _displayString = displayString; }
/// <inheritdoc /> /// <summary> /// Determines whether the specified object can be converted into a <see cref="T:System.String" />. /// </summary> /// <param name="value">The object to evaluate for conversion.</param> /// <param name="context">Context information that is used for conversion.</param> /// <returns> /// <see langword="true" /> if the <paramref name="value" /> can be converted into a /// <see cref="T:System.String" />; otherwise, <see langword="false" />. /// </returns> public override bool CanConvertToString(object value, IValueSerializerContext context) { var gesture = value as MultiKeyGesture; return(gesture != null && ModifierKeysConverter.IsDefinedModifierKeys(gesture.Modifiers) && MultiKeyGesture.IsDefinedKey(gesture.Key)); }
public static void VerifyIsHotkeyDefined(Hotkey hotkey) { if (!ModifierKeysConverter.IsDefinedModifierKeys(hotkey.Modifiers)) { throw new InvalidEnumArgumentException("hotkey.Modifiers", Convert.ToInt32(hotkey.Modifiers), typeof(ModifierKeys)); } VerifyIsDefined <Key>(hotkey.Key, "Key"); }
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (((destinationType == typeof(string)) && (context != null)) && (context.Instance != null)) { UnrestrictedKeyGesture instance = context.Instance as UnrestrictedKeyGesture; if (instance != null) { return(ModifierKeysConverter.IsDefinedModifierKeys(instance.Modifiers) && IsDefinedKey(instance.Key)); } } return(false); }
/// <inheritdoc /> public override bool CanConvertToString(object value, IValueSerializerContext context) { var result = false; var gesture = value as MouseWheelGesture; if (gesture != null) { if (ModifierKeysConverter.IsDefinedModifierKeys(gesture.Modifiers) && gesture.MouseAction == MouseAction.WheelClick && MouseWheelDirectionExt.IsDefined(gesture.Direction)) { result = true; } } return(result); }
///<summary> ///TypeConverter method override. ///</summary> ///<param name="context">ITypeDescriptorContext</param> ///<param name="destinationType">Type to convert to</param> ///<returns>true if conversion is possible</returns> public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { // We can convert to an InstanceDescriptor or to a string. if (destinationType == typeof(string)) { // When invoked by the serialization engine we can convert to string only for known type if (context != null && context.Instance != null) { AnyKeyGesture keyGesture = context.Instance as AnyKeyGesture; if (keyGesture != null) { return(ModifierKeysConverter.IsDefinedModifierKeys(keyGesture.Modifiers) && IsDefinedKey(keyGesture.Key)); } } } return(false); }
public MultiKeyGesture(IEnumerable <Key> keys, ModifierKeys modifiers, string displayString) : base(Key.None, modifiers, GetDisplayString(keys, modifiers, displayString)) { if (keys == null) { throw new ArgumentNullException(nameof(keys)); } if (!ModifierKeysConverter.IsDefinedModifierKeys(modifiers)) { throw new InvalidEnumArgumentException(nameof(modifiers), (int)modifiers, typeof(ModifierKeys)); } _keys = new List <Key>(keys); if (_keys.Count == 0) { throw new ArgumentException("A multi key gesture needs to have at least one key."); } }
/// <summary> /// Private constructor that does the real work. /// </summary> /// <param name="key">Key</param> /// <param name="modifiers">Modifiers</param> /// <param name="displayString">display string</param> /// <param name="validateGesture">If true, throws an exception if the key and modifier are not valid</param> private AnyKeyGesture(Key key, ModifierKeys modifiers, string displayString, bool validateGesture) { if (!ModifierKeysConverter.IsDefinedModifierKeys(modifiers)) { throw new InvalidEnumArgumentException("modifiers", (int)modifiers, typeof(ModifierKeys)); } if (!IsDefinedKey(key)) { throw new InvalidEnumArgumentException("key", (int)key, typeof(Key)); } if (validateGesture && !IsValid(key, modifiers)) { throw new NotSupportedException($"Unsupported key gesture \"{this}\"!"); } _modifiers = modifiers; _key = key; _displayString = displayString ?? string.Empty; }
public static string GetCultureModifiersName(ModifierKeys modifiers, CultureInfo culture = null) { if (!ModifierKeysConverter.IsDefinedModifierKeys(modifiers)) { throw new InvalidEnumArgumentException(nameof(modifiers), (int)modifiers, typeof(ModifierKeys)); } var str = string.Empty; if ((modifiers & ModifierKeys.Control) == ModifierKeys.Control) { str += GetSingleCultureModifierName(ModifierKeys.Control, culture); } if ((modifiers & ModifierKeys.Shift) == ModifierKeys.Shift) { if (str.Length > 0) { str += ModifierDelimiter; } str += GetSingleCultureModifierName(ModifierKeys.Shift, culture); } if ((modifiers & ModifierKeys.Alt) == ModifierKeys.Alt) { if (str.Length > 0) { str += ModifierDelimiter; } str += GetSingleCultureModifierName(ModifierKeys.Alt, culture); } if ((modifiers & ModifierKeys.Windows) == ModifierKeys.Windows) { if (str.Length > 0) { str += ModifierDelimiter; } str += GetSingleCultureModifierName(ModifierKeys.Windows, culture); } return(str); }
public static bool IsHotkeyDefined(Hotkey hotkey) { return (ModifierKeysConverter.IsDefinedModifierKeys(hotkey.Modifiers) && IsDefined <Key>(hotkey.Key)); }
public override bool CanConvertToString(object value, IValueSerializerContext context) { UnrestrictedKeyGesture gesture = value as UnrestrictedKeyGesture; return(((gesture != null) && ModifierKeysConverter.IsDefinedModifierKeys(gesture.Modifiers)) && UnrestrictedKeyGestureConverter.IsDefinedKey(gesture.Key)); }