SPPPropertyGridItemBool.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /********************************************
  2. ** 工作室:S&P工作室
  3. ** 作者 :张东斌
  4. ** 日期 :2007年6月
  5. *********************************************/
  6. #include "StdAfx.h"
  7. #include "SPVC80Helpers.h"
  8. #include "SPPropertyGridInplaceEdit.h"
  9. #include "SPPropertyGridInplaceButton.h"
  10. #include "SPPropertyGridInplaceList.h"
  11. #include "SPPropertyGridItem.h"
  12. #include "SPPropertyGriditembool.h"
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CSPPropertyGridItemBool
  20. IMPLEMENT_DYNAMIC( CSPPropertyGridItemBool , CSPPropertyGridItem )
  21. CSPPropertyGridItemBool::CSPPropertyGridItemBool( CString strCaption , BOOL bValue , BOOL * pBindBool ) : CSPPropertyGridItem( strCaption ) , m_strTrueText( _T( "True" ) ) , m_strFalseText( _T( "False" ) )
  22. {
  23. m_pBindBool = pBindBool;
  24. _Init( bValue );
  25. }
  26. CSPPropertyGridItemBool::CSPPropertyGridItemBool( UINT nID , BOOL bValue , BOOL * pBindBool ) : CSPPropertyGridItem( nID ) , m_strTrueText( _T( "True" ) ) , m_strFalseText( _T( "False" ) )
  27. {
  28. m_pBindBool = pBindBool;
  29. _Init( bValue );
  30. }
  31. CSPPropertyGridItemBool::~CSPPropertyGridItemBool()
  32. {
  33. }
  34. /////////////////////////////////////////////////////////////////////////////
  35. //
  36. void CSPPropertyGridItemBool::_Init( BOOL bValue )
  37. {
  38. SetBool( bValue );
  39. m_nFlags = SPGridItemHasComboButton | SPGridItemHasEdit;
  40. m_pConstraints->AddConstraint( m_strTrueText );
  41. m_pConstraints->AddConstraint( m_strFalseText );
  42. SetConstraintEdit( TRUE );
  43. }
  44. void CSPPropertyGridItemBool::SetValue( CString strValue )
  45. {
  46. SetBool( strValue.CompareNoCase( m_strTrueText ) == 0 );
  47. }
  48. void CSPPropertyGridItemBool::SetBool( BOOL bValue )
  49. {
  50. m_bValue = bValue;
  51. if ( m_pBindBool )
  52. {
  53. *m_pBindBool = bValue;
  54. }
  55. CSPPropertyGridItem::SetValue( bValue ? m_strTrueText : m_strFalseText );
  56. }
  57. void CSPPropertyGridItemBool::BindToBool( BOOL * pBindBool )
  58. {
  59. m_pBindBool = pBindBool;
  60. if ( m_pBindBool )
  61. {
  62. *m_pBindBool = m_bValue;
  63. }
  64. }
  65. void CSPPropertyGridItemBool::OnBeforeInsert()
  66. {
  67. if ( m_pBindBool && *m_pBindBool != m_bValue )
  68. {
  69. SetBool( *m_pBindBool );
  70. }
  71. }
  72. BOOL CSPPropertyGridItemBool::SetValueText( CString & strValueText , CString strNewText )
  73. {
  74. // see if the value exists.
  75. int iIndex = m_pConstraints->FindConstraint( strValueText );
  76. if ( iIndex != -1 )
  77. {
  78. // if this is the current value change it as well.
  79. if ( GetValue() == strValueText )
  80. {
  81. CSPPropertyGridItem::SetValue( strNewText );
  82. }
  83. // update the value.
  84. strValueText = strNewText;
  85. m_pConstraints->GetConstraintAt( iIndex )->m_strConstraint = strValueText;
  86. return TRUE;
  87. }
  88. return FALSE;
  89. }
  90. BOOL CSPPropertyGridItemBool::SetTrueFalseText( CString strTrueText , CString strFalseText )
  91. {
  92. // update the "True" value text
  93. if ( !SetValueText( m_strTrueText,strTrueText ) )
  94. {
  95. return FALSE;
  96. }
  97. // update the "False" value text
  98. if ( !SetValueText( m_strFalseText,strFalseText ) )
  99. {
  100. return FALSE;
  101. }
  102. return TRUE;
  103. }
  104. /////////////////////////////////////////////////////////////////////////////
  105. // CSPPropertyGridItemEnum
  106. IMPLEMENT_DYNAMIC( CSPPropertyGridItemEnum , CSPPropertyGridItem )
  107. CSPPropertyGridItemEnum::CSPPropertyGridItemEnum( CString strCaption , int nValue , int * pBindEnum ) : CSPPropertyGridItem( strCaption )
  108. {
  109. m_pBindEnum = pBindEnum;
  110. _Init( nValue );
  111. }
  112. CSPPropertyGridItemEnum::CSPPropertyGridItemEnum( UINT nID , int nValue , int * pBindEnum ) : CSPPropertyGridItem( nID )
  113. {
  114. m_pBindEnum = pBindEnum;
  115. _Init( nValue );
  116. }
  117. CSPPropertyGridItemEnum::~CSPPropertyGridItemEnum()
  118. {
  119. }
  120. void CSPPropertyGridItemEnum::_Init( int nValue )
  121. {
  122. SetEnum( nValue );
  123. m_nFlags = SPGridItemHasComboButton | SPGridItemHasEdit;
  124. SetConstraintEdit( TRUE );
  125. }
  126. void CSPPropertyGridItemEnum::SetValue( CString strValue )
  127. {
  128. int nIndex = m_pConstraints->FindConstraint( strValue );
  129. ASSERT( nIndex >= 0 );
  130. if ( nIndex >= 0 )
  131. {
  132. SetEnum( m_pConstraints->GetConstraintAt( nIndex ) );
  133. }
  134. }
  135. void CSPPropertyGridItemEnum::SetEnum( int nValue )
  136. {
  137. m_nValue = nValue;
  138. if ( m_pBindEnum )
  139. {
  140. *m_pBindEnum = nValue;
  141. }
  142. int nIndex = m_pConstraints->FindConstraint( nValue );
  143. CSPPropertyGridItem::SetValue( m_pConstraints->GetAt( nIndex ) );
  144. }
  145. void CSPPropertyGridItemEnum::SetEnum( CSPPropertyGridItemConstraint * pContraint )
  146. {
  147. m_nValue = ( int ) pContraint->m_dwData;
  148. if ( m_pBindEnum )
  149. {
  150. *m_pBindEnum = m_nValue;
  151. }
  152. CSPPropertyGridItem::SetValue( pContraint->m_strConstraint );
  153. }
  154. void CSPPropertyGridItemEnum::BindToEnum( int * pBindEnum )
  155. {
  156. m_pBindEnum = pBindEnum;
  157. if ( m_pBindEnum )
  158. {
  159. *m_pBindEnum = m_nValue;
  160. }
  161. }
  162. void CSPPropertyGridItemEnum::OnBeforeInsert()
  163. {
  164. if ( m_pBindEnum && *m_pBindEnum != m_nValue )
  165. {
  166. SetEnum( *m_pBindEnum );
  167. }
  168. }
  169. void CSPPropertyGridItemEnum::OnConstraintsChanged()
  170. {
  171. if ( m_strValue.IsEmpty() )
  172. {
  173. int nIndex = m_pConstraints->FindConstraint( m_nValue );
  174. if ( nIndex != -1 )
  175. m_strValue = m_pConstraints->GetAt( nIndex );
  176. }
  177. }
  178. /////////////////////////////////////////////////////////////////////////////
  179. // CSPPropertyGridItemFlags
  180. class CSPPropertyGridItemFlags::CSPPropertyGridItemFlag : public CSPPropertyGridItemBool
  181. {
  182. public:
  183. CSPPropertyGridItemFlag( CString strCaption , DWORD dwFlag ) : CSPPropertyGridItemBool( strCaption ) , m_dwFlag( dwFlag )
  184. {
  185. }
  186. void OnValueChanged( CString strValue );
  187. DWORD m_dwFlag;
  188. };
  189. void CSPPropertyGridItemFlags::CSPPropertyGridItemFlag::OnValueChanged( CString strValue )
  190. {
  191. SetValue( strValue );
  192. CSPPropertyGridItemFlags * pParent = DYNAMIC_DOWNCAST( CSPPropertyGridItemFlags,m_pParent );
  193. ASSERT( pParent );
  194. if ( GetBool() )
  195. {
  196. pParent->m_nValue |= m_dwFlag;
  197. }
  198. else
  199. {
  200. pParent->m_nValue &= ~m_dwFlag;
  201. }
  202. pParent->OnValueChanged( pParent->GetFlagsString() );
  203. }
  204. IMPLEMENT_DYNAMIC( CSPPropertyGridItemFlags , CSPPropertyGridItem )
  205. CSPPropertyGridItemFlags::CSPPropertyGridItemFlags( CString strCaption , int nValue , int * pBindFlags ) : CSPPropertyGridItem( strCaption )
  206. {
  207. m_pBindFlags = pBindFlags;
  208. _Init( nValue );
  209. }
  210. CSPPropertyGridItemFlags::CSPPropertyGridItemFlags( UINT nID , int nValue , int * pBindFlags ) : CSPPropertyGridItem( nID )
  211. {
  212. m_pBindFlags = pBindFlags;
  213. _Init( nValue );
  214. }
  215. CSPPropertyGridItemFlags::~CSPPropertyGridItemFlags()
  216. {
  217. }
  218. void CSPPropertyGridItemFlags::_Init( int nValue )
  219. {
  220. SetFlags( nValue );
  221. m_nFlags = SPGridItemHasEdit;
  222. }
  223. AFX_INLINE BOOL HasFlag( CString strValue , CString strFlag )
  224. {
  225. strFlag.MakeLower();
  226. int nIndex = strValue.Find( strFlag );
  227. if ( nIndex == -1 )
  228. {
  229. return FALSE;
  230. }
  231. TCHAR chLast = nIndex + strFlag.GetLength() == strValue.GetLength() ? _T( ' ' ) : strValue[strFlag.GetLength() + nIndex];
  232. TCHAR chFirst = nIndex == 0 ? _T( ' ' ) : strValue[nIndex - 1];
  233. return ( chLast == ' ' || chLast == ',' || chLast == ';' || chLast == ']' ) && ( chFirst == ' ' || chFirst == ',' || chFirst == ';' || chFirst == '[' );
  234. }
  235. void CSPPropertyGridItemFlags::SetValue( CString strValue )
  236. {
  237. int nValue = 0;
  238. strValue.MakeLower();
  239. CSPPropertyGridItemConstraints *pConstraints = GetConstraints();
  240. for ( int i = 0; i < pConstraints->GetCount(); i++ )
  241. {
  242. if ( HasFlag( strValue,pConstraints->GetAt( i ) ) )
  243. {
  244. nValue |= pConstraints->GetConstraintAt( i )->m_dwData;
  245. }
  246. }
  247. SetFlags( nValue );
  248. }
  249. void CSPPropertyGridItemFlags::SetFlags( int nValue )
  250. {
  251. m_nValue = nValue;
  252. if ( m_pBindFlags )
  253. {
  254. *m_pBindFlags = nValue;
  255. }
  256. UpdateChilds();
  257. CSPPropertyGridItem::SetValue( GetFlagsString() );
  258. }
  259. void CSPPropertyGridItemFlags::BindToFlags( int * pBindFlags )
  260. {
  261. m_pBindFlags = pBindFlags;
  262. if ( m_pBindFlags )
  263. {
  264. *m_pBindFlags = m_nValue;
  265. }
  266. }
  267. CString CSPPropertyGridItemFlags::GetFlagsString()
  268. {
  269. CString str;
  270. CSPPropertyGridItemConstraints *pConstraints = GetConstraints();
  271. int nValue = 0;
  272. for ( int i = 0; i < pConstraints->GetCount(); i++ )
  273. {
  274. CSPPropertyGridItemConstraint * pConstraint = pConstraints->GetConstraintAt( i );
  275. if ( ( nValue & pConstraint->m_dwData ) == pConstraint->m_dwData )
  276. continue;
  277. if ( ( m_nValue & pConstraint->m_dwData ) == pConstraint->m_dwData )
  278. {
  279. str += ( str.IsEmpty() ? _T( "" ) : _T( ";" ) ) + pConstraint->m_strConstraint;
  280. nValue |= pConstraint->m_dwData;
  281. }
  282. }
  283. return _T( "[" ) + str + _T( "]" );
  284. }
  285. void CSPPropertyGridItemFlags::UpdateChilds()
  286. {
  287. CSPPropertyGridItems * pItems = GetChilds();
  288. for ( int i = 0; i < pItems->GetCount(); i++ )
  289. {
  290. CSPPropertyGridItemFlag * pItem = ( CSPPropertyGridItemFlag * ) pItems->GetAt( i );
  291. pItem->SetBool( ( m_nValue & pItem->m_dwFlag ) == pItem->m_dwFlag );
  292. }
  293. }
  294. void CSPPropertyGridItemFlags::OnBeforeInsert()
  295. {
  296. if ( m_pBindFlags && *m_pBindFlags != m_nValue )
  297. {
  298. SetFlags( *m_pBindFlags );
  299. }
  300. }
  301. void CSPPropertyGridItemFlags::OnConstraintsChanged()
  302. {
  303. GetChilds()->Clear();
  304. CSPPropertyGridItemConstraints *pConstraints = GetConstraints();
  305. for ( int i = 0; i < pConstraints->GetCount(); i++ )
  306. {
  307. AddChildItem( new CSPPropertyGridItemFlag( pConstraints->GetAt( i ),( int ) pConstraints->GetConstraintAt( i )->m_dwData ) );
  308. }
  309. UpdateChilds();
  310. m_strValue = GetFlagsString();
  311. }