mirror of
https://github.com/soukoku/ntwain.git
synced 2025-04-05 20:59:23 +08:00
Added CapabilitiesExt.cs to handle custom capabilities if present in CapSupportedCaps
This commit is contained in:
parent
25b612633c
commit
d3bd1db863
@ -21,6 +21,7 @@ namespace NTwain
|
||||
{
|
||||
if (source == null) { throw new ArgumentNullException("source"); }
|
||||
_source = source;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -37,7 +38,9 @@ namespace NTwain
|
||||
using (TWCapability cap = new TWCapability(capabilityId))
|
||||
{
|
||||
cap.ContainerType = ContainerType.OneValue;
|
||||
|
||||
var rc = _source.DGControl.Capability.QuerySupport(cap);
|
||||
|
||||
if (rc == ReturnCode.Success)
|
||||
{
|
||||
var read = CapabilityReader.ReadValue(cap);
|
||||
@ -46,6 +49,8 @@ namespace NTwain
|
||||
{
|
||||
retVal = read.OneValue.ConvertToEnum<QuerySupports>();
|
||||
}
|
||||
|
||||
//ICapWrapper<Int64> cw = new CapWrapper<Int64>(_source, capabilityId, ValueExtensions.ConvertToEnum<Int64>, false);
|
||||
}
|
||||
}
|
||||
return retVal;
|
||||
|
137
src/NTwain/CapabilitiesExt.cs
Normal file
137
src/NTwain/CapabilitiesExt.cs
Normal file
@ -0,0 +1,137 @@
|
||||
using NTwain.Data;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NTwain
|
||||
{
|
||||
public class CapabilitiesExt : Capabilities
|
||||
{
|
||||
private IDataSource _source;
|
||||
public CapabilitiesExt(IDataSource dataSource) : base(dataSource) { _source = dataSource; }
|
||||
|
||||
private List<CapabilityId> _custom;
|
||||
|
||||
public List<CapabilityId> CustomCapabilities
|
||||
{
|
||||
get
|
||||
{
|
||||
return _custom ?? LoadCustomCapabilities();
|
||||
}
|
||||
}
|
||||
|
||||
private List<CapabilityId> LoadCustomCapabilities()
|
||||
{
|
||||
List<CapabilityId> custom = new List<CapabilityId>();
|
||||
foreach (CapabilityId capId in CapSupportedCaps.GetValues())
|
||||
{
|
||||
var capName = capId.ToString();
|
||||
var wrapper = GetType().GetProperty(capName);
|
||||
|
||||
// not defined in Capabilites
|
||||
if (wrapper == null)
|
||||
{
|
||||
custom.Add(capId);
|
||||
}
|
||||
}
|
||||
return custom;
|
||||
}
|
||||
|
||||
|
||||
private Func<BoolType, TWOneValue> boolFunc = value => new TWOneValue
|
||||
{
|
||||
Item = (uint)value,
|
||||
ItemType = ItemType.Bool
|
||||
};
|
||||
|
||||
private Func<int, TWOneValue> intFunc = value => new TWOneValue
|
||||
{
|
||||
Item = (uint)value,
|
||||
ItemType = ItemType.UInt16
|
||||
};
|
||||
|
||||
private Func<byte, TWOneValue> byteFunc = value => new TWOneValue
|
||||
{
|
||||
Item = value,
|
||||
ItemType = ItemType.UInt8
|
||||
};
|
||||
|
||||
private Func<uint, TWOneValue> uintFunc = value => new TWOneValue
|
||||
{
|
||||
Item = value,
|
||||
ItemType = ItemType.UInt32
|
||||
};
|
||||
|
||||
|
||||
public object GetCap<TValue> (CapabilityId Capability)
|
||||
{
|
||||
QuerySupports? s = QuerySupport(Capability);
|
||||
bool readOnly = true;
|
||||
byte b = 0;
|
||||
if (s != null)
|
||||
{
|
||||
b = (byte)s;
|
||||
if ((b & (1 << 2)) != 0)
|
||||
{
|
||||
readOnly = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof(TValue) == typeof(string))
|
||||
{
|
||||
return new CapWrapper<string>(_source, Capability, ValueExtensions.ConvertToString, readOnly);
|
||||
}
|
||||
else if (typeof(TValue) == typeof(int))
|
||||
{
|
||||
if (readOnly)
|
||||
{
|
||||
return new CapWrapper<int>(_source, Capability, ValueExtensions.ConvertToEnum<int>, true);
|
||||
}
|
||||
return new CapWrapper<int>(_source, Capability, ValueExtensions.ConvertToEnum<int>, intFunc);
|
||||
}
|
||||
else if (typeof(TValue) == typeof(byte))
|
||||
{
|
||||
if (readOnly)
|
||||
{
|
||||
return new CapWrapper<byte>(_source, Capability, ValueExtensions.ConvertToEnum<byte>, true);
|
||||
}
|
||||
return new CapWrapper<byte>(_source, Capability, ValueExtensions.ConvertToEnum<byte>, byteFunc);
|
||||
}
|
||||
else if (typeof(TValue) == typeof(uint))
|
||||
{
|
||||
if (readOnly)
|
||||
{
|
||||
return new CapWrapper<uint>(_source, Capability, ValueExtensions.ConvertToEnum<uint>, true);
|
||||
}
|
||||
return new CapWrapper<uint>(_source, Capability, ValueExtensions.ConvertToEnum<uint>, uintFunc);
|
||||
}
|
||||
else if (typeof(TValue) == typeof(TWFrame))
|
||||
{
|
||||
return new CapWrapper<TWFrame>(_source, Capability, ValueExtensions.ConvertToFrame, readOnly);
|
||||
}
|
||||
else if (typeof(TValue) == typeof(BoolType))
|
||||
{
|
||||
if (readOnly)
|
||||
{
|
||||
return new CapWrapper<BoolType>(_source, Capability, ValueExtensions.ConvertToEnum<BoolType>, true);
|
||||
}
|
||||
|
||||
return new CapWrapper<BoolType>(_source, Capability, ValueExtensions.ConvertToEnum<BoolType>, boolFunc);
|
||||
}
|
||||
else if (typeof(TValue) == typeof(TWFix32))
|
||||
{
|
||||
|
||||
if (readOnly)
|
||||
{
|
||||
return new CapWrapper<TWFix32>(_source, Capability, ValueExtensions.ConvertToFix32, true);
|
||||
}
|
||||
return new CapWrapper<TWFix32>(_source, Capability, ValueExtensions.ConvertToFix32, value => value.ToOneValue());
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Unknown defintion for type of {typeof(TValue)} in GetCap");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -255,7 +255,7 @@ namespace NTwain
|
||||
// }
|
||||
//}
|
||||
|
||||
private Capabilities _caps;
|
||||
private CapabilitiesExt _caps;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the capabilities for this data source.
|
||||
@ -265,7 +265,7 @@ namespace NTwain
|
||||
/// </value>
|
||||
public ICapabilities Capabilities
|
||||
{
|
||||
get { return _caps ?? (_caps = new Capabilities(this)); }
|
||||
get { return _caps ?? (_caps = new CapabilitiesExt(this)); }
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user