home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / APPUI2.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-03  |  6.8 KB  |  271 lines

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