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.

390 lines
16 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: A Class to create a window that you can type and edit text in.
  4. // Window can hold single line or multiline text.
  5. // If it is single it can scroll horizontally in response to
  6. // key input and mouse selection.
  7. //
  8. // $NoKeywords: $
  9. //=============================================================================//
  10. #ifndef TEXTENTRY_H
  11. #define TEXTENTRY_H
  12. #ifdef _WIN32
  13. #pragma once
  14. #endif
  15. #include <vgui/VGUI.h>
  16. #include <Color.h>
  17. #include <vgui_controls/Panel.h>
  18. #include <vgui_controls/Label.h>
  19. #include <vgui_controls/ListPanel.h>
  20. #include <utlvector.h>
  21. namespace vgui
  22. {
  23. //-----------------------------------------------------------------------------
  24. // Purpose: Text-input handler
  25. // Behaviour Specs:
  26. // This class handles input from mouse and keyboard.
  27. // TextEntry classes support several box styles, horizontal scrolling with no scrollbar
  28. // vertical scrolling with or without a scrollbar, single line, multiline,
  29. // editable and noneditable.
  30. //
  31. // Shared behaviour:
  32. // URL's are a different text color and are clickable. Clicking them brings up a web browser.
  33. // For vertical scroll bars, up and down arrows scroll one line at a time.
  34. // Clicking and dragging the nob scrolls through text lines.
  35. // Mouse wheel also moves the nob.
  36. // User can select and highlight text in the window.
  37. // Double clicking on a word selects it.
  38. //
  39. // Non editable:
  40. // No blinking cursor in non editable windows.
  41. // Right clicking mouse opens copy menu. Menu's top left corner is where mouse is.
  42. // Ctrl-c will also copy the text.
  43. // Editable:
  44. // Blinking cursor is positioned where text will be inserted.
  45. // Text keys type chars in the window.
  46. // ctrl-c copy highlighted text
  47. // ctrl-v paste highlighted text
  48. // ctrl-x cut highlighted text
  49. // ctrl-right arrow move cursor to the start of the next word
  50. // ctrl-left arrow move cursor to the start of the prev word
  51. // ctrl-enter delete the selected text (and inserts a newline if _catchEnterKey is true)
  52. // insert delete selected text and pastes text from the clipboard
  53. // delete delete the selected text
  54. // ctrl-home move cursor to the start of the text
  55. // ctrl-end move cursor to the end of the text.
  56. // left arrow move cursor before prev char
  57. // ctrl-shift left/right arrow selects text a word at a time
  58. // right arrow move cursor before next char
  59. // up arrow move cursor up one line.
  60. // down arrow move cursor down one line.
  61. // home move cursor to start of current line
  62. // end move cursor to end of current line
  63. // backspace delete prev char or selected text.
  64. // Trying to move to the prev/next char/line/word when there is none moves the cursor to the
  65. // start/end of the text.
  66. // Horizontal scrolling:
  67. // Trying to move to the prev/next char/line/word when there is none scrolls the text
  68. // horizontally in the window so the new text displays at the correct side.
  69. // When moving to prev chars scrolling is staggered. To next chars it is one char at a time.
  70. // Cut/Copy/Paste Menu:
  71. // Right clicking mouse brings up cut/copy/paste menu.
  72. // If no text is highlighted the cut/copy options are dimmed. Cut is dimmed in non editable panels
  73. // If there is no text in the clipboard or panel is not editable the paste option is dimmed.
  74. // If the mouse is right clicked over selected text, the text stays selected.
  75. // If the mouse is right clicked over unselected text, any selected text is deselected.
  76. //
  77. //
  78. //-----------------------------------------------------------------------------
  79. class TextEntry : public Panel
  80. {
  81. DECLARE_CLASS_SIMPLE( TextEntry, Panel );
  82. public:
  83. TextEntry(Panel *parent, const char *panelName);
  84. virtual ~TextEntry();
  85. virtual void SetText(const wchar_t *wszText);
  86. virtual void SetText(const char *text);
  87. virtual void GetText(OUT_Z_BYTECAP(bufLenInBytes) char *buf, int bufLenInBytes);
  88. virtual void GetText(OUT_Z_BYTECAP(bufLenInBytes) wchar_t *buf, int bufLenInBytes);
  89. virtual int GetTextLength() const;
  90. virtual bool IsTextFullySelected() const;
  91. // editing
  92. virtual void GotoLeft(); // move cursor one char left
  93. virtual void GotoRight(); // move cursor one char right
  94. virtual void GotoUp(); // move cursor one line up
  95. virtual void GotoDown(); // move cursor one line down
  96. virtual void GotoWordRight(); // move cursor to Start of next word
  97. virtual void GotoWordLeft(); // move cursor to Start of prev word
  98. virtual void GotoFirstOfLine(); // go to Start of the current line
  99. virtual void GotoEndOfLine(); // go to end of the current line
  100. virtual void GotoTextStart(); // go to Start of text buffer
  101. virtual void GotoTextEnd(); // go to end of text buffer
  102. virtual void InsertChar(wchar_t ch);
  103. virtual void InsertString(const char *text);
  104. virtual void InsertString(const wchar_t *wszText);
  105. virtual void Backspace();
  106. virtual void Delete();
  107. virtual void SelectNone();
  108. virtual void OpenEditMenu();
  109. MESSAGE_FUNC( CutSelected, "DoCutSelected" );
  110. MESSAGE_FUNC( CopySelected, "DoCopySelected" );
  111. MESSAGE_FUNC( Paste, "DoPaste" );
  112. MESSAGE_FUNC_INT( LanguageChanged, "DoLanguageChanged", handle );
  113. MESSAGE_FUNC_INT( ConversionModeChanged, "DoConversionModeChanged", handle );
  114. MESSAGE_FUNC_INT( SentenceModeChanged, "DoSentenceModeChanged", handle );
  115. MESSAGE_FUNC_WCHARPTR( CompositionString, "DoCompositionString", string );
  116. MESSAGE_FUNC( ShowIMECandidates, "DoShowIMECandidates" );
  117. MESSAGE_FUNC( HideIMECandidates, "DoHideIMECandidates" );
  118. MESSAGE_FUNC( UpdateIMECandidates, "DoUpdateIMECandidates" );
  119. virtual void DeleteSelected();
  120. virtual void Undo();
  121. virtual void SaveUndoState();
  122. virtual void SetFont(HFont font);
  123. virtual void SetTextHidden(bool bHideText);
  124. virtual void SetEditable(bool state);
  125. virtual bool IsEditable();
  126. virtual void SetEnabled(bool state);
  127. // move the cursor to line 'line', given how many pixels are in a line
  128. virtual void MoveCursor(int line, int pixelsAcross);
  129. // sets the color of the background when the control is disabled
  130. virtual void SetDisabledBgColor(Color col);
  131. // set whether the box handles more than one line of entry
  132. virtual void SetMultiline(bool state);
  133. virtual bool IsMultiline();
  134. // sets visibility of scrollbar
  135. virtual void SetVerticalScrollbar(bool state);
  136. // sets whether or not the edit catches and stores ENTER key presses
  137. virtual void SetCatchEnterKey(bool state);
  138. // sets whether or not to send "TextNewLine" msgs when ENTER key is pressed
  139. virtual void SendNewLine(bool send);
  140. // sets limit of number of characters insertable into field; set to -1 to remove maximum
  141. // only works with if rich-edit is NOT enabled
  142. virtual void SetMaximumCharCount(int maxChars);
  143. virtual int GetMaximumCharCount();
  144. virtual void SetAutoProgressOnHittingCharLimit(bool state);
  145. // sets whether to wrap text once maxChars is reached (on a line by line basis)
  146. virtual void SetWrap(bool wrap);
  147. virtual void RecalculateLineBreaks();
  148. virtual void LayoutVerticalScrollBarSlider();
  149. virtual bool RequestInfo(KeyValues *outputData);
  150. // sets the height of the window so all text is visible.
  151. // used by tooltips
  152. void SetToFullHeight();
  153. // sets the width of the window so all text is visible. (will create one line)
  154. // used by tooltips
  155. void SetToFullWidth();
  156. int GetNumLines();
  157. /* INFO HANDLING
  158. "GetText"
  159. returns:
  160. "text" - text contained in the text box
  161. */
  162. /* CUSTOM MESSAGE HANDLING
  163. "SetText"
  164. input: "text" - text is set to be this string
  165. */
  166. /* MESSAGE SENDING (to action signal targets)
  167. "TextChanged" - sent when the text is edited by the user
  168. "TextNewLine" - sent when the end key is pressed in the text entry AND _sendNewLines is true
  169. "TextKillFocus" - sent when focus leaves textentry field
  170. */
  171. // Selects all the text in the text entry.
  172. void SelectAllText(bool bResetCursorPos);
  173. void SelectNoText();
  174. void SelectAllOnFirstFocus( bool status );
  175. void SetDrawWidth(int width); // width from right side of window we have to draw in
  176. int GetDrawWidth();
  177. void SetHorizontalScrolling(bool status); // turn horizontal scrolling on or off.
  178. // sets whether non-asci characters (unicode chars > 127) are allowed in the control - defaults to OFF
  179. void SetAllowNonAsciiCharacters(bool state);
  180. // sets whether or not number input only is allowed
  181. void SetAllowNumericInputOnly(bool state);
  182. // By default, we draw the language shortname on the right hand side of the control
  183. void SetDrawLanguageIDAtLeft( bool state );
  184. virtual bool GetDropContextMenu( Menu *menu, CUtlVector< KeyValues * >& data );
  185. virtual bool IsDroppable( CUtlVector< KeyValues * >& data );
  186. virtual void OnPanelDropped( CUtlVector< KeyValues * >& data );
  187. virtual Panel *GetDragPanel();
  188. virtual void OnCreateDragData( KeyValues *msg );
  189. void SelectAllOnFocusAlways( bool status );
  190. void SetSelectionTextColor( const Color& clr );
  191. void SetSelectionBgColor( const Color& clr );
  192. void SetSelectionUnfocusedBgColor( const Color& clr );
  193. void SetUseFallbackFont( bool bState, HFont hFallback );
  194. protected:
  195. virtual void ResetCursorBlink();
  196. virtual void PerformLayout(); // layout the text in the window
  197. virtual void ApplySchemeSettings(IScheme *pScheme);
  198. virtual void PaintBackground();
  199. virtual int DrawChar(wchar_t ch, HFont font, int index, int x, int y);
  200. virtual bool DrawCursor(int x, int y);
  201. virtual void SetCharAt(wchar_t ch, int index); // set the value of a char in the text buffer
  202. virtual void ApplySettings( KeyValues *inResourceData );
  203. virtual void GetSettings( KeyValues *outResourceData );
  204. virtual const char *GetDescription( void );
  205. virtual void FireActionSignal();
  206. virtual bool GetSelectedRange(int& cx0,int& cx1);
  207. virtual void CursorToPixelSpace(int cursorPos, int &cx, int &cy);
  208. virtual int PixelToCursorSpace(int cx, int cy);
  209. virtual void AddAnotherLine(int &cx, int &cy);
  210. virtual int GetYStart(); // works out ypixel position drawing started at
  211. virtual bool SelectCheck( bool fromMouse = false ); // check if we are in text selection mode
  212. MESSAGE_FUNC_WCHARPTR( OnSetText, "SetText", text );
  213. MESSAGE_FUNC( OnSliderMoved, "ScrollBarSliderMoved" ); // respond to scroll bar events
  214. virtual void OnKillFocus();
  215. virtual void OnMouseWheeled(int delta); // respond to mouse wheel events
  216. virtual void OnKeyCodePressed(KeyCode code); //respond to keyboard events
  217. virtual void OnKeyCodeTyped(KeyCode code); //respond to keyboard events
  218. virtual void OnKeyTyped(wchar_t unichar); //respond to keyboard events
  219. virtual void OnCursorMoved(int x, int y); // respond to moving the cursor with mouse button down
  220. virtual void OnMousePressed(MouseCode code); // respond to mouse down events
  221. virtual void OnMouseDoublePressed( MouseCode code );
  222. virtual void OnMouseTriplePressed( MouseCode code );
  223. virtual void OnMouseReleased( MouseCode code ); // respond to mouse up events
  224. virtual void OnKeyFocusTicked(); // do while window has keyboard focus
  225. virtual void OnMouseFocusTicked(); // do while window has mouse focus
  226. virtual void OnCursorEntered(); // handle cursor entering window
  227. virtual void OnCursorExited(); // handle cursor exiting window
  228. virtual void OnMouseCaptureLost();
  229. virtual void OnSizeChanged(int newWide, int newTall);
  230. // Returns the character index the drawing should Start at
  231. virtual int GetStartDrawIndex(int &lineBreakIndexIndex);
  232. public:
  233. // helper accessors for common gets
  234. virtual float GetValueAsFloat();
  235. virtual int GetValueAsInt();
  236. protected:
  237. void ScrollRight(); // scroll to right until cursor is visible
  238. void ScrollLeft(); // scroll to left
  239. bool IsCursorOffRightSideOfWindow(int cursorPos); // check if cursor is off right side of window
  240. bool IsCursorOffLeftSideOfWindow(int cursorPos); // check if cursor is off left side of window
  241. void ScrollLeftForResize();
  242. void OnSetFocus();
  243. // Change keyboard layout type
  244. void OnChangeIME( bool forward );
  245. bool NeedsEllipses( HFont font, int *pIndex );
  246. private:
  247. MESSAGE_FUNC_INT( OnSetState, "SetState", state );
  248. // get index in buffer of the Start of the current line we are on
  249. int GetCurrentLineStart();
  250. // get index in buffer of the end of the current line we are on
  251. int GetCurrentLineEnd();
  252. bool IsLineBreak(int index);
  253. int GetCursorLine();
  254. void MoveScrollBar(int delta);
  255. void CalcBreakIndex(); // calculate _recalculateLineBreaksIndex
  256. void CreateEditMenu(); // create copy/cut/paste menu
  257. public:
  258. Menu *GetEditMenu(); // retrieve copy/cut/paste menu
  259. private:
  260. void FlipToLastIME();
  261. public:
  262. virtual void GetTextRange( wchar_t *buf, int from, int numchars ); // copy a portion of the text to the buffer and add zero-termination
  263. virtual void GetTextRange( char *buf, int from, int numchars ); // copy a portion of the text to the buffer and add zero-termination
  264. private:
  265. CUtlVector<wchar_t> m_TextStream; // the text in the text window is stored in this buffer
  266. CUtlVector<wchar_t> m_UndoTextStream; // a copy of the text buffer to revert changes
  267. CUtlVector<int> m_LineBreaks; // an array that holds the index in the buffer to wrap lines at
  268. int _cursorPos; // the position in the text buffer of the blinking cursor
  269. bool _cursorIsAtEnd;
  270. bool _putCursorAtEnd;
  271. int _undoCursorPos; // a copy of the cursor position to revert changes
  272. bool _cursorBlink; // whether cursor is blinking or not
  273. bool _hideText; // whether text is visible on screen or not
  274. bool _editable; // whether text is editable or not
  275. bool _mouseSelection; // whether we are highlighting text or not (selecting text)
  276. bool _mouseDragSelection; // tells weather mouse is outside window and button is down so we select text
  277. int _mouseSelectCursorStart; // where mouse button was pressed down in text window
  278. long _cursorNextBlinkTime; // time of next cursor blink
  279. int _cursorBlinkRate; // speed of cursor blinking
  280. int _select[2]; // select[1] is the offset in the text to where the cursor is currently
  281. // select[0] is the offset to where the cursor was dragged to. or -1 if no drag.
  282. int _pixelsIndent;
  283. int _charCount;
  284. int _maxCharCount; // max number of chars that can be in the text buffer
  285. HFont _font; // font of chars in the text buffer
  286. HFont _smallfont;
  287. bool _dataChanged; // whether anything in the window has changed.
  288. bool _multiline; // whether buffer is multiline or just a single line
  289. bool _verticalScrollbar; // whether window has a vertical scroll bar
  290. ScrollBar *_vertScrollBar; // the scroll bar used in the window
  291. Color _cursorColor; // color of the text cursor
  292. Color _disabledFgColor;
  293. Color _disabledBgColor;
  294. Color _selectionColor;
  295. Color _selectionTextColor; // color of the highlighted text
  296. Color _defaultSelectionBG2Color;
  297. int _currentStartLine; // use for checking vertical text scrolling (multiline)
  298. int _currentStartIndex; // use for horizontal text scrolling (!multiline)
  299. bool _horizScrollingAllowed; // use to disable horizontal text scrolling period.
  300. Color _focusEdgeColor;
  301. bool _catchEnterKey;
  302. bool _wrap;
  303. bool _sendNewLines;
  304. int _drawWidth;
  305. // selection data
  306. Menu *m_pEditMenu; ///cut/copy/paste popup
  307. int _recalculateBreaksIndex; // tells next linebreakindex index to Start recalculating line breaks
  308. bool _selectAllOnFirstFocus : 1; // highlights all text in window when focus is gained.
  309. bool _selectAllOnFocusAlways : 1;
  310. bool _firstFocusStatus; // keep track if we've had that first focus or not
  311. bool m_bAllowNumericInputOnly;
  312. bool m_bAllowNonAsciiCharacters;
  313. bool m_bAutoProgressOnHittingCharLimit;
  314. enum
  315. {
  316. MAX_COMPOSITION_STRING = 256,
  317. };
  318. wchar_t m_szComposition[ MAX_COMPOSITION_STRING ];
  319. Menu *m_pIMECandidates;
  320. int m_hPreviousIME;
  321. bool m_bDrawLanguageIDAtLeft;
  322. int m_nLangInset;
  323. bool m_bUseFallbackFont : 1;
  324. HFont m_hFallbackFont;
  325. };
  326. }
  327. #endif // TEXTENTRY_H