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

  1. // CAmbientPropertiesDlg.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. // CAmbientPropertiesDlg dialog
  16.  
  17.  
  18. CAmbientPropertiesDlg::CAmbientPropertiesDlg( CWnd* pParent,
  19.    CTestContainer98Doc* pDoc ) :
  20.    CDialog( CAmbientPropertiesDlg::IDD, pParent ),
  21.    m_pDoc( pDoc ),
  22.    m_tChanged( FALSE ),
  23.    m_dispidChanged( DISPID_UNKNOWN )
  24. {
  25.    ASSERT( m_pDoc != NULL );
  26.     //{{AFX_DATA_INIT(CAmbientPropertiesDlg)
  27.     //}}AFX_DATA_INIT
  28. }
  29.  
  30.  
  31. void CAmbientPropertiesDlg::DoDataExchange(CDataExchange* pDX)
  32. {
  33.     CDialog::DoDataExchange(pDX);
  34.     //{{AFX_DATA_MAP(CAmbientPropertiesDlg)
  35.     DDX_Control(pDX, IDC_STATIC_PROPERTYVALUE, m_staticPropertyValue);
  36.     DDX_Control(pDX, IDC_DISPID, m_editDISPID);
  37.     DDX_Control(pDX, IDC_CHOOSEFONT_AMBIENT, m_butChooseFont);
  38.     DDX_Control(pDX, IDC_CHOOSECOLOR_AMBIENT, m_butChooseColor);
  39.     DDX_Control(pDX, IDC_PROPNAME, m_cboxPropName);
  40.     DDX_Control(pDX, IDC_SETVALUE_AMBIENT, m_butSetValue);
  41.     DDX_Control(pDX, IDC_PROPVALUE, m_editPropValue);
  42.     DDX_Control(pDX, IDC_PROPTYPE, m_cboxPropType);
  43.     DDX_Control(pDX, IDC_ENABLED, m_checkEnabled);
  44.     //}}AFX_DATA_MAP
  45. }
  46.  
  47.  
  48. BEGIN_MESSAGE_MAP(CAmbientPropertiesDlg, CDialog)
  49.     //{{AFX_MSG_MAP(CAmbientPropertiesDlg)
  50.     ON_CBN_SELCHANGE(IDC_PROPNAME, OnPropNameSelChange)
  51.     ON_CBN_SELCHANGE(IDC_PROPTYPE, OnPropTypeSelChange)
  52.     ON_BN_CLICKED(IDC_SETVALUE_AMBIENT, OnSetValue)
  53.     ON_BN_CLICKED(IDC_ENABLED, OnEnabled)
  54.     ON_BN_CLICKED(IDC_CHOOSECOLOR_AMBIENT, OnChooseColor)
  55.     ON_BN_CLICKED(IDC_CHOOSEFONT_AMBIENT, OnChooseFont)
  56.     ON_BN_CLICKED(IDC_NEWPROPERTY, OnNewProperty)
  57.     ON_EN_SETFOCUS(IDC_PROPVALUE, OnPropValueSetFocus)
  58.     ON_WM_HELPINFO()
  59.     ON_WM_CONTEXTMENU()
  60.     //}}AFX_MSG_MAP
  61. END_MESSAGE_MAP()
  62.  
  63. /////////////////////////////////////////////////////////////////////////////
  64. // CAmbientPropertiesDlg message handlers
  65.  
  66. BOOL CAmbientPropertiesDlg::OnInitDialog()
  67. {
  68.    int iProp;
  69.    int iType;
  70.    int iItem;
  71.  
  72.     CDialog::OnInitDialog();
  73.  
  74.    // Fill in the available ambient properties
  75.    for( iProp = 0; iProp < m_pDoc->GetNumAmbientProperties(); iProp++ )
  76.    {
  77.       iItem = m_cboxPropName.AddString( m_pDoc->GetAmbientProperty(
  78.          iProp )->GetName() );
  79.       m_cboxPropName.SetItemData( iItem, iProp );
  80.    }
  81.  
  82.    for( iType = 0; iType < NUM_VARTYPES; iType++ )
  83.    {
  84.       iItem = m_cboxPropType.AddString( VTIToString( iType ) );
  85.       m_cboxPropType.SetItemData( iItem, iType );
  86.    }
  87.  
  88.    m_cboxPropName.SetCurSel( 0 );
  89.    OnPropNameSelChange();
  90.  
  91.    return( TRUE );  // return TRUE unless you set the focus to a control
  92. }
  93.  
  94. void CAmbientPropertiesDlg::OnPropNameSelChange()
  95. {
  96.    USES_CONVERSION;
  97.    int iProp;
  98.    int iItem;
  99.    CString strDisplay;
  100.  
  101.    iItem = m_cboxPropName.GetCurSel();
  102.    ASSERT( iItem != CB_ERR );
  103.    iProp = m_cboxPropName.GetItemData( iItem );
  104.  
  105.    m_pProp = m_pDoc->GetAmbientProperty( iProp );
  106.  
  107.    strDisplay.Format( _T( "%d" ), m_pProp->GetID() );
  108.    m_editDISPID.SetWindowText( strDisplay );
  109.    m_editDISPID.SetReadOnly( m_pProp->IsStock() );
  110.  
  111. //   m_cboxPropType.SetCurSel( m_pProp->GetVTI() );
  112.    m_cboxPropType.SelectString( -1, VTIToString( m_pProp->GetVTI() ) );
  113.    TRY
  114.    {
  115.       strDisplay = VariantToString( m_pProp->GetValue() );
  116.       m_editPropValue.SetWindowText( strDisplay );
  117.    }
  118.    CATCH( COleException, e )
  119.    {
  120.       TRACE( "ChangeType() failed.\n" );
  121.    }
  122.    END_CATCH
  123.  
  124.    OnPropTypeSelChange();
  125.    if( m_pProp->IsEnabled() )
  126.    {
  127.       m_checkEnabled.SetCheck( 1 );
  128.       m_cboxPropType.EnableWindow( TRUE );
  129.       m_editPropValue.EnableWindow( TRUE );
  130.       m_butSetValue.EnableWindow( TRUE );
  131.       m_butChooseColor.EnableWindow( TRUE );
  132.       m_butChooseFont.EnableWindow( TRUE );
  133.    }
  134.    else
  135.    {
  136.       m_checkEnabled.SetCheck( 0 );
  137.       m_cboxPropType.EnableWindow( FALSE );
  138.       m_editPropValue.EnableWindow( FALSE );
  139.       m_butSetValue.EnableWindow( FALSE );
  140.       m_butChooseColor.EnableWindow( FALSE );
  141.       m_butChooseFont.EnableWindow( FALSE );
  142.    }
  143. }
  144.  
  145. void CAmbientPropertiesDlg::OnPropTypeSelChange()
  146. {
  147.    int iType;
  148.    int iItem;
  149.  
  150.    iItem = m_cboxPropType.GetCurSel();
  151.    ASSERT( iItem != CB_ERR );
  152.    iType = m_cboxPropType.GetItemData( iItem );
  153.  
  154.    switch( iType )
  155.    {
  156.    case VTI_FONT:
  157.       m_butChooseColor.ShowWindow( SW_HIDE );
  158.       m_staticPropertyValue.ShowWindow( SW_HIDE );
  159.       m_editPropValue.ShowWindow( SW_HIDE );
  160.       m_butSetValue.ShowWindow( SW_HIDE );
  161.       m_butChooseFont.ShowWindow( SW_NORMAL );
  162.       break;
  163.  
  164.    case VTI_COLOR:
  165.       m_butChooseFont.ShowWindow( SW_HIDE );
  166.       m_staticPropertyValue.ShowWindow( SW_HIDE );
  167.       m_editPropValue.ShowWindow( SW_HIDE );
  168.       m_butSetValue.ShowWindow( SW_HIDE );
  169.       m_butChooseColor.ShowWindow( SW_NORMAL );
  170.       break;
  171.  
  172.    default:
  173.       m_butChooseColor.ShowWindow( SW_HIDE );
  174.       m_butChooseFont.ShowWindow( SW_HIDE );
  175.       m_staticPropertyValue.ShowWindow( SW_NORMAL );
  176.       m_editPropValue.ShowWindow( SW_NORMAL );
  177.       m_butSetValue.ShowWindow( SW_NORMAL );
  178.       break;
  179.    }
  180. }
  181.  
  182. void CAmbientPropertiesDlg::OnSetValue()
  183. {
  184.    COleVariant varValue;
  185.    CString strValue;
  186.    int iType;
  187.    int iItem;
  188.  
  189.    m_editPropValue.GetWindowText( strValue );
  190.    varValue = strValue;
  191.  
  192.    TRY
  193.    {
  194.       iItem = m_cboxPropType.GetCurSel();
  195.       ASSERT( iItem != CB_ERR );
  196.       iType = m_cboxPropType.GetItemData( iItem );
  197.       varValue.ChangeType( VTIToVT( iType ) );
  198.       m_pProp->SetValue( varValue, iType );
  199.       OnPropertyChanged( m_pProp->GetID() );
  200.    }
  201.    CATCH( COleException, e )
  202.    {
  203.       TRACE( "ChangeType() failed.\n" );
  204.    }
  205.    END_CATCH
  206.  
  207.    m_editPropValue.SetFocus();
  208.    m_editPropValue.SetSel( 0, -1 );
  209. }
  210.  
  211. void CAmbientPropertiesDlg::OnEnabled()
  212. {
  213.    if( m_checkEnabled.GetCheck() )
  214.    {
  215.       m_pProp->Enable( TRUE );
  216.       OnPropNameSelChange();
  217.    }
  218.    else
  219.    {
  220.       m_pProp->Enable( FALSE );
  221.       OnPropNameSelChange();
  222.    }
  223. }
  224.  
  225. BOOL CAmbientPropertiesDlg::GetChangedPropertyID( DISPID* pdispid ) const
  226. {
  227.    if( m_tChanged )
  228.    {
  229.       *pdispid = m_dispidChanged;
  230.    }
  231.    else
  232.    {
  233.       *pdispid = DISPID_UNKNOWN;
  234.    }
  235.  
  236.    return( m_tChanged );
  237. }
  238.  
  239. void CAmbientPropertiesDlg::OnChooseColor()
  240. {
  241.    int nResult;
  242.    COleVariant varColor;
  243.  
  244.    varColor = m_pProp->GetValue();
  245.    try
  246.    {
  247.       varColor.ChangeType( VT_COLOR );
  248.    }
  249.    catch( CException* pException )
  250.    {
  251.       varColor = COleVariant( long( 0 ), VT_COLOR );
  252.  
  253.      pException->Delete();
  254.    }
  255.  
  256.    ASSERT( varColor.vt == VT_COLOR );
  257.  
  258.    CColorDialog dlg( varColor.lVal );
  259.  
  260.    nResult = dlg.DoModal();
  261.    if( nResult != IDOK )
  262.    {
  263.       return;
  264.    }
  265.  
  266.    varColor = COleVariant( long( dlg.GetColor() ), VT_COLOR );
  267.    m_pProp->SetValue( varColor );
  268.    OnPropertyChanged( m_pProp->GetID() );
  269. }
  270.  
  271. void CAmbientPropertiesDlg::OnChooseFont()
  272. {
  273.    USES_CONVERSION;
  274.    int nResult;
  275.    IFontDispPtr pFontDisp;
  276.    IFontPtr pFont;
  277.    FONTDESC desc;
  278.    HRESULT hResult;
  279.    COleVariant varFont;
  280.    CFont font;
  281.    HFONT hFont;
  282.    LOGFONT logfont;
  283.    LOGFONT* pLogFont;
  284.    BOOL tSuccess;
  285.  
  286.    hFont = NULL;
  287.    varFont = m_pProp->GetValue();
  288.    if( varFont.vt == VT_FONT )
  289.    {
  290.       pFont = varFont.pdispVal;
  291.       if( pFont != NULL )
  292.       {
  293.          hResult = pFont->get_hFont( &hFont );
  294.          if( FAILED( hResult ) )
  295.          {
  296.             hFont = NULL;
  297.          }
  298.          pFont.Release();
  299.       }
  300.    }
  301.  
  302.    if( hFont != NULL )
  303.    {
  304.       font.Attach( hFont );
  305.       tSuccess = font.GetLogFont( &logfont );
  306.       font.Detach();
  307.       if( !tSuccess )
  308.       {
  309.          hFont = NULL;
  310.       }
  311.    }
  312.  
  313.    if( hFont != NULL )
  314.    {
  315.       pLogFont = &logfont;
  316.    }
  317.    else
  318.    {
  319.       pLogFont = NULL;
  320.    }
  321.  
  322.    CFontDialog dlg( pLogFont );
  323.  
  324.    nResult = dlg.DoModal();
  325.    if( nResult != IDOK )
  326.    {
  327.       return;
  328.    }
  329.  
  330.    memset( &desc, 0, sizeof( desc ) );
  331.    desc.cbSizeofstruct = sizeof( desc );
  332.    desc.lpstrName = T2OLE( dlg.m_cf.lpLogFont->lfFaceName );
  333.    desc.cySize = COleCurrency( dlg.m_cf.iPointSize/10, 1000*
  334.       (dlg.m_cf.iPointSize%10) );
  335.    desc.sWeight = short( dlg.m_cf.lpLogFont->lfWeight );
  336.    desc.sCharset = dlg.m_cf.lpLogFont->lfCharSet;
  337.    desc.fItalic = dlg.m_cf.lpLogFont->lfItalic;
  338.    desc.fUnderline = dlg.m_cf.lpLogFont->lfUnderline;
  339.    desc.fStrikethrough = dlg.m_cf.lpLogFont->lfStrikeOut;
  340.  
  341.    hResult = OleCreateFontIndirect( &desc, IID_IFontDisp, (void**)&pFontDisp );
  342.    if( FAILED( hResult ) )
  343.    {
  344.       TRACE( "OleCreateFontIndirect() failed\n" );
  345.       return;
  346.    }
  347.  
  348.    varFont.vt = VT_DISPATCH;
  349.    varFont.pdispVal = pFontDisp;
  350.    varFont.pdispVal->AddRef();
  351.    m_pProp->SetValue( varFont );
  352.    OnPropertyChanged( m_pProp->GetID() );
  353. }
  354.  
  355. void CAmbientPropertiesDlg::OnPropertyChanged( DISPID dispid )
  356. {
  357.    if( m_tChanged == FALSE )
  358.    {
  359.       // This is the first property we changed.
  360.       m_tChanged = TRUE;
  361.       m_dispidChanged = dispid;
  362.    }
  363.    else
  364.    {
  365.       if( m_dispidChanged != dispid )
  366.       {
  367.          // We've already changed a different property, so we'll have to do
  368.          // a bulk notify.
  369.          m_dispidChanged = DISPID_UNKNOWN;
  370.       }
  371.    }
  372. }
  373.  
  374. void CAmbientPropertiesDlg::OnNewProperty()
  375. {
  376.    COleVariant var;
  377.    int iProp;
  378.    int iItem;
  379.    CNewAmbientPropertyDlg dlg;
  380.    int nResult;
  381.  
  382.    dlg.m_strName.LoadString( IDS_NEWPROPERTY );
  383.    dlg.m_dispid = DISPID_UNKNOWN;
  384.  
  385.    nResult = dlg.DoModal();
  386.    if( nResult != IDOK )
  387.    {
  388.       return;
  389.    }
  390.  
  391.    var = COleVariant( VARIANT_FALSE, VT_BOOL );
  392.    try
  393.    {
  394.       iProp = m_pDoc->AddAmbientProperty( dlg.m_dispid, dlg.m_strName, var,
  395.          VTI_BOOL );
  396.       iItem = m_cboxPropName.AddString( dlg.m_strName );
  397.       m_cboxPropName.SetItemData( iItem, iProp );
  398.       m_cboxPropName.SetCurSel( iItem );
  399.       OnPropertyChanged( dlg.m_dispid );
  400.       OnPropNameSelChange();
  401.    }
  402.    catch( ... )
  403.    {
  404.       TRACE( "Failed to add ambient property\n" );
  405.    }
  406. }
  407.  
  408. void CAmbientPropertiesDlg::OnPropValueSetFocus()
  409. {
  410.    // The ParamValue edit box just got the keyboard focus.  The user should be
  411.    // able to set the value to the contents of the edit box just by hitting
  412.    // ENTER.
  413.    SetDefID( IDC_SETVALUE_AMBIENT );
  414. }
  415.  
  416. static DWORD rgmapCHID[] =
  417. {
  418.    IDC_PROPNAME, HIDC_PROPNAME,
  419.    IDC_DISPID, HIDC_DISPID,
  420.    IDC_ENABLED, HIDC_ENABLED,
  421.    IDC_CHOOSECOLOR_AMBIENT, HIDC_CHOOSECOLOR_AMBIENT,
  422.    IDC_CHOOSEFONT_AMBIENT, HIDC_CHOOSEFONT_AMBIENT,
  423.    IDC_PROPTYPE, HIDC_PROPTYPE,
  424.    IDC_SETVALUE_AMBIENT, HIDC_SETVALUE_AMBIENT,
  425.    IDC_PROPVALUE, HIDC_PROPVALUE,
  426.    IDC_NEWPROPERTY, HIDC_NEWPROPERTY,
  427.    IDC_STATIC_PROPERTYVALUE, DWORD( -1 ),
  428.    0, 0
  429. };
  430.  
  431. BOOL CAmbientPropertiesDlg::OnHelpInfo( HELPINFO* pHelpInfo )
  432. {
  433.    return( ::WinHelp( HWND( pHelpInfo->hItemHandle ),
  434.       AfxGetApp()->m_pszHelpFilePath, HELP_WM_HELP, DWORD( LPVOID(
  435.       rgmapCHID ) ) ) );
  436. }
  437.  
  438. void CAmbientPropertiesDlg::OnContextMenu( CWnd* pWnd, CPoint /* point */ )
  439. {
  440.     ::WinHelp( HWND( *pWnd ), AfxGetApp()->m_pszHelpFilePath, HELP_CONTEXTMENU,
  441.       DWORD( LPVOID( rgmapCHID ) ) );
  442. }
  443.