]> git.lyx.org Git - lyx.git/blob - src/Row.cpp
Remove enumitem example file from attic.
[lyx.git] / src / Row.cpp
1 /**
2  * \file Row.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  * \author André Pönitz
9  * \author Jürgen Vigna
10  * \author Jean-Marc Lasgouttes
11  *
12  * Full author contact details are available in file CREDITS.
13  *
14  * Metrics for an on-screen text row.
15  */
16
17 #include <config.h>
18
19 #include "Row.h"
20
21 #include "DocIterator.h"
22
23 #include "frontends/FontMetrics.h"
24
25 #include "support/debug.h"
26 #include "support/lassert.h"
27 #include "support/lstrings.h"
28 #include "support/lyxalgo.h"
29
30 #include <ostream>
31
32 using namespace std;
33
34 namespace lyx {
35
36 using support::rtrim;
37 using frontend::FontMetrics;
38
39
40 int Row::Element::countSeparators() const
41 {
42         if (type != STRING)
43                 return 0;
44         // Consecutive spaces count as only one separator.
45         bool wasspace = false;
46         int nsep = 0;
47         for (size_t i = 0 ; i < str.size() ; ++i) {
48                 if (str[i] == ' ') {
49                         if (!wasspace) {
50                                 ++nsep;
51                                 wasspace = true;
52                         }
53                 } else
54                         wasspace = false;
55         }
56         return nsep;
57 }
58
59
60 double Row::Element::pos2x(pos_type const i) const
61 {
62         // This can happen with inline completion when clicking on the
63         // row after the completion.
64         if (i < pos || i > endpos)
65                 return 0;
66
67         double w = 0;
68         //handle first the two bounds of the element
69         if (i == endpos && type != VIRTUAL
70                 && !(inset && inset->lyxCode() == SEPARATOR_CODE))
71                 w = isRTL() ? 0 : full_width();
72         else if (i == pos || type != STRING)
73                 w = isRTL() ? full_width() : 0;
74         else {
75                 FontMetrics const & fm = theFontMetrics(font);
76                 w = fm.pos2x(str, i - pos, isRTL(), extra);
77         }
78
79         return w;
80 }
81
82
83 pos_type Row::Element::x2pos(int &x) const
84 {
85         //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
86         size_t i = 0;
87
88         switch (type) {
89         case STRING: {
90                 FontMetrics const & fm = theFontMetrics(font);
91                 i = fm.x2pos(str, x, isRTL(), extra);
92                 break;
93         }
94         case VIRTUAL:
95                 // those elements are actually empty (but they have a width)
96                 i = 0;
97                 x = isRTL() ? int(full_width()) : 0;
98                 break;
99         case INSET:
100         case SPACE:
101                 // those elements contain only one position. Round to
102                 // the closest side.
103                 if (x > full_width()) {
104                         x = int(full_width());
105                         i = !isRTL();
106                 } else {
107                         x = 0;
108                         i = isRTL();
109                 }
110
111         }
112         //lyxerr << "=> p=" << pos + i << " x=" << x << endl;
113         return pos + i;
114
115 }
116
117
118 bool Row::Element::breakAt(int w, bool force)
119 {
120         if (type != STRING || dim.wid <= w)
121                 return false;
122
123         FontMetrics const & fm = theFontMetrics(font);
124         int x = w;
125         if(fm.breakAt(str, x, isRTL(), force)) {
126                 dim.wid = x;
127                 endpos = pos + str.length();
128                 //lyxerr << "breakAt(" << w << ")  Row element Broken at " << x << "(w(str)=" << fm.width(str) << "): e=" << *this << endl;
129                 return true;
130         }
131
132         // Qt will not break at a leading space, and we need that sometimes, see
133         //   http://www.lyx.org/trac/ticket/9921.
134         // It would be nice to fix this properly, but for now do it by hand.
135         // FIXME: figure out what to do for RtL text.
136         if (!isRTL() && !str.empty() && str[0] == ' ') {
137                 dim.wid = 0;
138                 str = ' ';
139                 endpos = pos + 1;
140                 return true;
141         }
142
143         return false;
144 }
145
146
147 pos_type Row::Element::left_pos() const
148 {
149         return isRTL() ? endpos : pos;
150 }
151
152
153 pos_type Row::Element::right_pos() const
154 {
155         return isRTL() ? pos : endpos;
156 }
157
158
159 Row::Row()
160         : separator(0), label_hfill(0), left_margin(0), right_margin(0),
161           sel_beg(-1), sel_end(-1),
162           begin_margin_sel(false), end_margin_sel(false),
163           changed_(false), crc_(0), pos_(0), end_(0), right_boundary_(false)
164 {}
165
166
167 void Row::setCrc(size_type crc) const
168 {
169         changed_ = crc != crc_;
170         crc_ = crc;
171 }
172
173
174 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
175                 DocIterator const & end) const
176 {
177         pos_type const sel_pos = left_margin ? sel_beg : sel_end;
178         pos_type const margin_pos = left_margin ? pos_ : end_;
179
180         // Is the chosen margin selected ?
181         if (sel_pos == margin_pos) {
182                 if (beg.pos() == end.pos())
183                         // This is a special case in which the space between after
184                         // pos i-1 and before pos i is selected, i.e. the margins
185                         // (see DocIterator::boundary_).
186                         return beg.boundary() && !end.boundary();
187                 else if (end.pos() == margin_pos)
188                         // If the selection ends around the margin, it is only
189                         // drawn if the cursor is after the margin.
190                         return !end.boundary();
191                 else if (beg.pos() == margin_pos)
192                         // If the selection begins around the margin, it is
193                         // only drawn if the cursor is before the margin.
194                         return beg.boundary();
195                 else
196                         return true;
197         }
198         return false;
199 }
200
201
202 void Row::setSelectionAndMargins(DocIterator const & beg,
203                 DocIterator const & end) const
204 {
205         setSelection(beg.pos(), end.pos());
206
207         if (selection()) {
208                 end_margin_sel = isMarginSelected(false, beg, end);
209                 begin_margin_sel = isMarginSelected(true, beg, end);
210         }
211 }
212
213
214 void Row::setSelection(pos_type beg, pos_type end) const
215 {
216         if (pos_ >= beg && pos_ <= end)
217                 sel_beg = pos_;
218         else if (beg > pos_ && beg <= end_)
219                 sel_beg = beg;
220         else
221                 sel_beg = -1;
222
223         if (end_ >= beg && end_ <= end)
224                 sel_end = end_;
225         else if (end < end_ && end >= pos_)
226                 sel_end = end;
227         else
228                 sel_end = -1;
229 }
230
231
232 bool Row::selection() const
233 {
234         return sel_beg != -1 && sel_end != -1;
235 }
236
237
238 ostream & operator<<(ostream & os, Row::Element const & e)
239 {
240         if (e.isRTL())
241                 os << e.endpos << "<<" << e.pos << " ";
242         else
243                 os << e.pos << ">>" << e.endpos << " ";
244
245         switch (e.type) {
246         case Row::STRING:
247                 os << "STRING: `" << to_utf8(e.str) << "' ("
248                    << e.countSeparators() << " sep.), ";
249                 break;
250         case Row::VIRTUAL:
251                 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
252                 break;
253         case Row::INSET:
254                 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
255                 break;
256         case Row::SPACE:
257                 os << "SPACE: ";
258                 break;
259         }
260         os << "width=" << e.full_width();
261         return os;
262 }
263
264
265 ostream & operator<<(ostream & os, Row const & row)
266 {
267         os << " pos: " << row.pos_ << " end: " << row.end_
268            << " left_margin: " << row.left_margin
269            << " width: " << row.dim_.wid
270            << " right_margin: " << row.right_margin
271            << " ascent: " << row.dim_.asc
272            << " descent: " << row.dim_.des
273            << " separator: " << row.separator
274            << " label_hfill: " << row.label_hfill 
275            << " row_boundary: " << row.right_boundary() << "\n";
276         double x = row.left_margin;
277         Row::Elements::const_iterator it = row.elements_.begin();
278         for ( ; it != row.elements_.end() ; ++it) {
279                 os << "x=" << x << " => " << *it << endl;
280                 x += it->full_width();
281         }
282         return os;
283 }
284
285
286 int Row::left_x() const
287 {
288         double x = left_margin;
289         const_iterator const end = elements_.end();
290         const_iterator cit = elements_.begin();
291         while (cit != end && cit->isVirtual()) {
292                 x += cit->full_width();
293                 ++cit;
294         }
295         return int(x + 0.5);
296 }
297
298
299 int Row::right_x() const
300 {
301         double x = dim_.wid;
302         const_iterator const begin = elements_.begin();
303         const_iterator cit = elements_.end();
304         while (cit != begin) {
305                 --cit;
306                 if (cit->isVirtual())
307                         x -= cit->full_width();
308                 else
309                         break;
310         }
311         return int(x + 0.5);
312 }
313
314
315 int Row::countSeparators() const
316 {
317         int n = 0;
318         const_iterator const end = elements_.end();
319         for (const_iterator cit = elements_.begin() ; cit != end ; ++cit)
320                 n += cit->countSeparators();
321         return n;
322 }
323
324
325 void Row::setSeparatorExtraWidth(double w)
326 {
327         separator = w;
328         iterator const end = elements_.end();
329         for (iterator it = elements_.begin() ; it != end ; ++it)
330                 if (it->type == Row::STRING)
331                         it->extra = w;
332 }
333
334
335 bool Row::sameString(Font const & f, Change const & ch) const
336 {
337         if (elements_.empty())
338                 return false;
339         Element const & elt = elements_.back();
340         return elt.type == STRING && !elt.final
341                    && elt.font == f && elt.change == ch;
342 }
343
344
345 void Row::finalizeLast()
346 {
347         if (elements_.empty())
348                 return;
349         Element & elt = elements_.back();
350         if (elt.final)
351                 return;
352         elt.final = true;
353
354         if (elt.type == STRING) {
355                 dim_.wid -= elt.dim.wid;
356                 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
357                 dim_.wid += elt.dim.wid;
358         }
359 }
360
361
362 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
363               Font const & f, Change const & ch)
364 {
365         finalizeLast();
366         Element e(INSET, pos, f, ch);
367         e.inset = ins;
368         e.dim = dim;
369         elements_.push_back(e);
370         dim_.wid += dim.wid;
371 }
372
373
374 void Row::add(pos_type const pos, char_type const c,
375               Font const & f, Change const & ch)
376 {
377         if (!sameString(f, ch)) {
378                 finalizeLast();
379                 Element e(STRING, pos, f, ch);
380                 elements_.push_back(e);
381         }
382         if (back().str.length() % 30 == 0) {
383                 dim_.wid -= back().dim.wid;
384                 back().str += c;
385                 back().endpos = pos + 1;
386                 back().dim.wid = theFontMetrics(back().font).width(back().str);
387                 dim_.wid += back().dim.wid;
388         } else {
389                 back().str += c;
390                 back().endpos = pos + 1;
391         }
392 }
393
394
395 void Row::addVirtual(pos_type const pos, docstring const & s,
396                      Font const & f, Change const & ch)
397 {
398         finalizeLast();
399         Element e(VIRTUAL, pos, f, ch);
400         e.str = s;
401         e.dim.wid = theFontMetrics(f).width(s);
402         dim_.wid += e.dim.wid;
403         e.endpos = pos;
404         elements_.push_back(e);
405         finalizeLast();
406 }
407
408
409 void Row::addSpace(pos_type const pos, int const width,
410                    Font const & f, Change const & ch)
411 {
412         finalizeLast();
413         Element e(SPACE, pos, f, ch);
414         e.dim.wid = width;
415         elements_.push_back(e);
416         dim_.wid += e.dim.wid;
417 }
418
419
420 void Row::pop_back()
421 {
422         dim_.wid -= elements_.back().dim.wid;
423         elements_.pop_back();
424 }
425
426
427 void Row::shortenIfNeeded(pos_type const keep, int const w)
428 {
429         if (empty() || width() <= w)
430                 return;
431
432         Elements::iterator const beg = elements_.begin();
433         Elements::iterator const end = elements_.end();
434         int wid = left_margin;
435
436         // Search for the first element that goes beyond right margin
437         Elements::iterator cit = beg;
438         for ( ; cit != end ; ++cit) {
439                 if (wid + cit->dim.wid > w)
440                         break;
441                 wid += cit->dim.wid;
442         }
443
444         if (cit == end) {
445                 // This should not happen since the row is too long.
446                 LYXERR0("Something is wrong cannot shorten row: " << *this);
447                 return;
448         }
449
450         // Iterate backwards over breakable elements and try to break them
451         Elements::iterator cit_brk = cit;
452         int wid_brk = wid + cit_brk->dim.wid;
453         ++cit_brk;
454         while (cit_brk != beg) {
455                 --cit_brk;
456                 // make a copy of the element to work on it.
457                 Element brk = *cit_brk;
458                 wid_brk -= brk.dim.wid;
459                 if (brk.countSeparators() == 0 || brk.pos < keep)
460                         continue;
461                 /* We have found a suitable separable element. This is the common case.
462                  * Try to break it cleanly (at word boundary) at a length that is both
463                  * - less than the available space on the row
464                  * - shorter than the natural width of the element, in order to enforce
465                  *   break-up.
466                  */
467                 if (brk.breakAt(min(w - wid_brk, brk.dim.wid - 2), false)) {
468                         /* if this element originally did not cause a row overflow
469                          * in itself, and the remainder of the row would still be
470                          * too large after breaking, then we will have issues in
471                          * next row. Thus breaking does not help.
472                          */
473                         if (wid_brk + cit_brk->dim.wid < w
474                             && dim_.wid - (wid_brk + brk.dim.wid) >= w) {
475                                 break;
476                         }
477                         end_ = brk.endpos;
478                         /* after breakAt, there may be spaces at the end of the
479                          * string, but they are not counted in the string length
480                          * (QTextLayout feature, actually). We remove them, but do
481                          * not change the end of the row, since spaces at row
482                          * break are invisible.
483                          */
484                         brk.str = rtrim(brk.str);
485                         brk.endpos = brk.pos + brk.str.length();
486                         *cit_brk = brk;
487                         dim_.wid = wid_brk + brk.dim.wid;
488                         // If there are other elements, they should be removed.
489                         elements_.erase(cit_brk + 1, end);
490                         return;
491                 }
492         }
493
494         if (cit != beg && cit->type == VIRTUAL) {
495                 // It is not possible to separate a virtual element from the
496                 // previous one.
497                 --cit;
498                 wid -= cit->dim.wid;
499         }
500
501         if (cit != beg) {
502                 // There is no usable separator, but several elements have
503                 // been added. We can cut right here.
504                 end_ = cit->pos;
505                 dim_.wid = wid;
506                 elements_.erase(cit, end);
507                 return;
508         }
509
510         /* If we are here, it means that we have not found a separator to
511          * shorten the row. Let's try to break it again, but not at word
512          * boundary this time.
513          */
514         if (cit->breakAt(w - wid, true)) {
515                 end_ = cit->endpos;
516                 // See comment above.
517                 cit->str = rtrim(cit->str);
518                 cit->endpos = cit->pos + cit->str.length();
519                 dim_.wid = wid + cit->dim.wid;
520                 // If there are other elements, they should be removed.
521                 elements_.erase(next(cit, 1), end);
522         }
523 }
524
525
526 void Row::reverseRTL(bool const rtl_par)
527 {
528         pos_type i = 0;
529         pos_type const end = elements_.size();
530         while (i < end) {
531                 // gather a sequence of elements with the same direction
532                 bool const rtl = elements_[i].isRTL();
533                 pos_type j = i;
534                 while (j < end && elements_[j].isRTL() == rtl)
535                         ++j;
536                 // if the direction is not the same as the paragraph
537                 // direction, the sequence has to be reverted.
538                 if (rtl != rtl_par)
539                         reverse(elements_.begin() + i, elements_.begin() + j);
540                 i = j;
541         }
542         // If the paragraph itself is RTL, reverse everything
543         if (rtl_par)
544                 reverse(elements_.begin(), elements_.end());
545 }
546
547 } // namespace lyx