PageRenderTime 32ms CodeModel.GetById 8ms app.highlight 20ms RepoModel.GetById 1ms 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
  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
 36#ifndef LL_LLLINEEDITOR_H
 37#define LL_LLLINEEDITOR_H
 38
 39#include "v4color.h"
 40#include "llframetimer.h"
 41
 42#include "lleditmenuhandler.h"
 43#include "lluictrl.h"
 44#include "lluiimage.h"
 45#include "lluistring.h"
 46#include "llviewborder.h"
 47
 48#include "llpreeditor.h"
 49#include "lltextvalidate.h"
 50
 51class LLFontGL;
 52class LLLineEditorRollback;
 53class LLButton;
 54class LLContextMenu;
 55
 56class LLLineEditor
 57: public LLUICtrl, public LLEditMenuHandler, protected LLPreeditor
 58{
 59public:
 60
 61	typedef boost::function<void (LLLineEditor* caller)> keystroke_callback_t;
 62	
 63	struct MaxLength : public LLInitParam::ChoiceBlock<MaxLength>
 64	{
 65		Alternative<S32> bytes, chars;
 66		
 67		MaxLength() : bytes("max_length_bytes", 254),
 68					  chars("max_length_chars", 0) 
 69		{}
 70	};
 71
 72	struct Params : public LLInitParam::Block<Params, LLUICtrl::Params>
 73	{
 74		Optional<std::string>			default_text;
 75		Optional<MaxLength>				max_length;
 76		Optional<keystroke_callback_t>	keystroke_callback;
 77
 78		Optional<LLTextValidate::validate_func_t, LLTextValidate::ValidateTextNamedFuncs>	prevalidate_callback;
 79		Optional<LLTextValidate::validate_func_t, LLTextValidate::ValidateTextNamedFuncs>	prevalidate_input_callback;
 80		
 81		Optional<LLViewBorder::Params>	border;
 82
 83		Optional<LLUIImage*>			background_image,
 84										background_image_disabled,
 85										background_image_focused;
 86
 87		Optional<bool>					select_on_focus,
 88										revert_on_esc,
 89										commit_on_focus_lost,
 90										ignore_tab,
 91										is_password;
 92
 93		// colors
 94		Optional<LLUIColor>				cursor_color,
 95										text_color,
 96										text_readonly_color,
 97										text_tentative_color,
 98										highlight_color,
 99										preedit_bg_color;
100		
101		Optional<S32>					text_pad_left,
102										text_pad_right;
103
104		Ignored							bg_visible;
105		
106		Params();
107	};
108protected:
109	LLLineEditor(const Params&);
110	friend class LLUICtrlFactory;
111	friend class LLFloaterEditUI;
112	void showContextMenu(S32 x, S32 y);
113public:
114	virtual ~LLLineEditor();
115
116	// mousehandler overrides
117	/*virtual*/ BOOL	handleMouseDown(S32 x, S32 y, MASK mask);
118	/*virtual*/ BOOL	handleMouseUp(S32 x, S32 y, MASK mask);
119	/*virtual*/ BOOL	handleHover(S32 x, S32 y, MASK mask);
120	/*virtual*/ BOOL	handleDoubleClick(S32 x,S32 y,MASK mask);
121	/*virtual*/ BOOL	handleMiddleMouseDown(S32 x,S32 y,MASK mask);
122	/*virtual*/ BOOL	handleRightMouseDown(S32 x, S32 y, MASK mask);
123	/*virtual*/ BOOL	handleKeyHere(KEY key, MASK mask );
124	/*virtual*/ BOOL	handleUnicodeCharHere(llwchar uni_char);
125	/*virtual*/ void	onMouseCaptureLost();
126
127	// LLEditMenuHandler overrides
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
143	virtual void	selectAll();
144	virtual BOOL	canSelectAll() const;
145
146	virtual void	deselect();
147	virtual BOOL	canDeselect() const;
148
149	// view overrides
150	virtual void	draw();
151	virtual void	reshape(S32 width,S32 height,BOOL called_from_parent=TRUE);
152	virtual void	onFocusReceived();
153	virtual void	onFocusLost();
154	virtual void	setEnabled(BOOL enabled);
155
156	// UI control overrides
157	virtual void	clear();
158	virtual void	onTabInto();
159	virtual void	setFocus( BOOL b );
160	virtual void 	setRect(const LLRect& rect);
161	virtual BOOL	acceptsTextInput() const;
162	virtual void	onCommit();
163	virtual BOOL	isDirty() const;	// Returns TRUE if user changed value at all
164	virtual void	resetDirty();		// Clear dirty state
165
166	// assumes UTF8 text
167	virtual void	setValue(const LLSD& value );
168	virtual LLSD	getValue() const;
169	virtual BOOL	setTextArg( const std::string& key, const LLStringExplicit& text );
170	virtual BOOL	setLabelArg( const std::string& key, const LLStringExplicit& text );
171
172	void			setLabel(const LLStringExplicit &new_label) { mLabel = new_label; }
173	const std::string& 	getLabel()	{ return mLabel.getString(); }
174
175	void			setText(const LLStringExplicit &new_text);
176
177	const std::string& getText() const		{ return mText.getString(); }
178	LLWString       getWText() const	{ return mText.getWString(); }
179	LLWString getConvertedText() const; // trimmed text with paragraphs converted to newlines
180
181	S32				getLength() const	{ return mText.length(); }
182
183	S32				getCursor()	const	{ return mCursorPos; }
184	void			setCursor( S32 pos );
185	void			setCursorToEnd();
186
187	// Selects characters 'start' to 'end'.
188	void			setSelection(S32 start, S32 end);
189	virtual void	getSelectionRange(S32 *position, S32 *length) const;
190	
191	void			setCommitOnFocusLost( BOOL b )	{ mCommitOnFocusLost = b; }
192	void			setRevertOnEsc( BOOL b )		{ mRevertOnEsc = b; }
193
194	void setCursorColor(const LLColor4& c)			{ mCursorColor = c; }
195	const LLColor4& getCursorColor() const			{ return mCursorColor.get(); }
196
197	void setFgColor( const LLColor4& c )			{ mFgColor = c; }
198	void setReadOnlyFgColor( const LLColor4& c )	{ mReadOnlyFgColor = c; }
199	void setTentativeFgColor(const LLColor4& c)		{ mTentativeFgColor = c; }
200
201	const LLColor4& getFgColor() const			{ return mFgColor.get(); }
202	const LLColor4& getReadOnlyFgColor() const	{ return mReadOnlyFgColor.get(); }
203	const LLColor4& getTentativeFgColor() const { return mTentativeFgColor.get(); }
204
205	const LLFontGL* getFont() const { return mGLFont; }
206	void setFont(const LLFontGL* font);
207
208	void			setIgnoreArrowKeys(BOOL b)		{ mIgnoreArrowKeys = b; }
209	void			setIgnoreTab(BOOL b)			{ mIgnoreTab = b; }
210	void			setPassDelete(BOOL b)			{ mPassDelete = b; }
211	void			setDrawAsterixes(BOOL b);
212
213	// get the cursor position of the beginning/end of the prev/next word in the text
214	S32				prevWordPos(S32 cursorPos) const;
215	S32				nextWordPos(S32 cursorPos) const;
216
217	BOOL			hasSelection() const { return (mSelectionStart != mSelectionEnd); }
218	void			startSelection();
219	void			endSelection();
220	void			extendSelection(S32 new_cursor_pos);
221	void			deleteSelection();
222
223	void			setSelectAllonFocusReceived(BOOL b);
224	void			setSelectAllonCommit(BOOL b) { mSelectAllonCommit = b; }
225	
226	typedef boost::function<void (LLLineEditor* caller, void* user_data)> callback_t;
227	void			setKeystrokeCallback(callback_t callback, void* user_data);
228
229	void			setMaxTextLength(S32 max_text_length);
230	void			setMaxTextChars(S32 max_text_chars);
231	// Manipulate left and right padding for text
232	void getTextPadding(S32 *left, S32 *right);
233	void setTextPadding(S32 left, S32 right);
234
235	// Prevalidation controls which keystrokes can affect the editor
236	void			setPrevalidate( LLTextValidate::validate_func_t func );
237	// This method sets callback that prevents from:
238	// - deleting, selecting, typing, cutting, pasting characters that are not valid.
239	// Also callback that this method sets differs from setPrevalidate in a way that it validates just inputed
240	// symbols, before existing text is modified, but setPrevalidate validates line after it was modified.
241	void			setPrevalidateInput(LLTextValidate::validate_func_t func);
242	static BOOL		postvalidateFloat(const std::string &str);
243
244	bool			prevalidateInput(const LLWString& wstr);
245	BOOL			evaluateFloat();
246
247	// line history support:
248	void			setEnableLineHistory( BOOL enabled ) { mHaveHistory = enabled; } // switches line history on or off 
249	void			updateHistory(); // stores current line in history
250
251	void			setReplaceNewlinesWithSpaces(BOOL replace);
252
253	void			setContextMenu(LLContextMenu* new_context_menu);
254
255private:
256	// private helper methods
257
258	void                    pasteHelper(bool is_primary);
259
260	void			removeChar();
261	void			addChar(const llwchar c);
262	void			setCursorAtLocalPos(S32 local_mouse_x);
263	S32				findPixelNearestPos(S32 cursor_offset = 0) const;
264	S32				calcCursorPos(S32 mouse_x);
265	BOOL			handleSpecialKey(KEY key, MASK mask);
266	BOOL			handleSelectionKey(KEY key, MASK mask);
267	BOOL			handleControlKey(KEY key, MASK mask);
268	S32				handleCommitKey(KEY key, MASK mask);
269	void			updateTextPadding();
270	
271	// Draw the background image depending on enabled/focused state.
272	void			drawBackground();
273
274	//
275	// private data members
276	//
277	void			updateAllowingLanguageInput();
278	BOOL			hasPreeditString() const;
279	// Implementation (overrides) of LLPreeditor
280	virtual void	resetPreedit();
281	virtual void	updatePreedit(const LLWString &preedit_string,
282						const segment_lengths_t &preedit_segment_lengths, const standouts_t &preedit_standouts, S32 caret_position);
283	virtual void	markAsPreedit(S32 position, S32 length);
284	virtual void	getPreeditRange(S32 *position, S32 *length) const;
285	virtual BOOL	getPreeditLocation(S32 query_position, LLCoordGL *coord, LLRect *bounds, LLRect *control) const;
286	virtual S32		getPreeditFontSize() const;
287	virtual LLWString getPreeditString() const { return getWText(); }
288
289protected:
290	LLUIString		mText;					// The string being edited.
291	std::string		mPrevText;				// Saved string for 'ESC' revert
292	LLUIString		mLabel;					// text label that is visible when no user text provided
293
294	// line history support:
295	BOOL		mHaveHistory;				// flag for enabled line history
296	typedef std::vector<std::string>	line_history_t;
297	line_history_t	mLineHistory;			// line history storage
298	line_history_t::iterator	mCurrentHistoryLine;	// currently browsed history line
299
300	LLViewBorder* mBorder;
301	const LLFontGL*	mGLFont;
302	S32			mMaxLengthBytes;			// Max length of the UTF8 string in bytes
303	S32			mMaxLengthChars;			// Maximum number of characters in the string
304	S32			mCursorPos;					// I-beam is just after the mCursorPos-th character.
305	S32			mScrollHPos;				// Horizontal offset from the start of mText.  Used for scrolling.
306	LLFrameTimer mScrollTimer;
307	S32			mTextPadLeft;				// Used to reserve space before the beginning of the text for children.
308	S32			mTextPadRight;				// Used to reserve space after the end of the text for children.
309	S32			mTextLeftEdge;				// Pixels, cached left edge of text based on left padding and width
310	S32			mTextRightEdge;				// Pixels, cached right edge of text based on right padding and width
311
312	BOOL		mCommitOnFocusLost;
313	BOOL		mRevertOnEsc;
314
315	keystroke_callback_t mKeystrokeCallback;
316
317	BOOL		mIsSelecting;				// Selection for clipboard operations
318	S32			mSelectionStart;
319	S32			mSelectionEnd;
320	S32			mLastSelectionX;
321	S32			mLastSelectionY;
322	S32			mLastSelectionStart;
323	S32			mLastSelectionEnd;
324
325	LLTextValidate::validate_func_t mPrevalidateFunc;
326	LLTextValidate::validate_func_t mPrevalidateInputFunc;
327
328	LLFrameTimer mKeystrokeTimer;
329	LLTimer		mTripleClickTimer;
330
331	LLUIColor	mCursorColor;
332	LLUIColor	mFgColor;
333	LLUIColor	mReadOnlyFgColor;
334	LLUIColor	mTentativeFgColor;
335	LLUIColor	mHighlightColor;		// background for selected text
336	LLUIColor	mPreeditBgColor;		// preedit marker background color
337
338	S32			mBorderThickness;
339
340	BOOL		mIgnoreArrowKeys;
341	BOOL		mIgnoreTab;
342	BOOL		mDrawAsterixes;
343
344	BOOL		mSelectAllonFocusReceived;
345	BOOL		mSelectAllonCommit;
346	BOOL		mPassDelete;
347
348	BOOL		mReadOnly;
349
350	LLWString	mPreeditWString;
351	LLWString	mPreeditOverwrittenWString;
352	std::vector<S32> mPreeditPositions;
353	LLPreeditor::standouts_t mPreeditStandouts;
354
355	LLHandle<LLContextMenu> mContextMenuHandle;
356
357private:
358	// Instances that by default point to the statics but can be overidden in XML.
359	LLPointer<LLUIImage> mBgImage;
360	LLPointer<LLUIImage> mBgImageDisabled;
361	LLPointer<LLUIImage> mBgImageFocused;
362
363	BOOL        mReplaceNewlinesWithSpaces; // if false, will replace pasted newlines with paragraph symbol.
364
365	// private helper class
366	class LLLineEditorRollback
367	{
368	public:
369		LLLineEditorRollback( LLLineEditor* ed )
370			:
371			mCursorPos( ed->mCursorPos ),
372			mScrollHPos( ed->mScrollHPos ),
373			mIsSelecting( ed->mIsSelecting ),
374			mSelectionStart( ed->mSelectionStart ),
375			mSelectionEnd( ed->mSelectionEnd )
376		{
377			mText = ed->getText();
378		}
379
380		void doRollback( LLLineEditor* ed )
381		{
382			ed->mCursorPos = mCursorPos;
383			ed->mScrollHPos = mScrollHPos;
384			ed->mIsSelecting = mIsSelecting;
385			ed->mSelectionStart = mSelectionStart;
386			ed->mSelectionEnd = mSelectionEnd;
387			ed->mText = mText;
388			ed->mPrevText = mText;
389		}
390
391		std::string getText()   { return mText; }
392
393	private:
394		std::string mText;
395		S32		mCursorPos;
396		S32		mScrollHPos;
397		BOOL	mIsSelecting;
398		S32		mSelectionStart;
399		S32		mSelectionEnd;
400	}; // end class LLLineEditorRollback
401
402}; // end class LLLineEditor
403
404// Build time optimization, generate once in .cpp file
405#ifndef LLLINEEDITOR_CPP
406extern template class LLLineEditor* LLView::getChild<class LLLineEditor>(
407	const std::string& name, BOOL recurse) const;
408#endif
409
410#endif  // LL_LINEEDITOR_