home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / oletsvr.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  5KB  |  187 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_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.     CString strLocalFilterExt;
  116.  
  117.     if (!m_pDocTemplate->GetDocString(strServerName,
  118.        CDocTemplate::regFileTypeId) || strServerName.IsEmpty())
  119.     {
  120.         TRACE0("Error: not enough information in DocTemplate to register OLE server.\n");
  121.         return;
  122.     }
  123.     if (!m_pDocTemplate->GetDocString(strLocalServerName,
  124.        CDocTemplate::regFileTypeName))
  125.         strLocalServerName = strServerName;     // use non-localized name
  126.     if (!m_pDocTemplate->GetDocString(strLocalShortName,
  127.         CDocTemplate::fileNewName))
  128.         strLocalShortName = strLocalServerName; // use long name
  129.     if (!m_pDocTemplate->GetDocString(strLocalFilterName,
  130.         CDocTemplate::filterName))
  131.         ASSERT(nAppType != OAT_DOC_OBJECT_SERVER);
  132.     if (!m_pDocTemplate->GetDocString(strLocalFilterExt,
  133.         CDocTemplate::filterExt))
  134.         ASSERT(nAppType != OAT_DOC_OBJECT_SERVER);
  135.  
  136.     ASSERT(strServerName.Find(' ') == -1);  // no spaces allowed
  137.  
  138.     int nIconIndex = 0;
  139.     POSITION pos = AfxGetApp()->GetFirstDocTemplatePosition();
  140.     for (int nIndex = 1; pos != NULL; nIndex++)
  141.     {
  142.         CDocTemplate* pTemplate = AfxGetApp()->GetNextDocTemplate(pos);
  143.         if (pTemplate == m_pDocTemplate)
  144.         {
  145.             nIconIndex = nIndex;
  146.             pos = NULL; // set exit condition
  147.         }
  148.     }
  149.  
  150.     // place entries in system registry
  151.     if (!AfxOleRegisterServerClass(m_clsid, strServerName,
  152.         strLocalShortName, strLocalServerName, nAppType,
  153.         rglpszRegister, rglpszOverwrite, nIconIndex,
  154.         strLocalFilterName, strLocalFilterExt))
  155.     {
  156.         // not fatal (don't fail just warn)
  157.         AfxMessageBox(AFX_IDP_FAILED_TO_AUTO_REGISTER);
  158.     }
  159. }
  160.  
  161. CCmdTarget* COleTemplateServer::OnCreateObject()
  162. {
  163.     ASSERT_VALID(this);
  164.     ASSERT_VALID(m_pDocTemplate);
  165.  
  166.     // save application user control status
  167.     BOOL bUserCtrl = AfxOleGetUserCtrl();
  168.  
  169.     // create invisible doc/view/frame set
  170.     CDocument* pDoc = m_pDocTemplate->OpenDocumentFile(NULL, FALSE);
  171.  
  172.     // restore application's user control status
  173.     AfxOleSetUserCtrl(bUserCtrl);
  174.  
  175.     if (pDoc != NULL)
  176.     {
  177.         ASSERT_VALID(pDoc);
  178.         ASSERT_KINDOF(CDocument, pDoc);
  179.  
  180.         // all new documents created by OLE start out modified
  181.         pDoc->SetModifiedFlag();
  182.     }
  183.     return pDoc;
  184. }
  185.  
  186. /////////////////////////////////////////////////////////////////////////////
  187.