using NTwain.Data; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace NTwain { /// /// Wrapped class for reading/writing a TWAIN capability associated with a . /// /// The TWAIN type of the value. public class CapabilityControl { DataSource _source; Func _convertRoutine; Func _setCustomRoutine; Func _setProvider; // an simplified way to set() that only needs on cap value /// /// Initializes a new instance of the class. /// /// The source. /// The capability. /// The value conversion routine in Get methods. /// Callback to provide the capability object for set method. /// /// source /// or /// valueConversionRoutine /// or /// setValueProvider /// public CapabilityControl(DataSource source, CapabilityId capability, Func getConversionRoutine, Func setValueProvider) { if (source == null) { throw new ArgumentNullException("source"); } if (getConversionRoutine == null) { throw new ArgumentNullException("valueConversionRoutine"); } if (setValueProvider == null) { throw new ArgumentNullException("setValueProvider"); } _source = source; _convertRoutine = getConversionRoutine; _setProvider = setValueProvider; Capability = capability; SupportedActions = source.CapQuerySupport(capability); } /// /// Initializes a new instance of the class. /// /// The source. /// The capability. /// The value conversion routine in Get methods. /// Callback to perform set value. /// /// source /// or /// valueConversionRoutine /// or /// setValueRoutine /// public CapabilityControl(DataSource source, CapabilityId capability, Func getConversionRoutine, Func setValueRoutine) { if (source == null) { throw new ArgumentNullException("source"); } if (getConversionRoutine == null) { throw new ArgumentNullException("valueConversionRoutine"); } if (setValueRoutine == null) { throw new ArgumentNullException("setValueRoutine"); } _source = source; _convertRoutine = getConversionRoutine; _setCustomRoutine = setValueRoutine; Capability = capability; SupportedActions = source.CapQuerySupport(capability); } bool Supports(QuerySupports flag) { return (SupportedActions & flag) == flag; } #region properties /// /// Gets the capability. /// /// /// The capability. /// public CapabilityId Capability { get; private set; } /// /// Gets the supported actions. /// /// /// The supported actions. /// public QuerySupports SupportedActions { get; private set; } /// /// Gets a value indicating whether this capability is supported. /// /// /// true if this capability is supported; otherwise, false. /// public bool IsSupported { get { return SupportedActions > QuerySupports.None; } } /// /// Gets a value indicating whether is supported. /// /// /// true if this capability can get values; otherwise, false. /// public bool CanGet { get { return Supports(QuerySupports.Get); } } /// /// Gets a value indicating whether is supported. /// /// /// true if this capability can get default value; otherwise, false. /// public bool CanGetDefault { get { return Supports(QuerySupports.GetDefault); } } /// /// Gets a value indicating whether is supported. /// /// /// true if this capability can get current value; otherwise, false. /// public bool CanGetCurrent { get { return Supports(QuerySupports.GetCurrent); } } /// /// Gets a value indicating whether is supported. /// /// /// true if this capability can get label; otherwise, false. /// public bool CanGetLabel { get { return Supports(QuerySupports.GetLabel); } } /// /// Gets a value indicating whether is supported. /// /// /// true if this capability can get help; otherwise, false. /// public bool CanGetHelp { get { return Supports(QuerySupports.GetHelp); } } /// /// Gets a value indicating whether is supported. /// /// /// true if this capability can get label enum; otherwise, false. /// public bool CanGetLabelEnum { get { return Supports(QuerySupports.GetLabelEnum); } } /// /// Gets a value indicating whether is supported. /// /// /// true if this capability can reset; otherwise, false. /// public bool CanReset { get { return Supports(QuerySupports.Reset); } } /// /// Gets a value indicating whether is supported. /// /// /// true if this capability can set; otherwise, false. /// public bool CanSet { get { return Supports(QuerySupports.Set); } } ///// ///// Gets a value indicating whether is supported. ///// ///// ///// true if this capability can set constraint; otherwise, false. ///// //public bool CanSetConstraint { get { return Supports(QuerySupports.SetConstraint); } } #endregion #region get methods /// /// Gets the default value of this capability. /// /// public TValue GetDefault() { if (CanGetDefault) { return _convertRoutine(_source.CapGetDefault(Capability)); } return default(TValue); } /// /// Gets the current value of this capability. /// /// public TValue GetCurrent() { if (CanGetCurrent) { return _convertRoutine(_source.CapGetCurrent(Capability)); } return default(TValue); } /// /// Gets all the possible values of this capability. /// /// public IList Get() { if (CanGet) { return _source.CapGet(Capability).Select(o => _convertRoutine(o)).ToList(); } return new List(); } /// /// [Experimental] Gets the label value of this capability. /// /// public string GetLabel() { object value = null; if (CanGetLabel) { using (TWCapability cap = new TWCapability(Capability)) { var rc = _source.DGControl.Capability.GetLabel(cap); if (rc == ReturnCode.Success) { var read = CapabilityReader.ReadValue(cap); switch (read.ContainerType) { case ContainerType.OneValue: // most likely not correct value = read.OneValue; break; } } } } return value == null ? null : value.ToString(); } /// /// [Experimental] Gets the help value of this capability. /// /// public string GetHelp() { object value = null; if (CanGetHelp) { using (TWCapability cap = new TWCapability(Capability)) { var rc = _source.DGControl.Capability.GetHelp(cap); if (rc == ReturnCode.Success) { var read = CapabilityReader.ReadValue(cap); switch (read.ContainerType) { case ContainerType.OneValue: // most likely not correct value = read.OneValue; break; } } } } return value == null ? null : value.ToString(); } /// /// [Experimental] Gets the display names for possible values of this capability. /// /// public IList GetLabelEnum() { var list = new List(); if (CanGetLabelEnum) { using (TWCapability cap = new TWCapability(Capability)) { var rc = _source.DGControl.Capability.GetLabelEnum(cap); if (rc == ReturnCode.Success) { cap.ReadMultiCapValues(list); } } } return list.Select(o => o.ToString()).ToList(); } #endregion #region set methods /// /// Resets all values and constraint to power-on defaults. /// /// public ReturnCode ResetAll() { return _source.CapResetAll(Capability); } /// /// Resets the current value to power-on default. /// /// public ReturnCode Reset() { return _source.CapReset(Capability); } /// /// Sets the current value of this capability. /// /// The value. /// public ReturnCode Set(TValue value) { ReturnCode rc = ReturnCode.Failure; if (CanSet) { if (_setCustomRoutine != null) { rc = _setCustomRoutine(value); } else if (_setProvider != null) { using (var cap = _setProvider(value)) { rc = _source.DGControl.Capability.Set(cap); } } } return rc; } #endregion } }