UIEditField.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <TurboBadger/tb_widgets.h>
  23. #include <TurboBadger/tb_widgets_common.h>
  24. #include <TurboBadger/tb_editfield.h>
  25. #include "UI.h"
  26. #include "UIEvents.h"
  27. #include "UIEditField.h"
  28. using namespace tb;
  29. namespace Atomic
  30. {
  31. UIEditField::UIEditField(Context* context, bool createWidget) : UIWidget(context, false),
  32. firstFocusFlag_(false)
  33. {
  34. if (createWidget)
  35. {
  36. widget_ = new TBEditField();
  37. widget_->SetDelegate(this);
  38. GetSubsystem<UI>()->WrapWidget(this, widget_);
  39. }
  40. }
  41. UIEditField::~UIEditField()
  42. {
  43. }
  44. void UIEditField::SetReadOnly(bool readonly)
  45. {
  46. if (!widget_)
  47. return;
  48. TBEditField* w = (TBEditField*) widget_;
  49. w->SetReadOnly(readonly);
  50. }
  51. void UIEditField::SetStyling(bool styling)
  52. {
  53. if (!widget_)
  54. return;
  55. TBEditField* w = (TBEditField*) widget_;
  56. w->SetStyling(styling);
  57. }
  58. void UIEditField::SetMultiline(bool multiline)
  59. {
  60. if (!widget_)
  61. return;
  62. TBEditField* w = (TBEditField*) widget_;
  63. w->SetMultiline(multiline);
  64. }
  65. void UIEditField::SetWrapping(bool wrap)
  66. {
  67. if (!widget_)
  68. return;
  69. TBEditField* w = (TBEditField*) widget_;
  70. w->SetWrapping(wrap);
  71. }
  72. bool UIEditField::GetWrapping()
  73. {
  74. if (!widget_)
  75. return false;
  76. TBEditField* w = (TBEditField*) widget_;
  77. return w->GetWrapping();
  78. }
  79. void UIEditField::SetEditType(UI_EDIT_TYPE type)
  80. {
  81. if (!widget_)
  82. return;
  83. // safe cast?
  84. TBEditField* w = (TBEditField*) widget_;
  85. w->SetEditType((tb::EDIT_TYPE) type);
  86. }
  87. void UIEditField::ScrollTo(int x, int y)
  88. {
  89. if (!widget_)
  90. return;
  91. // safe cast?
  92. TBEditField* w = (TBEditField*) widget_;
  93. w->ScrollTo(x, y);
  94. }
  95. void UIEditField::AppendText(const String& text)
  96. {
  97. if (!widget_)
  98. return;
  99. // safe cast?
  100. TBEditField* w = (TBEditField*) widget_;
  101. w->AppendText(text.CString());
  102. }
  103. void UIEditField::SetAdaptToContentSize(bool adapt)
  104. {
  105. if (!widget_)
  106. return;
  107. TBEditField* w = (TBEditField*) widget_;
  108. w->SetAdaptToContentSize(adapt);
  109. }
  110. bool UIEditField::GetAdaptToContentSize() const
  111. {
  112. if (!widget_)
  113. return false;
  114. // safe cast?
  115. TBEditField* w = (TBEditField*) widget_;
  116. return w->GetAdaptToContentSize();
  117. }
  118. void UIEditField::Reformat(bool update_fragments)
  119. {
  120. if (!widget_)
  121. return;
  122. TBEditField* w = (TBEditField*) widget_;
  123. w->Reformat(update_fragments);
  124. }
  125. void UIEditField::SetTextAlign(UI_TEXT_ALIGN align)
  126. {
  127. if (!widget_)
  128. return;
  129. // safe cast?
  130. TBEditField* w = (TBEditField*) widget_;
  131. switch (align)
  132. {
  133. case UI_TEXT_ALIGN_CENTER:
  134. w->SetTextAlign(TB_TEXT_ALIGN_CENTER);
  135. break;
  136. case UI_TEXT_ALIGN_LEFT:
  137. w->SetTextAlign(TB_TEXT_ALIGN_LEFT);
  138. break;
  139. case UI_TEXT_ALIGN_RIGHT:
  140. w->SetTextAlign(TB_TEXT_ALIGN_RIGHT);
  141. break;
  142. }
  143. }
  144. void UIEditField::OnFocusChanged(bool focused)
  145. {
  146. UIWidget::OnFocusChanged(focused);
  147. if (!widget_)
  148. return;
  149. // safe cast?
  150. TBEditField* w = (TBEditField*) widget_;
  151. TBStyleEdit* styleEdit = w->GetStyleEdit();
  152. if (styleEdit != NULL)
  153. {
  154. if (focused)
  155. {
  156. if (!w->GetMultiline())
  157. styleEdit->selection.SelectAll();
  158. firstFocusFlag_ = true;
  159. }
  160. else
  161. {
  162. styleEdit->selection.SelectNothing();
  163. }
  164. }
  165. }
  166. bool UIEditField::OnEvent(const tb::TBWidgetEvent &ev)
  167. {
  168. if (ev.type == EVENT_TYPE_CUSTOM && ev.ref_id == TBIDC("edit_complete"))
  169. {
  170. VariantMap eventData;
  171. eventData[UIWidgetEditComplete::P_WIDGET] = this;
  172. SendEvent(E_UIWIDGETEDITCOMPLETE, eventData);
  173. return true;
  174. }
  175. else if (ev.type == EVENT_TYPE_POINTER_DOWN)
  176. {
  177. // Select the entire value in the field when it is selected
  178. if (widget_ && firstFocusFlag_)
  179. {
  180. firstFocusFlag_ = false;
  181. // safe cast?
  182. TBEditField* w = (TBEditField*) widget_;
  183. TBStyleEdit* styleEdit = w->GetStyleEdit();
  184. if (styleEdit != NULL && !w->GetMultiline())
  185. {
  186. styleEdit->selection.SelectAll();
  187. }
  188. }
  189. }
  190. return UIWidget::OnEvent(ev);
  191. }
  192. }