xref: /haiku/src/apps/haikudepot/textview/TextEditor.h (revision a629567a9001547736cfe892cdf992be16868fed)
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 
18 class KeyEvent {
19 public:
20 			const char*			bytes;
21 			int32				length;
22 			int32				key;
23 			int32				modifiers;
24 };
25 
26 
27 class TextEditor : public BReferenceable {
28 public:
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				SetSelection(TextSelection selection);
52 	inline	TextSelection		Selection() const
53 									{ return fSelection; }
54 
55 			void				SetCharacterStyle(::CharacterStyle style);
56 			::CharacterStyle	CharacterStyle() const
57 									{ return fStyleAtCaret; }
58 
59 	virtual	void				KeyDown(KeyEvent event);
60 
61 	virtual	status_t			Insert(int32 offset, const BString& string);
62 	virtual	status_t			Remove(int32 offset, int32 length);
63 
64 			void				LineUp(bool select);
65 			void				LineDown(bool select);
66 			void				LineStart(bool select);
67 			void				LineEnd(bool select);
68 
69 			bool				HasSelection() const;
70 			int32				SelectionStart() const;
71 			int32				SelectionEnd() const;
72 			int32				SelectionLength() const;
73 	inline	int32				CaretOffset() const
74 									{ return fSelection.Caret(); }
75 
76 private:
77 			void				_MoveToLine(int32 lineIndex, bool select);
78 			void				_SetCaretOffset(int32 offset,
79 									bool updateAnchor,
80 									bool lockSelectionAnchor,
81 									bool updateSelectionStyle);
82 			void				_SetSelection(int32 caret, int32 anchor,
83 									bool updateAnchor,
84 									bool updateSelectionStyle);
85 
86 			void				_UpdateStyleAtCaret();
87 
88 private:
89 			TextDocumentRef		fDocument;
90 			TextDocumentLayoutRef fLayout;
91 			TextSelection		fSelection;
92 			float				fCaretAnchorX;
93 			::CharacterStyle	fStyleAtCaret;
94 			bool				fEditingEnabled;
95 };
96 
97 
98 typedef BReference<TextEditor> TextEditorRef;
99 
100 
101 #endif // TEXT_EDITOR_H
102