1 /* 2 * Copyright 2013-2015, Stephan Aßmus <superstippi@gmx.de>. 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 #ifndef TEXT_DOCUMENT_LAYOUT_H 6 #define TEXT_DOCUMENT_LAYOUT_H 7 8 #include <Referenceable.h> 9 10 #include "List.h" 11 #include "TextDocument.h" 12 #include "ParagraphLayout.h" 13 14 15 class BView; 16 17 18 class ParagraphLayoutInfo { 19 public: ParagraphLayoutInfo()20 ParagraphLayoutInfo() 21 : 22 y(0.0f), 23 layout() 24 { 25 } 26 ParagraphLayoutInfo(float y,const ParagraphLayoutRef & layout)27 ParagraphLayoutInfo(float y, const ParagraphLayoutRef& layout) 28 : 29 y(y), 30 layout(layout) 31 { 32 } 33 ParagraphLayoutInfo(const ParagraphLayoutInfo & other)34 ParagraphLayoutInfo(const ParagraphLayoutInfo& other) 35 : 36 y(other.y), 37 layout(other.layout) 38 { 39 } 40 41 42 ParagraphLayoutInfo& operator=(const ParagraphLayoutInfo& other) 43 { 44 y = other.y; 45 layout = other.layout; 46 return *this; 47 } 48 49 bool operator==(const ParagraphLayoutInfo& other) const 50 { 51 return y == other.y 52 && layout == other.layout; 53 } 54 55 bool operator!=(const ParagraphLayoutInfo& other) const 56 { 57 return !(*this == other); 58 } 59 60 public: 61 float y; 62 ParagraphLayoutRef layout; 63 }; 64 65 66 class TextDocumentLayout : public BReferenceable { 67 public: 68 TextDocumentLayout(); 69 TextDocumentLayout( 70 const TextDocumentRef& document); 71 TextDocumentLayout( 72 const TextDocumentLayout& other); 73 virtual ~TextDocumentLayout(); 74 75 void SetTextDocument( 76 const TextDocumentRef& document); 77 78 void Invalidate(); 79 void InvalidateParagraphs(int32 start, int32 count); 80 81 void SetWidth(float width); Width()82 float Width() const 83 { return fWidth; } 84 85 float Height(); 86 void Draw(BView* view, const BPoint& offset, 87 const BRect& updateRect); 88 89 int32 LineIndexForOffset(int32 textOffset); 90 int32 FirstOffsetOnLine(int32 lineIndex); 91 int32 LastOffsetOnLine(int32 lineIndex); 92 int32 CountLines(); 93 94 void GetLineBounds(int32 lineIndex, 95 float& x1, float& y1, 96 float& x2, float& y2); 97 98 void GetTextBounds(int32 textOffset, 99 float& x1, float& y1, 100 float& x2, float& y2); 101 102 int32 TextOffsetAt(float x, float y, 103 bool& rightOfCenter); 104 105 private: 106 void _Init(); 107 void _ValidateLayout(); 108 void _Layout(); 109 110 void _DrawLayout(BView* view, 111 const ParagraphLayoutInfo& layout) const; 112 113 int32 _ParagraphLayoutIndexForOffset( 114 int32& textOffset); 115 int32 _ParagraphLayoutIndexForLineIndex( 116 int32& lineIndex, 117 int32& paragraphOffset); 118 119 private: 120 float fWidth; 121 bool fLayoutValid; 122 123 TextDocumentRef fDocument; 124 TextListenerRef fTextListener; 125 std::vector<ParagraphLayoutInfo> 126 fParagraphLayouts; 127 }; 128 129 130 typedef BReference<TextDocumentLayout> TextDocumentLayoutRef; 131 132 #endif // TEXT_DOCUMENT_LAYOUT_H 133