guiMLTextCtrl.h 7.9 KB

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