/indra/llui/lltexteditor.h

https://bitbucket.org/lindenlab/viewer-beta/ · C Header · 341 lines · 198 code · 79 blank · 64 comment · 0 complexity · 1f44670bf9a062d5233296ae9cea8af2 MD5 · raw file

  1. /**
  2. * @file lltexteditor.h
  3. * @brief LLTextEditor base class
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. // Text editor widget to let users enter a a multi-line ASCII document//
  27. #ifndef LL_LLTEXTEDITOR_H
  28. #define LL_LLTEXTEDITOR_H
  29. #include "llrect.h"
  30. #include "llkeywords.h"
  31. #include "llframetimer.h"
  32. #include "lldarray.h"
  33. #include "llstyle.h"
  34. #include "lleditmenuhandler.h"
  35. #include "lldarray.h"
  36. #include "llviewborder.h" // for params
  37. #include "lltextbase.h"
  38. #include "lltextvalidate.h"
  39. #include "llpreeditor.h"
  40. #include "llcontrol.h"
  41. class LLFontGL;
  42. class LLScrollbar;
  43. class LLKeywordToken;
  44. class TextCmd;
  45. class LLUICtrlFactory;
  46. class LLScrollContainer;
  47. class LLTextEditor :
  48. public LLTextBase,
  49. protected LLPreeditor
  50. {
  51. public:
  52. struct Params : public LLInitParam::Block<Params, LLTextBase::Params>
  53. {
  54. Optional<std::string> default_text;
  55. Optional<LLTextValidate::validate_func_t, LLTextValidate::ValidateTextNamedFuncs> prevalidate_callback;
  56. Optional<bool> embedded_items,
  57. ignore_tab,
  58. show_line_numbers,
  59. commit_on_focus_lost,
  60. show_context_menu;
  61. //colors
  62. Optional<LLUIColor> default_color;
  63. Params();
  64. };
  65. void initFromParams(const Params&);
  66. protected:
  67. LLTextEditor(const Params&);
  68. friend class LLUICtrlFactory;
  69. public:
  70. //
  71. // Constants
  72. //
  73. static const llwchar FIRST_EMBEDDED_CHAR = 0x100000;
  74. static const llwchar LAST_EMBEDDED_CHAR = 0x10ffff;
  75. static const S32 MAX_EMBEDDED_ITEMS = LAST_EMBEDDED_CHAR - FIRST_EMBEDDED_CHAR + 1;
  76. virtual ~LLTextEditor();
  77. typedef boost::signals2::signal<void (LLTextEditor* caller)> keystroke_signal_t;
  78. void setKeystrokeCallback(const keystroke_signal_t::slot_type& callback);
  79. void setParseHighlights(BOOL parsing) {mParseHighlights=parsing;}
  80. static S32 spacesPerTab();
  81. // mousehandler overrides
  82. virtual BOOL handleMouseDown(S32 x, S32 y, MASK mask);
  83. virtual BOOL handleMouseUp(S32 x, S32 y, MASK mask);
  84. virtual BOOL handleRightMouseDown(S32 x, S32 y, MASK mask);
  85. virtual BOOL handleHover(S32 x, S32 y, MASK mask);
  86. virtual BOOL handleDoubleClick(S32 x, S32 y, MASK mask );
  87. virtual BOOL handleMiddleMouseDown(S32 x,S32 y,MASK mask);
  88. virtual BOOL handleKeyHere(KEY key, MASK mask );
  89. virtual BOOL handleUnicodeCharHere(llwchar uni_char);
  90. virtual void onMouseCaptureLost();
  91. // view overrides
  92. virtual void draw();
  93. virtual void onFocusReceived();
  94. virtual void onFocusLost();
  95. virtual void onCommit();
  96. virtual void setEnabled(BOOL enabled);
  97. // uictrl overrides
  98. virtual void clear();
  99. virtual void setFocus( BOOL b );
  100. virtual BOOL isDirty() const;
  101. // LLEditMenuHandler interface
  102. virtual void undo();
  103. virtual BOOL canUndo() const;
  104. virtual void redo();
  105. virtual BOOL canRedo() const;
  106. virtual void cut();
  107. virtual BOOL canCut() const;
  108. virtual void copy();
  109. virtual BOOL canCopy() const;
  110. virtual void paste();
  111. virtual BOOL canPaste() const;
  112. virtual void updatePrimary();
  113. virtual void copyPrimary();
  114. virtual void pastePrimary();
  115. virtual BOOL canPastePrimary() const;
  116. virtual void doDelete();
  117. virtual BOOL canDoDelete() const;
  118. virtual void selectAll();
  119. virtual BOOL canSelectAll() const;
  120. virtual bool canLoadOrSaveToFile();
  121. void selectNext(const std::string& search_text_in, BOOL case_insensitive, BOOL wrap = TRUE);
  122. BOOL replaceText(const std::string& search_text, const std::string& replace_text, BOOL case_insensitive, BOOL wrap = TRUE);
  123. void replaceTextAll(const std::string& search_text, const std::string& replace_text, BOOL case_insensitive);
  124. // Undo/redo stack
  125. void blockUndo();
  126. // Text editing
  127. virtual void makePristine();
  128. BOOL isPristine() const;
  129. BOOL allowsEmbeddedItems() const { return mAllowEmbeddedItems; }
  130. //
  131. // Text manipulation
  132. //
  133. // inserts text at cursor
  134. void insertText(const std::string &text);
  135. void insertText(LLWString &text);
  136. void appendWidget(const LLInlineViewSegment::Params& params, const std::string& text, bool allow_undo);
  137. // Non-undoable
  138. void setText(const LLStringExplicit &utf8str, const LLStyle::Params& input_params = LLStyle::Params());
  139. // Removes text from the end of document
  140. // Does not change highlight or cursor position.
  141. void removeTextFromEnd(S32 num_chars);
  142. BOOL tryToRevertToPristineState();
  143. void setCursorAndScrollToEnd();
  144. void getCurrentLineAndColumn( S32* line, S32* col, BOOL include_wordwrap );
  145. void loadKeywords(const std::string& filename,
  146. const std::vector<std::string>& funcs,
  147. const std::vector<std::string>& tooltips,
  148. const LLColor3& func_color);
  149. LLKeywords::keyword_iterator_t keywordsBegin() { return mKeywords.begin(); }
  150. LLKeywords::keyword_iterator_t keywordsEnd() { return mKeywords.end(); }
  151. // Hacky methods to make it into a word-wrapping, potentially scrolling,
  152. // read-only text box.
  153. void setCommitOnFocusLost(BOOL b) { mCommitOnFocusLost = b; }
  154. // Hack to handle Notecards
  155. virtual BOOL importBuffer(const char* buffer, S32 length );
  156. virtual BOOL exportBuffer(std::string& buffer );
  157. const LLUUID& getSourceID() const { return mSourceID; }
  158. const LLTextSegmentPtr getPreviousSegment() const;
  159. void getSelectedSegments(segment_vec_t& segments) const;
  160. void setShowContextMenu(bool show) { mShowContextMenu = show; }
  161. bool getShowContextMenu() const { return mShowContextMenu; }
  162. protected:
  163. void showContextMenu(S32 x, S32 y);
  164. void drawPreeditMarker();
  165. void assignEmbedded(const std::string &s);
  166. void removeCharOrTab();
  167. void indentSelectedLines( S32 spaces );
  168. S32 indentLine( S32 pos, S32 spaces );
  169. void unindentLineBeforeCloseBrace();
  170. BOOL handleNavigationKey(const KEY key, const MASK mask);
  171. BOOL handleSpecialKey(const KEY key, const MASK mask);
  172. BOOL handleSelectionKey(const KEY key, const MASK mask);
  173. BOOL handleControlKey(const KEY key, const MASK mask);
  174. BOOL selectionContainsLineBreaks();
  175. void deleteSelection(BOOL transient_operation);
  176. S32 prevWordPos(S32 cursorPos) const;
  177. S32 nextWordPos(S32 cursorPos) const;
  178. void autoIndent();
  179. void findEmbeddedItemSegments(S32 start, S32 end);
  180. void getSegmentsInRange(segment_vec_t& segments, S32 start, S32 end, bool include_partial) const;
  181. virtual llwchar pasteEmbeddedItem(llwchar ext_char) { return ext_char; }
  182. // Here's the method that takes and applies text commands.
  183. S32 execute(TextCmd* cmd);
  184. // Undoable operations
  185. void addChar(llwchar c); // at mCursorPos
  186. S32 addChar(S32 pos, llwchar wc);
  187. void addLineBreakChar();
  188. S32 overwriteChar(S32 pos, llwchar wc);
  189. void removeChar();
  190. S32 removeChar(S32 pos);
  191. S32 insert(S32 pos, const LLWString &wstr, bool group_with_next_op, LLTextSegmentPtr segment);
  192. S32 remove(S32 pos, S32 length, bool group_with_next_op);
  193. void updateAllowingLanguageInput();
  194. BOOL hasPreeditString() const;
  195. // Overrides LLPreeditor
  196. virtual void resetPreedit();
  197. virtual void updatePreedit(const LLWString &preedit_string,
  198. const segment_lengths_t &preedit_segment_lengths, const standouts_t &preedit_standouts, S32 caret_position);
  199. virtual void markAsPreedit(S32 position, S32 length);
  200. virtual void getPreeditRange(S32 *position, S32 *length) const;
  201. virtual void getSelectionRange(S32 *position, S32 *length) const;
  202. virtual BOOL getPreeditLocation(S32 query_offset, LLCoordGL *coord, LLRect *bounds, LLRect *control) const;
  203. virtual S32 getPreeditFontSize() const;
  204. virtual LLWString getPreeditString() const { return getWText(); }
  205. //
  206. // Protected data
  207. //
  208. // Probably deserves serious thought to hiding as many of these
  209. // as possible behind protected accessor methods.
  210. //
  211. // Use these to determine if a click on an embedded item is a drag or not.
  212. S32 mMouseDownX;
  213. S32 mMouseDownY;
  214. LLWString mPreeditWString;
  215. LLWString mPreeditOverwrittenWString;
  216. std::vector<S32> mPreeditPositions;
  217. std::vector<BOOL> mPreeditStandouts;
  218. protected:
  219. LLUIColor mDefaultColor;
  220. BOOL mShowLineNumbers;
  221. /*virtual*/ void updateSegments();
  222. void updateLinkSegments();
  223. private:
  224. //
  225. // Methods
  226. //
  227. void pasteHelper(bool is_primary);
  228. void drawLineNumbers();
  229. void onKeyStroke();
  230. //
  231. // Data
  232. //
  233. LLKeywords mKeywords;
  234. // Concrete TextCmd sub-classes used by the LLTextEditor base class
  235. class TextCmdInsert;
  236. class TextCmdAddChar;
  237. class TextCmdOverwriteChar;
  238. class TextCmdRemove;
  239. class LLViewBorder* mBorder;
  240. BOOL mBaseDocIsPristine;
  241. TextCmd* mPristineCmd;
  242. TextCmd* mLastCmd;
  243. typedef std::deque<TextCmd*> undo_stack_t;
  244. undo_stack_t mUndoStack;
  245. BOOL mTabsToNextField; // if true, tab moves focus to next field, else inserts spaces
  246. BOOL mCommitOnFocusLost;
  247. BOOL mTakesFocus;
  248. BOOL mAllowEmbeddedItems;
  249. bool mShowContextMenu;
  250. bool mParseOnTheFly;
  251. LLUUID mSourceID;
  252. LLCoordGL mLastIMEPosition; // Last position of the IME editor
  253. keystroke_signal_t mKeystrokeSignal;
  254. LLTextValidate::validate_func_t mPrevalidateFunc;
  255. LLContextMenu* mContextMenu;
  256. }; // end class LLTextEditor
  257. // Build time optimization, generate once in .cpp file
  258. #ifndef LLTEXTEDITOR_CPP
  259. extern template class LLTextEditor* LLView::getChild<class LLTextEditor>(
  260. const std::string& name, BOOL recurse) const;
  261. #endif
  262. #endif // LL_TEXTEDITOR_