home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / OLETSVR.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-04  |  5.1 KB  |  182 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_OLE3_SEG
  14. #pragma code_seg(AFX_OLE3_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. /////////////////////////////////////////////////////////////////////////////
  25. // COleTemplateServer
  26.  
  27. COleTemplateServer::COleTemplateServer()
  28.     : COleObjectFactory(CLSID_NULL, NULL, FALSE, NULL)
  29. {
  30.     m_pDocTemplate = NULL;
  31. }
  32.  
  33. BOOL COleTemplateServer::Register()
  34. {
  35.     return COleObjectFactory::Register();
  36. }
  37.  
  38. BOOL COleTemplateServer::OnCmdMsg(UINT nID, int nCode, void* pExtra,
  39.     AFX_CMDHANDLERINFO* pHandlerInfo)
  40. {
  41.     BOOL bReturn;
  42.     if (nCode == CN_OLE_UNREGISTER)
  43.         bReturn = Unregister();
  44.     else
  45.         bReturn = COleObjectFactory::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
  46.  
  47.     return bReturn;
  48. }
  49.  
  50. BOOL COleTemplateServer::Unregister()
  51. {
  52.     BOOL bReturn = COleObjectFactory::Unregister();
  53.     if (!bReturn)
  54.         return FALSE;
  55.  
  56.     // get registration info from doc template string
  57.     CString strServerName;
  58.     CString strLocalServerName;
  59.     CString strLocalShortName;
  60.  
  61.     if (!m_pDocTemplate->GetDocString(strServerName,
  62.        CDocTemplate::regFileTypeId) || strServerName.IsEmpty())
  63.     {
  64.         TRACE0("Error: not enough information in DocTemplate to unregister OLE server.\n");
  65.         return FALSE;
  66.     }
  67.     if (!m_pDocTemplate->GetDocString(strLocalServerName,
  68.        CDocTemplate::regFileTypeName))
  69.         strLocalServerName = strServerName;     // use non-localized name
  70.     if (!m_pDocTemplate->GetDocString(strLocalShortName,
  71.         CDocTemplate::fileNewName))
  72.         strLocalShortName = strLocalServerName; // use long name
  73.  
  74.     ASSERT(strServerName.Find(' ') == -1);  // no spaces allowed
  75.  
  76.     // place entries in system registry
  77.     if (!AfxOleUnregisterServerClass(m_clsid, strServerName,
  78.         strLocalShortName, strLocalServerName, (OLE_APPTYPE) m_bOAT))
  79.     {
  80.         bReturn = FALSE;
  81.     }
  82.  
  83.     return bReturn;
  84. }
  85.  
  86. void COleTemplateServer::ConnectTemplate(
  87.     REFCLSID clsid, CDocTemplate* pDocTemplate, BOOL bMultiInstance)
  88. {
  89.     ASSERT_VALID(this);
  90.     ASSERT_VALID(pDocTemplate);
  91.     ASSERT(pDocTemplate->m_pAttachedFactory == NULL);
  92.  
  93.     // setup initial state of underlying COleObjectFactory
  94.     m_clsid = clsid;
  95.     ASSERT(m_pRuntimeClass == NULL);
  96.     m_bMultiInstance = bMultiInstance;
  97.  
  98.     // attach the doc template to the factory
  99.     m_pDocTemplate = pDocTemplate;
  100.     m_pDocTemplate->m_pAttachedFactory = this;
  101. }
  102.  
  103. void COleTemplateServer::UpdateRegistry(OLE_APPTYPE nAppType,
  104.     LPCTSTR* rglpszRegister, LPCTSTR* rglpszOverwrite)
  105. {
  106.     ASSERT(m_pDocTemplate != NULL);
  107.     ASSERT(m_bOAT == (BYTE) OAT_UNKNOWN);
  108.     m_bOAT = (BYTE) nAppType;
  109.  
  110.     // get registration info from doc template string
  111.     CString strServerName;
  112.     CString strLocalServerName;
  113.     CString strLocalShortName;
  114.     CString strLocalFilterName;
  115.  
  116.     if (!m_pDocTemplate->GetDocString(strServerName,
  117.        CDocTemplate::regFileTypeId) || strServerName.IsEmpty())
  118.     {
  119.         TRACE0("Error: not enough information in DocTemplate to register OLE server.\n");
  120.         return;
  121.     }
  122.     if (!m_pDocTemplate->GetDocString(strLocalServerName,
  123.        CDocTemplate::regFileTypeName))
  124.         strLocalServerName = strServerName;     // use non-localized name
  125.     if (!m_pDocTemplate->GetDocString(strLocalShortName,
  126.         CDocTemplate::fileNewName))
  127.         strLocalShortName = strLocalServerName; // use long name
  128.     if (!m_pDocTemplate->GetDocString(strLocalFilterName,
  129.         CDocTemplate::filterName))
  130.         ASSERT(nAppType != OAT_DOC_OBJECT_SERVER);
  131.  
  132.     ASSERT(strServerName.Find(' ') == -1);  // no spaces allowed
  133.  
  134.     int nIconIndex = 0;
  135.     POSITION pos = AfxGetApp()->GetFirstDocTemplatePosition();
  136.     for (int nIndex = 1; pos != NULL; nIndex++)
  137.     {
  138.         CDocTemplate* pTemplate = AfxGetApp()->GetNextDocTemplate(pos);
  139.         if (pTemplate == m_pDocTemplate)
  140.         {
  141.             nIconIndex = nIndex;
  142.             pos = NULL; // set exit condition
  143.         }
  144.     }
  145.  
  146.     // place entries in system registry
  147.     if (!AfxOleRegisterServerClass(m_clsid, strServerName,
  148.         strLocalShortName, strLocalServerName, nAppType,
  149.         rglpszRegister, rglpszOverwrite, nIconIndex, strLocalFilterName))
  150.     {
  151.         // not fatal (don't fail just warn)
  152.         AfxMessageBox(AFX_IDP_FAILED_TO_AUTO_REGISTER);
  153.     }
  154. }
  155.  
  156. CCmdTarget* COleTemplateServer::OnCreateObject()
  157. {
  158.     ASSERT_VALID(this);
  159.     ASSERT_VALID(m_pDocTemplate);
  160.  
  161.     // save application user control status
  162.     BOOL bUserCtrl = AfxOleGetUserCtrl();
  163.  
  164.     // create invisible doc/view/frame set
  165.     CDocument* pDoc = m_pDocTemplate->OpenDocumentFile(NULL, FALSE);
  166.  
  167.     // restore application's user control status
  168.     AfxOleSetUserCtrl(bUserCtrl);
  169.  
  170.     if (pDoc != NULL)
  171.     {
  172.         ASSERT_VALID(pDoc);
  173.         ASSERT_KINDOF(CDocument, pDoc);
  174.  
  175.         // all new documents created by OLE start out modified
  176.         pDoc->SetModifiedFlag();
  177.     }
  178.     return pDoc;
  179. }
  180.  
  181. /////////////////////////////////////////////////////////////////////////////
  182.