| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- /********************************************
- ** 工作室:S&P工作室
- ** 作者 :张东斌
- ** 日期 :2007年6月
- *********************************************/
- #include "StdAfx.h"
- #include "SPVC80Helpers.h"
- #include "SPPropertyGridInplaceEdit.h"
- #include "SPPropertyGridInplaceButton.h"
- #include "SPPropertyGridInplaceList.h"
- #include "SPPropertyGridItem.h"
- #include "SPPropertyGriditembool.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CSPPropertyGridItemBool
- IMPLEMENT_DYNAMIC( CSPPropertyGridItemBool , CSPPropertyGridItem )
- CSPPropertyGridItemBool::CSPPropertyGridItemBool( CString strCaption , BOOL bValue , BOOL * pBindBool ) : CSPPropertyGridItem( strCaption ) , m_strTrueText( _T( "True" ) ) , m_strFalseText( _T( "False" ) )
- {
- m_pBindBool = pBindBool;
- _Init( bValue );
- }
- CSPPropertyGridItemBool::CSPPropertyGridItemBool( UINT nID , BOOL bValue , BOOL * pBindBool ) : CSPPropertyGridItem( nID ) , m_strTrueText( _T( "True" ) ) , m_strFalseText( _T( "False" ) )
- {
- m_pBindBool = pBindBool;
- _Init( bValue );
- }
- CSPPropertyGridItemBool::~CSPPropertyGridItemBool()
- {
- }
- /////////////////////////////////////////////////////////////////////////////
- //
- void CSPPropertyGridItemBool::_Init( BOOL bValue )
- {
- SetBool( bValue );
- m_nFlags = SPGridItemHasComboButton | SPGridItemHasEdit;
- m_pConstraints->AddConstraint( m_strTrueText );
- m_pConstraints->AddConstraint( m_strFalseText );
- SetConstraintEdit( TRUE );
- }
- void CSPPropertyGridItemBool::SetValue( CString strValue )
- {
- SetBool( strValue.CompareNoCase( m_strTrueText ) == 0 );
- }
- void CSPPropertyGridItemBool::SetBool( BOOL bValue )
- {
- m_bValue = bValue;
- if ( m_pBindBool )
- {
- *m_pBindBool = bValue;
- }
- CSPPropertyGridItem::SetValue( bValue ? m_strTrueText : m_strFalseText );
- }
- void CSPPropertyGridItemBool::BindToBool( BOOL * pBindBool )
- {
- m_pBindBool = pBindBool;
- if ( m_pBindBool )
- {
- *m_pBindBool = m_bValue;
- }
- }
- void CSPPropertyGridItemBool::OnBeforeInsert()
- {
- if ( m_pBindBool && *m_pBindBool != m_bValue )
- {
- SetBool( *m_pBindBool );
- }
- }
- BOOL CSPPropertyGridItemBool::SetValueText( CString & strValueText , CString strNewText )
- {
- // see if the value exists.
- int iIndex = m_pConstraints->FindConstraint( strValueText );
- if ( iIndex != -1 )
- {
- // if this is the current value change it as well.
- if ( GetValue() == strValueText )
- {
- CSPPropertyGridItem::SetValue( strNewText );
- }
- // update the value.
- strValueText = strNewText;
- m_pConstraints->GetConstraintAt( iIndex )->m_strConstraint = strValueText;
- return TRUE;
- }
- return FALSE;
- }
- BOOL CSPPropertyGridItemBool::SetTrueFalseText( CString strTrueText , CString strFalseText )
- {
- // update the "True" value text
- if ( !SetValueText( m_strTrueText,strTrueText ) )
- {
- return FALSE;
- }
- // update the "False" value text
- if ( !SetValueText( m_strFalseText,strFalseText ) )
- {
- return FALSE;
- }
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CSPPropertyGridItemEnum
- IMPLEMENT_DYNAMIC( CSPPropertyGridItemEnum , CSPPropertyGridItem )
- CSPPropertyGridItemEnum::CSPPropertyGridItemEnum( CString strCaption , int nValue , int * pBindEnum ) : CSPPropertyGridItem( strCaption )
- {
- m_pBindEnum = pBindEnum;
- _Init( nValue );
- }
- CSPPropertyGridItemEnum::CSPPropertyGridItemEnum( UINT nID , int nValue , int * pBindEnum ) : CSPPropertyGridItem( nID )
- {
- m_pBindEnum = pBindEnum;
- _Init( nValue );
- }
- CSPPropertyGridItemEnum::~CSPPropertyGridItemEnum()
- {
- }
- void CSPPropertyGridItemEnum::_Init( int nValue )
- {
- SetEnum( nValue );
- m_nFlags = SPGridItemHasComboButton | SPGridItemHasEdit;
- SetConstraintEdit( TRUE );
- }
- void CSPPropertyGridItemEnum::SetValue( CString strValue )
- {
- int nIndex = m_pConstraints->FindConstraint( strValue );
- ASSERT( nIndex >= 0 );
- if ( nIndex >= 0 )
- {
- SetEnum( m_pConstraints->GetConstraintAt( nIndex ) );
- }
- }
- void CSPPropertyGridItemEnum::SetEnum( int nValue )
- {
- m_nValue = nValue;
- if ( m_pBindEnum )
- {
- *m_pBindEnum = nValue;
- }
- int nIndex = m_pConstraints->FindConstraint( nValue );
- CSPPropertyGridItem::SetValue( m_pConstraints->GetAt( nIndex ) );
- }
- void CSPPropertyGridItemEnum::SetEnum( CSPPropertyGridItemConstraint * pContraint )
- {
- m_nValue = ( int ) pContraint->m_dwData;
- if ( m_pBindEnum )
- {
- *m_pBindEnum = m_nValue;
- }
- CSPPropertyGridItem::SetValue( pContraint->m_strConstraint );
- }
- void CSPPropertyGridItemEnum::BindToEnum( int * pBindEnum )
- {
- m_pBindEnum = pBindEnum;
- if ( m_pBindEnum )
- {
- *m_pBindEnum = m_nValue;
- }
- }
- void CSPPropertyGridItemEnum::OnBeforeInsert()
- {
- if ( m_pBindEnum && *m_pBindEnum != m_nValue )
- {
- SetEnum( *m_pBindEnum );
- }
- }
- void CSPPropertyGridItemEnum::OnConstraintsChanged()
- {
- if ( m_strValue.IsEmpty() )
- {
- int nIndex = m_pConstraints->FindConstraint( m_nValue );
- if ( nIndex != -1 )
- m_strValue = m_pConstraints->GetAt( nIndex );
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CSPPropertyGridItemFlags
- class CSPPropertyGridItemFlags::CSPPropertyGridItemFlag : public CSPPropertyGridItemBool
- {
- public:
- CSPPropertyGridItemFlag( CString strCaption , DWORD dwFlag ) : CSPPropertyGridItemBool( strCaption ) , m_dwFlag( dwFlag )
- {
- }
- void OnValueChanged( CString strValue );
- DWORD m_dwFlag;
- };
- void CSPPropertyGridItemFlags::CSPPropertyGridItemFlag::OnValueChanged( CString strValue )
- {
- SetValue( strValue );
- CSPPropertyGridItemFlags * pParent = DYNAMIC_DOWNCAST( CSPPropertyGridItemFlags,m_pParent );
- ASSERT( pParent );
- if ( GetBool() )
- {
- pParent->m_nValue |= m_dwFlag;
- }
- else
- {
- pParent->m_nValue &= ~m_dwFlag;
- }
- pParent->OnValueChanged( pParent->GetFlagsString() );
- }
- IMPLEMENT_DYNAMIC( CSPPropertyGridItemFlags , CSPPropertyGridItem )
- CSPPropertyGridItemFlags::CSPPropertyGridItemFlags( CString strCaption , int nValue , int * pBindFlags ) : CSPPropertyGridItem( strCaption )
- {
- m_pBindFlags = pBindFlags;
- _Init( nValue );
- }
- CSPPropertyGridItemFlags::CSPPropertyGridItemFlags( UINT nID , int nValue , int * pBindFlags ) : CSPPropertyGridItem( nID )
- {
- m_pBindFlags = pBindFlags;
- _Init( nValue );
- }
- CSPPropertyGridItemFlags::~CSPPropertyGridItemFlags()
- {
- }
- void CSPPropertyGridItemFlags::_Init( int nValue )
- {
- SetFlags( nValue );
- m_nFlags = SPGridItemHasEdit;
- }
- AFX_INLINE BOOL HasFlag( CString strValue , CString strFlag )
- {
- strFlag.MakeLower();
- int nIndex = strValue.Find( strFlag );
- if ( nIndex == -1 )
- {
- return FALSE;
- }
- TCHAR chLast = nIndex + strFlag.GetLength() == strValue.GetLength() ? _T( ' ' ) : strValue[strFlag.GetLength() + nIndex];
- TCHAR chFirst = nIndex == 0 ? _T( ' ' ) : strValue[nIndex - 1];
- return ( chLast == ' ' || chLast == ',' || chLast == ';' || chLast == ']' ) && ( chFirst == ' ' || chFirst == ',' || chFirst == ';' || chFirst == '[' );
- }
- void CSPPropertyGridItemFlags::SetValue( CString strValue )
- {
- int nValue = 0;
- strValue.MakeLower();
- CSPPropertyGridItemConstraints *pConstraints = GetConstraints();
- for ( int i = 0; i < pConstraints->GetCount(); i++ )
- {
- if ( HasFlag( strValue,pConstraints->GetAt( i ) ) )
- {
- nValue |= pConstraints->GetConstraintAt( i )->m_dwData;
- }
- }
- SetFlags( nValue );
- }
- void CSPPropertyGridItemFlags::SetFlags( int nValue )
- {
- m_nValue = nValue;
- if ( m_pBindFlags )
- {
- *m_pBindFlags = nValue;
- }
- UpdateChilds();
- CSPPropertyGridItem::SetValue( GetFlagsString() );
- }
- void CSPPropertyGridItemFlags::BindToFlags( int * pBindFlags )
- {
- m_pBindFlags = pBindFlags;
- if ( m_pBindFlags )
- {
- *m_pBindFlags = m_nValue;
- }
- }
- CString CSPPropertyGridItemFlags::GetFlagsString()
- {
- CString str;
- CSPPropertyGridItemConstraints *pConstraints = GetConstraints();
- int nValue = 0;
- for ( int i = 0; i < pConstraints->GetCount(); i++ )
- {
- CSPPropertyGridItemConstraint * pConstraint = pConstraints->GetConstraintAt( i );
- if ( ( nValue & pConstraint->m_dwData ) == pConstraint->m_dwData )
- continue;
- if ( ( m_nValue & pConstraint->m_dwData ) == pConstraint->m_dwData )
- {
- str += ( str.IsEmpty() ? _T( "" ) : _T( ";" ) ) + pConstraint->m_strConstraint;
- nValue |= pConstraint->m_dwData;
- }
- }
- return _T( "[" ) + str + _T( "]" );
- }
- void CSPPropertyGridItemFlags::UpdateChilds()
- {
- CSPPropertyGridItems * pItems = GetChilds();
- for ( int i = 0; i < pItems->GetCount(); i++ )
- {
- CSPPropertyGridItemFlag * pItem = ( CSPPropertyGridItemFlag * ) pItems->GetAt( i );
- pItem->SetBool( ( m_nValue & pItem->m_dwFlag ) == pItem->m_dwFlag );
- }
- }
- void CSPPropertyGridItemFlags::OnBeforeInsert()
- {
- if ( m_pBindFlags && *m_pBindFlags != m_nValue )
- {
- SetFlags( *m_pBindFlags );
- }
- }
- void CSPPropertyGridItemFlags::OnConstraintsChanged()
- {
- GetChilds()->Clear();
- CSPPropertyGridItemConstraints *pConstraints = GetConstraints();
- for ( int i = 0; i < pConstraints->GetCount(); i++ )
- {
- AddChildItem( new CSPPropertyGridItemFlag( pConstraints->GetAt( i ),( int ) pConstraints->GetConstraintAt( i )->m_dwData ) );
- }
- UpdateChilds();
- m_strValue = GetFlagsString();
- }
|