guiTextEditCtrl.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  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
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell 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
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _GUITEXTEDITCTRL_H_
  23. #define _GUITEXTEDITCTRL_H_
  24. #ifndef _GUITYPES_H_
  25. #include "gui/guiTypes.h"
  26. #endif
  27. #ifndef _GUITEXTCTRL_H_
  28. #include "gui/guiTextCtrl.h"
  29. #endif
  30. class GuiTextEditCtrl : public GuiControl
  31. {
  32. private:
  33. typedef GuiControl Parent;
  34. static U32 smNumAwake;
  35. protected:
  36. S32 mMaxStrLen;
  37. StringBuffer mTextBuffer;
  38. StringTableEntry mValidateCommand;
  39. StringTableEntry mEscapeCommand;
  40. public:
  41. //input mode
  42. enum InputMode
  43. {
  44. AllText = 0,
  45. Decimal,
  46. Number,
  47. Alpha,
  48. AlphaNumeric
  49. };
  50. protected:
  51. InputMode mInputMode;
  52. bool mReturnCausesTab;
  53. // for animating the cursor
  54. S32 mNumFramesElapsed;
  55. U32 mTimeLastCursorFlipped;
  56. bool mCursorOn;
  57. //Edit Cursor
  58. GuiCursor* mEditCursor;
  59. bool mInsertOn;
  60. S32 mMouseDragStart;
  61. Point2I mTextOffset;
  62. bool mTextOffsetReset;
  63. bool mDragHit;
  64. S32 mScrollDir;
  65. //undo members
  66. StringBuffer mUndoText;
  67. S32 mUndoBlockStart;
  68. S32 mUndoBlockEnd;
  69. S32 mUndoCursorPos;
  70. void saveUndoState();
  71. S32 mBlockStart;
  72. S32 mBlockEnd;
  73. S32 mCursorPos;
  74. S32 setCursorPos(const Point2I &offset);
  75. bool mHistoryDirty;
  76. S32 mHistoryLast;
  77. S32 mHistoryIndex;
  78. S32 mHistorySize;
  79. bool mPasswordText;
  80. StringTableEntry mPasswordMask;
  81. /// If set, any non-ESC key is handled here or not at all
  82. bool mSinkAllKeyEvents;
  83. UTF16 **mHistoryBuf;
  84. void updateHistory(StringBuffer *txt, bool moveIndex);
  85. S32 textBufferWidth(StringBuffer buffer);
  86. StringBuffer truncate(StringBuffer buffer, StringBuffer terminationString, S32 width);
  87. public:
  88. GuiTextEditCtrl();
  89. ~GuiTextEditCtrl();
  90. DECLARE_CONOBJECT(GuiTextEditCtrl);
  91. static void initPersistFields();
  92. bool onAdd();
  93. void inspectPostApply();
  94. bool onWake();
  95. void onSleep();
  96. /// Get the contents of the control.
  97. ///
  98. /// dest should be of size GuiTextEditCtrl::MAX_STRING_LENGTH+1.
  99. virtual void getText(char *dest);
  100. virtual void setText(const UTF8* txt);
  101. virtual void setText(const UTF16* txt);
  102. virtual void setTextID(S32 id);
  103. virtual void setTextID(const char *id);
  104. S32 getCursorPos() { return( mCursorPos ); }
  105. void reallySetCursorPos( const S32 newPos );
  106. void selectAllText();
  107. bool validate();
  108. const char *getScriptValue();
  109. void setScriptValue(const char *value);
  110. bool onKeyDown(const GuiEvent &event);
  111. void onTouchDown(const GuiEvent &event);
  112. void onTouchDragged(const GuiEvent &event);
  113. void onTouchUp(const GuiEvent &event);
  114. void onCopy(bool andCut);
  115. void onPaste();
  116. void onUndo();
  117. virtual void setFirstResponder();
  118. virtual void onLoseFirstResponder();
  119. void parentResized(const Point2I &oldParentExtent, const Point2I &newParentExtent);
  120. bool hasText();
  121. void onStaticModified(const char* slotName);
  122. void onPreRender();
  123. void onRender(Point2I offset, const RectI &updateRect);
  124. virtual void drawText( const RectI &drawRect, GuiControlState currentState );
  125. bool inputModeValidate(const U16 key, S32 cursorPos);
  126. void keyDenied();
  127. void execConsoleCallback();
  128. inline void setValidateCommand(const char *newCmd) { mValidateCommand = newCmd ? StringTable->insert(newCmd) : StringTable->EmptyString; }
  129. inline const char * getValidateCommand() { return mValidateCommand; }
  130. inline void setEscapeCommand(const char *newCmd) { mEscapeCommand = newCmd ? StringTable->insert(newCmd) : StringTable->EmptyString; }
  131. inline const char * getEscapeCommand() { return mEscapeCommand; }
  132. inline void setReturnCausesTab(bool setting) { mReturnCausesTab = setting; }
  133. inline bool getReturnCausesTab() { return mReturnCausesTab; }
  134. inline void setSinkAllKeyEvents(bool setting) { mSinkAllKeyEvents = setting; }
  135. inline bool getSinkAllKeyEvents() { return mSinkAllKeyEvents; }
  136. inline void setIsPassword(bool setting) { mPasswordText = setting; }
  137. inline bool getIsPassword() { return mPasswordText; }
  138. void setMaxLength(S32 max);
  139. inline S32 getMaxLength() { return mMaxStrLen; }
  140. static bool setMaxLengthProperty(void* obj, const char* data) { static_cast<GuiTextEditCtrl*>(obj)->setMaxLength(dAtoi(data)); return true; }
  141. void setInputMode(const InputMode mode);
  142. inline InputMode getInputMode() { return mInputMode; }
  143. static bool setInputMode(void* obj, const char* data)
  144. {
  145. // Fetch body type.
  146. const InputMode mode = getInputModeEnum(data);
  147. // Check for error.
  148. if (mode < AllText || mode > AlphaNumeric)
  149. return false;
  150. static_cast<GuiTextEditCtrl*>(obj)->setInputMode(mode);
  151. return true;
  152. }
  153. static const char* getInputMode(void* obj, const char* data) { return getInputModeDescription(static_cast<GuiTextEditCtrl*>(obj)->getInputMode()); }
  154. static bool writeInputMode(void* obj, StringTableEntry pFieldName) { return static_cast<GuiTextEditCtrl*>(obj)->getInputMode() != AllText; }
  155. static InputMode getInputModeEnum(const char* label);
  156. static const char* getInputModeDescription(const InputMode mode);
  157. enum Constants { MAX_STRING_LENGTH = 1024 };
  158. private:
  159. bool tabNext();
  160. bool tabPrev();
  161. void handleBackSpace();
  162. };
  163. #endif //_GUI_TEXTEDIT_CTRL_H