1/*
2 * Copyright 2013-2014, Stephan A��mus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5#ifndef TEXT_DOCUMENT_H
6#define TEXT_DOCUMENT_H
7
8#include <Referenceable.h>
9
10#include "CharacterStyle.h"
11#include "List.h"
12#include "Paragraph.h"
13#include "TextListener.h"
14#include "UndoableEditListener.h"
15
16
17class TextDocument;
18typedef BReference<TextDocument> TextDocumentRef;
19
20
21class TextDocument : public BReferenceable {
22public:
23								TextDocument();
24								TextDocument(
25									CharacterStyle characterStyle,
26									ParagraphStyle paragraphStyle);
27								TextDocument(const TextDocument& other);
28
29			TextDocument&		operator=(const TextDocument& other);
30			bool				operator==(const TextDocument& other) const;
31			bool				operator!=(const TextDocument& other) const;
32
33			// Text insertion and removing
34			status_t			Insert(int32 textOffset, const BString& text);
35			status_t			Insert(int32 textOffset, const BString& text,
36									CharacterStyle style);
37			status_t			Insert(int32 textOffset, const BString& text,
38									CharacterStyle characterStyle,
39									ParagraphStyle paragraphStyle);
40
41			status_t			Remove(int32 textOffset, int32 length);
42
43			status_t			Replace(int32 textOffset, int32 length,
44									const BString& text);
45			status_t			Replace(int32 textOffset, int32 length,
46									const BString& text,
47									CharacterStyle style);
48			status_t			Replace(int32 textOffset, int32 length,
49									const BString& text,
50									CharacterStyle characterStyle,
51									ParagraphStyle paragraphStyle);
52			status_t			Replace(int32 textOffset, int32 length,
53									TextDocumentRef document);
54
55			// Style access
56			const CharacterStyle& CharacterStyleAt(int32 textOffset) const;
57			const ParagraphStyle& ParagraphStyleAt(int32 textOffset) const;
58			BCursor				CursorAt(int32 textOffset) const;
59			const BMessage*		ClickMessageAt(int32 textOffset) const;
60
61			int32				CountParagraphs() const;
62			const Paragraph&	ParagraphAtIndex(int32 index) const;
63
64			int32				ParagraphIndexFor(int32 textOffset,
65									int32& paragraphOffset) const;
66
67			const Paragraph&	ParagraphAt(int32 textOffset,
68									int32& paragraphOffset) const;
69
70			const Paragraph&	ParagraphAt(int32 index) const;
71
72			bool				Append(const Paragraph& paragraph);
73
74			// Query information
75			int32				Length() const;
76
77			BString				Text() const;
78			BString				Text(int32 textOffset, int32 length) const;
79			TextDocumentRef		SubDocument(int32 textOffset,
80									int32 length) const;
81
82			void				PrintToStream() const;
83
84			// Support
85	static	TextDocumentRef		NormalizeText(const BString& text,
86									CharacterStyle characterStyle,
87									ParagraphStyle paragraphStyle);
88
89			// Listener support
90			bool				AddListener(TextListenerRef listener);
91			bool				RemoveListener(TextListenerRef listener);
92			bool				AddUndoListener(
93									UndoableEditListenerRef listener);
94			bool				RemoveUndoListener(
95									UndoableEditListenerRef listener);
96
97private:
98			status_t			_Insert(int32 textOffset,
99									TextDocumentRef document,
100									int32& firstParagraph,
101									int32& paragraphCount);
102			status_t			_Remove(int32 textOffset, int32 length,
103									int32& firstParagraph,
104									int32& paragraphCount);
105
106private:
107			void				_NotifyTextChanging(
108									TextChangingEvent& event) const;
109			void				_NotifyTextChanged(
110									const TextChangedEvent& event) const;
111			void				_NotifyUndoableEditHappened(
112									const UndoableEditRef& edit) const;
113
114private:
115			std::vector<Paragraph>
116								fParagraphs;
117			Paragraph			fEmptyLastParagraph;
118			CharacterStyle		fDefaultCharacterStyle;
119
120			std::vector<TextListenerRef>
121								fTextListeners;
122			std::vector<UndoableEditListenerRef>
123								fUndoListeners;
124};
125
126
127#endif // TEXT_DOCUMENT_H
128