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