summaryrefslogtreecommitdiff
path: root/indra/llui/lltexteditor.h
blob: a04261c4be96b116ba3e29b69833fa0a44d5f951 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/** 
 * @file lltexteditor.h
 * @brief LLTextEditor base class
 *
 * $LicenseInfo:firstyear=2001&license=viewergpl$
 * 
 * Copyright (c) 2001-2009, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at
 * http://secondlifegrid.net/programs/open_source/licensing/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

// Text editor widget to let users enter a a multi-line ASCII document//

#ifndef LL_LLTEXTEDITOR_H
#define LL_LLTEXTEDITOR_H

#include "llrect.h"
#include "llkeywords.h"
#include "lluictrl.h"
#include "llframetimer.h"
#include "lldarray.h"
#include "llstyle.h"
#include "lleditmenuhandler.h"
#include "lldarray.h"
#include "llviewborder.h" // for params
#include "lltextbase.h"

#include "llpreeditor.h"
#include "llcontrol.h"

class LLFontGL;
class LLScrollbar;
class LLKeywordToken;
class LLTextCmd;
class LLUICtrlFactory;
class LLScrollContainer;

class LLInlineViewSegment : public LLTextSegment
{
public:
	LLInlineViewSegment(LLView* widget, S32 start, S32 end);
	~LLInlineViewSegment();
	/*virtual*/ S32			getWidth(S32 first_char, S32 num_chars) const;
	/*virtual*/ S32			getNumChars(S32 num_pixels, S32 segment_offset, S32 line_offset, S32 max_chars) const;
	/*virtual*/ void		updateLayout(const class LLTextBase& editor);
	/*virtual*/ F32			draw(S32 start, S32 end, S32 selection_start, S32 selection_end, const LLRect& draw_rect);
	/*virtuaL*/ S32			getMaxHeight() const;
	/*virtual*/ bool		canEdit() const { return false; }
	/*virtual*/ void		unlinkFromDocument(class LLTextBase* editor);
	/*virtual*/ void		linkToDocument(class LLTextBase* editor);

private:
	LLView* mView;
};

class LLTextEditor :
	public LLTextBase,
	public LLUICtrl,
	private LLEditMenuHandler,
	protected LLPreeditor
{
public:
	struct Params : public LLInitParam::Block<Params, LLUICtrl::Params>
	{
		Optional<std::string>	default_text;
		Optional<S32>			max_text_length;

		Optional<bool>			read_only,
								embedded_items,
								word_wrap,
								ignore_tab,
								hide_border,
								track_bottom,
								handle_edit_keys_directly,
								show_line_numbers,
								commit_on_focus_lost;

		//colors
		Optional<LLUIColor>		cursor_color,
								default_color,
								text_color,
								text_readonly_color,
								bg_readonly_color,
								bg_writeable_color,
								bg_focus_color,
								link_color;

		Optional<LLViewBorder::Params> border;

		Ignored					type,
								length,
								is_unicode,
								hide_scrollbar;

		Params();
	};

	void initFromParams(const Params&);
protected:
	LLTextEditor(const Params&);
	friend class LLUICtrlFactory;
public:
	//
	// Constants
	//
	static const llwchar FIRST_EMBEDDED_CHAR = 0x100000;
	static const llwchar LAST_EMBEDDED_CHAR =  0x10ffff;
	static const S32 MAX_EMBEDDED_ITEMS = LAST_EMBEDDED_CHAR - FIRST_EMBEDDED_CHAR + 1;


	struct compare_segment_end
	{
		bool operator()(const LLTextSegmentPtr& a, const LLTextSegmentPtr& b) const
		{
			return a->getEnd() < b->getEnd();
		}
	};

	virtual ~LLTextEditor();

	typedef boost::signals2::signal<void (LLTextEditor* caller)> keystroke_signal_t;

	void	setKeystrokeCallback(const keystroke_signal_t::slot_type& callback);

	void	setParseHighlights(BOOL parsing) {mParseHighlights=parsing;}

	// mousehandler overrides
	virtual BOOL	handleMouseDown(S32 x, S32 y, MASK mask);
	virtual BOOL	handleMouseUp(S32 x, S32 y, MASK mask);
	virtual BOOL	handleHover(S32 x, S32 y, MASK mask);
	virtual BOOL	handleDoubleClick(S32 x, S32 y, MASK mask );
	virtual BOOL	handleMiddleMouseDown(S32 x,S32 y,MASK mask);

	virtual BOOL	handleKeyHere(KEY key, MASK mask );
	virtual BOOL	handleUnicodeCharHere(llwchar uni_char);

	virtual BOOL	handleToolTip(S32 x, S32 y, std::string& msg, LLRect& sticky_rect);
	virtual BOOL	handleDragAndDrop(S32 x, S32 y, MASK mask, BOOL drop,
									  EDragAndDropType cargo_type, void *cargo_data,
									  EAcceptance *accept, std::string& tooltip_msg);
	virtual void	onMouseCaptureLost();

	// view overrides
	virtual void	reshape(S32 width, S32 height, BOOL called_from_parent = TRUE);
	virtual void	draw();
	virtual void	onFocusReceived();
	virtual void	onFocusLost();
	virtual void	onCommit();
	virtual void	setEnabled(BOOL enabled);

	// uictrl overrides
	virtual void	clear();
	virtual void	setFocus( BOOL b );
	virtual BOOL	acceptsTextInput() const;
	virtual BOOL	isDirty() const;
	virtual void 	setValue(const LLSD& value);

	// LLEditMenuHandler interface
	virtual void	undo();
	virtual BOOL	canUndo() const;
	virtual void	redo();
	virtual BOOL	canRedo() const;

	virtual void	cut();
	virtual BOOL	canCut() const;
	virtual void	copy();
	virtual BOOL	canCopy() const;
	virtual void	paste();
	virtual BOOL	canPaste() const;
 
	virtual void	updatePrimary();
	virtual void	copyPrimary();
	virtual void	pastePrimary();
	virtual BOOL	canPastePrimary() const;

	virtual void	doDelete();
	virtual BOOL	canDoDelete() const;
	virtual void	selectAll();
	virtual BOOL	canSelectAll()	const;
	virtual void	deselect();
	virtual BOOL	canDeselect() const;

	virtual void	onValueChange(S32 start, S32 end);

	void			selectNext(const std::string& search_text_in, BOOL case_insensitive, BOOL wrap = TRUE);
	BOOL			replaceText(const std::string& search_text, const std::string& replace_text, BOOL case_insensitive, BOOL wrap = TRUE);
	void			replaceTextAll(const std::string& search_text, const std::string& replace_text, BOOL case_insensitive);
	BOOL			hasSelection() const		{ return (mSelectionStart !=mSelectionEnd); }
	void			replaceUrlLabel(const std::string &url, const std::string &label);
	
	// Undo/redo stack
	void			blockUndo();

	// Text editing
	virtual void	makePristine();
	BOOL			isPristine() const;
	BOOL			allowsEmbeddedItems() const { return mAllowEmbeddedItems; }
	S32				getLength() const { return getWText().length(); }
	void			setReadOnly(bool read_only) { mReadOnly = read_only; }
	bool			getReadOnly() { return mReadOnly; }

	//
	// Text manipulation
	//

	// inserts text at cursor
	void			insertText(const std::string &text);
	// appends text at end
	void 			appendText(const std::string &wtext, bool allow_undo, bool prepend_newline,
								const LLStyle::Params& style = LLStyle::Params());

	void 			appendColoredText(const std::string &wtext, bool allow_undo, 
									  bool prepend_newline,
									  const LLColor4 &color,
									  const std::string& font_name = LLStringUtil::null);
	// if styled text starts a line, you need to prepend a newline.
	void 			appendStyledText(const std::string &new_text, bool allow_undo, 
									 bool prepend_newline,
									 const LLStyle::Params& style);
	void			appendHighlightedText(const std::string &new_text,  bool allow_undo, 
										  bool prepend_newline,	 S32  highlight_part,
										  const LLStyle::Params& style);
	void			appendWidget(LLView* widget, const std::string &widget_text, bool allow_undo, bool prepend_newline);
	// Non-undoable
	void			setText(const LLStringExplicit &utf8str);
	void			setWText(const LLWString &wtext);


	// Removes text from the end of document
	// Does not change highlight or cursor position.
	void 			removeTextFromEnd(S32 num_chars);

	BOOL			tryToRevertToPristineState();

	bool			setCursor(S32 row, S32 column);
	bool			setCursorPos(S32 offset, bool keep_cursor_offset = false);
	void			setCursorAndScrollToEnd();

	void			getLineAndColumnForPosition( S32 position,  S32* line, S32* col, BOOL include_wordwrap );
	void			getCurrentLineAndColumn( S32* line, S32* col, BOOL include_wordwrap );
	S32				getLineForPosition(S32 position);
	S32				getCurrentLine();

	void			loadKeywords(const std::string& filename,
								 const std::vector<std::string>& funcs,
								 const std::vector<std::string>& tooltips,
								 const LLColor3& func_color);
	LLKeywords::keyword_iterator_t keywordsBegin()	{ return mKeywords.begin(); }
	LLKeywords::keyword_iterator_t keywordsEnd()	{ return mKeywords.end(); }

	// Hacky methods to make it into a word-wrapping, potentially scrolling,
	// read-only text box.
	void			setCommitOnFocusLost(BOOL b)			{ mCommitOnFocusLost = b; }

	// Hack to handle Notecards
	virtual BOOL	importBuffer(const char* buffer, S32 length );
	virtual BOOL	exportBuffer(std::string& buffer );

	const class DocumentPanel*	getDocumentPanel() const { return mDocumentPanel; }

	const LLUUID&	getSourceID() const						{ return mSourceID; }

	// Callbacks
 	std::string     getText() const;
	
	// Callback for when a Url has been resolved by the server
	void            onUrlLabelUpdated(const std::string &url, const std::string &label);

	// Getters
	LLWString       getWText() const;
	llwchar			getWChar(S32 pos) const { return getWText()[pos]; }
	LLWString		getWSubString(S32 pos, S32 len) const { return getWText().substr(pos, len); }
	
	typedef std::vector<LLTextSegmentPtr> segment_vec_t;

	const LLTextSegmentPtr	getPreviousSegment() const;
	void getSelectedSegments(segment_vec_t& segments) const;

	void getSegmentsInRange(segment_vec_t& segments, S32 start, S32 end, bool include_partial) const;
	LLRect			getLocalRectFromDocIndex(S32 index) const; 

	void			addDocumentChild(LLView* view);
	void			removeDocumentChild(LLView* view);

protected:
	// Change cursor
	void			startOfLine();
	void			endOfLine();
	void			startOfDoc();
	void			endOfDoc();

	void			drawPreeditMarker();

	void			needsReflow() { mReflowNeeded = TRUE; }
	void			needsScroll() { mScrollNeeded = TRUE; }
	void			updateCursorXPos();

	void			updateScrollFromCursor();
	void			updateTextRect();
	const LLRect&	getTextRect() const { return mTextRect; }

	void 			assignEmbedded(const std::string &s);
	BOOL 			truncate();				// Returns true if truncation occurs
	
	void			removeCharOrTab();
	void			setCursorAtLocalPos(S32 x, S32 y, bool round, bool keep_cursor_offset = false);
	/*virtual*/ S32 getDocIndexFromLocalCoord( S32 local_x, S32 local_y, BOOL round ) const;

	void			indentSelectedLines( S32 spaces );
	S32				indentLine( S32 pos, S32 spaces );
	void			unindentLineBeforeCloseBrace();

	void			reportBadKeystroke() { make_ui_sound("UISndBadKeystroke"); }

	BOOL			handleNavigationKey(const KEY key, const MASK mask);
	BOOL			handleSpecialKey(const KEY key, const MASK mask, BOOL* return_key_hit);
	BOOL			handleSelectionKey(const KEY key, const MASK mask);
	BOOL			handleControlKey(const KEY key, const MASK mask);
	BOOL			handleEditKey(const KEY key, const MASK mask);

	BOOL			selectionContainsLineBreaks();
	void			startSelection();
	void			endSelection();
	void			deleteSelection(BOOL transient_operation);

	S32				prevWordPos(S32 cursorPos) const;
	S32				nextWordPos(S32 cursorPos) const;

	S32 			getLineCount() const { return mLineInfoList.size(); }
	S32 			getLineStart( S32 line ) const;
	S32 			getLineHeight( S32 line ) const;
	void			getLineAndOffset(S32 pos, S32* linep, S32* offsetp, bool include_wordwrap = true) const;
	S32				getPos(S32 line, S32 offset);

	void			changePage(S32 delta);
	void			changeLine(S32 delta);

	void			autoIndent();
	
	void			findEmbeddedItemSegments(S32 start, S32 end);
	void			insertSegment(LLTextSegmentPtr segment_to_insert);
	
	virtual llwchar	pasteEmbeddedItem(llwchar ext_char) { return ext_char; }
	
	// Abstract inner base class representing an undoable editor command.
	// Concrete sub-classes can be defined for operations such as insert, remove, etc.
	// Used as arguments to the execute() method below.
	class LLTextCmd
	{
	public:
		LLTextCmd( S32 pos, BOOL group_with_next, LLTextSegmentPtr segment = LLTextSegmentPtr() ) 
		:	mPos(pos), 
			mGroupWithNext(group_with_next)
		{
			if (segment.notNull())
			{
				mSegments.push_back(segment);
			}
		}
		virtual			~LLTextCmd() {}
		virtual BOOL	execute(LLTextEditor* editor, S32* delta) = 0;
		virtual S32		undo(LLTextEditor* editor) = 0;
		virtual S32		redo(LLTextEditor* editor) = 0;
		virtual BOOL	canExtend(S32 pos) const { return FALSE; }
		virtual void	blockExtensions() {}
		virtual BOOL	extendAndExecute( LLTextEditor* editor, S32 pos, llwchar c, S32* delta ) { llassert(0); return 0; }
		virtual BOOL	hasExtCharValue( llwchar value ) const { return FALSE; }

		// Defined here so they can access protected LLTextEditor editing methods
		S32				insert(LLTextEditor* editor, S32 pos, const LLWString &wstr) { return editor->insertStringNoUndo( pos, wstr, &mSegments ); }
		S32 			remove(LLTextEditor* editor, S32 pos, S32 length) { return editor->removeStringNoUndo( pos, length ); }
		S32				overwrite(LLTextEditor* editor, S32 pos, llwchar wc) { return editor->overwriteCharNoUndo(pos, wc); }
		
		S32				getPosition() const { return mPos; }
		BOOL			groupWithNext() const { return mGroupWithNext; }
		
	protected:
		const S32			mPos;
		BOOL				mGroupWithNext;
		segment_vec_t		mSegments;
	};
	// Here's the method that takes and applies text commands.
	S32 			execute(LLTextCmd* cmd);

	// Undoable operations
	void			addChar(llwchar c); // at mCursorPos
	S32				addChar(S32 pos, llwchar wc);
	S32				overwriteChar(S32 pos, llwchar wc);
	void			removeChar();
	S32 			removeChar(S32 pos);
	S32				insert(S32 pos, const LLWString &wstr, bool group_with_next_op, LLTextSegmentPtr segment);
	S32				remove(S32 pos, S32 length, bool group_with_next_op);
	S32				append(const LLWString &wstr, bool group_with_next_op, LLTextSegmentPtr segment);
	
	// Direct operations
	S32				insertStringNoUndo(S32 pos, const LLWString &wstr, segment_vec_t* segments = NULL); // returns num of chars actually inserted
	S32 			removeStringNoUndo(S32 pos, S32 length);
	S32				overwriteCharNoUndo(S32 pos, llwchar wc);

	void			resetKeystrokeTimer() { mKeystrokeTimer.reset(); }

	void			updateAllowingLanguageInput();
	BOOL			hasPreeditString() const;

	// Overrides LLPreeditor
	virtual void	resetPreedit();
	virtual void	updatePreedit(const LLWString &preedit_string,
						const segment_lengths_t &preedit_segment_lengths, const standouts_t &preedit_standouts, S32 caret_position);
	virtual void	markAsPreedit(S32 position, S32 length);
	virtual void	getPreeditRange(S32 *position, S32 *length) const;
	virtual void	getSelectionRange(S32 *position, S32 *length) const;
	virtual BOOL	getPreeditLocation(S32 query_offset, LLCoordGL *coord, LLRect *bounds, LLRect *control) const;
	virtual S32		getPreeditFontSize() const;
	//
	// Protected data
	//
	// Probably deserves serious thought to hiding as many of these 
	// as possible behind protected accessor methods.
	//

	// I-beam is just after the mCursorPos-th character.
	S32				mCursorPos;

	// Use these to determine if a click on an embedded item is a drag or not.
	S32				mMouseDownX;
	S32				mMouseDownY;
	
	// Are we in the middle of a drag-select?  To figure out if there is a current
	// selection, call hasSelection().
	BOOL			mIsSelecting;
	S32				mSelectionStart;
	S32				mSelectionEnd;
	S32				mLastSelectionX;
	S32				mLastSelectionY;

	BOOL			mParseHighlights;

	// Scrollbar data
	class DocumentPanel*	mDocumentPanel;
	LLScrollContainer*	mScroller;

	void			*mOnScrollEndData;

	LLWString			mPreeditWString;
	LLWString			mPreeditOverwrittenWString;
	std::vector<S32> 	mPreeditPositions;
	std::vector<BOOL> 	mPreeditStandouts;

	S32			mScrollIndex; // index into document that controls default scroll position

protected:
	LLUIColor		mCursorColor;
	LLUIColor		mFgColor;
	LLUIColor		mDefaultColor;
	LLUIColor		mReadOnlyFgColor;
	LLUIColor		mWriteableBgColor;
	LLUIColor		mReadOnlyBgColor;
	LLUIColor		mFocusBgColor;
	LLUIColor		mLinkColor;

	BOOL			mReadOnly;
	BOOL			mShowLineNumbers;

	void			updateSegments();
	void			updateLinkSegments();

private:

	//
	// Methods
	//
	void	        pasteHelper(bool is_primary);

	virtual 		LLTextViewModel* getViewModel() const;
	void			reflow(S32 startpos = 0);

	void			createDefaultSegment();
	LLStyleSP		getDefaultStyle();
	S32				getEditableIndex(S32 index, bool increasing_direction);

	void			drawBackground();
	void			drawSelectionBackground();
	void			drawCursor();
	void			drawText();
	void			drawLineNumbers();

	S32				getFirstVisibleLine() const;

	void			onKeyStroke();

	//
	// Data
	//
	LLKeywords		mKeywords;

	// Concrete LLTextCmd sub-classes used by the LLTextEditor base class
	class LLTextCmdInsert;
	class LLTextCmdAddChar;
	class LLTextCmdOverwriteChar;
	class LLTextCmdRemove;

	S32				mMaxTextByteLength;		// Maximum length mText is allowed to be in bytes

	class LLViewBorder*	mBorder;

	BOOL			mBaseDocIsPristine;
	LLTextCmd*		mPristineCmd;

	LLTextCmd*		mLastCmd;

	typedef std::deque<LLTextCmd*> undo_stack_t;
	undo_stack_t	mUndoStack;

	S32				mDesiredXPixel;			// X pixel position where the user wants the cursor to be
	LLRect			mTextRect;				// The rect in which text is drawn.  Excludes borders.
	// List of offsets and segment index of the start of each line.  Always has at least one node (0).
	struct line_info
	{
		line_info(S32 index_start, S32 index_end, S32 top, S32 bottom, S32 line_num) 
		:	mDocIndexStart(index_start), 
			mDocIndexEnd(index_end),
			mTop(top),
			mBottom(bottom),
			mLineNum(line_num)
		{}
		S32 mDocIndexStart;
		S32 mDocIndexEnd;
		S32 mTop;
		S32 mBottom;
		S32 mLineNum; // actual line count (ignoring soft newlines due to word wrap)
	};
	struct compare_bottom;
	struct compare_top;
	struct line_end_compare;
	typedef std::vector<line_info> line_list_t;
	line_list_t		mLineInfoList;
	BOOL			mReflowNeeded;
	BOOL			mScrollNeeded;

	LLFrameTimer	mKeystrokeTimer;

	BOOL			mTabsToNextField;		// if true, tab moves focus to next field, else inserts spaces
	BOOL			mCommitOnFocusLost;
	BOOL			mTakesFocus;
	BOOL			mTrackBottom;			// if true, keeps scroll position at bottom during resize

	BOOL			mAllowEmbeddedItems;

	LLUUID			mSourceID;

	// If true, the standard edit keys (Ctrl-X, Delete, etc,) are handled here 
	//instead of routed by the menu system
	BOOL			mHandleEditKeysDirectly;  

	LLCoordGL		mLastIMEPosition;		// Last position of the IME editor

	keystroke_signal_t mKeystrokeSignal;
}; // end class LLTextEditor


#endif  // LL_TEXTEDITOR_