UIEditField.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. {
  33. if (createWidget)
  34. {
  35. widget_ = new TBEditField();
  36. widget_->SetDelegate(this);
  37. GetSubsystem<UI>()->WrapWidget(this, widget_);
  38. }
  39. }
  40. UIEditField::~UIEditField()
  41. {
  42. }
  43. void UIEditField::SetReadOnly(bool readonly)
  44. {
  45. if (!widget_)
  46. return;
  47. TBEditField* w = (TBEditField*) widget_;
  48. w->SetReadOnly(readonly);
  49. }
  50. void UIEditField::SetStyling(bool styling)
  51. {
  52. if (!widget_)
  53. return;
  54. TBEditField* w = (TBEditField*) widget_;
  55. w->SetStyling(styling);
  56. }
  57. void UIEditField::SetMultiline(bool multiline)
  58. {
  59. if (!widget_)
  60. return;
  61. TBEditField* w = (TBEditField*) widget_;
  62. w->SetMultiline(multiline);
  63. }
  64. void UIEditField::SetWrapping(bool wrap)
  65. {
  66. if (!widget_)
  67. return;
  68. TBEditField* w = (TBEditField*) widget_;
  69. w->SetWrapping(wrap);
  70. }
  71. bool UIEditField::GetWrapping()
  72. {
  73. if (!widget_)
  74. return false;
  75. TBEditField* w = (TBEditField*) widget_;
  76. return w->GetWrapping();
  77. }
  78. void UIEditField::SetEditType(UI_EDIT_TYPE type)
  79. {
  80. if (!widget_)
  81. return;
  82. // safe cast?
  83. TBEditField* w = (TBEditField*) widget_;
  84. w->SetEditType((tb::EDIT_TYPE) type);
  85. }
  86. void UIEditField::ScrollTo(int x, int y)
  87. {
  88. if (!widget_)
  89. return;
  90. // safe cast?
  91. TBEditField* w = (TBEditField*) widget_;
  92. w->ScrollTo(x, y);
  93. }
  94. void UIEditField::AppendText(const String& text)
  95. {
  96. if (!widget_)
  97. return;
  98. // safe cast?
  99. TBEditField* w = (TBEditField*) widget_;
  100. w->AppendText(text.CString());
  101. }
  102. void UIEditField::SetAdaptToContentSize(bool adapt)
  103. {
  104. if (!widget_)
  105. return;
  106. TBEditField* w = (TBEditField*) widget_;
  107. w->SetAdaptToContentSize(adapt);
  108. }
  109. bool UIEditField::GetAdaptToContentSize() const
  110. {
  111. if (!widget_)
  112. return false;
  113. // safe cast?
  114. TBEditField* w = (TBEditField*) widget_;
  115. return w->GetAdaptToContentSize();
  116. }
  117. void UIEditField::Reformat(bool update_fragments)
  118. {
  119. if (!widget_)
  120. return;
  121. TBEditField* w = (TBEditField*) widget_;
  122. w->Reformat(update_fragments);
  123. }
  124. void UIEditField::SetTextAlign(UI_TEXT_ALIGN align)
  125. {
  126. if (!widget_)
  127. return;
  128. // safe cast?
  129. TBEditField* w = (TBEditField*) widget_;
  130. switch (align)
  131. {
  132. case UI_TEXT_ALIGN_CENTER:
  133. w->SetTextAlign(TB_TEXT_ALIGN_CENTER);
  134. break;
  135. case UI_TEXT_ALIGN_LEFT:
  136. w->SetTextAlign(TB_TEXT_ALIGN_LEFT);
  137. break;
  138. case UI_TEXT_ALIGN_RIGHT:
  139. w->SetTextAlign(TB_TEXT_ALIGN_RIGHT);
  140. break;
  141. }
  142. }
  143. bool UIEditField::OnEvent(const tb::TBWidgetEvent &ev)
  144. {
  145. if (ev.type == EVENT_TYPE_CUSTOM && ev.ref_id == TBIDC("edit_complete"))
  146. {
  147. VariantMap eventData;
  148. eventData[UIWidgetEditComplete::P_WIDGET] = this;
  149. SendEvent(E_UIWIDGETEDITCOMPLETE, eventData);
  150. return true;
  151. }
  152. return UIWidget::OnEvent(ev);
  153. }
  154. }