]> git.lyx.org Git - lyx.git/blob - src/RowPainter.cpp
81b58a869196347de68131440b010de0efeac193
[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), 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::paintForeignMark(double orig_x, Language const * lang, int desc) const
167 {
168         if (!lyxrc.mark_foreign_language)
169                 return;
170         if (lang == latex_language)
171                 return;
172         if (lang == pi_.base.bv->buffer().params().language)
173                 return;
174
175         int const y = yo_ + solid_line_offset_ + desc + solid_line_thickness_ / 2;
176         pi_.pain.line(int(orig_x), y, int(x_), y, Color_language,
177                 Painter::line_solid, solid_line_thickness_);
178 }
179
180
181 void RowPainter::paintMisspelledMark(double const orig_x,
182                                      Row::Element const & e) const
183 {
184         // if changed the misspelled marker gets placed slightly lower than normal
185         // to avoid drawing at the same vertical offset
186         int const y = yo_ + solid_line_offset_ + solid_line_thickness_
187                 + (e.change.changed() ? solid_line_thickness_ + 1 : 0)
188                 + dotted_line_offset_;
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, dotted_line_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) {
247                 Font copy = e.font;
248                 copy.fontInfo().setPaintColor(Color_selectiontext);
249                 pi_.pain.text(int(x_), yo_, e.str, copy, e.extra);
250         } else if (e.change.changed()) {
251                 Font copy = e.font;
252                 copy.fontInfo().setPaintColor(e.change.color());
253                 pi_.pain.text(int(x_), yo_, e.str, copy, e.extra);
254         } else if (!some_sel) {
255                 pi_.pain.text(int(x_), yo_, e.str, e.font, e.extra);
256         } else {
257                 pi_.pain.text(int(x_), yo_, e.str, e.font, Color_selectiontext,
258                                     max(row_.sel_beg, e.pos) - e.pos,
259                         min(row_.sel_end, e.endpos) - e.pos, e.extra);
260         }
261         x_ += e.full_width();
262 }
263
264
265 void RowPainter::paintChange(double orig_x, Font const & font,
266                              Change const & change) const
267 {
268         if (!change.changed())
269                 return;
270         // Calculate 1/3 height of font
271         FontMetrics const & fm = theFontMetrics(font);
272         int const y_bar = change.deleted() ? yo_ - fm.maxAscent() / 3
273                 : yo_ + 2 * solid_line_offset_ + solid_line_thickness_;
274         pi_.pain.line(int(orig_x), y_bar, int(x_), y_bar,
275                       change.color(), Painter::line_solid, solid_line_thickness_);
276 }
277
278
279 void RowPainter::paintChangeBar() const
280 {
281         pos_type const start = row_.pos();
282         pos_type end = row_.endpos();
283
284         if (par_.size() == end) {
285                 // this is the last row of the paragraph;
286                 // thus, we must also consider the imaginary end-of-par character
287                 end++;
288         }
289
290         if (start == end || !par_.isChanged(start, end))
291                 return;
292
293         int const height = text_metrics_.isLastRow(pit_, row_)
294                 ? row_.ascent()
295                 : row_.height();
296
297         pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
298 }
299
300
301 void RowPainter::paintAppendix() const
302 {
303         // only draw the appendix frame once (for the main text)
304         if (!par_.params().appendix() || !text_.isMainText())
305                 return;
306
307         int y = yo_ - row_.ascent();
308
309         if (par_.params().startOfAppendix())
310                 y += 2 * defaultRowHeight();
311
312         pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
313         pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
314 }
315
316
317 void RowPainter::paintDepthBar() const
318 {
319         depth_type const depth = par_.getDepth();
320
321         if (depth <= 0)
322                 return;
323
324         depth_type prev_depth = 0;
325         if (!text_metrics_.isFirstRow(pit_, row_)) {
326                 pit_type pit2 = pit_;
327                 if (row_.pos() == 0)
328                         --pit2;
329                 prev_depth = pars_[pit2].getDepth();
330         }
331
332         depth_type next_depth = 0;
333         if (!text_metrics_.isLastRow(pit_, row_)) {
334                 pit_type pit2 = pit_;
335                 if (row_.endpos() >= pars_[pit2].size())
336                         ++pit2;
337                 next_depth = pars_[pit2].getDepth();
338         }
339
340         for (depth_type i = 1; i <= depth; ++i) {
341                 int const w = nestMargin() / 5;
342                 int x = int(xo_) + w * i;
343                 // only consider the changebar space if we're drawing outermost text
344                 if (text_.isMainText())
345                         x += changebarMargin();
346
347                 int const starty = yo_ - row_.ascent();
348                 int const h =  row_.height() - 1 - (i - next_depth - 1) * 3;
349
350                 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
351
352                 if (i > prev_depth)
353                         pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
354                 if (i > next_depth)
355                         pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
356         }
357 }
358
359
360 int RowPainter::paintAppendixStart(int y) const
361 {
362         FontInfo pb_font = sane_font;
363         pb_font.setColor(Color_appendix);
364         pb_font.decSize();
365
366         int w = 0;
367         int a = 0;
368         int d = 0;
369
370         docstring const label = _("Appendix");
371         theFontMetrics(pb_font).rectText(label, w, a, d);
372
373         int const text_start = int(xo_ + (width_ - w) / 2);
374         int const text_end = text_start + w;
375
376         pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
377
378         pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
379         pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
380
381         return 3 * defaultRowHeight();
382 }
383
384
385 void RowPainter::paintTooLargeMarks(bool const left, bool const right) const
386 {
387         if (left)
388                 pi_.pain.line(dotted_line_thickness_, yo_ - row_.ascent(),
389                                           dotted_line_thickness_, yo_ + row_.descent(),
390                                           Color_scroll,
391                                           Painter::line_onoffdash, dotted_line_thickness_);
392         if (right) {
393                 int const wwidth = pi_.base.bv->workWidth() - dotted_line_thickness_;
394                 pi_.pain.line(wwidth, yo_ - row_.ascent(),
395                                           wwidth, yo_ + row_.descent(),
396                                           Color_scroll,
397                                           Painter::line_onoffdash, dotted_line_thickness_);
398         }
399 }
400
401
402 void RowPainter::paintFirst() const
403 {
404         BufferParams const & bparams = pi_.base.bv->buffer().params();
405         Layout const & layout = par_.layout();
406
407         int y_top = 0;
408
409         // start of appendix?
410         if (par_.params().startOfAppendix())
411                 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
412
413         if (bparams.paragraph_separation == BufferParams::ParagraphSkipSeparation
414                 && pit_ != 0) {
415                 if (layout.latextype == LATEX_PARAGRAPH
416                     && !par_.getDepth()) {
417                         y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
418                 } else {
419                         Layout const & playout = pars_[pit_ - 1].layout();
420                         if (playout.latextype == LATEX_PARAGRAPH
421                             && !pars_[pit_ - 1].getDepth()) {
422                                 // is it right to use defskip here, too? (AS)
423                                 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
424                         }
425                 }
426         }
427
428         bool const is_first =
429                 text_.isFirstInSequence(pit_) || !layout.isParagraphGroup();
430         //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
431
432         if (layout.labelIsInline()
433                         && (layout.labeltype != LABEL_STATIC || is_first)) {
434                 paintLabel();
435         } else if (is_first && layout.labelIsAbove()) {
436                 paintTopLevelLabel();
437         }
438 }
439
440
441 void RowPainter::paintLabel() const
442 {
443         docstring const str = par_.labelString();
444         if (str.empty())
445                 return;
446
447         bool const is_rtl = text_.isRTL(par_);
448         Layout const & layout = par_.layout();
449         FontInfo const font = labelFont();
450         FontMetrics const & fm = theFontMetrics(font);
451         double x = x_;
452
453         if (is_rtl)
454                 x = width_ - row_.right_margin + fm.width(layout.labelsep);
455         else
456                 x = x_ - fm.width(layout.labelsep) - fm.width(str);
457
458         pi_.pain.text(int(x), yo_, str, font);
459 }
460
461
462 void RowPainter::paintTopLevelLabel() const
463 {
464         BufferParams const & bparams = pi_.base.bv->buffer().params();
465         bool const is_rtl = text_.isRTL(par_);
466         ParagraphParameters const & pparams = par_.params();
467         Layout const & layout = par_.layout();
468         FontInfo const font = labelFont();
469         docstring const str = par_.labelString();
470         if (str.empty())
471                 return;
472
473         double spacing_val = 1.0;
474         if (!pparams.spacing().isDefault())
475                 spacing_val = pparams.spacing().getValue();
476         else
477                 spacing_val = bparams.spacing().getValue();
478
479         FontMetrics const & fm = theFontMetrics(font);
480
481         int const labeladdon = int(fm.maxHeight()
482                 * layout.spacing.getValue() * spacing_val);
483
484         int maxdesc =
485                 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
486                 + (layout.labelbottomsep * defaultRowHeight()));
487
488         double x = x_;
489         if (layout.labeltype == LABEL_CENTERED) {
490                 x = row_.left_margin + (width_ - row_.left_margin - row_.right_margin) / 2;
491                 x -= fm.width(str) / 2;
492         } else if (is_rtl) {
493                 x = width_ - row_.right_margin - fm.width(str);
494         }
495         pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
496 }
497
498
499 /** Check if the current paragraph is the last paragraph in a
500     proof environment */
501 static int getEndLabel(pit_type p, Text const & text)
502 {
503         ParagraphList const & pars = text.paragraphs();
504         pit_type pit = p;
505         depth_type par_depth = pars[p].getDepth();
506         while (pit != pit_type(pars.size())) {
507                 Layout const & layout = pars[pit].layout();
508                 int const endlabeltype = layout.endlabeltype;
509
510                 if (endlabeltype != END_LABEL_NO_LABEL) {
511                         if (p + 1 == pit_type(pars.size()))
512                                 return endlabeltype;
513
514                         depth_type const next_depth =
515                                 pars[p + 1].getDepth();
516                         if (par_depth > next_depth ||
517                             (par_depth == next_depth && layout != pars[p + 1].layout()))
518                                 return endlabeltype;
519                         break;
520                 }
521                 if (par_depth == 0)
522                         break;
523                 pit = text.outerHook(pit);
524                 if (pit != pit_type(pars.size()))
525                         par_depth = pars[pit].getDepth();
526         }
527         return END_LABEL_NO_LABEL;
528 }
529
530
531 void RowPainter::paintLast()
532 {
533         bool const is_rtl = text_.isRTL(par_);
534         int const endlabel = getEndLabel(pit_, text_);
535
536         // paint imaginary end-of-paragraph character
537
538         Change const & change = par_.lookupChange(par_.size());
539         if (change.changed()) {
540                 FontMetrics const & fm =
541                         theFontMetrics(pi_.base.bv->buffer().params().getFont());
542                 int const length = fm.maxAscent() / 2;
543                 Color col = change.color();
544
545                 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
546                            Painter::line_solid, 3);
547
548                 if (change.deleted()) {
549                         pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
550                                 yo_ + 2, col, Painter::line_solid, 3);
551                 } else {
552                         pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
553                                 yo_ + 2, col, Painter::line_solid, 3);
554                 }
555         }
556
557         // draw an endlabel
558
559         switch (endlabel) {
560         case END_LABEL_BOX:
561         case END_LABEL_FILLED_BOX: {
562                 FontInfo const font = labelFont();
563                 FontMetrics const & fm = theFontMetrics(font);
564                 int const size = int(0.75 * fm.maxAscent());
565                 int const y = yo_ - size;
566                 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
567                 int x = is_rtl ? nestMargin() + changebarMargin()
568                         : max_row_width - row_.right_margin;
569
570                 // If needed, move the box a bit to avoid overlapping with text.
571                 int const rem = max_row_width - row_.width();
572                 if (rem <= 0)
573                         x += is_rtl ? rem : - rem;
574
575                 if (endlabel == END_LABEL_BOX)
576                         pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
577                 else
578                         pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
579                 break;
580         }
581
582         case END_LABEL_STATIC: {
583                 FontInfo const font = labelFont();
584                 FontMetrics const & fm = theFontMetrics(font);
585                 docstring const & str = par_.layout().endlabelstring();
586                 double const x = is_rtl ? x_ - fm.width(str) : x_;
587                 pi_.pain.text(int(x), yo_, str, font);
588                 break;
589         }
590
591         case END_LABEL_NO_LABEL:
592                 break;
593         }
594 }
595
596
597 void RowPainter::paintOnlyInsets()
598 {
599         Row::const_iterator cit = row_.begin();
600         Row::const_iterator const & end = row_.end();
601         for ( ; cit != end ; ++cit) {
602                 Row::Element const & e = *cit;
603                 if (e.type == Row::INSET) {
604                         // If outer row has changed, nested insets are repainted completely.
605                         pi_.base.bv->coordCache().insets().add(e.inset, int(x_), yo_);
606                         bool const nested_inset =
607                                 (e.inset->asInsetMath() && !e.inset->asInsetMath()->asMacroTemplate())
608                                 || e.inset->asInsetText() || e.inset->asInsetTabular();
609                         if (!nested_inset) {
610                                 x_ += e.full_width();
611                                 continue;
612                         }
613                         paintInset(e.inset, e.font, e.change, e.pos);
614                 } else
615                         x_ += e.full_width();
616         }
617 }
618
619
620 void RowPainter::paintText()
621 {
622         Row::const_iterator cit = row_.begin();
623         Row::const_iterator const & end = row_.end();
624         for ( ; cit != end ; ++cit) {
625                 double const orig_x = x_;
626                 Row::Element const & e = *cit;
627                 int foreign_descent = 0;
628
629                 switch (e.type) {
630                 case Row::STRING:
631                 case Row::VIRTUAL:
632                         paintStringAndSel(e);
633
634                         // Paint the spelling marks if enabled.
635                         if (lyxrc.spellcheck_continuously && pi_.do_spellcheck)
636                                 paintMisspelledMark(orig_x, e);
637                         break;
638                 case Row::INSET: {
639                         // If outer row has changed, nested insets are repaint completely.
640                         pi_.base.bv->coordCache().insets().add(e.inset, int(x_), yo_);
641                         paintInset(e.inset, e.font, e.change, e.pos);
642                         foreign_descent = e.dim.descent();
643                 }
644                         break;
645                 case Row::SPACE:
646                         pi_.pain.textDecoration(e.font.fontInfo(), int(x_), yo_, int(e.full_width()));
647                         x_ += e.full_width();
648                 }
649
650                 // The line that indicates word in a different language
651                 paintForeignMark(orig_x, e.font.language(), foreign_descent);
652
653                 // change tracking (not for insets that track their own changes)
654                 if (e.type != Row::INSET || ! e.inset->canTrackChanges())
655                         paintChange(orig_x, e.font, e.change);
656         }
657 }
658
659
660 void RowPainter::paintSelection() const
661 {
662         if (!row_.selection())
663                 return;
664         Cursor const & curs = pi_.base.bv->cursor();
665         DocIterator beg = curs.selectionBegin();
666         beg.pit() = pit_;
667         beg.pos() = row_.sel_beg;
668
669         DocIterator end = curs.selectionEnd();
670         end.pit() = pit_;
671         end.pos() = row_.sel_end;
672
673         bool const begin_boundary = beg.pos() >= row_.endpos();
674         bool const end_boundary = row_.sel_end == row_.endpos();
675
676         DocIterator cur = beg;
677         cur.boundary(begin_boundary);
678         int x1 = text_metrics_.cursorX(beg.top(), begin_boundary);
679         int x2 = text_metrics_.cursorX(end.top(), end_boundary);
680         int const y1 = yo_ - row_.ascent();
681         int const y2 = y1 + row_.height();
682
683         int const rm = text_.isMainText() ? pi_.base.bv->rightMargin() : 0;
684         int const lm = text_.isMainText() ? pi_.base.bv->leftMargin() : 0;
685
686         // draw the margins
687         if (row_.begin_margin_sel) {
688                 if (text_.isRTL(beg.paragraph())) {
689                         pi_.pain.fillRectangle(int(xo_ + x1), y1,
690                                 text_metrics_.width() - rm - x1, y2 - y1, Color_selection);
691                 } else {
692                         pi_.pain.fillRectangle(int(xo_ + lm), y1, x1 - lm, y2 - y1,
693                                 Color_selection);
694                 }
695         }
696
697         if (row_.end_margin_sel) {
698                 if (text_.isRTL(beg.paragraph())) {
699                         pi_.pain.fillRectangle(int(xo_ + lm), y1, x2 - lm, y2 - y1,
700                                 Color_selection);
701                 } else {
702                         pi_.pain.fillRectangle(int(xo_ + x2), y1, text_metrics_.width() - rm - x2,
703                                 y2 - y1, Color_selection);
704                 }
705         }
706
707         // if we are on a boundary from the beginning, it's probably
708         // a RTL boundary and we jump to the other side directly as this
709         // segement is 0-size and confuses the logic below
710         if (cur.boundary())
711                 cur.boundary(false);
712
713         // go through row and draw from RTL boundary to RTL boundary
714         while (cur < end) {
715                 bool draw_now = false;
716
717                 // simplified cursorForward code below which does not
718                 // descend into insets and which does not go into the
719                 // next line. Compare the logic with the original cursorForward
720
721                 // if left of boundary -> just jump to right side, but
722                 // for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
723                 if (cur.boundary()) {
724                         cur.boundary(false);
725                 }       else if (text_metrics_.isRTLBoundary(cur.pit(), cur.pos() + 1)) {
726                         // in front of RTL boundary -> Stay on this side of the boundary
727                         // because:  ab|cDDEEFFghi -> abc|DDEEFFghi
728                         ++cur.pos();
729                         cur.boundary(true);
730                         draw_now = true;
731                 } else {
732                         // move right
733                         ++cur.pos();
734
735                         // line end?
736                         if (cur.pos() == row_.endpos())
737                                 cur.boundary(true);
738                 }
739
740                 if (x1 == -1) {
741                         // the previous segment was just drawn, now the next starts
742                         x1 = text_metrics_.cursorX(cur.top(), cur.boundary());
743                 }
744
745                 if (!(cur < end) || draw_now) {
746                         x2 = text_metrics_.cursorX(cur.top(), cur.boundary());
747                         pi_.pain.fillRectangle(int(xo_ + min(x1, x2)), y1, abs(x2 - x1),
748                                 y2 - y1, Color_selection);
749
750                         // reset x1, so it is set again next round (which will be on the
751                         // right side of a boundary or at the selection end)
752                         x1 = -1;
753                 }
754         }
755 }
756
757
758 } // namespace lyx