home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / OLEINIT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-11  |  6.7 KB  |  262 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. #define new DEBUG_NEW
  23.  
  24. #ifdef _MAC
  25. AEEventHandlerUPP _afxPfnOleAuto;
  26. #endif
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // OLE OLE_DATA init structure
  30.  
  31. OLE_DATA _oleData;
  32.  
  33. OLE_DATA::OLE_DATA()
  34. {
  35.     // OLE 1.0 Clipboard formats
  36.     cfNative = ::RegisterClipboardFormat(_T("Native"));
  37.     cfOwnerLink = ::RegisterClipboardFormat(_T("OwnerLink"));
  38.     cfObjectLink = ::RegisterClipboardFormat(_T("ObjectLink"));
  39.  
  40.     // OLE 2.0 Clipboard formats
  41.     cfEmbeddedObject = ::RegisterClipboardFormat(_T("Embedded Object"));
  42.     cfEmbedSource = ::RegisterClipboardFormat(_T("Embed Source"));
  43.     cfLinkSource = ::RegisterClipboardFormat(_T("Link Source"));
  44.     cfObjectDescriptor = ::RegisterClipboardFormat(_T("Object Descriptor"));
  45.     cfLinkSourceDescriptor = ::RegisterClipboardFormat(_T("Link Source Descriptor"));
  46.     cfFileName = ::RegisterClipboardFormat(_T("FileName"));
  47.     cfFileNameW = ::RegisterClipboardFormat(_T("FileNameW"));
  48.     cfRichTextFormat = ::RegisterClipboardFormat(_T("Rich Text Format"));
  49.     cfRichTextAndObjects = ::RegisterClipboardFormat(_T("RichEdit Text and Objects"));
  50. }
  51.  
  52. /////////////////////////////////////////////////////////////////////////////
  53. // OLE initialization & termination
  54.  
  55. BOOL AFXAPI AfxOleInit()
  56. {
  57.     _AFX_THREAD_STATE* pState = AfxGetThreadState();
  58.     ASSERT(!pState->m_bNeedTerm);    // calling it twice?
  59.  
  60.     // Special case DLL context to assume that the calling app initializes OLE.
  61.     // For DLLs where this is not the case, those DLLs will need to initialize
  62.     // OLE for themselves via OleInitialize.  This is done since MFC cannot provide
  63.     // automatic uninitialize for DLLs because it is not valid to shutdown OLE
  64.     // during a DLL_PROCESS_DETACH.
  65.     if (afxContextIsDLL)
  66.     {
  67.         pState->m_bNeedTerm = -1;  // -1 is a special flag
  68.         return TRUE;
  69.     }
  70.  
  71.     // first, initialize OLE
  72.     SCODE sc = ::OleInitialize(NULL);
  73.     if (FAILED(sc))
  74.     {
  75.         // warn about non-NULL success codes
  76.         TRACE1("Warning: OleInitialize returned scode = %s.\n",
  77.             AfxGetFullScodeString(sc));
  78.         goto InitFailed;
  79.     }
  80.     // termination required when OleInitialize does not fail
  81.     pState->m_bNeedTerm = TRUE;
  82.  
  83.     // hook idle time and exit time for required OLE cleanup
  84.     CWinThread* pThread; pThread = AfxGetThread();
  85.     pThread->m_lpfnOleTermOrFreeLib = AfxOleTermOrFreeLib;
  86.  
  87.     // allocate and initialize default message filter
  88.     if (pThread->m_pMessageFilter == NULL)
  89.     {
  90.         pThread->m_pMessageFilter = new COleMessageFilter;
  91.         ASSERT(AfxOleGetMessageFilter() != NULL);
  92.         AfxOleGetMessageFilter()->Register();
  93.     }
  94.  
  95. #ifdef _MAC
  96.     CWinApp* pApp; pApp = AfxGetApp();
  97. #ifndef _WINDLL
  98.     // Mac MFC uses a static version of ole2ui which must be initialized
  99.     if (pState->m_bNeedTerm && !::OleUIInitialize(pApp->m_hInstance,
  100.             pApp->m_hPrevInstance, SZCLASSICONBOX, SZCLASSRESULTIMAGE))
  101.         goto InitFailed;
  102. #endif
  103.  
  104.     _afxPfnOleAuto = NewAEEventHandlerProc(_AfxOleAutoHandler);
  105.     if (_afxPfnOleAuto != NULL)
  106.     {
  107.         AEInstallEventHandler('OLE2', 'AUTO', _afxPfnOleAuto, (long) pApp, false);
  108.     }
  109. #endif
  110.  
  111.     return TRUE;
  112.  
  113. InitFailed:
  114.     AfxOleTerm();
  115.     return FALSE;
  116. }
  117.  
  118. void AFXAPI AfxOleTerm(BOOL bJustRevoke)
  119. {
  120.     // release clipboard ownership
  121.     COleDataSource::FlushClipboard();
  122.  
  123.     // revoke all class factories
  124.     COleObjectFactory::RevokeAll();
  125.  
  126. #ifndef _AFX_NO_OCC_SUPPORT
  127.     AfxOleUnlockAllControls();
  128. #endif
  129.  
  130.     if (!bJustRevoke)
  131.     {
  132.         CWinThread* pThread = AfxGetThread();
  133.         if (pThread != NULL)
  134.         {
  135.             // destroy message filter (may be derived class)
  136.             delete pThread->m_pMessageFilter;
  137.             pThread->m_pMessageFilter = NULL;
  138.         }
  139.  
  140.         // terminate OLE last
  141.         _AFX_THREAD_STATE* pState = AfxGetThreadState();
  142.         // -1 is special case, so need to compare against TRUE
  143.         if (pState->m_bNeedTerm == TRUE)
  144.         {
  145.             CoFreeUnusedLibraries();
  146. #if defined(_MAC) && !defined (_WINDLL)
  147.             ::OleUIUnInitialize();
  148. #endif
  149.             ::OleUninitialize();
  150.             pState->m_bNeedTerm = FALSE;
  151.         }
  152. #ifdef _MAC
  153.         AERemoveEventHandler('OLE2', 'AUTO', _afxPfnOleAuto, false);
  154. #endif
  155.     }
  156. }
  157.  
  158. void AFXAPI AfxOleTermOrFreeLib(BOOL bTerm, BOOL bJustRevoke)
  159. {
  160.     if (bTerm)
  161.     {
  162.         AfxOleTerm(bJustRevoke);
  163.     }
  164.     else
  165.     {
  166.         // only call CoFreeUnusedLibraries if one minute has gone by
  167.         static DWORD lTickCount = GetTickCount();
  168.         if (GetTickCount() - lTickCount > 60000)
  169.         {
  170.             CoFreeUnusedLibraries();
  171.             lTickCount = GetTickCount();
  172.         }
  173.     }
  174. }
  175.  
  176. /////////////////////////////////////////////////////////////////////////////
  177. // CWinApp support for parsing OLE command line
  178.  
  179. static BOOL ParseOption(LPTSTR lpszCmdLine, LPCTSTR lpszOption)
  180. {
  181.     int nLen = lstrlen(lpszOption);
  182.     while (*lpszCmdLine != 0)
  183.     {
  184.         if ((*lpszCmdLine == '-' || *lpszCmdLine == '/') &&
  185.             _tcsncmp(lpszOption, lpszCmdLine+1, nLen) == 0)
  186.         {
  187.             // remove the option from the command line
  188.             int nCmdLen = lstrlen(lpszCmdLine);
  189.             memmove(lpszCmdLine, lpszCmdLine + nLen + 1,
  190.                 (nCmdLen - nLen) * sizeof(TCHAR));
  191.             return TRUE;
  192.         }
  193.         lpszCmdLine++;
  194.     }
  195.     return FALSE;
  196. }
  197.  
  198. BOOL CWinApp::RunEmbedded()
  199. {
  200.     ASSERT(m_lpCmdLine != NULL);
  201.  
  202.     // hard coded non-localized name
  203.     if (ParseOption(m_lpCmdLine, _T("Embedding")))
  204.     {
  205.         AfxOleSetUserCtrl(FALSE);
  206.         return TRUE;
  207.     }
  208.     return FALSE;   // not run with /Embedding
  209. }
  210.  
  211. BOOL CWinApp::RunAutomated()
  212. {
  213.     ASSERT(m_lpCmdLine != NULL);
  214.  
  215.     // hard coded non-localized name
  216.     if (ParseOption(m_lpCmdLine, _T("Automation")))
  217.     {
  218.         AfxOleSetUserCtrl(FALSE);
  219.         return TRUE;
  220.     }
  221.     return FALSE;   // not run with /Automation
  222. }
  223.  
  224. #ifdef _MAC
  225. /////////////////////////////////////////////////////////////////////////////
  226. // OLE Auto AppleEvent handler
  227.  
  228. OSErr PASCAL _AfxOleAutoHandler(AppleEvent* pae, AppleEvent*, long lRefcon)
  229. {
  230.     CWinApp* pApp;
  231.     OSErr    err;
  232.     DescType dtT;
  233.     Size     lT;
  234.  
  235.     err = AEGetAttributePtr(pae, keyMissedKeywordAttr, typeWildCard,
  236.             &dtT, NULL, 0, &lT);
  237.  
  238.     if (err == errAEDescNotFound)
  239.     {
  240.         pApp = (CWinApp*) lRefcon;
  241.         ASSERT_VALID(pApp);
  242.         ASSERT_KINDOF(CWinApp, pApp);
  243.  
  244.         if(COleObjectFactory::RegisterAll())
  245.         {
  246.             return noErr;
  247.         }
  248.         else
  249.         {
  250.             return errAEEventNotHandled;
  251.         }
  252.     }
  253.     else if (err == noErr)
  254.         return errAEEventNotHandled;
  255.     else
  256.         return err;
  257.  
  258. }
  259. #endif
  260.  
  261. /////////////////////////////////////////////////////////////////////////////
  262.