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