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