PageRenderTime 87ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llui/lllineeditor.h

https://bitbucket.org/lindenlab/viewer-beta/
C Header | 410 lines | 273 code | 78 blank | 59 comment | 1 complexity | 8f0752a0a149811ce23a954227aef382 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lllineeditor.h
  3. * @brief Text editor widget to let users enter/edit a single line.
  4. *
  5. * Features:
  6. * Text entry of a single line (text, delete, left and right arrow, insert, return).
  7. * Callbacks either on every keystroke or just on the return key.
  8. * Focus (allow multiple text entry widgets)
  9. * Clipboard (cut, copy, and paste)
  10. * Horizontal scrolling to allow strings longer than widget size allows
  11. * Pre-validation (limit which keys can be used)
  12. * Optional line history so previous entries can be recalled by CTRL UP/DOWN
  13. *
  14. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  15. * Second Life Viewer Source Code
  16. * Copyright (C) 2010, Linden Research, Inc.
  17. *
  18. * This library is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU Lesser General Public
  20. * License as published by the Free Software Foundation;
  21. * version 2.1 of the License only.
  22. *
  23. * This library is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. * Lesser General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Lesser General Public
  29. * License along with this library; if not, write to the Free Software
  30. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  31. *
  32. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  33. * $/LicenseInfo$
  34. */
  35. #ifndef LL_LLLINEEDITOR_H
  36. #define LL_LLLINEEDITOR_H
  37. #include "v4color.h"
  38. #include "llframetimer.h"
  39. #include "lleditmenuhandler.h"
  40. #include "lluictrl.h"
  41. #include "lluiimage.h"
  42. #include "lluistring.h"
  43. #include "llviewborder.h"
  44. #include "llpreeditor.h"
  45. #include "lltextvalidate.h"
  46. class LLFontGL;
  47. class LLLineEditorRollback;
  48. class LLButton;
  49. class LLContextMenu;
  50. class LLLineEditor
  51. : public LLUICtrl, public LLEditMenuHandler, protected LLPreeditor
  52. {
  53. public:
  54. typedef boost::function<void (LLLineEditor* caller)> keystroke_callback_t;
  55. struct MaxLength : public LLInitParam::ChoiceBlock<MaxLength>
  56. {
  57. Alternative<S32> bytes, chars;
  58. MaxLength() : bytes("max_length_bytes", 254),
  59. chars("max_length_chars", 0)
  60. {}
  61. };
  62. struct Params : public LLInitParam::Block<Params, LLUICtrl::Params>
  63. {
  64. Optional<std::string> default_text;
  65. Optional<MaxLength> max_length;
  66. Optional<keystroke_callback_t> keystroke_callback;
  67. Optional<LLTextValidate::validate_func_t, LLTextValidate::ValidateTextNamedFuncs> prevalidate_callback;
  68. Optional<LLTextValidate::validate_func_t, LLTextValidate::ValidateTextNamedFuncs> prevalidate_input_callback;
  69. Optional<LLViewBorder::Params> border;
  70. Optional<LLUIImage*> background_image,
  71. background_image_disabled,
  72. background_image_focused;
  73. Optional<bool> select_on_focus,
  74. revert_on_esc,
  75. commit_on_focus_lost,
  76. ignore_tab,
  77. is_password;
  78. // colors
  79. Optional<LLUIColor> cursor_color,
  80. text_color,
  81. text_readonly_color,
  82. text_tentative_color,
  83. highlight_color,
  84. preedit_bg_color;
  85. Optional<S32> text_pad_left,
  86. text_pad_right;
  87. Ignored bg_visible;
  88. Params();
  89. };
  90. protected:
  91. LLLineEditor(const Params&);
  92. friend class LLUICtrlFactory;
  93. friend class LLFloaterEditUI;
  94. void showContextMenu(S32 x, S32 y);
  95. public:
  96. virtual ~LLLineEditor();
  97. // mousehandler overrides
  98. /*virtual*/ BOOL handleMouseDown(S32 x, S32 y, MASK mask);
  99. /*virtual*/ BOOL handleMouseUp(S32 x, S32 y, MASK mask);
  100. /*virtual*/ BOOL handleHover(S32 x, S32 y, MASK mask);
  101. /*virtual*/ BOOL handleDoubleClick(S32 x,S32 y,MASK mask);
  102. /*virtual*/ BOOL handleMiddleMouseDown(S32 x,S32 y,MASK mask);
  103. /*virtual*/ BOOL handleRightMouseDown(S32 x, S32 y, MASK mask);
  104. /*virtual*/ BOOL handleKeyHere(KEY key, MASK mask );
  105. /*virtual*/ BOOL handleUnicodeCharHere(llwchar uni_char);
  106. /*virtual*/ void onMouseCaptureLost();
  107. // LLEditMenuHandler overrides
  108. virtual void cut();
  109. virtual BOOL canCut() const;
  110. virtual void copy();
  111. virtual BOOL canCopy() const;
  112. virtual void paste();
  113. virtual BOOL canPaste() const;
  114. virtual void updatePrimary();
  115. virtual void copyPrimary();
  116. virtual void pastePrimary();
  117. virtual BOOL canPastePrimary() const;
  118. virtual void doDelete();
  119. virtual BOOL canDoDelete() const;
  120. virtual void selectAll();
  121. virtual BOOL canSelectAll() const;
  122. virtual void deselect();
  123. virtual BOOL canDeselect() const;
  124. // view overrides
  125. virtual void draw();
  126. virtual void reshape(S32 width,S32 height,BOOL called_from_parent=TRUE);
  127. virtual void onFocusReceived();
  128. virtual void onFocusLost();
  129. virtual void setEnabled(BOOL enabled);
  130. // UI control overrides
  131. virtual void clear();
  132. virtual void onTabInto();
  133. virtual void setFocus( BOOL b );
  134. virtual void setRect(const LLRect& rect);
  135. virtual BOOL acceptsTextInput() const;
  136. virtual void onCommit();
  137. virtual BOOL isDirty() const; // Returns TRUE if user changed value at all
  138. virtual void resetDirty(); // Clear dirty state
  139. // assumes UTF8 text
  140. virtual void setValue(const LLSD& value );
  141. virtual LLSD getValue() const;
  142. virtual BOOL setTextArg( const std::string& key, const LLStringExplicit& text );
  143. virtual BOOL setLabelArg( const std::string& key, const LLStringExplicit& text );
  144. void setLabel(const LLStringExplicit &new_label) { mLabel = new_label; }
  145. const std::string& getLabel() { return mLabel.getString(); }
  146. void setText(const LLStringExplicit &new_text);
  147. const std::string& getText() const { return mText.getString(); }
  148. LLWString getWText() const { return mText.getWString(); }
  149. LLWString getConvertedText() const; // trimmed text with paragraphs converted to newlines
  150. S32 getLength() const { return mText.length(); }
  151. S32 getCursor() const { return mCursorPos; }
  152. void setCursor( S32 pos );
  153. void setCursorToEnd();
  154. // Selects characters 'start' to 'end'.
  155. void setSelection(S32 start, S32 end);
  156. virtual void getSelectionRange(S32 *position, S32 *length) const;
  157. void setCommitOnFocusLost( BOOL b ) { mCommitOnFocusLost = b; }
  158. void setRevertOnEsc( BOOL b ) { mRevertOnEsc = b; }
  159. void setCursorColor(const LLColor4& c) { mCursorColor = c; }
  160. const LLColor4& getCursorColor() const { return mCursorColor.get(); }
  161. void setFgColor( const LLColor4& c ) { mFgColor = c; }
  162. void setReadOnlyFgColor( const LLColor4& c ) { mReadOnlyFgColor = c; }
  163. void setTentativeFgColor(const LLColor4& c) { mTentativeFgColor = c; }
  164. const LLColor4& getFgColor() const { return mFgColor.get(); }
  165. const LLColor4& getReadOnlyFgColor() const { return mReadOnlyFgColor.get(); }
  166. const LLColor4& getTentativeFgColor() const { return mTentativeFgColor.get(); }
  167. const LLFontGL* getFont() const { return mGLFont; }
  168. void setFont(const LLFontGL* font);
  169. void setIgnoreArrowKeys(BOOL b) { mIgnoreArrowKeys = b; }
  170. void setIgnoreTab(BOOL b) { mIgnoreTab = b; }
  171. void setPassDelete(BOOL b) { mPassDelete = b; }
  172. void setDrawAsterixes(BOOL b);
  173. // get the cursor position of the beginning/end of the prev/next word in the text
  174. S32 prevWordPos(S32 cursorPos) const;
  175. S32 nextWordPos(S32 cursorPos) const;
  176. BOOL hasSelection() const { return (mSelectionStart != mSelectionEnd); }
  177. void startSelection();
  178. void endSelection();
  179. void extendSelection(S32 new_cursor_pos);
  180. void deleteSelection();
  181. void setSelectAllonFocusReceived(BOOL b);
  182. void setSelectAllonCommit(BOOL b) { mSelectAllonCommit = b; }
  183. typedef boost::function<void (LLLineEditor* caller, void* user_data)> callback_t;
  184. void setKeystrokeCallback(callback_t callback, void* user_data);
  185. void setMaxTextLength(S32 max_text_length);
  186. void setMaxTextChars(S32 max_text_chars);
  187. // Manipulate left and right padding for text
  188. void getTextPadding(S32 *left, S32 *right);
  189. void setTextPadding(S32 left, S32 right);
  190. // Prevalidation controls which keystrokes can affect the editor
  191. void setPrevalidate( LLTextValidate::validate_func_t func );
  192. // This method sets callback that prevents from:
  193. // - deleting, selecting, typing, cutting, pasting characters that are not valid.
  194. // Also callback that this method sets differs from setPrevalidate in a way that it validates just inputed
  195. // symbols, before existing text is modified, but setPrevalidate validates line after it was modified.
  196. void setPrevalidateInput(LLTextValidate::validate_func_t func);
  197. static BOOL postvalidateFloat(const std::string &str);
  198. bool prevalidateInput(const LLWString& wstr);
  199. BOOL evaluateFloat();
  200. // line history support:
  201. void setEnableLineHistory( BOOL enabled ) { mHaveHistory = enabled; } // switches line history on or off
  202. void updateHistory(); // stores current line in history
  203. void setReplaceNewlinesWithSpaces(BOOL replace);
  204. void setContextMenu(LLContextMenu* new_context_menu);
  205. private:
  206. // private helper methods
  207. void pasteHelper(bool is_primary);
  208. void removeChar();
  209. void addChar(const llwchar c);
  210. void setCursorAtLocalPos(S32 local_mouse_x);
  211. S32 findPixelNearestPos(S32 cursor_offset = 0) const;
  212. S32 calcCursorPos(S32 mouse_x);
  213. BOOL handleSpecialKey(KEY key, MASK mask);
  214. BOOL handleSelectionKey(KEY key, MASK mask);
  215. BOOL handleControlKey(KEY key, MASK mask);
  216. S32 handleCommitKey(KEY key, MASK mask);
  217. void updateTextPadding();
  218. // Draw the background image depending on enabled/focused state.
  219. void drawBackground();
  220. //
  221. // private data members
  222. //
  223. void updateAllowingLanguageInput();
  224. BOOL hasPreeditString() const;
  225. // Implementation (overrides) of LLPreeditor
  226. virtual void resetPreedit();
  227. virtual void updatePreedit(const LLWString &preedit_string,
  228. const segment_lengths_t &preedit_segment_lengths, const standouts_t &preedit_standouts, S32 caret_position);
  229. virtual void markAsPreedit(S32 position, S32 length);
  230. virtual void getPreeditRange(S32 *position, S32 *length) const;
  231. virtual BOOL getPreeditLocation(S32 query_position, LLCoordGL *coord, LLRect *bounds, LLRect *control) const;
  232. virtual S32 getPreeditFontSize() const;
  233. virtual LLWString getPreeditString() const { return getWText(); }
  234. protected:
  235. LLUIString mText; // The string being edited.
  236. std::string mPrevText; // Saved string for 'ESC' revert
  237. LLUIString mLabel; // text label that is visible when no user text provided
  238. // line history support:
  239. BOOL mHaveHistory; // flag for enabled line history
  240. typedef std::vector<std::string> line_history_t;
  241. line_history_t mLineHistory; // line history storage
  242. line_history_t::iterator mCurrentHistoryLine; // currently browsed history line
  243. LLViewBorder* mBorder;
  244. const LLFontGL* mGLFont;
  245. S32 mMaxLengthBytes; // Max length of the UTF8 string in bytes
  246. S32 mMaxLengthChars; // Maximum number of characters in the string
  247. S32 mCursorPos; // I-beam is just after the mCursorPos-th character.
  248. S32 mScrollHPos; // Horizontal offset from the start of mText. Used for scrolling.
  249. LLFrameTimer mScrollTimer;
  250. S32 mTextPadLeft; // Used to reserve space before the beginning of the text for children.
  251. S32 mTextPadRight; // Used to reserve space after the end of the text for children.
  252. S32 mTextLeftEdge; // Pixels, cached left edge of text based on left padding and width
  253. S32 mTextRightEdge; // Pixels, cached right edge of text based on right padding and width
  254. BOOL mCommitOnFocusLost;
  255. BOOL mRevertOnEsc;
  256. keystroke_callback_t mKeystrokeCallback;
  257. BOOL mIsSelecting; // Selection for clipboard operations
  258. S32 mSelectionStart;
  259. S32 mSelectionEnd;
  260. S32 mLastSelectionX;
  261. S32 mLastSelectionY;
  262. S32 mLastSelectionStart;
  263. S32 mLastSelectionEnd;
  264. LLTextValidate::validate_func_t mPrevalidateFunc;
  265. LLTextValidate::validate_func_t mPrevalidateInputFunc;
  266. LLFrameTimer mKeystrokeTimer;
  267. LLTimer mTripleClickTimer;
  268. LLUIColor mCursorColor;
  269. LLUIColor mFgColor;
  270. LLUIColor mReadOnlyFgColor;
  271. LLUIColor mTentativeFgColor;
  272. LLUIColor mHighlightColor; // background for selected text
  273. LLUIColor mPreeditBgColor; // preedit marker background color
  274. S32 mBorderThickness;
  275. BOOL mIgnoreArrowKeys;
  276. BOOL mIgnoreTab;
  277. BOOL mDrawAsterixes;
  278. BOOL mSelectAllonFocusReceived;
  279. BOOL mSelectAllonCommit;
  280. BOOL mPassDelete;
  281. BOOL mReadOnly;
  282. LLWString mPreeditWString;
  283. LLWString mPreeditOverwrittenWString;
  284. std::vector<S32> mPreeditPositions;
  285. LLPreeditor::standouts_t mPreeditStandouts;
  286. LLHandle<LLContextMenu> mContextMenuHandle;
  287. private:
  288. // Instances that by default point to the statics but can be overidden in XML.
  289. LLPointer<LLUIImage> mBgImage;
  290. LLPointer<LLUIImage> mBgImageDisabled;
  291. LLPointer<LLUIImage> mBgImageFocused;
  292. BOOL mReplaceNewlinesWithSpaces; // if false, will replace pasted newlines with paragraph symbol.
  293. // private helper class
  294. class LLLineEditorRollback
  295. {
  296. public:
  297. LLLineEditorRollback( LLLineEditor* ed )
  298. :
  299. mCursorPos( ed->mCursorPos ),
  300. mScrollHPos( ed->mScrollHPos ),
  301. mIsSelecting( ed->mIsSelecting ),
  302. mSelectionStart( ed->mSelectionStart ),
  303. mSelectionEnd( ed->mSelectionEnd )
  304. {
  305. mText = ed->getText();
  306. }
  307. void doRollback( LLLineEditor* ed )
  308. {
  309. ed->mCursorPos = mCursorPos;
  310. ed->mScrollHPos = mScrollHPos;
  311. ed->mIsSelecting = mIsSelecting;
  312. ed->mSelectionStart = mSelectionStart;
  313. ed->mSelectionEnd = mSelectionEnd;
  314. ed->mText = mText;
  315. ed->mPrevText = mText;
  316. }
  317. std::string getText() { return mText; }
  318. private:
  319. std::string mText;
  320. S32 mCursorPos;
  321. S32 mScrollHPos;
  322. BOOL mIsSelecting;
  323. S32 mSelectionStart;
  324. S32 mSelectionEnd;
  325. }; // end class LLLineEditorRollback
  326. }; // end class LLLineEditor
  327. // Build time optimization, generate once in .cpp file
  328. #ifndef LLLINEEDITOR_CPP
  329. extern template class LLLineEditor* LLView::getChild<class LLLineEditor>(
  330. const std::string& name, BOOL recurse) const;
  331. #endif
  332. #endif // LL_LINEEDITOR_