2
0

guiTextEditCtrl.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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/core/guiTypes.h"
  26. #endif
  27. #ifndef _GUITEXTCTRL_H_
  28. #include "gui/controls/guiTextCtrl.h"
  29. #endif
  30. #ifndef _STRINGBUFFER_H_
  31. #include "core/stringBuffer.h"
  32. #endif
  33. class SFXTrack;
  34. class GuiTextEditCtrl : public GuiTextCtrl
  35. {
  36. private:
  37. typedef GuiTextCtrl Parent;
  38. protected:
  39. DECLARE_CALLBACK( void, onTabComplete, (const char* val));
  40. DECLARE_CALLBACK( void, onReturn, ());
  41. DECLARE_CALLBACK( void, onValidate, ());
  42. StringBuffer mTextBuffer;
  43. String mValidateCommand;
  44. String mEscapeCommand;
  45. SFXTrack* mDeniedSound;
  46. // for animating the cursor
  47. S32 mNumFramesElapsed;
  48. U32 mTimeLastCursorFlipped;
  49. ColorI mCursorColor;
  50. bool mCursorOn;
  51. bool mInsertOn;
  52. S32 mMouseDragStart;
  53. Point2I mTextOffset;
  54. bool mTextOffsetReset;
  55. bool mDragHit;
  56. bool mTabComplete;
  57. S32 mScrollDir;
  58. //undo members
  59. StringBuffer mUndoText;
  60. S32 mUndoBlockStart;
  61. S32 mUndoBlockEnd;
  62. S32 mUndoCursorPos;
  63. void saveUndoState();
  64. S32 mBlockStart;
  65. S32 mBlockEnd;
  66. S32 mCursorPos;
  67. virtual S32 calculateCursorPos( const Point2I &globalPos );
  68. bool mHistoryDirty;
  69. S32 mHistoryLast;
  70. S32 mHistoryIndex;
  71. S32 mHistorySize;
  72. bool mPasswordText;
  73. StringTableEntry mPasswordMask;
  74. S32 mDoubleClickTimeMS;
  75. S32 mMouseUpTime;
  76. StringTableEntry mPlaceholderText;
  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. void playDeniedSound();
  82. void execConsoleCallback();
  83. bool mTextValid;
  84. virtual void handleCharInput( U16 ascii );
  85. S32 findNextWord();
  86. S32 findPrevWord();
  87. public:
  88. GuiTextEditCtrl();
  89. ~GuiTextEditCtrl();
  90. DECLARE_CONOBJECT(GuiTextEditCtrl);
  91. DECLARE_DESCRIPTION( "A control that allows to edit a single line of text. ");
  92. static void initPersistFields();
  93. bool onAdd();
  94. /// Get the contents of the control.
  95. ///
  96. /// dest should be of size GuiTextCtrl::MAX_STRING_LENGTH+1.
  97. void getText(char *dest);
  98. virtual void getRenderText(char *dest);
  99. void setText(S32 tag);
  100. virtual void setText(const UTF8* txt);
  101. virtual void setText(const UTF16* txt);
  102. S32 getCursorPos() { return( mCursorPos ); }
  103. void setCursorPos( const S32 newPos );
  104. void invalidText(bool playSound = true);
  105. void validText();
  106. bool isValidText();
  107. inline bool isPasswordText() { return mPasswordText; }
  108. bool isAllTextSelected();
  109. void selectAllText();
  110. void clearSelectedText();
  111. void forceValidateText();
  112. const char *getScriptValue();
  113. void setScriptValue(const char *value);
  114. bool getSinkAllKeys() { return mSinkAllKeyEvents; }
  115. void setSinkAllKeys(bool state) { mSinkAllKeyEvents = state; }
  116. virtual bool onKeyDown(const GuiEvent &event);
  117. virtual void onMouseDown(const GuiEvent &event);
  118. virtual void onMouseDragged(const GuiEvent &event);
  119. virtual void onMouseUp(const GuiEvent &event);
  120. void onCopy(bool andCut);
  121. void onPaste();
  122. void onUndo();
  123. virtual void setFirstResponder();
  124. virtual void onLoseFirstResponder();
  125. bool hasText();
  126. void onStaticModified(const char* slotName, const char* newValue = NULL);
  127. void onPreRender();
  128. void onRender(Point2I offset, const RectI &updateRect);
  129. virtual void drawText( const RectI &drawRect, bool isFocused );
  130. bool dealWithEnter( bool clearResponder );
  131. static bool setPlaceholderText(void* object, const char* index, const char* data)
  132. {
  133. static_cast<GuiTextEditCtrl*>(object)->setPlaceholderText(data); return true;
  134. }
  135. static const char* getPlaceholderText(void* obj, const char* data)
  136. {
  137. return static_cast<GuiTextEditCtrl*>(obj)->getPlaceholderText();
  138. }
  139. virtual void setPlaceholderText(const char* txt = NULL)
  140. {
  141. mPlaceholderText = StringTable->insert(txt, true);
  142. }
  143. const char* getPlaceholderText() { return (const char*)mPlaceholderText; }
  144. };
  145. #endif //_GUI_TEXTEDIT_CTRL_H