xref: /haiku/src/apps/haikudepot/textview/TextDocument.h (revision 4e3137c085bae361922078f123dceb92da700640)
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 typedef List<Paragraph, false>					ParagraphList;
18 typedef List<TextListenerRef, false>			TextListenerList;
19 typedef List<UndoableEditListenerRef, false>	UndoListenerList;
20 
21 class TextDocument;
22 typedef BReference<TextDocument> TextDocumentRef;
23 
24 
25 class TextDocument : public BReferenceable {
26 public:
27 								TextDocument();
28 								TextDocument(
29 									CharacterStyle characterStyle,
30 									ParagraphStyle paragraphStyle);
31 								TextDocument(const TextDocument& other);
32 
33 			TextDocument&		operator=(const TextDocument& other);
34 			bool				operator==(const TextDocument& other) const;
35 			bool				operator!=(const TextDocument& other) const;
36 
37 			// Text insertion and removing
38 			status_t			Insert(int32 textOffset, const BString& text);
39 			status_t			Insert(int32 textOffset, const BString& text,
40 									CharacterStyle style);
41 			status_t			Insert(int32 textOffset, const BString& text,
42 									CharacterStyle characterStyle,
43 									ParagraphStyle paragraphStyle);
44 
45 			status_t			Remove(int32 textOffset, int32 length);
46 
47 			status_t			Replace(int32 textOffset, int32 length,
48 									const BString& text);
49 			status_t			Replace(int32 textOffset, int32 length,
50 									const BString& text,
51 									CharacterStyle style);
52 			status_t			Replace(int32 textOffset, int32 length,
53 									const BString& text,
54 									CharacterStyle characterStyle,
55 									ParagraphStyle paragraphStyle);
56 			status_t			Replace(int32 textOffset, int32 length,
57 									TextDocumentRef document);
58 
59 			// Style access
60 			const CharacterStyle& CharacterStyleAt(int32 textOffset) const;
61 			const ParagraphStyle& ParagraphStyleAt(int32 textOffset) const;
62 
63 			// Paragraph access
64 			const ParagraphList& Paragraphs() const
65 									{ return fParagraphs; }
66 
67 			int32				CountParagraphs() const;
68 
69 			int32				ParagraphIndexFor(int32 textOffset,
70 									int32& paragraphOffset) const;
71 
72 			const Paragraph&	ParagraphAt(int32 textOffset,
73 									int32& paragraphOffset) const;
74 
75 			const Paragraph&	ParagraphAt(int32 index) const;
76 
77 			bool				Append(const Paragraph& paragraph);
78 
79 			// Query information
80 			int32				Length() const;
81 
82 			BString				Text() const;
83 			BString				Text(int32 textOffset, int32 length) const;
84 			TextDocumentRef		SubDocument(int32 textOffset,
85 									int32 length) const;
86 
87 			void				PrintToStream() const;
88 
89 			// Support
90 	static	TextDocumentRef		NormalizeText(const BString& text,
91 									CharacterStyle characterStyle,
92 									ParagraphStyle paragraphStyle);
93 
94 			// Listener support
95 			bool				AddListener(TextListenerRef listener);
96 			bool				RemoveListener(TextListenerRef listener);
97 			bool				AddUndoListener(
98 									UndoableEditListenerRef listener);
99 			bool				RemoveUndoListener(
100 									UndoableEditListenerRef listener);
101 
102 private:
103 			status_t			_Insert(int32 textOffset,
104 									TextDocumentRef document,
105 									int32& firstParagraph,
106 									int32& paragraphCount);
107 			status_t			_Remove(int32 textOffset, int32 length,
108 									int32& firstParagraph,
109 									int32& paragraphCount);
110 
111 private:
112 			void				_NotifyTextChanging(
113 									TextChangingEvent& event) const;
114 			void				_NotifyTextChanged(
115 									const TextChangedEvent& event) const;
116 			void				_NotifyUndoableEditHappened(
117 									const UndoableEditRef& edit) const;
118 
119 private:
120 			ParagraphList		fParagraphs;
121 			Paragraph			fEmptyLastParagraph;
122 			CharacterStyle		fDefaultCharacterStyle;
123 
124 			TextListenerList	fTextListeners;
125 			UndoListenerList	fUndoListeners;
126 };
127 
128 
129 #endif // TEXT_DOCUMENT_H
130