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