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
17class BClipboard;
18class BMessageRunner;
19
20
21class TextDocumentView : public BView, public BInvoker {
22public:
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				MouseUp(BPoint where);
38	virtual	void				MouseMoved(BPoint where, uint32 transit,
39									const BMessage* dragMessage);
40
41	virtual	void				KeyDown(const char* bytes, int32 numBytes);
42	virtual	void				KeyUp(const char* bytes, int32 numBytes);
43
44	virtual	BSize				MinSize();
45	virtual	BSize				MaxSize();
46	virtual	BSize				PreferredSize();
47
48	virtual	bool				HasHeightForWidth();
49	virtual	void				GetHeightForWidth(float width, float* min,
50									float* max, float* preferred);
51
52	virtual void				Relayout();
53
54	// TextDocumentView interface
55			void				SetTextDocument(
56									const TextDocumentRef& document);
57
58			void				SetEditingEnabled(bool enabled);
59			void				SetTextEditor(
60									const TextEditorRef& editor);
61
62			void				SetInsets(float inset);
63			void				SetInsets(float horizontal, float vertical);
64			void				SetInsets(float left, float top, float right,
65									float bottom);
66
67			void				SetSelectionEnabled(bool enabled);
68			void				SetCaret(BPoint where, bool extendSelection);
69
70			void				SelectAll();
71			bool				HasSelection() const;
72			void				GetSelection(int32& start, int32& end) const;
73
74			void				Copy(BClipboard* clipboard);
75
76private:
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
88private:
89			TextDocumentRef		fTextDocument;
90			TextDocumentLayout	fTextDocumentLayout;
91			TextEditorRef		fTextEditor;
92
93			float				fInsetLeft;
94			float				fInsetTop;
95			float				fInsetRight;
96			float				fInsetBottom;
97
98			BRect				fCaretBounds;
99			BMessageRunner*		fCaretBlinker;
100			int32				fCaretBlinkToken;
101			bool				fSelectionEnabled;
102			bool				fShowCaret;
103			bool				fMouseDown;
104};
105
106#endif // TEXT_DOCUMENT_VIEW_H
107