SPPPropertyGridItemSize.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 "SPPropertyGridItemSize.h"
  13. #include "SPPropertyGridItemNumber.h"
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CSPPropertyGridItemSize::CSPPropertyGridItemSizeWidth
  21. class CSPPropertyGridItemSize::CSPPropertyGridItemSizeWidth : public CSPPropertyGridItemNumber
  22. {
  23. public:
  24. CSPPropertyGridItemSizeWidth( CString strCaption ) : CSPPropertyGridItemNumber( strCaption )
  25. {
  26. }
  27. virtual void OnValueChanged( CString strValue )
  28. {
  29. ( ( CSPPropertyGridItemSize * ) m_pParent )->SetWidth( strValue );
  30. }
  31. };
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CSPPropertyGridItemSize::CSPPropertyGridItemSizeHeight
  34. class CSPPropertyGridItemSize::CSPPropertyGridItemSizeHeight : public CSPPropertyGridItemNumber
  35. {
  36. public:
  37. CSPPropertyGridItemSizeHeight( CString strCaption ) : CSPPropertyGridItemNumber( strCaption )
  38. {
  39. }
  40. virtual void OnValueChanged( CString strValue )
  41. {
  42. ( ( CSPPropertyGridItemSize * ) m_pParent )->SetHeight( strValue );
  43. }
  44. };
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CSPPropertyGridItemSize
  47. IMPLEMENT_DYNAMIC( CSPPropertyGridItemSize , CSPPropertyGridItem )
  48. CSPPropertyGridItemSize::CSPPropertyGridItemSize( CString strCaption , CSize size , CSize * pBindSize ) : CSPPropertyGridItem( strCaption )
  49. {
  50. m_szValue = size;
  51. BindToSize( pBindSize );
  52. m_strValue = SizeToString( size );
  53. }
  54. CSPPropertyGridItemSize::CSPPropertyGridItemSize( UINT nID , CSize size , CSize * pBindSize ) : CSPPropertyGridItem( nID )
  55. {
  56. m_szValue = size;
  57. BindToSize( pBindSize );
  58. m_strValue = SizeToString( size );
  59. }
  60. CSPPropertyGridItemSize::~CSPPropertyGridItemSize()
  61. {
  62. }
  63. /////////////////////////////////////////////////////////////////////////////
  64. //
  65. void CSPPropertyGridItemSize::OnAddChildItem()
  66. {
  67. m_itemWidth = ( CSPPropertyGridItemSizeWidth * ) AddChildItem( new CSPPropertyGridItemSizeWidth( _T( "Width" ) ) );
  68. m_itemHeight = ( CSPPropertyGridItemSizeHeight * ) AddChildItem( new CSPPropertyGridItemSizeHeight( _T( "Height" ) ) );
  69. UpdateChilds();
  70. }
  71. CString CSPPropertyGridItemSize::SizeToString( CSize size )
  72. {
  73. CString str;
  74. str.Format( _T( "%i; %i" ),size.cx,size.cy );
  75. return str;
  76. }
  77. CSize CSPPropertyGridItemSize::StringToSize( CString str )
  78. {
  79. CString strWidth, strHeight;
  80. AfxExtractSubString( strWidth,str,0,';' );
  81. AfxExtractSubString( strHeight,str,1,';' );
  82. return CSize( _ttoi( strWidth ),_ttoi( strHeight ) );
  83. }
  84. void CSPPropertyGridItemSize::SetValue( CString strValue )
  85. {
  86. SetSize( StringToSize( strValue ) );
  87. }
  88. void CSPPropertyGridItemSize::SetSize( CSize size )
  89. {
  90. m_szValue = size;
  91. if ( m_pBindSize )
  92. {
  93. *m_pBindSize = m_szValue;
  94. }
  95. CSPPropertyGridItem::SetValue( SizeToString( m_szValue ) );
  96. UpdateChilds();
  97. }
  98. void CSPPropertyGridItemSize::BindToSize( CSize * pBindSize )
  99. {
  100. m_pBindSize = pBindSize;
  101. if ( m_pBindSize )
  102. {
  103. *m_pBindSize = m_szValue;
  104. }
  105. }
  106. void CSPPropertyGridItemSize::OnBeforeInsert()
  107. {
  108. if ( m_pBindSize && *m_pBindSize != m_szValue )
  109. {
  110. SetSize( *m_pBindSize );
  111. }
  112. }
  113. void CSPPropertyGridItemSize::UpdateChilds()
  114. {
  115. m_itemWidth->SetNumber( m_szValue.cx );
  116. m_itemHeight->SetNumber( m_szValue.cy );
  117. }
  118. void CSPPropertyGridItemSize::SetWidth( CString strWidth )
  119. {
  120. OnValueChanged( SizeToString( CSize( _ttoi( strWidth ),m_szValue.cy ) ) );
  121. }
  122. void CSPPropertyGridItemSize::SetHeight( CString strHeight )
  123. {
  124. OnValueChanged( SizeToString( CSize( m_szValue.cx,_ttoi( strHeight ) ) ) );
  125. }