xref: /haiku/src/apps/haikudepot/textview/TextDocument.h (revision 1deede7388b04dbeec5af85cae7164735ea9e70d)
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 
17 class TextDocument;
18 typedef BReference<TextDocument> TextDocumentRef;
19 
20 
21 class TextDocument : public BReferenceable {
22 public:
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 
59 			int32				CountParagraphs() const;
60 			const Paragraph&	ParagraphAtIndex(int32 index) const;
61 
62 			int32				ParagraphIndexFor(int32 textOffset,
63 									int32& paragraphOffset) const;
64 
65 			const Paragraph&	ParagraphAt(int32 textOffset,
66 									int32& paragraphOffset) const;
67 
68 			const Paragraph&	ParagraphAt(int32 index) const;
69 
70 			bool				Append(const Paragraph& paragraph);
71 
72 			// Query information
73 			int32				Length() const;
74 
75 			BString				Text() const;
76 			BString				Text(int32 textOffset, int32 length) const;
77 			TextDocumentRef		SubDocument(int32 textOffset,
78 									int32 length) const;
79 
80 			void				PrintToStream() const;
81 
82 			// Support
83 	static	TextDocumentRef		NormalizeText(const BString& text,
84 									CharacterStyle characterStyle,
85 									ParagraphStyle paragraphStyle);
86 
87 			// Listener support
88 			bool				AddListener(TextListenerRef listener);
89 			bool				RemoveListener(TextListenerRef listener);
90 			bool				AddUndoListener(
91 									UndoableEditListenerRef listener);
92 			bool				RemoveUndoListener(
93 									UndoableEditListenerRef listener);
94 
95 private:
96 			status_t			_Insert(int32 textOffset,
97 									TextDocumentRef document,
98 									int32& firstParagraph,
99 									int32& paragraphCount);
100 			status_t			_Remove(int32 textOffset, int32 length,
101 									int32& firstParagraph,
102 									int32& paragraphCount);
103 
104 private:
105 			void				_NotifyTextChanging(
106 									TextChangingEvent& event) const;
107 			void				_NotifyTextChanged(
108 									const TextChangedEvent& event) const;
109 			void				_NotifyUndoableEditHappened(
110 									const UndoableEditRef& edit) const;
111 
112 private:
113 			std::vector<Paragraph>
114 								fParagraphs;
115 			Paragraph			fEmptyLastParagraph;
116 			CharacterStyle		fDefaultCharacterStyle;
117 
118 			std::vector<TextListenerRef>
119 								fTextListeners;
120 			std::vector<UndoableEditListenerRef>
121 								fUndoListeners;
122 };
123 
124 
125 #endif // TEXT_DOCUMENT_H
126