TwainApp.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /***************************************************************************
  2. * Copyright ?2007 TWAIN Working Group:
  3. * Adobe Systems Incorporated, AnyDoc Software Inc., Eastman Kodak Company,
  4. * Fujitsu Computer Products of America, JFL Peripheral Solutions Inc.,
  5. * Ricoh Corporation, and Xerox Corporation.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * * Neither the name of the TWAIN Working Group nor the
  16. * names of its contributors may be used to endorse or promote products
  17. * derived from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY TWAIN Working Group ``AS IS'' AND ANY
  20. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL TWAIN Working Group BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. ***************************************************************************/
  31. /**
  32. * @file TwainApp.h
  33. * TWAIN Application.
  34. * A TWAIN Application communicates with the DSM to acquire images.
  35. * The goal of the application is to acquire data from a Source.
  36. * However, applications cannot contact the Source directly. All requests for
  37. * data, capability information, error information, etc. must be handled
  38. * Through the Source Manager.
  39. * @author JFL Peripheral Solutions Inc.
  40. * @date April 2007
  41. */
  42. /*! \mainpage TWAIN Application
  43. *
  44. * The goal of the application is to acquire data from a Source.
  45. * However, applications cannot contact the Source directly. All requests for
  46. * data, capability information, error information, etc. must be handled
  47. * Through the Source Manager.
  48. *
  49. *
  50. *
  51. *
  52. *
  53. *
  54. *
  55. *
  56. * Copyright ?2007 TWAIN Working Group: Adobe Systems Incorporated,
  57. * AnyDoc Software Inc., Eastman Kodak Company,
  58. * Fujitsu Computer Products of America, JFL Peripheral Solutions Inc.,
  59. * Ricoh Corporation, and Xerox Corporation.
  60. * All rights reserved.
  61. */
  62. #ifndef __TWAINAPP_H__
  63. #define __TWAINAPP_H__
  64. #pragma once
  65. #ifdef _WINDOWS
  66. #ifndef __AFXWIN_H__
  67. #error include 'stdafx.h' before including this file for PCH
  68. #endif
  69. #endif
  70. #include <string>
  71. #include <vector>
  72. #include "CommonTWAIN.h"
  73. #include "DSMInterface.h"
  74. #ifndef TWH_CMP_MSC
  75. typedef void* HWND;
  76. #endif
  77. using namespace std;
  78. extern bool gUSE_CALLBACKS;
  79. /**
  80. * Determine if two TW_FIX32 variables are equal.
  81. * @param[in] _fix1 First TW_FIX32
  82. * @param[in] _fix2 Second TW_FIX32
  83. * @return bool TRUE if equal
  84. */
  85. bool operator== (const TW_FIX32& _fix1, const TW_FIX32& _fix2);
  86. /**
  87. * Output CMD messages. In CMD app used for prompt. In GUI app used by debugger
  88. * @param[in] pStr the message to display
  89. * @param[in] ... additional varibles
  90. */
  91. void PrintCMDMessage(const char* const pStr, ...);
  92. /**
  93. * The Main Application Class.
  94. */
  95. class TwainApp
  96. {
  97. public:
  98. /**
  99. * Constructor for the main application.
  100. * Initialize the application.
  101. * @param[in] parent handle to Window
  102. */
  103. TwainApp(HWND parent = NULL);
  104. /**
  105. * Deconstructor for the main application TwainApp.
  106. * Clean up any alocated memory.
  107. */
  108. ~TwainApp();
  109. int GetDSMState();
  110. TW_UINT16 DSM_Entry(TW_UINT32 _DG,
  111. TW_UINT16 _DAT,
  112. TW_UINT16 _MSG,
  113. TW_MEMREF _pData);
  114. /**
  115. * Print out error message and condition code from DS. Looks up the
  116. * condition code using the _pdestID and prints it after the _errorMsg.
  117. * If _errorMsg is empty, a default message is displayed.
  118. * @param[in] _pdestID the destination to get the condition code. 0 is DSM
  119. * anything else is a source.
  120. * @param[in] _errorMsg the error message to display. If empty a standard message
  121. * is displayed
  122. * @return a valid TWCC_xxxx condition code
  123. */
  124. TW_INT16 printError(pTW_IDENTITY _pdestID = 0, const string& _errorMsg = "");
  125. /**
  126. * Retrieves the condition code from the DSM.
  127. * @param[in] _pdestID a pointer to the source to query, or 0 for the DSM
  128. * @param[out] _cc this will be set to the condition code
  129. * @return a valid TWRC_xxxx return code
  130. */
  131. TW_UINT16 getTWCC(pTW_IDENTITY _pdestID, TW_INT16& _cc);
  132. /**
  133. * Fills an identity structure with this applications identity details.
  134. * Initialize the Apps identity so the DSM can uniquely identify it.
  135. * @param[out] _identity a structure that will get filled with Applications details
  136. */
  137. virtual void fillIdentity(TW_IDENTITY& _identity);
  138. /**
  139. * Exit application clean up everything.
  140. */
  141. void exit();
  142. /**
  143. * Try to connect to the DSM. Set the App to correct state.
  144. * m_DSMState is set to state 3 if successful.
  145. */
  146. void connectDSM();
  147. /**
  148. * Try to disconnect from the DSM. Set the App to correct state.
  149. * m_DSMState is set to state 2 if successful.
  150. */
  151. void disconnectDSM();
  152. /**
  153. * Queries the DSM for all available Data Sources. Discover all Data
  154. * Sources available and build a list of DS and store them in m_DataSources.
  155. */
  156. void getSources();
  157. /**
  158. * Try to load a Data Source. Loads the data source with the ID passed in.
  159. * To open a Data Source the DSM must be in state 3 (Open). If successful
  160. * moves the app to state 4
  161. * @param[in] _dsID the ID of the datasource to load, -1 to open the default
  162. */
  163. virtual void loadDS(const TW_INT32 _dsID);
  164. /**
  165. * Unloads the currently loaded datasource. If successful moves the
  166. * app to state 3.
  167. */
  168. virtual void unloadDS();
  169. /**
  170. * Request the acquisition of data from the currently opened source. If
  171. * successful moves app to state 5.
  172. * @param[in] hWnd the handle to the window
  173. * @param[in] bShowUI true to show UI false to enable without UI
  174. * @return true if successfully enabled DS.
  175. */
  176. virtual bool enableDS(TW_HANDLE hWnd, BOOL bShowUI);
  177. /**
  178. * Show the DS GUI so the user can modify current settings.
  179. * successful moves app to state 5.
  180. * @param[in] hWnd the handle to the window
  181. * @return true if successfully enabled DS.
  182. */
  183. virtual bool enableDSUIOnly(TW_HANDLE hWnd);
  184. /**
  185. * Tries to disable the sources UI. If successful moves app to state 4
  186. */
  187. virtual void disableDS();
  188. /**
  189. * Starts the scanning process using native mode. Data will be transfered
  190. * in native mode.
  191. */
  192. void initiateTransfer_Native();
  193. /**
  194. * Starts the scanning process using file mode. Data will be transfered
  195. * via a file.
  196. */
  197. void initiateTransfer_File(TW_UINT16 fileformat = TWFF_TIFF);
  198. /**
  199. * Starts the scanning process using the memory strip mode. Data will be transfered
  200. * in memory using a buffer.
  201. */
  202. void initiateTransfer_Memory();
  203. /**
  204. * Abort the scanning process.
  205. * @return a valid TWRC_xxxx return code.
  206. */
  207. TW_UINT16 DoAbortXfer();
  208. /**
  209. * Try to sets a OneValue Capability of any of the TW_INT or TW_UINT types to the value passed
  210. * @param[in] Cap the capability to update to set
  211. * @param[in] _value the value to set
  212. * @param[in] _type the TWAIN Type to set
  213. * @return a valid TWRC_xxxx return code.
  214. */
  215. TW_UINT16 set_CapabilityOneValue(TW_UINT16 Cap, const int _value, TW_UINT16 _type);
  216. /**
  217. * Try to sets a OneValue Capability of type TW_FIX32 to the value passed
  218. * @param[in] Cap the capability to update to set
  219. * @param[in] _pValue the value to set
  220. * @return a valid TWRC_xxxx return code.
  221. */
  222. TW_UINT16 set_CapabilityOneValue(TW_UINT16 Cap, const pTW_FIX32 _pValue);
  223. /**
  224. * Try to sets a OneValue Capability of type TW_FRAME to the value passed
  225. * @param[in] Cap the capability to update to set
  226. * @param[in] _pValue the value to set
  227. * @return a valid TWRC_xxxx return code.
  228. */
  229. TW_UINT16 set_CapabilityOneValue(TW_UINT16 Cap, const pTW_FRAME _pValue);
  230. /**
  231. * Try to sets an Array Capability of any of the TW_INT or TW_UINT types to the values passed
  232. * @param[in] Cap the capability to update to set
  233. * @param[in] _pValues pointer to the array of values to set
  234. * @param[in] Count the number of items in the array
  235. * @param[in] -type the type of items to set
  236. * @return a valid TWRC_xxxx return code.
  237. */
  238. TW_UINT16 set_CapabilityArray(TW_UINT16 Cap, const int * _pValues, int Count, TW_UINT16 _type);
  239. /**
  240. * Gets the capability.
  241. * @param[in,out] _cap the capability struct to fill. _cap.Cap needs to be
  242. * @param[in] _msg one of MSG_GET, MSG_GETCURRENT, MSG_GETDEFAULT, or MSG_RESET
  243. * filled with the capability to retrieve.
  244. * @return a valid TWCC_xxxx condition code
  245. */
  246. TW_INT16 get_CAP(TW_CAPABILITY& _cap, TW_UINT16 _msg = MSG_GET);
  247. /**
  248. * Query Support messages of the capability.
  249. * @param[in] _cap the capability id to lookup.
  250. * @param[out] _QS the Query Support of the cap
  251. * @return a valid TWRC_xxxx return code
  252. */
  253. TW_INT16 QuerySupport_CAP(TW_UINT16 _cap, TW_UINT32 &_QS);
  254. /**
  255. * Get the lable for a capability.
  256. * @param[in] _cap the capability id to lookup.
  257. * @param[out] sLable the Lable if returned
  258. * @return a valid TWRC_xxxx return code
  259. */
  260. TW_INT16 GetLabel(TW_UINT16 _cap, string &sLable);
  261. /**
  262. * Get the help or description for a capability.
  263. * @param[in] _cap the capability id to lookup.
  264. * @param[out] sDescription the Description or help if returned
  265. * @return a valid TWRC_xxxx return code
  266. */
  267. TW_INT16 GetHelp(TW_UINT16 _cap, string &sHelp);
  268. /**
  269. * Returns a pointer to the applications identity structure.
  270. * @return a TW_IDENTITY pointer to the applications identity struct.
  271. */
  272. pTW_IDENTITY getAppIdentity();
  273. /**
  274. * Returns a pointer to the currently opened data source.
  275. * @param[in] _index the DS index to return
  276. * @return a TW_IDENTITY pointer to the opened data source
  277. * if nothing open then return NULL.
  278. */
  279. pTW_IDENTITY getDataSource(TW_INT16 _index = -1) const;
  280. /**
  281. * Returns a pointer to the default data source.
  282. * @return a TW_IDENTITY pointer to the default.
  283. */
  284. pTW_IDENTITY getDefaultDataSource(void);
  285. /**
  286. * Set the default data source.
  287. * @param[in] _index the DS index to set as default
  288. * @return a TW_IDENTITY pointer of the default.
  289. */
  290. pTW_IDENTITY setDefaultDataSource(unsigned int _index);
  291. /**
  292. * Display the Select Source Dialog and returns a pointer to the selected data source.
  293. * @return a TW_IDENTITY pointer. NULL if no source selected.
  294. */
  295. pTW_IDENTITY selectDefaultDataSource(void);
  296. /**
  297. * Gets the imageinfo of the currently transfered image
  298. * @return pointer to the image info
  299. */
  300. pTW_IMAGEINFO getIMAGEINFO() {return &m_ImageInfo;}
  301. /**
  302. * Gets the extended image info of the currently transfered image
  303. * @return pointer to the extended image info
  304. */
  305. string getEXIMAGEINFO(){return m_strExImageInfo;}
  306. /**
  307. * retrieve the imageinfo for the current image
  308. * @return true on succes
  309. */
  310. bool updateIMAGEINFO();
  311. /**
  312. * retrieve the extended image info for the current image
  313. * @return true on succes
  314. */
  315. virtual void updateEXTIMAGEINFO();
  316. int m_DSMState; /**< The current TWAIN state of the dsm (2-7) */
  317. TW_UINT16 m_DSMessage; /**< Statis to indicate if we are waiting for DS */
  318. protected:
  319. /**
  320. * Create a DIB of the dimention set by m_ImageInfo and bitdepth of
  321. * data source.
  322. * @return the DIB created on succes or 0 if failed.
  323. */
  324. virtual void navtive_graber( PBITMAPINFOHEADER pDIB );
  325. void setDSDefaultConfig();
  326. virtual BOOL HasStopCommand();
  327. HWND m_Parent; /**< Handle to Window to recieve window messages. */
  328. TW_IDENTITY m_MyInfo; /**< Identity structure with this applications identity details */
  329. pTW_IDENTITY m_pDataSource; /**< Pointer of Identity structure to current loaded data souce */
  330. vector<TW_IDENTITY> m_DataSources; /**< Store a list of available data sources located by DSM */
  331. TW_IMAGEINFO m_ImageInfo; /**< Detailed image info retrieved from the DS */
  332. pTW_EXTIMAGEINFO m_pExtImageInfo; /**< Detailed extended image info retrieved from the DS */
  333. TW_UINT16 m_nXferNum; /**< Keep track of the number of images attempted to transfer - used when saving file to disk*/
  334. string m_strExImageInfo; /**< Extended image info retrieved from the DS */
  335. TW_UINT16 m_nGetLableSupported; /**< Remember if MSG_GETLABLE is supported */
  336. TW_UINT16 m_nGetHelpSupported; /**< Remember if MSG_GETHELP is supported */
  337. string m_strSavePath; /**< The Path to save the files to */
  338. TW_USERINTERFACE m_ui; /**< Coordinates UI between application and data source. */
  339. };
  340. #endif // __TWAINAPP_H__