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 / insctldg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  9.9 KB  |  421 lines

  1. // InsertControlDlg.Cpp : implementation file
  2. //
  3.  
  4. #include "StdAfx.H"
  5. #include "TestCon.H"
  6. #include "Resource.HM"
  7.  
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CInsertControlDlg dialog
  16.  
  17.  
  18. CInsertControlDlg::CInsertControlDlg( CWnd* pParent ) :
  19.    CDialog( CInsertControlDlg::IDD, pParent ),
  20.    m_clsid( CLSID_NULL )
  21. {
  22.     //{{AFX_DATA_INIT(CInsertControlDlg)
  23.         // NOTE: the ClassWizard will add member initialization here
  24.     //}}AFX_DATA_INIT
  25. }
  26.  
  27.  
  28. void CInsertControlDlg::DoDataExchange(CDataExchange* pDX)
  29. {
  30.    int iItem;
  31.    POSITION posControl;
  32.  
  33.     CDialog::DoDataExchange(pDX);
  34.     //{{AFX_DATA_MAP(CInsertControlDlg)
  35.     DDX_Control(pDX, IDC_SERVERPATH, m_staticServerPath);
  36.     DDX_Control(pDX, IDC_REQUIREDCATEGORIES, m_butRequiredCategories);
  37.     DDX_Control(pDX, IDC_IGNOREREQUIREDCATEGORIES, m_butIgnoreRequiredCategories);
  38.     DDX_Control(pDX, IDOK, m_butOK);
  39.     DDX_Control(pDX, IDC_CONTROLS, m_lbControls);
  40.     //}}AFX_DATA_MAP
  41.  
  42.    if( pDX->m_bSaveAndValidate )
  43.    {
  44.       iItem = m_lbControls.GetCurSel();
  45.       if( iItem == LB_ERR )
  46.       {
  47.          m_clsid = CLSID_NULL;
  48.       }
  49.       else
  50.       {
  51.          posControl = POSITION( m_lbControls.GetItemDataPtr( iItem ) );
  52.          ASSERT( posControl != NULL );
  53.          m_clsid = m_lControls.GetAt( posControl );
  54.       }
  55.    }
  56. }
  57.  
  58.  
  59. BEGIN_MESSAGE_MAP(CInsertControlDlg, CDialog)
  60.     //{{AFX_MSG_MAP(CInsertControlDlg)
  61.     ON_BN_CLICKED(IDC_IMPLEMENTEDCATEGORIES, OnImplementedCategories)
  62.     ON_LBN_DBLCLK(IDC_CONTROLS, OnControlsDblClk)
  63.     ON_BN_CLICKED(IDC_REQUIREDCATEGORIES, OnRequiredCategories)
  64.     ON_LBN_SELCHANGE(IDC_CONTROLS, OnControlsSelChange)
  65.     ON_BN_CLICKED(IDC_IGNOREREQUIREDCATEGORIES, OnIgnoreRequiredCategories)
  66.     ON_WM_HELPINFO()
  67.     ON_WM_CONTEXTMENU()
  68.     //}}AFX_MSG_MAP
  69. END_MESSAGE_MAP()
  70.  
  71. /////////////////////////////////////////////////////////////////////////////
  72. // CInsertControlDlg message handlers
  73.  
  74. BOOL CInsertControlDlg::OnInitDialog()
  75. {
  76.    HRESULT hResult;
  77.    CATID catid;
  78.  
  79.    hResult = m_pCatInfo.CreateInstance( CLSID_StdComponentCategoriesMgr, NULL,
  80.       CLSCTX_INPROC_SERVER );
  81.    if( FAILED( hResult ) )
  82.    {
  83.       TRACE( "Failed to create category manager\n" );
  84.       EndDialog( IDCANCEL );
  85.       return( TRUE );
  86.    }
  87.  
  88.     CDialog::OnInitDialog();
  89.  
  90.    catid = CATID_Control;
  91.    m_aImplementedCategories.Add( catid );
  92.  
  93.    m_butIgnoreRequiredCategories.SetCheck( 0 );
  94.  
  95.    m_lbControls.ModifyStyle( 0, WS_HSCROLL );
  96.  
  97.    RefreshControlList();
  98.  
  99.    m_lbControls.SetFocus();
  100.  
  101.     return( FALSE );
  102. }
  103.  
  104. void GetClassServerPath( REFCLSID clsid, CString& strServerPath )
  105. {
  106.    HKEY hKey;
  107.    HKEY hServerKey;
  108.    OLECHAR szCLSID[64];
  109.    LONG nResult;
  110.    ULONG nBytes;
  111.    DWORD dwType;
  112.    LPTSTR pszServerPath;
  113.  
  114.    StringFromGUID2( clsid, szCLSID, 64 );
  115.  
  116.    hKey = NULL;
  117.    hServerKey = NULL;
  118.    try
  119.    {
  120.       nResult = RegOpenKeyEx( HKEY_CLASSES_ROOT, CString( "CLSID\\" )+CString(
  121.          szCLSID ), 0, KEY_READ, &hKey );
  122.       if( nResult != ERROR_SUCCESS )
  123.       {
  124.          throw( E_FAIL );
  125.       }
  126.  
  127.       nResult = RegOpenKeyEx( hKey, _T( "InprocServer32" ), 0, KEY_READ,
  128.          &hServerKey );
  129.       if( nResult != ERROR_SUCCESS )
  130.       {
  131.          nResult = RegOpenKeyEx( hKey, _T( "InprocHandler32" ), 0, KEY_READ,
  132.             &hServerKey );
  133.          if( nResult != ERROR_SUCCESS )
  134.          {
  135.             nResult = RegOpenKeyEx( hKey, _T( "LocalServer32" ), 0, KEY_READ,
  136.                &hServerKey );
  137.             if( nResult != ERROR_SUCCESS )
  138.             {
  139.                throw( E_FAIL );
  140.             }
  141.          }
  142.       }
  143.  
  144.       nBytes = 0;
  145.       nResult = RegQueryValueEx( hServerKey, NULL, NULL, &dwType, NULL,
  146.          &nBytes );
  147.       if( (nResult != ERROR_SUCCESS) || (dwType != REG_SZ) )
  148.       {
  149.          throw( E_FAIL );
  150.       }
  151.       pszServerPath = LPTSTR( _alloca( nBytes ) );
  152.       nResult = RegQueryValueEx( hServerKey, NULL, NULL, &dwType,
  153.          LPBYTE( pszServerPath ), &nBytes );
  154.       if( (nResult != ERROR_SUCCESS) || (dwType != REG_SZ) )
  155.       {
  156.          throw( E_FAIL );
  157.       }
  158.  
  159.       strServerPath = pszServerPath;
  160.  
  161.       RegCloseKey( hKey );
  162.       hKey = NULL;
  163.       RegCloseKey( hServerKey );
  164.       hServerKey = NULL;
  165.    }
  166.    catch( HRESULT )
  167.    {
  168.       if( hKey != NULL )
  169.       {
  170.          RegCloseKey( hKey );
  171.       }
  172.       if( hServerKey != NULL )
  173.       {
  174.          RegCloseKey( hServerKey );
  175.       }
  176.  
  177.       strServerPath.LoadString( IDS_SERVERNOTFOUND );
  178.  
  179.       return;
  180.    }
  181. }
  182.  
  183. void CInsertControlDlg::RefreshControlList()
  184. {
  185.    BOOL tDone;
  186.    HRESULT hResult;
  187.    IEnumGUIDPtr pEnum;
  188.    ULONG nImplementedCategories;
  189.    CATID* pcatidImpl;
  190.    ULONG nRequiredCategories;
  191.    CATID* pcatidReq;
  192.    CLSID clsid;
  193.    LPOLESTR pszName;
  194.    CString strName;
  195.    ULONG iCategory;
  196.    int iItem;
  197.    POSITION posControl;
  198.    CString strServerPath;
  199.    CString strString;
  200.  
  201.    m_lbControls.ResetContent();
  202.    m_lControls.RemoveAll();
  203.  
  204.    nImplementedCategories = m_aImplementedCategories.GetSize();
  205.    if( nImplementedCategories == 0 )
  206.    {
  207.       nImplementedCategories = ULONG( -1 );
  208.       pcatidImpl = NULL;
  209.    }
  210.    else
  211.    {
  212.       pcatidImpl = (CATID*)_alloca( nImplementedCategories*sizeof( CATID ) );
  213.       for( iCategory = 0; iCategory < nImplementedCategories; iCategory++ )
  214.       {
  215.          pcatidImpl[iCategory] = m_aImplementedCategories[iCategory];
  216.       }
  217.    }
  218.  
  219.    if( m_butIgnoreRequiredCategories.GetCheck() )
  220.    {
  221.       nRequiredCategories = ULONG( -1 );
  222.       pcatidReq = NULL;
  223.    }
  224.    else
  225.    {
  226.       nRequiredCategories = m_aRequiredCategories.GetSize();
  227.       if( nRequiredCategories == 0 )
  228.       {
  229.          pcatidReq = NULL;
  230.       }
  231.       else
  232.       {
  233.          pcatidReq = (CATID*)_alloca( nRequiredCategories*sizeof( CATID ) );
  234.          for( iCategory = 0; iCategory < nRequiredCategories; iCategory++ )
  235.          {
  236.             pcatidReq[iCategory] = m_aRequiredCategories[iCategory];
  237.          }
  238.       }
  239.    }
  240.  
  241.    hResult = m_pCatInfo->EnumClassesOfCategories( nImplementedCategories,
  242.       pcatidImpl, nRequiredCategories, pcatidReq, &pEnum );
  243.    if( FAILED( hResult ) )
  244.    {
  245.       return;
  246.    }
  247.  
  248.    tDone = FALSE;
  249.    while( !tDone )
  250.    {
  251.       hResult = pEnum->Next( 1, &clsid, NULL );
  252.       if( hResult == S_OK )
  253.       {
  254.          pszName = NULL;
  255.          hResult = OleRegGetUserType( clsid, USERCLASSTYPE_FULL, &pszName );
  256.          if( SUCCEEDED( hResult ) )
  257.          {
  258.             strName = pszName;
  259.             CoTaskMemFree( pszName );
  260.             pszName = NULL;
  261.             iItem = m_lbControls.AddString( strName );
  262.             posControl = m_lControls.AddTail( clsid );
  263.             m_lbControls.SetItemDataPtr( iItem, posControl );
  264.          }
  265.       }
  266.       else
  267.       {
  268.          tDone = TRUE;
  269.       }
  270.    }
  271.  
  272.    OnControlsSelChange();
  273. }
  274.  
  275. void CInsertControlDlg::OnImplementedCategories()
  276. {
  277.    CComponentCategoriesDlg dlg( IDS_IMPLEMENTEDCATEGORIES );
  278.    int nResult;
  279.    int iCategory;
  280.    POSITION posCategory;
  281.    CATID catid;
  282.  
  283.    for( iCategory = 0; iCategory < m_aImplementedCategories.GetSize();
  284.       iCategory++ )
  285.    {
  286.       dlg.m_lSelectedCategories.AddTail( m_aImplementedCategories[
  287.          iCategory] );
  288.    }
  289.  
  290.    nResult = dlg.DoModal();
  291.    if( nResult != IDOK )
  292.    {
  293.       return;
  294.    }
  295.  
  296.    m_aImplementedCategories.RemoveAll();
  297.    posCategory = dlg.m_lSelectedCategories.GetHeadPosition();
  298.    while( posCategory != NULL )
  299.    {
  300.       catid = dlg.m_lSelectedCategories.GetNext( posCategory );
  301.       m_aImplementedCategories.Add( catid );
  302.    }
  303.  
  304.    RefreshControlList();
  305. }
  306.  
  307. void CInsertControlDlg::OnControlsDblClk()
  308. {
  309.    OnOK();
  310. }
  311.  
  312. void CInsertControlDlg::OnRequiredCategories()
  313. {
  314.    CComponentCategoriesDlg dlg( IDS_REQUIREDCATEGORIES );
  315.    int nResult;
  316.    int iCategory;
  317.    POSITION posCategory;
  318.    CATID catid;
  319.  
  320.    for( iCategory = 0; iCategory < m_aRequiredCategories.GetSize();
  321.       iCategory++ )
  322.    {
  323.       dlg.m_lSelectedCategories.AddTail( m_aRequiredCategories[iCategory] );
  324.    }
  325.  
  326.    nResult = dlg.DoModal();
  327.    if( nResult != IDOK )
  328.    {
  329.       return;
  330.    }
  331.  
  332.    m_aRequiredCategories.RemoveAll();
  333.    posCategory = dlg.m_lSelectedCategories.GetHeadPosition();
  334.    while( posCategory != NULL )
  335.    {
  336.       catid = dlg.m_lSelectedCategories.GetNext( posCategory );
  337.       m_aRequiredCategories.Add( catid );
  338.    }
  339.  
  340.    RefreshControlList();
  341. }
  342.  
  343. void CInsertControlDlg::OnControlsSelChange()
  344. {
  345.    int iItem;
  346.    POSITION posControl;
  347.    CString strServerPath;
  348.    CLSID clsid;
  349.    CDC dc;
  350.    CFont* pFont;
  351.    LPTSTR pszServerPath;
  352.    CRect rect;
  353.    CFont* pOldFont;
  354.  
  355.    iItem = m_lbControls.GetCurSel();
  356.    if( iItem != LB_ERR )
  357.    {
  358.       m_butOK.EnableWindow( TRUE );
  359.       posControl = POSITION( m_lbControls.GetItemDataPtr( iItem ) );
  360.       clsid = m_lControls.GetAt( posControl );
  361.       GetClassServerPath( clsid, strServerPath );
  362.  
  363.       dc.CreateCompatibleDC( NULL );
  364.  
  365.       pFont = m_staticServerPath.GetFont();
  366.       pOldFont = dc.SelectObject( pFont );
  367.  
  368.       // Workaround for SHLWAPI bug (in weird cases, PathCompactPath actually
  369.       // expands the pathname)
  370.       pszServerPath = strServerPath.GetBuffer( MAX_PATH+2 );
  371.       m_staticServerPath.GetWindowRect( &rect );
  372.       PathCompactPath( dc, pszServerPath, rect.Width() );
  373.       strServerPath.ReleaseBuffer();
  374.  
  375.       dc.SelectObject( pOldFont );
  376.  
  377.       m_staticServerPath.SetWindowText( strServerPath );
  378.    }
  379.    else
  380.    {
  381.       m_butOK.EnableWindow( FALSE );
  382.       m_staticServerPath.SetWindowText( NULL );
  383.    }
  384. }
  385.  
  386. void CInsertControlDlg::OnIgnoreRequiredCategories()
  387. {
  388.    RefreshControlList();
  389.  
  390.    if( m_butIgnoreRequiredCategories.GetCheck() )
  391.    {
  392.       m_butRequiredCategories.EnableWindow( FALSE );
  393.    }
  394.    else
  395.    {
  396.       m_butRequiredCategories.EnableWindow( TRUE );
  397.    }
  398. }
  399.  
  400. static DWORD rgmapCHID[] =
  401. {
  402.    IDC_CONTROLS, HIDC_CONTROLS,
  403.    IDC_IMPLEMENTEDCATEGORIES, HIDC_IMPLEMENTEDCATEGORIES,
  404.    IDC_REQUIREDCATEGORIES, HIDC_REQUIREDCATEGORIES,
  405.    IDC_IGNOREREQUIREDCATEGORIES, HIDC_IGNOREREQUIREDCATEGORIES,
  406.    0, 0
  407. };
  408.  
  409. BOOL CInsertControlDlg::OnHelpInfo( HELPINFO* pHelpInfo )
  410. {
  411.     return( ::WinHelp( HWND( pHelpInfo->hItemHandle ),
  412.       AfxGetApp()->m_pszHelpFilePath, HELP_WM_HELP, DWORD( LPVOID(
  413.       rgmapCHID ) ) ) );
  414. }
  415.  
  416. void CInsertControlDlg::OnContextMenu( CWnd* pWnd, CPoint /* point */ )
  417. {
  418.    ::WinHelp( HWND( *pWnd ), AfxGetApp()->m_pszHelpFilePath, HELP_CONTEXTMENU,
  419.       DWORD( LPVOID( rgmapCHID ) ) );
  420. }
  421.