xref: /haiku/headers/os/interface/TextView.h (revision 89755088d790ff4fe36f8aa77dacb2bd15507108)
1 /*
2  * Copyright (c) 2007, Haiku, Inc.
3  * Distributed under the terms of the MIT license.
4  */
5 #ifndef _TEXTVIEW_H
6 #define _TEXTVIEW_H
7 
8 
9 #include <BeBuild.h>
10 #include <View.h>
11 
12 class BMessageRunner;
13 class BBitmap;
14 class BClipboard;
15 class BFile;
16 class BList;
17 class _BTextGapBuffer_;
18 class _BLineBuffer_;
19 class _BStyleBuffer_;
20 class _BWidthBuffer_;
21 class _BUndoBuffer_;
22 class _BInlineInput_;
23 class _BTextTrackState_;
24 class _BTextChangeResult_;
25 
26 extern "C" status_t	_init_interface_kit_();
27 
28 
29 struct text_run {
30 	int32		offset;
31 	BFont		font;
32 	rgb_color	color;
33 };
34 
35 struct text_run_array {
36 	int32		count;
37 	text_run	runs[1];
38 };
39 
40 enum undo_state {
41 	B_UNDO_UNAVAILABLE,
42 	B_UNDO_TYPING,
43 	B_UNDO_CUT,
44 	B_UNDO_PASTE,
45 	B_UNDO_CLEAR,
46 	B_UNDO_DROP
47 };
48 
49 class BTextView : public BView {
50 	public:
51 		BTextView(BRect frame, const char* name, BRect textRect,
52 			uint32 resizeMask, uint32 flags = B_WILL_DRAW | B_PULSE_NEEDED);
53 		BTextView(BRect	frame, const char* name, BRect textRect,
54 			const BFont* initialFont, const rgb_color* initialColor,
55 			uint32 resizeMask, uint32 flags);
56 		BTextView(BMessage* data);
57 		virtual	~BTextView();
58 
59 		static	BArchivable* Instantiate(BMessage* data);
60 		virtual	status_t Archive(BMessage* data, bool deep = true) const;
61 
62 		virtual	void AttachedToWindow();
63 		virtual	void DetachedFromWindow();
64 		virtual	void Draw(BRect inRect);
65 		virtual	void MouseDown(BPoint where);
66 		virtual void MouseUp(BPoint where);
67 		virtual	void MouseMoved(BPoint where, uint32 code,
68 			const BMessage* message);
69 		virtual	void WindowActivated(bool state);
70 		virtual	void KeyDown(const char* bytes, int32 numBytes);
71 		virtual	void Pulse();
72 		virtual	void FrameResized(float width, float height);
73 		virtual	void MakeFocus(bool focusState = true);
74 		virtual	void MessageReceived(BMessage* message);
75 
76 		virtual BHandler* ResolveSpecifier(BMessage* message, int32 index,
77 			BMessage* specifier, int32 form, const char* property);
78 		virtual status_t GetSupportedSuites(BMessage* data);
79 		virtual status_t Perform(perform_code d, void* arg);
80 
81 		void SetText(const char* inText, const text_run_array* inRuns = NULL);
82 		void SetText(const char* inText, int32 inLength,
83 			const text_run_array* inRuns = NULL);
84 		void SetText(BFile* inFile, int32 startOffset, int32 inLength,
85 			const text_run_array* inRuns = NULL);
86 
87 		void Insert(const char* inText, const text_run_array* inRuns = NULL);
88 		void Insert(const char* inText, int32 inLength, const text_run_array* inRuns = NULL);
89 		void Insert(int32 startOffset, const char* inText, int32 inLength, const text_run_array* inRuns = NULL);
90 
91 		void Delete();
92 		void Delete(int32 startOffset, int32 endOffset);
93 
94 		const char*	Text() const;
95 		int32 TextLength() const;
96 		void GetText(int32 offset, int32 length, char* buffer) const;
97 		uint8 ByteAt(int32 offset) const;
98 
99 		int32 CountLines() const;
100 		int32 CurrentLine() const;
101 		void GoToLine(int32 lineNum);
102 
103 		virtual	void Cut(BClipboard* clipboard);
104 		virtual	void Copy(BClipboard* clipboard);
105 		virtual	void Paste(BClipboard* clipboard);
106 		void Clear();
107 
108 		virtual	bool AcceptsPaste(BClipboard* clipboard);
109 		virtual	bool AcceptsDrop(const BMessage* inMessage);
110 
111 		virtual	void Select(int32 startOffset, int32 endOffset);
112 		void SelectAll();
113 		void GetSelection(int32* outStart, int32* outEnd) const;
114 
115 		void SetFontAndColor(const BFont* inFont, uint32 inMode = B_FONT_ALL,
116 			const rgb_color* inColor = NULL);
117 		void SetFontAndColor(int32 startOffset, int32 endOffset,
118 			const BFont* inFont, uint32 inMode = B_FONT_ALL,
119 			const rgb_color* inColor = NULL);
120 
121 		void GetFontAndColor(int32 inOffset, BFont* outFont,
122 			rgb_color* outColor = NULL) const;
123 		void GetFontAndColor(BFont* outFont, uint32* sameProperties,
124 			rgb_color* outColor = NULL, bool* sameColor = NULL) const;
125 
126 		void SetRunArray(int32 startOffset, int32 endOffset,
127 			const text_run_array* inRuns);
128 		text_run_array*	RunArray(int32	startOffset, int32	endOffset,
129 			int32* outSize = NULL) const;
130 
131 		int32 LineAt(int32 offset) const;
132 		int32 LineAt(BPoint point) const;
133 		BPoint PointAt(int32 inOffset, float* outHeight = NULL) const;
134 		int32 OffsetAt(BPoint point) const;
135 		int32 OffsetAt(int32 line) const;
136 
137 		virtual	void FindWord(int32	inOffset, int32* outFromOffset,
138 			int32* outToOffset);
139 
140 		virtual	bool CanEndLine(int32 offset);
141 
142 		float LineWidth(int32 lineNum = 0) const;
143 		float LineHeight(int32 lineNum = 0) const;
144 		float TextHeight(int32 startLine, int32 endLine) const;
145 
146 		void GetTextRegion(int32 startOffset, int32 endOffset,
147 			BRegion* outRegion) const;
148 
149 		virtual	void ScrollToOffset(int32 inOffset);
150 		void ScrollToSelection();
151 
152 		void Highlight(int32 startOffset, int32 endOffset);
153 
154 		void SetTextRect(BRect rect);
155 		BRect TextRect() const;
156 		void SetStylable(bool stylable);
157 		bool IsStylable() const;
158 		void SetTabWidth(float width);
159 		float TabWidth() const;
160 		void MakeSelectable(bool selectable = true);
161 		bool IsSelectable() const;
162 		void MakeEditable(bool editable = true);
163 		bool IsEditable() const;
164 		void SetWordWrap(bool wrap);
165 		bool DoesWordWrap() const;
166 		void SetMaxBytes(int32 max);
167 		int32 MaxBytes() const;
168 		void DisallowChar(uint32 aChar);
169 		void AllowChar(uint32 aChar);
170 		void SetAlignment(alignment flag);
171 		alignment Alignment() const;
172 		void SetAutoindent(bool state);
173 		bool DoesAutoindent() const;
174 		void SetColorSpace(color_space colors);
175 		color_space ColorSpace() const;
176 		void MakeResizable(bool resize, BView* resizeView = NULL);
177 		bool IsResizable() const;
178 		void SetDoesUndo(bool undo);
179 		bool DoesUndo() const;
180 		void HideTyping(bool enabled);
181 		bool IsTypingHidden() const;
182 
183 		virtual void ResizeToPreferred();
184 		virtual void GetPreferredSize(float* _width, float* _height);
185 		virtual void AllAttached();
186 		virtual void AllDetached();
187 
188 		static text_run_array* AllocRunArray(int32 entryCount,
189 			int32* outSize = NULL);
190 		static text_run_array* CopyRunArray(const text_run_array* orig,
191 			int32 countDelta = 0);
192 		static void FreeRunArray(text_run_array* array);
193 		static void* FlattenRunArray(const text_run_array* inArray,
194 			int32* outSize = NULL);
195 		static text_run_array* UnflattenRunArray(const void* data,
196 			int32* outSize = NULL);
197 
198 	protected:
199 		virtual	void InsertText(const char* inText, int32 inLength,
200 			int32 inOffset, const text_run_array* inRuns);
201 		virtual	void DeleteText(int32 fromOffset, int32 toOffset);
202 
203 	public:
204 		virtual	void Undo(BClipboard* clipboard);
205 		undo_state UndoState(bool* isRedo) const;
206 
207 	protected:
208 		virtual	void GetDragParameters(BMessage* drag, BBitmap** _bitmap,
209 			BPoint* point, BHandler** _handler);
210 
211 	private:
212 		friend status_t	_init_interface_kit_();
213 		friend class _BTextTrackState_;
214 
215 		virtual void _ReservedTextView3();
216 		virtual void _ReservedTextView4();
217 		virtual void _ReservedTextView5();
218 		virtual void _ReservedTextView6();
219 		virtual void _ReservedTextView7();
220 		virtual void _ReservedTextView8();
221 		virtual void _ReservedTextView9();
222 		virtual void _ReservedTextView10();
223 		virtual void _ReservedTextView11();
224 		virtual void _ReservedTextView12();
225 
226 		void _InitObject(BRect textRect, const BFont* initialFont,
227 			const rgb_color* initialColor);
228 
229 		void _HandleBackspace();
230 		void _HandleArrowKey(uint32 inArrowKey);
231 		void _HandleDelete();
232 		void _HandlePageKey(uint32 inPageKey);
233 		void _HandleAlphaKey(const char* bytes, int32 numBytes);
234 
235 		void _Refresh(int32 fromOffset, int32 toOffset, bool erase, bool scroll);
236 		void _RecalculateLineBreaks(int32* startLine, int32* endLine);
237 		int32 _FindLineBreak(int32 fromOffset, float* outAscent,
238 			float* outDescent, float* ioWidth);
239 
240 		float _StyledWidth(int32 fromOffset, int32 length,
241 			float* outAscent = NULL, float* outDescent = NULL) const;
242 
243 		float _StyledWidthUTF8Safe(int32 fromOffset, int32 numChars,
244 			float* outAscent = NULL, float* outDescent = NULL) const;
245 
246 		float _ActualTabWidth(float location) const;
247 
248 		void _DoInsertText(const char* inText, int32 inLength, int32 inOffset,
249 			const text_run_array* inRuns, _BTextChangeResult_* outResult);
250 
251 		void _DoDeleteText(int32 fromOffset, int32 toOffset,
252 			_BTextChangeResult_* outResult);
253 
254 		void _DrawLine(BView* view, const int32 &startLine,
255 			const int32& startOffset, const bool& erase, BRect& eraseRect,
256 			BRegion& inputRegion);
257 
258 		void _DrawLines(int32 startLine, int32 endLine, int32 startOffset = -1,
259 			bool erase = false);
260 		void _DrawCaret(int32 offset);
261 		void _ShowCaret();
262 		void _HideCaret();
263 		void _InvertCaret();
264 		void _DragCaret(int32 offset);
265 
266 		void _StopMouseTracking();
267 		bool _PerformMouseUp(BPoint where);
268 		bool _PerformMouseMoved(BPoint where, uint32 code);
269 
270 		void _TrackMouse(BPoint where, const BMessage* message,
271 			bool force = false);
272 
273 		void _TrackDrag(BPoint where);
274 		void _InitiateDrag();
275 		bool _MessageDropped(BMessage* inMessage, BPoint where, BPoint offset);
276 
277 		void _PerformAutoScrolling();
278 		void _UpdateScrollbars();
279 		void _AutoResize(bool doRedraw = true);
280 
281 		void _NewOffscreen(float padding = 0.0F);
282 		void _DeleteOffscreen();
283 
284 		void _Activate();
285 		void _Deactivate();
286 
287 		void _NormalizeFont(BFont* font);
288 
289 		void _SetRunArray(int32 startOffset, int32 endOffset,
290 			const text_run_array* inRuns);
291 
292 		uint32 _CharClassification(int32 offset) const;
293 		int32 _NextInitialByte(int32 offset) const;
294 		int32 _PreviousInitialByte(int32 offset) const;
295 
296 		bool _GetProperty(BMessage* specifier, int32 form, const char* property,
297 			BMessage* reply);
298 
299 		bool _SetProperty(BMessage* specifier, int32 form, const char* property,
300 			BMessage* reply);
301 
302 		bool _CountProperties(BMessage* specifier, int32 form,
303 			const char* property, BMessage* reply);
304 
305 		void _HandleInputMethodChanged(BMessage* message);
306 		void _HandleInputMethodLocationRequest();
307 		void _CancelInputMethod();
308 
309 		static void LockWidthBuffer();
310 		static void UnlockWidthBuffer();
311 
312 		_BTextGapBuffer_*		fText;
313 		_BLineBuffer_*			fLines;
314 		_BStyleBuffer_*			fStyles;
315 		BRect					fTextRect;
316 		float					fMinTextRectWidth;
317 		int32					fSelStart;
318 		int32					fSelEnd;
319 		bool					fCaretVisible;
320 		bigtime_t				fCaretTime;
321 		int32					fClickOffset;
322 		int32					fClickCount;
323 		bigtime_t				fClickTime;
324 		int32					fDragOffset;
325 		uint8					fCursor;
326 		bool					fActive;
327 		bool					fStylable;
328 		float					fTabWidth;
329 		bool					fSelectable;
330 		bool					fEditable;
331 		bool					fWrap;
332 		int32					fMaxBytes;
333 		BList*					fDisallowedChars;
334 		alignment				fAlignment;
335 		bool					fAutoindent;
336 		BBitmap* 				fOffscreen;
337 		color_space				fColorSpace;
338 		bool					fResizable;
339 		BView*					fContainerView;
340 		_BUndoBuffer_*			fUndo;
341 		_BInlineInput_*			fInline;
342 		BMessageRunner *		fDragRunner;
343 		BMessageRunner *		fClickRunner;
344 		BPoint					fWhere;
345 		_BTextTrackState_*		fTrackingMouse;
346 		_BTextChangeResult_*	fTextChange;
347 		uint32					_reserved[8];
348 
349 		static _BWidthBuffer_*	sWidths;
350 		static sem_id			sWidthSem;
351 		static int32			sWidthAtom;
352 };
353 
354 #endif	// _TEXTVIEW_H
355