TWAIN Helper Glue/TWAcquire.c

// ===========================================================================
//  TWAcquire.c         TWAIN 1.9               ©1991-2001 TWAIN Working Group
// ===========================================================================
//  Sample code for responding to the Acquire... menu item.
//
 
#include "TWAcquire.h"
#include "TWGlue.h"
 
// ---------------------------------------------------------------------------
//  ¥ TWAcquire                                               
// ---------------------------------------------------------------------------
//  
 
TW_UINT16 TWAcquire ( PicHandle *pPictHandle )
{
    TW_UINT16           ReturnCode=TWRC_SUCCESS;
    TW_EVENT            Event;
    TW_USERINTERFACE    UserInterface;
    
    ReturnCode=TWOpenDS();
    
    UserInterface.hParent = NULL;
    UserInterface.ShowUI=TRUE;
    
    ReturnCode=TWEnableDS(&UserInterface);
    
    if (UserInterface.ModalUI && ( TWRC_SUCCESS == ReturnCode ) )
    {
        Event.pEvent=NULL;
        ReturnCode=TWMessageDS(DG_CONTROL,DAT_EVENT,MSG_PROCESSEVENT,(TW_MEMREF)&Event);
        
        if (Event.TWMessage == MSG_XFERREADY)
        {
            TWTransferImage(pPictHandle);
            
            Event.pEvent=NULL;
            ReturnCode=TWMessageDS(DG_CONTROL,DAT_EVENT,MSG_PROCESSEVENT,(TW_MEMREF)&Event);
        }
        ReturnCode=ProcessTWMessage(Event.TWMessage);
    }
    return(ReturnCode);
}
 
 
 
// ---------------------------------------------------------------------------
//  ¥ TWTransferImage                                                 
// ---------------------------------------------------------------------------
//  
 
TW_UINT16 TWTransferImage ( PicHandle *pPictHandle )
{
    TW_IMAGEINFO        ImageInfo;
    TW_UINT16           PendingXfers;
    TW_INT16            result = TWRC_SUCCESS;
    
    result = TWMessageDS(DG_IMAGE,DAT_IMAGEINFO,MSG_GET,(TW_MEMREF)&ImageInfo);
    
    result = TWMessageDS(DG_CONTROL,DAT_PENDINGXFERS,MSG_GET,(TW_MEMREF)&PendingXfers);
    
    result = TWMessageDS(DG_IMAGE,DAT_IMAGENATIVEXFER,MSG_GET,(TW_MEMREF)pPictHandle);
    
    result = TWMessageDS(DG_CONTROL,DAT_PENDINGXFERS,MSG_ENDXFER,(TW_MEMREF)&PendingXfers);
    
    return result;
}
 
 
 
// ---------------------------------------------------------------------------
//  ¥ TWAcquireFile                                           
// ---------------------------------------------------------------------------
//  
 
TW_UINT16 TWAcquireFile ( FSSpecPtr specPtr, TW_UINT16 fileFormat )
{
    TW_UINT16           result = TWRC_SUCCESS;
    TW_EVENT            event;
    TW_USERINTERFACE    userInterface;
    
    result = TWOpenDS();
    
    userInterface.hParent = NULL;
    userInterface.ShowUI = TRUE;
    
    result = TWEnableDS ( &userInterface );
      
    if ( userInterface.ModalUI && result == TWRC_SUCCESS )
    {
        event.pEvent = NULL;
        result = TWMessageDS ( DG_CONTROL, DAT_EVENT, MSG_PROCESSEVENT, (TW_MEMREF)&event );
        
        if ( event.TWMessage == MSG_XFERREADY )
        {
            result = TWTransferFile ( specPtr, fileFormat );
            
            event.pEvent = NULL;
            result = TWMessageDS ( DG_CONTROL, DAT_EVENT, MSG_PROCESSEVENT, (TW_MEMREF)&event);
        }
        
        result = ProcessTWMessage ( event.TWMessage );
    }
    
    return result;
}
 
 
 
// ---------------------------------------------------------------------------
//  ¥ TWTransferFile                                                  
// ---------------------------------------------------------------------------
//   
 
TW_UINT16 TWTransferFile ( FSSpecPtr specPtr, TW_UINT16 fileFormat )
{
    // According to page 83 of the TWAIN 1.9 Specification, 
    // "Macintosh developers must use TWSX_FILE2, instead of TWSX_FILE, in order 
    // to correctly address image and audio files in the newer versions of the 
    // operating system."
    
    TW_INT16 isTWSX_FILE2Capable = FALSE;
    TW_INT16 result = TWRC_SUCCESS;
    
    // verify that the Data Source supports the disk file transfer mechanism
    result = TWGetDSCapability ( ICAP_XFERMECH, TWSX_FILE2, &isTWSX_FILE2Capable );
    if ( result == TWRC_SUCCESS )
    {
        if ( isTWSX_FILE2Capable == TRUE )
        {
            TW_INT16 supportsFileFormat = FALSE;
            result = TWGetDSCapability ( ICAP_IMAGEFILEFORMAT, fileFormat, &supportsFileFormat );
            if ( result == TWRC_SUCCESS )
            {
                if ( supportsFileFormat == TRUE )
                {
                    // set the DS transfer mode to TWSX_FILE2
                    result = TWSetDSCapability ( ICAP_XFERMECH, TWSX_FILE2 );
                    
                    // setup the file transfer
                    TW_SETUPFILEXFER2 fileTransferInfo;
                    fileTransferInfo.FileName = (TW_MEMREF)specPtr->name;
                    fileTransferInfo.FileNameType = TWTY_STR64;
                    fileTransferInfo.Format = fileFormat;
                    fileTransferInfo.VRefNum = specPtr->vRefNum;
                    fileTransferInfo.ParID = specPtr->parID;
                    result = TWMessageDS ( DG_CONTROL, DAT_SETUPFILEXFER2, MSG_SET, (TW_MEMREF)&fileTransferInfo );
 
                    // transfer the image into the file
                    result = TWMessageDS ( DG_IMAGE, DAT_IMAGEFILEXFER, MSG_GET, NULL );
 
                    // wrap up
                    TW_UINT16 pendingXfers;
                    result = TWMessageDS ( DG_CONTROL, DAT_PENDINGXFERS, MSG_ENDXFER, (TW_MEMREF)&pendingXfers);
                }
                
                else result = TWRC_FAILURE;
            }
        }
        
        else result = TWRC_FAILURE;
    }
            
    return result;
}
 
 
 
// ---------------------------------------------------------------------------
//  ¥ ProcessTWMessage                                                
// ---------------------------------------------------------------------------
//  
 
TW_UINT16 ProcessTWMessage ( TW_UINT16 TWMessage )
{
    TW_UINT16           ReturnCode=TWRC_SUCCESS;
    TW_USERINTERFACE    UserInterface;
    
    switch(TWMessage)
    {
        case MSG_CLOSEDSREQ:
            ReturnCode=TWDisableDS(&UserInterface);
            ReturnCode=TWCloseDS();
            break;
            
        case MSG_XFERREADY: /* XferReadys are handled higher up */
            break;
    }
    
    return(ReturnCode);
}