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

  1. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2.  
  3. Copyright (c) 1998 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     appsink.cpp
  8.  
  9. Abstract:
  10.  
  11.     SR App sample application message sinks.  A CFormEventSink that will be
  12.     attached to all the forms we create in our application, and a CAppMessageSink
  13.     that will be attached to the forms manager.
  14.  
  15. Environment:
  16.  
  17.     AutoPC
  18.  
  19. -------------------------------------------------------------------*/
  20. // AutoPC specific includes
  21. #include <windows.h>
  22. #include <olectl.h>
  23. #include <asfc.h>
  24. #include <ascmnctl.h>
  25. #include <speech.h>
  26.  
  27. // Application specific includes
  28. #include "app.h"
  29. #include "resource.h"
  30. #include "appsink.h"
  31.  
  32. // Global application object
  33. CSRApp* g_pApp;    
  34.  
  35. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  36. Class:
  37.     CFormEventSink
  38.  
  39. Description:
  40.     Event sink attached to each of the forms.
  41. -------------------------------------------------------------------*/
  42.  
  43. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  44. Function:
  45.     CFormEventSink::CFormEventSink
  46.  
  47. Description:
  48.     Constructor.  Initializes all member variables.
  49.  
  50. Parameters:
  51.     DWORD AppThreadID - Applications thread ID
  52.  
  53. Returns:
  54.     Nothing.
  55. -------------------------------------------------------------------*/
  56. CFormEventSink::CFormEventSink(DWORD AppThreadID)
  57. {
  58.     m_cRef = 0;
  59.     m_AppThreadId = AppThreadID;
  60. }
  61.  
  62. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  63. Function:
  64.     CFormEventSink::QueryInterface
  65.  
  66. Description:
  67.     Standard COM IUknown method.  We implement IUnknown, IDispatch
  68.     (although not properly implemented), and IASEventSink interfaces.  
  69.  
  70. Parameters:
  71.     REFIID - Reference to requested Interface GUID
  72.     PVOID * - Target for interface pointer.
  73.  
  74. Returns:
  75.     HRESULT - NOERROR on success.  Other error code on fail.
  76. -------------------------------------------------------------------*/
  77. STDMETHODIMP
  78. CFormEventSink::QueryInterface(REFIID riid,
  79.                                PVOID *ppv)
  80. {
  81.     HRESULT hr = NOERROR;
  82.  
  83.     if(!ppv)
  84.         return E_INVALIDARG;
  85.  
  86.     if (riid == IID_ASEVENTSINK)
  87.         *ppv = (PVOID) (IASEventSink*) this; 
  88.      else if (riid == IID_IUnknown)
  89.         *ppv = (PVOID) (IUnknown*) this;
  90.        else if (riid == IID_IDispatch)
  91.         *ppv = (PVOID) (IDispatch*) this;
  92.     else
  93.         hr = E_NOINTERFACE;
  94.  
  95.     if(SUCCEEDED(hr)) 
  96.         AddRef();
  97.  
  98.     return hr;
  99. }
  100.  
  101. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  102. Function:
  103.     CFormEventSink::AddRef
  104.  
  105. Description:
  106.     Standard COM IUknown method.  Increases reference count on 
  107.     CFormEventSink object.
  108.  
  109. Parameters:
  110.     Nothing.
  111.  
  112. Returns:
  113.     ULONG - Reference count after increment.
  114. -------------------------------------------------------------------*/
  115. STDMETHODIMP_(ULONG)    
  116. CFormEventSink::AddRef()
  117. {
  118.     InterlockedIncrement(&m_cRef);
  119.     return m_cRef;         
  120. }
  121.  
  122. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  123. Function:
  124.     CFormEventSink::Release
  125.  
  126. Description:
  127.     Standard COM IUknown method.  Decrements reference count.  Frees
  128.     object when it is 0.
  129.  
  130. Parameters:
  131.     Nothing.
  132.  
  133. Returns:
  134.     ULONG - Reference count after decrement.
  135. -------------------------------------------------------------------*/
  136. STDMETHODIMP_(ULONG)CFormEventSink::Release()
  137. {
  138.     if (InterlockedDecrement(&m_cRef) == 0)
  139.     {
  140.         delete this;
  141.         return(0);
  142.     }
  143.     return m_cRef;
  144. }            
  145.  
  146. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  147. Function:
  148.     CFormEventSink::ReceiveMsg
  149.  
  150. Description:
  151.     IASEventSink method attached to Form.
  152.  
  153. Parameters:
  154.     long - Win32 Message
  155.     long - Win32 Parameter
  156.     long - Win32 Parameter
  157.  
  158. Returns:
  159.     HRESULT - S_FALSE if message should continue down message path.
  160.               NOERROR if message should stop here
  161. -------------------------------------------------------------------*/
  162. STDMETHODIMP
  163. CFormEventSink::ReceiveMsg(LONG uMsg, LONG wParam, LONG lParam)
  164. {
  165.     long lRet;
  166.     HRESULT hr;
  167.  
  168.     switch (uMsg)
  169.     {
  170.     default:
  171.         break;
  172.  
  173.     case WM_COMMAND:
  174.         switch (LOWORD(wParam))
  175.         {
  176.         default:
  177.             break;
  178.  
  179.         case IDC_PLB_MENU:
  180.             switch(HIWORD(lParam))
  181.             {
  182.             default:
  183.                 break;
  184.  
  185.             case IDMI_TRAIN_COLORS:
  186.                 g_pApp->m_pSpeech->Train(L"Black",    sizeof(L"Black"),    L"Black",    sizeof(L"Black"),    NULL, NULL);
  187.                 g_pApp->m_pSpeech->Train(L"Red",    sizeof(L"Red"),        L"Red",        sizeof(L"Red"),        NULL, NULL);
  188.                 g_pApp->m_pSpeech->Train(L"Green",    sizeof(L"Green"),    L"Green",    sizeof(L"Green"),    NULL, NULL);
  189.                 g_pApp->m_pSpeech->Train(L"Blue",    sizeof(L"Blue"),    L"Blue",    sizeof(L"Blue"),    NULL, NULL);
  190.                 g_pApp->m_pSpeech->Train(L"Yellow", sizeof(L"Yellow"),    L"Yellow",    sizeof(L"Yellow"),    NULL, NULL);
  191.                 g_pApp->m_pSpeech->Train(L"Violet", sizeof(L"Violet"),    L"Violet",    sizeof(L"Violet"),    NULL, NULL);
  192.                 g_pApp->m_pSpeech->Train(L"Cyan",    sizeof(L"Cyan"),    L"Cyan",    sizeof(L"Cyan"),    NULL, NULL);
  193.                 g_pApp->m_pSpeech->Train(L"White",    sizeof(L"White"),    L"White",    sizeof(L"White"),    NULL, NULL);
  194.                 break;
  195.  
  196.             case IDMI_EXIT:
  197.                 g_pApp->Quit();
  198.                 break;
  199.  
  200.             case IDMI_NUMBERS:
  201.                 g_pApp->m_pActiveForms->SetFocus(g_pApp->m_pNumbersForm);
  202.                 break;
  203.  
  204.             case IDMI_COLORS:
  205.                 hr = g_pApp->CreateColorsVoiceMenu();
  206.                 if(FAILED(hr))
  207.                 {
  208.                     // Message box saying words not trained yet
  209.                     HRESULT hr = g_pApp->m_pManage->fmMessageBox(
  210.                         g_pApp->m_bstrNoColors,
  211.                         g_pApp->m_bstrNoColorsTitle,
  212.                         MB_OK | MB_DEFBUTTON2,
  213.                         FMMB_FLG_TTS,
  214.                         &lRet);
  215.                 }
  216.                 else
  217.                 {
  218.                     g_pApp->m_pActiveForms->SetFocus(g_pApp->m_pColorsForm);
  219.                 }
  220.                 break;
  221.             } // endswitch HIWORD(lParam)
  222.         break;
  223.         } // endswitch LOWORD(wParam)
  224.  
  225.     } // endswitch uMsg
  226.  
  227.     return NOERROR;
  228. }
  229.  
  230. //--------------------------------------------------------------------------------------------
  231. //
  232. // CAppMessageSink SINK
  233. //
  234. //--------------------------------------------------------------------------------------------
  235.  
  236. CAppMessageSink::CAppMessageSink(DWORD   AppThreadID)
  237. {
  238.     m_cRef = 0;
  239.     m_AppThreadId = AppThreadID;
  240. }
  241.  
  242. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  243. Function:
  244.     CAppMessageSink::QueryInterface
  245.  
  246. Description:
  247.     Standard COM IUknown method.  We implement IUnknown, IDispatch
  248.     (although not properly implemented), IASEventSink, and 
  249.     IASClassMsgSink interfaces.  
  250.  
  251. Parameters:
  252.     REFIID - Reference to requested Interface GUID
  253.     PVOID * - Target for interface pointer.
  254.  
  255. Returns:
  256.     HRESULT - NOERROR on success.  Other error code on fail.
  257. -------------------------------------------------------------------*/
  258. STDMETHODIMP
  259. CAppMessageSink::QueryInterface(REFIID     riid,
  260.                                 PVOID *    ppv)
  261. {
  262.     if(!ppv)
  263.         return E_INVALIDARG;
  264.  
  265.     if (riid == IID_ASCLASSMSGSINK)
  266.         *ppv = (PVOID) (IASClassMsgSink *) this; 
  267.     else if (riid == IID_ASEVENTSINK)
  268.         *ppv = (PVOID) (IASEventSink*) this; 
  269.       else if (riid == IID_IUnknown)
  270.         *ppv = (PVOID) (IUnknown *) (IASClassMsgSink *)this;
  271.        else if (riid == IID_IDispatch)
  272.         *ppv = (PVOID) (IDispatch *) (IASClassMsgSink *)this;
  273.     else
  274.         return E_NOINTERFACE;
  275.  
  276.     AddRef();
  277.  
  278.     return NOERROR;
  279. }
  280.  
  281. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  282. Function:
  283.     CAppMessageSink::AddRef
  284.  
  285. Description:
  286.     Standard COM IUknown method.  Increases reference count on 
  287.     CAppMessageSink object.
  288.  
  289. Parameters:
  290.     Nothing.
  291.  
  292. Returns:
  293.     ULONG - Reference count after increment.
  294. -------------------------------------------------------------------*/
  295. STDMETHODIMP_(ULONG)
  296. CAppMessageSink::AddRef()
  297. {
  298.     InterlockedIncrement(&m_cRef);
  299.     return m_cRef;         
  300. }
  301.  
  302. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  303. Function:
  304.     CAppMessageSink::Release
  305.  
  306. Description:
  307.     Standard COM IUknown method.  Decrements reference count.  Frees
  308.     object when it is 0.
  309.  
  310. Parameters:
  311.     Nothing.
  312.  
  313. Returns:
  314.     ULONG - Reference count after decrement.
  315. -------------------------------------------------------------------*/
  316. STDMETHODIMP_(ULONG)
  317. CAppMessageSink::Release()
  318. {
  319.     if (InterlockedDecrement(&m_cRef) == 0)
  320.     {
  321.         delete this;
  322.         return(0);
  323.     }
  324.     return m_cRef;
  325. }            
  326.  
  327. // IASClassMsgSink
  328. // This method is invoked whenever an event is fired
  329. STDMETHODIMP CAppMessageSink::HandleMessage(IDispatch* pdispControl, LONG uMsg, LONG wParam, LONG lParam)
  330. {
  331.     switch (uMsg)
  332.     {
  333.     default:
  334.         break;
  335.  
  336.     case WM_SPCH_RECOG: 
  337.         switch (lParam)
  338.         {
  339.         default:
  340.             break;
  341.  
  342.         case IDVMI_BLACK:
  343.             g_pApp->m_pColorsForm->put_BackColor(BLACK);
  344.             break;
  345.         case IDVMI_RED:
  346.             g_pApp->m_pColorsForm->put_BackColor(RED);
  347.             break;
  348.         case IDVMI_GREEN:
  349.             g_pApp->m_pColorsForm->put_BackColor(GREEN);
  350.             break;
  351.         case IDVMI_BLUE:
  352.             g_pApp->m_pColorsForm->put_BackColor(BLUE);
  353.             break;
  354.         case IDVMI_YELLOW:
  355.             g_pApp->m_pColorsForm->put_BackColor(YELLOW);
  356.             break;
  357.         case IDVMI_VIOLET:
  358.             g_pApp->m_pColorsForm->put_BackColor(VIOLET);
  359.             break;
  360.         case IDVMI_CYAN:
  361.             g_pApp->m_pColorsForm->put_BackColor(CYAN);
  362.             break;
  363.         case IDVMI_WHITE:
  364.             g_pApp->m_pColorsForm->put_BackColor(WHITE);
  365.             break;
  366.  
  367.         case IDVMI_ZERO:
  368.         case IDVMI_ONE:
  369.         case IDVMI_TWO:
  370.         case IDVMI_THREE:
  371.         case IDVMI_FOUR:
  372.         case IDVMI_FIVE:
  373.         case IDVMI_SIX:
  374.         case IDVMI_SEVEN:
  375.         case IDVMI_EIGHT:
  376.         case IDVMI_NINE:
  377.             g_pApp->AddNumber(lParam - IDVMI_NUMBERS_BASE);
  378.             break;
  379.  
  380.         case IDVMI_NUMBERS_END:
  381.             g_pApp->m_pActiveForms->SetFocus(g_pApp->m_pMainForm);
  382.             break;
  383.  
  384.         case IDVMI_COLORS_END:
  385.             g_pApp->m_pActiveForms->SetFocus(g_pApp->m_pMainForm);
  386.             break;
  387.  
  388.         }
  389.         break;
  390.     }
  391.  
  392.     return S_FALSE;
  393. }
  394.  
  395.