Counter Strike : Global Offensive Source Code
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.

289 lines
9.4 KiB

  1. //========= Copyright � 1996-2005, 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. void SetText(const char *text);
  28. 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. void SetUnderlineFont( HFont font );
  91. bool IsAllTextAlphaZero() const;
  92. bool HasText() const;
  93. void SetDrawTextOnly();
  94. protected:
  95. virtual void OnThink();
  96. virtual void PerformLayout(); // layout the text in the window
  97. virtual void ApplySchemeSettings(IScheme *pScheme);
  98. virtual void Paint();
  99. virtual void ApplySettings( KeyValues *inResourceData );
  100. virtual void GetSettings( KeyValues *outResourceData );
  101. virtual const char *GetDescription( void );
  102. MESSAGE_FUNC_WCHARPTR( OnSetText, "SetText", text );
  103. MESSAGE_FUNC( OnSliderMoved, "ScrollBarSliderMoved" ); // respond to scroll bar events
  104. virtual void OnKillFocus();
  105. virtual void OnMouseWheeled(int delta); // respond to mouse wheel events
  106. virtual void OnKeyCodeTyped(KeyCode code); //respond to keyboard events
  107. MESSAGE_FUNC_INT( OnClickPanel, "ClickPanel", index);
  108. virtual void OnCursorMoved(int x, int y); // respond to moving the cursor with mouse button down
  109. virtual void OnMousePressed(MouseCode code); // respond to mouse down events
  110. virtual void OnMouseDoublePressed(MouseCode code);
  111. virtual void OnMouseReleased(MouseCode code); // respond to mouse up events
  112. virtual void OnMouseFocusTicked(); // do while window has mouse focus
  113. virtual void OnCursorEntered(); // handle cursor entering window
  114. virtual void OnCursorExited(); // handle cursor exiting window
  115. virtual void OnMouseCaptureLost();
  116. virtual void OnSizeChanged(int newWide, int newTall);
  117. virtual void OnSetFocus();
  118. // clickable url handling
  119. int ParseTextStringForUrls(const char *text, int startPos, char *pchURLText, int cchURLText, char *pchURL, int cchURL, bool &clickable);
  120. virtual void OnTextClicked(const wchar_t *text);
  121. #ifdef DBGFLAG_VALIDATE
  122. virtual void Validate( CValidator &validator, char *pchName );
  123. #endif // DBGFLAG_VALIDATE
  124. protected:
  125. ScrollBar *_vertScrollBar; // the scroll bar used in the window
  126. private:
  127. const wchar_t *ResolveLocalizedTextAndVariables( char const *pchLookup, OUT_Z_BYTECAP(outbufsizeinbytes) wchar_t *outbuf, size_t outbufsizeinbytes );
  128. void CheckRecalcLineBreaks();
  129. void GotoWordRight(); // move cursor to start of next word
  130. void GotoWordLeft(); // move cursor to start of prev word
  131. void TruncateTextStream();
  132. bool GetSelectedRange(int& cx0,int& cx1);
  133. void CursorToPixelSpace(int cursorPos, int &cx, int &cy);
  134. int PixelToCursorSpace(int cx, int cy);
  135. void AddAnotherLine(int &cx, int &cy);
  136. void RecalculateDefaultState(int startIndex);
  137. void LayoutVerticalScrollBarSlider();
  138. void OpenEditMenu();
  139. void FinishingURL(int x, int y);
  140. // Returns the character index the drawing should Start at
  141. int GetStartDrawIndex(int &lineBreakIndexIndex);
  142. int GetCursorLine();
  143. int GetClickableTextIndexStart(int startIndex);
  144. void CreateEditMenu(); // create copy/cut/paste menu
  145. MESSAGE_FUNC_INT( MoveScrollBar, "MoveScrollBar", delta );
  146. MESSAGE_FUNC_INT( MoveScrollBarDirect, "MoveScrollBarDirect", delta );
  147. // linebreak stream functions
  148. void InvalidateLineBreakStream();
  149. void RecalculateLineBreaks();
  150. struct TFade
  151. {
  152. float flFadeStartTime;
  153. float flFadeLength;
  154. float flFadeSustain;
  155. int iOriginalAlpha;
  156. };
  157. // format stream - describes changes in formatting for the text stream
  158. struct TFormatStream
  159. {
  160. // render state
  161. Color color;
  162. int pixelsIndent;
  163. bool textClickable;
  164. CUtlSymbol m_sClickableTextAction;
  165. TFade fade;
  166. // position in TextStream that these changes take effect
  167. int textStreamIndex;
  168. };
  169. bool m_bResetFades;
  170. bool m_bInteractive;
  171. bool m_bUnusedScrollbarInvis;
  172. bool m_bAllTextAlphaIsZero;
  173. // data
  174. CUtlVector<wchar_t> m_TextStream; // the text in the text window is stored in this buffer
  175. CUtlVector<int> m_LineBreaks; // an array that holds the index in the buffer to wrap lines at
  176. CUtlVector<TFormatStream> m_FormatStream; // list of format changes
  177. bool m_bRecalcLineBreaks;
  178. int _recalculateBreaksIndex; // tells next linebreakindex index to Start recalculating line breaks
  179. bool _invalidateVerticalScrollbarSlider;
  180. int _cursorPos; // the position in the text buffer of the blinking cursor
  181. bool _mouseSelection; // whether we are highlighting text or not (selecting text)
  182. bool _mouseDragSelection; // tells weather mouse is outside window and button is down so we select text
  183. int _select[2]; // select[1] is the offset in the text to where the cursor is currently
  184. // select[0] is the offset to where the cursor was dragged to. or -1 if no drag.
  185. int _pixelsIndent;
  186. int _maxCharCount; // max number of chars that can be in the text buffer
  187. HFont _font; // font of chars in the text buffer
  188. HFont m_hFontUnderline;
  189. Color _selectionColor;
  190. Color _selectionTextColor; // color of the highlighted text
  191. bool _currentTextClickable;
  192. CUtlVector<ClickPanel *> _clickableTextPanels;
  193. int _clickableTextIndex;
  194. Color _defaultTextColor;
  195. int _drawOffsetX;
  196. int _drawOffsetY;
  197. Panel *m_pInterior;
  198. // sub-controls
  199. Menu *m_pEditMenu; // cut/copy/paste popup
  200. char *m_pszInitialText; // initial text
  201. // saved state
  202. bool _recalcSavedRenderState;
  203. struct TRenderState
  204. {
  205. // rendering positions
  206. int x, y;
  207. // basic state
  208. Color textColor;
  209. int pixelsIndent;
  210. bool textClickable;
  211. // index into our current position in the formatting stream
  212. int formatStreamIndex;
  213. };
  214. TRenderState m_CachedRenderState; // cached render state for the beginning of painting
  215. // updates a render state based on the formatting and color streams
  216. // returns true if any state changed
  217. bool UpdateRenderState(int textStreamPos, TRenderState &renderState);
  218. void CalculateFade( TRenderState &renderState );
  219. void GenerateRenderStateForTextStreamIndex(int textStreamIndex, TRenderState &renderState);
  220. int FindFormatStreamIndexForTextStreamPos(int textStreamIndex);
  221. // draws a string of characters with the same formatting using the current render state
  222. int DrawString(int iFirst, int iLast, TRenderState &renderState, HFont font);
  223. };
  224. } // namespace vgui
  225. #endif // RICHTEXT_H