home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / AutoPC / apcsdk10.exe / data1.cab / Win32_Samples / win32 / Keyboard / appsink.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-13  |  9.7 KB  |  397 lines

  1. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2.  
  3. Copyright (c) 1998 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     appsink.cpp
  8.  
  9. Abstract:
  10.  
  11.     Keyboard Sample application sinks.  Implements two classes, 
  12.     CFormEventSink and CAppMessageSink.
  13.  
  14. Environment:
  15.  
  16.     AutoPC
  17.  
  18. -------------------------------------------------------------------*/
  19. // System specific includes
  20. #include <windows.h>
  21. #include <olectl.h>
  22. #include <asfc.h>
  23. #include <ascmnctl.h>
  24. #include <speech.h>
  25. #include <keypad.h>
  26.  
  27. // Application specific includes
  28. #include "app.h"
  29. #include "resource.h"
  30. #include "appsink.h"
  31.  
  32. // Global Application class object
  33. extern CKeyboardApp* g_pApp;
  34.  
  35. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  36. Class:
  37.     CFormEventSink
  38.  
  39. Description:
  40.     Implements IASEventSink interface.  Doesn't handle any messages
  41.     (All of them are handled in the ClassMsgSink attached to the form
  42.     manager)
  43. -------------------------------------------------------------------*/
  44.  
  45. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  46. Function:
  47.     CFormEventSink::CFormEventSink
  48.  
  49. Description:
  50.     Constructor.  Initializes all member variables.
  51.  
  52. Parameters:
  53.     DWORD - Applications main thread ID
  54.  
  55. Returns:
  56.     Nothing.
  57. -------------------------------------------------------------------*/
  58. CFormEventSink::CFormEventSink(DWORD AppThreadID)
  59. {
  60.     m_cRef = 0;
  61.     m_AppThreadId = AppThreadID;
  62. }
  63.  
  64. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  65. Function:
  66.     CFormEventSink::QueryInterface
  67.  
  68. Description:
  69.     Standard COM IUknown method.  
  70.  
  71. Parameters:
  72.     REFIID - Reference to requested Interface GUID
  73.     PVOID * - Target for interface pointer.
  74.  
  75. Returns:
  76.     HRESULT - NOERROR on success.  Other error code on fail.
  77. -------------------------------------------------------------------*/
  78. STDMETHODIMP
  79. CFormEventSink::QueryInterface( REFIID riid,
  80.                                 PVOID *ppv)
  81. {
  82.  
  83.     HRESULT hr = NOERROR;
  84.  
  85.     if (!ppv)
  86.         hr = E_INVALIDARG;
  87.  
  88.     // Return the requested interface
  89.     if( SUCCEEDED( hr ) ) {
  90.         if (riid == IID_ASEVENTSINK)
  91.             *ppv = (PVOID) (IASEventSink*) this; 
  92.         else if (riid == IID_IUnknown)
  93.             *ppv = (PVOID) (IUnknown*) (IVCmdNotifySinkW*) this;
  94.         else if (riid == IID_IDispatch)
  95.             *ppv = (PVOID) (IDispatch*) this;
  96.         else
  97.             hr = E_NOINTERFACE;
  98.     }
  99.  
  100.     if( SUCCEEDED( hr ) ) 
  101.         AddRef();
  102.  
  103.     return hr;
  104. }
  105.  
  106. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  107. Function:
  108.     CFormEventSink::AddRef
  109.  
  110. Description:
  111.     Standard COM IUknown method.  
  112.  
  113. Parameters:
  114.     Nothing.
  115.  
  116. Returns:
  117.     ULONG - Reference count after increment.
  118. -------------------------------------------------------------------*/
  119. STDMETHODIMP_(ULONG)
  120. CFormEventSink::AddRef()
  121. {
  122.     InterlockedIncrement(&m_cRef);
  123.  
  124.     return m_cRef;         
  125. }
  126.  
  127. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  128. Function:
  129.     CFormEventSink::Release
  130.  
  131. Description:
  132.     Standard COM IUknown method.  
  133.  
  134. Parameters:
  135.     Nothing.
  136.  
  137. Returns:
  138.     ULONG - Reference count after increment.
  139. -------------------------------------------------------------------*/
  140. STDMETHODIMP_(ULONG)
  141. CFormEventSink::Release()
  142. {
  143.  
  144.     if (InterlockedDecrement(&m_cRef) == 0)
  145.     {
  146.         delete this;
  147.         return(0);
  148.     }
  149.     return m_cRef;
  150. }            
  151.  
  152. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  153. Function:
  154.     CAppEventSink::ReceiveMsg
  155.  
  156. Description:
  157.     IASEventSink method attached to Form.
  158.  
  159. Parameters:
  160.     long - Win32 Message
  161.     long - Win32 Parameter
  162.     long - Win32 Parameter
  163.  
  164. Returns:
  165.     HRESULT - S_FALSE if message should continue down message path.
  166.               NOERROR if message should stop here
  167. -------------------------------------------------------------------*/
  168. STDMETHODIMP
  169. CFormEventSink::ReceiveMsg(LONG uMsg, LONG wParam, LONG lParam)
  170. {
  171.     return S_FALSE;
  172. }
  173.  
  174.  
  175. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  176. Class:
  177.     CAppMessageSink
  178.  
  179. Description:
  180.     ClassMsgSink and EventSink attached to the form Manager.
  181. -------------------------------------------------------------------*/
  182.  
  183. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  184. Function:
  185.     CAppMessageSink::CAppMessageSink
  186.  
  187. Description:
  188.     Constructor.  Initializes all member variables.
  189.  
  190. Parameters:
  191.     DWORD - Applications main thread ID
  192.  
  193. Returns:
  194.     Nothing.
  195. -------------------------------------------------------------------*/
  196. CAppMessageSink::CAppMessageSink(DWORD AppThreadID)
  197. {
  198.     m_cRef = 0;
  199.     m_AppThreadId = AppThreadID;
  200. }
  201.  
  202. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  203. Function:
  204.     CAppMessageSink::QueryInterface
  205.  
  206. Description:
  207.     Standard COM IUknown method.  
  208.  
  209. Parameters:
  210.     REFIID - Reference to requested Interface GUID
  211.     PVOID * - Target for interface pointer.
  212.  
  213. Returns:
  214.     HRESULT - NOERROR on success.  Other error code on fail.
  215. -------------------------------------------------------------------*/
  216. STDMETHODIMP    
  217. CAppMessageSink::QueryInterface(REFIID riid, PVOID *ppv)
  218. {
  219.     HRESULT hr = NOERROR;
  220.  
  221.     if (!ppv)
  222.         hr = E_INVALIDARG;
  223.  
  224.     // Return the requested interface.
  225.     if( SUCCEEDED( hr ) ) {
  226.         if (riid == IID_ASCLASSMSGSINK)
  227.             *ppv = (PVOID) (IASClassMsgSink *) this; 
  228.         else if (riid == IID_ASEVENTSINK)
  229.             *ppv = (PVOID) (IASEventSink*) this; 
  230.         else if (riid == IID_IUnknown)
  231.             *ppv = (PVOID) (IUnknown *) (IASClassMsgSink *)this;
  232.         else if (riid == IID_IDispatch)
  233.             *ppv = (PVOID) (IDispatch *) (IASClassMsgSink *)this;
  234.         else
  235.             hr = E_NOINTERFACE;
  236.     }
  237.  
  238.     if( SUCCEEDED( hr ) ) 
  239.         AddRef();
  240.  
  241.     return hr;
  242. }
  243.  
  244. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  245. Function:
  246.     CAppEventSink::AddRef
  247.  
  248. Description:
  249.     Standard COM IUknown method. Increases the objects reference count.
  250.  
  251. Parameters:
  252.     Nothing.
  253.  
  254. Returns:
  255.     ULONG - Reference count after increment.
  256. -------------------------------------------------------------------*/
  257. STDMETHODIMP_(ULONG)
  258. CAppMessageSink::AddRef( VOID)
  259. {
  260.     InterlockedIncrement(&m_cRef);
  261.     return m_cRef;         
  262. }
  263.  
  264. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  265. Function:
  266.     CAppEventSink::Release
  267.  
  268. Description:
  269.     Standard COM IUknown method. Decreases the objects reference
  270.     count and deletes it when no longer needed.
  271.  
  272. Parameters:
  273.     Nothing.
  274.  
  275. Returns:
  276.     ULONG - Reference count after decrement.
  277. -------------------------------------------------------------------*/
  278. STDMETHODIMP_(ULONG)
  279. CAppMessageSink::Release( VOID)
  280. {
  281.     if (InterlockedDecrement(&m_cRef) == 0)
  282.     {
  283.         delete this;
  284.         return(0);
  285.     }
  286.     return m_cRef;
  287. }            
  288.  
  289. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  290. Function:
  291.     CAppEventSink::HandleKeyPress
  292.  
  293. Description:
  294.     IASClassMsgSink method. Called when a keypad key (lKeyPress = 'x'), 
  295.     enter (VK_RETURN), and escape (VK_ESCAPE).
  296.  
  297. Parameters:
  298.     IDispatch * pdispControl - Control receiving keypress.
  299.     LONG lKeyPress - Key being pressed.
  300.  
  301. Returns:
  302.     S_FALSE - To keep message going down the message path.
  303.     NOERROR - To eat the message.
  304. -------------------------------------------------------------------*/
  305. STDMETHODIMP 
  306. CAppMessageSink::HandleKeyPress(IDispatch* pdispControl, LONG lKeyPress)
  307. {
  308.     WCHAR wsztext[80] = { 0 };
  309.  
  310.     switch (lKeyPress)
  311.     {
  312.     // Case of any numeric keypad presses
  313.     case '0': case '1': case '2': case '3': case '4': case '5': case '6':
  314.     case '7': case '8': case '9': case '*': case '#':
  315.         wsprintf(wsztext, TEXT("HandleKeyPress %c "), (char)lKeyPress);
  316.         break;
  317.  
  318.     // Enter (return) pressed
  319.     case VK_RETURN:
  320.         lstrcpy(wsztext, TEXT("HandleKeyPress enter"));
  321.         break;
  322.  
  323.     // Any keypress not handled above.  On v1.0 devices, there shouldn't
  324.     // be any, but on future devices, they may include a keyboard or
  325.     // key input device.
  326.     default:
  327.         wsprintf(wsztext, TEXT("HandleKeyPress missed %d "), lKeyPress);
  328.         break;
  329.     }
  330.     g_pApp->UpdateSystemLabel(wsztext);
  331.     return S_FALSE;
  332. }
  333.  
  334. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  335. Function:
  336.     CAppEventSink::HandleMessage
  337.  
  338. Description:
  339.     IASClassMsgSink method. Called when a message is fired towards 
  340.     our forms manager.
  341.  
  342. Parameters:
  343.     IDispatch * pdispControl - Control to receive message.
  344.     LONG uMsg - Win32 Message.
  345.     LONG wParam - Message Parameter.
  346.     LONG lParam - Message Parameter.
  347.  
  348. Returns:
  349.     S_FALSE - To keep message going down the message path.
  350.     NOERROR - To eat the message.
  351. -------------------------------------------------------------------*/
  352. STDMETHODIMP 
  353. CAppMessageSink::HandleMessage(IDispatch* pdispControl, long uMsg, long wParam, long lParam)
  354. {
  355.     // If someone presses the escape key (which would bring them back 
  356.     // to the shell), we kill the process by sending a WM_QUIT
  357.     // message to our app.
  358.     if(uMsg == WM_REMOTE_KEYDOWN && wParam == VK_ESCAPE)
  359.     {
  360.         PostThreadMessage(m_AppThreadId, WM_QUIT, 0, 0);
  361.         return NOERROR;
  362.     }
  363.  
  364.     // We let a main application method handle these messages.
  365.     g_pApp->ProcessMessage(uMsg, wParam, lParam);
  366.  
  367.     return S_FALSE;
  368. }
  369.  
  370. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  371. Function:
  372.     CAppEventSink::ReceiveMsg
  373.  
  374. Description:
  375.     IASEventSink method. Attached to the forms manager.  Called first 
  376.     when a message is fired towards our forms manager.  In this case we 
  377.     do nothing and let the ClassMsgSink methods handle all messages.
  378.  
  379. Parameters:
  380.     LONG uMsg - Win32 Message.
  381.     LONG wParam - Message Parameter.
  382.     LONG lParam - Message Parameter.
  383.  
  384. Returns:
  385.     S_FALSE - To keep message going down the message path.
  386.     NOERROR - To eat the message.
  387. -------------------------------------------------------------------*/
  388. STDMETHODIMP 
  389. CAppMessageSink::ReceiveMsg(long uMsg, long wParam, long lParam)
  390. {
  391.     return S_FALSE;
  392. }
  393.  
  394.  
  395.  
  396.  
  397.