]> git.lyx.org Git - lyx.git/blob - src/lyxrow.C
70edc13f1b0a0c17ed9ade1806a9e8bbdcee8645
[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 pos_type Row::lastPos() const
134 {
135         if (!par()->size())
136                 return 0;
137
138         if (!next() || next()->par() != par()) {
139                 return par()->size() - 1;
140         } else {
141                 return next()->pos() - 1;
142         }
143 }
144
145
146 namespace {
147
148 bool nextRowIsAllInset(Row const & row, pos_type last)
149 {
150         if (!row.next())
151                 return false;
152
153         if (row.par() != row.next()->par())
154                 return false;
155
156         if (!row.par()->isInset(last + 1))
157                 return false;
158
159         Inset * i = row.par()->getInset(last + 1);
160         return i->needFullRow() || i->display();
161 }
162
163 };
164
165
166 pos_type Row::lastPrintablePos() const
167 {
168         pos_type const last = lastPos();
169         bool const ignore_space_at_last = !nextRowIsAllInset(*this, last);
170
171         if (ignore_space_at_last && par()->isSeparator(last))
172                 return last - 1;
173
174         return last;
175 }
176
177
178 int Row::numberOfSeparators() const
179 {
180         pos_type const last = lastPrintablePos();
181         pos_type p = max(pos(), par()->beginningOfMainBody());
182
183         int n = 0;
184         for (; p <= last; ++p) {
185                 if (par()->isSeparator(p)) {
186                         ++n;
187                 }
188         }
189         return n;
190 }
191
192
193 int Row::numberOfHfills() const
194 {
195         pos_type const last = lastPos();
196         pos_type first = pos();
197
198         // hfill *DO* count at the beginning of paragraphs!
199         if (first) {
200                 while (first <= last && par()->isHfill(first)) {
201                         ++first;
202                 }
203         }
204
205         first = max(first, par()->beginningOfMainBody());
206
207         int n = 0;
208
209         // last, because the end is ignored!
210         for (pos_type p = first; p <= last; ++p) {
211                 if (par()->isHfill(p))
212                         ++n;
213         }
214         return n;
215 }
216
217
218 int Row::numberOfLabelHfills() const
219 {
220         pos_type last = lastPos();
221         pos_type first = pos();
222
223         // hfill *DO* count at the beginning of paragraphs!
224         if (first) {
225                 while (first < last && par()->isHfill(first))
226                         ++first;
227         }
228
229         last = min(last, par()->beginningOfMainBody());
230         int n = 0;
231
232         // last, because the end is ignored!
233         for (pos_type p = first; p < last; ++p) {
234                 if (par()->isHfill(p))
235                         ++n;
236         }
237         return n;
238 }
239
240
241 bool Row::hfillExpansion(pos_type pos) const
242 {
243         if (!par()->isHfill(pos))
244                 return false;
245
246         // at the end of a row it does not count
247         // unless another hfill exists on the line
248         if (pos >= lastPos()) {
249                 pos_type i = this->pos();
250                 while (i < pos && !par()->isHfill(i)) {
251                         ++i;
252                 }
253                 if (i == pos) {
254                         return false;
255                 }
256         }
257
258         // at the beginning of a row it does not count, if it is not
259         // the first row of a paragaph
260         if (!this->pos())
261                 return true;
262
263         // in some labels  it does not count
264         if (par()->layout()->margintype != MARGIN_MANUAL
265             && pos < par()->beginningOfMainBody())
266                 return false;
267
268         // if there is anything between the first char of the row and
269         // the sepcified position that is not a newline and not a hfill,
270         // the hfill will count, otherwise not
271         pos_type i = this->pos();
272         while (i < pos && (par()->isNewline(i)
273                            || par()->isHfill(i)))
274                 ++i;
275
276         return i != pos;
277 }