]> git.lyx.org Git - lyx.git/blob - src/lyxrow.C
The row-painter patch
[lyx.git] / src / lyxrow.C
1 /**
2  * \file lyxrow.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  *
8  * Full author contact details are available in file CREDITS
9  *
10  * Metrics for an on-screen text row.
11  */
12
13 #include <config.h>
14
15 #include "lyxrow.h"
16 #include "paragraph.h"
17 #include "layout.h"
18 #include "lyxlayout.h"
19
20 using lyx::pos_type;
21
22 using std::max;
23 using std::min;
24
25 Row::Row()
26         : par_(0), pos_(0), fill_(0), height_(0), width_(0),
27           ascent_of_text_(0), baseline_(0), next_(0), previous_(0)
28 {}
29
30
31 void Row::par(Paragraph * p)
32 {
33         par_ = p;
34 }
35
36
37 void Row::pos(pos_type p)
38 {
39         pos_ = p;
40 }
41
42
43 pos_type Row::pos() const
44 {
45         return pos_;
46 }
47
48
49 void Row::fill(int f)
50 {
51         fill_ = f;
52 }
53
54
55 int Row::fill() const
56 {
57         return fill_;
58 }
59
60
61 void Row::height(unsigned short h)
62 {
63         height_ = h;
64 }
65
66
67 void Row::width(unsigned int w)
68 {
69         width_ = w;
70 }
71
72
73 unsigned int Row::width() const
74 {
75         return width_;
76 }
77
78
79 void Row::ascent_of_text(unsigned short a)
80 {
81         ascent_of_text_ = a;
82 }
83
84
85 unsigned short Row::ascent_of_text() const
86 {
87         return ascent_of_text_;
88 }
89
90
91 void Row::top_of_text(unsigned int top)
92 {
93         top_of_text_ = top;
94 }
95
96  
97 unsigned int Row::top_of_text() const
98 {
99         return top_of_text_;
100 }
101
102  
103 void Row::baseline(unsigned int b)
104 {
105         baseline_ = b;
106 }
107
108
109 unsigned int Row::baseline() const
110 {
111         return baseline_;
112 }
113
114
115 void Row::next(Row * r)
116 {
117         next_ = r;
118 }
119
120
121 void Row::previous(Row * r)
122 {
123         previous_ = r;
124 }
125
126
127 Row * Row::previous() const
128 {
129         return previous_;
130 }
131
132
133 bool Row::isParStart() const
134 {
135         return !pos();
136 }
137
138
139 bool Row::isParEnd() const
140 {
141         return !next() || next()->par() != par();
142 }
143
144
145 pos_type Row::lastPos() const
146 {
147         if (!par()->size())
148                 return 0;
149
150         if (isParEnd()) {
151                 return par()->size() - 1;
152         } else {
153                 return next()->pos() - 1;
154         }
155 }
156
157
158 namespace {
159
160 bool nextRowIsAllInset(Row const & row, pos_type last)
161 {
162         if (!row.par()->isInset(last + 1))
163                 return false;
164
165         Inset * i = row.par()->getInset(last + 1);
166         return i->needFullRow() || i->display();
167 }
168
169 };
170
171
172 pos_type Row::lastPrintablePos() const
173 {
174         pos_type const last = lastPos();
175
176         // if this row is an end of par, just act like lastPos()
177         if (isParEnd())
178                 return last;
179
180         bool const nextrownotinset = !nextRowIsAllInset(*this, last);
181
182         if (nextrownotinset && par()->isSeparator(last))
183                 return last - 1;
184
185         return last;
186 }
187
188
189 int Row::numberOfSeparators() const
190 {
191         pos_type const last = lastPrintablePos();
192         pos_type p = max(pos(), par()->beginningOfMainBody());
193
194         int n = 0;
195         for (; p <= last; ++p) {
196                 if (par()->isSeparator(p)) {
197                         ++n;
198                 }
199         }
200         return n;
201 }
202
203
204 int Row::numberOfHfills() const
205 {
206         pos_type const last = lastPos();
207         pos_type first = pos();
208
209         // hfill *DO* count at the beginning of paragraphs!
210         if (first) {
211                 while (first <= last && par()->isHfill(first)) {
212                         ++first;
213                 }
214         }
215
216         first = max(first, par()->beginningOfMainBody());
217
218         int n = 0;
219
220         // last, because the end is ignored!
221         for (pos_type p = first; p <= last; ++p) {
222                 if (par()->isHfill(p))
223                         ++n;
224         }
225         return n;
226 }
227
228
229 int Row::numberOfLabelHfills() const
230 {
231         pos_type last = lastPos();
232         pos_type first = pos();
233
234         // hfill *DO* count at the beginning of paragraphs!
235         if (first) {
236                 while (first < last && par()->isHfill(first))
237                         ++first;
238         }
239
240         last = min(last, par()->beginningOfMainBody());
241         int n = 0;
242
243         // last, because the end is ignored!
244         for (pos_type p = first; p < last; ++p) {
245                 if (par()->isHfill(p))
246                         ++n;
247         }
248         return n;
249 }
250
251
252 bool Row::hfillExpansion(pos_type pos) const
253 {
254         if (!par()->isHfill(pos))
255                 return false;
256
257         // at the end of a row it does not count
258         // unless another hfill exists on the line
259         if (pos >= lastPos()) {
260                 pos_type i = this->pos();
261                 while (i < pos && !par()->isHfill(i)) {
262                         ++i;
263                 }
264                 if (i == pos) {
265                         return false;
266                 }
267         }
268
269         // at the beginning of a row it does not count, if it is not
270         // the first row of a paragaph
271         if (isParStart())
272                 return true;
273
274         // in some labels  it does not count
275         if (par()->layout()->margintype != MARGIN_MANUAL
276             && pos < par()->beginningOfMainBody())
277                 return false;
278
279         // if there is anything between the first char of the row and
280         // the sepcified position that is not a newline and not a hfill,
281         // the hfill will count, otherwise not
282         pos_type i = this->pos();
283         while (i < pos && (par()->isNewline(i)
284                            || par()->isHfill(i)))
285                 ++i;
286
287         return i != pos;
288 }