ModifierKeysConverter.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / wpf / src / Base / System / Windows / Input / ModifierKeysConverter.cs / 1 / ModifierKeysConverter.cs

                            //---------------------------------------------------------------------------- 
//
// File: ModifierKeysConverter.cs
//
// Description: 
//
//      ModifierKeysConverter : Converts a Modifier string to the *Type* that the string represents and vice-versa. 
// 
// Features:
// 
// History:
//  05/28/2003       created: Chandrasekhar Rentachintala
//
// Copyright (C) 2003 by Microsoft Corporation.  All rights reserved. 
//
//--------------------------------------------------------------------------- 
using System; 
using System.ComponentModel;    // for TypeConverter
using System.Globalization;     // for CultureInfo 
using System.Reflection;
using MS.Internal;
using System.Windows;
using System.Windows.Input; 
using System.Windows.Markup;
using MS.Utility; 
 
namespace System.Windows.Input
{ 
    /// 
    /// Key Converter class for converting between a string and the Type of a Modifiers
    /// 
    ///  
    public class ModifierKeysConverter : TypeConverter
    { 
        ///  
        /// CanConvertFrom()
        ///  
        /// 
        /// 
        /// 
        ///  
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        { 
            // We can only handle string. 
            if (sourceType == typeof(string))
            { 
                return true;
            }
            else
            { 
                return false;
            } 
        } 

        ///  
        /// TypeConverter method override.
        /// 
        /// ITypeDescriptorContext
        /// Type to convert to 
        /// true if conversion is possible
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
        { 
            // We can convert 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 &&
					context.Instance is ModifierKeys) 
                {
                    return (IsDefinedModifierKeys((ModifierKeys)context.Instance)); 
                } 
            }
            return false; 
        }

        /// 
        /// ConvertFrom() 
        /// 
        ///  
        ///  
        /// 
        ///  
        /// 
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object source)
        {
            if (source is string) 
            {
                string modifiersToken = ((string)source).Trim(); 
                ModifierKeys modifiers = GetModifierKeys(modifiersToken, CultureInfo.InvariantCulture); 
                return modifiers;
            } 
            throw GetConvertFromException(source);
        }

        ///  
        /// ConvertTo()
        ///  
        ///  
        /// 
        ///  
        /// 
        /// 
        /// 
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
        {
            if (destinationType == null) 
                throw new ArgumentNullException("destinationType"); 

            if (destinationType == typeof(string)) 
            {
                ModifierKeys modifiers = (ModifierKeys)value;

                if (!IsDefinedModifierKeys(modifiers)) 
                    throw new InvalidEnumArgumentException("modifiers", (int)modifiers, typeof(ModifierKeys));
                else 
                { 
                    string strModifiers = "";
 
                    if ((modifiers & ModifierKeys.Control) == ModifierKeys.Control)
                    {
                        strModifiers += MatchModifiers(ModifierKeys.Control);
                    } 

                    if ((modifiers & ModifierKeys.Alt) == ModifierKeys.Alt) 
                    { 
                        if (strModifiers.Length > 0)
                            strModifiers += Modifier_Delimiter; 

                        strModifiers += MatchModifiers(ModifierKeys.Alt);
                    }
 
                    if ((modifiers & ModifierKeys.Windows) == ModifierKeys.Windows)
                    { 
                        if (strModifiers.Length > 0) 
                            strModifiers += Modifier_Delimiter;
 
                        strModifiers += MatchModifiers(ModifierKeys.Windows); ;
                    }

                    if ((modifiers & ModifierKeys.Shift) == ModifierKeys.Shift) 
                    {
                        if (strModifiers.Length > 0) 
                            strModifiers += Modifier_Delimiter; 

                        strModifiers += MatchModifiers(ModifierKeys.Shift); ; 
                    }

                    return strModifiers;
                } 
            }
            throw GetConvertToException(value,destinationType); 
        } 

        private ModifierKeys GetModifierKeys(string modifiersToken, CultureInfo culture) 
        {
            ModifierKeys modifiers = ModifierKeys.None;
            if (modifiersToken.Length != 0)
            { 
                int offset = 0;
                do 
                { 
                    offset = modifiersToken.IndexOf(Modifier_Delimiter);
                    string token = (offset < 0) ? modifiersToken : modifiersToken.Substring(0, offset); 
                    token = token.Trim();
                    token = token.ToUpper(culture);

                    if (token == String.Empty) 
                        break;
 
                    switch (token) 
                    {
                        case "CONTROL" : 
                        case "CTRL" :
                            modifiers |= ModifierKeys.Control;
                            break;
 
                        case "SHIFT" :
                            modifiers |= ModifierKeys.Shift; 
                            break; 

                        case "ALT": 
                            modifiers |= ModifierKeys.Alt;
                            break;

                        case "WINDOWS": 
                        case "WIN":
                            modifiers |= ModifierKeys.Windows; 
                            break; 

                        default: 
                            throw new NotSupportedException(SR.Get(SRID.Unsupported_Modifier, token));
                    }

                    modifiersToken = modifiersToken.Substring(offset + 1); 
                } while (offset != -1);
            } 
            return modifiers; 
        }
 
        /// 
        ///     Check for Valid enum, as any int can be casted to the enum.
        /// 
        public static bool IsDefinedModifierKeys(ModifierKeys modifierKeys) 
        {
            return (modifierKeys == ModifierKeys.None || (((int)modifierKeys & ~((int)ModifierKeysFlag)) == 0)); 
        } 

	    private const char Modifier_Delimiter = '+'; 

        private static ModifierKeys ModifierKeysFlag  =  ModifierKeys.Windows | ModifierKeys.Shift |
                                                         ModifierKeys.Alt     | ModifierKeys.Control ;
 
        internal static string MatchModifiers(ModifierKeys modifierKeys)
        { 
            string modifiers = String.Empty; 
            switch (modifierKeys)
            { 
                case ModifierKeys.Control: modifiers="Ctrl";break;
                case ModifierKeys.Shift  : modifiers="Shift";break;
                case ModifierKeys.Alt    : modifiers="Alt";break;
                case ModifierKeys.Windows: modifiers="Windows";break; 
            }
            return modifiers; 
        } 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
 	//---------------------------------------------------------------------------- 
//
// File: ModifierKeysConverter.cs
//
// Description: 
//
//      ModifierKeysConverter : Converts a Modifier string to the *Type* that the string represents and vice-versa. 
// 
// Features:
// 
// History:
//  05/28/2003       created: Chandrasekhar Rentachintala
//
// Copyright (C) 2003 by Microsoft Corporation.  All rights reserved. 
//
//--------------------------------------------------------------------------- 
using System; 
using System.ComponentModel;    // for TypeConverter
using System.Globalization;     // for CultureInfo 
using System.Reflection;
using MS.Internal;
using System.Windows;
using System.Windows.Input; 
using System.Windows.Markup;
using MS.Utility; 
 
namespace System.Windows.Input
{ 
    /// 
    /// Key Converter class for converting between a string and the Type of a Modifiers
    /// 
    ///  
    public class ModifierKeysConverter : TypeConverter
    { 
        ///  
        /// CanConvertFrom()
        ///  
        /// 
        /// 
        /// 
        ///  
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        { 
            // We can only handle string. 
            if (sourceType == typeof(string))
            { 
                return true;
            }
            else
            { 
                return false;
            } 
        } 

        ///  
        /// TypeConverter method override.
        /// 
        /// ITypeDescriptorContext
        /// Type to convert to 
        /// true if conversion is possible
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
        { 
            // We can convert 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 &&
					context.Instance is ModifierKeys) 
                {
                    return (IsDefinedModifierKeys((ModifierKeys)context.Instance)); 
                } 
            }
            return false; 
        }

        /// 
        /// ConvertFrom() 
        /// 
        ///  
        ///  
        /// 
        ///  
        /// 
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object source)
        {
            if (source is string) 
            {
                string modifiersToken = ((string)source).Trim(); 
                ModifierKeys modifiers = GetModifierKeys(modifiersToken, CultureInfo.InvariantCulture); 
                return modifiers;
            } 
            throw GetConvertFromException(source);
        }

        ///  
        /// ConvertTo()
        ///  
        ///  
        /// 
        ///  
        /// 
        /// 
        /// 
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
        {
            if (destinationType == null) 
                throw new ArgumentNullException("destinationType"); 

            if (destinationType == typeof(string)) 
            {
                ModifierKeys modifiers = (ModifierKeys)value;

                if (!IsDefinedModifierKeys(modifiers)) 
                    throw new InvalidEnumArgumentException("modifiers", (int)modifiers, typeof(ModifierKeys));
                else 
                { 
                    string strModifiers = "";
 
                    if ((modifiers & ModifierKeys.Control) == ModifierKeys.Control)
                    {
                        strModifiers += MatchModifiers(ModifierKeys.Control);
                    } 

                    if ((modifiers & ModifierKeys.Alt) == ModifierKeys.Alt) 
                    { 
                        if (strModifiers.Length > 0)
                            strModifiers += Modifier_Delimiter; 

                        strModifiers += MatchModifiers(ModifierKeys.Alt);
                    }
 
                    if ((modifiers & ModifierKeys.Windows) == ModifierKeys.Windows)
                    { 
                        if (strModifiers.Length > 0) 
                            strModifiers += Modifier_Delimiter;
 
                        strModifiers += MatchModifiers(ModifierKeys.Windows); ;
                    }

                    if ((modifiers & ModifierKeys.Shift) == ModifierKeys.Shift) 
                    {
                        if (strModifiers.Length > 0) 
                            strModifiers += Modifier_Delimiter; 

                        strModifiers += MatchModifiers(ModifierKeys.Shift); ; 
                    }

                    return strModifiers;
                } 
            }
            throw GetConvertToException(value,destinationType); 
        } 

        private ModifierKeys GetModifierKeys(string modifiersToken, CultureInfo culture) 
        {
            ModifierKeys modifiers = ModifierKeys.None;
            if (modifiersToken.Length != 0)
            { 
                int offset = 0;
                do 
                { 
                    offset = modifiersToken.IndexOf(Modifier_Delimiter);
                    string token = (offset < 0) ? modifiersToken : modifiersToken.Substring(0, offset); 
                    token = token.Trim();
                    token = token.ToUpper(culture);

                    if (token == String.Empty) 
                        break;
 
                    switch (token) 
                    {
                        case "CONTROL" : 
                        case "CTRL" :
                            modifiers |= ModifierKeys.Control;
                            break;
 
                        case "SHIFT" :
                            modifiers |= ModifierKeys.Shift; 
                            break; 

                        case "ALT": 
                            modifiers |= ModifierKeys.Alt;
                            break;

                        case "WINDOWS": 
                        case "WIN":
                            modifiers |= ModifierKeys.Windows; 
                            break; 

                        default: 
                            throw new NotSupportedException(SR.Get(SRID.Unsupported_Modifier, token));
                    }

                    modifiersToken = modifiersToken.Substring(offset + 1); 
                } while (offset != -1);
            } 
            return modifiers; 
        }
 
        /// 
        ///     Check for Valid enum, as any int can be casted to the enum.
        /// 
        public static bool IsDefinedModifierKeys(ModifierKeys modifierKeys) 
        {
            return (modifierKeys == ModifierKeys.None || (((int)modifierKeys & ~((int)ModifierKeysFlag)) == 0)); 
        } 

	    private const char Modifier_Delimiter = '+'; 

        private static ModifierKeys ModifierKeysFlag  =  ModifierKeys.Windows | ModifierKeys.Shift |
                                                         ModifierKeys.Alt     | ModifierKeys.Control ;
 
        internal static string MatchModifiers(ModifierKeys modifierKeys)
        { 
            string modifiers = String.Empty; 
            switch (modifierKeys)
            { 
                case ModifierKeys.Control: modifiers="Ctrl";break;
                case ModifierKeys.Shift  : modifiers="Shift";break;
                case ModifierKeys.Alt    : modifiers="Alt";break;
                case ModifierKeys.Windows: modifiers="Windows";break; 
            }
            return modifiers; 
        } 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK