Added wrapping methods to userinterface triplets.

This commit is contained in:
Eugene Wang
2018-11-17 12:21:17 -05:00
parent bc1dfd2992
commit 751f885949
7 changed files with 94 additions and 51 deletions

View File

@@ -21,6 +21,7 @@ namespace ConsoleApp
using (var session = new TwainSession(config)) using (var session = new TwainSession(config))
{ {
session.PropertyChanged += Session_PropertyChanged; session.PropertyChanged += Session_PropertyChanged;
session.SourceDisabled += Session_SourceDisabled;
if (session.Open(IntPtr.Zero) == NTwain.Data.ReturnCode.Success) if (session.Open(IntPtr.Zero) == NTwain.Data.ReturnCode.Success)
{ {
@@ -67,17 +68,24 @@ namespace ConsoleApp
Console.ReadLine(); Console.ReadLine();
} }
private static void TestThisSource(TwainSession session, DataSource targetSrc) private static void Session_SourceDisabled(object sender, EventArgs e)
{ {
Console.WriteLine($"Testing data source {targetSrc}"); var session = (TwainSession)sender;
Console.WriteLine($"Session source disabled.");
session.CurrentSource.Close();
}
private static void TestThisSource(TwainSession session, DataSource source)
{
Console.WriteLine($"Testing data source {source}");
Console.WriteLine(); Console.WriteLine();
targetSrc.Open(); source.Open();
var testStatus = session.GetStatus(); var testStatus = session.GetStatus();
var testMessage = session.GetLocalizedStatus(ref testStatus); var testMessage = session.GetLocalizedStatus(ref testStatus);
targetSrc.Close(); var rc = source.ShowUI(IntPtr.Zero);
} }
private static void Session_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) private static void Session_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)

View File

@@ -532,9 +532,9 @@ namespace NTwain.Data
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
partial struct TW_USERINTERFACE partial struct TW_USERINTERFACE
{ {
TW_BOOL _showUI; public TW_BOOL ShowUI;
TW_BOOL _modalUI; public TW_BOOL ModalUI;
TW_HANDLE _hParent; public TW_HANDLE hParent;
} }
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]

View File

@@ -2266,44 +2266,7 @@ namespace NTwain.Data
/// </summary> /// </summary>
public uint ReceiveSize { get => _ReceiveSize; } public uint ReceiveSize { get => _ReceiveSize; }
} }
/// <summary>
/// This structure is used to handle the user interface coordination between an application and a
/// Source.
/// </summary>
partial struct TW_USERINTERFACE
{
/// <summary>
/// Set to TRUE by the application if the Source should activate its built-in user
/// interface. Otherwise, set to FALSE. Note that not all sources support ShowUI =
/// FALSE.
/// </summary>
public bool ShowUI
{
get { return _showUI > 0; }
set { _showUI = value ? TwainConst.True : TwainConst.False; }
}
/// <summary>
/// If ShowUI is TRUE, then an application setting this to TRUE requests the Source to
/// run Modal.
/// </summary>
public bool ModalUI
{
//get { return _modalUI > 0; }
set { _modalUI = value ? TwainConst.True : TwainConst.False; }
}
/// <summary>
/// Microsoft Windows only: Applications window handle. The Source designates
/// the hWnd as its parent when creating the Source dialog.
/// </summary>
public IntPtr Parent
{
//get { return _hParent; }
set { _hParent = value; }
}
}
/// <summary> /// <summary>
/// Provides entry points required by TWAIN 2.0 Applications and Sources. /// Provides entry points required by TWAIN 2.0 Applications and Sources.
/// </summary> /// </summary>

View File

@@ -34,6 +34,50 @@ namespace NTwain
/// <returns></returns> /// <returns></returns>
public ReturnCode Close() => Session.DGControl.Identity.CloseDS(Identity32); public ReturnCode Close() => Session.DGControl.Identity.CloseDS(Identity32);
/// <summary>
/// Showing driver configuration UI if supported.
/// </summary>
/// <param name="windowHandle">The parent window handle if on Windows. Use <see cref="IntPtr.Zero"/> if not applicable.</param>
/// <param name="modal">if set to <c>true</c> the driver UI may be displayed as modal.</param>
/// <returns></returns>
public ReturnCode ShowUI(IntPtr windowHandle, bool modal = false)
{
var ui = new TW_USERINTERFACE
{
ShowUI = 1,
ModalUI = (ushort)(modal ? 1 : 0),
hParent = windowHandle
};
var rc = Session.DGControl.UserInterface.EnableDSUIOnly(ref ui);
if (rc == ReturnCode.Success)
{
Session._lastEnableUI = ui;
}
return rc;
}
/// <summary>
/// Enables the source for transferring data.
/// </summary>
/// <param name="showUI">if set to <c>true</c> then show driver UI. Not all sources support turning UI off.</param>
/// <param name="windowHandle">The parent window handle if on Windows. Use <see cref="IntPtr.Zero"/> if not applicable.</param>
/// <param name="modal">if set to <c>true</c> any driver UI may be displayed as modal.</param>
/// <returns></returns>
public ReturnCode Enable(bool showUI, IntPtr windowHandle, bool modal = false)
{
var ui = new TW_USERINTERFACE
{
ShowUI = (ushort)(showUI ? 1 : 0),
ModalUI = (ushort)(modal ? 1 : 0),
hParent = windowHandle
};
var rc = Session.DGControl.UserInterface.EnableDS(ref ui);
if (rc == ReturnCode.Success)
{
Session._lastEnableUI = ui;
}
return rc;
}
/// <summary> /// <summary>
/// Gets the source status. Useful after getting a non-success return code. /// Gets the source status. Useful after getting a non-success return code.

View File

@@ -9,7 +9,7 @@ namespace NTwain.Triplets.Control
sealed class UserInterface : BaseTriplet sealed class UserInterface : BaseTriplet
{ {
internal UserInterface(TwainSession session) : base(session) { } internal UserInterface(TwainSession session) : base(session) { }
public ReturnCode DisableDS(ref TW_USERINTERFACE ui) public ReturnCode DisableDS(ref TW_USERINTERFACE ui)
{ {
var rc = ReturnCode.Failure; var rc = ReturnCode.Failure;
@@ -38,21 +38,22 @@ namespace NTwain.Triplets.Control
rc = NativeMethods.DsmMac64(Session.Config.App32, Session.CurrentSource.Identity32, rc = NativeMethods.DsmMac64(Session.Config.App32, Session.CurrentSource.Identity32,
DataGroups.Control, DataArgumentType.UserInterface, Message.DisableDS, ref ui); DataGroups.Control, DataArgumentType.UserInterface, Message.DisableDS, ref ui);
} }
if (rc == ReturnCode.Success) if (rc == ReturnCode.Success)
{ {
Session.State = TwainState.S4; Session.State = TwainState.S4;
Session.OnSourceDisabled(EventArgs.Empty);
} }
return rc; return rc;
} }
public ReturnCode EnableDS(ref TW_USERINTERFACE ui) public ReturnCode EnableDS(ref TW_USERINTERFACE ui)
{ {
var rc = ReturnCode.Failure; var rc = ReturnCode.Failure;
if (Session.State == TwainState.S4) if (Session.State == TwainState.S4)
{ {
Session.State = TwainState.S5; //tentative Session.State = TwainState.S5; //tentative
if (Is32Bit) if (Is32Bit)
{ {
if (IsWin) if (IsWin)
@@ -79,14 +80,14 @@ namespace NTwain.Triplets.Control
} }
if (!(rc == ReturnCode.Success && if (!(rc == ReturnCode.Success &&
(!ui.ShowUI && rc == ReturnCode.CheckStatus))) (ui.ShowUI == 0 && rc == ReturnCode.CheckStatus)))
{ {
Session.State = TwainState.S4; Session.State = TwainState.S4;
} }
} }
return rc; return rc;
} }
public ReturnCode EnableDSUIOnly(ref TW_USERINTERFACE ui) public ReturnCode EnableDSUIOnly(ref TW_USERINTERFACE ui)
{ {
var rc = ReturnCode.Failure; var rc = ReturnCode.Failure;

View File

@@ -13,6 +13,8 @@ namespace NTwain
{ {
partial class TwainSession partial class TwainSession
{ {
internal TW_USERINTERFACE _lastEnableUI;
private void HandleSourceMsg(Message msg) private void HandleSourceMsg(Message msg)
{ {
switch (msg) switch (msg)
@@ -26,12 +28,23 @@ namespace NTwain
} }
break; break;
case Message.XferReady: case Message.XferReady:
if (State < TwainState.S6)
{
State = TwainState.S6;
}
DoTransferRoutine();
break; break;
case Message.CloseDSReq: case Message.CloseDSReq:
DGControl.UserInterface.DisableDS(ref _lastEnableUI);
break; break;
case Message.CloseDSOK: case Message.CloseDSOK:
break; break;
} }
} }
private void DoTransferRoutine()
{
throw new NotImplementedException();
}
} }
} }

View File

@@ -47,6 +47,20 @@ namespace NTwain
///// </summary> ///// </summary>
//public DGCustom DGCustom { get; set; } //public DGCustom DGCustom { get; set; }
/// <summary>
/// Occurs when an enabled source has been disabled (back to state 4).
/// </summary>
public event EventHandler SourceDisabled;
/// <summary>
/// Raises the <see cref="SourceDisabled"/> event.
/// </summary>
/// <param name="e"></param>
internal protected virtual void OnSourceDisabled(EventArgs e)
{
var handler = SourceDisabled;
handler?.Invoke(this, e);
}
/// <summary> /// <summary>
/// Occurs when the source has generated an event. /// Occurs when the source has generated an event.