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

  1. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2.  
  3. Copyright (c) 1998 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     names.cpp
  8.  
  9. Abstract:
  10.  
  11.     What's in a Name Sample application.  Implementation of sink and main app 
  12.     class.
  13.  
  14. Environment:
  15.  
  16.     AutoPC
  17.  
  18. -------------------------------------------------------------------*/
  19.  
  20. #include <windows.h>
  21. #include <olectl.h>
  22. #include <asfc.h>           // Apollo Forms Manager
  23. #include <ascmnctl.h>       // Apollo Common Controls
  24. #include <keypad.h>            // VK definitions for faceplate keys
  25. #include <abapi.h>            // Address Book api functions
  26.  
  27. #define APCDBG_INIT "What's in a name?"    // Text used in debug messages    
  28. #include <apcdebug.h>        // Standard APC debug header
  29.  
  30. #include "names.h"            // App and Event sink class definitions
  31. #include "resource.h"        // resource IDs
  32.  
  33. #include "namemeanings.h"    // String array of names and their meanings
  34.  
  35. //--------------------------------------------------------------------------------------------
  36. // Globals
  37. //--------------------------------------------------------------------------------------------
  38. CNamesApp*    g_pApp;    // Main application class
  39.  
  40. WCHAR*        SZEXIT            = L"Exit";
  41. WCHAR*        SZSELECTNAME    = L"Select Contact";
  42.  
  43. HINSTANCE    g_hInst            = NULL;        // application instance handle 
  44. DWORD        g_idThread        = 0;        // the ID of the main application thread
  45.  
  46. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  47. Class:
  48.     CAppEventSink
  49.  
  50. Description:
  51.     Implements IASEventSink interface (to receive the exit WM_COMMAND)
  52.     and IUnknown (because its a COM object)
  53. -------------------------------------------------------------------*/
  54.  
  55. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  56. Functions:
  57.     QueryInterface, AddRef, Release
  58.  
  59. Description:
  60.     COM requires the next three functions to be implemented.
  61.  
  62. Parameters:
  63.     Standard COM object params
  64. -------------------------------------------------------------------*/
  65. // Request a specific interface
  66. STDMETHODIMP 
  67. CAppEventSink::QueryInterface(REFIID riid, PVOID *ppv)
  68. {
  69.     if (!ppv) return E_INVALIDARG;
  70.  
  71.     // Return a pointer to the requested interface
  72.     if (riid == IID_ASEVENTSINK) *ppv = (PVOID) (IASEventSink *) this; 
  73.     else if (riid == IID_IUnknown) *ppv = (PVOID) (IUnknown *) this;
  74.     else if (riid == IID_IDispatch) *ppv = (PVOID) (IDispatch *) this;
  75.     else return E_NOINTERFACE;
  76.  
  77.     AddRef();
  78.     return NOERROR;
  79. }
  80.  
  81. // Increment this objects reference count
  82. STDMETHODIMP_(ULONG) 
  83. CAppEventSink::AddRef()
  84. {
  85.     InterlockedIncrement(&m_cRef);
  86.     return m_cRef;         
  87. }
  88.  
  89. // Decrement the objects reference count.  Delete if no longer needed
  90. STDMETHODIMP_(ULONG)
  91. CAppEventSink::Release()
  92. {
  93.     if (InterlockedDecrement(&m_cRef) > 0) return m_cRef;
  94.  
  95.     delete this;
  96.     return 0; 
  97. }            
  98.  
  99.  
  100. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  101. Function:
  102.     ReceiveMsg
  103.  
  104. Description:
  105.     Applications event sink.  
  106.  
  107. Parameters:
  108.     Standard Win32 Message params.  
  109. -------------------------------------------------------------------*/
  110. STDMETHODIMP 
  111. CAppEventSink::ReceiveMsg(long uMsg, long wParam, long lParam)
  112. {
  113.     if (WM_COMMAND != uMsg)
  114.         return S_FALSE;
  115.  
  116.     // WM_COMMAND message parameters for a menu/PLB:
  117.     //        wParamLO - ID of control message sent to
  118.     //        wParamHI - Action taken on control
  119.     //            LBN_DBLCLK        - option selected
  120.     //            PLBN_SPINCHANGE    - A spinner change
  121.     //            PLBN_BEGINEDIT    - started to edit an edit item
  122.     //            PLBN_ENDEDIT    - finished editing an edit item
  123.     //        lParamLO - Index of item (selected or spun to)
  124.     //        lParamHI - Item ID acted on
  125.     //
  126.     // Since there is only one menu control in this application, we won't
  127.     // check wParamLO
  128.     if(HIWORD(wParam) == LBN_DBLCLK)
  129.     {
  130.     switch (HIWORD(lParam))
  131.         {
  132.         case ID_EXIT:
  133.             PostThreadMessage(g_idThread, WM_QUIT, 0, 0);
  134.             break;
  135.  
  136.         case ID_ADDRESS:
  137.             if(SUCCEEDED(g_pApp->PickName())) 
  138.                 g_pApp->ShowMeaning();
  139.             break;
  140.         }
  141.     }
  142.     return S_FALSE;
  143. }
  144.  
  145. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  146. Class:
  147.     CNamesApp
  148.  
  149. Description:
  150.     Main application specific class.  Doesn't implement COM or any
  151.     other interfaces.  
  152. -------------------------------------------------------------------*/
  153.  
  154. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  155. Function:
  156.     CNamesApp
  157.  
  158. Description:
  159.     Application object constructor.
  160. -------------------------------------------------------------------*/
  161. CNamesApp::CNamesApp()
  162. {
  163.     m_pManage            = NULL; 
  164.     m_pForm                = NULL;    
  165.     m_pSink                = NULL;    
  166.     m_pPLBMenu            = NULL;
  167.     m_oh                = NULL;
  168.     m_bstrAppName        = NULL;
  169.     m_bstrExit            = NULL;
  170.     m_bstrSelectNameText= NULL;
  171. }
  172.  
  173. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  174. Function:
  175.     ~CNamesApp
  176.  
  177. Description:
  178.     Destructor.  Does nothing, all cleanup handled in CleanUp function
  179. -------------------------------------------------------------------*/
  180. CNamesApp::~CNamesApp()
  181. {
  182.  
  183. }
  184.  
  185. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  186. Function:
  187.     PickName
  188.  
  189. Description:
  190.     Opens address book, allows user to select a contact, then saves
  191.     the First and Last name from the contacts database.
  192.  
  193. Parameters:
  194.     Returns HRESULT code
  195. -------------------------------------------------------------------*/
  196. HRESULT
  197. CNamesApp::PickName()
  198. {
  199.     HRESULT hr;
  200.     WCHAR *wszName = NULL;
  201.  
  202.     hr = abOpen(m_pManage);
  203.  
  204.     if(SUCCEEDED(hr))
  205.     {
  206.         // Fill out ABDialogInfo structure.  Controls what features and views
  207.         // are enabled/disabled on address book popup dialog.
  208.         // Parameter description found in abapi.h
  209.         ABDIALOGINFO abDI = {
  210.             0,                // contact ID - read/write
  211.             ABE_EXITMAIN,    // feature enable mask - combination of ABE_ constants - read
  212.             0,                // depends on wInitView - should be zero if unused
  213.             ABV_MAINVIEW,    // initial/final view - one of ABV_ constants - read/write
  214.             1,                // field ordinal - read/write
  215.             ABV_CARDHOME };    // initial card
  216.  
  217.         // Bring up the Address Book dialog box
  218.         hr = abDialog(&abDI);
  219.  
  220.         if (hr == S_OK)
  221.         {
  222.             CEPROPID rgidProps[] = { HHPR_GIVEN_NAME, HHPR_SURNAME };    // List of properties we want
  223.             PCEPROPVAL prgProps = 0;
  224.             WORD cProps = sizeof(rgidProps)/sizeof(CEPROPID);            // Number of properties
  225.  
  226.             // Try to get properties of the last record the user was looking at
  227.             // Make sure neither the first (given) or last name are NULL
  228.             if(SUCCEEDED(abRead(abDI.ceoid, &cProps, rgidProps, &prgProps)))
  229.             {
  230.                 if(prgProps->val.lpwstr)
  231.                     wcscpy(m_bstrName, prgProps->val.lpwstr);        // Save first name field
  232.  
  233.                 if((prgProps+1)->val.lpwstr)
  234.                     wcscpy(m_bstrLastName, prgProps->val.lpwstr);    // Save last name field
  235.  
  236.                 LocalFree(prgProps);
  237.             }
  238.             else hr = E_FAIL;
  239.         }
  240.         else hr = E_FAIL;
  241.     }
  242.         
  243.     abClose();
  244.     return hr;
  245. }
  246.  
  247. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  248. Function:
  249.     Init
  250.  
  251. Description:
  252.     Application initialization.  Allocates storage for all strings,
  253.     Creates event sink, forms manager, main form, and adds controls
  254.     to it.
  255.  
  256. Parameters:
  257.     Returns HRESULT code.
  258. -------------------------------------------------------------------*/
  259. HRESULT
  260. CNamesApp::Init()
  261. {
  262.     // Load app string name from resource script
  263.     WCHAR wszAppName[40];
  264.     LoadString(g_hInst, STR_APP_SHELL_NAME, wszAppName, 39);
  265.     m_bstrAppName = SysAllocString(wszAppName);
  266.     if(!m_bstrAppName) return E_FAIL;
  267.  
  268.     m_bstrSelectNameText = SysAllocString(SZSELECTNAME);
  269.     if(!m_bstrSelectNameText) return E_FAIL;
  270.  
  271.     m_bstrExit = SysAllocString(SZEXIT);
  272.     if(!m_bstrExit) return E_FAIL;
  273.  
  274.     m_bstrName = SysAllocStringLen(NULL, 100);
  275.     if(!m_bstrName) return E_FAIL;
  276.     
  277.     m_bstrLastName = SysAllocStringLen(NULL, 100);
  278.     if(!m_bstrLastName) return E_FAIL;
  279.  
  280.     if(FAILED(CreateEventSink())) return E_FAIL;
  281.     if(FAILED(GetFormsManager())) return E_FAIL;
  282.     if(FAILED(CreateMainForm())) return E_FAIL;
  283.     if(FAILED(AddControls())) return E_FAIL;
  284.  
  285.     return NOERROR;
  286. }
  287.  
  288. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  289. Function:
  290.     CleanUp
  291.  
  292. Description:
  293.     Frees all strings, Releases all pointers.
  294. -------------------------------------------------------------------*/
  295. void
  296. CNamesApp::CleanUp()
  297. {
  298.     m_pManage->DeregisterStartedApplication(m_oh, m_bstrAppName);
  299.     
  300.     if(m_bstrSelectNameText) SysFreeString(m_bstrSelectNameText);
  301.     if(m_bstrExit) SysFreeString(m_bstrExit);
  302.     if(m_bstrName) SysFreeString(m_bstrName);
  303.     if(m_bstrLastName) SysFreeString(m_bstrLastName);
  304.  
  305.     if(m_pPLBMenu) {
  306.         m_pPLBMenu->RemoveAll();
  307.         m_pPLBMenu->Release();
  308.     }
  309.  
  310.     // Must close the form before we release
  311.     if(m_pForm) {
  312.         m_pForm->Close();
  313.         m_pForm->Release();
  314.     }
  315.  
  316.     if(m_pSink) m_pSink->Release();
  317.     if(m_pManage) m_pManage->Release();
  318.     if(m_bstrAppName) SysFreeString(m_bstrAppName);
  319. }
  320.  
  321. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  322. Function:
  323.     CreateMainForm
  324.  
  325. Description:
  326.     Creates the main form and attatches the event sink to it.
  327.  
  328. Parameters:
  329.     Returns HRESULT return code.
  330. -------------------------------------------------------------------*/
  331. HRESULT 
  332. CNamesApp::CreateMainForm()
  333. {
  334.     HRESULT hr;
  335.   
  336.     // Create the form
  337.     hr = CoCreateInstance(
  338.         CLSID_ASFORM,            // Class ID of form object
  339.         NULL,                    // Not part of COM aggregation
  340.         CLSCTX_INPROC_SERVER,    // Create object in our process space
  341.         IID_ASFORM,                // Request the IASForm interface
  342.        (PVOID *) &m_pForm);        // Target to receive interface
  343.     if (FAILED(hr)) return hr;
  344.  
  345.     // Have to initialize and start our form
  346.     hr = m_pForm->Init(ID_MAINFORM, NULL, m_pSink);
  347.     if (FAILED(hr)) return hr;
  348.  
  349.     // Must make form visible
  350.     m_pForm->put_Visible(TRUE);
  351.  
  352.     hr = m_pManage->Start(m_pForm);
  353.     if (FAILED(hr)) return hr;
  354.  
  355.     m_pForm->put_Caption(m_bstrAppName);    // Add the caption to title bar
  356.  
  357.     return NOERROR;
  358. }
  359.  
  360. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  361. Function:
  362.     CreateEventSink
  363.  
  364. Description:
  365.     Creates the Application's event sink.  
  366.  
  367. Parameters:
  368.     Returns HRESULT return code  
  369. -------------------------------------------------------------------*/
  370. HRESULT 
  371. CNamesApp::CreateEventSink()
  372. {
  373.     HRESULT hr;
  374.     CAppEventSink * pSink;
  375.     pSink = new CAppEventSink;
  376.  
  377.     // get an interface for us to keep
  378.     hr = pSink->QueryInterface(IID_ASEVENTSINK, (PVOID *) &m_pSink);
  379.     
  380.     return hr;
  381. }
  382.  
  383. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  384. Function:
  385.     GetFormsManager
  386.  
  387. Description:
  388.     Creates forms manager interface, registers the application, and
  389.     Moves the app to foreground (in case it wasn't started form shell)
  390.  
  391. Parameters:
  392.     Returns HRESULT return code
  393. -------------------------------------------------------------------*/
  394. HRESULT 
  395. CNamesApp::GetFormsManager()
  396. {
  397.     HRESULT hr;
  398.  
  399.     hr = CoCreateInstance(
  400.         CLSID_FMMANAGE,            // Class ID of form's manager object 
  401.         NULL,                    // No COM aggregation
  402.         CLSCTX_INPROC_SERVER,    // Create object in our process space
  403.         IID_FMMANAGE,            // Request IfmManage interface
  404.         (PVOID *) &m_pManage);    // Target to receive interface
  405.     if(FAILED(hr)) return hr;
  406.  
  407.     // Get the default faceplate handle (form context handle)
  408.     hr = m_pManage->GetFormsContextHandle(&m_oh);
  409.     if(FAILED(hr)) return hr;
  410.  
  411.     // Register our application on the default faceplate with the 
  412.     // form's manager
  413.     hr = m_pManage->RegisterStartedApplication(m_oh, m_bstrAppName, 0, 0);
  414.     if(FAILED(hr)) return hr;
  415.  
  416.     // Bring our application to the foreground, in case app wasn't started
  417.     // from the AutoPc shell (ppsh or visual C ide)
  418.     hr = m_pManage->MoveAppToForeground(GetCurrentProcessId(), 0, 0);
  419.     if(FAILED(hr)) return hr;
  420.  
  421. hr = m_pManage->put_EventSink(m_pSink);
  422. if(FAILED(hr)) return E_FAIL;
  423.  
  424.     return S_OK;
  425. }
  426.  
  427. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  428. Function:
  429.     AddControls
  430.  
  431. Description:
  432.     Adds controls to the main form.
  433.  
  434. Parameters:
  435.     Returns HRESULT return code
  436. -------------------------------------------------------------------*/
  437. HRESULT 
  438. CNamesApp::AddControls()
  439. {
  440.     HRESULT         hr;
  441.  
  442.     // Create a Power List Box
  443.     hr = CoCreateInstance(
  444.         CLSID_ASPOWERLISTBOX,        // Class ID of the power list box  
  445.         NULL,                        // No COM aggregation
  446.         CLSCTX_INPROC_SERVER,        // Create object in our process space
  447.         IID_ASPOWERLISTBOX,            // Request the IASPowerListBox interface
  448.         (PVOID *) &m_pPLBMenu);        // Target for requested interface
  449.     if (FAILED(hr))    return hr;
  450.  
  451.     // Set the dimensions of our power list box
  452.     m_pPLBMenu->SetBounds(0, 18, 220, 46); 
  453.  
  454.     // Add the power list box control to our form
  455.     hr = m_pForm->Add(m_pPLBMenu, ID_PLBMENU);
  456.     if (FAILED(hr)) return hr;
  457.  
  458.     IASPowerListBoxItem* pItem;
  459.  
  460.     hr = m_pPLBMenu->CreateItem(&pItem,ID_ADDRESS, ASFC_PWRLB_TYPE_COMMAND);
  461.     if (FAILED(hr)) return hr;
  462.     pItem->put_Caption(m_bstrSelectNameText);
  463.     pItem->Release();
  464.  
  465.     hr = m_pPLBMenu->CreateItem(&pItem,ID_EXIT, ASFC_PWRLB_TYPE_COMMAND);
  466.     if (FAILED(hr)) return hr;
  467.     pItem->put_Caption(m_bstrExit);
  468.     pItem->Release();
  469.  
  470.       return S_OK;                    
  471. }
  472.  
  473. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  474. Function:
  475.     ShowMeaning
  476.  
  477. Description:
  478.     Shows the meaning of whatever name is in the bstrName and bstrLastName
  479.     variables.  Pops up a message box to display it.
  480. -------------------------------------------------------------------*/
  481. void CNamesApp::ShowMeaning()
  482. {
  483.     BSTR bstrMsg = SysAllocStringLen(NULL, 100);
  484.     BSTR bstrTitle = SysAllocStringLen(NULL, 100);
  485.     
  486.     int i=0;
  487.  
  488.     wsprintf(bstrMsg, L"There is no meaning in the database.");
  489.  
  490.     // Go through all the names in the "database" (big array) and
  491.     // try and find a match
  492.     while(wszaNames[i] != NULL)
  493.     {
  494.         if(!wcscmp(wszaNames[i], m_bstrName))
  495.         {
  496.             wsprintf(bstrMsg, L"%s", wszaMeanings[i]);
  497.             break;
  498.         }
  499.         i++;
  500.     }
  501.     
  502.     // Format a message
  503.     wsprintf(bstrTitle, L"%s %s's Name Means:", m_bstrName, m_bstrLastName);
  504.  
  505.     long lRet = 0;
  506.  
  507.     // bring up a message box
  508.     HRESULT hr = m_pManage->fmMessageBox(
  509.         bstrMsg,        // Message to be displayed
  510.         bstrTitle,        // Title of message box
  511.         MB_OK,            // Only have OK button on MB
  512.         FMMB_FLG_TTS,    // TTS out the message
  513.         &lRet);            // Return
  514.  
  515.     SysFreeString(bstrMsg);
  516.     SysFreeString(bstrTitle);
  517. }
  518.  
  519.  
  520.  
  521. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  522. Function:
  523.     WinMain
  524.  
  525. Description:
  526.     Main Win32 Application entry point
  527.  
  528. Parameters:
  529.     Standard Win32 CE
  530. -------------------------------------------------------------------*/
  531. int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR wszCmd, int nCmdShow)
  532. {
  533.     MSG         msg;
  534.  
  535.     g_hInst = hInst;                    //save hInstance
  536.     g_idThread = GetCurrentThreadId();  //save the id of the main thread
  537.     g_pApp = new CNamesApp;            //Allocate our new App
  538.  
  539.     PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
  540.  
  541.     if(!g_pApp)
  542.         return FALSE;
  543.  
  544.     CoInitializeEx( NULL, COINIT_MULTITHREADED );
  545.  
  546.     HRESULT hr = InitASFormsManager();   // Initialize the forms manager
  547.     if (FAILED(hr))
  548.     {
  549.         CoUninitialize();
  550.         return FALSE;
  551.     }
  552.  
  553.     if(SUCCEEDED(g_pApp->Init()))
  554.     {
  555.         while (GetMessage(&msg, NULL, 0, 0))  // loop until the application quits
  556.         {
  557.             DispatchMessage(&msg);
  558.         }
  559.     }
  560.  
  561.     g_pApp->CleanUp();
  562.     delete g_pApp;
  563.  
  564.     UnInitASFormsManager(); // uninitialize the forms manager
  565.     CoUninitialize();       // uninitialize COM
  566.         
  567.     return TRUE; 
  568. }
  569.  
  570.  
  571.