guiTextEditCtrl.h 6.3 KB

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