]> git.lyx.org Git - lyx.git/blob - src/RowPainter.cpp
support for the languages Bosnian, Friulian, Macedonian, Piedmontese and Romansh
[lyx.git] / src / RowPainter.cpp
1 /**
2  * \file RowPainter.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author various
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13 #include <algorithm>
14
15 #include "RowPainter.h"
16
17 #include "Buffer.h"
18 #include "CoordCache.h"
19 #include "Cursor.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
22 #include "Changes.h"
23 #include "Language.h"
24 #include "Layout.h"
25 #include "LyXRC.h"
26 #include "Row.h"
27 #include "MetricsInfo.h"
28 #include "Paragraph.h"
29 #include "ParagraphMetrics.h"
30 #include "ParagraphParameters.h"
31 #include "TextMetrics.h"
32 #include "VSpace.h"
33
34 #include "frontends/FontMetrics.h"
35 #include "frontends/Painter.h"
36
37 #include "insets/InsetText.h"
38
39 #include "mathed/InsetMath.h"
40
41 #include "support/debug.h"
42 #include "support/gettext.h"
43 #include "support/textutils.h"
44
45 #include "support/lassert.h"
46 #include <boost/crc.hpp>
47
48 #include <stdlib.h>
49
50 using namespace std;
51
52 namespace lyx {
53
54 using frontend::Painter;
55 using frontend::FontMetrics;
56
57
58 RowPainter::RowPainter(PainterInfo & pi,
59         Text const & text, Row const & row, int x, int y)
60         : pi_(pi), text_(text),
61           text_metrics_(pi_.base.bv->textMetrics(&text)),
62           pars_(text.paragraphs()),
63           row_(row), par_(text.paragraphs()[row.pit()]),
64           pm_(text_metrics_.parMetrics(row.pit())), change_(pi_.change_),
65           xo_(x), yo_(y), width_(text_metrics_.width())
66 {
67         x_ = row_.left_margin + xo_;
68
69         //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
70         //row_.dump();
71
72         LBUFERR(row.pit() >= 0);
73         LBUFERR(row.pit() < int(text.paragraphs().size()));
74 }
75
76
77 FontInfo RowPainter::labelFont() const
78 {
79         FontInfo f = text_.labelFont(par_);
80         // selected text?
81         if (row_.begin_margin_sel || pi_.selected)
82                 f.setPaintColor(Color_selectiontext);
83         return f;
84 }
85
86
87 // If you want to debug inset metrics uncomment the following line:
88 //#define DEBUG_METRICS
89 // This draws green lines around each inset.
90
91
92 void RowPainter::paintInset(Row::Element const & e) const
93 {
94         // Handle selection
95         bool const pi_selected = pi_.selected;
96         Cursor const & cur = pi_.base.bv->cursor();
97         if (cur.selection() && cur.text() == &text_
98                 && cur.normalAnchor().text() == &text_)
99                 pi_.selected = row_.sel_beg <= e.pos && row_.sel_end > e.pos;
100
101         LASSERT(e.inset, return);
102         // Backup full_repaint status because some insets (InsetTabular)
103         // requires a full repaint
104         bool const pi_full_repaint = pi_.full_repaint;
105         bool const pi_do_spellcheck = pi_.do_spellcheck;
106         Change const pi_change = pi_.change_;
107
108         pi_.base.font = e.inset->inheritFont() ? e.font.fontInfo() :
109                 pi_.base.bv->buffer().params().getFont().fontInfo();
110         pi_.ltr_pos = !e.font.isVisibleRightToLeft();
111         pi_.change_ = change_.changed() ? change_ : e.change;
112         pi_.do_spellcheck &= e.inset->allowSpellCheck();
113
114         int const x1 = int(x_);
115         pi_.base.bv->coordCache().insets().add(e.inset, x1, yo_);
116         // insets are painted completely. Recursive
117         // FIXME: it is wrong to completely paint the background
118         // if we want to do single row painting.
119         e.inset->drawBackground(pi_, x1, yo_);
120         e.inset->drawSelection(pi_, x1, yo_);
121         e.inset->draw(pi_, x1, yo_);
122
123         // Restore full_repaint status.
124         pi_.full_repaint = pi_full_repaint;
125         pi_.change_ = pi_change;
126         pi_.do_spellcheck = pi_do_spellcheck;
127         pi_.selected = pi_selected;
128
129 #ifdef DEBUG_METRICS
130         Dimension const & dim = pi_.base.bv->coordCache().insets().dim(e.inset);
131         int const x2 = x1 + dim.wid;
132         int const y1 = yo_ + dim.des;
133         int const y2 = yo_ - dim.asc;
134         pi_.pain.line(x1, y1, x1, y2, Color_green);
135         pi_.pain.line(x1, y1, x2, y1, Color_green);
136         pi_.pain.line(x2, y1, x2, y2, Color_green);
137         pi_.pain.line(x1, y2, x2, y2, Color_green);
138 #endif
139 }
140
141
142 void RowPainter::paintForeignMark(Row::Element const & e) const
143 {
144         Language const * lang = e.font.language();
145         if (!lyxrc.mark_foreign_language)
146                 return;
147         if (lang == latex_language)
148                 return;
149         if (lang == pi_.base.bv->buffer().params().language)
150                 return;
151
152         int const desc = e.inset ? e.dim.descent() : 0;
153         int const y = yo_ + pi_.base.solidLineOffset()
154                 + desc + pi_.base.solidLineThickness() / 2;
155         pi_.pain.line(int(x_), y, int(x_ + e.full_width()), y, Color_language,
156                       Painter::line_solid, pi_.base.solidLineThickness());
157 }
158
159
160 void RowPainter::paintMisspelledMark(Row::Element const & e) const
161 {
162         // if changed the misspelled marker gets placed slightly lower than normal
163         // to avoid drawing at the same vertical offset
164         FontMetrics const & fm = theFontMetrics(e.font);
165         int const thickness = max(fm.lineWidth(), 2);
166         int const y = yo_ + pi_.base.solidLineOffset() + pi_.base.solidLineThickness()
167                 + (e.change.changed() ? pi_.base.solidLineThickness() + 1 : 0)
168                 + 1 + thickness / 2;
169
170         //FIXME: this could be computed only once, it is probably not costly.
171         // check for cursor position
172         // don't draw misspelled marker for words at cursor position
173         // we don't want to disturb the process of text editing
174         DocIterator const nw = pi_.base.bv->cursor().newWord();
175         pos_type cpos = -1;
176         if (!nw.empty() && par_.id() == nw.paragraph().id()) {
177                 cpos = nw.pos();
178                 if (cpos > 0 && cpos == par_.size() && !par_.isWordSeparator(cpos-1))
179                         --cpos;
180                 else if (cpos > 0 && par_.isWordSeparator(cpos))
181                         --cpos;
182         }
183
184         pos_type pos = e.pos;
185         while (pos < e.pos + pos_type(e.str.length())) {
186                 if (!par_.isMisspelled(pos)) {
187                         ++pos;
188                         continue;
189                 }
190
191                 FontSpan const & range = par_.getSpellRange(pos);
192
193                 // Skip element which are being edited
194                 if (range.contains(cpos)) {
195                         // the range includes the last element
196                         pos = range.last + 1;
197                         continue;
198                 }
199
200                 FontMetrics const & fm = theFontMetrics(e.font);
201                 int x1 = fm.pos2x(e.str, range.first - e.pos,
202                                   e.isRTL(), e.extra);
203                 int x2 = fm.pos2x(e.str, min(range.last - e.pos + 1,
204                                                                          pos_type(e.str.length())),
205                                                                          e.isRTL(), e.extra);
206                 if (x1 > x2)
207                         swap(x1, x2);
208
209                 pi_.pain.line(int(x_ + x1), y, int(x_ + x2), y, 
210                               Color_error,
211                               Painter::line_onoffdash, thickness);
212                 pos = range.last + 1;
213         }
214 }
215
216
217 void RowPainter::paintStringAndSel(Row::Element const & e) const
218 {
219         // at least part of text selected?
220         bool const some_sel = (e.endpos >= row_.sel_beg && e.pos < row_.sel_end)
221                 || pi_.selected;
222         // all the text selected?
223         bool const all_sel = (e.pos >= row_.sel_beg && e.endpos < row_.sel_end)
224                 || pi_.selected;
225
226         if (all_sel || e.change.changed()) {
227                 Font copy = e.font;
228                 Color const col = e.change.changed() ? e.change.color()
229                                                      : Color_selectiontext;
230                 copy.fontInfo().setPaintColor(col);
231                 pi_.pain.text(int(x_), yo_, e.str, copy, e.extra, e.full_width());
232         } else if (!some_sel) {
233                 pi_.pain.text(int(x_), yo_, e.str, e.font, e.extra, e.full_width());
234         } else {
235                 pi_.pain.text(int(x_), yo_, e.str, e.font, Color_selectiontext,
236                               max(row_.sel_beg, e.pos) - e.pos,
237                               min(row_.sel_end, e.endpos) - e.pos,
238                               e.extra, e.full_width());
239         }
240 }
241
242
243 void RowPainter::paintChange(Row::Element const & e) const
244 {
245         e.change.paintCue(pi_, x_, yo_, x_ + e.full_width(), e.font.fontInfo());
246 }
247
248
249 void RowPainter::paintChangeBar() const
250 {
251         pos_type const start = row_.pos();
252         pos_type end = row_.endpos();
253
254         if (par_.size() == end) {
255                 // this is the last row of the paragraph;
256                 // thus, we must also consider the imaginary end-of-par character
257                 end++;
258         }
259
260         if (start == end || !par_.isChanged(start, end))
261                 return;
262
263         int const height = text_metrics_.isLastRow(row_)
264                 ? row_.ascent()
265                 : row_.height();
266
267         pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
268 }
269
270
271 void RowPainter::paintAppendix() const
272 {
273         // only draw the appendix frame once (for the main text)
274         if (!par_.params().appendix() || !text_.isMainText())
275                 return;
276
277         int y = yo_ - row_.ascent();
278
279         if (par_.params().startOfAppendix())
280                 y += 2 * defaultRowHeight();
281
282         pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
283         pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
284 }
285
286
287 void RowPainter::paintDepthBar() const
288 {
289         depth_type const depth = par_.getDepth();
290
291         if (depth <= 0)
292                 return;
293
294         depth_type prev_depth = 0;
295         if (!text_metrics_.isFirstRow(row_)) {
296                 pit_type pit2 = row_.pit();
297                 if (row_.pos() == 0)
298                         --pit2;
299                 prev_depth = pars_[pit2].getDepth();
300         }
301
302         depth_type next_depth = 0;
303         if (!text_metrics_.isLastRow(row_)) {
304                 pit_type pit2 = row_.pit();
305                 if (row_.endpos() >= pars_[pit2].size())
306                         ++pit2;
307                 next_depth = pars_[pit2].getDepth();
308         }
309
310         for (depth_type i = 1; i <= depth; ++i) {
311                 int const w = nestMargin() / 5;
312                 int x = int(xo_) + w * i;
313                 // only consider the changebar space if we're drawing outermost text
314                 if (text_.isMainText())
315                         x += changebarMargin();
316
317                 int const starty = yo_ - row_.ascent();
318                 int const h =  row_.height() - 1 - (i - next_depth - 1) * 3;
319
320                 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
321
322                 if (i > prev_depth)
323                         pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
324                 if (i > next_depth)
325                         pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
326         }
327 }
328
329
330 void RowPainter::paintAppendixStart(int y) const
331 {
332         FontInfo pb_font = sane_font;
333         pb_font.setColor(Color_appendix);
334         pb_font.decSize();
335
336         int w = 0;
337         int a = 0;
338         int d = 0;
339
340         docstring const label = _("Appendix");
341         theFontMetrics(pb_font).rectText(label, w, a, d);
342
343         int const text_start = int(xo_ + (width_ - w) / 2);
344         int const text_end = text_start + w;
345
346         pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
347
348         pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
349         pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
350 }
351
352
353 void RowPainter::paintTooLargeMarks(bool const left, bool const right) const
354 {
355         if (left)
356                 pi_.pain.line(pi_.base.dottedLineThickness(), yo_ - row_.ascent(),
357                                           pi_.base.dottedLineThickness(), yo_ + row_.descent(),
358                                           Color_scroll, Painter::line_onoffdash,
359                               pi_.base.dottedLineThickness());
360         if (right) {
361                 int const wwidth =
362                         pi_.base.bv->workWidth() - pi_.base.dottedLineThickness();
363                 pi_.pain.line(wwidth, yo_ - row_.ascent(),
364                                           wwidth, yo_ + row_.descent(),
365                                           Color_scroll, Painter::line_onoffdash,
366                               pi_.base.dottedLineThickness());
367         }
368 }
369
370
371 void RowPainter::paintFirst() const
372 {
373         Layout const & layout = par_.layout();
374
375         // start of appendix?
376         if (par_.params().startOfAppendix())
377             paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
378
379         bool const is_first =
380                 text_.isFirstInSequence(row_.pit()) || !layout.isParagraphGroup();
381         //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
382
383         if (layout.labelIsInline()
384             && (layout.labeltype != LABEL_STATIC || is_first))
385                 paintLabel();
386         else if (is_first && layout.labelIsAbove())
387                 paintTopLevelLabel();
388 }
389
390
391 void RowPainter::paintLabel() const
392 {
393         docstring const & str = par_.labelString();
394         if (str.empty())
395                 return;
396
397         bool const is_rtl = text_.isRTL(par_);
398         Layout const & layout = par_.layout();
399         FontInfo const font = labelFont();
400         FontMetrics const & fm = theFontMetrics(font);
401         double x = x_;
402
403         if (is_rtl)
404                 x = width_ - row_.right_margin + fm.width(layout.labelsep);
405         else
406                 x = x_ - fm.width(layout.labelsep) - fm.width(str);
407
408         pi_.pain.text(int(x), yo_, str, font);
409 }
410
411
412 void RowPainter::paintTopLevelLabel() const
413 {
414         BufferParams const & bparams = pi_.base.bv->buffer().params();
415         bool const is_rtl = text_.isRTL(par_);
416         ParagraphParameters const & pparams = par_.params();
417         Layout const & layout = par_.layout();
418         FontInfo const font = labelFont();
419         docstring const str = par_.labelString();
420         if (str.empty())
421                 return;
422
423         double spacing_val = 1.0;
424         if (!pparams.spacing().isDefault())
425                 spacing_val = pparams.spacing().getValue();
426         else
427                 spacing_val = bparams.spacing().getValue();
428
429         FontMetrics const & fm = theFontMetrics(font);
430
431         int const labeladdon = int(fm.maxHeight()
432                 * layout.spacing.getValue() * spacing_val);
433
434         int maxdesc =
435                 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
436                 + (layout.labelbottomsep * defaultRowHeight()));
437
438         double x = x_;
439         if (layout.labeltype == LABEL_CENTERED) {
440                 x = row_.left_margin + (width_ - row_.left_margin - row_.right_margin) / 2;
441                 x -= fm.width(str) / 2;
442         } else if (is_rtl) {
443                 x = width_ - row_.right_margin - fm.width(str);
444         }
445         pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
446 }
447
448
449 /** Check if the current paragraph is the last paragraph in a
450     proof environment */
451 static int getEndLabel(pit_type p, Text const & text)
452 {
453         ParagraphList const & pars = text.paragraphs();
454         pit_type pit = p;
455         depth_type par_depth = pars[p].getDepth();
456         while (pit != pit_type(pars.size())) {
457                 Layout const & layout = pars[pit].layout();
458                 int const endlabeltype = layout.endlabeltype;
459
460                 if (endlabeltype != END_LABEL_NO_LABEL) {
461                         if (p + 1 == pit_type(pars.size()))
462                                 return endlabeltype;
463
464                         depth_type const next_depth =
465                                 pars[p + 1].getDepth();
466                         if (par_depth > next_depth ||
467                             (par_depth == next_depth && layout != pars[p + 1].layout()))
468                                 return endlabeltype;
469                         break;
470                 }
471                 if (par_depth == 0)
472                         break;
473                 pit = text.outerHook(pit);
474                 if (pit != pit_type(pars.size()))
475                         par_depth = pars[pit].getDepth();
476         }
477         return END_LABEL_NO_LABEL;
478 }
479
480
481 void RowPainter::paintLast() const
482 {
483         bool const is_rtl = text_.isRTL(par_);
484         int const endlabel = getEndLabel(row_.pit(), text_);
485
486         // paint imaginary end-of-paragraph character
487
488         Change const & change = par_.lookupChange(par_.size());
489         if (change.changed()) {
490                 FontMetrics const & fm =
491                         theFontMetrics(pi_.base.bv->buffer().params().getFont());
492                 int const length = fm.maxAscent() / 2;
493                 Color col = change.color();
494
495                 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
496                            Painter::line_solid, 3);
497
498                 if (change.deleted()) {
499                         pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
500                                 yo_ + 2, col, Painter::line_solid, 3);
501                 } else {
502                         pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
503                                 yo_ + 2, col, Painter::line_solid, 3);
504                 }
505         }
506
507         // draw an endlabel
508
509         switch (endlabel) {
510         case END_LABEL_BOX:
511         case END_LABEL_FILLED_BOX: {
512                 FontInfo const font = labelFont();
513                 FontMetrics const & fm = theFontMetrics(font);
514                 int const size = int(0.75 * fm.maxAscent());
515                 int const y = yo_ - size;
516
517                 // If needed, move the box a bit to avoid overlapping with text.
518                 int x = 0;
519                 if (is_rtl) {
520                         int const normal_x = nestMargin() + changebarMargin();
521                         x = min(normal_x, row_.left_margin - size - Inset::TEXT_TO_INSET_OFFSET);
522                 } else {
523                         int const normal_x = width_ - row_.right_margin
524                                 - size - Inset::TEXT_TO_INSET_OFFSET;
525                         x = max(normal_x, row_.width());
526                 }
527
528                 if (endlabel == END_LABEL_BOX)
529                         pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
530                 else
531                         pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
532                 break;
533         }
534
535         case END_LABEL_STATIC: {
536                 FontInfo const font = labelFont();
537                 FontMetrics const & fm = theFontMetrics(font);
538                 docstring const & str = par_.layout().endlabelstring();
539                 double const x = is_rtl ? x_ - fm.width(str) : x_;
540                 pi_.pain.text(int(x), yo_, str, font);
541                 break;
542         }
543
544         case END_LABEL_NO_LABEL:
545                 break;
546         }
547 }
548
549
550 void RowPainter::paintOnlyInsets()
551 {
552         Row::const_iterator cit = row_.begin();
553         Row::const_iterator const & end = row_.end();
554         for ( ; cit != end ; ++cit) {
555                 Row::Element const & e = *cit;
556                 if (e.type == Row::INSET) {
557                         paintInset(e);
558                         // The line that indicates word in a different language
559                         paintForeignMark(e);
560                         // change tracking (not for insets that handle it themselves)
561                         if (!e.inset->canPaintChange(*pi_.base.bv))
562                                 paintChange(e);
563                 }
564
565                 x_ += e.full_width();
566         }
567 }
568
569
570 void RowPainter::paintText()
571 {
572         Row::const_iterator cit = row_.begin();
573         Row::const_iterator const & end = row_.end();
574         for ( ; cit != end ; ++cit) {
575                 Row::Element const & e = *cit;
576
577                 switch (e.type) {
578                 case Row::STRING:
579                 case Row::VIRTUAL:
580                         paintStringAndSel(e);
581
582                         // Paint the spelling marks if enabled.
583                         if (lyxrc.spellcheck_continuously && pi_.do_spellcheck && pi_.pain.isDrawingEnabled())
584                                 paintMisspelledMark(e);
585                         break;
586
587                 case Row::INSET:
588                         paintInset(e);
589                         break;
590
591                 case Row::SPACE:
592                         pi_.pain.textDecoration(e.font.fontInfo(), int(x_), yo_, int(e.full_width()));
593                 }
594
595                 // The line that indicates word in a different language
596                 paintForeignMark(e);
597
598                 // change tracking (not for insets that handle it themselves)
599                 if (e.type != Row::INSET || ! e.inset->canPaintChange(*pi_.base.bv))
600                         paintChange(e);
601
602                 x_ += e.full_width();
603         }
604 }
605
606
607 void RowPainter::paintSelection() const
608 {
609         if (!row_.selection())
610                 return;
611         Cursor const & curs = pi_.base.bv->cursor();
612         DocIterator beg = curs.selectionBegin();
613         beg.pit() = row_.pit();
614         beg.pos() = row_.sel_beg;
615
616         DocIterator end = curs.selectionEnd();
617         end.pit() = row_.pit();
618         end.pos() = row_.sel_end;
619
620         bool const begin_boundary = beg.pos() >= row_.endpos();
621         bool const end_boundary = row_.sel_end == row_.endpos();
622
623         DocIterator cur = beg;
624         cur.boundary(begin_boundary);
625         int x1 = text_metrics_.cursorX(beg.top(), begin_boundary);
626         int x2 = text_metrics_.cursorX(end.top(), end_boundary);
627         int const y1 = yo_ - row_.ascent();
628         int const y2 = y1 + row_.height();
629
630         int const rm = text_.isMainText() ? pi_.base.bv->rightMargin() : 0;
631         int const lm = text_.isMainText() ? pi_.base.bv->leftMargin() : 0;
632
633         // draw the margins
634         if (row_.begin_margin_sel) {
635                 if (text_.isRTL(beg.paragraph())) {
636                         pi_.pain.fillRectangle(int(xo_ + x1), y1,
637                                 text_metrics_.width() - rm - x1, y2 - y1, Color_selection);
638                 } else {
639                         pi_.pain.fillRectangle(int(xo_ + lm), y1, x1 - lm, y2 - y1,
640                                 Color_selection);
641                 }
642         }
643
644         if (row_.end_margin_sel) {
645                 if (text_.isRTL(beg.paragraph())) {
646                         pi_.pain.fillRectangle(int(xo_ + lm), y1, x2 - lm, y2 - y1,
647                                 Color_selection);
648                 } else {
649                         pi_.pain.fillRectangle(int(xo_ + x2), y1, text_metrics_.width() - rm - x2,
650                                 y2 - y1, Color_selection);
651                 }
652         }
653
654         // if we are on a boundary from the beginning, it's probably
655         // a RTL boundary and we jump to the other side directly as this
656         // segement is 0-size and confuses the logic below
657         if (cur.boundary())
658                 cur.boundary(false);
659
660         // go through row and draw from RTL boundary to RTL boundary
661         while (cur < end) {
662                 bool draw_now = false;
663
664                 // simplified cursorForward code below which does not
665                 // descend into insets and which does not go into the
666                 // next line. Compare the logic with the original cursorForward
667
668                 // if left of boundary -> just jump to right side, but
669                 // for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
670                 if (cur.boundary()) {
671                         cur.boundary(false);
672                 }       else if (text_metrics_.isRTLBoundary(cur.pit(), cur.pos() + 1)) {
673                         // in front of RTL boundary -> Stay on this side of the boundary
674                         // because:  ab|cDDEEFFghi -> abc|DDEEFFghi
675                         ++cur.pos();
676                         cur.boundary(true);
677                         draw_now = true;
678                 } else {
679                         // move right
680                         ++cur.pos();
681
682                         // line end?
683                         if (cur.pos() == row_.endpos())
684                                 cur.boundary(true);
685                 }
686
687                 if (x1 == -1) {
688                         // the previous segment was just drawn, now the next starts
689                         x1 = text_metrics_.cursorX(cur.top(), cur.boundary());
690                 }
691
692                 if (!(cur < end) || draw_now) {
693                         x2 = text_metrics_.cursorX(cur.top(), cur.boundary());
694                         pi_.pain.fillRectangle(int(xo_ + min(x1, x2)), y1, abs(x2 - x1),
695                                 y2 - y1, Color_selection);
696
697                         // reset x1, so it is set again next round (which will be on the
698                         // right side of a boundary or at the selection end)
699                         x1 = -1;
700                 }
701         }
702 }
703
704
705 } // namespace lyx