Attempt on enable source that doesn't work yet.

This commit is contained in:
Eugene Wang
2023-04-02 09:07:30 -04:00
parent f8cd01069c
commit 9ddbe70ec7
12 changed files with 188 additions and 1 deletions

View File

@@ -42,6 +42,7 @@
label3 = new System.Windows.Forms.Label(); label3 = new System.Windows.Forms.Label();
btnOpenDef = new System.Windows.Forms.Button(); btnOpenDef = new System.Windows.Forms.Button();
btnClose = new System.Windows.Forms.Button(); btnClose = new System.Windows.Forms.Button();
btnShowSettings = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)splitContainer1).BeginInit(); ((System.ComponentModel.ISupportInitialize)splitContainer1).BeginInit();
splitContainer1.Panel1.SuspendLayout(); splitContainer1.Panel1.SuspendLayout();
splitContainer1.Panel2.SuspendLayout(); splitContainer1.Panel2.SuspendLayout();
@@ -158,6 +159,7 @@
// //
// splitContainer1.Panel2 // splitContainer1.Panel2
// //
splitContainer1.Panel2.Controls.Add(btnShowSettings);
splitContainer1.Panel2.Controls.Add(btnClose); splitContainer1.Panel2.Controls.Add(btnClose);
splitContainer1.Panel2.Controls.Add(label2); splitContainer1.Panel2.Controls.Add(label2);
splitContainer1.Panel2.Controls.Add(lblCurrent); splitContainer1.Panel2.Controls.Add(lblCurrent);
@@ -203,6 +205,16 @@
btnClose.UseVisualStyleBackColor = true; btnClose.UseVisualStyleBackColor = true;
btnClose.Click += btnClose_Click; btnClose.Click += btnClose_Click;
// //
// btnShowSettings
//
btnShowSettings.Location = new System.Drawing.Point(161, 32);
btnShowSettings.Name = "btnShowSettings";
btnShowSettings.Size = new System.Drawing.Size(132, 23);
btnShowSettings.TabIndex = 5;
btnShowSettings.Text = "Show settings only";
btnShowSettings.UseVisualStyleBackColor = true;
btnShowSettings.Click += btnShowSettings_Click;
//
// Form1 // Form1
// //
AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
@@ -236,5 +248,6 @@
private System.Windows.Forms.Button btnOpenDef; private System.Windows.Forms.Button btnOpenDef;
private System.Windows.Forms.Label lblState; private System.Windows.Forms.Label lblState;
private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button btnShowSettings;
} }
} }

View File

@@ -100,5 +100,10 @@ namespace WinForm32
twain.DGControl.Identity.OpenDS(twain.DefaultSource); twain.DGControl.Identity.OpenDS(twain.DefaultSource);
} }
private void btnShowSettings_Click(object sender, EventArgs e)
{
twain.EnableSource(true, true);
}
} }
} }

View File

@@ -81,5 +81,12 @@ namespace NTwain.DSM
ref TW_IDENTITY_MACOSX origin, ref TW_IDENTITY_MACOSX dest, ref TW_IDENTITY_MACOSX origin, ref TW_IDENTITY_MACOSX dest,
DG dg, DAT dat, MSG msg, ref DG xfergroup DG dg, DAT dat, MSG msg, ref DG xfergroup
); );
[DllImport(DsmName, CharSet = CharSet.Ansi)]
internal static extern ushort DSM_Entry
(
ref TW_IDENTITY_MACOSX origin, ref TW_IDENTITY_MACOSX dest,
DG dg, DAT dat, MSG msg, ref TW_USERINTERFACE userinterface
);
} }
} }

View File

@@ -81,5 +81,12 @@ namespace NTwain.DSM
ref TW_IDENTITY_MACOSX origin, ref TW_IDENTITY_MACOSX dest, ref TW_IDENTITY_MACOSX origin, ref TW_IDENTITY_MACOSX dest,
DG dg, DAT dat, MSG msg, ref DG xfergroup DG dg, DAT dat, MSG msg, ref DG xfergroup
); );
[DllImport(DsmName, CharSet = CharSet.Ansi)]
internal static extern ushort DSM_Entry
(
ref TW_IDENTITY_MACOSX origin, ref TW_IDENTITY_MACOSX dest,
DG dg, DAT dat, MSG msg, ref TW_USERINTERFACE userinterface
);
} }
} }

View File

@@ -81,5 +81,12 @@ namespace NTwain.DSM
ref TW_IDENTITY_LEGACY origin, ref TW_IDENTITY_LEGACY dest, ref TW_IDENTITY_LEGACY origin, ref TW_IDENTITY_LEGACY dest,
DG dg, DAT dat, MSG msg, ref DG xfergroup DG dg, DAT dat, MSG msg, ref DG xfergroup
); );
[DllImport(DsmName, CharSet = CharSet.Ansi)]
internal static extern ushort DSM_Entry
(
ref TW_IDENTITY_LEGACY origin, ref TW_IDENTITY_LEGACY dest,
DG dg, DAT dat, MSG msg, ref TW_USERINTERFACE userinterface
);
} }
} }

View File

@@ -81,5 +81,12 @@ namespace NTwain.DSM
ref TW_IDENTITY_LEGACY origin, ref TW_IDENTITY_LEGACY dest, ref TW_IDENTITY_LEGACY origin, ref TW_IDENTITY_LEGACY dest,
DG dg, DAT dat, MSG msg, ref DG xfergroup DG dg, DAT dat, MSG msg, ref DG xfergroup
); );
[DllImport(DsmName, CharSet = CharSet.Ansi)]
internal static extern ushort DSM_Entry
(
ref TW_IDENTITY_LEGACY origin, ref TW_IDENTITY_LEGACY dest,
DG dg, DAT dat, MSG msg, ref TW_USERINTERFACE userinterface
);
} }
} }

View File

@@ -0,0 +1,92 @@
using NTwain.DSM;
using TWAINWorkingGroup;
namespace NTwain.Triplets.ControlDATs
{
/// <summary>
/// Contains calls used with <see cref="DG.CONTROL"/> and <see cref="DAT.USERINTERFACE"/>.
/// </summary>
public class UserInterface : TripletBase
{
public UserInterface(TwainSession session) : base(session)
{
}
/// <summary>
/// Disables source to bring state down to <see cref="STATE.S4"/>.
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public STS DisableDS(ref TW_USERINTERFACE data)
{
var rc = DoIt(MSG.DISABLEDS, ref data);
if (rc == STS.SUCCESS)
{
Session.State = STATE.S4;
}
return rc;
}
/// <summary>
/// Enables source to bring state up to <see cref="STATE.S5"/>.
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public STS EnableDS(ref TW_USERINTERFACE data)
{
var rc = DoIt(MSG.ENABLEDS, ref data);
if (rc == STS.SUCCESS || (data.ShowUI == 0 && rc == STS.CHECKSTATUS))
{
Session.State = STATE.S5;
}
return rc;
}
/// <summary>
/// Enables source to bring state up to <see cref="STATE.S5"/>.
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public STS EnableDSUIOnly(ref TW_USERINTERFACE data)
{
var rc = DoIt(MSG.ENABLEDSUIONLY, ref data);
if (rc == STS.SUCCESS)
{
Session.State = STATE.S5;
}
return rc;
}
STS DoIt(MSG msg, ref TW_USERINTERFACE data)
{
var rc = STS.FAILURE;
if (TwainPlatform.IsWindows)
{
var app = Session.AppIdentity;
var ds = Session.CurrentSource;
if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM)
{
rc = (STS)WinLegacyDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.USERINTERFACE, msg, ref data);
}
else
{
rc = (STS)WinNewDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.USERINTERFACE, msg, ref data);
}
}
else if (TwainPlatform.IsMacOSX)
{
TW_IDENTITY_MACOSX app = Session.AppIdentity;
TW_IDENTITY_MACOSX ds = Session.CurrentSource;
if (TwainPlatform.PreferLegacyDSM)
{
rc = (STS)OSXLegacyDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.USERINTERFACE, msg, ref data);
}
else
{
rc = (STS)OSXNewDSM.DSM_Entry(ref app, ref ds, DG.CONTROL, DAT.USERINTERFACE, msg, ref data);
}
}
return rc;
}
}
}

View File

@@ -41,5 +41,8 @@ namespace NTwain.Triplets
private XferGroup? _xferGroup; private XferGroup? _xferGroup;
public XferGroup XferGroup => _xferGroup ??= new XferGroup(Session); public XferGroup XferGroup => _xferGroup ??= new XferGroup(Session);
private UserInterface? _userInterface;
public UserInterface UserInterface => _userInterface ??= new UserInterface(Session);
} }
} }

View File

@@ -14,7 +14,7 @@ namespace NTwain.Triplets
/// <exception cref="System.ArgumentNullException"></exception> /// <exception cref="System.ArgumentNullException"></exception>
public TripletBase(TwainSession session) public TripletBase(TwainSession session)
{ {
Session = session ?? throw new ArgumentNullException("session"); Session = session ?? throw new ArgumentNullException(nameof(session));
} }
/// <summary> /// <summary>

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Diagnostics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using TWAINWorkingGroup; using TWAINWorkingGroup;
@@ -55,6 +56,7 @@ namespace NTwain
IntPtr twnull IntPtr twnull
) )
{ {
Debug.WriteLine($"Legacy callback got {msg}");
HandleSourceMsg(msg); HandleSourceMsg(msg);
return (ushort)STS.SUCCESS; return (ushort)STS.SUCCESS;
} }
@@ -69,6 +71,7 @@ namespace NTwain
IntPtr twnull IntPtr twnull
) )
{ {
Debug.WriteLine($"OSX callback got {msg}");
HandleSourceMsg(msg); HandleSourceMsg(msg);
return (ushort)STS.SUCCESS; return (ushort)STS.SUCCESS;
} }
@@ -82,10 +85,17 @@ namespace NTwain
case MSG.DEVICEEVENT: case MSG.DEVICEEVENT:
break; break;
case MSG.CLOSEDSOK: case MSG.CLOSEDSOK:
DisableSource();
break; break;
case MSG.CLOSEDSREQ: case MSG.CLOSEDSREQ:
DisableSource();
break; break;
} }
} }
public void HandleWin32Message(uint msg)
{
}
} }
} }

View File

@@ -21,5 +21,37 @@ namespace NTwain
rc = DGControl.Identity.GetNext(out ds); rc = DGControl.Identity.GetNext(out ds);
} }
} }
/// <summary>
/// Enables the currently open source.
/// </summary>
/// <param name="showUI">Whether to show driver interface.</param>
/// <param name="uiOnly">If true try to display only driver dialog (no capture).
/// Otherwise capturing will begin after this.</param>
/// <returns></returns>
public STS EnableSource(bool showUI, bool uiOnly)
{
var ui = new TW_USERINTERFACE
{
ShowUI = (ushort)((showUI || uiOnly) ? 1 : 0),
hParent = _hwnd,
};
var rc = uiOnly ? DGControl.UserInterface.EnableDSUIOnly(ref ui) : DGControl.UserInterface.EnableDS(ref ui);
if (rc == STS.SUCCESS || (!uiOnly && !showUI && rc == STS.CHECKSTATUS))
{
// keep it around for disable use
_userInterface = ui;
};
return rc;
}
/// <summary>
/// Disables the currently enabled source.
/// </summary>
/// <returns></returns>
public STS DisableSource()
{
return DGControl.UserInterface.DisableDS(ref _userInterface);
}
} }
} }

View File

@@ -87,6 +87,7 @@ namespace NTwain
} }
internal IntPtr _hwnd; internal IntPtr _hwnd;
internal TW_USERINTERFACE _userInterface;
/// <summary> /// <summary>
/// Tries to bring the TWAIN session down to some state. /// Tries to bring the TWAIN session down to some state.
@@ -102,6 +103,9 @@ namespace NTwain
switch (State) switch (State)
{ {
case STATE.S5:
DGControl.UserInterface.DisableDS(ref _userInterface);
break;
case STATE.S4: case STATE.S4:
DGControl.Identity.CloseDS(); DGControl.Identity.CloseDS();
break; break;