Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

297 lines
9.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef RICHTEXT_H
  8. #define RICHTEXT_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include <vgui_controls/Panel.h>
  13. #include <utlvector.h>
  14. namespace vgui
  15. {
  16. class ClickPanel;
  17. //-----------------------------------------------------------------------------
  18. // Purpose: Non-editable display of a rich text control
  19. //-----------------------------------------------------------------------------
  20. class RichText : public Panel
  21. {
  22. DECLARE_CLASS_SIMPLE( RichText, Panel );
  23. public:
  24. RichText(Panel *parent, const char *panelName);
  25. ~RichText();
  26. // text manipulation
  27. virtual void SetText(const char *text);
  28. virtual void SetText(const wchar_t *text);
  29. void GetText(int offset, OUT_Z_BYTECAP(bufLenInBytes) wchar_t *buf, int bufLenInBytes);
  30. void GetText(int offset, OUT_Z_BYTECAP(bufLenInBytes) char *pch, int bufLenInBytes);
  31. // configuration
  32. void SetFont(HFont font);
  33. // inserts characters at the end of the stream
  34. void InsertChar(wchar_t ch);
  35. void InsertString(const char *text);
  36. void InsertString(const wchar_t *wszText);
  37. // selection
  38. void SelectNone();
  39. void SelectAllText();
  40. void SelectNoText();
  41. MESSAGE_FUNC( CutSelected, "DoCutSelected" );
  42. MESSAGE_FUNC( CopySelected, "DoCopySelected" );
  43. // sets the RichText control interactive or not (meaning you can select/copy text in the window)
  44. void SetPanelInteractive( bool bInteractive ){ m_bInteractive = bInteractive; }
  45. // sets the RichText scrollbar invisible if it's not going to be used
  46. void SetUnusedScrollbarInvisible( bool bInvis ){ m_bUnusedScrollbarInvis = bInvis; }
  47. // cursor movement
  48. void GotoTextStart(); // go to start of text buffer
  49. void GotoTextEnd(); // go to end of text buffer
  50. // configuration
  51. // sets visibility of scrollbar
  52. void SetVerticalScrollbar(bool state);
  53. // sets limit of number of characters insertable into field; set to -1 to remove maximum
  54. // only works with if rich-edit is NOT enabled
  55. void SetMaximumCharCount(int maxChars);
  56. // rich edit commands
  57. void InsertColorChange(Color col);
  58. // IndentChange doesn't take effect until the next newline character
  59. void InsertIndentChange(int pixelsIndent);
  60. // clickable text
  61. // notification that text was clicked is through "TextClicked" message
  62. void InsertClickableTextStart( const char *pchClickAction = NULL );
  63. void InsertClickableTextEnd();
  64. // inserts a string that needs to be scanned for urls/mailto commands to be made clickable
  65. void InsertPossibleURLString(const char *text, Color URLTextColor, Color normalTextColor);
  66. void InsertFade( float flSustain, float flLength );
  67. void ResetAllFades( bool bHold, bool bOnlyExpired = false, float flNewSustain = -1.0f );
  68. // sets the height of the window so all text is visible.
  69. // used by tooltips
  70. void SetToFullHeight();
  71. int GetNumLines();
  72. /* CUSTOM MESSAGE HANDLING
  73. "SetText"
  74. input: "text" - text is set to be this string
  75. */
  76. /* MESSAGE SENDING (to action signal targets)
  77. "TextChanged" - sent when the text is edited by the user
  78. "TextClicked" - sent when clickable text has been clicked on
  79. "text" - the text that was clicked on
  80. */
  81. virtual bool RequestInfo(KeyValues *outputData);
  82. /* INFO HANDLING
  83. "GetText"
  84. returns:
  85. "text" - text contained in the text box
  86. */
  87. virtual void SetFgColor( Color color );
  88. virtual void SetDrawOffsets( int ofsx, int ofsy );
  89. bool IsScrollbarVisible();
  90. // sets how URL's are handled
  91. // if set, a "URLClicked" "url" "<data>" message will be sent to that panel
  92. void SetURLClickedHandler( Panel *pPanelToHandleClickMsg );
  93. void SetUnderlineFont( HFont font );
  94. bool IsAllTextAlphaZero() const;
  95. bool HasText() const;
  96. void SetDrawTextOnly();
  97. protected:
  98. virtual void OnThink();
  99. virtual void PerformLayout(); // layout the text in the window
  100. virtual void ApplySchemeSettings(IScheme *pScheme);
  101. virtual void Paint();
  102. virtual void ApplySettings( KeyValues *inResourceData );
  103. virtual void GetSettings( KeyValues *outResourceData );
  104. virtual const char *GetDescription( void );
  105. MESSAGE_FUNC_WCHARPTR( OnSetText, "SetText", text );
  106. MESSAGE_FUNC( OnSliderMoved, "ScrollBarSliderMoved" ); // respond to scroll bar events
  107. virtual void OnKillFocus();
  108. virtual void OnMouseWheeled(int delta); // respond to mouse wheel events
  109. virtual void OnKeyCodeTyped(KeyCode code); //respond to keyboard events
  110. MESSAGE_FUNC_INT( OnClickPanel, "ClickPanel", index);
  111. virtual void OnCursorMoved(int x, int y); // respond to moving the cursor with mouse button down
  112. virtual void OnMousePressed(MouseCode code); // respond to mouse down events
  113. virtual void OnMouseDoublePressed(MouseCode code);
  114. virtual void OnMouseReleased(MouseCode code); // respond to mouse up events
  115. virtual void OnMouseFocusTicked(); // do while window has mouse focus
  116. virtual void OnCursorEntered(); // handle cursor entering window
  117. virtual void OnCursorExited(); // handle cursor exiting window
  118. virtual void OnMouseCaptureLost();
  119. virtual void OnSizeChanged(int newWide, int newTall);
  120. virtual void OnSetFocus();
  121. // clickable url handling
  122. int ParseTextStringForUrls(const char *text, int startPos, char *pchURLText, int cchURLText, char *pchURL, int cchURL, bool &clickable);
  123. virtual void OnTextClicked(const wchar_t *text);
  124. #ifdef DBGFLAG_VALIDATE
  125. virtual void Validate( CValidator &validator, char *pchName );
  126. #endif // DBGFLAG_VALIDATE
  127. protected:
  128. ScrollBar *_vertScrollBar; // the scroll bar used in the window
  129. private:
  130. int GetLineHeight();
  131. HFont GetDefaultFont();
  132. const wchar_t *ResolveLocalizedTextAndVariables( char const *pchLookup, OUT_Z_BYTECAP(outbufsizeinbytes) wchar_t *outbuf, size_t outbufsizeinbytes );
  133. void CheckRecalcLineBreaks();
  134. void GotoWordRight(); // move cursor to start of next word
  135. void GotoWordLeft(); // move cursor to start of prev word
  136. void TruncateTextStream();
  137. bool GetSelectedRange(int& cx0,int& cx1);
  138. void CursorToPixelSpace(int cursorPos, int &cx, int &cy);
  139. int PixelToCursorSpace(int cx, int cy);
  140. void AddAnotherLine(int &cx, int &cy);
  141. void RecalculateDefaultState(int startIndex);
  142. void LayoutVerticalScrollBarSlider();
  143. void OpenEditMenu();
  144. void FinishingURL(int x, int y);
  145. // Returns the character index the drawing should Start at
  146. int GetStartDrawIndex(int &lineBreakIndexIndex);
  147. int GetCursorLine();
  148. int GetClickableTextIndexStart(int startIndex);
  149. void CreateEditMenu(); // create copy/cut/paste menu
  150. MESSAGE_FUNC_INT( MoveScrollBar, "MoveScrollBar", delta );
  151. MESSAGE_FUNC_INT( MoveScrollBarDirect, "MoveScrollBarDirect", delta );
  152. // linebreak stream functions
  153. void InvalidateLineBreakStream();
  154. void RecalculateLineBreaks();
  155. struct TFade
  156. {
  157. float flFadeStartTime;
  158. float flFadeLength;
  159. float flFadeSustain;
  160. int iOriginalAlpha;
  161. };
  162. // format stream - describes changes in formatting for the text stream
  163. struct TFormatStream
  164. {
  165. // render state
  166. Color color;
  167. int pixelsIndent;
  168. bool textClickable;
  169. CUtlSymbol m_sClickableTextAction;
  170. TFade fade;
  171. // position in TextStream that these changes take effect
  172. int textStreamIndex;
  173. };
  174. bool m_bResetFades;
  175. bool m_bInteractive;
  176. bool m_bUnusedScrollbarInvis;
  177. bool m_bAllTextAlphaIsZero;
  178. // data
  179. CUtlVector<wchar_t> m_TextStream; // the text in the text window is stored in this buffer
  180. CUtlVector<int> m_LineBreaks; // an array that holds the index in the buffer to wrap lines at
  181. CUtlVector<TFormatStream> m_FormatStream; // list of format changes
  182. bool m_bRecalcLineBreaks;
  183. int _recalculateBreaksIndex; // tells next linebreakindex index to Start recalculating line breaks
  184. bool _invalidateVerticalScrollbarSlider;
  185. int _cursorPos; // the position in the text buffer of the blinking cursor
  186. bool _mouseSelection; // whether we are highlighting text or not (selecting text)
  187. bool _mouseDragSelection; // tells weather mouse is outside window and button is down so we select text
  188. int _select[2]; // select[1] is the offset in the text to where the cursor is currently
  189. // select[0] is the offset to where the cursor was dragged to. or -1 if no drag.
  190. int _pixelsIndent;
  191. int _maxCharCount; // max number of chars that can be in the text buffer
  192. HFont _font; // font of chars in the text buffer
  193. HFont m_hFontUnderline;
  194. Color _selectionColor;
  195. Color _selectionTextColor; // color of the highlighted text
  196. bool _currentTextClickable;
  197. CUtlVector<ClickPanel *> _clickableTextPanels;
  198. int _clickableTextIndex;
  199. Color _defaultTextColor;
  200. int _drawOffsetX;
  201. int _drawOffsetY;
  202. Panel *m_pInterior;
  203. PHandle m_hPanelToHandleClickingURLs;
  204. // sub-controls
  205. Menu *m_pEditMenu; // cut/copy/paste popup
  206. char *m_pszInitialText; // initial text
  207. // saved state
  208. bool _recalcSavedRenderState;
  209. struct TRenderState
  210. {
  211. // rendering positions
  212. int x, y;
  213. // basic state
  214. Color textColor;
  215. int pixelsIndent;
  216. bool textClickable;
  217. // index into our current position in the formatting stream
  218. int formatStreamIndex;
  219. };
  220. TRenderState m_CachedRenderState; // cached render state for the beginning of painting
  221. // updates a render state based on the formatting and color streams
  222. // returns true if any state changed
  223. bool UpdateRenderState(int textStreamPos, TRenderState &renderState);
  224. void CalculateFade( TRenderState &renderState );
  225. void GenerateRenderStateForTextStreamIndex(int textStreamIndex, TRenderState &renderState);
  226. int FindFormatStreamIndexForTextStreamPos(int textStreamIndex);
  227. // draws a string of characters with the same formatting using the current render state
  228. int DrawString(int iFirst, int iLast, TRenderState &renderState, HFont font);
  229. };
  230. } // namespace vgui
  231. #endif // RICHTEXT_H