Files
ntwain/NTwain/CapWrapper.cs
2021-04-25 09:36:21 -04:00

144 lines
4.4 KiB
C#

using System.Collections.Generic;
using System.Linq;
using TWAINWorkingGroup;
namespace NTwain
{
/// <summary>
/// Contains operations for a <see cref="CAP"/>.
/// </summary>
/// <typeparam name="TValue">Individual value type of the cap. Must be one of TWAIN's supported cap value types.
/// You are responsible for using the correct type for a cap.</typeparam>
public class CapWrapper<TValue> where TValue : struct
{
protected readonly TWAIN _twain;
public CapWrapper(TWAIN twain, CAP cap)
{
_twain = twain;
Cap = cap;
var twCap = new TW_CAPABILITY
{
Cap = cap
};
var sts = _twain.DatCapability(DG.CONTROL, MSG.QUERYSUPPORT, ref twCap);
if (sts == STS.SUCCESS && twCap.ConType == TWON.ONEVALUE)
{
Supports = ValueReader.ReadOneValue<TWQC>(_twain, twCap);
}
}
/// <summary>
/// The operations supported by the cap.
/// Not all sources supports this so it may be unknown.
/// </summary>
public TWQC Supports { get; }
/// <summary>
/// The cap being targeted.
/// </summary>
public CAP Cap { get; }
/// <summary>
/// Try to get list of the cap's supported values.
/// </summary>
/// <returns></returns>
public IList<TValue> GetValues()
{
var twCap = new TW_CAPABILITY
{
Cap = Cap
};
var sts = _twain.DatCapability(DG.CONTROL, MSG.GET, ref twCap);
if (sts == STS.SUCCESS)
{
switch (twCap.ConType)
{
case TWON.ONEVALUE:
return new[] { ValueReader.ReadOneValue<TValue>(_twain, twCap) };
case TWON.ENUMERATION:
return ValueReader.ReadEnumeration<TValue>(_twain, twCap);
case TWON.ARRAY:
return ValueReader.ReadArray<TValue>(_twain, twCap);
case TWON.RANGE:
return ValueReader.ReadRange<TValue>(_twain, twCap).values.ToList();
}
}
return new TValue[0];
}
/// <summary>
/// Try to get the cap's current value.
/// </summary>
/// <returns></returns>
public TValue GetCurrent()
{
var twCap = new TW_CAPABILITY
{
Cap = Cap
};
var sts = _twain.DatCapability(DG.CONTROL, MSG.GETCURRENT, ref twCap);
if (sts == STS.SUCCESS)
{
switch (twCap.ConType)
{
case TWON.ONEVALUE:
return ValueReader.ReadOneValue<TValue>(_twain, twCap);
case TWON.ENUMERATION:
return ValueReader.ReadEnumeration<TValue>(_twain, twCap)[0];
case TWON.RANGE:
return ValueReader.ReadRange<TValue>(_twain, twCap).currentVal;
}
}
return default(TValue);
}
/// <summary>
/// Try to get the cap's default value.
/// </summary>
/// <returns></returns>
public TValue GetDefault()
{
var twCap = new TW_CAPABILITY
{
Cap = Cap
};
var sts = _twain.DatCapability(DG.CONTROL, MSG.GETDEFAULT, ref twCap);
if (sts == STS.SUCCESS)
{
switch (twCap.ConType)
{
case TWON.ONEVALUE:
return ValueReader.ReadOneValue<TValue>(_twain, twCap);
case TWON.ENUMERATION:
return ValueReader.ReadEnumeration<TValue>(_twain, twCap)[0];
case TWON.RANGE:
return ValueReader.ReadRange<TValue>(_twain, twCap).defaultVal;
}
}
return default(TValue);
}
/// <summary>
/// Resets the cap's current value to power-on default.
/// </summary>
/// <returns></returns>
public STS Reset()
{
var twCap = new TW_CAPABILITY
{
Cap = Cap
};
var sts = _twain.DatCapability(DG.CONTROL, MSG.RESET, ref twCap);
return sts;
}
}
}