using NTwain.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NTwain
{
//TODO: handle multi-value sets
///
/// Wrapped class for reading/writing a TWAIN capability associated with a .
///
/// The TWAIN type of the value.
public class CapWrapper : NTwain.ICapWrapper
{
ICapControl _source;
Func _getConvertRoutine;
Func _setCustomRoutine;
Func _setOneValueFunc;
///
/// Initializes a new instance of the class.
///
/// The source.
/// The capability.
/// The value conversion routine in Get methods.
///
/// source
/// or
/// getConversionRoutine
///
public CapWrapper(ICapControl source, CapabilityId capability,
Func getConversionRoutine)
{
if (source == null) { throw new ArgumentNullException("source"); }
if (getConversionRoutine == null) { throw new ArgumentNullException("getConversionRoutine"); }
_source = source;
_getConvertRoutine = getConversionRoutine;
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 provide the capability object for set method.
///
/// source
/// or
/// getConversionRoutine
/// or
/// setValueProvider
///
public CapWrapper(ICapControl source, CapabilityId capability,
Func getConversionRoutine,
Func setValueProvider)
{
if (source == null) { throw new ArgumentNullException("source"); }
if (getConversionRoutine == null) { throw new ArgumentNullException("getConversionRoutine"); }
if (setValueProvider == null) { throw new ArgumentNullException("setValueProvider"); }
_source = source;
_getConvertRoutine = getConversionRoutine;
_setOneValueFunc = 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
/// getConversionRoutine
/// or
/// setValueRoutine
///
public CapWrapper(ICapControl source, CapabilityId capability,
Func getConversionRoutine,
Func setValueRoutine)
{
if (source == null) { throw new ArgumentNullException("source"); }
if (getConversionRoutine == null) { throw new ArgumentNullException("getConversionRoutine"); }
if (setValueRoutine == null) { throw new ArgumentNullException("setValueRoutine"); }
_source = source;
_getConvertRoutine = 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 _getConvertRoutine(_source.CapGetDefault(Capability));
}
return default(TValue);
}
///
/// Gets the current value of this capability.
///
///
public TValue GetCurrent()
{
if (CanGetCurrent)
{
return _getConvertRoutine(_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 => _getConvertRoutine(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)
{
CapabilityReader.ReadValue(cap).PopulateFromCapValues(list);
}
}
}
return list.Select(o => o.ToString()).ToList();
}
#endregion
#region set methods
///
/// Resets the current value to power-on default.
///
///
public ReturnCode Reset()
{
return _source.CapReset(Capability);
}
///
/// Simplified version that sets the current value of this capability.
///
/// The value.
///
/// Simple Set() is not defined for this capability.
public ReturnCode Set(TValue value)
{
ReturnCode rc = ReturnCode.Failure;
if (CanSet)
{
if (_setCustomRoutine != null)
{
rc = _setCustomRoutine(value);
}
else if (_setOneValueFunc != null)
{
using (var cap = new TWCapability(Capability, _setOneValueFunc(value)))
{
rc = _source.DGControl.Capability.Set(cap);
}
}
else
{
throw new InvalidOperationException("Simple Set() is not defined for this capability.");
}
}
return rc;
}
///
/// A version of Set that uses an array.
///
/// The value.
///
public ReturnCode Set(TWArray value)
{
ReturnCode rc = ReturnCode.Failure;
if (CanSet)
{
using (var cap = new TWCapability(Capability, value))
{
rc = _source.DGControl.Capability.Set(cap);
}
}
return rc;
}
///
/// A version of Set that uses an enumeration.
///
/// The value.
///
public ReturnCode Set(TWEnumeration value)
{
ReturnCode rc = ReturnCode.Failure;
if (CanSet)
{
using (var cap = new TWCapability(Capability, value))
{
rc = _source.DGControl.Capability.Set(cap);
}
}
return rc;
}
///
/// Sets the constraint value of this capability.
///
/// The value.
///
public ReturnCode SetConstraint(TWOneValue value)
{
ReturnCode rc = ReturnCode.Failure;
if (CanSetConstraint)
{
using (var cap = new TWCapability(Capability, value))
{
rc = _source.DGControl.Capability.SetConstraint(cap);
}
}
return rc;
}
///
/// Sets the constraint value of this capability.
///
/// The value.
///
public ReturnCode SetConstraint(TWEnumeration value)
{
ReturnCode rc = ReturnCode.Failure;
if (CanSetConstraint)
{
using (var cap = new TWCapability(Capability, value))
{
rc = _source.DGControl.Capability.SetConstraint(cap);
}
}
return rc;
}
///
/// Sets the constraint value of this capability.
///
/// The value.
///
public ReturnCode SetConstraint(TWRange value)
{
ReturnCode rc = ReturnCode.Failure;
if (CanSetConstraint)
{
using (var cap = new TWCapability(Capability, value))
{
rc = _source.DGControl.Capability.SetConstraint(cap);
}
}
return rc;
}
#endregion
}
}