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