xref: /haiku/src/apps/haikudepot/textview/TextDocumentLayout.h (revision b3fe70844e087a579563b43cf4c1b2525946ca27)
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_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:
20 	ParagraphLayoutInfo()
21 		:
22 		y(0.0f),
23 		layout()
24 	{
25 	}
26 
27 	ParagraphLayoutInfo(float y, const ParagraphLayoutRef& layout)
28 		:
29 		y(y),
30 		layout(layout)
31 	{
32 	}
33 
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 typedef List<ParagraphLayoutInfo, false> ParagraphLayoutList;
67 
68 
69 class TextDocumentLayout : public BReferenceable {
70 public:
71 								TextDocumentLayout();
72 								TextDocumentLayout(
73 									const TextDocumentRef& document);
74 								TextDocumentLayout(
75 									const TextDocumentLayout& other);
76 	virtual						~TextDocumentLayout();
77 
78 			void				SetTextDocument(
79 									const TextDocumentRef& document);
80 
81 			void				Invalidate();
82 			void				InvalidateParagraphs(int32 start, int32 count);
83 
84 			void				SetWidth(float width);
85 			float				Width() const
86 									{ return fWidth; }
87 
88 			float				Height();
89 			void				Draw(BView* view, const BPoint& offset,
90 									const BRect& updateRect);
91 
92 			int32				LineIndexForOffset(int32 textOffset);
93 			int32				FirstOffsetOnLine(int32 lineIndex);
94 			int32				LastOffsetOnLine(int32 lineIndex);
95 			int32				CountLines();
96 
97 			void				GetLineBounds(int32 lineIndex,
98 									float& x1, float& y1,
99 									float& x2, float& y2);
100 
101 			void				GetTextBounds(int32 textOffset,
102 									float& x1, float& y1,
103 									float& x2, float& y2);
104 
105 			int32				TextOffsetAt(float x, float y,
106 									bool& rightOfCenter);
107 
108 private:
109 			void				_Init();
110 			void				_ValidateLayout();
111 			void				_Layout();
112 
113 			void				_DrawLayout(BView* view,
114 									const ParagraphLayoutInfo& layout) const;
115 
116 			int32				_ParagraphLayoutIndexForOffset(
117 									int32& textOffset);
118 			int32				_ParagraphLayoutIndexForLineIndex(
119 									int32& lineIndex,
120 									int32& paragraphOffset);
121 
122 private:
123 			float				fWidth;
124 			bool				fLayoutValid;
125 
126 			TextDocumentRef		fDocument;
127 			ParagraphLayoutList	fParagraphLayouts;
128 };
129 
130 
131 typedef BReference<TextDocumentLayout> TextDocumentLayoutRef;
132 
133 #endif // TEXT_DOCUMENT_LAYOUT_H
134