Properties.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. GWEN
  3. Copyright (c) 2010 Facepunch Studios
  4. See license in Gwen.h
  5. */
  6. #include "Gwen/Gwen.h"
  7. #include "Gwen/Skin.h"
  8. #include "Gwen/Controls/Properties.h"
  9. #include "Gwen/Utility.h"
  10. using namespace Gwen;
  11. using namespace Gwen::Controls;
  12. GWEN_CONTROL_CONSTRUCTOR( Properties )
  13. {
  14. m_SplitterBar = new SplitterBar( this );
  15. m_SplitterBar->SetPos( 80, 0 );
  16. m_SplitterBar->SetCursor( Gwen::CursorType::SizeWE );
  17. m_SplitterBar->onDragged.Add( this, &Properties::OnSplitterMoved );
  18. m_SplitterBar->SetShouldDrawBackground( false );
  19. }
  20. void Properties::PostLayout( Gwen::Skin::Base* /*skin*/ )
  21. {
  22. m_SplitterBar->SetHeight( 0 );
  23. if ( SizeToChildren( false, true ) )
  24. {
  25. InvalidateParent();
  26. }
  27. m_SplitterBar->SetSize( 3, Height() );
  28. }
  29. void Properties::OnSplitterMoved( Controls::Base * /*control*/ )
  30. {
  31. InvalidateChildren();
  32. }
  33. int Properties::GetSplitWidth()
  34. {
  35. return m_SplitterBar->X();
  36. }
  37. PropertyRow* Properties::Add( const UnicodeString& text, const UnicodeString& value )
  38. {
  39. Property::Base* pProp = new Property::Text( this );
  40. pProp->SetPropertyValue( value );
  41. return Add( text, pProp );
  42. }
  43. PropertyRow* Properties::Add( const String& text, const String& value )
  44. {
  45. return Add( Gwen::Utility::StringToUnicode( text ), Gwen::Utility::StringToUnicode( value ) );
  46. }
  47. PropertyRow* Properties::Add( const UnicodeString& text, Property::Base* pProp )
  48. {
  49. PropertyRow* row = new PropertyRow( this );
  50. row->Dock( Pos::Top );
  51. row->GetLabel()->SetText( text );
  52. row->SetProperty( pProp );
  53. m_SplitterBar->BringToFront();
  54. return row;
  55. }
  56. PropertyRow* Properties::Add( const String& text, Property::Base* pProp )
  57. {
  58. return Add( Gwen::Utility::StringToUnicode( text ), pProp );
  59. }
  60. void Properties::Clear()
  61. {
  62. Base::List ChildListCopy = Children;
  63. for ( Base::List::iterator it = ChildListCopy.begin(); it != ChildListCopy.end(); ++it )
  64. {
  65. PropertyRow* row = (*it)->DynamicCastPropertyRow();
  66. if ( !row ) continue;
  67. row->DelayedDelete();
  68. }
  69. }
  70. GWEN_CONTROL_CONSTRUCTOR( PropertyRow )
  71. {
  72. m_Property = NULL;
  73. m_Label = new Label( this );
  74. m_Label->SetAlignment( Pos::CenterV | Pos::Left );
  75. m_Label->Dock( Pos::Left );
  76. m_Label->SetMargin( Margin( 2, 0, 0, 0 ) );
  77. SetHeight( 16 );
  78. }
  79. void PropertyRow::Render( Gwen::Skin::Base* skin )
  80. {
  81. skin->DrawPropertyRow( this, m_Label->Right(), m_Property->IsEditing() );
  82. }
  83. void PropertyRow::Layout( Gwen::Skin::Base* /*skin*/ )
  84. {
  85. Properties* pParent = GetParent()->DynamicCastProperties();
  86. if ( !pParent ) return;
  87. m_Label->SetWidth( pParent->GetSplitWidth() );
  88. }
  89. void PropertyRow::SetProperty( Property::Base* prop )
  90. {
  91. m_Property = prop;
  92. m_Property->SetParent( this );
  93. m_Property->Dock( Pos::Fill );
  94. m_Property->onChange.Add( this, &ThisClass::OnPropertyValueChanged );
  95. }
  96. void PropertyRow::OnPropertyValueChanged( Gwen::Controls::Base* /*control*/ )
  97. {
  98. onChange.Call( this );
  99. }