NumericUpDown.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. GWEN
  3. Copyright (c) 2010 Facepunch Studios
  4. See license in Gwen.h
  5. */
  6. #include "Gwen/Gwen.h"
  7. #include "Gwen/Utility.h"
  8. #include "Gwen/Skin.h"
  9. #include "Gwen/Controls/NumericUpDown.h"
  10. #include "Gwen/Controls/Layout/Splitter.h"
  11. using namespace Gwen;
  12. using namespace Gwen::Controls;
  13. GWEN_CONTROL_CONSTRUCTOR( NumericUpDown )
  14. {
  15. SetSize( 100, 20 );
  16. Layout::Splitter* pSplitter = new Layout::Splitter( this );
  17. pSplitter->Dock( Pos::Right );
  18. pSplitter->SetSize( 13, 13 );
  19. NumericUpDownButton_Up* pButtonUp = new NumericUpDownButton_Up( pSplitter );
  20. pButtonUp->onPress.Add( this, &NumericUpDown::OnButtonUp );
  21. pButtonUp->SetTabable( false );
  22. pSplitter->SetPanel( 0, pButtonUp );
  23. NumericUpDownButton_Down* pButtonDown = new NumericUpDownButton_Down( pSplitter );
  24. pButtonDown->onPress.Add( this, &NumericUpDown::OnButtonDown );
  25. pButtonDown->SetTabable( false );
  26. pButtonUp->SetPadding( Padding( 0, 1, 1, 0 ) );
  27. pSplitter->SetPanel( 1, pButtonDown );
  28. m_iMax = 100;
  29. m_iMin = 0;
  30. m_iNumber = 0;
  31. SetText( "0" );
  32. }
  33. void NumericUpDown::OnButtonUp( Base* /*control*/ )
  34. {
  35. SyncNumberFromText();
  36. SetValue( m_iNumber + 1 );
  37. }
  38. void NumericUpDown::OnButtonDown( Base* /*control*/ )
  39. {
  40. SyncNumberFromText();
  41. SetValue( m_iNumber - 1 );
  42. }
  43. void NumericUpDown::SyncTextFromNumber()
  44. {
  45. SetText( Utility::ToString( m_iNumber ) );
  46. }
  47. void NumericUpDown::SyncNumberFromText()
  48. {
  49. SetValue( (int) GetFloatFromText() );
  50. }
  51. void NumericUpDown::SetMin( int i )
  52. {
  53. m_iMin = i;
  54. }
  55. void NumericUpDown::SetMax( int i )
  56. {
  57. m_iMax = i;
  58. }
  59. void NumericUpDown::SetValue( int i )
  60. {
  61. if ( i > m_iMax ) i = m_iMax;
  62. if ( i < m_iMin ) i = m_iMin;
  63. if ( m_iNumber == i )
  64. {
  65. return;
  66. }
  67. m_iNumber = i;
  68. // Don't update the text if we're typing in it..
  69. if ( !HasFocus() )
  70. {
  71. SyncTextFromNumber();
  72. }
  73. OnChange();
  74. }
  75. void NumericUpDown::OnChange()
  76. {
  77. onChanged.Call( this );
  78. }
  79. void NumericUpDown::OnTextChanged()
  80. {
  81. BaseClass::OnTextChanged();
  82. SyncNumberFromText();
  83. }
  84. void NumericUpDown::OnEnter()
  85. {
  86. SyncNumberFromText();
  87. SyncTextFromNumber();
  88. }