xref: /haiku/src/apps/haikudepot/textview/TextDocumentView.h (revision 9a6a20d4689307142a7ed26a1437ba47e244e73f)
1 /*
2  * Copyright 2013-2015, Stephan Aßmus <superstippi@gmx.de>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 #ifndef TEXT_DOCUMENT_VIEW_H
6 #define TEXT_DOCUMENT_VIEW_H
7 
8 #include <Invoker.h>
9 #include <String.h>
10 #include <View.h>
11 
12 #include "TextDocument.h"
13 #include "TextDocumentLayout.h"
14 #include "TextEditor.h"
15 
16 
17 class BClipboard;
18 class BMessageRunner;
19 
20 
21 class TextDocumentView : public BView, public BInvoker {
22 public:
23 								TextDocumentView(const char* name = NULL);
24 	virtual						~TextDocumentView();
25 
26 	// BView implementation
27 	virtual	void				MessageReceived(BMessage* message);
28 
29 	virtual void				Draw(BRect updateRect);
30 
31 	virtual	void				AttachedToWindow();
32 	virtual void				FrameResized(float width, float height);
33 	virtual	void				WindowActivated(bool active);
34 	virtual	void				MakeFocus(bool focus = true);
35 
36 	virtual	void				MouseDown(BPoint where);
37 	virtual	void				MouseMoved(BPoint where, uint32 transit,
38 									const BMessage* dragMessage);
39 
40 	virtual	void				KeyDown(const char* bytes, int32 numBytes);
41 	virtual	void				KeyUp(const char* bytes, int32 numBytes);
42 
43 	virtual	BSize				MinSize();
44 	virtual	BSize				MaxSize();
45 	virtual	BSize				PreferredSize();
46 
47 	virtual	bool				HasHeightForWidth();
48 	virtual	void				GetHeightForWidth(float width, float* min,
49 									float* max, float* preferred);
50 
51 	virtual void				Relayout();
52 
53 	// TextDocumentView interface
54 			void				SetTextDocument(
55 									const TextDocumentRef& document);
56 
57 			void				SetEditingEnabled(bool enabled);
58 			void				SetTextEditor(
59 									const TextEditorRef& editor);
60 
61 			void				SetInsets(float inset);
62 			void				SetInsets(float horizontal, float vertical);
63 			void				SetInsets(float left, float top, float right,
64 									float bottom);
65 
66 			void				SetSelectionEnabled(bool enabled);
67 			void				SetCaret(BPoint where, bool extendSelection);
68 
69 			void				SelectAll();
70 			bool				HasSelection() const;
71 			void				GetSelection(int32& start, int32& end) const;
72 
73 			void				Copy(BClipboard* clipboard);
74 			void				Paste(BClipboard* clipboard);
75 
76 private:
77 			float				_TextLayoutWidth(float viewWidth) const;
78 
79 			void				_UpdateScrollBars();
80 
81 			void				_ShowCaret(bool show);
82 			void				_BlinkCaret();
83 			void				_DrawCaret(int32 textOffset);
84 			void				_DrawSelection();
85 			void				_GetSelectionShape(BShape& shape,
86 									int32 start, int32 end);
87 
88 			status_t			_PastePossiblyDisallowedChars(const char* str, int32 maxLength);
89 			void				_PasteAllowedChars(const char* str, int32 maxLength);
90 	static	bool				_IsAllowedChar(char c);
91 	static	bool				_AreCharsAllowed(const char* str, int32 maxLength);
92 
93 private:
94 			TextDocumentRef		fTextDocument;
95 			TextDocumentLayout	fTextDocumentLayout;
96 			TextEditorRef		fTextEditor;
97 
98 			float				fInsetLeft;
99 			float				fInsetTop;
100 			float				fInsetRight;
101 			float				fInsetBottom;
102 
103 			BRect				fCaretBounds;
104 			BMessageRunner*		fCaretBlinker;
105 			int32				fCaretBlinkToken;
106 			bool				fSelectionEnabled;
107 			bool				fShowCaret;
108 };
109 
110 #endif // TEXT_DOCUMENT_VIEW_H
111