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

  1. // ComponentCategoriesDlg.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. // CComponentCategoriesDlg dialog
  16.  
  17.  
  18. CComponentCategoriesDlg::CComponentCategoriesDlg( UINT idCaption,
  19.    CWnd* pParent ) :
  20.    CDialog( CComponentCategoriesDlg::IDD, pParent ),
  21.    m_idCaption( idCaption )
  22. {
  23.     //{{AFX_DATA_INIT(CComponentCategoriesDlg)
  24.         // NOTE: the ClassWizard will add member initialization here
  25.     //}}AFX_DATA_INIT
  26. }
  27.  
  28.  
  29. void CComponentCategoriesDlg::DoDataExchange(CDataExchange* pDX)
  30. {
  31.    int iItem;
  32.    int iCategory;
  33.    POSITION posSelectedCategory;
  34.    CATID catid;
  35.  
  36.     CDialog::DoDataExchange(pDX);
  37.     //{{AFX_DATA_MAP(CComponentCategoriesDlg)
  38.     DDX_Control(pDX, IDC_CATEGORIES, m_lbCategories);
  39.     //}}AFX_DATA_MAP
  40.  
  41.    if( pDX->m_bSaveAndValidate )
  42.    {
  43.       m_lSelectedCategories.RemoveAll();
  44.       for( iItem = 0; iItem < m_lbCategories.GetCount(); iItem++ )
  45.       {
  46.          if( m_lbCategories.GetCheck( iItem ) )
  47.          {
  48.             iCategory = m_lbCategories.GetItemData( iItem );
  49.             m_lSelectedCategories.AddTail( m_aAvailableCategories[iCategory] );
  50.          }
  51.       }
  52.    }
  53.    else
  54.    {
  55.       posSelectedCategory = m_lSelectedCategories.GetHeadPosition();
  56.       while( posSelectedCategory != NULL )
  57.       {
  58.          catid = m_lSelectedCategories.GetNext( posSelectedCategory );
  59.          iCategory = 0;
  60.          while( (iCategory < m_aAvailableCategories.GetSize()) && !IsEqualGUID(
  61.             m_aAvailableCategories[iCategory], catid ) )
  62.          {
  63.             iCategory++;
  64.          }
  65.          if( iCategory < m_aAvailableCategories.GetSize() )
  66.          {
  67.             // We found the category
  68.             iItem = 0;
  69.             while( int( m_lbCategories.GetItemData( iItem ) ) != iCategory )
  70.             {
  71.                iItem++;
  72.                ASSERT( iItem < m_lbCategories.GetCount() );
  73.             }
  74.             m_lbCategories.SetCheck( iItem, 1 );
  75.          }
  76.       }
  77.    }
  78. }
  79.  
  80.  
  81. BEGIN_MESSAGE_MAP(CComponentCategoriesDlg, CDialog)
  82.     //{{AFX_MSG_MAP(CComponentCategoriesDlg)
  83.     ON_BN_CLICKED(IDC_SELECTALL, OnSelectAll)
  84.     ON_WM_HELPINFO()
  85.     ON_WM_CONTEXTMENU()
  86.     //}}AFX_MSG_MAP
  87. END_MESSAGE_MAP()
  88.  
  89. /////////////////////////////////////////////////////////////////////////////
  90. // CComponentCategoriesDlg message handlers
  91.  
  92. BOOL CComponentCategoriesDlg::OnInitDialog()
  93. {
  94.    IEnumCATEGORYINFOPtr pEnum;
  95.    HRESULT hResult;
  96.    BOOL tDone;
  97.    CATEGORYINFO info;
  98.    CString strCategory;
  99.    CString strCaption;
  100.    int iItem;
  101.    int iCategory;
  102.  
  103.     CDialog::OnInitDialog();
  104.  
  105.    strCaption.LoadString( m_idCaption );
  106.    SetWindowText( strCaption );
  107.  
  108.    hResult = m_pCatInfo.CreateInstance( CLSID_StdComponentCategoriesMgr, NULL,
  109.       CLSCTX_INPROC_SERVER );
  110.    if( FAILED( hResult ) )
  111.    {
  112.       TRACE( "Failed to create category manager.\n" );
  113.       return( TRUE );
  114.    }
  115.  
  116.    strCaption.LoadString( m_idCaption );
  117.    SetWindowText( strCaption );
  118.  
  119.    hResult = m_pCatInfo->EnumCategories( GetUserDefaultLCID(), &pEnum );
  120.    if( FAILED( hResult ) )
  121.    {
  122.       TRACE( "Failed to enumerate categories.\n" );
  123.       return( TRUE );
  124.    }
  125.  
  126.    tDone = FALSE;
  127.    while( !tDone )
  128.    {
  129.       hResult = pEnum->Next( 1, &info, NULL );
  130.       if( hResult == S_OK )
  131.       {
  132.          strCategory = info.szDescription;
  133.          iItem = m_lbCategories.AddString( strCategory );
  134.          iCategory = m_aAvailableCategories.Add( info.catid );
  135.          m_lbCategories.SetItemData( iItem, iCategory );
  136.       }
  137.       else
  138.       {
  139.          tDone = TRUE;
  140.       }
  141.    }
  142.  
  143.    UpdateData( FALSE );
  144.  
  145.     return( TRUE );
  146. }
  147.  
  148. void CComponentCategoriesDlg::OnSelectAll()
  149. {
  150.    m_lbCategories.SelItemRange( TRUE, 0, m_lbCategories.GetCount() );
  151. }
  152.  
  153. static DWORD rgmapCHID[] =
  154. {
  155.    IDC_CATEGORIES, HIDC_CATEGORIES,
  156.    IDC_SELECTALL, HIDC_SELECTALL,
  157.    0, 0
  158. };
  159.  
  160. BOOL CComponentCategoriesDlg::OnHelpInfo( HELPINFO* pHelpInfo )
  161. {
  162.    return( ::WinHelp( HWND( pHelpInfo->hItemHandle ),
  163.       AfxGetApp()->m_pszHelpFilePath, HELP_WM_HELP, DWORD( LPVOID(
  164.       rgmapCHID ) ) ) );
  165. }
  166.  
  167. void CComponentCategoriesDlg::OnContextMenu( CWnd* pWnd, CPoint /* point */ )
  168. {
  169.    ::WinHelp( HWND( *pWnd ), AfxGetApp()->m_pszHelpFilePath, HELP_CONTEXTMENU,
  170.       DWORD( LPVOID( rgmapCHID ) ) );
  171. }
  172.