tb_inline_select.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #include "tb_inline_select.h"
  6. #include <assert.h>
  7. #include <math.h>
  8. #include <stdlib.h>
  9. namespace tb {
  10. // FIX: axis should affect the buttons arrow skin!
  11. // FIX: unfocus should set the correct text!
  12. // == TBInlineSelect ========================================================================================
  13. TBInlineSelect::TBInlineSelect()
  14. : m_value(0)
  15. , m_min(0)
  16. , m_max(100)
  17. {
  18. SetSkinBg(TBIDC("TBInlineSelect"));
  19. AddChild(&m_layout);
  20. m_layout.AddChild(&m_buttons[0]);
  21. m_layout.AddChild(&m_editfield);
  22. m_layout.AddChild(&m_buttons[1]);
  23. m_layout.SetRect(GetPaddingRect());
  24. m_layout.SetGravity(WIDGET_GRAVITY_ALL);
  25. m_layout.SetSpacing(0);
  26. m_buttons[0].SetSkinBg(TBIDC("TBButton.flat"));
  27. m_buttons[1].SetSkinBg(TBIDC("TBButton.flat"));
  28. m_buttons[0].GetContentRoot()->AddChild(new TBSkinImage(TBIDC("arrowdark.left")));
  29. m_buttons[1].GetContentRoot()->AddChild(new TBSkinImage(TBIDC("arrowdark.right")));
  30. m_buttons[0].SetIsFocusable(false);
  31. m_buttons[1].SetIsFocusable(false);
  32. m_buttons[0].SetID(TBIDC("dec"));
  33. m_buttons[1].SetID(TBIDC("inc"));
  34. m_buttons[0].SetAutoRepeat(true);
  35. m_buttons[1].SetAutoRepeat(true);
  36. m_editfield.SetTextAlign(TB_TEXT_ALIGN_CENTER);
  37. m_editfield.SetEditType(EDIT_TYPE_NUMBER);
  38. m_editfield.SetText("0");
  39. }
  40. TBInlineSelect::~TBInlineSelect()
  41. {
  42. m_layout.RemoveChild(&m_buttons[1]);
  43. m_layout.RemoveChild(&m_editfield);
  44. m_layout.RemoveChild(&m_buttons[0]);
  45. RemoveChild(&m_layout);
  46. }
  47. void TBInlineSelect::SetEditFieldLayoutParams(LayoutParams& lp)
  48. {
  49. m_editfield.SetLayoutParams(lp);
  50. }
  51. void TBInlineSelect::SetLimits(double min, double max)
  52. {
  53. assert(min <= max);
  54. m_min = min;
  55. m_max = max;
  56. SetValueDouble(m_value);
  57. }
  58. void TBInlineSelect::SetValueInternal(double value, bool update_text)
  59. {
  60. value = CLAMP(value, m_min, m_max);
  61. if (value == m_value)
  62. return;
  63. m_value = value;
  64. if (update_text)
  65. {
  66. TBStr strval;
  67. double prec = m_value - floor(m_value);
  68. if (prec < .001)
  69. {
  70. strval.SetFormatted("%.0f", m_value);
  71. }
  72. else
  73. strval.SetFormatted("%.2f", m_value);
  74. m_editfield.SetText(strval);
  75. }
  76. TBWidgetEvent ev(EVENT_TYPE_CHANGED);
  77. InvokeEvent(ev);
  78. // Warning: Do nothing here since the event might have deleted us.
  79. // If needed, check if we are alive using a safe pointer first.
  80. }
  81. void TBInlineSelect::OnSkinChanged()
  82. {
  83. m_layout.SetRect(GetPaddingRect());
  84. }
  85. bool TBInlineSelect::OnEvent(const TBWidgetEvent &ev)
  86. {
  87. if (ev.type == EVENT_TYPE_KEY_DOWN)
  88. {
  89. if (ev.special_key == TB_KEY_UP || ev.special_key == TB_KEY_DOWN)
  90. {
  91. double dv = ev.special_key == TB_KEY_UP ? 1 : -1;
  92. SetValueDouble(GetValueDouble() + dv);
  93. return true;
  94. }
  95. }
  96. else if (ev.type == EVENT_TYPE_CLICK && ev.target->GetID() == TBIDC("dec"))
  97. {
  98. SetValueDouble(GetValueDouble() - 1);
  99. return true;
  100. }
  101. else if (ev.type == EVENT_TYPE_CLICK && ev.target->GetID() == TBIDC("inc"))
  102. {
  103. SetValueDouble(GetValueDouble() + 1);
  104. return true;
  105. }
  106. else if (ev.type == EVENT_TYPE_CHANGED && ev.target == &m_editfield)
  107. {
  108. TBStr text;
  109. m_editfield.GetText(text);
  110. SetValueInternal((double) atof(text), false);
  111. }
  112. return false;
  113. }
  114. }; // namespace tb