Label.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. GWEN
  3. Copyright (c) 2010 Facepunch Studios
  4. See license in Gwen.h
  5. */
  6. #include "Gwen/Gwen.h"
  7. #include "Gwen/Controls/Label.h"
  8. #include "Gwen/Utility.h"
  9. using namespace Gwen;
  10. using namespace Gwen::Controls;
  11. GWEN_CONTROL_CONSTRUCTOR( Label )
  12. {
  13. m_Text = new ControlsInternal::Text( this );
  14. m_Text->SetFont( GetSkin()->GetDefaultFont() );
  15. SetMouseInputEnabled( false );
  16. SetBounds( 0, 0, 100, 10 );
  17. SetAlignment( Gwen::Pos::Left | Gwen::Pos::Top );
  18. }
  19. void Label::Layout( Skin::Base* /*skin*/ )
  20. {
  21. int iAlign = m_iAlign;
  22. int x = m_rTextPadding.left + m_Padding.left;
  23. int y = m_rTextPadding.top + m_Padding.top;
  24. if ( iAlign & Pos::Right ) x = Width() - m_Text->Width() - m_rTextPadding.right - m_Padding.right;
  25. if ( iAlign & Pos::CenterH ) x = (m_rTextPadding.left + m_Padding.left) + ((Width() - m_Text->Width() ) * 0.5f) - m_rTextPadding.right - m_Padding.right;
  26. if ( iAlign & Pos::CenterV ) y = (m_rTextPadding.top + m_Padding.top) + ((Height() - m_Text->Height()) * 0.5f) - m_rTextPadding.bottom - m_Padding.bottom;
  27. if ( iAlign & Pos::Bottom ) y = Height() - m_Text->Height() - m_rTextPadding.bottom - m_Padding.bottom;
  28. m_Text->SetPos( x, y );
  29. }
  30. void Label::SetText( const UnicodeString& str, bool bDoEvents )
  31. {
  32. if ( m_Text->GetText() == str ) return;
  33. m_Text->SetString( str );
  34. Redraw();
  35. if ( bDoEvents )
  36. OnTextChanged();
  37. }
  38. void Label::SetText( const String& str, bool bDoEvents )
  39. {
  40. SetText( Gwen::Utility::StringToUnicode( str ), bDoEvents );
  41. }
  42. void Label::SizeToContents()
  43. {
  44. m_Text->SetPos( m_rTextPadding.left + m_Padding.left, m_rTextPadding.top + m_Padding.top );
  45. m_Text->RefreshSize();
  46. SetSize( m_Text->Width() + m_Padding.left + m_Padding.right + m_rTextPadding.left + m_rTextPadding.right, m_Text->Height() + m_Padding.top + m_Padding.bottom + m_rTextPadding.top + m_rTextPadding.bottom );
  47. }
  48. Gwen::Point Label::GetCharacterPosition( int iChar )
  49. {
  50. Gwen::Point p = m_Text->GetCharacterPosition( iChar );
  51. p.x += m_Text->X();
  52. p.y += m_Text->Y();
  53. return p;
  54. }