Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Core / CSharp / System / Windows / Input / Command / KeyGestureConverter.cs / 1 / KeyGestureConverter.cs
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
//
// Description: KeyGestureConverter - Converts a KeyGesture string
// to the *Type* that the string represents
//
//
// History:
// 05/01/2004 : Chandrasekhar Rentachintala - Created
//
//---------------------------------------------------------------------------
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 MS.Utility;
namespace System.Windows.Input
{
///
/// KeyGesture - Converter class for converting between a string and the Type of a KeyGesture
///
public class KeyGestureConverter : TypeConverter
{
private const char MODIFIERS_DELIMITER = '+' ;
internal const char DISPLAYSTRING_SEPARATOR = ',' ;
///
///CanConvertFrom()
///
///ITypeDescriptorContext
///type to convert from
///true if the given type can be converted, false otherwise
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 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)
{
KeyGesture keyGesture = context.Instance as KeyGesture;
if (keyGesture != null)
{
return (ModifierKeysConverter.IsDefinedModifierKeys(keyGesture.Modifiers)
&& IsDefinedKey(keyGesture.Key));
}
}
}
return false;
}
///
/// ConvertFrom()
///
///
///
///
///
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object source)
{
if (source != null && source is string)
{
string fullName = ((string)source).Trim();
if (fullName == String.Empty)
return new KeyGesture(Key.None);
string keyToken;
string modifiersToken;
string displayString;
// break apart display string
int index = fullName.IndexOf(DISPLAYSTRING_SEPARATOR);
if (index >= 0)
{
displayString = fullName.Substring(index + 1).Trim();
fullName = fullName.Substring(0, index).Trim();
}
else
{
displayString = String.Empty;
}
// break apart key and modifiers
index = fullName.LastIndexOf(MODIFIERS_DELIMITER);
if (index >= 0)
{ // modifiers exists
modifiersToken = fullName.Substring(0, index);
keyToken = fullName.Substring(index + 1);
}
else
{
modifiersToken = String.Empty;
keyToken = fullName;
}
ModifierKeys modifiers = ModifierKeys.None;
object resultkey = keyConverter.ConvertFrom(context, culture, keyToken);
if (resultkey != null)
{
object temp = modifierKeysConverter.ConvertFrom(context, culture, modifiersToken);
if (temp != null)
{
modifiers = (ModifierKeys)temp;
}
return new KeyGesture((Key)resultkey, modifiers, displayString);
}
}
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))
{
if (value != null)
{
KeyGesture keyGesture = value as KeyGesture;
if (keyGesture != null)
{
if (keyGesture.Key == Key.None)
return String.Empty;
string strBinding = "" ;
string strKey = (string)keyConverter.ConvertTo(context, culture, keyGesture.Key, destinationType) as string;
if (strKey != String.Empty)
{
strBinding += modifierKeysConverter.ConvertTo(context, culture, keyGesture.Modifiers, destinationType) as string;
if (strBinding != String.Empty)
{
strBinding += MODIFIERS_DELIMITER;
}
strBinding += strKey;
if (!String.IsNullOrEmpty(keyGesture.DisplayString))
{
strBinding += DISPLAYSTRING_SEPARATOR + keyGesture.DisplayString;
}
}
return strBinding;
}
}
else
{
return String.Empty;
}
}
throw GetConvertToException(value,destinationType);
}
// Check for Valid enum, as any int can be casted to the enum.
internal static bool IsDefinedKey(Key key)
{
return (key >= Key.None && key <= Key.OemClear);
}
private static KeyConverter keyConverter = new KeyConverter();
private static ModifierKeysConverter modifierKeysConverter = new ModifierKeysConverter();
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
//
// Description: KeyGestureConverter - Converts a KeyGesture string
// to the *Type* that the string represents
//
//
// History:
// 05/01/2004 : Chandrasekhar Rentachintala - Created
//
//---------------------------------------------------------------------------
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 MS.Utility;
namespace System.Windows.Input
{
///
/// KeyGesture - Converter class for converting between a string and the Type of a KeyGesture
///
public class KeyGestureConverter : TypeConverter
{
private const char MODIFIERS_DELIMITER = '+' ;
internal const char DISPLAYSTRING_SEPARATOR = ',' ;
///
///CanConvertFrom()
///
///ITypeDescriptorContext
///type to convert from
///true if the given type can be converted, false otherwise
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 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)
{
KeyGesture keyGesture = context.Instance as KeyGesture;
if (keyGesture != null)
{
return (ModifierKeysConverter.IsDefinedModifierKeys(keyGesture.Modifiers)
&& IsDefinedKey(keyGesture.Key));
}
}
}
return false;
}
///
/// ConvertFrom()
///
///
///
///
///
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object source)
{
if (source != null && source is string)
{
string fullName = ((string)source).Trim();
if (fullName == String.Empty)
return new KeyGesture(Key.None);
string keyToken;
string modifiersToken;
string displayString;
// break apart display string
int index = fullName.IndexOf(DISPLAYSTRING_SEPARATOR);
if (index >= 0)
{
displayString = fullName.Substring(index + 1).Trim();
fullName = fullName.Substring(0, index).Trim();
}
else
{
displayString = String.Empty;
}
// break apart key and modifiers
index = fullName.LastIndexOf(MODIFIERS_DELIMITER);
if (index >= 0)
{ // modifiers exists
modifiersToken = fullName.Substring(0, index);
keyToken = fullName.Substring(index + 1);
}
else
{
modifiersToken = String.Empty;
keyToken = fullName;
}
ModifierKeys modifiers = ModifierKeys.None;
object resultkey = keyConverter.ConvertFrom(context, culture, keyToken);
if (resultkey != null)
{
object temp = modifierKeysConverter.ConvertFrom(context, culture, modifiersToken);
if (temp != null)
{
modifiers = (ModifierKeys)temp;
}
return new KeyGesture((Key)resultkey, modifiers, displayString);
}
}
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))
{
if (value != null)
{
KeyGesture keyGesture = value as KeyGesture;
if (keyGesture != null)
{
if (keyGesture.Key == Key.None)
return String.Empty;
string strBinding = "" ;
string strKey = (string)keyConverter.ConvertTo(context, culture, keyGesture.Key, destinationType) as string;
if (strKey != String.Empty)
{
strBinding += modifierKeysConverter.ConvertTo(context, culture, keyGesture.Modifiers, destinationType) as string;
if (strBinding != String.Empty)
{
strBinding += MODIFIERS_DELIMITER;
}
strBinding += strKey;
if (!String.IsNullOrEmpty(keyGesture.DisplayString))
{
strBinding += DISPLAYSTRING_SEPARATOR + keyGesture.DisplayString;
}
}
return strBinding;
}
}
else
{
return String.Empty;
}
}
throw GetConvertToException(value,destinationType);
}
// Check for Valid enum, as any int can be casted to the enum.
internal static bool IsDefinedKey(Key key)
{
return (key >= Key.None && key <= Key.OemClear);
}
private static KeyConverter keyConverter = new KeyConverter();
private static ModifierKeysConverter modifierKeysConverter = new ModifierKeysConverter();
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- EncoderParameters.cs
- StringResourceManager.cs
- CodeDirectoryCompiler.cs
- TraceEventCache.cs
- EmptyElement.cs
- ConfigXmlElement.cs
- TreeViewCancelEvent.cs
- SnapLine.cs
- QueryOutputWriter.cs
- TextWriterEngine.cs
- ScrollChrome.cs
- DataRelationCollection.cs
- Serializer.cs
- EndCreateSecurityTokenRequest.cs
- CompileXomlTask.cs
- Link.cs
- MailMessage.cs
- MachineKeySection.cs
- FormViewRow.cs
- MessageDecoder.cs
- BatchParser.cs
- Camera.cs
- SourceLineInfo.cs
- DSACryptoServiceProvider.cs
- StringTraceRecord.cs
- XmlUnspecifiedAttribute.cs
- NodeFunctions.cs
- PixelShader.cs
- X509CertificateClaimSet.cs
- CqlLexer.cs
- CodeMethodInvokeExpression.cs
- Token.cs
- TraceSwitch.cs
- HttpCacheVaryByContentEncodings.cs
- NameSpaceEvent.cs
- WebServiceReceive.cs
- HScrollBar.cs
- HotSpot.cs
- CalendarDateRangeChangingEventArgs.cs
- TemplateComponentConnector.cs
- Substitution.cs
- Constants.cs
- ChannelBuilder.cs
- ClientSideProviderDescription.cs
- DateTimeStorage.cs
- NativeMethods.cs
- EmissiveMaterial.cs
- CompilerWrapper.cs
- ContentFilePart.cs
- StorageFunctionMapping.cs
- DataGridViewControlCollection.cs
- RSAOAEPKeyExchangeDeformatter.cs
- CodeAttributeDeclarationCollection.cs
- EventLogQuery.cs
- COM2EnumConverter.cs
- ShimAsPublicXamlType.cs
- Automation.cs
- XmlDictionaryReaderQuotas.cs
- VariableDesigner.xaml.cs
- WindowsFormsHelpers.cs
- SqlProviderManifest.cs
- SerializableAttribute.cs
- LabelLiteral.cs
- IncrementalReadDecoders.cs
- HelpInfo.cs
- Visitors.cs
- Compilation.cs
- ContentPosition.cs
- SqlRowUpdatingEvent.cs
- JsonMessageEncoderFactory.cs
- HtmlProps.cs
- DefaultAssemblyResolver.cs
- ConfigurationValue.cs
- CannotUnloadAppDomainException.cs
- Size.cs
- EdmToObjectNamespaceMap.cs
- ShapingEngine.cs
- ElementMarkupObject.cs
- smtpconnection.cs
- NullableBoolConverter.cs
- PrintEvent.cs
- XPathException.cs
- ResourceManager.cs
- FirstMatchCodeGroup.cs
- Rule.cs
- DataServiceRequestOfT.cs
- PageHandlerFactory.cs
- ScopelessEnumAttribute.cs
- SubpageParagraph.cs
- ScriptingProfileServiceSection.cs
- MimeObjectFactory.cs
- NullableIntAverageAggregationOperator.cs
- OleDbSchemaGuid.cs
- clipboard.cs
- ApplicationTrust.cs
- ReferencedCollectionType.cs
- CultureSpecificCharacterBufferRange.cs
- ProviderConnectionPoint.cs
- ProjectionPruner.cs
- TextClipboardData.cs