2014-04-05 20:14:19 -04:00
using System ;
using NTwain.Triplets ;
using NTwain.Data ;
using NTwain.Values ;
using System.Windows.Forms ;
using System.Security.Permissions ;
namespace NTwain
{
/// <summary>
/// Provides a session for working with TWAIN api in an application.
/// This is the old implementation for reference purposes only.
/// </summary>
[Obsolete("For reference purposes only.")]
2014-04-05 21:54:08 -04:00
public class TwainSessionOld : TwainSession , IMessageFilter
2014-04-05 20:14:19 -04:00
{
/// <summary>
/// Initializes a new instance of the <see cref="TwainSessionOld" /> class.
/// </summary>
/// <param name="appId">The app id.</param>
/// <exception cref="System.ArgumentNullException"></exception>
public TwainSessionOld ( TWIdentity appId ) : base ( appId ) { }
2014-04-05 20:50:13 -04:00
protected override void DoTransferRoutine ( )
2014-04-05 20:14:19 -04:00
{
TWPendingXfers pending = new TWPendingXfers ( ) ;
var rc = ReturnCode . Success ;
do
{
2014-04-06 07:08:52 -04:00
//IList<FileFormat> formats = Enumerable.Empty<FileFormat>().ToList();
//IList<Compression> compressions = Enumerable.Empty<Compression>().ToList();
//bool canDoFileXfer = this.CapGetImageXferMech().Contains(XferMech.File);
//var curFormat = this.GetCurrentCap<FileFormat>(CapabilityId.ICapImageFileFormat);
//var curComp = this.GetCurrentCap<Compression>(CapabilityId.ICapCompression);
2014-04-05 20:14:19 -04:00
TWImageInfo imgInfo ;
bool skip = false ;
if ( DGImage . ImageInfo . Get ( out imgInfo ) ! = ReturnCode . Success )
{
// bad!
skip = true ;
}
2014-04-06 07:08:52 -04:00
//try
//{
// formats = this.CapGetImageFileFormat();
//}
//catch { }
//try
//{
// compressions = this.CapGetCompression();
//}
//catch { }
2014-04-05 20:14:19 -04:00
// ask consumer for cancel in case of non-ui multi-page transfers
2014-04-06 07:08:52 -04:00
//TransferReadyEventArgs args = new TransferReadyEventArgs(pending, formats, curFormat, compressions,
// curComp, canDoFileXfer, imgInfo);
var args = new TransferReadyEventArgs
{
PendingImageInfo = imgInfo ,
PendingTransferCount = pending . Count ,
EndOfJob = pending . EndOfJob = = 0
} ;
2014-04-05 20:14:19 -04:00
args . CancelCurrent = skip ;
OnTransferReady ( args ) ;
if ( ! args . CancelAll & & ! args . CancelCurrent )
{
2014-04-09 21:30:07 -04:00
Values . XferMech mech = this . GetCurrentCap ( CapabilityId . ICapXferMech ) . ConvertToEnum < XferMech > ( ) ;
2014-04-05 20:14:19 -04:00
2014-04-06 07:08:52 -04:00
//if (args.CanDoFileXfer && !string.IsNullOrEmpty(args.OutputFile))
//{
// var setXferRC = DGControl.SetupFileXfer.Set(new TWSetupFileXfer
// {
// FileName = args.OutputFile,
// Format = args.ImageFormat
// });
// if (setXferRC == ReturnCode.Success)
// {
// mech = XferMech.File;
// }
//}
2014-04-05 20:14:19 -04:00
// I don't know how this is supposed to work so it probably doesn't
//this.CapSetImageFormat(args.ImageFormat);
//this.CapSetImageCompression(args.ImageCompression);
#region do xfer
IntPtr dataPtr = IntPtr . Zero ;
IntPtr lockedPtr = IntPtr . Zero ;
string file = null ;
try
{
ReturnCode xrc = ReturnCode . Cancel ;
switch ( mech )
{
case Values . XferMech . Native :
xrc = DGImage . ImageNativeXfer . Get ( ref dataPtr ) ;
break ;
case Values . XferMech . File :
2014-04-06 07:08:52 -04:00
//xrc = DGImage.ImageFileXfer.Get();
//if (File.Exists(args.OutputFile))
//{
// file = args.OutputFile;
//}
2014-04-05 20:14:19 -04:00
break ;
case Values . XferMech . MemFile :
// not supported yet
//TWImageMemXfer memxfer = new TWImageMemXfer();
//xrc = DGImage.ImageMemXfer.Get(memxfer);
break ;
}
if ( xrc = = ReturnCode . XferDone )
{
State = 7 ;
if ( dataPtr ! = IntPtr . Zero )
{
lockedPtr = MemoryManager . Instance . Lock ( dataPtr ) ;
}
2014-04-11 19:25:48 -04:00
OnDataTransferred ( new DataTransferredEventArgs { NativeData = lockedPtr , FileDataPath = file } ) ;
2014-04-05 20:14:19 -04:00
}
//}
//else if (group == DataGroups.Audio)
//{
// var xrc = DGAudio.AudioNativeXfer.Get(ref dataPtr);
// if (xrc == ReturnCode.XferDone)
// {
// State = 7;
// try
// {
// var dtHand = DataTransferred;
// if (dtHand != null)
// {
// lockedPtr = MemoryManager.Instance.MemLock(dataPtr);
// dtHand(this, new DataTransferredEventArgs(lockedPtr));
// }
// }
// catch { }
// }
//}
}
finally
{
State = 6 ;
// data here is allocated by source so needs to use shared mem calls
if ( lockedPtr ! = IntPtr . Zero )
{
MemoryManager . Instance . Unlock ( lockedPtr ) ;
lockedPtr = IntPtr . Zero ;
}
if ( dataPtr ! = IntPtr . Zero )
{
MemoryManager . Instance . Free ( dataPtr ) ;
dataPtr = IntPtr . Zero ;
}
}
#endregion
}
if ( args . CancelAll )
{
rc = DGControl . PendingXfers . Reset ( pending ) ;
if ( rc = = ReturnCode . Success )
{
// if audio exit here
//if (group == DataGroups.Audio)
//{
// //???
// return;
//}
}
}
else
{
rc = DGControl . PendingXfers . EndXfer ( pending ) ;
}
} while ( rc = = ReturnCode . Success & & pending . Count ! = 0 ) ;
State = 5 ;
DisableSource ( ) ;
}
#region messaging use
/// <summary>
/// Message loop processor for winform.
/// Use this by adding the <see cref="TwainSessionOld"/> as an <see cref="IMessageFilter "/>.
/// </summary>
/// <param name="m">The message to be dispatched. You cannot modify this message.</param>
/// <returns>
/// true to filter the message and stop it from being dispatched; false to allow the message to continue to the next filter or control.
/// </returns>
//[EnvironmentPermissionAttribute(SecurityAction.LinkDemand)]
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
bool IMessageFilter . PreFilterMessage ( ref System . Windows . Forms . Message m )
{
2014-04-06 08:22:59 -04:00
var winmsg = new MESSAGE ( m . HWnd , m . Msg , m . WParam , m . LParam ) ;
2014-04-05 20:14:19 -04:00
return HandleWndProcMessage ( ref winmsg ) ;
}
/// <summary>
/// Message loop processor for WPF.
/// </summary>
/// <param name="hwnd">The window handle.</param>
/// <param name="msg">The message ID.</param>
/// <param name="wParam">The message's wParam value.</param>
/// <param name="lParam">The message's lParam value.</param>
/// <param name="handled">A value that indicates whether the message was handled. Set the value to true if the message was handled; otherwise, false.</param>
/// <returns></returns>
[EnvironmentPermissionAttribute(SecurityAction.LinkDemand)]
public IntPtr PreFilterMessage ( IntPtr hwnd , int msg , IntPtr wParam , IntPtr lParam , ref bool handled )
{
2014-04-06 08:22:59 -04:00
var winmsg = new MESSAGE ( hwnd , msg , wParam , lParam ) ;
2014-04-05 20:14:19 -04:00
handled = base . HandleWndProcMessage ( ref winmsg ) ;
return IntPtr . Zero ;
}
#endregion
}
}