guiMLTextCtrl.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 _GUIMLTEXTCTRL_H_
  23. #define _GUIMLTEXTCTRL_H_
  24. #ifndef _GUICONTROL_H_
  25. #include "gui/core/guiControl.h"
  26. #endif
  27. #ifndef _STRINGBUFFER_H_
  28. #include "core/stringBuffer.h"
  29. #endif
  30. #ifndef SOUND_ASSET_H
  31. #include "T3D/assets/SoundAsset.h"
  32. #endif
  33. class GFont;
  34. class SFXTrack;
  35. GFX_DeclareTextureProfile(GFXMLTextureProfile);
  36. class GuiMLTextCtrl : public GuiControl
  37. {
  38. typedef GuiControl Parent;
  39. //-------------------------------------- Public interfaces...
  40. public:
  41. enum Justification
  42. {
  43. LeftJustify,
  44. RightJustify,
  45. CenterJustify,
  46. };
  47. struct Font {
  48. char *faceName;
  49. U32 faceNameLen;
  50. U32 size;
  51. Resource<GFont> fontRes;
  52. Font *next;
  53. };
  54. struct Bitmap {
  55. char bitmapName[1024];
  56. U32 bitmapNameLen;
  57. GFXTexHandle bitmapObject;
  58. Bitmap *next;
  59. };
  60. struct URL
  61. {
  62. bool mouseDown;
  63. U32 textStart;
  64. U32 len;
  65. bool noUnderline;
  66. };
  67. struct Style
  68. {
  69. ColorI color;
  70. ColorI shadowColor;
  71. ColorI linkColor;
  72. ColorI linkColorHL;
  73. Point2I shadowOffset;
  74. Font *font;
  75. bool used;
  76. Style *next;
  77. };
  78. struct Atom
  79. {
  80. U32 textStart;
  81. U32 len;
  82. U32 xStart;
  83. U32 yStart;
  84. U32 width;
  85. U32 baseLine;
  86. U32 descent;
  87. Style *style;
  88. bool isClipped;
  89. URL *url;
  90. Atom *next;
  91. };
  92. struct Line {
  93. U32 y;
  94. U32 height;
  95. U32 divStyle;
  96. U32 textStart;
  97. U32 len;
  98. Atom *atomList;
  99. Line *next;
  100. };
  101. struct BitmapRef : public RectI
  102. {
  103. BitmapRef *nextBlocker;
  104. U32 textStart;
  105. U32 len;
  106. Bitmap *bitmap;
  107. BitmapRef *next;
  108. };
  109. struct LineTag {
  110. U32 id;
  111. S32 y;
  112. LineTag *next;
  113. };
  114. GuiMLTextCtrl();
  115. ~GuiMLTextCtrl();
  116. DECLARE_CALLBACK( void, onURL, (const char* url));
  117. DECLARE_CALLBACK( void, onResize, ( S32 width, S32 maxY ));
  118. // Text retrieval functions
  119. U32 getNumChars() const;
  120. U32 getText(char* pBuffer, const U32 bufferSize) const;
  121. U32 getWrappedText(char* pBuffer, const U32 bufferSize) const;
  122. const char* getTextContent();
  123. void insertChars(const char* inputChars,
  124. const U32 numInputChars,
  125. const U32 position);
  126. // Text substitution functions
  127. void setText(const char* textBuffer, const U32 numChars);
  128. void addText(const char* textBuffer, const U32 numChars, bool reformat);
  129. void setAlpha(F32 alpha) { mAlpha = alpha;}
  130. bool setCursorPosition(const S32);
  131. void ensureCursorOnScreen();
  132. // Scroll functions
  133. void scrollToTag( U32 id );
  134. void scrollToTop();
  135. void scrollToBottom();
  136. virtual void reflow();
  137. DECLARE_CONOBJECT(GuiMLTextCtrl);
  138. DECLARE_CATEGORY( "Gui Text" );
  139. DECLARE_DESCRIPTION( "A control that displays multiple lines of text." );
  140. static void initPersistFields();
  141. void setScriptValue(const char *value);
  142. const char *getScriptValue();
  143. static char *stripControlChars(const char *inString);
  144. //-------------------------------------- Protected Structures and constants
  145. protected:
  146. bool mIsEditCtrl;
  147. U32 *mTabStops;
  148. U32 mTabStopCount;
  149. U32 mCurTabStop;
  150. F32 mAlpha;
  151. DataChunker mViewChunker;
  152. DataChunker mResourceChunker;
  153. Line *mLineList;
  154. Bitmap *mBitmapList;
  155. BitmapRef *mBitmapRefList;
  156. Font *mFontList;
  157. LineTag *mTagList;
  158. bool mDirty;
  159. Style *mCurStyle;
  160. U32 mCurLMargin;
  161. U32 mCurRMargin;
  162. U32 mCurJustify;
  163. U32 mCurDiv;
  164. U32 mCurY;
  165. U32 mCurClipX;
  166. Atom *mLineAtoms;
  167. Atom **mLineAtomPtr;
  168. Atom *mEmitAtoms;
  169. Atom **mEmitAtomPtr;
  170. BitmapRef mSentinel;
  171. Line **mLineInsert;
  172. BitmapRef *mBlockList;
  173. U32 mScanPos;
  174. U32 mCurX;
  175. U32 mMaxY;
  176. URL *mCurURL;
  177. URL *mHitURL;
  178. void freeLineBuffers();
  179. void freeResources();
  180. Bitmap *allocBitmap(const char *bitmapName, U32 bitmapNameLen);
  181. Font *allocFont(const char *faceName, U32 faceNameLen, U32 size);
  182. LineTag *allocLineTag(U32 id);
  183. void emitNewLine(U32 textStart);
  184. Atom *buildTextAtom(U32 start, U32 len, U32 left, U32 right, URL *url);
  185. void emitTextToken(U32 textStart, U32 len);
  186. void emitBitmapToken(Bitmap *bmp, U32 textStart, bool bitmapBreak);
  187. void processEmitAtoms();
  188. Atom *splitAtomListEmit(Atom *list, U32 width);
  189. void drawAtomText(bool sel, U32 start, U32 end, Atom *atom, Line *line, Point2I offset);
  190. Atom *findHitAtom(const Point2I localCoords);
  191. Style *allocStyle(Style *style);
  192. static const U32 csmTextBufferGrowthSize;
  193. //-------------------------------------- Data...
  194. protected:
  195. // Cursor position should always be <= mCurrTextSize
  196. U32 mCursorPosition;
  197. // Actual text data. The line buffer is rebuilt from the linear text
  198. // given a specific width. TextBuffer is /not/ \0 terminated
  199. StringBuffer mTextBuffer;
  200. U32 mLineStart;
  201. S32 mMaxBufferSize;
  202. StringTableEntry mInitialText;
  203. // Selection information
  204. bool mSelectionActive;
  205. U32 mSelectionStart;
  206. U32 mSelectionEnd;
  207. U32 mVertMoveAnchor;
  208. bool mVertMoveAnchorValid;
  209. S32 mSelectionAnchor;
  210. Point2I mSelectionAnchorDropped;
  211. // Font resource
  212. Resource<GFont> mFont;
  213. // Console settable parameters
  214. S32 mLineSpacingPixels;
  215. bool mAllowColorChars;
  216. bool mUseURLMouseCursor;
  217. // Too many chars sound:
  218. DECLARE_SOUNDASSET(GuiMLTextCtrl, DeniedSound);
  219. DECLARE_ASSET_SETGET(GuiMLTextCtrl, DeniedSound);
  220. //-------------------------------------- Protected interface
  221. protected:
  222. // Inserting and deleting character blocks...
  223. void deleteChars(const U32 rangeStart,
  224. const U32 rangeEnd);
  225. void copyToClipboard(const U32 rangeStart,
  226. const U32 rangeEnd);
  227. // Selection maintainence
  228. bool isSelectionActive() const;
  229. void clearSelection();
  230. // Pixel -> text position mappings
  231. S32 getTextPosition(const Point2I& localPosition);
  232. // Gui control overrides
  233. bool onWake();
  234. void onSleep();
  235. void onPreRender();
  236. void onRender(Point2I offset, const RectI &updateRect);
  237. void getCursorPositionAndColor(Point2I &cursorTop, Point2I &cursorBottom, ColorI &color);
  238. void inspectPostApply();
  239. void parentResized(const RectI& oldParentRect, const RectI& newParentRect);
  240. bool onKeyDown(const GuiEvent& event);
  241. void onMouseDown(const GuiEvent&);
  242. void onMouseDragged(const GuiEvent&);
  243. void onMouseUp(const GuiEvent&);
  244. virtual void getCursor(GuiCursor *&cursor, bool &showCursor, const GuiEvent &lastGuiEvent);
  245. public:
  246. // Gui control overrides
  247. bool onAdd();
  248. void setSelectionStart( U32 start ) { clearSelection(); mSelectionStart = start; };
  249. void setSelectionEnd( U32 end ) { mSelectionEnd = end;};
  250. void setSelectionActive(bool active) { mSelectionActive = active; };
  251. S32 getCursorPosition() { return( mCursorPosition ); }
  252. virtual bool resize(const Point2I &newPosition, const Point2I &newExtent);
  253. };
  254. #endif // _H_GUIMLTEXTCTRL_