1/*
2 * Copyright 2014, Stephan A��mus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5#ifndef TEXT_EDITOR_H
6#define TEXT_EDITOR_H
7
8
9#include <Point.h>
10#include <Referenceable.h>
11
12#include "CharacterStyle.h"
13#include "TextDocument.h"
14#include "TextDocumentLayout.h"
15#include "TextSelection.h"
16
17
18class KeyEvent {
19public:
20			const char*			bytes;
21			int32				length;
22			int32				key;
23			int32				modifiers;
24};
25
26
27class TextEditor : public BReferenceable {
28public:
29								TextEditor();
30								TextEditor(const TextEditor& other);
31	virtual						~TextEditor();
32
33			TextEditor&			operator=(const TextEditor& other);
34			bool				operator==(const TextEditor& other) const;
35			bool				operator!=(const TextEditor& other) const;
36
37			void				SetDocument(const TextDocumentRef& ref);
38			TextDocumentRef		Document() const
39									{ return fDocument; }
40
41			void				SetLayout(
42									const TextDocumentLayoutRef& ref);
43			TextDocumentLayoutRef Layout() const
44									{ return fLayout; }
45
46			void				SetEditingEnabled(bool enabled);
47	inline	bool				IsEditingEnabled() const
48									{ return fEditingEnabled; }
49
50			void				SetCaret(BPoint location, bool extendSelection);
51			void				SelectAll();
52			void				SetSelection(TextSelection selection);
53	inline	TextSelection		Selection() const
54									{ return fSelection; }
55
56			void				SetCharacterStyle(::CharacterStyle style);
57			::CharacterStyle	CharacterStyle() const
58									{ return fStyleAtCaret; }
59
60	virtual	void				KeyDown(KeyEvent event);
61
62	virtual	status_t			Insert(int32 offset, const BString& string);
63	virtual	status_t			Remove(int32 offset, int32 length);
64	virtual	status_t			Replace(int32 offset, int32 length,
65									const BString& string);
66
67			void				LineUp(bool select);
68			void				LineDown(bool select);
69			void				LineStart(bool select);
70			void				LineEnd(bool select);
71
72			bool				HasSelection() const;
73			int32				SelectionStart() const;
74			int32				SelectionEnd() const;
75			int32				SelectionLength() const;
76	inline	int32				CaretOffset() const
77									{ return fSelection.Caret(); }
78
79private:
80			void				_MoveToLine(int32 lineIndex, bool select);
81			void				_SetCaretOffset(int32 offset,
82									bool updateAnchor,
83									bool lockSelectionAnchor,
84									bool updateSelectionStyle);
85			void				_SetSelection(int32 caret, int32 anchor,
86									bool updateAnchor,
87									bool updateSelectionStyle);
88
89			void				_UpdateStyleAtCaret();
90
91private:
92			TextDocumentRef		fDocument;
93			TextDocumentLayoutRef fLayout;
94			TextSelection		fSelection;
95			float				fCaretAnchorX;
96			::CharacterStyle	fStyleAtCaret;
97			bool				fEditingEnabled;
98};
99
100
101typedef BReference<TextEditor> TextEditorRef;
102
103
104#endif // TEXT_EDITOR_H
105