home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC / src / appui2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  6.8 KB  |  259 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_INIT_SEG
  14. #pragma code_seg(AFX_INIT_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // DDE and ShellExecute support
  24.  
  25. BOOL CWinApp::ProcessShellCommand(CCommandLineInfo& rCmdInfo)
  26. {
  27.     BOOL bResult = TRUE;
  28.     switch (rCmdInfo.m_nShellCommand)
  29.     {
  30.     case CCommandLineInfo::FileNew:
  31.         if (!AfxGetApp()->OnCmdMsg(ID_FILE_NEW, 0, NULL, NULL))
  32.             OnFileNew();
  33.         if (m_pMainWnd == NULL)
  34.             bResult = FALSE;
  35.         break;
  36.  
  37.         // If we've been asked to open a file, call OpenDocumentFile()
  38.  
  39.     case CCommandLineInfo::FileOpen:
  40.         if (!OpenDocumentFile(rCmdInfo.m_strFileName))
  41.             bResult = FALSE;
  42.         break;
  43.  
  44. #if !defined(_WIN32_WCE_NO_PRINTING)
  45.         // If the user wanted to print, hide our main window and
  46.         // fire a message to ourselves to start the printing
  47.  
  48.     case CCommandLineInfo::FilePrintTo:
  49.     case CCommandLineInfo::FilePrint:
  50.         m_nCmdShow = SW_HIDE;
  51.         ASSERT(m_pCmdInfo == NULL);
  52.         OpenDocumentFile(rCmdInfo.m_strFileName);
  53.         m_pCmdInfo = &rCmdInfo;
  54.         m_pMainWnd->SendMessage(WM_COMMAND, ID_FILE_PRINT_DIRECT);
  55.         m_pCmdInfo = NULL;
  56.         bResult = FALSE;
  57.         break;
  58. #endif // _WIN32_WCE_NO_PRINTING
  59.  
  60. #if !defined(_WIN32_WCE_NO_OLE)
  61.         // If we're doing DDE, hide ourselves
  62.  
  63.     case CCommandLineInfo::FileDDE:
  64.         m_pCmdInfo = (CCommandLineInfo*)m_nCmdShow;
  65.         m_nCmdShow = SW_HIDE;
  66.         break;
  67. #endif // _WIN32_WCE_NO_OLE
  68.  
  69.     // If we've been asked to unregister, unregister and then terminate
  70.     case CCommandLineInfo::AppUnregister:
  71.         {
  72. #if !defined(_WIN32_WCE)
  73.             UnregisterShellFileTypes();
  74. #endif // _WIN32_WCE
  75.             BOOL bUnregistered = Unregister();
  76.  
  77.             // if you specify /EMBEDDED, we won't make an success/failure box
  78.             // this use of /EMBEDDED is not related to OLE
  79.  
  80. #if !defined(_WIN32_WCE_NO_OLE)  
  81.             if (!rCmdInfo.m_bRunEmbedded)
  82. #endif // _WIN32_WCE_NO_OLE
  83.             {
  84.                 if (bUnregistered)
  85.                     AfxMessageBox(AFX_IDP_UNREG_DONE);
  86.                 else
  87.                     AfxMessageBox(AFX_IDP_UNREG_FAILURE);
  88.             }
  89.             bResult = FALSE;    // that's all we do
  90.  
  91.             // If nobody is using it already, we can use it.
  92.             // We'll flag that we're unregistering and not save our state
  93.             // on the way out. This new object gets deleted by the
  94.             // app object destructor.
  95.  
  96.             if (m_pCmdInfo == NULL)
  97.             {
  98.                 m_pCmdInfo = new CCommandLineInfo;
  99.                 m_pCmdInfo->m_nShellCommand = CCommandLineInfo::AppUnregister;
  100.             }
  101.         }
  102.         break;
  103.     }
  104.     return bResult;
  105. }
  106.  
  107.  
  108. BOOL CWinApp::Unregister()
  109. {
  110.     HKEY    hKey = 0;
  111.     TCHAR   szBuf[MAX_PATH+1];
  112.     LONG    cSize;
  113.     BOOL    bRet = TRUE;
  114.  
  115.     POSITION pos = GetFirstDocTemplatePosition();
  116.     while (pos != NULL)
  117.     {
  118.         CDocTemplate* pTempl = GetNextDocTemplate(pos);
  119.         if (pTempl != NULL)
  120.             pTempl->OnCmdMsg(0, CN_OLE_UNREGISTER, NULL, NULL);
  121.     }
  122.  
  123.     // Remove profile information -- the registry entries exist if
  124.     // SetRegistryKey() was used.
  125.  
  126.     if (m_pszRegistryKey)
  127.     {
  128.         ASSERT(m_pszProfileName);
  129.  
  130.         CString strKey = _T("Software\\");
  131.         strKey += m_pszRegistryKey;
  132.         CString strSubKey = strKey + _T("\\") + m_pszProfileName;
  133.  
  134.         DelRegTree(HKEY_CURRENT_USER, strSubKey);
  135.  
  136.         // If registry key is empty then remove it
  137.  
  138.         DWORD   dwResult;
  139.         if ((dwResult = ::WCE_FCTN(RegOpenKey)(HKEY_CURRENT_USER, strKey, &hKey)) == 
  140.             ERROR_SUCCESS)
  141.         {
  142.             if (::WCE_FCTN(RegEnumKey)(hKey, 0, szBuf, _MAX_PATH) == ERROR_NO_MORE_ITEMS)
  143.                 DelRegTree(HKEY_CURRENT_USER, strKey);
  144.             ::RegCloseKey(hKey);
  145.         }
  146.         if (WCE_FCTN(RegQueryValue)(HKEY_CURRENT_USER, strSubKey, szBuf, &cSize) == ERROR_SUCCESS)
  147.             bRet = TRUE;
  148.     }
  149.     return bRet;
  150. }
  151.  
  152. // Under Win32, a reg key may not be deleted unless it is empty.
  153. // Thus, to delete a tree,  one must recursively enumerate and
  154. // delete all of the sub-keys.
  155.  
  156. LONG CWinApp::DelRegTree(HKEY hParentKey, const CString& strKeyName)
  157. {
  158.     return AfxDelRegTreeHelper(hParentKey, strKeyName);
  159. }
  160.  
  161. LONG AFXAPI AfxDelRegTreeHelper(HKEY hParentKey, const CString& strKeyName)
  162. {
  163.     TCHAR   szSubKeyName[256];
  164.     HKEY    hCurrentKey;
  165.     DWORD   dwResult;
  166.  
  167.     if ((dwResult = WCE_FCTN(RegOpenKey)(hParentKey, strKeyName, &hCurrentKey)) ==
  168.         ERROR_SUCCESS)
  169.     {
  170.         // Remove all subkeys of the key to delete
  171.         while ((dwResult = WCE_FCTN(RegEnumKey)(hCurrentKey, 0, szSubKeyName, 255)) ==
  172.                 ERROR_SUCCESS)
  173.         {
  174.             if ((dwResult = AfxDelRegTreeHelper(hCurrentKey, szSubKeyName)) != ERROR_SUCCESS)
  175.                 break;
  176.         }
  177.  
  178.         // If all went well, we should now be able to delete the requested key
  179.         if ((dwResult == ERROR_NO_MORE_ITEMS) || (dwResult == ERROR_BADKEY))
  180.         {
  181. #if defined(_WIN32_WCE)
  182.             // WinCE: The registry key must be closed before deleting.
  183.             RegCloseKey(hCurrentKey);
  184. #endif // _WIN32_WCE
  185.             dwResult = RegDeleteKey(hParentKey, strKeyName);
  186.         }
  187.     }
  188.  
  189.     RegCloseKey(hCurrentKey);
  190.     return dwResult;
  191. }
  192.  
  193. #if !defined(_WIN32_WCE)
  194. void CWinApp::EnableShellOpen()
  195. {
  196.     ASSERT(m_atomApp == NULL && m_atomSystemTopic == NULL); // do once
  197.  
  198.     m_atomApp = ::GlobalAddAtom(m_pszExeName);
  199.     m_atomSystemTopic = ::GlobalAddAtom(_T("system"));
  200. }
  201.  
  202. void CWinApp::RegisterShellFileTypes(BOOL bCompat)
  203. {
  204.     ASSERT(m_pDocManager != NULL);
  205.     m_pDocManager->RegisterShellFileTypes(bCompat);
  206. }
  207.  
  208. void CWinApp::RegisterShellFileTypesCompat()
  209. {
  210.     ASSERT(m_pDocManager != NULL);
  211.     m_pDocManager->RegisterShellFileTypes(TRUE);
  212. }
  213.  
  214. void CWinApp::UnregisterShellFileTypes()
  215. {
  216.     ASSERT(m_pDocManager != NULL);
  217.     m_pDocManager->UnregisterShellFileTypes();
  218. }
  219. #endif // _WIN32_WCE
  220.  
  221. #ifdef AFX_CORE2_SEG
  222. #pragma code_seg(AFX_CORE2_SEG)
  223. #endif
  224.  
  225. int CWinApp::GetOpenDocumentCount()
  226. {
  227.     ASSERT(m_pDocManager != NULL);
  228.     return m_pDocManager->GetOpenDocumentCount();
  229. }
  230.  
  231. /////////////////////////////////////////////////////////////////////////////
  232. // Doc template support
  233.  
  234. #ifdef AFX_CORE3_SEG
  235. #pragma code_seg(AFX_CORE3_SEG)
  236. #endif
  237.  
  238. void CWinApp::AddDocTemplate(CDocTemplate* pTemplate)
  239. {
  240.     if (m_pDocManager == NULL)
  241.         m_pDocManager = new CDocManager;
  242.     m_pDocManager->AddDocTemplate(pTemplate);
  243. }
  244.  
  245. POSITION CWinApp::GetFirstDocTemplatePosition() const
  246. {
  247.     if (m_pDocManager == NULL)
  248.         return NULL;
  249.     return m_pDocManager->GetFirstDocTemplatePosition();
  250. }
  251.  
  252. CDocTemplate* CWinApp::GetNextDocTemplate(POSITION& rPosition) const
  253. {
  254.     ASSERT(m_pDocManager != NULL);
  255.     return m_pDocManager->GetNextDocTemplate(rPosition);
  256. }
  257.  
  258. /////////////////////////////////////////////////////////////////////////////
  259.