]> git.lyx.org Git - lyx.git/blob - src/RowPainter.cpp
43b3be20ce67c35766183f5e647cd2fffc1ec625
[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 "ParagraphParameters.h"
30 #include "TextMetrics.h"
31 #include "VSpace.h"
32
33 #include "frontends/FontMetrics.h"
34 #include "frontends/Painter.h"
35
36 #include "insets/InsetText.h"
37
38 #include "mathed/InsetMath.h"
39
40 #include "support/debug.h"
41 #include "support/gettext.h"
42 #include "support/textutils.h"
43
44 #include "support/lassert.h"
45 #include <boost/crc.hpp>
46
47 #include <stdlib.h>
48
49 using namespace std;
50
51 namespace lyx {
52
53 using frontend::Painter;
54 using frontend::FontMetrics;
55
56
57 RowPainter::RowPainter(PainterInfo & pi,
58         Text const & text, Row const & row, int x, int y)
59         : pi_(pi), text_(text),
60           tm_(pi_.base.bv->textMetrics(&text)),
61           pars_(text.paragraphs()),
62           row_(row), par_(text.paragraphs()[row.pit()]),
63           change_(pi_.change_),
64           xo_(x), yo_(y)
65 {
66         x_ = row_.left_margin + xo_;
67
68         //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
69         //row_.dump();
70
71         LBUFERR(row.pit() >= 0);
72         LBUFERR(row.pit() < int(text.paragraphs().size()));
73 }
74
75
76 FontInfo RowPainter::labelFont(bool end) const
77 {
78         FontInfo f = text_.labelFont(par_);
79         // selected text?
80         if ((end ? row_.end_margin_sel : row_.begin_margin_sel)
81             || 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::paintLanguageMarkings(Row::Element const & e) const
143 {
144         paintForeignMark(e);
145         paintNoSpellingMark(e);
146 }
147
148
149 void RowPainter::paintForeignMark(Row::Element const & e) const
150 {
151         Language const * lang = e.font.language();
152         if (!lyxrc.mark_foreign_language)
153                 return;
154         if (lang == latex_language)
155                 return;
156         if (lang == pi_.base.bv->buffer().params().language)
157                 return;
158
159         int const desc = e.inset ? e.dim.descent() : 0;
160         int const y = yo_ + min(3 * pi_.base.solidLineOffset() / 2 + desc,
161                                 row_.descent() - 1);
162         pi_.pain.line(int(x_), y, int(x_ + e.full_width() - 1), y, Color_language,
163                       Painter::line_solid, pi_.base.solidLineThickness());
164 }
165
166
167 void RowPainter::paintNoSpellingMark(Row::Element const & e) const
168 {
169         //if (!lyxrc.mark_no_spelling)
170         //      return;
171         if (e.font.language() == latex_language)
172                 return;
173         if (e.font.fontInfo().nospellcheck() != FONT_ON)
174                 return;
175
176         // We at the same voffset than the misspelled mark, since
177         // these two are mutually exclusive
178         int const desc = e.inset ? e.dim.descent() : 0;
179         int const y = yo_ + pi_.base.solidLineOffset() + desc
180                 + pi_.base.solidLineThickness()
181                 + (e.change.changed() ? pi_.base.solidLineThickness() + 1 : 0)
182                 + 1;
183         pi_.pain.line(int(x_), y, int(x_ + e.full_width()), y, Color_language,
184                       Painter::line_onoffdash, pi_.base.solidLineThickness());
185 }
186
187
188 void RowPainter::paintMisspelledMark(Row::Element const & e) const
189 {
190         if (e.font.fontInfo().nospellcheck() == FONT_ON)
191                 return;
192         // if changed the misspelled marker gets placed slightly lower than normal
193         // to avoid drawing at the same vertical offset
194         FontMetrics const & fm = theFontMetrics(e.font);
195         int const thickness = max(fm.lineWidth(), 2);
196         int const y = yo_ + pi_.base.solidLineOffset() + pi_.base.solidLineThickness()
197                 + (e.change.changed() ? pi_.base.solidLineThickness() + 1 : 0)
198                 + 1 + thickness / 2;
199
200         //FIXME: this could be computed only once, it is probably not costly.
201         // check for cursor position
202         // don't draw misspelled marker for words at cursor position
203         // we don't want to disturb the process of text editing
204         DocIterator const nw = pi_.base.bv->cursor().newWord();
205         pos_type cpos = -1;
206         if (!nw.empty() && par_.id() == nw.paragraph().id()) {
207                 cpos = nw.pos();
208                 if (cpos > 0 && cpos == par_.size() && !par_.isWordSeparator(cpos-1))
209                         --cpos;
210                 else if (cpos > 0 && par_.isWordSeparator(cpos))
211                         --cpos;
212         }
213
214         pos_type pos = e.pos;
215         while (pos < e.pos + pos_type(e.str.length())) {
216                 if (!par_.isMisspelled(pos)) {
217                         ++pos;
218                         continue;
219                 }
220
221                 FontSpan const & range = par_.getSpellRange(pos);
222
223                 // Skip element which are being edited
224                 if (range.contains(cpos)) {
225                         // the range includes the last element
226                         pos = range.last + 1;
227                         continue;
228                 }
229
230                 int x1 = fm.pos2x(e.str, range.first - e.pos,
231                                   e.isRTL(), e.extra);
232                 int x2 = fm.pos2x(e.str, min(range.last - e.pos + 1,
233                                                                          pos_type(e.str.length())),
234                                                                          e.isRTL(), e.extra);
235                 if (x1 > x2)
236                         swap(x1, x2);
237
238                 pi_.pain.line(int(x_ + x1), y, int(x_ + x2), y,
239                               Color_error,
240                               Painter::line_onoffdash, thickness);
241                 pos = range.last + 1;
242         }
243 }
244
245
246 void RowPainter::paintStringAndSel(Row::Element const & e) const
247 {
248         // at least part of text selected?
249         bool const some_sel = (e.endpos >= row_.sel_beg && e.pos < row_.sel_end)
250                 || pi_.selected;
251         // all the text selected?
252         bool const all_sel = (e.pos >= row_.sel_beg && e.endpos < row_.sel_end)
253                 || pi_.selected;
254
255         if (all_sel || e.change.changed()) {
256                 Font copy = e.font;
257                 Color const col = e.change.changed() ? e.change.color()
258                                                      : Color_selectiontext;
259                 copy.fontInfo().setPaintColor(col);
260                 pi_.pain.text(int(x_), yo_, e.str, copy, e.extra, e.full_width());
261         } else if (!some_sel) {
262                 pi_.pain.text(int(x_), yo_, e.str, e.font, e.extra, e.full_width());
263         } else {
264                 pi_.pain.text(int(x_), yo_, e.str, e.font, Color_selectiontext,
265                               max(row_.sel_beg, e.pos) - e.pos,
266                               min(row_.sel_end, e.endpos) - e.pos,
267                               e.extra, e.full_width());
268         }
269 }
270
271
272 void RowPainter::paintTextDecoration(Row::Element const & e) const
273 {
274         // element selected?
275         bool const sel = (e.pos >= row_.sel_beg && e.endpos <= row_.sel_end)
276                 || pi_.selected;
277         FontInfo copy = e.font.fontInfo();
278         if (sel || e.change.changed()) {
279                 Color const col = e.change.changed() ? e.change.color()
280                                                      : Color_selectiontext;
281                 copy.setPaintColor(col);
282         }
283         pi_.pain.textDecoration(copy, int(x_), yo_, int(e.full_width()));
284 }
285
286
287 void RowPainter::paintChange(Row::Element const & e) const
288 {
289         e.change.paintCue(pi_, x_, yo_, x_ + e.full_width(), e.font.fontInfo());
290 }
291
292
293 void RowPainter::paintChangeBar() const
294 {
295         int const height = tm_.isLastRow(row_)
296                 ? row_.ascent()
297                 : row_.height();
298
299         pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
300 }
301
302
303 void RowPainter::paintAppendix() const
304 {
305         // only draw the appendix frame once (for the main text)
306         if (!par_.params().appendix() || !text_.isMainText())
307                 return;
308
309         int y = yo_ - row_.ascent();
310
311         if (par_.params().startOfAppendix())
312                 y += 2 * defaultRowHeight();
313
314         pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
315         pi_.pain.line(tm_.width() - 2, y, tm_.width() - 2, yo_ + row_.height(), Color_appendix);
316 }
317
318
319 void RowPainter::paintDepthBar() const
320 {
321         depth_type const depth = par_.getDepth();
322
323         if (depth <= 0)
324                 return;
325
326         depth_type prev_depth = 0;
327         if (!tm_.isFirstRow(row_)) {
328                 pit_type pit2 = row_.pit();
329                 if (row_.pos() == 0)
330                         --pit2;
331                 prev_depth = pars_[pit2].getDepth();
332         }
333
334         depth_type next_depth = 0;
335         if (!tm_.isLastRow(row_)) {
336                 pit_type pit2 = row_.pit();
337                 if (row_.endpos() >= pars_[pit2].size())
338                         ++pit2;
339                 next_depth = pars_[pit2].getDepth();
340         }
341
342         for (depth_type i = 1; i <= depth; ++i) {
343                 int const w = nestMargin() / 5;
344                 int x = int(xo_) + w * i;
345                 // consider the bufferview left margin if we're drawing outermost text
346                 if (text_.isMainText())
347                         x += pi_.base.bv->leftMargin();
348
349                 int const starty = yo_ - row_.ascent();
350                 int const h =  row_.height() - 1 - (i - next_depth - 1) * 3;
351
352                 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
353
354                 if (i > prev_depth)
355                         pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
356                 if (i > next_depth)
357                         pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
358         }
359 }
360
361
362 void RowPainter::paintAppendixStart(int y) const
363 {
364         FontInfo pb_font = sane_font;
365         pb_font.setColor(Color_appendix);
366         pb_font.decSize();
367
368         int w = 0;
369         int a = 0;
370         int d = 0;
371
372         docstring const label = _("Appendix");
373         theFontMetrics(pb_font).rectText(label, w, a, d);
374
375         int const text_start = int(xo_ + (tm_.width() - w) / 2);
376         int const text_end = text_start + w;
377
378         pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
379
380         pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
381         pi_.pain.line(text_end, y, int(xo_ + tm_.width() - 2), y, Color_appendix);
382 }
383
384
385 void RowPainter::paintTooLargeMarks(bool const left, bool const right) const
386 {
387         if (left)
388                 pi_.pain.line(pi_.base.dottedLineThickness(), yo_ - row_.ascent(),
389                                           pi_.base.dottedLineThickness(), yo_ + row_.descent(),
390                                           Color_scroll, Painter::line_onoffdash,
391                               pi_.base.dottedLineThickness());
392         if (right) {
393                 int const wwidth =
394                         pi_.base.bv->workWidth() - pi_.base.dottedLineThickness();
395                 pi_.pain.line(wwidth, yo_ - row_.ascent(),
396                                           wwidth, yo_ + row_.descent(),
397                                           Color_scroll, Painter::line_onoffdash,
398                               pi_.base.dottedLineThickness());
399         }
400 }
401
402
403 void RowPainter::paintFirst() const
404 {
405         Layout const & layout = par_.layout();
406
407         // start of appendix?
408         if (par_.params().startOfAppendix())
409             paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
410
411         bool const is_first =
412                 text_.isFirstInSequence(row_.pit()) || !layout.isParagraphGroup();
413         //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
414
415         if (layout.labelIsInline()
416             && (layout.labeltype != LABEL_STATIC || is_first))
417                 paintLabel();
418         else if (is_first && layout.labelIsAbove())
419                 paintTopLevelLabel();
420 }
421
422
423 void RowPainter::paintLabel() const
424 {
425         docstring const & str = par_.labelString();
426         if (str.empty())
427                 return;
428
429         Layout const & layout = par_.layout();
430         FontInfo const font = labelFont(false);
431         FontMetrics const & fm = theFontMetrics(font);
432         double x = x_;
433
434         if (row_.isRTL())
435                 x = tm_.width() - row_.right_margin + fm.width(layout.labelsep);
436         else
437                 x = x_ - fm.width(layout.labelsep) - fm.width(str);
438
439         pi_.pain.text(int(x), yo_, str, font);
440 }
441
442
443 void RowPainter::paintTopLevelLabel() const
444 {
445         BufferParams const & bparams = pi_.base.bv->buffer().params();
446         ParagraphParameters const & pparams = par_.params();
447         Layout const & layout = par_.layout();
448         FontInfo const font = labelFont(false);
449         docstring const str = par_.labelString();
450         if (str.empty())
451                 return;
452
453         double spacing_val = 1.0;
454         if (!pparams.spacing().isDefault())
455                 spacing_val = pparams.spacing().getValue();
456         else
457                 spacing_val = bparams.spacing().getValue();
458
459         FontMetrics const & fm = theFontMetrics(font);
460
461         int const labeladdon = int(fm.maxHeight()
462                 * layout.spacing.getValue() * spacing_val);
463
464         int maxdesc =
465                 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
466                 + (layout.labelbottomsep * defaultRowHeight()));
467
468         double x = x_;
469         if (layout.labeltype == LABEL_CENTERED) {
470                 x = row_.left_margin + (tm_.width() - row_.left_margin - row_.right_margin) / 2;
471                 x -= fm.width(str) / 2;
472         } else if (row_.isRTL()) {
473                 x = tm_.width() - row_.right_margin - fm.width(str);
474         }
475         pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
476 }
477
478
479 /** Check if the current paragraph is the last paragraph in a
480     proof environment */
481 static int getEndLabel(pit_type p, Text const & text)
482 {
483         ParagraphList const & pars = text.paragraphs();
484         pit_type pit = p;
485         depth_type par_depth = pars[p].getDepth();
486         while (pit != pit_type(pars.size())) {
487                 Layout const & layout = pars[pit].layout();
488                 int const endlabeltype = layout.endlabeltype;
489
490                 if (endlabeltype != END_LABEL_NO_LABEL) {
491                         if (p + 1 == pit_type(pars.size()))
492                                 return endlabeltype;
493
494                         depth_type const next_depth =
495                                 pars[p + 1].getDepth();
496                         if (par_depth > next_depth ||
497                             (par_depth == next_depth && layout != pars[p + 1].layout()))
498                                 return endlabeltype;
499                         break;
500                 }
501                 if (par_depth == 0)
502                         break;
503                 pit = text.outerHook(pit);
504                 if (pit != pit_type(pars.size()))
505                         par_depth = pars[pit].getDepth();
506         }
507         return END_LABEL_NO_LABEL;
508 }
509
510
511 void RowPainter::paintLast() const
512 {
513         int const endlabel = getEndLabel(row_.pit(), text_);
514
515         // paint imaginary end-of-paragraph character
516
517         Change const & change = par_.lookupChange(par_.size());
518         if (change.changed()) {
519                 FontMetrics const & fm =
520                         theFontMetrics(pi_.base.bv->buffer().params().getFont());
521                 int const length = fm.maxAscent() / 2;
522                 Color col = change.color();
523
524                 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
525                            Painter::line_solid, 3);
526
527                 if (change.deleted()) {
528                         pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
529                                 yo_ + 2, col, Painter::line_solid, 3);
530                 } else {
531                         pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
532                                 yo_ + 2, col, Painter::line_solid, 3);
533                 }
534         }
535
536         // draw an endlabel
537
538         switch (endlabel) {
539         case END_LABEL_BOX:
540         case END_LABEL_FILLED_BOX: {
541                 FontInfo font = labelFont(true);
542                 if (font.realColor() != Color_selectiontext)
543                         font.setPaintColor(Color_eolmarker);
544                 FontMetrics const & fm = theFontMetrics(font);
545                 int const size = int(0.75 * fm.maxAscent());
546                 int const y = yo_ - size;
547
548                 // If needed, move the box a bit to avoid overlapping with text.
549                 int x = 0;
550                 if (row_.isRTL()) {
551                         int const normal_x = nestMargin() + changebarMargin();
552                         x = min(normal_x, row_.left_margin - size - Inset::TEXT_TO_INSET_OFFSET);
553                 } else {
554                         int const normal_x = tm_.width() - row_.right_margin
555                                 - size - Inset::TEXT_TO_INSET_OFFSET;
556                         x = max(normal_x, row_.width());
557                 }
558
559                 if (endlabel == END_LABEL_BOX)
560                         pi_.pain.rectangle(x, y, size, size, font.realColor());
561                 else
562                         pi_.pain.fillRectangle(x, y, size, size, font.realColor());
563                 break;
564         }
565
566         case END_LABEL_STATIC: {
567                 FontInfo const font = labelFont(true);
568                 FontMetrics const & fm = theFontMetrics(font);
569                 docstring const & str = par_.layout().endlabelstring();
570                 double const x = row_.isRTL() ? x_ - fm.width(str) : x_;
571                 pi_.pain.text(int(x), yo_, str, font);
572                 break;
573         }
574
575         case END_LABEL_NO_LABEL:
576                 break;
577         }
578 }
579
580
581 void RowPainter::paintOnlyInsets()
582 {
583         Row::const_iterator cit = row_.begin();
584         Row::const_iterator const & end = row_.end();
585         for ( ; cit != end ; ++cit) {
586                 Row::Element const & e = *cit;
587                 if (e.type == Row::INSET) {
588                         paintInset(e);
589                         // The markings of foreign languages
590                         // and of text ignored for spellchecking
591                         paintLanguageMarkings(e);
592                         // change tracking (not for insets that handle it themselves)
593                         if (!e.inset->canPaintChange(*pi_.base.bv))
594                                 paintChange(e);
595                 }
596
597                 x_ += e.full_width();
598         }
599 }
600
601
602 void RowPainter::paintText()
603 {
604         Row::const_iterator cit = row_.begin();
605         Row::const_iterator const & end = row_.end();
606         for ( ; cit != end ; ++cit) {
607                 Row::Element const & e = *cit;
608
609                 switch (e.type) {
610                 case Row::STRING:
611                 case Row::VIRTUAL:
612                         paintStringAndSel(e);
613
614                         // Paint the spelling marks if enabled.
615                         if (lyxrc.spellcheck_continuously && pi_.do_spellcheck && !pi_.pain.isNull())
616                                 paintMisspelledMark(e);
617                         break;
618
619                 case Row::INSET:
620                         paintInset(e);
621                         paintTextDecoration(e);
622                         break;
623
624                 case Row::SPACE:
625                         paintTextDecoration(e);
626                 }
627
628                 // The markings of foreign languages
629                 // and of text ignored for spellchecking
630                 paintLanguageMarkings(e);
631
632                 // change tracking (not for insets that handle it themselves)
633                 if (e.type != Row::INSET || ! e.inset->canPaintChange(*pi_.base.bv))
634                         paintChange(e);
635
636                 x_ += e.full_width();
637         }
638 }
639
640
641 void RowPainter::paintSelection() const
642 {
643         if (!row_.selection())
644                 return;
645
646         int const y1 = yo_ - row_.ascent();
647         int const y2 = y1 + row_.height();
648
649         // draw the margins
650         if (row_.isRTL() ? row_.end_margin_sel : row_.begin_margin_sel)
651                 pi_.pain.fillRectangle(int(xo_), y1, row_.left_margin, y2 - y1,
652                                        Color_selection);
653
654         // go through row and draw from RTL boundary to RTL boundary
655         double x = xo_ + row_.left_margin;
656         for (auto const & e : row_) {
657                 // These are the same tests as in paintStringAndSel, except
658                 // that all_sel has an additional clause that triggers for end
659                 // of paragraph markers. The clause was not used in
660                 // paintStringAndSel to avoid changing the drawing color.
661                 // at least part of text selected?
662                 bool const some_sel = (e.endpos >= row_.sel_beg && e.pos < row_.sel_end)
663                         || pi_.selected;
664                 // all the text selected?
665                 bool const all_sel = (e.pos >= row_.sel_beg && e.endpos < row_.sel_end)
666                     || (e.isVirtual() && e.pos == row_.endpos() && row_.end_margin_sel)
667                     || pi_.selected;
668
669                 if (all_sel) {
670                         // the 3rd argument is written like that to avoid rounding issues
671                         pi_.pain.fillRectangle(int(x), y1,
672                                                int(x + e.full_width()) - int(x), y2 - y1,
673                                                Color_selection);
674                 } else if (some_sel) {
675                         pos_type const from = min(max(row_.sel_beg, e.pos), e.endpos);
676                         pos_type const to = max(min(row_.sel_end, e.endpos), e.pos);
677                         double x1 = e.pos2x(from);
678                         double x2 = e.pos2x(to);
679                         if (x1 > x2)
680                                 swap(x1, x2);
681                         // the 3rd argument is written like that to avoid rounding issues
682                         pi_.pain.fillRectangle(int(x + x1), y1, int(x2 + x) - int(x1 + x),
683                                                y2 - y1, Color_selection);
684                 }
685                 x += e.full_width();
686         }
687
688         if (row_.isRTL() ? row_.begin_margin_sel : row_.end_margin_sel)
689                 pi_.pain.fillRectangle(int(x), y1,
690                                        int(xo_ + tm_.width()) - int(x), y2 - y1,
691                                        Color_selection);
692
693 }
694
695
696 } // namespace lyx