home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / ole / tstcon / tcdoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  36.0 KB  |  1,507 lines

  1. // CTestContainer98Doc.cpp : implementation of the CTestContainer98Doc class
  2. //
  3.  
  4. #include "StdAfx.H"
  5. #include "TestCon.H"
  6.  
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12.  
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CTestContainer98Doc
  15.  
  16. IMPLEMENT_DYNCREATE( CTestContainer98Doc, COleDocument )
  17.  
  18. BEGIN_MESSAGE_MAP(CTestContainer98Doc, COleDocument)
  19.     //{{AFX_MSG_MAP(CTestContainer98Doc)
  20.     ON_COMMAND(ID_OPTIONS_FREEZEEVENTS, OnOptionsFreezeEvents)
  21.     ON_UPDATE_COMMAND_UI(ID_OPTIONS_FREEZEEVENTS, OnUpdateOptionsFreezeEvents)
  22.     ON_COMMAND(ID_OPTIONS_SLOWDRAWING, OnOptionsSlowDrawing)
  23.     ON_UPDATE_COMMAND_UI(ID_OPTIONS_SLOWDRAWING, OnUpdateOptionsSlowDrawing)
  24.     ON_COMMAND(ID_OPTIONS_TWOPASSDRAWING, OnOptionsTwoPassDrawing)
  25.     ON_UPDATE_COMMAND_UI(ID_OPTIONS_TWOPASSDRAWING, OnUpdateOptionsTwoPassDrawing)
  26.     ON_COMMAND(ID_OPTIONS_LOGGING, OnOptionsLogging)
  27.     ON_COMMAND(ID_CONTAINER_OPTIONS, OnContainerOptions)
  28.     ON_COMMAND(ID_CONTAINER_AMBIENTPROPERTIES, OnContainerAmbientProperties)
  29.     ON_COMMAND(ID_CONTAINER_INFO, OnContainerInfo)
  30.     ON_COMMAND(ID_EDIT_DELETEALL, OnEditDeleteAll)
  31.     ON_UPDATE_COMMAND_UI(ID_EDIT_DELETEALL, OnUpdateEditDeleteAll)
  32.     ON_COMMAND(ID_CONTAINER_TABORDER, OnContainerTabOrder)
  33.     ON_COMMAND(ID_TOOLS_MACROS, OnToolsMacros)
  34.     ON_COMMAND(ID_RUNMACRO, OnRunMacro)
  35.     //}}AFX_MSG_MAP
  36.     // Enable default OLE container implementation
  37.     ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, COleDocument::OnUpdatePasteMenu)
  38.     ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE_LINK, COleDocument::OnUpdatePasteLinkMenu)
  39.     ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_CONVERT, COleDocument::OnUpdateObjectVerbMenu)
  40.     ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_LINKS, COleDocument::OnUpdateEditLinksMenu)
  41.     ON_COMMAND(ID_OLE_EDIT_LINKS, COleDocument::OnEditLinks)
  42.    ON_UPDATE_COMMAND_UI_RANGE( ID_OLE_VERB_FIRST, ID_OLE_VERB_LAST, COleDocument::OnUpdateObjectVerbMenu )
  43. END_MESSAGE_MAP()
  44.  
  45. BEGIN_CONNECTION_MAP( CTestContainer98Doc, COleDocument )
  46.    CONNECTION_PART( CTestContainer98Doc, __uuidof( ITestContainer98Events ), TC98CP )
  47. END_CONNECTION_MAP()
  48.  
  49. BEGIN_DISPATCH_MAP( CTestContainer98Doc, COleDocument )
  50.     //{{AFX_DISPATCH_MAP(CTestContainer98Doc)
  51.     DISP_PROPERTY_EX(CTestContainer98Doc, "PrimarySelection", GetPrimarySelection, SetPrimarySelection, VT_DISPATCH)
  52.     DISP_FUNCTION(CTestContainer98Doc, "FindControl", FindControl, VT_DISPATCH, VTS_BSTR)
  53.     DISP_FUNCTION(CTestContainer98Doc, "Log", Log, VT_EMPTY, VTS_BSTR)
  54.     DISP_FUNCTION(CTestContainer98Doc, "InsertControl", InsertControl, VT_DISPATCH, VTS_BSTR VTS_BSTR)
  55.     //}}AFX_DISPATCH_MAP
  56. END_DISPATCH_MAP()
  57.  
  58. BEGIN_INTERFACE_MAP( CTestContainer98Doc, COleDocument )
  59.     INTERFACE_PART( CTestContainer98Doc, __uuidof( ITestContainer98 ), Dispatch )
  60.    INTERFACE_PART( CTestContainer98Doc, IID_IConnectionPointContainer, ConnPtContainer )
  61. END_INTERFACE_MAP()
  62.  
  63. const IID LIBID_TestContainer98 = {0x198184FB,0xB837,0x11D0,{0x8D,0xF1,0x00,0xC0,0x4F,0xB6,0x8D,0x60}};
  64.  
  65. IMPLEMENT_OLETYPELIB( CTestContainer98Doc, LIBID_TestContainer98, 1, 0 )
  66.  
  67. /////////////////////////////////////////////////////////////////////////////
  68. // CTestContainer98Doc construction/destruction
  69.  
  70. CTestContainer98Doc::CTestContainer98Doc() :
  71.    m_pFontHolder( NULL ),
  72.    m_tUserMode( FALSE ),
  73.    m_tFreezeEvents( FALSE ),
  74.    m_tAllowWindowless( TRUE ),
  75.    m_tTwoPassDrawing( TRUE ),
  76.    m_tSlowDrawing( FALSE ),
  77.    m_tHonorIgnoreActivateWhenVisible( TRUE ),
  78.    m_tUseIPointerInactive( TRUE ),
  79.    m_tUseQuickActivation( TRUE ),
  80.    m_tIOleInPlaceSiteWindowless( TRUE ),
  81.    m_tIOleInPlaceSiteEx( TRUE ),
  82.    m_tIAdviseSinkEx( TRUE ),
  83.    m_tSBindHost( TRUE ),
  84.    m_pLog( NULL ),
  85.    m_pScriptManager( NULL )
  86. {
  87.    m_descFont.lpstrName = NULL;
  88.  
  89.     // Use OLE compound files
  90.     EnableCompoundFile();
  91.  
  92.    EnableConnections();
  93.  
  94.     EnableAutomation();
  95.  
  96.     AfxOleLockApp();
  97. }
  98.  
  99. CTestContainer98Doc::~CTestContainer98Doc()
  100. {
  101.    Cleanup();
  102.  
  103.     AfxOleUnlockApp();
  104. }
  105.  
  106. void CTestContainer98Doc::Cleanup()
  107. {
  108.    int iProp;
  109.  
  110.    if( m_pScriptManager != NULL )
  111.    {
  112.       delete m_pScriptManager;
  113.       m_pScriptManager = NULL;
  114.    }
  115.  
  116.    delete m_pLog;
  117.    m_pLog = NULL;
  118.  
  119.    for( iProp = 0; iProp < m_apAmbientProperties.GetSize(); iProp++ )
  120.    {
  121.       delete m_apAmbientProperties[iProp];
  122.    }
  123.    m_apAmbientProperties.SetSize( 0 );
  124.  
  125.    delete m_pFontHolder;
  126.    m_pFontHolder = NULL;
  127.  
  128.    delete[] m_descFont.lpstrName;
  129.    m_descFont.lpstrName = NULL;
  130. }
  131.  
  132. void CTestContainer98Doc::CreateUniqueItemName( CTestContainer98Item* pItem,
  133.    LPCTSTR pszBaseName, CString& strUniqueName )
  134. {
  135.    POSITION posItem;
  136.    CTestContainer98Item* pSearchItem;
  137.    ULONG nTag;
  138.    BOOL tCollisionFound;
  139.  
  140.    ASSERT( pszBaseName != NULL );
  141.  
  142.    // Assume the name is already unique
  143.    strUniqueName = pszBaseName;
  144.  
  145.    nTag = 1;
  146.    do
  147.    {
  148.       tCollisionFound = FALSE;
  149.       posItem = GetStartPosition();
  150.       while( (posItem != NULL) && !tCollisionFound )
  151.       {
  152.          pSearchItem = (CTestContainer98Item*)GetNextItem( posItem );
  153.          if( pSearchItem != pItem )
  154.          {
  155.             if( strUniqueName == pSearchItem->GetDisplayName() )
  156.             {
  157.                // This name is already in use.
  158.                tCollisionFound = TRUE;
  159.                strUniqueName.Format( "%s%u", pszBaseName, nTag );
  160.                nTag++;
  161.             }
  162.          }
  163.       }
  164.    } while( tCollisionFound );
  165. }
  166.  
  167. BOOL CTestContainer98Doc::Initialize()
  168. {
  169.    USES_CONVERSION;
  170.    CLSID clsid;
  171.    HRESULT hResult;
  172.    CMainFrame* pMainFrame;
  173.    CTestContainer98App* pApp;
  174.    CFileLog* pFileLog;
  175.    BOOL tSuccess;
  176.  
  177.    pApp = (CTestContainer98App*)AfxGetApp();
  178.  
  179.    m_tUserMode = pApp->m_options.m_tUserMode;
  180.    m_tFreezeEvents = FALSE;
  181.    m_tAllowWindowless = pApp->m_options.m_tAllowWindowless;
  182.    m_tTwoPassDrawing = pApp->m_options.m_tTwoPassDrawing;
  183.    m_tSlowDrawing = FALSE;
  184.    m_tHonorIgnoreActivateWhenVisible =
  185.       pApp->m_options.m_tHonorIgnoreActivateWhenVisible;
  186.    m_tUseIPointerInactive = pApp->m_options.m_tUseIPointerInactive;
  187.    m_tUseQuickActivation = pApp->m_options.m_tQuickActivation;
  188.    m_tIOleInPlaceSiteWindowless = pApp->m_options.m_tIOleInPlaceSiteWindowless;
  189.    m_tIOleInPlaceSiteEx = pApp->m_options.m_tIOleInPlaceSiteEx;
  190.    m_tIAdviseSinkEx = pApp->m_options.m_tIAdviseSinkEx;
  191.    m_tSBindHost = pApp->m_options.m_tSBindHost;
  192.    m_pLog = NULL;
  193.  
  194.    pMainFrame = DYNAMIC_DOWNCAST( CMainFrame, AfxGetMainWnd() );
  195.  
  196.    switch( pApp->m_options.m_iLogType )
  197.    {
  198.    case TCLOG_NULL:
  199.       m_pLog = new CNullLog;
  200.       break;
  201.  
  202.    case TCLOG_OUTPUTWINDOW:
  203.       m_pLog = new COutputWindowLog( GetOutputEditControl() );
  204.       break;
  205.  
  206.    case TCLOG_DEBUG:
  207.       m_pLog = new CDebugLog;
  208.       break;
  209.  
  210.    case TCLOG_FILE:
  211.       pFileLog = new CFileLog;
  212.       tSuccess = pFileLog->Create( pApp->m_options.m_strLogFileName );
  213.       if( !tSuccess )
  214.       {
  215.          delete pFileLog;
  216.       }
  217.       m_pLog = pFileLog;
  218.       break;
  219.  
  220.    default:
  221.       ASSERT( FALSE );
  222.       break;
  223.    }
  224.    if( m_pLog == NULL )
  225.    {
  226.       return( FALSE );
  227.    }
  228.  
  229.    hResult = InitDefaultAmbientProperties();
  230.    if( FAILED( hResult ) )
  231.    {
  232.       return( FALSE );
  233.    }
  234.  
  235.    if( !pApp->m_options.m_strProgID.IsEmpty() )
  236.    {
  237.       try
  238.       {
  239.          hResult = CLSIDFromProgID( T2COLE( pApp->m_options.m_strProgID ),
  240.             &clsid );
  241.          if( FAILED( hResult ) )
  242.          {
  243.             AfxThrowOleException( hResult );
  244.          }
  245.          AddControl( clsid, NULL );
  246.       }
  247.       catch( CException* pException )
  248.       {
  249.          pException->Delete();
  250.          TCTrace( TRACELEVEL_NORMAL, "Failed to load control.\n" );
  251.       }
  252.       pApp->m_options.m_strProgID.Empty();
  253.    }
  254.  
  255.     return( TRUE );
  256. }
  257.  
  258. BOOL CTestContainer98Doc::OnNewDocument()
  259. {
  260.     if( !COleDocument::OnNewDocument() )
  261.    {
  262.       return( FALSE );
  263.    }
  264.  
  265.    return( Initialize() );
  266. }
  267.  
  268.  
  269. /////////////////////////////////////////////////////////////////////////////
  270. // CTestContainer98Doc serialization
  271.  
  272. void CTestContainer98Doc::Serialize( CArchive& ar )
  273. {
  274.    POSITION posScript;
  275.    CScript* pScript;
  276.    int nScripts;
  277.    int iScript;
  278.    CString strScriptName;
  279.    CString strScriptPath;
  280.    CScriptManager* pScriptManager;
  281.    CString strErrorMessage;
  282.    HRESULT hResult;
  283.    int iProp;
  284.    CAmbientProperty* pUserMode;
  285.  
  286.    if( ar.IsLoading() )
  287.    {
  288.       Initialize();
  289.  
  290.       for( iProp = 0; iProp < m_apAmbientProperties.GetSize(); iProp++ )
  291.       {
  292.          delete m_apAmbientProperties[iProp];
  293.       }
  294.       m_apAmbientProperties.SetSize( 0 );
  295.  
  296.       m_apAmbientProperties.Serialize( ar );
  297.  
  298.       pUserMode = FindAmbientProperty( DISPID_AMBIENT_USERMODE );
  299.       if( pUserMode != NULL )
  300.       {
  301.          if( pUserMode->GetValue().vt == VT_BOOL )
  302.          {
  303.             m_tUserMode = pUserMode->GetValue().boolVal;
  304.          }
  305.       }
  306.    }
  307.    else
  308.    {
  309.       m_apAmbientProperties.Serialize( ar );
  310.    }
  311.  
  312.     // Calling the base class COleDocument enables serialization
  313.     //  of the container document's COleClientItem objects.
  314.     COleDocument::Serialize( ar );
  315.  
  316.    if( ar.IsStoring() )
  317.    {
  318.       pScriptManager = GetScriptManager( FALSE );
  319.       if( pScriptManager != NULL )
  320.       {
  321.          ar<<pScriptManager->GetNumScripts();
  322.  
  323.          posScript = pScriptManager->GetFirstScriptPosition();
  324.          while( posScript != NULL )
  325.          {
  326.             pScript = pScriptManager->GetNextScript( posScript );
  327.             ar<<pScript->GetName();
  328.          }
  329.       }
  330.       else
  331.       {
  332.          ar<<int( 0 );
  333.       }
  334.    }
  335.    else
  336.    {
  337.       ar>>nScripts;
  338.       if( nScripts > 0 )
  339.       {
  340.          pScriptManager = GetScriptManager();
  341.          if( pScriptManager != NULL )
  342.          {
  343.             for( iScript = 0; iScript < nScripts; iScript++ )
  344.             {
  345.                ar>>strScriptName;
  346.                strScriptPath = m_strDirectory+strScriptName+_T( ".dsm" );
  347.                hResult = pScriptManager->LoadScript( strScriptPath,
  348.                   strScriptName );
  349.                if( FAILED( hResult ) )
  350.                {
  351.                   _com_error error( hResult );
  352.  
  353.                   AfxFormatString2( strErrorMessage, IDS_ERRORLOADINGSCRIPT,
  354.                      strScriptName, error.ErrorMessage() );
  355.                   AfxMessageBox( strErrorMessage );
  356.                }
  357.             }
  358.          }
  359.       }
  360.    }
  361. }
  362.  
  363. /////////////////////////////////////////////////////////////////////////////
  364. // CTestContainer98Doc diagnostics
  365.  
  366. #ifdef _DEBUG
  367. void CTestContainer98Doc::AssertValid() const
  368. {
  369.     COleDocument::AssertValid();
  370. }
  371.  
  372. void CTestContainer98Doc::Dump( CDumpContext& dc ) const
  373. {
  374.     COleDocument::Dump(dc);
  375. }
  376. #endif //_DEBUG
  377.  
  378. /////////////////////////////////////////////////////////////////////////////
  379. // CTestContainer98Doc commands
  380.  
  381. void CTestContainer98Doc::BringToFront( CDocItem* pItem )
  382. {
  383.    POSITION posItem;
  384.  
  385.    ASSERT( pItem != NULL );
  386.  
  387.    posItem = m_docItemList.Find( pItem );
  388.    ASSERT( posItem != NULL );
  389.  
  390.    m_docItemList.RemoveAt( posItem );
  391.    m_docItemList.AddHead( pItem );
  392. }
  393.  
  394. void CTestContainer98Doc::SendToBack( CDocItem* pItem )
  395. {
  396.    POSITION posItem;
  397.  
  398.    ASSERT( pItem != NULL );
  399.  
  400.    posItem = m_docItemList.Find( pItem );
  401.    ASSERT( posItem != NULL );
  402.  
  403.    m_docItemList.RemoveAt( posItem );
  404.    m_docItemList.AddTail( pItem );
  405. }
  406.  
  407. HRESULT CTestContainer98Doc::InitDefaultAmbientProperties()
  408. {
  409.    CString strName;
  410.    LPCOLESTR pszFaceName;
  411.    CAmbientProperty* pProp;
  412.    VARIANT_BOOL tUserMode;
  413.  
  414.    if( m_tUserMode )
  415.    {
  416.       tUserMode = VAR_TRUE;
  417.    }
  418.    else
  419.    {
  420.       tUserMode = VAR_FALSE;
  421.    }
  422.  
  423.    strName.LoadString( IDS_APROPNAME_USERMODE );
  424.    pProp = new CAmbientProperty( DISPID_AMBIENT_USERMODE, strName, COleVariant(
  425.       tUserMode, VT_BOOL ), VTI_BOOL, TRUE );
  426.    m_apAmbientProperties.Add( pProp );
  427.  
  428.    strName.LoadString( IDS_APROPNAME_UIDEAD );
  429.    pProp = new CAmbientProperty( DISPID_AMBIENT_UIDEAD, strName, COleVariant(
  430.       VARIANT_FALSE, VT_BOOL ), VTI_BOOL, TRUE );
  431.    m_apAmbientProperties.Add( pProp );
  432.  
  433.    strName.LoadString( IDS_APROPNAME_SHOWHATCHING );
  434.    pProp = new CAmbientProperty( DISPID_AMBIENT_SHOWHATCHING, strName,
  435.       COleVariant( VARIANT_BOOL( -1 ), VT_BOOL ), VTI_BOOL, TRUE );
  436.    m_apAmbientProperties.Add( pProp );
  437.  
  438.    strName.LoadString( IDS_APROPNAME_SHOWGRABHANDLES );
  439.    pProp = new CAmbientProperty( DISPID_AMBIENT_SHOWGRABHANDLES, strName,
  440.       COleVariant( VARIANT_BOOL( -1 ), VT_BOOL ), VTI_BOOL, TRUE );
  441.    m_apAmbientProperties.Add( pProp );
  442.  
  443.    strName.LoadString( IDS_APROPNAME_TEXTALIGN );
  444.    pProp = new CAmbientProperty( DISPID_AMBIENT_TEXTALIGN, strName,
  445.       COleVariant( short( 0 ), VT_I2 ), VTI_I2, TRUE );
  446.    m_apAmbientProperties.Add( pProp );
  447.  
  448.    strName.LoadString( IDS_APROPNAME_BACKCOLOR );
  449.    pProp = new CAmbientProperty( DISPID_AMBIENT_BACKCOLOR, strName,
  450.       COleVariant( long( GetSysColor( COLOR_WINDOW ) ), VT_I4 ), VTI_COLOR,
  451.       TRUE );
  452.    m_apAmbientProperties.Add( pProp );
  453.  
  454.    delete m_pFontHolder;
  455.    m_pFontHolder = new CFontHolder( NULL );
  456.    if( m_pFontHolder == NULL )
  457.    {
  458.       AfxThrowMemoryException();
  459.    }
  460.    m_descFont.cbSizeofstruct = sizeof( m_descFont );
  461.    pszFaceName = L"MS Sans Serif";
  462.    delete[] m_descFont.lpstrName;
  463.    m_descFont.lpstrName = new OLECHAR[ocslen( pszFaceName )+1];
  464.    if( m_descFont.lpstrName == NULL )
  465.    {
  466.       AfxThrowMemoryException();
  467.    }
  468.    ocscpy( m_descFont.lpstrName, pszFaceName );
  469.    m_descFont.cySize.Lo = 80000L;
  470.    m_descFont.cySize.Hi = 0;
  471.    m_descFont.sWeight = FW_BOLD;
  472.    m_descFont.sCharset = 0;
  473.    m_descFont.fItalic = FALSE;
  474.    m_descFont.fUnderline = FALSE;
  475.    m_descFont.fStrikethrough = FALSE;
  476.    m_pFontHolder->InitializeFont( &m_descFont );
  477.  
  478.    COleVariant varFont;
  479.    varFont.vt = VT_DISPATCH;
  480.    varFont.pdispVal = m_pFontHolder->GetFontDispatch();
  481.  
  482.    strName.LoadString( IDS_APROPNAME_FONT );
  483.    pProp = new CAmbientProperty( DISPID_AMBIENT_FONT, strName, varFont,
  484.       VTI_FONT, TRUE );
  485.    m_apAmbientProperties.Add( pProp );
  486.  
  487.    strName.LoadString( IDS_APROPNAME_FORECOLOR );
  488.    pProp = new CAmbientProperty( DISPID_AMBIENT_FORECOLOR, strName,
  489.       COleVariant( long( GetSysColor( COLOR_WINDOWTEXT ) ), VT_I4 ), VTI_COLOR,
  490.       TRUE );
  491.    m_apAmbientProperties.Add( pProp );
  492.  
  493.    strName.LoadString( IDS_APROPNAME_SCALEUNITS );
  494.    pProp = new CAmbientProperty( DISPID_AMBIENT_SCALEUNITS, strName,
  495.       COleVariant( _T( "" ), VT_BSTR ), VTI_BSTR, TRUE );
  496.    m_apAmbientProperties.Add( pProp );
  497.  
  498.    strName.LoadString( IDS_APROPNAME_DISPLAYNAME );
  499.    pProp = new CAmbientProperty( DISPID_AMBIENT_DISPLAYNAME, strName,
  500.       COleVariant( _T( "" ), VT_BSTR ), VTI_BSTR, TRUE );
  501.    m_apAmbientProperties.Add( pProp );
  502.  
  503.    strName.LoadString( IDS_APROPNAME_LOCALEID );
  504.    pProp = new CAmbientProperty( DISPID_AMBIENT_LOCALEID, strName, COleVariant(
  505.       long( GetUserDefaultLCID() ), VT_I4 ), VTI_I4, TRUE );
  506.    m_apAmbientProperties.Add( pProp );
  507.  
  508.    strName.LoadString( IDS_APROPNAME_MESSAGEREFLECT );
  509.    pProp = new CAmbientProperty( DISPID_AMBIENT_MESSAGEREFLECT, strName,
  510.       COleVariant( VARIANT_FALSE, VT_BOOL ), VTI_BOOL, TRUE );
  511.    m_apAmbientProperties.Add( pProp );
  512.  
  513.    strName.LoadString( IDS_APROPNAME_SUPPORTSMNEMONICS );
  514.    pProp = new CAmbientProperty( DISPID_AMBIENT_SUPPORTSMNEMONICS, strName,
  515.       COleVariant( VARIANT_BOOL( -1 ), VT_BOOL ), VTI_BOOL, TRUE );
  516.    m_apAmbientProperties.Add( pProp );
  517.  
  518.    return( S_OK );
  519. }
  520.  
  521. BOOL CTestContainer98Doc::CanWindowlessActivate() const
  522. {
  523.    return( m_tAllowWindowless );
  524. }
  525.  
  526. BOOL CTestContainer98Doc::UseIPointerInactive() const
  527. {
  528.    return( m_tUseIPointerInactive );
  529. }
  530.  
  531. BOOL CTestContainer98Doc::UseQuickActivation() const
  532. {
  533.    return( m_tUseQuickActivation );
  534. }
  535.  
  536. BOOL CTestContainer98Doc::UseSlowDrawing() const
  537. {
  538.    return( m_tSlowDrawing );
  539. }
  540.  
  541. BOOL CTestContainer98Doc::UseTwoPassDrawing() const
  542. {
  543.    return( m_tTwoPassDrawing );
  544. }
  545.  
  546. BOOL CTestContainer98Doc::HonorIgnoreActivateWhenVisible() const
  547. {
  548.    return( m_tHonorIgnoreActivateWhenVisible );
  549. }
  550.  
  551. int CTestContainer98Doc::AddAmbientProperty( DISPID dispid, LPCTSTR pszName,
  552.    const VARIANT& var, int vti )
  553. {
  554.    CAmbientProperty* pProp;
  555.    int iProp;
  556.  
  557.    pProp = new CAmbientProperty( dispid, pszName, var, vti );
  558.    if( pProp == NULL )
  559.    {
  560.       AfxThrowMemoryException();
  561.    }
  562.  
  563.    iProp = m_apAmbientProperties.Add( pProp );
  564.  
  565.    return( iProp );
  566. }
  567.  
  568. CAmbientProperty* CTestContainer98Doc::FindAmbientProperty( DISPID dispid )
  569. {
  570.    int iProp;
  571.  
  572.    for( iProp = 0; iProp < m_apAmbientProperties.GetSize(); iProp++ )
  573.    {
  574.       if( m_apAmbientProperties[iProp]->GetID() == dispid )
  575.       {
  576.          return( m_apAmbientProperties[iProp] );
  577.       }
  578.    }
  579.  
  580.    // We didn't find the property.
  581.    return( NULL );
  582. }
  583.  
  584. CAmbientProperty* CTestContainer98Doc::FindAmbientProperty( LPCOLESTR pszName )
  585. {
  586.    USES_CONVERSION;
  587.    int iProp;
  588.    LPCTSTR pszNameT;
  589.  
  590.    pszNameT = OLE2CT( pszName );
  591.    for( iProp = 0; iProp < m_apAmbientProperties.GetSize(); iProp++ )
  592.    {
  593.       if( m_apAmbientProperties[iProp]->GetName() == pszNameT )
  594.       {
  595.          return( m_apAmbientProperties[iProp] );
  596.       }
  597.    }
  598.  
  599.    // We didn't find the property.
  600.    return( NULL );
  601. }
  602.  
  603. CAmbientProperty* CTestContainer98Doc::GetAmbientProperty( int iProp )
  604. {
  605.    return( m_apAmbientProperties[iProp] );
  606. }
  607.  
  608. int CTestContainer98Doc::GetNumAmbientProperties() const
  609. {
  610.    return( m_apAmbientProperties.GetSize() );
  611. }
  612.  
  613. POSITION CTestContainer98Doc::FindItem( CDocItem* pItem ) const
  614. {
  615.    return( m_docItemList.Find( pItem ) );
  616. }
  617.  
  618. POSITION CTestContainer98Doc::GetTailPosition() const
  619. {
  620.    return( m_docItemList.GetTailPosition() );
  621. }
  622.  
  623. CDocItem* CTestContainer98Doc::GetPrevItem( POSITION& pos ) const
  624. {
  625.    return( (CDocItem*)m_docItemList.GetPrev( pos ) );
  626. }
  627.  
  628. CTestContainer98Item* CTestContainer98Doc::GetNextTabItem(
  629.    CTestContainer98Item* pItem ) const
  630. {
  631.    POSITION posItem;
  632.  
  633.    if( pItem == NULL )
  634.    {
  635.       if( m_lpTabOrder.IsEmpty() )
  636.       {
  637.          return( NULL );
  638.       }
  639.  
  640.       return( m_lpTabOrder.GetHead() );
  641.    }
  642.  
  643.    posItem = m_lpTabOrder.Find( pItem );
  644.    ASSERT( posItem != NULL );
  645.    m_lpTabOrder.GetNext( posItem );
  646.    if( posItem == NULL )
  647.    {
  648.       // Wrap around to the beginning of the list.
  649.       return( m_lpTabOrder.GetHead() );
  650.    }
  651.  
  652.    return( m_lpTabOrder.GetAt( posItem ) );
  653. }
  654.  
  655. void CTestContainer98Doc::SetItemTabOrder( CTestContainer98Item* pItem,
  656.    int iTabOrder )
  657. {
  658.    POSITION posItem;
  659.  
  660.    ASSERT( pItem != NULL );
  661.  
  662.    posItem = FindItem( pItem );
  663.    ASSERT( posItem != NULL );
  664.  
  665.    posItem = m_lpTabOrder.Find( pItem );
  666.    ASSERT( posItem != NULL );
  667.  
  668.    m_lpTabOrder.RemoveAt( posItem );
  669.    posItem = m_lpTabOrder.FindIndex( iTabOrder );
  670.    if( posItem == NULL )
  671.    {
  672.       // We went off the end of the list.
  673.       m_lpTabOrder.AddTail( pItem );
  674.    }
  675.    else
  676.    {
  677.       m_lpTabOrder.InsertBefore( posItem, pItem );
  678.    }
  679. }
  680.  
  681. BOOL CTestContainer98Doc::GetUserMode() const
  682. {
  683.    return( m_tUserMode );
  684. }
  685.  
  686. void CTestContainer98Doc::SetUserMode( BOOL tUserMode )
  687. {
  688.    CAmbientProperty* pProp;
  689.  
  690.    if( (m_tUserMode && tUserMode) || (!m_tUserMode && !tUserMode) )
  691.    {
  692.       // No change
  693.       return;
  694.    }
  695.  
  696.    m_tUserMode = tUserMode;
  697.    pProp = FindAmbientProperty( DISPID_AMBIENT_USERMODE );
  698.    ASSERT( pProp != NULL );
  699.  
  700.    if( m_tUserMode )
  701.    {
  702.       pProp->SetValue( COleVariant( VARIANT_BOOL( -1 ), VT_BOOL ) );
  703.    }
  704.    else
  705.    {
  706.       pProp->SetValue( COleVariant( VARIANT_FALSE, VT_BOOL ) );
  707.    }
  708.  
  709.    if( pProp->IsEnabled() )
  710.    {
  711.       BroadcastAmbientPropertyChange( pProp->GetID() );
  712.    }
  713. }
  714.  
  715. void CTestContainer98Doc::BroadcastAmbientPropertyChange( DISPID dispid )
  716. {
  717.    POSITION posItem;
  718.    CTestContainer98Item* pItem;
  719.  
  720.    TCTrace( TRACELEVEL_NORMAL,
  721.       "Broadcasting ambient property change.  DISPID=%d\n", dispid );
  722.  
  723.    posItem = GetStartPosition();
  724.    while( posItem != NULL )
  725.    {
  726.       pItem = (CTestContainer98Item*)GetNextItem( posItem );
  727.       pItem->OnAmbientPropertyChange( dispid );
  728.    }
  729.  
  730.    if( (dispid == DISPID_AMBIENT_USERMODE) || (dispid == DISPID_UNKNOWN) )
  731.    {
  732.       // Invalidate the view, since some objects need to be rendered
  733.       // differently in design mode vs. user mode.
  734.       UpdateAllViews( NULL );
  735.    }
  736. }
  737.  
  738. BOOL CTestContainer98Doc::SupportsInterface( REFIID iid )
  739. {
  740.    if( iid == IID_IOleInPlaceSiteEx )
  741.    {
  742.       return( m_tIOleInPlaceSiteEx || m_tIOleInPlaceSiteWindowless );
  743.    }
  744.    else if( iid == IID_IOleInPlaceSiteWindowless )
  745.    {
  746.       return( m_tIOleInPlaceSiteWindowless );
  747.    }
  748.    else if( iid == IID_IAdviseSinkEx )
  749.    {
  750.       return( m_tIAdviseSinkEx );
  751.    }
  752.  
  753.    return( FALSE );
  754. }
  755.  
  756. BOOL CTestContainer98Doc::SupportsService( REFGUID sid )
  757. {
  758.    if( sid == SID_SBindHost )
  759.    {
  760.       return( m_tSBindHost );
  761.    }
  762.  
  763.    return( FALSE );
  764. }
  765.  
  766. void CTestContainer98Doc::PreCloseFrame( CFrameWnd* pFrame )
  767. {
  768.    POSITION posItem;
  769.    CTestContainer98Item* pItem;
  770.  
  771.    posItem = GetStartPosition();
  772.    while( posItem != NULL )
  773.    {
  774.       pItem = (CTestContainer98Item*)GetNextItem( posItem );
  775.       if( pItem != NULL )
  776.       {
  777.          if( pItem->IsInPlaceActive() )
  778.          {
  779.             pItem->Deactivate();
  780.          }
  781.       }
  782.    }
  783.  
  784.    COleDocument::PreCloseFrame( pFrame );
  785. }
  786.  
  787. BOOL CTestContainer98Doc::CanCloseFrame( CFrameWnd* pFrame )
  788. {
  789.    return( COleDocument::CanCloseFrame( pFrame ) );
  790. }
  791.  
  792. BOOL CTestContainer98Doc::OnSaveDocument( LPCTSTR pszPathName )
  793. {
  794.    return( COleDocument::OnSaveDocument( pszPathName ) );
  795. }
  796.  
  797. void CTestContainer98Doc::AddItem( CDocItem* pItem )
  798. {
  799.     COleDocument::AddItem( pItem );
  800.  
  801.    m_lpTabOrder.AddTail( (CTestContainer98Item*)pItem );
  802. }
  803.  
  804. CLog* CTestContainer98Doc::GetLog() const
  805. {
  806.    return( m_pLog );
  807. }
  808.  
  809. void CTestContainer98Doc::DeleteContents()
  810. {
  811.    Cleanup();
  812.  
  813.     COleDocument::DeleteContents();
  814. }
  815.  
  816. void CTestContainer98Doc::OnOptionsFreezeEvents()
  817. {
  818.    POSITION posItem;
  819.    CTestContainer98Item* pItem;
  820.  
  821.    m_tFreezeEvents = !m_tFreezeEvents;
  822.  
  823.    posItem = GetStartPosition();
  824.    while( posItem != NULL )
  825.    {
  826.       // Set the item's FreezeEvents status.
  827.       pItem = (CTestContainer98Item*)GetNextItem( posItem );
  828.       pItem->FreezeEvents( m_tFreezeEvents );
  829.    }
  830. }
  831.  
  832. void CTestContainer98Doc::OnUpdateOptionsFreezeEvents( CCmdUI* pCmdUI )
  833. {
  834.    if( m_tFreezeEvents )
  835.    {
  836.       pCmdUI->SetCheck( 1 );
  837.    }
  838.    else
  839.    {
  840.       pCmdUI->SetCheck( 0 );
  841.    }
  842. }
  843.  
  844. void CTestContainer98Doc::OnOptionsSlowDrawing()
  845. {
  846.    m_tSlowDrawing = !m_tSlowDrawing;
  847. }
  848.  
  849. void CTestContainer98Doc::OnUpdateOptionsSlowDrawing( CCmdUI* pCmdUI )
  850. {
  851.    pCmdUI->SetCheck( m_tSlowDrawing == TRUE );
  852. }
  853.  
  854. void CTestContainer98Doc::OnOptionsTwoPassDrawing()
  855. {
  856.    m_tTwoPassDrawing = !m_tTwoPassDrawing;
  857. }
  858.  
  859. void CTestContainer98Doc::OnUpdateOptionsTwoPassDrawing( CCmdUI* pCmdUI )
  860. {
  861.    pCmdUI->SetCheck( m_tTwoPassDrawing == TRUE );
  862. }
  863.  
  864. void CTestContainer98Doc::OnOptionsLogging()
  865. {
  866.    CLoggingDlg dlg;
  867.    CFileLog* pFileLog;
  868.    int nResult;
  869.    int iOldLogType;
  870.    BOOL tSuccess;
  871.  
  872.    if( m_pLog->IsKindOf( RUNTIME_CLASS( CNullLog ) ) )
  873.    {
  874.       dlg.m_iLogType = TCLOG_NULL;
  875.    }
  876.    else if( m_pLog->IsKindOf( RUNTIME_CLASS( COutputWindowLog ) ) )
  877.    {
  878.       dlg.m_iLogType = TCLOG_OUTPUTWINDOW;
  879.    }
  880.    else if( m_pLog->IsKindOf( RUNTIME_CLASS( CDebugLog ) ) )
  881.    {
  882.       dlg.m_iLogType = TCLOG_DEBUG;
  883.    }
  884.    else if( m_pLog->IsKindOf( RUNTIME_CLASS( CFileLog ) ) )
  885.    {
  886.       pFileLog = DYNAMIC_DOWNCAST( CFileLog, m_pLog );
  887.       dlg.m_iLogType = TCLOG_FILE;
  888.       dlg.m_strFileName = pFileLog->GetFileName();
  889.    }
  890.    iOldLogType = dlg.m_iLogType;
  891.  
  892.    nResult = dlg.DoModal();
  893.    if( nResult != IDOK )
  894.    {
  895.       return;
  896.    }
  897.  
  898.    switch( dlg.m_iLogType )
  899.    {
  900.    case TCLOG_NULL:
  901.       if( iOldLogType != TCLOG_NULL )
  902.       {
  903.          delete m_pLog;
  904.          m_pLog = new CNullLog;
  905.       }
  906.       break;
  907.  
  908.    case TCLOG_OUTPUTWINDOW:
  909.       if( iOldLogType != TCLOG_OUTPUTWINDOW )
  910.       {
  911.          CMainFrame* pMainFrame;
  912.  
  913.          delete m_pLog;
  914.          pMainFrame = STATIC_DOWNCAST( CMainFrame, AfxGetMainWnd() );
  915.          m_pLog = new COutputWindowLog( GetOutputEditControl() );
  916.       }
  917.       break;
  918.  
  919.    case TCLOG_DEBUG:
  920.       if( iOldLogType != TCLOG_DEBUG )
  921.       {
  922.          delete m_pLog;
  923.          m_pLog = new CDebugLog;
  924.       }
  925.       break;
  926.  
  927.    case TCLOG_FILE:
  928.       if( (iOldLogType != TCLOG_FILE) || ((iOldLogType == TCLOG_FILE) &&
  929.          ((DYNAMIC_DOWNCAST( CFileLog, m_pLog ))->GetFileName() !=
  930.          dlg.m_strFileName)) )
  931.       {
  932.          delete m_pLog;
  933.          m_pLog = new CFileLog;
  934.          pFileLog = DYNAMIC_DOWNCAST( CFileLog, m_pLog );
  935.          tSuccess = pFileLog->Create( dlg.m_strFileName );
  936.          if( !tSuccess )
  937.          {
  938.             TRACE( "Failed to create log file.\n" );
  939.          }
  940.       }
  941.       break;
  942.    }
  943. }
  944.  
  945. void CTestContainer98Doc::OnContainerOptions()
  946. {
  947.    int nResult;
  948.    CFeaturesPage pageFeatures;
  949.    CInterfacesPage pageInterfaces;
  950.    CServicesPage pageServices;
  951.    CPropertySheet sheet( IDS_CONTAINEROPTIONS );
  952.  
  953.    pageFeatures.m_tAllowWindowless = m_tAllowWindowless;
  954.    pageFeatures.m_tTwoPassDrawing = m_tTwoPassDrawing;
  955.    pageFeatures.m_tHonorIgnoreActivateWhenVisible =
  956.       m_tHonorIgnoreActivateWhenVisible;
  957.    pageFeatures.m_tUseIPointerInactive = m_tUseIPointerInactive;
  958.    pageFeatures.m_tUseQuickActivation = m_tUseQuickActivation;
  959.    sheet.AddPage( &pageFeatures );
  960.  
  961.    pageInterfaces.m_tIOleInPlaceSiteEx = m_tIOleInPlaceSiteEx;
  962.    pageInterfaces.m_tIOleInPlaceSiteWindowless = m_tIOleInPlaceSiteWindowless;
  963.    pageInterfaces.m_tIAdviseSinkEx = m_tIAdviseSinkEx;
  964.    sheet.AddPage( &pageInterfaces );
  965.  
  966.    pageServices.m_tSBindHost = m_tSBindHost;
  967.    sheet.AddPage( &pageServices );
  968.  
  969.    sheet.m_psh.dwFlags &= ~PSH_HASHELP;
  970.    sheet.m_psh.dwFlags |= PSH_NOAPPLYNOW;
  971.    nResult = sheet.DoModal();
  972.    if( nResult != IDOK )
  973.    {
  974.       return;
  975.    }
  976.  
  977.    m_tAllowWindowless = pageFeatures.m_tAllowWindowless;
  978.    m_tTwoPassDrawing = pageFeatures.m_tTwoPassDrawing;
  979.    m_tHonorIgnoreActivateWhenVisible =
  980.       pageFeatures.m_tHonorIgnoreActivateWhenVisible;
  981.    m_tUseIPointerInactive = pageFeatures.m_tUseIPointerInactive;
  982.    m_tUseQuickActivation = pageFeatures.m_tUseQuickActivation;
  983.  
  984.    m_tIOleInPlaceSiteEx = pageInterfaces.m_tIOleInPlaceSiteEx;
  985.    m_tIOleInPlaceSiteWindowless = pageInterfaces.m_tIOleInPlaceSiteWindowless;
  986.    m_tIAdviseSinkEx = pageInterfaces.m_tIAdviseSinkEx;
  987.  
  988.    m_tSBindHost = pageServices.m_tSBindHost;
  989. }
  990.  
  991. void CTestContainer98Doc::OnContainerAmbientProperties()
  992. {
  993.    DISPID dispidChanged;
  994.    BOOL tChanged;
  995.    CAmbientProperty* pProperty;
  996.  
  997.    CAmbientPropertiesDlg dlg( NULL, this );
  998.  
  999.    dlg.DoModal();
  1000.  
  1001.    // Check for changes.
  1002.    tChanged = dlg.GetChangedPropertyID( &dispidChanged );
  1003.    if( tChanged )
  1004.    {
  1005.       if( (dispidChanged == DISPID_AMBIENT_USERMODE) || (dispidChanged ==
  1006.          DISPID_UNKNOWN) )
  1007.       {
  1008.          // Update our cached version of the UserMode property.
  1009.          pProperty = FindAmbientProperty( DISPID_AMBIENT_USERMODE );
  1010.          ASSERT( pProperty != NULL );
  1011.          ASSERT( pProperty->GetValue().vt == VT_BOOL );
  1012.          m_tUserMode = pProperty->GetValue().bVal;
  1013.       }
  1014.       BroadcastAmbientPropertyChange( dispidChanged );
  1015.    }
  1016. }
  1017.  
  1018. void CTestContainer98Doc::OnContainerInfo()
  1019. {
  1020.    CContainerInfoDlg dlg( NULL, this );
  1021.  
  1022.    dlg.DoModal();
  1023. }
  1024.  
  1025. void CTestContainer98Doc::OnEditDeleteAll()
  1026. {
  1027.    CTestContainer98Item* pItem;
  1028.    POSITION posItem;
  1029.    POSITION posView;
  1030.    CTestContainer98View* pView;
  1031.  
  1032.    posView = GetFirstViewPosition();
  1033.    ASSERT( posView != NULL );
  1034.    pView = DYNAMIC_DOWNCAST( CTestContainer98View, GetNextView( posView ) );
  1035.    pView->ClearSelection();
  1036.  
  1037.    do
  1038.    {
  1039.       posItem = GetStartPosition();
  1040.       if( posItem != NULL )
  1041.       {
  1042.          pItem = (CTestContainer98Item*)GetNextItem( posItem );
  1043.          ASSERT( pItem != NULL );
  1044.          pItem->Delete();
  1045.       }
  1046.    } while( posItem != NULL );
  1047.  
  1048.    UpdateAllViews( NULL );
  1049. }
  1050.  
  1051. void CTestContainer98Doc::OnUpdateEditDeleteAll( CCmdUI* pCmdUI )
  1052. {
  1053.    POSITION posItem;
  1054.  
  1055.    posItem = GetStartPosition();
  1056.    pCmdUI->Enable( posItem != NULL );
  1057. }
  1058.  
  1059. void CTestContainer98Doc::RemoveItem( CDocItem* pItem )
  1060. {
  1061.    POSITION posItem;
  1062.  
  1063.    posItem = m_lpTabOrder.Find( DYNAMIC_DOWNCAST( CTestContainer98Item,
  1064.       pItem ) );
  1065.    ASSERT( posItem != NULL );
  1066.    m_lpTabOrder.RemoveAt( posItem );
  1067.  
  1068.     COleDocument::RemoveItem( pItem );
  1069. }
  1070.  
  1071. void CTestContainer98Doc::OnContainerTabOrder()
  1072. {
  1073.    POSITION posItem;
  1074.    CTabOrderDlg dlg;
  1075.    int nResult;
  1076.    CTestContainer98Item* pItem;
  1077.  
  1078.    posItem = m_lpTabOrder.GetHeadPosition();
  1079.    while( posItem != NULL )
  1080.    {
  1081.       pItem = m_lpTabOrder.GetNext( posItem );
  1082.       ASSERT( pItem != NULL );
  1083.       dlg.m_lpTabOrder.AddTail( pItem );
  1084.    }
  1085.  
  1086.    nResult = dlg.DoModal();
  1087.    if( nResult != IDOK )
  1088.    {
  1089.       return;
  1090.    }
  1091.  
  1092.    m_lpTabOrder.RemoveAll();
  1093.    posItem = dlg.m_lpTabOrder.GetHeadPosition();
  1094.    while( posItem != NULL )
  1095.    {
  1096.       pItem = dlg.m_lpTabOrder.GetNext( posItem );
  1097.       m_lpTabOrder.AddTail( pItem );
  1098.    }
  1099. }
  1100.  
  1101. CEdit* CTestContainer98Doc::GetOutputEditControl()
  1102. {
  1103.    POSITION posView;
  1104.    CView* pView;
  1105.  
  1106.    posView = GetFirstViewPosition();
  1107.    ASSERT( posView != NULL );
  1108.  
  1109.    while( posView != NULL )
  1110.    {
  1111.       pView = GetNextView( posView );
  1112.       ASSERT( pView != NULL );
  1113.       if( pView->IsKindOf( RUNTIME_CLASS( CEditView ) ) )
  1114.       {
  1115.          return( &(STATIC_DOWNCAST( CEditView, pView )->GetEditCtrl()) );
  1116.       }
  1117.    }
  1118.  
  1119.    return( NULL );
  1120. }
  1121.  
  1122. CTestContainer98View* CTestContainer98Doc::GetView() const
  1123. {
  1124.    POSITION posView;
  1125.    CView* pView;
  1126.  
  1127.    posView = GetFirstViewPosition();
  1128.    ASSERT( posView != NULL );
  1129.  
  1130.    while( posView != NULL )
  1131.    {
  1132.       pView = GetNextView( posView );
  1133.       ASSERT( pView != NULL );
  1134.       if( pView->IsKindOf( RUNTIME_CLASS( CTestContainer98View ) ) )
  1135.       {
  1136.          return( STATIC_DOWNCAST( CTestContainer98View, pView ) );
  1137.       }
  1138.    }
  1139.  
  1140.    ASSERT( FALSE );
  1141.  
  1142.    return( NULL );
  1143. }
  1144.  
  1145. CTestContainer98Item* CTestContainer98Doc::AddControl( REFCLSID clsid,
  1146.    LPCTSTR pszName, REFIID iidPersistanceMedium, IUnknown* pPersistanceMedium )
  1147. {
  1148.    CTestContainer98Item* pItem;
  1149.  
  1150.    pItem = NULL;
  1151.    try
  1152.    {
  1153.       pItem = new CTestContainer98Item( this );
  1154.       ASSERT_VALID( pItem );
  1155.  
  1156.       if( !pItem->CreateOrLoad( clsid, pszName, iidPersistanceMedium,
  1157.          pPersistanceMedium ) )
  1158.       {
  1159.          AfxThrowOleException( E_FAIL );
  1160.       }
  1161.       ASSERT_VALID( pItem );
  1162.  
  1163.       pItem->InvalidateItem();
  1164.    }
  1165.    catch( ... )
  1166.    {
  1167.       pItem->Delete();
  1168.  
  1169.       throw;
  1170.    }
  1171.    ASSERT( pItem != NULL );
  1172.  
  1173.    return( pItem );
  1174. }
  1175.  
  1176. LPDISPATCH CTestContainer98Doc::InsertControl( LPCTSTR pszProgID,
  1177.    LPCTSTR pszName )
  1178. {
  1179.    USES_CONVERSION;
  1180.    CLSID clsid;
  1181.    HRESULT hResult;
  1182.    CTestContainer98Item* pItem;
  1183.    IDispatch* pDispatch;
  1184.    CScriptManager* pScriptManager;
  1185.  
  1186.    TRACE( "InsertControl: %s\n", pszProgID );
  1187.  
  1188.    hResult = CLSIDFromProgID( T2COLE( pszProgID ), &clsid );
  1189.    if( FAILED( hResult ) )
  1190.    {
  1191.       TRACE( "CLSIDFromProgID failed\n" );
  1192.       return( NULL );
  1193.    }
  1194.  
  1195.    pItem = NULL;
  1196.    try
  1197.    {
  1198.       pItem = AddControl( clsid, pszName );
  1199.       ASSERT_VALID( pItem );
  1200.  
  1201.       pItem->InvalidateItem();
  1202.    }
  1203.    catch( COleException* pException )
  1204.    {
  1205.       ASSERT( pItem == NULL );
  1206.  
  1207.       hResult = pException->m_sc;
  1208.       pException->Delete();
  1209.  
  1210.       return( NULL );
  1211.    }
  1212.    catch( CException* pException )
  1213.    {
  1214.       ASSERT( pItem == NULL );
  1215.  
  1216.       pException->Delete();
  1217.  
  1218.       return( NULL );
  1219.    }
  1220.    ASSERT( pItem != NULL );
  1221.  
  1222.    pDispatch = NULL;
  1223.    hResult = pItem->m_lpObject->QueryInterface( IID_IDispatch,
  1224.       (void**)&pDispatch );
  1225.    if( FAILED( hResult ) )
  1226.    {
  1227.       pItem->Delete();
  1228.       return( NULL );
  1229.    }
  1230.    ASSERT( pDispatch != NULL );
  1231.  
  1232.    pScriptManager = GetScriptManager( FALSE );
  1233.    if( pScriptManager != NULL )
  1234.    {
  1235.       pScriptManager->AddNamedItem( pszName, pDispatch );
  1236.    }
  1237.  
  1238.    FireOnNewControl( pDispatch );
  1239.  
  1240.    return( pDispatch );
  1241. }
  1242.  
  1243. LPDISPATCH CTestContainer98Doc::GetPrimarySelection()
  1244. {
  1245.    IDispatch* pDispatch;
  1246.    POSITION posView;
  1247.    CTestContainer98View* pView;
  1248.    HRESULT hResult;
  1249.  
  1250.    posView = GetFirstViewPosition();
  1251.    ASSERT( posView != NULL );
  1252.    pView = (CTestContainer98View*)GetNextView( posView );
  1253.    ASSERT( posView == NULL );
  1254.  
  1255.    if( pView->m_pSelection == NULL )
  1256.    {
  1257.       return( NULL );
  1258.    }
  1259.  
  1260.    pDispatch = NULL;
  1261.    hResult = pView->m_pSelection->m_lpObject->QueryInterface( IID_IDispatch,
  1262.       (void**)&pDispatch );
  1263.    if( FAILED( hResult ) )
  1264.    {
  1265.       return( NULL );
  1266.    }
  1267.  
  1268.    return( pDispatch );
  1269. }
  1270.  
  1271. void CTestContainer98Doc::SetPrimarySelection( LPDISPATCH pNewSelection )
  1272. {
  1273.    IUnknownPtr punkItem;
  1274.    IUnknownPtr punkNewSelection;
  1275.    POSITION posItem;
  1276.    CTestContainer98Item* pItem;
  1277.  
  1278.    if( pNewSelection == NULL )
  1279.    {
  1280.       GetView()->ClearSelection();
  1281.    }
  1282.    else
  1283.    {
  1284.       punkNewSelection = pNewSelection;
  1285.       ASSERT( punkNewSelection != NULL );
  1286.  
  1287.       posItem = GetStartPosition();
  1288.       while( posItem != NULL )
  1289.       {
  1290.          pItem = (CTestContainer98Item*)GetNextItem( posItem );
  1291.          punkItem = pItem->m_lpObject;
  1292.          ASSERT( punkItem != NULL );
  1293.  
  1294.          if( punkItem == punkNewSelection )
  1295.          {
  1296.             GetView()->SetSelection( pItem );
  1297.             return;
  1298.          }
  1299.       }
  1300.    }
  1301. }
  1302.  
  1303. LPDISPATCH CTestContainer98Doc::FindControl( LPCTSTR pszName )
  1304. {
  1305.    POSITION posItem;
  1306.    CTestContainer98Item* pItem;
  1307.    IDispatch* pDispatch;
  1308.    HRESULT hResult;
  1309.  
  1310.    posItem = GetStartPosition();
  1311.    while( posItem != NULL )
  1312.    {
  1313.       pItem = (CTestContainer98Item*)GetNextItem( posItem );
  1314.       if( pItem->GetDisplayName() == pszName )
  1315.       {
  1316.          hResult = pItem->m_lpObject->QueryInterface( IID_IDispatch,
  1317.             (void**)&pDispatch );
  1318.          if( FAILED( hResult ) )
  1319.          {
  1320.             return( NULL );
  1321.          }
  1322.  
  1323.          return( pDispatch );
  1324.       }
  1325.    }
  1326.  
  1327.     return( NULL );
  1328. }
  1329.  
  1330. void CTestContainer98Doc::OnToolsMacros()
  1331. {
  1332.    CMacroDlg dlg;
  1333.    int nResult;
  1334.    HRESULT hResult;
  1335.    CScriptManager* pScriptManager;
  1336.  
  1337.    pScriptManager = GetScriptManager();
  1338.    if( pScriptManager == NULL )
  1339.    {
  1340.       return;
  1341.    }
  1342.  
  1343.    dlg.m_pScriptManager = m_pScriptManager;
  1344.  
  1345.    nResult = dlg.DoModal();
  1346.    if( nResult != IDOK )
  1347.    {
  1348.       return;
  1349.    }
  1350.  
  1351.    hResult = dlg.m_pScript->RunMacro( dlg.m_strMacroName );
  1352.    if( FAILED( hResult ) )
  1353.    {
  1354.       TRACE( "Script failed\n" );
  1355.       return;
  1356.    }
  1357. }
  1358.  
  1359. HRESULT CTestContainer98Doc::InitScriptManager()
  1360. {
  1361.    POSITION posItem;
  1362.    CTestContainer98Item* pItem;
  1363.    IDispatchPtr pDispatch;
  1364.  
  1365.    m_pScriptManager = new CScriptManager( this );
  1366.    if( m_pScriptManager == NULL )
  1367.    {
  1368.       return( E_OUTOFMEMORY );
  1369.    }
  1370.  
  1371.    posItem = GetStartPosition();
  1372.    while( posItem != NULL )
  1373.    {
  1374.       pItem = (CTestContainer98Item*)GetNextItem( posItem );
  1375.       pDispatch = pItem->m_lpObject;
  1376.       if( pDispatch == NULL )
  1377.       {
  1378.          return( E_NOINTERFACE );
  1379.       }
  1380.       m_pScriptManager->AddNamedItem( pItem->GetDisplayName(), pDispatch );
  1381.    }
  1382.  
  1383.    return( S_OK );
  1384. }
  1385.  
  1386. void CTestContainer98Doc::Log( LPCTSTR pszMessage )
  1387. {
  1388.    CLog* pLog;
  1389.  
  1390.    pLog = GetLog();
  1391.    (*pLog)<<pszMessage;
  1392. }
  1393.  
  1394. void CTestContainer98Doc::FireOnNewControl( IDispatch* pControl )
  1395. {
  1396.    int iSink;
  1397.    IDispatch* pSink;
  1398.    COleDispatchDriver driver;
  1399.  
  1400.    const CPtrArray* pConnections = m_xTC98CP.GetConnections();
  1401.    ASSERT( pConnections != NULL );
  1402.  
  1403.    for( iSink = 0; iSink < pConnections->GetSize(); iSink++ )
  1404.    {
  1405.       pSink = (IDispatch*)(pConnections->GetAt( iSink ));
  1406.       ASSERT( pSink != NULL );
  1407.  
  1408.       driver.AttachDispatch( pSink, FALSE );
  1409.  
  1410.       try
  1411.       {
  1412.          driver.InvokeHelper( 1, DISPATCH_METHOD, VT_EMPTY, NULL, LPBYTE(
  1413.             VTS_DISPATCH ), pControl );
  1414.       }
  1415.       catch( CException* pException )
  1416.       {
  1417.          pException->Delete();
  1418.       }
  1419.    }
  1420. }
  1421.  
  1422. BOOL CTestContainer98Doc::GetDispatchIID( IID* piid )
  1423. {
  1424.    ASSERT( piid != NULL );
  1425.  
  1426.    *piid = __uuidof( ITestContainer98 );
  1427.  
  1428.    return( TRUE );
  1429. }
  1430.  
  1431.  
  1432. BOOL CTestContainer98Doc::OnOpenDocument( LPCTSTR pszPathName )
  1433. {
  1434.    int iChar;
  1435.  
  1436.    ASSERT( pszPathName != NULL );
  1437.  
  1438.    iChar = lstrlen( pszPathName )-1;
  1439.    while( (iChar >= 0) && (pszPathName[iChar] != _T( '\\' )) )
  1440.    {
  1441.       iChar--;
  1442.    }
  1443.  
  1444.    m_strDirectory = CString( pszPathName ).Left( iChar+1 );
  1445.  
  1446.     if( !COleDocument::OnOpenDocument( pszPathName ) )
  1447.    {
  1448.         return( FALSE );
  1449.    }
  1450.  
  1451.     return( TRUE );
  1452. }
  1453.  
  1454. CScriptManager* CTestContainer98Doc::GetScriptManager( BOOL tCreate )
  1455. {
  1456.    HRESULT hResult;
  1457.  
  1458.    if( m_pScriptManager == NULL )
  1459.    {
  1460.       if( tCreate )
  1461.       {
  1462.          hResult = InitScriptManager();
  1463.          if( FAILED( hResult ) )
  1464.          {
  1465.             AfxMessageBox( IDS_NOSCRIPTING );
  1466.          }
  1467.          else
  1468.          {
  1469.             ASSERT( m_pScriptManager != NULL );
  1470.          }
  1471.       }
  1472.    }
  1473.  
  1474.    return( m_pScriptManager );
  1475. }
  1476.  
  1477. void CTestContainer98Doc::OnRunMacro()
  1478. {
  1479.    CScript* pScript;
  1480.    CString strMacroName;
  1481.    CMainFrame* pMainFrame;
  1482.    CComboBox* pComboBox;
  1483.    HRESULT hResult;
  1484.    CScriptManager* pScriptManager;
  1485.  
  1486.    pScriptManager = GetScriptManager();
  1487.    if( pScriptManager == NULL )
  1488.    {
  1489.       return;
  1490.    }
  1491.  
  1492.    pMainFrame = STATIC_DOWNCAST( CMainFrame, AfxGetMainWnd() );
  1493.    pComboBox = STATIC_DOWNCAST( CComboBox,
  1494.       pMainFrame->m_wndMacroBar.GetDlgItem( IDC_MACRO ) );
  1495.  
  1496.    pComboBox->GetLBText( 0, strMacroName );
  1497.    TRACE( "Macro: %s\n", LPCTSTR( strMacroName ) );
  1498.    pScript = pScriptManager->FindMacro( strMacroName );
  1499.    if( pScript != NULL )
  1500.    {
  1501.       hResult = pScript->RunMacro( strMacroName );
  1502.       if( FAILED( hResult ) )
  1503.       {
  1504.       }
  1505.    }
  1506. }
  1507.