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 15 16 typedef List<Paragraph, false> ParagraphList; 17 typedef List<TextListenerRef, false> TextListenerList; 18 19 class TextDocument; 20 typedef BReference<TextDocument> TextDocumentRef; 21 22 23 class TextDocument : public BReferenceable { 24 public: 25 TextDocument(); 26 TextDocument( 27 const CharacterStyle& characterStyle, 28 const ParagraphStyle& paragraphStyle); 29 TextDocument(const TextDocument& other); 30 31 TextDocument& operator=(const TextDocument& other); 32 bool operator==(const TextDocument& other) const; 33 bool operator!=(const TextDocument& other) const; 34 35 // Text insertion and removing 36 status_t Insert(int32 textOffset, const BString& text); 37 status_t Insert(int32 textOffset, const BString& text, 38 const CharacterStyle& style); 39 status_t Insert(int32 textOffset, const BString& text, 40 const CharacterStyle& characterStyle, 41 const ParagraphStyle& paragraphStyle); 42 43 status_t Remove(int32 textOffset, int32 length); 44 45 status_t Replace(int32 textOffset, int32 length, 46 const BString& text); 47 status_t Replace(int32 textOffset, int32 length, 48 const BString& text, 49 const CharacterStyle& style); 50 status_t Replace(int32 textOffset, int32 length, 51 const BString& text, 52 const CharacterStyle& characterStyle, 53 const ParagraphStyle& paragraphStyle); 54 55 // Style access 56 const CharacterStyle& CharacterStyleAt(int32 textOffset) const; 57 const ParagraphStyle& ParagraphStyleAt(int32 textOffset) const; 58 59 // Paragraph access 60 const ParagraphList& Paragraphs() const 61 { return fParagraphs; } 62 63 int32 ParagraphIndexFor(int32 textOffset, 64 int32& paragraphOffset) const; 65 66 const Paragraph& ParagraphAt(int32 textOffset, 67 int32& paragraphOffset) const; 68 69 const Paragraph& ParagraphAt(int32 index) const; 70 71 bool Append(const Paragraph& paragraph); 72 73 // Query information 74 int32 Length() const; 75 76 BString Text() const; 77 BString Text(int32 textOffset, int32 length) const; 78 TextDocumentRef SubDocument(int32 textOffset, 79 int32 length) const; 80 81 void PrintToStream() const; 82 83 // Listener support 84 bool AddListener(const TextListenerRef& listener); 85 bool RemoveListener(const TextListenerRef& listener); 86 87 private: 88 void _NotifyTextChanging( 89 TextChangingEvent& event) const; 90 void _NotifyTextChanged( 91 const TextChangedEvent& event) const; 92 93 private: 94 ParagraphList fParagraphs; 95 Paragraph fEmptyLastParagraph; 96 CharacterStyle fDefaultCharacterStyle; 97 98 TextListenerList fTextListeners; 99 }; 100 101 102 #endif // TEXT_DOCUMENT_H 103