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