guiTextEditCtrl.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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; // max string len, must be less then or equal to 255
  37. Resource<GFont> mFont;
  38. bool mTruncateWhenUnfocused;
  39. StringBuffer mTextBuffer;
  40. StringTableEntry mValidateCommand;
  41. StringTableEntry mEscapeCommand;
  42. AssetPtr<AudioAsset> mDeniedSound;
  43. // for animating the cursor
  44. S32 mNumFramesElapsed;
  45. U32 mTimeLastCursorFlipped;
  46. ColorI mCursorColor;
  47. bool mCursorOn;
  48. //Edit Cursor
  49. GuiCursor* mEditCursor;
  50. bool mInsertOn;
  51. S32 mMouseDragStart;
  52. Point2I mTextOffset;
  53. bool mTextOffsetReset;
  54. bool mDragHit;
  55. bool mTabComplete;
  56. S32 mScrollDir;
  57. //undo members
  58. StringBuffer mUndoText;
  59. S32 mUndoBlockStart;
  60. S32 mUndoBlockEnd;
  61. S32 mUndoCursorPos;
  62. void saveUndoState();
  63. S32 mBlockStart;
  64. S32 mBlockEnd;
  65. S32 mCursorPos;
  66. S32 setCursorPos(const Point2I &offset);
  67. bool mHistoryDirty;
  68. S32 mHistoryLast;
  69. S32 mHistoryIndex;
  70. S32 mHistorySize;
  71. bool mPasswordText;
  72. StringTableEntry mPasswordMask;
  73. /// If set, any non-ESC key is handled here or not at all
  74. bool mSinkAllKeyEvents;
  75. UTF16 **mHistoryBuf;
  76. void updateHistory(StringBuffer *txt, bool moveIndex);
  77. S32 textBufferWidth(StringBuffer buffer);
  78. StringBuffer truncate(StringBuffer buffer, StringBuffer terminationString, S32 width);
  79. public:
  80. GuiTextEditCtrl();
  81. ~GuiTextEditCtrl();
  82. DECLARE_CONOBJECT(GuiTextEditCtrl);
  83. static void initPersistFields();
  84. bool onAdd();
  85. void inspectPostApply();
  86. bool onWake();
  87. void onSleep();
  88. /// Get the contents of the control.
  89. ///
  90. /// dest should be of size GuiTextCtrl::MAX_STRING_LENGTH+1.
  91. void getText(char *dest);
  92. virtual void setText(const UTF8* txt);
  93. virtual void setText(const UTF16* txt);
  94. virtual void setTextID(S32 id);
  95. virtual void setTextID(const char *id);
  96. S32 getCursorPos() { return( mCursorPos ); }
  97. void reallySetCursorPos( const S32 newPos );
  98. void selectAllText();
  99. void forceValidateText();
  100. const char *getScriptValue();
  101. void setScriptValue(const char *value);
  102. bool onKeyDown(const GuiEvent &event);
  103. void onTouchDown(const GuiEvent &event);
  104. void onTouchDragged(const GuiEvent &event);
  105. void onTouchUp(const GuiEvent &event);
  106. void onCopy(bool andCut);
  107. void onPaste();
  108. void onUndo();
  109. virtual void setFirstResponder();
  110. virtual void onLoseFirstResponder();
  111. void parentResized(const Point2I &oldParentExtent, const Point2I &newParentExtent);
  112. bool hasText();
  113. void onStaticModified(const char* slotName);
  114. void onPreRender();
  115. void onRender(Point2I offset, const RectI &updateRect);
  116. virtual void drawText( const RectI &drawRect, GuiControlState currentState );
  117. void playDeniedSound();
  118. void execConsoleCallback();
  119. enum Constants { MAX_STRING_LENGTH = 1024 };
  120. };
  121. #endif //_GUI_TEXTEDIT_CTRL_H