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