PageRenderTime 53ms CodeModel.GetById 10ms app.highlight 37ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 27// Text editor widget to let users enter a a multi-line ASCII document//
 28
 29#ifndef LL_LLTEXTEDITOR_H
 30#define LL_LLTEXTEDITOR_H
 31
 32#include "llrect.h"
 33#include "llkeywords.h"
 34#include "llframetimer.h"
 35#include "lldarray.h"
 36#include "llstyle.h"
 37#include "lleditmenuhandler.h"
 38#include "lldarray.h"
 39#include "llviewborder.h" // for params
 40#include "lltextbase.h"
 41#include "lltextvalidate.h"
 42
 43#include "llpreeditor.h"
 44#include "llcontrol.h"
 45
 46class LLFontGL;
 47class LLScrollbar;
 48class LLKeywordToken;
 49class TextCmd;
 50class LLUICtrlFactory;
 51class LLScrollContainer;
 52
 53class LLTextEditor :
 54	public LLTextBase,
 55	protected LLPreeditor
 56{
 57public:
 58	struct Params : public LLInitParam::Block<Params, LLTextBase::Params>
 59	{
 60		Optional<std::string>	default_text;
 61		Optional<LLTextValidate::validate_func_t, LLTextValidate::ValidateTextNamedFuncs>	prevalidate_callback;
 62
 63		Optional<bool>			embedded_items,
 64								ignore_tab,
 65								show_line_numbers,
 66								commit_on_focus_lost,
 67								show_context_menu;
 68
 69		//colors
 70		Optional<LLUIColor>		default_color;
 71
 72		Params();
 73	};
 74
 75	void initFromParams(const Params&);
 76protected:
 77	LLTextEditor(const Params&);
 78	friend class LLUICtrlFactory;
 79public:
 80	//
 81	// Constants
 82	//
 83	static const llwchar FIRST_EMBEDDED_CHAR = 0x100000;
 84	static const llwchar LAST_EMBEDDED_CHAR =  0x10ffff;
 85	static const S32 MAX_EMBEDDED_ITEMS = LAST_EMBEDDED_CHAR - FIRST_EMBEDDED_CHAR + 1;
 86
 87	virtual ~LLTextEditor();
 88
 89	typedef boost::signals2::signal<void (LLTextEditor* caller)> keystroke_signal_t;
 90
 91	void	setKeystrokeCallback(const keystroke_signal_t::slot_type& callback);
 92
 93	void	setParseHighlights(BOOL parsing) {mParseHighlights=parsing;}
 94
 95	static S32		spacesPerTab();
 96
 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	handleRightMouseDown(S32 x, S32 y, MASK mask);
101	virtual BOOL	handleHover(S32 x, S32 y, MASK mask);
102	virtual BOOL	handleDoubleClick(S32 x, S32 y, MASK mask );
103	virtual BOOL	handleMiddleMouseDown(S32 x,S32 y,MASK mask);
104
105	virtual BOOL	handleKeyHere(KEY key, MASK mask );
106	virtual BOOL	handleUnicodeCharHere(llwchar uni_char);
107
108	virtual void	onMouseCaptureLost();
109
110	// view overrides
111	virtual void	draw();
112	virtual void	onFocusReceived();
113	virtual void	onFocusLost();
114	virtual void	onCommit();
115	virtual void	setEnabled(BOOL enabled);
116
117	// uictrl overrides
118	virtual void	clear();
119	virtual void	setFocus( BOOL b );
120	virtual BOOL	isDirty() const;
121
122	// LLEditMenuHandler interface
123	virtual void	undo();
124	virtual BOOL	canUndo() const;
125	virtual void	redo();
126	virtual BOOL	canRedo() const;
127
128	virtual void	cut();
129	virtual BOOL	canCut() const;
130	virtual void	copy();
131	virtual BOOL	canCopy() const;
132	virtual void	paste();
133	virtual BOOL	canPaste() const;
134 
135	virtual void	updatePrimary();
136	virtual void	copyPrimary();
137	virtual void	pastePrimary();
138	virtual BOOL	canPastePrimary() const;
139
140	virtual void	doDelete();
141	virtual BOOL	canDoDelete() const;
142	virtual void	selectAll();
143	virtual BOOL	canSelectAll()	const;
144
145	virtual bool	canLoadOrSaveToFile();
146
147	void			selectNext(const std::string& search_text_in, BOOL case_insensitive, BOOL wrap = TRUE);
148	BOOL			replaceText(const std::string& search_text, const std::string& replace_text, BOOL case_insensitive, BOOL wrap = TRUE);
149	void			replaceTextAll(const std::string& search_text, const std::string& replace_text, BOOL case_insensitive);
150	
151	// Undo/redo stack
152	void			blockUndo();
153
154	// Text editing
155	virtual void	makePristine();
156	BOOL			isPristine() const;
157	BOOL			allowsEmbeddedItems() const { return mAllowEmbeddedItems; }
158
159	//
160	// Text manipulation
161	//
162
163	// inserts text at cursor
164	void			insertText(const std::string &text);
165	void			insertText(LLWString &text);
166
167	void			appendWidget(const LLInlineViewSegment::Params& params, const std::string& text, bool allow_undo);
168	// Non-undoable
169	void			setText(const LLStringExplicit &utf8str, const LLStyle::Params& input_params = LLStyle::Params());
170
171
172	// Removes text from the end of document
173	// Does not change highlight or cursor position.
174	void 			removeTextFromEnd(S32 num_chars);
175
176	BOOL			tryToRevertToPristineState();
177
178	void			setCursorAndScrollToEnd();
179
180	void			getCurrentLineAndColumn( S32* line, S32* col, BOOL include_wordwrap );
181
182	void			loadKeywords(const std::string& filename,
183								 const std::vector<std::string>& funcs,
184								 const std::vector<std::string>& tooltips,
185								 const LLColor3& func_color);
186	LLKeywords::keyword_iterator_t keywordsBegin()	{ return mKeywords.begin(); }
187	LLKeywords::keyword_iterator_t keywordsEnd()	{ return mKeywords.end(); }
188
189	// Hacky methods to make it into a word-wrapping, potentially scrolling,
190	// read-only text box.
191	void			setCommitOnFocusLost(BOOL b)			{ mCommitOnFocusLost = b; }
192
193	// Hack to handle Notecards
194	virtual BOOL	importBuffer(const char* buffer, S32 length );
195	virtual BOOL	exportBuffer(std::string& buffer );
196
197	const LLUUID&	getSourceID() const						{ return mSourceID; }
198
199	const LLTextSegmentPtr	getPreviousSegment() const;
200	void getSelectedSegments(segment_vec_t& segments) const;
201
202	void			setShowContextMenu(bool show) { mShowContextMenu = show; }
203	bool			getShowContextMenu() const { return mShowContextMenu; }
204
205protected:
206	void			showContextMenu(S32 x, S32 y);
207	void			drawPreeditMarker();
208
209	void 			assignEmbedded(const std::string &s);
210	
211	void			removeCharOrTab();
212
213	void			indentSelectedLines( S32 spaces );
214	S32				indentLine( S32 pos, S32 spaces );
215	void			unindentLineBeforeCloseBrace();
216
217	BOOL			handleNavigationKey(const KEY key, const MASK mask);
218	BOOL			handleSpecialKey(const KEY key, const MASK mask);
219	BOOL			handleSelectionKey(const KEY key, const MASK mask);
220	BOOL			handleControlKey(const KEY key, const MASK mask);
221
222	BOOL			selectionContainsLineBreaks();
223	void			deleteSelection(BOOL transient_operation);
224
225	S32				prevWordPos(S32 cursorPos) const;
226	S32				nextWordPos(S32 cursorPos) const;
227
228	void			autoIndent();
229	
230	void			findEmbeddedItemSegments(S32 start, S32 end);
231	void			getSegmentsInRange(segment_vec_t& segments, S32 start, S32 end, bool include_partial) const;
232
233	virtual llwchar	pasteEmbeddedItem(llwchar ext_char) { return ext_char; }
234	
235
236	// Here's the method that takes and applies text commands.
237	S32 			execute(TextCmd* cmd);
238
239	// Undoable operations
240	void			addChar(llwchar c); // at mCursorPos
241	S32				addChar(S32 pos, llwchar wc);
242	void			addLineBreakChar();
243	S32				overwriteChar(S32 pos, llwchar wc);
244	void			removeChar();
245	S32 			removeChar(S32 pos);
246	S32				insert(S32 pos, const LLWString &wstr, bool group_with_next_op, LLTextSegmentPtr segment);
247	S32				remove(S32 pos, S32 length, bool group_with_next_op);
248	
249	void			updateAllowingLanguageInput();
250	BOOL			hasPreeditString() const;
251
252	// Overrides LLPreeditor
253	virtual void	resetPreedit();
254	virtual void	updatePreedit(const LLWString &preedit_string,
255						const segment_lengths_t &preedit_segment_lengths, const standouts_t &preedit_standouts, S32 caret_position);
256	virtual void	markAsPreedit(S32 position, S32 length);
257	virtual void	getPreeditRange(S32 *position, S32 *length) const;
258	virtual void	getSelectionRange(S32 *position, S32 *length) const;
259	virtual BOOL	getPreeditLocation(S32 query_offset, LLCoordGL *coord, LLRect *bounds, LLRect *control) const;
260	virtual S32		getPreeditFontSize() const;
261	virtual LLWString getPreeditString() const { return getWText(); }
262	//
263	// Protected data
264	//
265	// Probably deserves serious thought to hiding as many of these 
266	// as possible behind protected accessor methods.
267	//
268
269	// Use these to determine if a click on an embedded item is a drag or not.
270	S32				mMouseDownX;
271	S32				mMouseDownY;
272	
273	LLWString			mPreeditWString;
274	LLWString			mPreeditOverwrittenWString;
275	std::vector<S32> 	mPreeditPositions;
276	std::vector<BOOL> 	mPreeditStandouts;
277
278protected:
279	LLUIColor			mDefaultColor;
280
281	BOOL				mShowLineNumbers;
282
283	/*virtual*/ void	updateSegments();
284	void				updateLinkSegments();
285
286private:
287	//
288	// Methods
289	//
290	void	        pasteHelper(bool is_primary);
291
292	void			drawLineNumbers();
293
294	void			onKeyStroke();
295
296	//
297	// Data
298	//
299	LLKeywords		mKeywords;
300
301	// Concrete TextCmd sub-classes used by the LLTextEditor base class
302	class TextCmdInsert;
303	class TextCmdAddChar;
304	class TextCmdOverwriteChar;
305	class TextCmdRemove;
306
307	class LLViewBorder*	mBorder;
308
309	BOOL			mBaseDocIsPristine;
310	TextCmd*		mPristineCmd;
311
312	TextCmd*		mLastCmd;
313
314	typedef std::deque<TextCmd*> undo_stack_t;
315	undo_stack_t	mUndoStack;
316
317	BOOL			mTabsToNextField;		// if true, tab moves focus to next field, else inserts spaces
318	BOOL			mCommitOnFocusLost;
319	BOOL			mTakesFocus;
320
321	BOOL			mAllowEmbeddedItems;
322	bool			mShowContextMenu;
323	bool			mParseOnTheFly;
324
325	LLUUID			mSourceID;
326
327	LLCoordGL		mLastIMEPosition;		// Last position of the IME editor
328
329	keystroke_signal_t mKeystrokeSignal;
330	LLTextValidate::validate_func_t mPrevalidateFunc;
331
332	LLContextMenu* mContextMenu;
333}; // end class LLTextEditor
334
335// Build time optimization, generate once in .cpp file
336#ifndef LLTEXTEDITOR_CPP
337extern template class LLTextEditor* LLView::getChild<class LLTextEditor>(
338	const std::string& name, BOOL recurse) const;
339#endif
340
341#endif  // LL_TEXTEDITOR_