TextView.h revision fef862eb
1/*
2 * Copyright 2007-2009, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _TEXTVIEW_H
6#define _TEXTVIEW_H
7
8
9#include <Locker.h>
10#include <View.h>
11
12
13class BBitmap;
14class BClipboard;
15class BFile;
16class BList;
17class BMessageRunner;
18
19struct text_run {
20	int32		offset;
21	BFont		font;
22	rgb_color	color;
23};
24
25struct text_run_array {
26	int32		count;
27	text_run	runs[1];
28};
29
30enum undo_state {
31	B_UNDO_UNAVAILABLE,
32	B_UNDO_TYPING,
33	B_UNDO_CUT,
34	B_UNDO_PASTE,
35	B_UNDO_CLEAR,
36	B_UNDO_DROP
37};
38
39namespace BPrivate {
40	class TextGapBuffer;
41}
42
43
44class BTextView : public BView {
45public:
46								BTextView(BRect frame, const char* name,
47									BRect textRect, uint32 resizeMask,
48									uint32 flags
49										= B_WILL_DRAW | B_PULSE_NEEDED);
50								BTextView(BRect	frame, const char* name,
51									BRect textRect, const BFont* initialFont,
52									const rgb_color* initialColor,
53									uint32 resizeMask, uint32 flags);
54
55								BTextView(const char* name,
56									uint32 flags
57										= B_WILL_DRAW | B_PULSE_NEEDED);
58								BTextView(const char* name,
59									const BFont* initialFont,
60									const rgb_color* initialColor,
61									uint32 flags);
62
63								BTextView(BMessage* archive);
64
65	virtual						~BTextView();
66
67	static	BArchivable*		Instantiate(BMessage* archive);
68	virtual	status_t			Archive(BMessage* archive,
69									bool deep = true) const;
70
71	virtual	void				AttachedToWindow();
72	virtual	void				DetachedFromWindow();
73	virtual	void				Draw(BRect updateRect);
74	virtual	void				MouseDown(BPoint where);
75	virtual	void				MouseUp(BPoint where);
76	virtual	void				MouseMoved(BPoint where, uint32 code,
77									const BMessage* dragMessage);
78	virtual	void				WindowActivated(bool active);
79	virtual	void				KeyDown(const char* bytes, int32 numBytes);
80	virtual	void				Pulse();
81	virtual	void				FrameResized(float newWidth, float newHeight);
82	virtual	void				MakeFocus(bool focus = true);
83	virtual	void				MessageReceived(BMessage* message);
84
85	virtual	BHandler*			ResolveSpecifier(BMessage* message,
86									int32 index, BMessage* specifier,
87									int32 form, const char* property);
88	virtual	status_t			GetSupportedSuites(BMessage* data);
89	virtual	status_t			Perform(perform_code code, void* data);
90
91			void				SetText(const char* text,
92									const text_run_array* runs = NULL);
93			void				SetText(const char* text, int32 length,
94									const text_run_array* runs = NULL);
95			void				SetText(BFile* file, int32 offset,
96									int32 length,
97									const text_run_array* runs = NULL);
98
99			void				Insert(const char* text,
100									const text_run_array* runs = NULL);
101			void				Insert(const char* text, int32 length,
102									const text_run_array* runs = NULL);
103			void				Insert(int32 offset, const char* text,
104									int32 length,
105									const text_run_array* runs = NULL);
106
107			void				Delete();
108			void				Delete(int32 startOffset, int32 endOffset);
109
110			const char*			Text() const;
111			int32				TextLength() const;
112			void				GetText(int32 offset, int32 length,
113									char* buffer) const;
114			uint8				ByteAt(int32 offset) const;
115
116			int32				CountLines() const;
117			int32				CurrentLine() const;
118			void				GoToLine(int32 lineNumber);
119
120	virtual	void				Cut(BClipboard* clipboard);
121	virtual	void				Copy(BClipboard* clipboard);
122	virtual	void				Paste(BClipboard* clipboard);
123			void				Clear();
124
125	virtual	bool				AcceptsPaste(BClipboard* clipboard);
126	virtual	bool				AcceptsDrop(const BMessage* message);
127
128	virtual	void				Select(int32 startOffset, int32 endOffset);
129			void				SelectAll();
130			void				GetSelection(int32* _start, int32* _end) const;
131
132			void				SetFontAndColor(const BFont* font,
133									uint32 mode = B_FONT_ALL,
134									const rgb_color* color = NULL);
135			void				SetFontAndColor(int32 startOffset,
136									int32 endOffset, const BFont* font,
137									uint32 mode = B_FONT_ALL,
138									const rgb_color* color = NULL);
139
140			void				GetFontAndColor(int32 offset, BFont* _font,
141									rgb_color* _color = NULL) const;
142			void				GetFontAndColor(BFont* _font, uint32* _mode,
143									rgb_color* _color = NULL,
144									bool* _sameColor = NULL) const;
145
146			void				SetRunArray(int32 startOffset, int32 endOffset,
147									const text_run_array* runs);
148			text_run_array*		RunArray(int32 startOffset, int32 endOffset,
149									int32* _size = NULL) const;
150
151			int32				LineAt(int32 offset) const;
152			int32				LineAt(BPoint point) const;
153			BPoint				PointAt(int32 offset,
154									float* _height = NULL) const;
155			int32				OffsetAt(BPoint point) const;
156			int32				OffsetAt(int32 line) const;
157
158	virtual	void				FindWord(int32 offset, int32* _fromOffset,
159									int32* _toOffset);
160
161	virtual	bool				CanEndLine(int32 offset);
162
163			float				LineWidth(int32 lineNumber = 0) const;
164			float				LineHeight(int32 lineNumber = 0) const;
165			float				TextHeight(int32 startLine,
166									int32 endLine) const;
167
168			void				GetTextRegion(int32 startOffset,
169									int32 endOffset, BRegion* outRegion) const;
170
171	virtual	void				ScrollToOffset(int32 offset);
172			void				ScrollToSelection();
173
174			void				Highlight(int32 startOffset, int32 endOffset);
175
176			void				SetTextRect(BRect rect);
177			BRect				TextRect() const;
178			void				SetInsets(float left, float top, float right,
179									float bottom);
180			void				GetInsets(float* _left, float* _top,
181									float* _right, float* _bottom) const;
182
183			void				SetStylable(bool stylable);
184			bool				IsStylable() const;
185			void				SetTabWidth(float width);
186			float				TabWidth() const;
187			void				MakeSelectable(bool selectable = true);
188			bool				IsSelectable() const;
189			void				MakeEditable(bool editable = true);
190			bool				IsEditable() const;
191			void				SetWordWrap(bool wrap);
192			bool				DoesWordWrap() const;
193			void				SetMaxBytes(int32 max);
194			int32				MaxBytes() const;
195			void				DisallowChar(uint32 character);
196			void				AllowChar(uint32 character);
197			void				SetAlignment(alignment align);
198			alignment			Alignment() const;
199			void				SetAutoindent(bool state);
200			bool				DoesAutoindent() const;
201			void				SetColorSpace(color_space colors);
202			color_space			ColorSpace() const;
203			void				MakeResizable(bool resize,
204									BView* resizeView = NULL);
205			bool				IsResizable() const;
206			void				SetDoesUndo(bool undo);
207			bool				DoesUndo() const;
208			void				HideTyping(bool enabled);
209			bool				IsTypingHidden() const;
210
211	virtual	void				ResizeToPreferred();
212	virtual	void				GetPreferredSize(float* _width, float* _height);
213
214	virtual	void				AllAttached();
215	virtual	void				AllDetached();
216
217	static	text_run_array*		AllocRunArray(int32 entryCount,
218									int32* outSize = NULL);
219	static	text_run_array*		CopyRunArray(const text_run_array* orig,
220									int32 countDelta = 0);
221	static	void				FreeRunArray(text_run_array* array);
222	static	void*				FlattenRunArray(const text_run_array* runArray,
223									int32* _size = NULL);
224	static	text_run_array*		UnflattenRunArray(const void* data,
225									int32* _size = NULL);
226
227protected:
228	virtual	void				InsertText(const char* text, int32 length,
229									int32 offset, const text_run_array* runs);
230	virtual	void				DeleteText(int32 fromOffset, int32 toOffset);
231
232public:
233	virtual	void				Undo(BClipboard* clipboard);
234			undo_state			UndoState(bool* isRedo) const;
235
236protected:
237	virtual	void				GetDragParameters(BMessage* drag,
238									BBitmap** _bitmap, BPoint* point,
239									BHandler** _handler);
240
241	virtual	void				LayoutInvalidated(bool descendants);
242	virtual	void				DoLayout();
243
244public:
245	virtual	BSize				MinSize();
246	virtual	BSize				MaxSize();
247	virtual	BSize				PreferredSize();
248
249	virtual	bool				HasHeightForWidth();
250	virtual	void				GetHeightForWidth(float width, float* min,
251									float* max, float* preferred);
252
253private:
254	// FBC padding and forbidden methods
255	virtual	void				_ReservedTextView3();
256	virtual	void				_ReservedTextView4();
257	virtual	void				_ReservedTextView5();
258	virtual	void				_ReservedTextView6();
259	virtual	void				_ReservedTextView7();
260	virtual	void				_ReservedTextView8();
261	virtual	void				_ReservedTextView9();
262	virtual	void				_ReservedTextView10();
263	virtual	void				_ReservedTextView11();
264	virtual	void				_ReservedTextView12();
265
266private:
267			class InlineInput;
268			struct LayoutData;
269			class LineBuffer;
270			class StyleBuffer;
271			class TextTrackState;
272			class UndoBuffer;
273
274			// UndoBuffer derivatives
275			class CutUndoBuffer;
276			class PasteUndoBuffer;
277			class ClearUndoBuffer;
278			class DropUndoBuffer;
279			class TypingUndoBuffer;
280
281			friend class TextTrackState;
282
283			void				_InitObject(BRect textRect,
284									const BFont* initialFont,
285									const rgb_color* initialColor);
286
287			void				_ValidateLayoutData();
288			void				_ResetTextRect();
289
290			void				_HandleBackspace();
291			void				_HandleArrowKey(uint32 arrowKey,
292									int32 modifiers = -1);
293			void				_HandleDelete();
294			void				_HandlePageKey(uint32 pageKey,
295									int32 modifiers = -1);
296			void				_HandleAlphaKey(const char* bytes,
297									int32 numBytes);
298
299			void				_Refresh(int32 fromOffset, int32 toOffset,
300									bool scroll);
301			void				_RecalculateLineBreaks(int32* startLine,
302									int32* endLine);
303			int32				_FindLineBreak(int32 fromOffset,
304									float* _ascent, float* _descent,
305									float* inOutWidth);
306
307			float				_StyledWidth(int32 fromOffset, int32 length,
308									float* _ascent = NULL,
309									float* _descent = NULL) const;
310			float				_TabExpandedStyledWidth(int32 offset,
311									int32 length, float* _ascent = NULL,
312									float* _descent = NULL) const;
313
314			float				_ActualTabWidth(float location) const;
315
316			void				_DoInsertText(const char* text, int32 length,
317									int32 offset, const text_run_array* runs);
318
319			void				_DoDeleteText(int32 fromOffset,
320									int32 toOffset);
321
322			void				_DrawLine(BView* view, const int32 &startLine,
323									const int32& startOffset,
324									const bool& erase, BRect& eraseRect,
325									BRegion& inputRegion);
326
327			void				_DrawLines(int32 startLine, int32 endLine,
328									int32 startOffset = -1,
329									bool erase = false);
330			void				_RequestDrawLines(int32 startLine,
331									int32 endLine);
332
333			void				_DrawCaret(int32 offset, bool visible);
334			void				_ShowCaret();
335			void				_HideCaret();
336			void				_InvertCaret();
337			void				_DragCaret(int32 offset);
338
339			void				_StopMouseTracking();
340			bool				_PerformMouseUp(BPoint where);
341			bool				_PerformMouseMoved(BPoint where, uint32 code);
342
343			void				_TrackMouse(BPoint where,
344									const BMessage* message,
345									bool force = false);
346
347			void				_TrackDrag(BPoint where);
348			void				_InitiateDrag();
349			bool				_MessageDropped(BMessage* message,
350									BPoint where, BPoint offset);
351
352			void				_PerformAutoScrolling();
353			void				_UpdateScrollbars();
354			void				_ScrollBy(float horizontalStep,
355									float verticalStep);
356			void				_ScrollTo(float x, float y);
357
358			void				_AutoResize(bool doRedraw = true);
359
360			void				_NewOffscreen(float padding = 0.0);
361			void				_DeleteOffscreen();
362
363			void				_Activate();
364			void				_Deactivate();
365
366			void				_NormalizeFont(BFont* font);
367
368			void				_SetRunArray(int32 startOffset, int32 endOffset,
369									const text_run_array* runs);
370
371			void				_ApplyStyleRange(int32 fromOffset,
372									int32 toOffset,
373									uint32 mode = B_FONT_ALL,
374									const BFont* font = NULL,
375									const rgb_color* color = NULL,
376									bool syncNullStyle = true);
377
378			uint32				_CharClassification(int32 offset) const;
379			int32				_NextInitialByte(int32 offset) const;
380			int32				_PreviousInitialByte(int32 offset) const;
381
382			int32				_PreviousLineStart(int32 offset);
383			int32				_NextLineEnd(int32 offset);
384
385			int32				_PreviousWordBoundary(int32 offset);
386			int32				_NextWordBoundary(int32 offset);
387
388			int32				_PreviousWordStart(int32 offset);
389			int32				_NextWordEnd(int32 offset);
390
391			bool				_GetProperty(BMessage* specifier, int32 form,
392									const char* property, BMessage* reply);
393			bool				_SetProperty(BMessage* specifier, int32 form,
394									const char* property, BMessage* reply);
395			bool				_CountProperties(BMessage* specifier,
396									int32 form, const char* property,
397									BMessage* reply);
398
399			void				_HandleInputMethodChanged(BMessage* message);
400			void				_HandleInputMethodLocationRequest();
401			void				_CancelInputMethod();
402
403			int32				_LineAt(int32 offset) const;
404			int32				_LineAt(const BPoint& point) const;
405			bool				_IsOnEmptyLastLine(int32 offset) const;
406
407			float				_NullStyleHeight() const;
408
409			void				_ShowContextMenu(BPoint where);
410
411			void				_FilterDisallowedChars(char* text,
412									ssize_t& length, text_run_array* runArray);
413
414private:
415			BPrivate::TextGapBuffer*	fText;
416			LineBuffer*			fLines;
417			StyleBuffer*		fStyles;
418			BRect				fTextRect;
419			int32				fSelStart;
420			int32				fSelEnd;
421			bool				fCaretVisible;
422			bigtime_t			fCaretTime;
423			int32				fCaretOffset;
424			int32				fClickCount;
425			bigtime_t			fClickTime;
426			int32				fDragOffset;
427			uint8				fCursor;
428			bool				fActive;
429			bool				fStylable;
430			float				fTabWidth;
431			bool				fSelectable;
432			bool				fEditable;
433			bool				fWrap;
434			int32				fMaxBytes;
435			BList*				fDisallowedChars;
436			alignment			fAlignment;
437			bool				fAutoindent;
438			BBitmap* 			fOffscreen;
439			color_space			fColorSpace;
440			bool				fResizable;
441			BView*				fContainerView;
442			UndoBuffer*			fUndo;
443			InlineInput*		fInline;
444			BMessageRunner*		fDragRunner;
445			BMessageRunner*		fClickRunner;
446			BPoint				fWhere;
447			TextTrackState*		fTrackingMouse;
448
449			float				fMinTextRectWidth;
450			LayoutData*			fLayoutData;
451			int32				fLastClickOffset;
452
453			bool				fInstalledNavigateCommandWordwiseShortcuts;
454			bool				fInstalledNavigateOptionWordwiseShortcuts;
455			bool				fInstalledNavigateOptionLinewiseShortcuts;
456			bool				fInstalledNavigateHomeEndDocwiseShortcuts;
457
458			bool				fInstalledSelectCommandWordwiseShortcuts;
459			bool				fInstalledSelectOptionWordwiseShortcuts;
460			bool				fInstalledSelectOptionLinewiseShortcuts;
461			bool				fInstalledSelectHomeEndDocwiseShortcuts;
462
463			uint32				_reserved[6];
464};
465
466#endif	// _TEXTVIEW_H
467