Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

401 lines
12 KiB

  1. // wiatest.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "wiatest.h"
  5. #include "MainFrm.h"
  6. #include "ChildFrm.h"
  7. #include "wiatestDoc.h"
  8. #include "wiatestView.h"
  9. #include "WiaeditpropTable.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. LONG WIACONSTANT_VALUE_FROMINDEX(int index)
  16. {
  17. return g_EditPropTable[index].lVal;
  18. }
  19. TCHAR *WIACONSTANT_TSTR_FROMINDEX(int index)
  20. {
  21. return g_EditPropTable[index].pszValName;
  22. }
  23. INT FindEndIndexInTable(TCHAR *pszPropertyName)
  24. {
  25. int index = FindStartIndexInTable(pszPropertyName);
  26. if(index >=0){
  27. while((g_EditPropTable[index].pszPropertyName != NULL) && (lstrcmpi(pszPropertyName,g_EditPropTable[index].pszPropertyName) == 0)){
  28. index++;
  29. }
  30. }
  31. return (index - 1);
  32. }
  33. INT FindStartIndexInTable(TCHAR *pszPropertyName)
  34. {
  35. int index = 0;
  36. BOOL bFound = FALSE;
  37. while((g_EditPropTable[index].pszPropertyName != NULL) && (bFound == FALSE) ){
  38. // check for property name
  39. if(lstrcmpi(pszPropertyName,g_EditPropTable[index].pszPropertyName) == 0){
  40. // we found property name
  41. bFound = TRUE;
  42. } else {
  43. index++;
  44. }
  45. }
  46. if(!bFound){
  47. index = -1;
  48. }
  49. return index;
  50. }
  51. BOOL WIACONSTANT2TSTR(TCHAR *pszPropertyName, LONG lValue, TCHAR *pszValName)
  52. {
  53. BOOL bFound = FALSE;
  54. if(pszValName){
  55. int index = 0;
  56. while((g_EditPropTable[index].pszPropertyName != NULL) && (bFound == FALSE) ){
  57. // check for property name
  58. if(lstrcmpi(pszPropertyName,g_EditPropTable[index].pszPropertyName) == 0){
  59. // we found property name
  60. if(g_EditPropTable[index].lVal == lValue){
  61. lstrcpy(pszValName,g_EditPropTable[index].pszValName);
  62. bFound = TRUE;
  63. }
  64. }
  65. index++;
  66. }
  67. }
  68. return bFound;
  69. }
  70. BOOL TSTR2WIACONSTANT(TCHAR *pszPropertyName, TCHAR *pszValName, LONG *plVal)
  71. {
  72. BOOL bFound = FALSE;
  73. if(pszValName){
  74. int index = 0;
  75. while((g_EditPropTable[index].pszPropertyName != NULL) && (bFound == FALSE)){
  76. // check for property name
  77. if(lstrcmpi(pszPropertyName,g_EditPropTable[index].pszPropertyName) == 0){
  78. // we found property name
  79. if(lstrcmpi(g_EditPropTable[index].pszValName,pszValName) == 0){
  80. *plVal = g_EditPropTable[index].lVal;
  81. bFound = TRUE;
  82. }
  83. }
  84. index++;
  85. }
  86. }
  87. return bFound;
  88. }
  89. void RC2TSTR(UINT uResourceID, TCHAR *szString, LONG size)
  90. {
  91. memset(szString,0,size);
  92. INT iNumTCHARSWritten = 0;
  93. HINSTANCE hInstance = NULL;
  94. hInstance = AfxGetInstanceHandle();
  95. if(!hInstance){
  96. MessageBox(NULL,TEXT("Could not get WIATEST's HINSTANCE for string loading."),TEXT("WIATEST Error"),MB_ICONERROR);
  97. return;
  98. }
  99. iNumTCHARSWritten = LoadString(hInstance,uResourceID,szString,(size / (sizeof(TCHAR))));
  100. }
  101. void StatusMessageBox(HWND hWnd, UINT uResourceID)
  102. {
  103. TCHAR szResourceString[MAX_PATH];
  104. memset(szResourceString,0,sizeof(szResourceString));
  105. RC2TSTR(uResourceID,szResourceString,sizeof(szResourceString));
  106. StatusMessageBox(hWnd,szResourceString);
  107. }
  108. void StatusMessageBox(HWND hWnd, LPTSTR szStatusText)
  109. {
  110. TCHAR Title[MAX_PATH];
  111. memset(Title,0,sizeof(Title));
  112. // load status dialog title
  113. RC2TSTR(IDS_WIASTATUS_DIALOGTITLE,Title,sizeof(Title));
  114. MessageBox(hWnd,szStatusText,Title, MB_ICONINFORMATION);
  115. }
  116. void StatusMessageBox(UINT uResourceID)
  117. {
  118. TCHAR szResourceString[MAX_PATH];
  119. memset(szResourceString,0,sizeof(szResourceString));
  120. RC2TSTR(uResourceID,szResourceString,sizeof(szResourceString));
  121. StatusMessageBox(szResourceString);
  122. }
  123. void StatusMessageBox(LPTSTR szStatusText)
  124. {
  125. TCHAR Title[MAX_PATH];
  126. memset(Title,0,sizeof(Title));
  127. // load status dialog title
  128. RC2TSTR(IDS_WIASTATUS_DIALOGTITLE,Title,sizeof(Title));
  129. MessageBox(NULL,szStatusText,Title, MB_ICONINFORMATION);
  130. }
  131. void ErrorMessageBox(UINT uResourceID, HRESULT hrError)
  132. {
  133. TCHAR szResourceString[MAX_PATH];
  134. memset(szResourceString,0,sizeof(szResourceString));
  135. RC2TSTR(uResourceID,szResourceString,sizeof(szResourceString));
  136. ErrorMessageBox(szResourceString,hrError);
  137. }
  138. void ErrorMessageBox(LPTSTR szErrorText, HRESULT hrError)
  139. {
  140. ULONG ulLen = MAX_PATH;
  141. TCHAR MsgBuf[MAX_PATH];
  142. TCHAR *pAllocMsgBuf = NULL;
  143. TCHAR Title[MAX_PATH];
  144. memset(Title,0,sizeof(Title));
  145. memset(MsgBuf,0,sizeof(MsgBuf));
  146. // load error dialog title
  147. RC2TSTR(IDS_WIAERROR_DIALOGTITLE,Title,sizeof(Title));
  148. // attempt to handle WIA custom errors first
  149. switch (hrError) {
  150. case WIA_ERROR_GENERAL_ERROR:
  151. RC2TSTR(IDS_WIAERROR_GENERAL,MsgBuf,sizeof(MsgBuf));
  152. break;
  153. case WIA_ERROR_PAPER_JAM:
  154. RC2TSTR(IDS_WIAERROR_PAPERJAM ,MsgBuf,sizeof(MsgBuf));
  155. break;
  156. case WIA_ERROR_PAPER_EMPTY:
  157. RC2TSTR(IDS_WIAERROR_PAPEREMPTY ,MsgBuf,sizeof(MsgBuf));
  158. break;
  159. case WIA_ERROR_PAPER_PROBLEM:
  160. RC2TSTR(IDS_WIAERROR_PAPERPROBLEM ,MsgBuf,sizeof(MsgBuf));
  161. break;
  162. case WIA_ERROR_OFFLINE:
  163. RC2TSTR(IDS_WIAERROR_DEVICEOFFLINE ,MsgBuf,sizeof(MsgBuf));
  164. break;
  165. case WIA_ERROR_BUSY:
  166. RC2TSTR(IDS_WIAERROR_DEVICEBUSY,MsgBuf,sizeof(MsgBuf));
  167. break;
  168. case WIA_ERROR_WARMING_UP:
  169. RC2TSTR(IDS_WIAERROR_WARMINGUP,MsgBuf,sizeof(MsgBuf));
  170. break;
  171. case WIA_ERROR_USER_INTERVENTION:
  172. RC2TSTR(IDS_WIAERROR_USERINTERVENTION,MsgBuf,sizeof(MsgBuf));
  173. break;
  174. case WIA_ERROR_ITEM_DELETED:
  175. RC2TSTR(IDS_WIAERROR_ITEMDELETED,MsgBuf,sizeof(MsgBuf));
  176. break;
  177. case WIA_ERROR_DEVICE_COMMUNICATION:
  178. RC2TSTR(IDS_WIAERROR_DEVICECOMMUNICATION,MsgBuf,sizeof(MsgBuf));
  179. break;
  180. case WIA_ERROR_INVALID_COMMAND:
  181. RC2TSTR(IDS_WIAERROR_INVALIDCOMMAND,MsgBuf,sizeof(MsgBuf));
  182. break;
  183. case S_OK:
  184. lstrcpy(MsgBuf,szErrorText);
  185. break;
  186. default:
  187. ulLen = 0;
  188. ulLen = ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  189. NULL, hrError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  190. (LPTSTR)&pAllocMsgBuf, 0, NULL);
  191. break;
  192. }
  193. if (ulLen <= 0) {
  194. // just use the HRESULT as a formatted string
  195. TSPRINTF(MsgBuf,TEXT("HRESULT = 0x%08X"),hrError);
  196. } else {
  197. if(pAllocMsgBuf){
  198. // trim right (remove \r\n from formatted string)
  199. pAllocMsgBuf[ulLen - (2 * sizeof(TCHAR))] = 0; // reterminate the string
  200. // copy string into message buffer
  201. lstrcpy(MsgBuf,pAllocMsgBuf);
  202. // FormatMessage allocated a buffer to display
  203. LocalFree(pAllocMsgBuf);
  204. }
  205. }
  206. if(S_OK != hrError){
  207. TCHAR szFinalText[MAX_PATH];
  208. memset(szFinalText,0,sizeof(szFinalText));
  209. #ifndef UNICODE
  210. TSPRINTF(szFinalText,TEXT("%s\n(%s)"),szErrorText,MsgBuf);
  211. #else
  212. TSPRINTF(szFinalText,TEXT("%ws\n(%ws)"),szErrorText,MsgBuf);
  213. #endif
  214. MessageBox(NULL,szFinalText,Title,MB_ICONERROR);
  215. } else {
  216. MessageBox(NULL,szErrorText,Title,MB_ICONWARNING);
  217. }
  218. }
  219. /////////////////////////////////////////////////////////////////////////////
  220. // CWiatestApp
  221. BEGIN_MESSAGE_MAP(CWiatestApp, CWinApp)
  222. //{{AFX_MSG_MAP(CWiatestApp)
  223. ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  224. // NOTE - the ClassWizard will add and remove mapping macros here.
  225. // DO NOT EDIT what you see in these blocks of generated code!
  226. //}}AFX_MSG_MAP
  227. // Standard file based document commands
  228. ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  229. ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  230. // Standard print setup command
  231. ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
  232. END_MESSAGE_MAP()
  233. /////////////////////////////////////////////////////////////////////////////
  234. // CWiatestApp construction
  235. CWiatestApp::CWiatestApp()
  236. {
  237. // TODO: add construction code here,
  238. // Place all significant initialization in InitInstance
  239. }
  240. /////////////////////////////////////////////////////////////////////////////
  241. // The one and only CWiatestApp object
  242. CWiatestApp theApp;
  243. /////////////////////////////////////////////////////////////////////////////
  244. // CWiatestApp initialization
  245. BOOL CWiatestApp::InitInstance()
  246. {
  247. // initialize DEBUG library
  248. DBG_INIT(m_hInstance);
  249. // initialize COM
  250. CoInitialize(NULL);
  251. AfxEnableControlContainer();
  252. // Standard initialization
  253. // If you are not using these features and wish to reduce the size
  254. // of your final executable, you should remove from the following
  255. // the specific initialization routines you do not need.
  256. #ifdef _AFXDLL
  257. Enable3dControls(); // Call this when using MFC in a shared DLL
  258. #else
  259. Enable3dControlsStatic(); // Call this when linking to MFC statically
  260. #endif
  261. SetRegistryKey(_T("Microsoft"));
  262. LoadStdProfileSettings(); // Load standard INI file options (including MRU)
  263. // Register the application's document templates. Document templates
  264. // serve as the connection between documents, frame windows and views.
  265. CMultiDocTemplate* pDocTemplate;
  266. pDocTemplate = new CMultiDocTemplate(
  267. IDR_WIATESTYPE,
  268. RUNTIME_CLASS(CWiatestDoc),
  269. RUNTIME_CLASS(CChildFrame), // custom MDI child frame
  270. RUNTIME_CLASS(CWiatestView));
  271. AddDocTemplate(pDocTemplate);
  272. // create main MDI Frame window
  273. CMainFrame* pMainFrame = new CMainFrame;
  274. if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  275. return FALSE;
  276. m_pMainWnd = pMainFrame;
  277. // Parse command line for standard shell commands, DDE, file open
  278. CCommandLineInfo cmdInfo;
  279. ParseCommandLine(cmdInfo);
  280. #ifdef _OPEN_NEW_DEVICE_ON_STARTUP
  281. // Dispatch commands specified on the command line
  282. if (!ProcessShellCommand(cmdInfo))
  283. return FALSE;
  284. #endif
  285. // The main window has been initialized, so show and update it.
  286. pMainFrame->ShowWindow(m_nCmdShow);
  287. pMainFrame->UpdateWindow();
  288. return TRUE;
  289. }
  290. /////////////////////////////////////////////////////////////////////////////
  291. // CAboutDlg dialog used for App About
  292. class CAboutDlg : public CDialog
  293. {
  294. public:
  295. CAboutDlg();
  296. // Dialog Data
  297. //{{AFX_DATA(CAboutDlg)
  298. enum { IDD = IDD_ABOUTBOX };
  299. //}}AFX_DATA
  300. // ClassWizard generated virtual function overrides
  301. //{{AFX_VIRTUAL(CAboutDlg)
  302. protected:
  303. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  304. //}}AFX_VIRTUAL
  305. // Implementation
  306. protected:
  307. //{{AFX_MSG(CAboutDlg)
  308. // No message handlers
  309. //}}AFX_MSG
  310. DECLARE_MESSAGE_MAP()
  311. };
  312. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  313. {
  314. //{{AFX_DATA_INIT(CAboutDlg)
  315. //}}AFX_DATA_INIT
  316. }
  317. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  318. {
  319. CDialog::DoDataExchange(pDX);
  320. //{{AFX_DATA_MAP(CAboutDlg)
  321. //}}AFX_DATA_MAP
  322. }
  323. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  324. //{{AFX_MSG_MAP(CAboutDlg)
  325. // No message handlers
  326. //}}AFX_MSG_MAP
  327. END_MESSAGE_MAP()
  328. // App command to run the dialog
  329. void CWiatestApp::OnAppAbout()
  330. {
  331. CAboutDlg aboutDlg;
  332. aboutDlg.DoModal();
  333. }
  334. /////////////////////////////////////////////////////////////////////////////
  335. // CWiatestApp message handlers
  336. int CWiatestApp::ExitInstance()
  337. {
  338. // uninitialize COM
  339. CoUninitialize();
  340. return CWinApp::ExitInstance();
  341. }