tb_inline_select.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 <stdlib.h>
  8. namespace tb {
  9. // FIX: axis should affect the buttons arrow skin!
  10. // FIX: unfocus should set the correct text!
  11. // == TBInlineSelect ========================================================================================
  12. TBInlineSelect::TBInlineSelect()
  13. : m_value(0)
  14. , m_min(0)
  15. , m_max(100)
  16. {
  17. SetSkinBg(TBIDC("TBInlineSelect"));
  18. AddChild(&m_layout);
  19. m_layout.AddChild(&m_buttons[0]);
  20. m_layout.AddChild(&m_editfield);
  21. m_layout.AddChild(&m_buttons[1]);
  22. m_layout.SetRect(GetPaddingRect());
  23. m_layout.SetGravity(WIDGET_GRAVITY_ALL);
  24. m_layout.SetSpacing(0);
  25. m_buttons[0].SetSkinBg(TBIDC("TBButton.flat"));
  26. m_buttons[1].SetSkinBg(TBIDC("TBButton.flat"));
  27. m_buttons[0].GetContentRoot()->AddChild(new TBSkinImage(TBIDC("arrowdark.left")));
  28. m_buttons[1].GetContentRoot()->AddChild(new TBSkinImage(TBIDC("arrowdark.right")));
  29. m_buttons[0].SetIsFocusable(false);
  30. m_buttons[1].SetIsFocusable(false);
  31. m_buttons[0].SetID(TBIDC("dec"));
  32. m_buttons[1].SetID(TBIDC("inc"));
  33. m_buttons[0].SetAutoRepeat(true);
  34. m_buttons[1].SetAutoRepeat(true);
  35. m_editfield.SetTextAlign(TB_TEXT_ALIGN_CENTER);
  36. m_editfield.SetEditType(EDIT_TYPE_NUMBER);
  37. m_editfield.SetText("0");
  38. }
  39. TBInlineSelect::~TBInlineSelect()
  40. {
  41. m_layout.RemoveChild(&m_buttons[1]);
  42. m_layout.RemoveChild(&m_editfield);
  43. m_layout.RemoveChild(&m_buttons[0]);
  44. RemoveChild(&m_layout);
  45. }
  46. void TBInlineSelect::SetLimits(int min, int max)
  47. {
  48. assert(min <= max);
  49. m_min = min;
  50. m_max = max;
  51. SetValue(m_value);
  52. }
  53. void TBInlineSelect::SetValueInternal(int value, bool update_text)
  54. {
  55. value = CLAMP(value, m_min, m_max);
  56. if (value == m_value)
  57. return;
  58. m_value = value;
  59. if (update_text)
  60. {
  61. TBStr strval;
  62. strval.SetFormatted("%d", m_value);
  63. m_editfield.SetText(strval);
  64. }
  65. TBWidgetEvent ev(EVENT_TYPE_CHANGED);
  66. InvokeEvent(ev);
  67. // Warning: Do nothing here since the event might have deleted us.
  68. // If needed, check if we are alive using a safe pointer first.
  69. }
  70. void TBInlineSelect::OnSkinChanged()
  71. {
  72. m_layout.SetRect(GetPaddingRect());
  73. }
  74. bool TBInlineSelect::OnEvent(const TBWidgetEvent &ev)
  75. {
  76. if (ev.type == EVENT_TYPE_KEY_DOWN)
  77. {
  78. if (ev.special_key == TB_KEY_UP || ev.special_key == TB_KEY_DOWN)
  79. {
  80. int dv = ev.special_key == TB_KEY_UP ? 1 : -1;
  81. SetValue(GetValue() + dv);
  82. return true;
  83. }
  84. }
  85. else if (ev.type == EVENT_TYPE_CLICK && ev.target->GetID() == TBIDC("dec"))
  86. {
  87. SetValue(GetValue() - 1);
  88. return true;
  89. }
  90. else if (ev.type == EVENT_TYPE_CLICK && ev.target->GetID() == TBIDC("inc"))
  91. {
  92. SetValue(GetValue() + 1);
  93. return true;
  94. }
  95. else if (ev.type == EVENT_TYPE_CHANGED && ev.target == &m_editfield)
  96. {
  97. TBStr text;
  98. m_editfield.GetText(text);
  99. SetValueInternal(atoi(text), false);
  100. }
  101. return false;
  102. }
  103. }; // namespace tb