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.

248 lines
7.0 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1999
  6. //
  7. // File: eventprompt.h
  8. //
  9. //--------------------------------------------------------------------------
  10. #ifndef _EVENTPROMPT_H_
  11. #define _EVENTPROMPT_H_
  12. #include <windows.h>
  13. #include "wiaacmgr.h"
  14. #include "acqmgrcw.h"
  15. #include "evntparm.h"
  16. #include "resource.h"
  17. #include <commctrl.h>
  18. // CHandlerList does all our comboboxex operations for the list of event handlers
  19. class CHandlerList
  20. {
  21. private:
  22. HWND m_hwnd;
  23. public:
  24. CHandlerList (HWND hwnd)
  25. : m_hwnd(hwnd)
  26. {
  27. }
  28. ~CHandlerList ()
  29. {
  30. }
  31. UINT FillList (const CSimpleStringWide &strDeviceId, GUID &guidEvent);
  32. WIA_EVENT_HANDLER *GetSelectedHandler ();
  33. };
  34. // CEventPromptDlg does the work of asking the user which handler to run
  35. // and invoking the specified handler
  36. class CEventPromptDlg
  37. {
  38. private:
  39. CHandlerList *m_pList;
  40. HWND m_hwnd;
  41. CSharedMemorySection<HWND> *m_PromptData;
  42. CEventParameters *m_pEventParameters;
  43. CEventPromptDlg (CEventParameters *pEventParameters)
  44. : m_pEventParameters (pEventParameters), m_hwnd(NULL), m_PromptData(NULL),
  45. m_pList(NULL)
  46. {
  47. }
  48. ~CEventPromptDlg ()
  49. {
  50. if (m_PromptData)
  51. {
  52. delete m_PromptData;
  53. }
  54. if (m_pList)
  55. {
  56. delete m_pList;
  57. }
  58. }
  59. static INT_PTR CALLBACK DlgProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
  60. void OnInit ();
  61. INT_PTR OnCommand (WORD wCode, WORD widItem);
  62. INT_PTR OnOK ();
  63. void SetDefaultHandler (WIA_EVENT_HANDLER *pHandler);
  64. bool InvokeHandler (WIA_EVENT_HANDLER *pHandler);
  65. public:
  66. static HRESULT Invoke (HINSTANCE hInst, CEventParameters *pEventParameters)
  67. {
  68. WIA_PUSHFUNCTION(TEXT("CEventPromptDlg::Invoke"));
  69. HRESULT hr = S_OK;
  70. // Register common controls
  71. INITCOMMONCONTROLSEX icce;
  72. icce.dwSize = sizeof(icce);
  73. icce.dwICC = ICC_USEREX_CLASSES ;
  74. InitCommonControlsEx( &icce );
  75. CEventPromptDlg *pDlg = new CEventPromptDlg (pEventParameters);
  76. if (!pDlg)
  77. {
  78. hr = E_OUTOFMEMORY;
  79. }
  80. else if (-1 == DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_EVENTPROMPT), NULL, DlgProc,reinterpret_cast<LPARAM>(pDlg)))
  81. {
  82. DWORD dw = GetLastError();
  83. hr = HRESULT_FROM_WIN32(dw);
  84. }
  85. if (pDlg)
  86. {
  87. delete pDlg;
  88. }
  89. return hr;
  90. }
  91. }; //CEventPromptDlg
  92. // CPromptThread invokes the event handler prompt dialog
  93. class CPromptThread
  94. {
  95. private:
  96. CEventParameters m_EventParameters;
  97. CPromptThread (const CEventParameters &EventParameters)
  98. : m_EventParameters (EventParameters)
  99. {
  100. }
  101. ~CPromptThread ()
  102. {
  103. }
  104. HRESULT Run(void)
  105. {
  106. WIA_PUSHFUNCTION(TEXT("CPromptThread::Run"));
  107. HRESULT hr = CoInitialize (NULL);
  108. if (SUCCEEDED(hr))
  109. {
  110. hr = CEventPromptDlg::Invoke (g_hInstance, &m_EventParameters);
  111. CoUninitialize();
  112. }
  113. return hr;
  114. }
  115. static DWORD ThreadProc( LPVOID pvParam )
  116. {
  117. WIA_PUSHFUNCTION(TEXT("CPromptThread::ThreadProc"));
  118. #if !defined(DBG_GENERATE_PRETEND_EVENT)
  119. _Module.Lock();
  120. #endif
  121. DWORD dwResult = static_cast<DWORD>(E_FAIL);
  122. CPromptThread *pThread = reinterpret_cast<CPromptThread*>(pvParam);
  123. if (pThread)
  124. {
  125. dwResult = static_cast<DWORD>(pThread->Run());
  126. delete pThread;
  127. }
  128. #if !defined(DBG_GENERATE_PRETEND_EVENT)
  129. _Module.Unlock();
  130. #endif
  131. return dwResult;
  132. }
  133. public:
  134. static HANDLE Create( const CEventParameters &EventParameters )
  135. {
  136. WIA_PUSHFUNCTION(TEXT("CPromptThread::Create"));
  137. HANDLE hThreadResult = NULL;
  138. CPromptThread *pThread = new CPromptThread(EventParameters);
  139. if (pThread)
  140. {
  141. DWORD dwThreadId;
  142. hThreadResult = CreateThread( NULL, 0, ThreadProc, pThread, 0, &dwThreadId );
  143. if (!hThreadResult)
  144. {
  145. delete pThread;
  146. }
  147. }
  148. return hThreadResult;
  149. }
  150. }; // CPromptThread
  151. // CEventPrompt responds to the event by asking the user which event handler
  152. // to run
  153. class ATL_NO_VTABLE CEventPrompt :
  154. public CComObjectRootEx<CComSingleThreadModel>,
  155. public CComCoClass<CEventPrompt, &CLSID_EventPrompter>,
  156. public IWiaEventCallback
  157. {
  158. public:
  159. CEventPrompt()
  160. {
  161. }
  162. ~CEventPrompt()
  163. {
  164. }
  165. DECLARE_REGISTRY_RESOURCEID(IDR_EVENTPROMPT)
  166. DECLARE_PROTECT_FINAL_CONSTRUCT()
  167. BEGIN_COM_MAP(CEventPrompt)
  168. COM_INTERFACE_ENTRY(IWiaEventCallback)
  169. END_COM_MAP()
  170. public:
  171. // IWiaEventCallback
  172. STDMETHODIMP ImageEventCallback (const GUID __RPC_FAR *pEventGUID,
  173. BSTR bstrEventDescription,
  174. BSTR bstrDeviceID,
  175. BSTR bstrDeviceDescription,
  176. DWORD dwDeviceType,
  177. BSTR bstrFullItemName,
  178. ULONG *pulEventType,
  179. ULONG ulReserved)
  180. {
  181. WIA_PUSHFUNCTION(TEXT("CEventPrompt::ImageEventCallback"));
  182. bool bRun = true;
  183. // Find out if we already have a prompt for this event open.
  184. // If the user just keeps pressing the same button we ignore
  185. // successive presses until the choice is made.
  186. CSharedMemorySection<HWND> PromptSharedMemory;
  187. LPWSTR wszGuid;
  188. StringFromCLSID (*pEventGUID, &wszGuid);
  189. // use a unique name for the shared memory
  190. CSimpleStringWide strSection(wszGuid);
  191. strSection.Concat (CSimpleStringWide(bstrDeviceID));
  192. CoTaskMemFree (wszGuid);
  193. if (CSharedMemorySection<HWND>::SmsOpened == PromptSharedMemory.Open(CSimpleStringConvert::NaturalString(strSection), false))
  194. {
  195. HWND *pHwnd = PromptSharedMemory.Lock();
  196. if (pHwnd)
  197. {
  198. bRun = false;
  199. if (*pHwnd && IsWindow(*pHwnd))
  200. {
  201. SetForegroundWindow(*pHwnd);
  202. }
  203. PromptSharedMemory.Release();
  204. }
  205. }
  206. if (bRun)
  207. {
  208. CEventParameters EventParameters;
  209. EventParameters.EventGUID = *pEventGUID;
  210. EventParameters.strEventDescription = static_cast<LPCWSTR>(bstrEventDescription);
  211. EventParameters.strDeviceID = static_cast<LPCWSTR>(bstrDeviceID);
  212. EventParameters.strDeviceDescription = static_cast<LPCWSTR>(bstrDeviceDescription);
  213. EventParameters.ulEventType = *pulEventType;
  214. EventParameters.ulReserved = ulReserved;
  215. EventParameters.strFullItemName = bstrFullItemName;
  216. HANDLE hThread = CPromptThread::Create (EventParameters);
  217. if (hThread)
  218. {
  219. CloseHandle(hThread);
  220. }
  221. }
  222. return S_OK;
  223. }
  224. }; // CEventPrompt
  225. #endif