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.

405 lines
16 KiB

  1. //========= Copyright � 1996-2005, 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(char *buf, int bufLen);
  88. virtual void GetText(wchar_t *buf, int bufLen);
  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 int GetTextCursorPos() { return _cursorPos; }
  103. virtual void InsertChar(wchar_t ch);
  104. virtual void InsertString(const char *text);
  105. virtual void InsertString(wchar_t *wszText);
  106. virtual void Backspace();
  107. virtual void Delete();
  108. virtual void SelectNone();
  109. virtual void OpenEditMenu();
  110. MESSAGE_FUNC( CutSelected, "DoCutSelected" );
  111. MESSAGE_FUNC( CopySelected, "DoCopySelected" );
  112. MESSAGE_FUNC( Paste, "DoPaste" );
  113. MESSAGE_FUNC_INT( LanguageChanged, "DoLanguageChanged", handle );
  114. MESSAGE_FUNC_INT( ConversionModeChanged, "DoConversionModeChanged", handle );
  115. MESSAGE_FUNC_INT( SentenceModeChanged, "DoSentenceModeChanged", handle );
  116. MESSAGE_FUNC_WCHARPTR( CompositionString, "DoCompositionString", string );
  117. MESSAGE_FUNC( ShowIMECandidates, "DoShowIMECandidates" );
  118. MESSAGE_FUNC( HideIMECandidates, "DoHideIMECandidates" );
  119. MESSAGE_FUNC( UpdateIMECandidates, "DoUpdateIMECandidates" );
  120. virtual void DeleteSelected();
  121. virtual void Undo();
  122. virtual void SaveUndoState();
  123. virtual void SetFont(HFont font);
  124. virtual void SetTextHidden(bool bHideText);
  125. virtual void SetEditable(bool state);
  126. virtual bool IsEditable();
  127. virtual void SetEnabled(bool state);
  128. // move the cursor to line 'line', given how many pixels are in a line
  129. virtual void MoveCursor(int line, int pixelsAcross);
  130. // sets the color of the background when the control is disabled
  131. virtual void SetDisabledBgColor(Color col);
  132. // set whether the box handles more than one line of entry
  133. virtual void SetMultiline(bool state);
  134. virtual bool IsMultiline();
  135. // sets visibility of scrollbar
  136. virtual void SetVerticalScrollbar(bool state);
  137. // sets whether or not the edit catches and stores ENTER key presses
  138. virtual void SetCatchEnterKey(bool state);
  139. // sets whether or not to send "TextNewLine" msgs when ENTER key is pressed
  140. virtual void SendNewLine(bool send);
  141. // sets whether or not the edit catches and stores TAB key presses
  142. virtual void SetCatchTabKey(bool state);
  143. // set the number of spaces inserted for a tab key press when catch tab is enabled
  144. virtual void SetTabSpaces(int count);
  145. // sets limit of number of characters insertable into field; set to -1 to remove maximum
  146. // only works with if rich-edit is NOT enabled
  147. virtual void SetMaximumCharCount(int maxChars);
  148. virtual int GetMaximumCharCount();
  149. virtual void SetAutoProgressOnHittingCharLimit(bool state);
  150. // sets whether to wrap text once maxChars is reached (on a line by line basis)
  151. virtual void SetWrap(bool wrap);
  152. virtual void RecalculateLineBreaks();
  153. virtual void LayoutVerticalScrollBarSlider();
  154. virtual bool RequestInfo(KeyValues *outputData);
  155. // sets the height of the window so all text is visible.
  156. // used by tooltips
  157. void SetToFullHeight();
  158. // sets the width of the window so all text is visible. (will create one line)
  159. // used by tooltips
  160. void SetToFullWidth();
  161. int GetNumLines();
  162. // gets the current starting line, the first line to be displayed
  163. int GetCurrentStartLine() const;
  164. /* INFO HANDLING
  165. "GetText"
  166. returns:
  167. "text" - text contained in the text box
  168. */
  169. /* CUSTOM MESSAGE HANDLING
  170. "SetText"
  171. input: "text" - text is set to be this string
  172. */
  173. /* MESSAGE SENDING (to action signal targets)
  174. "TextChanged" - sent when the text is edited by the user
  175. "TextNewLine" - sent when the end key is pressed in the text entry AND _sendNewLines is true
  176. "TextKillFocus" - sent when focus leaves textentry field
  177. */
  178. // Selects all the text in the text entry.
  179. void SelectAllText(bool bResetCursorPos);
  180. void SelectNoText();
  181. void SelectAllOnFirstFocus( bool status );
  182. void SetDrawWidth(int width); // width from right side of window we have to draw in
  183. int GetDrawWidth();
  184. void SetHorizontalScrolling(bool status); // turn horizontal scrolling on or off.
  185. // sets whether non-asci characters (unicode chars > 127) are allowed in the control - defaults to OFF
  186. void SetAllowNonAsciiCharacters(bool state);
  187. // sets whether or not number input only is allowed
  188. void SetAllowNumericInputOnly(bool state);
  189. // By default, we draw the language shortname on the right hand side of the control
  190. void SetDrawLanguageIDAtLeft( bool state );
  191. virtual bool GetDropContextMenu( Menu *menu, CUtlVector< KeyValues * >& data );
  192. virtual bool IsDroppable( CUtlVector< KeyValues * >& data );
  193. virtual void OnPanelDropped( CUtlVector< KeyValues * >& data );
  194. virtual Panel *GetDragPanel();
  195. virtual void OnCreateDragData( KeyValues *msg );
  196. void SelectAllOnFocusAlways( bool status );
  197. void SetSelectionTextColor( const Color& clr );
  198. void SetSelectionBgColor( const Color& clr );
  199. void SetSelectionUnfocusedBgColor( const Color& clr );
  200. void SetUseFallbackFont( bool bState, HFont hFallback );
  201. virtual void SetAutoLocalize( bool bState ) { m_bAutoLocalize = bState; }
  202. virtual void GetSizerMinimumSize(int &wide, int &tall);
  203. protected:
  204. virtual void ResetCursorBlink();
  205. virtual void PerformLayout(); // layout the text in the window
  206. virtual void ApplySchemeSettings(IScheme *pScheme);
  207. virtual void PaintBackground();
  208. virtual int DrawChar(wchar_t ch, HFont font, int index, int x, int y);
  209. virtual bool DrawCursor(int x, int y);
  210. virtual void SetCharAt(wchar_t ch, int index); // set the value of a char in the text buffer
  211. virtual void ApplySettings( KeyValues *inResourceData );
  212. virtual void GetSettings( KeyValues *outResourceData );
  213. virtual const char *GetDescription( void );
  214. virtual void FireActionSignal();
  215. virtual bool GetSelectedRange(int& cx0,int& cx1);
  216. virtual void CursorToPixelSpace(int cursorPos, int &cx, int &cy);
  217. virtual int PixelToCursorSpace(int cx, int cy);
  218. virtual void AddAnotherLine(int &cx, int &cy);
  219. virtual int GetYStart(); // works out ypixel position drawing started at
  220. virtual bool SelectCheck( bool fromMouse = false ); // check if we are in text selection mode
  221. MESSAGE_FUNC_WCHARPTR( OnSetText, "SetText", text );
  222. MESSAGE_FUNC( OnSliderMoved, "ScrollBarSliderMoved" ); // respond to scroll bar events
  223. virtual void OnKillFocus();
  224. virtual void OnMouseWheeled(int delta); // respond to mouse wheel events
  225. virtual void OnKeyCodeTyped(KeyCode code); //respond to keyboard events
  226. virtual void OnKeyTyped(wchar_t unichar); //respond to keyboard events
  227. virtual void OnCursorMoved(int x, int y); // respond to moving the cursor with mouse button down
  228. virtual void OnMousePressed(MouseCode code); // respond to mouse down events
  229. virtual void OnMouseDoublePressed( MouseCode code );
  230. virtual void OnMouseTriplePressed( MouseCode code );
  231. virtual void OnMouseReleased( MouseCode code ); // respond to mouse up events
  232. virtual void OnKeyFocusTicked(); // do while window has keyboard focus
  233. virtual void OnMouseFocusTicked(); // do while window has mouse focus
  234. virtual void OnCursorEntered(); // handle cursor entering window
  235. virtual void OnCursorExited(); // handle cursor exiting window
  236. virtual void OnMouseCaptureLost();
  237. virtual void OnSizeChanged(int newWide, int newTall);
  238. // Returns the character index the drawing should Start at
  239. virtual int GetStartDrawIndex(int &lineBreakIndexIndex);
  240. public:
  241. // helper accessors for common gets
  242. virtual float GetValueAsFloat();
  243. virtual int GetValueAsInt();
  244. protected:
  245. void ScrollRight(); // scroll to right until cursor is visible
  246. void ScrollLeft(); // scroll to left
  247. bool IsCursorOffRightSideOfWindow(int cursorPos); // check if cursor is off right side of window
  248. bool IsCursorOffLeftSideOfWindow(int cursorPos); // check if cursor is off left side of window
  249. void ScrollLeftForResize();
  250. void OnSetFocus();
  251. // Change keyboard layout type
  252. void OnChangeIME( bool forward );
  253. bool NeedsEllipses( HFont font, int *pIndex );
  254. private:
  255. MESSAGE_FUNC_INT( OnSetState, "SetState", state );
  256. // get index in buffer of the Start of the current line we are on
  257. int GetCurrentLineStart();
  258. // get index in buffer of the end of the current line we are on
  259. int GetCurrentLineEnd();
  260. bool IsLineBreak(int index);
  261. int GetCursorLine();
  262. void MoveScrollBar(int delta);
  263. void CalcBreakIndex(); // calculate _recalculateLineBreaksIndex
  264. void CreateEditMenu(); // create copy/cut/paste menu
  265. public:
  266. Menu *GetEditMenu(); // retrieve copy/cut/paste menu
  267. private:
  268. void FlipToLastIME();
  269. public:
  270. virtual void GetTextRange( wchar_t *buf, int from, int numchars ); // copy a portion of the text to the buffer and add zero-termination
  271. virtual void GetTextRange( char *buf, int from, int numchars ); // copy a portion of the text to the buffer and add zero-termination
  272. private:
  273. CUtlVector<wchar_t> m_TextStream; // the text in the text window is stored in this buffer
  274. CUtlVector<wchar_t> m_UndoTextStream; // a copy of the text buffer to revert changes
  275. CUtlVector<int> m_LineBreaks; // an array that holds the index in the buffer to wrap lines at
  276. int _cursorPos; // the position in the text buffer of the blinking cursor
  277. bool _cursorIsAtEnd;
  278. bool _putCursorAtEnd;
  279. int _undoCursorPos; // a copy of the cursor position to revert changes
  280. bool _cursorBlink; // whether cursor is blinking or not
  281. bool _hideText; // whether text is visible on screen or not
  282. bool _editable; // whether text is editable or not
  283. bool _mouseSelection; // whether we are highlighting text or not (selecting text)
  284. bool _mouseDragSelection; // tells weather mouse is outside window and button is down so we select text
  285. int _mouseSelectCursorStart; // where mouse button was pressed down in text window
  286. long _cursorNextBlinkTime; // time of next cursor blink
  287. int _cursorBlinkRate; // speed of cursor blinking
  288. int _select[2]; // select[1] is the offset in the text to where the cursor is currently
  289. // select[0] is the offset to where the cursor was dragged to. or -1 if no drag.
  290. int _pixelsIndent;
  291. int _charCount;
  292. int _maxCharCount; // max number of chars that can be in the text buffer
  293. HFont _font; // font of chars in the text buffer
  294. HFont _smallfont;
  295. bool _dataChanged; // whether anything in the window has changed.
  296. bool _multiline; // whether buffer is multiline or just a single line
  297. bool _verticalScrollbar; // whether window has a vertical scroll bar
  298. ScrollBar *_vertScrollBar; // the scroll bar used in the window
  299. Color _cursorColor; // color of the text cursor
  300. Color _disabledFgColor;
  301. Color _disabledBgColor;
  302. Color _selectionColor;
  303. Color _selectionTextColor; // color of the highlighted text
  304. Color _defaultSelectionBG2Color;
  305. int _currentStartLine; // use for checking vertical text scrolling (multiline)
  306. int _currentStartIndex; // use for horizontal text scrolling (!multiline)
  307. bool _horizScrollingAllowed; // use to disable horizontal text scrolling period.
  308. Color _focusEdgeColor;
  309. bool _catchEnterKey;
  310. bool _catchTabKey;
  311. bool _wrap;
  312. bool _sendNewLines;
  313. int _drawWidth;
  314. int _tabSpaces; // number of spaces inserted for a tab
  315. // selection data
  316. Menu *m_pEditMenu; ///cut/copy/paste popup
  317. int _recalculateBreaksIndex; // tells next linebreakindex index to Start recalculating line breaks
  318. bool _selectAllOnFirstFocus : 1; // highlights all text in window when focus is gained.
  319. bool _selectAllOnFocusAlways : 1;
  320. bool _firstFocusStatus; // keep track if we've had that first focus or not
  321. bool m_bAllowNumericInputOnly;
  322. bool m_bAllowNonAsciiCharacters;
  323. bool m_bAutoProgressOnHittingCharLimit;
  324. enum
  325. {
  326. MAX_COMPOSITION_STRING = 256,
  327. };
  328. wchar_t m_szComposition[ MAX_COMPOSITION_STRING ];
  329. Menu *m_pIMECandidates;
  330. int m_hPreviousIME;
  331. bool m_bDrawLanguageIDAtLeft;
  332. int m_nLangInset;
  333. bool m_bUseFallbackFont : 1;
  334. HFont m_hFallbackFont;
  335. bool m_bAutoLocalize;
  336. };
  337. }
  338. #endif // TEXTENTRY_H