Moved wrapped capabilities into its own class to reduce DataSource complexity.

This commit is contained in:
soukoku
2015-02-20 18:17:51 -05:00
parent 59e8b996de
commit b757cc4a42
14 changed files with 288 additions and 322 deletions

View File

@@ -46,15 +46,15 @@
<Reference Include="WindowsBase" /> <Reference Include="WindowsBase" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="..\NTwain\Capabilities.cs">
<Link>Capabilities.cs</Link>
</Compile>
<Compile Include="..\NTwain\CapabilityReader.cs"> <Compile Include="..\NTwain\CapabilityReader.cs">
<Link>CapabilityReader.cs</Link> <Link>CapabilityReader.cs</Link>
</Compile> </Compile>
<Compile Include="..\NTwain\CapWrapper.cs"> <Compile Include="..\NTwain\CapWrapper.cs">
<Link>CapWrapper.cs</Link> <Link>CapWrapper.cs</Link>
</Compile> </Compile>
<Compile Include="..\NTwain\DataSource.Caps.cs">
<Link>DataSource.Caps.cs</Link>
</Compile>
<Compile Include="..\NTwain\DataSource.cs"> <Compile Include="..\NTwain\DataSource.cs">
<Link>DataSource.cs</Link> <Link>DataSource.cs</Link>
</Compile> </Compile>
@@ -79,9 +79,6 @@
<Compile Include="..\NTwain\DeviceEventArgs.cs"> <Compile Include="..\NTwain\DeviceEventArgs.cs">
<Link>DeviceEventArgs.cs</Link> <Link>DeviceEventArgs.cs</Link>
</Compile> </Compile>
<Compile Include="..\NTwain\ICapControl.cs">
<Link>ICapControl.cs</Link>
</Compile>
<Compile Include="..\NTwain\ICapWrapper.cs"> <Compile Include="..\NTwain\ICapWrapper.cs">
<Link>ICapWrapper.cs</Link> <Link>ICapWrapper.cs</Link>
</Compile> </Compile>

View File

@@ -114,7 +114,7 @@ namespace NTwain
var srcVersion = _source.ProtocolVersion; var srcVersion = _source.ProtocolVersion;
if (srcVersion >= ProtocolVersions.GetMinimumVersion(Capability)) if (srcVersion >= ProtocolVersions.GetMinimumVersion(Capability))
{ {
_supports = _source.CapQuerySupport(Capability); _supports = _source.Capabilities.QuerySupport(Capability);
if (!_supports.HasValue) if (!_supports.HasValue)
{ {
@@ -275,7 +275,7 @@ namespace NTwain
{ {
if (CanGetDefault) if (CanGetDefault)
{ {
return _getConvertRoutine(_source.CapGetDefault(Capability)); return _getConvertRoutine(_source.Capabilities.GetDefault(Capability));
} }
return default(TValue); return default(TValue);
} }
@@ -288,7 +288,7 @@ namespace NTwain
{ {
if (CanGetCurrent) if (CanGetCurrent)
{ {
return _getConvertRoutine(_source.CapGetCurrent(Capability)); return _getConvertRoutine(_source.Capabilities.GetCurrent(Capability));
} }
return default(TValue); return default(TValue);
} }
@@ -299,7 +299,7 @@ namespace NTwain
/// <returns></returns> /// <returns></returns>
public IList<TValue> GetValues() public IList<TValue> GetValues()
{ {
return _source.CapGet(Capability).Select(o => _getConvertRoutine(o)).ToList(); return _source.Capabilities.GetValues(Capability).Select(o => _getConvertRoutine(o)).ToList();
} }
/// <summary> /// <summary>
@@ -384,7 +384,7 @@ namespace NTwain
/// <returns></returns> /// <returns></returns>
public ReturnCode Reset() public ReturnCode Reset()
{ {
return _source.CapReset(Capability); return _source.Capabilities.Reset(Capability);
} }
/// <summary> /// <summary>

File diff suppressed because it is too large Load Diff

View File

@@ -55,10 +55,10 @@ namespace NTwain
Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "Thread {0}: CloseSource.", Thread.CurrentThread.ManagedThreadId)); Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "Thread {0}: CloseSource.", Thread.CurrentThread.ManagedThreadId));
rc = _session.DGControl.Identity.CloseDS(); rc = _session.DGControl.Identity.CloseDS();
if (rc == ReturnCode.Success) //if (rc == ReturnCode.Success)
{ //{
SupportedCaps = null; // SupportedCaps = null;
} //}
_session.UpdateCallback(); _session.UpdateCallback();
}); });
return rc; return rc;
@@ -158,34 +158,48 @@ namespace NTwain
/// </value> /// </value>
public bool IsOpen { get { return _session.IsSourceOpen; } } public bool IsOpen { get { return _session.IsSourceOpen; } }
static readonly CapabilityId[] _emptyCapList = new CapabilityId[0]; //static readonly CapabilityId[] _emptyCapList = new CapabilityId[0];
//private IList<CapabilityId> _supportedCapsList;
///// <summary>
///// Gets the list of supported caps for this source.
///// </summary>
///// <value>
///// The supported caps.
///// </value>
//[Obsolete("Use CapSupportedCaps.Get() instead.")]
//public IList<CapabilityId> SupportedCaps
//{
// get
// {
// if (_supportedCapsList == null && _session.State > 3)
// {
// _supportedCapsList = CapSupportedCaps.GetValues();
// }
// return _supportedCapsList ?? _emptyCapList;
// }
// private set
// {
// _supportedCapsList = value;
// //OnPropertyChanged("SupportedCaps");
// }
//}
private Capabilities _caps;
private IList<CapabilityId> _supportedCapsList;
/// <summary> /// <summary>
/// Gets the list of supported caps for this source. /// Gets the capabilities for this data source.
/// </summary> /// </summary>
/// <value> /// <value>
/// The supported caps. /// The capabilities.
/// </value> /// </value>
[Obsolete("Use CapSupportedCaps.Get() instead.")] public Capabilities Capabilities
public IList<CapabilityId> SupportedCaps
{ {
get get { return _caps ?? (_caps = new Capabilities(this)); }
{
if (_supportedCapsList == null && _session.State > 3)
{
_supportedCapsList = CapSupportedCaps.GetValues();
}
return _supportedCapsList ?? _emptyCapList;
}
private set
{
_supportedCapsList = value;
//OnPropertyChanged("SupportedCaps");
}
} }
/// <summary> /// <summary>
/// Gets the triplet operations defined for control data group. /// Gets the triplet operations defined for control data group.
/// </summary> /// </summary>

Binary file not shown.

View File

@@ -1,54 +0,0 @@
using NTwain.Data;
using System;
using System.Collections.Generic;
namespace NTwain
{
/// <summary>
/// Interface for providing basic functions at controlling caps.
/// </summary>
public interface ICapControl : ITripletControl
{
/// <summary>
/// Gets all the possible values for a capability.
/// </summary>
/// <param name="capabilityId">The capability identifier.</param>
/// <returns></returns>
IList<object> CapGet(CapabilityId capabilityId);
/// <summary>
/// Gets the current value for a capability.
/// </summary>
/// <param name="capabilityId">The capability identifier.</param>
/// <returns></returns>
object CapGetCurrent(CapabilityId capabilityId);
/// <summary>
/// Gets the default value for a capability.
/// </summary>
/// <param name="capabilityId">The capability identifier.</param>
/// <returns></returns>
object CapGetDefault(CapabilityId capabilityId);
/// <summary>
/// Gets the supported operations for a capability.
/// </summary>
/// <param name="capabilityId">The capability identifier.</param>
/// <returns></returns>
QuerySupports? CapQuerySupport(CapabilityId capabilityId);
/// <summary>
/// Resets the current value to power-on default.
/// </summary>
/// <param name="capabilityId">The capability identifier.</param>
/// <returns></returns>
ReturnCode CapReset(CapabilityId capabilityId);
/// <summary>
/// Resets all values and constraints to power-on defaults.
/// </summary>
/// <returns></returns>
ReturnCode CapResetAll();
}
}

View File

@@ -6,7 +6,7 @@ namespace NTwain
/// <summary> /// <summary>
/// Represents a TWAIN data source. /// Represents a TWAIN data source.
/// </summary> /// </summary>
public interface IDataSource : ICapControl public interface IDataSource : ITripletControl
{ {
/// <summary> /// <summary>
/// Gets the source's product name. /// Gets the source's product name.
@@ -48,14 +48,6 @@ namespace NTwain
/// </value> /// </value>
Version ProtocolVersion { get; } Version ProtocolVersion { get; }
/// <summary>
/// Gets the supported caps for this source.
/// </summary>
/// <value>
/// The supported caps.
/// </value>
IList<CapabilityId> SupportedCaps { get; }
/// <summary> /// <summary>
/// Gets the source's version information. /// Gets the source's version information.
/// </summary> /// </summary>
@@ -72,6 +64,14 @@ namespace NTwain
/// </value> /// </value>
bool IsOpen { get; } bool IsOpen { get; }
/// <summary>
/// Gets the capabilities for this data source.
/// </summary>
/// <value>
/// The capabilities.
/// </value>
Capabilities Capabilities { get; }
/// <summary> /// <summary>
/// Opens the source for capability negotiation. /// Opens the source for capability negotiation.
/// </summary> /// </summary>

View File

@@ -68,7 +68,7 @@ namespace NTwain.Internals
// some DS end up getting none but we will assume it's image // some DS end up getting none but we will assume it's image
if (xferImage) if (xferImage)
{ {
var mech = session.CurrentSource.ICapXferMech.GetCurrent(); var mech = session.CurrentSource.Capabilities.ICapXferMech.GetCurrent();
switch (mech) switch (mech)
{ {
@@ -90,7 +90,7 @@ namespace NTwain.Internals
} }
if (xferAudio) if (xferAudio)
{ {
var mech = session.CurrentSource.ACapXferMech.GetCurrent(); var mech = session.CurrentSource.Capabilities.ACapXferMech.GetCurrent();
switch (mech) switch (mech)
{ {
case XferMech.File: case XferMech.File:

View File

@@ -63,7 +63,6 @@
<Compile Include="DeviceEventArgs.cs" /> <Compile Include="DeviceEventArgs.cs" />
<Compile Include="GlobalSuppressions.cs" /> <Compile Include="GlobalSuppressions.cs" />
<Compile Include="IDataSource.cs" /> <Compile Include="IDataSource.cs" />
<Compile Include="ICapControl.cs" />
<Compile Include="Internals\Extensions.cs" /> <Compile Include="Internals\Extensions.cs" />
<Compile Include="DataTransferredEventArgs.cs" /> <Compile Include="DataTransferredEventArgs.cs" />
<Compile Include="IMemoryManager.cs" /> <Compile Include="IMemoryManager.cs" />
@@ -108,7 +107,7 @@
<Compile Include="Triplets\Dsm.WinOld.cs" /> <Compile Include="Triplets\Dsm.WinOld.cs" />
<Compile Include="Triplets\Dsm.WinNew.cs" /> <Compile Include="Triplets\Dsm.WinNew.cs" />
<Compile Include="TwainSessionInternal.cs" /> <Compile Include="TwainSessionInternal.cs" />
<Compile Include="DataSource.Caps.cs" /> <Compile Include="Capabilities.cs" />
<Compile Include="TwainSession.cs" /> <Compile Include="TwainSession.cs" />
<Compile Include="DataSource.cs" /> <Compile Include="DataSource.cs" />
<Compile Include="TwainStateException.cs" /> <Compile Include="TwainStateException.cs" />

View File

@@ -87,7 +87,7 @@ myDS.DGImage...;
``` ```
Additionally, the DataSource class itself has some handy pre-defined wrappers for common capability Additionally, the DataSource class has a Capabilities property with pre-defined wrappers for capability
negotiation. You can use the wrapper properties to see what capabilities or their operations are negotiation. You can use the wrapper properties to see what capabilities or their operations are
supported. You also won't have to keep track of what value types to use since the wrapper defines it supported. You also won't have to keep track of what value types to use since the wrapper defines it
for you. Most capabilities only require a simple single value to set for you. Most capabilities only require a simple single value to set
@@ -107,10 +107,10 @@ and the wrapper makes it easy to do that (see example below):
// (note: the name of the wrapper property is the same as the CapabilityId enum) // (note: the name of the wrapper property is the same as the CapabilityId enum)
PixelType myValue = PixelType.BlackWhite; PixelType myValue = PixelType.BlackWhite;
if (myDS.ICapPixelType.CanSet && if (myDS.Capabilities.ICapPixelType.CanSet &&
myDS.ICapPixelType.GetValues().Contains(myValue)) myDS.Capabilities.ICapPixelType.GetValues().Contains(myValue))
{ {
myDS.ICapPixelType.SetValue(myValue); myDS.Capabilities.ICapPixelType.SetValue(myValue);
} }

View File

@@ -24,17 +24,17 @@ namespace Tester.WPF
var capName = cap.ToString(); var capName = cap.ToString();
var wrapProperty = ds.GetType().GetProperty(capName); var wrapProperty = ds.Capabilities.GetType().GetProperty(capName);
if (wrapProperty != null) if (wrapProperty != null)
{ {
_wrapper = wrapProperty.GetGetMethod().Invoke(ds, null); _wrapper = wrapProperty.GetGetMethod().Invoke(ds.Capabilities, null);
var wrapperType = _wrapper.GetType(); var wrapperType = _wrapper.GetType();
_getMethod = wrapperType.GetMethod("GetValues"); _getMethod = wrapperType.GetMethod("GetValues");
_getCurrentMethod = wrapperType.GetMethod("GetCurrent"); _getCurrentMethod = wrapperType.GetMethod("GetCurrent");
_setMethod = wrapperType.GetMethods().FirstOrDefault(m => m.Name == "SetValue"); _setMethod = wrapperType.GetMethods().FirstOrDefault(m => m.Name == "SetValue");
} }
var supportTest = ds.CapQuerySupport(cap); var supportTest = ds.Capabilities.QuerySupport(cap);
if (supportTest.HasValue) if (supportTest.HasValue)
{ {
Supports = supportTest.Value; Supports = supportTest.Value;
@@ -55,7 +55,7 @@ namespace Tester.WPF
{ {
if (_getMethod == null) if (_getMethod == null)
{ {
return _ds.CapGet(Cap); return _ds.Capabilities.GetValues(Cap);
} }
return _getMethod.Invoke(_wrapper, null) as IEnumerable; return _getMethod.Invoke(_wrapper, null) as IEnumerable;
} }
@@ -63,7 +63,7 @@ namespace Tester.WPF
{ {
if (_getMethod == null) if (_getMethod == null)
{ {
return _ds.CapGetCurrent(Cap); return _ds.Capabilities.GetCurrent(Cap);
} }
return _getCurrentMethod.Invoke(_wrapper, null); return _getCurrentMethod.Invoke(_wrapper, null);
} }

View File

@@ -49,7 +49,7 @@ namespace Tester.WPF
//rc = DGControl.Status.Get(dsId, ref stat); //rc = DGControl.Status.Get(dsId, ref stat);
if (rc == ReturnCode.Success) if (rc == ReturnCode.Success)
{ {
foreach (var c in DS.CapSupportedCaps.GetValues().Select(o => new CapVM(DS, o))) foreach (var c in DS.Capabilities.CapSupportedCaps.GetValues().Select(o => new CapVM(DS, o)))
{ {
Caps.Add(c); Caps.Add(c);
} }

View File

@@ -120,7 +120,7 @@ namespace Tester.WPF
} }
}, () => }, () =>
{ {
return _session.State == 4 && _session.CurrentSource.CapEnableDSUIOnly.GetCurrent() == BoolType.True; return _session.State == 4 && _session.CurrentSource.Capabilities.CapEnableDSUIOnly.GetCurrent() == BoolType.True;
})); }));
} }
} }
@@ -246,9 +246,9 @@ namespace Tester.WPF
void _session_TransferReady(object sender, TransferReadyEventArgs e) void _session_TransferReady(object sender, TransferReadyEventArgs e)
{ {
if (_session.CurrentSource.ICapXferMech.GetCurrent() == XferMech.File) if (_session.CurrentSource.Capabilities.ICapXferMech.GetCurrent() == XferMech.File)
{ {
var formats = _session.CurrentSource.ICapImageFileFormat.GetValues(); var formats = _session.CurrentSource.Capabilities.ICapImageFileFormat.GetValues();
var wantFormat = formats.Contains(FileFormat.Tiff) ? FileFormat.Tiff : FileFormat.Bmp; var wantFormat = formats.Contains(FileFormat.Tiff) ? FileFormat.Tiff : FileFormat.Bmp;
var fileSetup = new TWSetupFileXfer var fileSetup = new TWSetupFileXfer

View File

@@ -230,7 +230,7 @@ namespace Tester.Winform
_stopScan = false; _stopScan = false;
if (_twain.CurrentSource.CapUIControllable.IsSupported)//.SupportedCaps.Contains(CapabilityId.CapUIControllable)) if (_twain.CurrentSource.Capabilities.CapUIControllable.IsSupported)//.SupportedCaps.Contains(CapabilityId.CapUIControllable))
{ {
// hide scanner ui if possible // hide scanner ui if possible
if (_twain.CurrentSource.Enable(SourceEnableMode.NoUI, false, this.Handle) == ReturnCode.Success) if (_twain.CurrentSource.Enable(SourceEnableMode.NoUI, false, this.Handle) == ReturnCode.Success)
@@ -301,26 +301,26 @@ namespace Tester.Winform
var src = _twain.CurrentSource; var src = _twain.CurrentSource;
_loadingCaps = true; _loadingCaps = true;
var test = src.SupportedCaps; //var test = src.SupportedCaps;
if (groupDepth.Enabled = src.ICapPixelType.IsSupported) if (groupDepth.Enabled = src.Capabilities.ICapPixelType.IsSupported)
{ {
LoadDepth(src.ICapPixelType); LoadDepth(src.Capabilities.ICapPixelType);
} }
if (groupDPI.Enabled = src.ICapXResolution.IsSupported && src.ICapYResolution.IsSupported) if (groupDPI.Enabled = src.Capabilities.ICapXResolution.IsSupported && src.Capabilities.ICapYResolution.IsSupported)
{ {
LoadDPI(src.ICapXResolution); LoadDPI(src.Capabilities.ICapXResolution);
} }
// TODO: find out if this is how duplex works or also needs the other option // TODO: find out if this is how duplex works or also needs the other option
if (groupDuplex.Enabled = src.CapDuplexEnabled.IsSupported) if (groupDuplex.Enabled = src.Capabilities.CapDuplexEnabled.IsSupported)
{ {
LoadDuplex(src.CapDuplexEnabled); LoadDuplex(src.Capabilities.CapDuplexEnabled);
} }
if (groupSize.Enabled = src.ICapSupportedSizes.IsSupported) if (groupSize.Enabled = src.Capabilities.ICapSupportedSizes.IsSupported)
{ {
LoadPaperSize(src.ICapSupportedSizes); LoadPaperSize(src.Capabilities.ICapSupportedSizes);
} }
btnAllSettings.Enabled = src.CapEnableDSUIOnly.IsSupported; btnAllSettings.Enabled = src.Capabilities.CapEnableDSUIOnly.IsSupported;
_loadingCaps = false; _loadingCaps = false;
} }
@@ -380,7 +380,7 @@ namespace Tester.Winform
if (!_loadingCaps && _twain.State == 4) if (!_loadingCaps && _twain.State == 4)
{ {
var sel = (SupportedSize)comboSize.SelectedItem; var sel = (SupportedSize)comboSize.SelectedItem;
_twain.CurrentSource.ICapSupportedSizes.SetValue(sel); _twain.CurrentSource.Capabilities.ICapSupportedSizes.SetValue(sel);
} }
} }
@@ -389,7 +389,7 @@ namespace Tester.Winform
if (!_loadingCaps && _twain.State == 4) if (!_loadingCaps && _twain.State == 4)
{ {
var sel = (PixelType)comboDepth.SelectedItem; var sel = (PixelType)comboDepth.SelectedItem;
_twain.CurrentSource.ICapPixelType.SetValue(sel); _twain.CurrentSource.Capabilities.ICapPixelType.SetValue(sel);
} }
} }
@@ -398,8 +398,8 @@ namespace Tester.Winform
if (!_loadingCaps && _twain.State == 4) if (!_loadingCaps && _twain.State == 4)
{ {
var sel = (TWFix32)comboDPI.SelectedItem; var sel = (TWFix32)comboDPI.SelectedItem;
_twain.CurrentSource.ICapXResolution.SetValue(sel); _twain.CurrentSource.Capabilities.ICapXResolution.SetValue(sel);
_twain.CurrentSource.ICapYResolution.SetValue(sel); _twain.CurrentSource.Capabilities.ICapYResolution.SetValue(sel);
} }
} }
@@ -407,7 +407,7 @@ namespace Tester.Winform
{ {
if (!_loadingCaps && _twain.State == 4) if (!_loadingCaps && _twain.State == 4)
{ {
_twain.CurrentSource.CapDuplexEnabled.SetValue(ckDuplex.Checked ? BoolType.True : BoolType.False); _twain.CurrentSource.Capabilities.CapDuplexEnabled.SetValue(ckDuplex.Checked ? BoolType.True : BoolType.False);
} }
} }