]> git.lyx.org Git - lyx.git/blob - src/RowPainter.cpp
946901280ad6c239c9810eba793a47b887c2a841
[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::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_.right_margin + fm.width(layout.labelsep);
465         else
466                 x = x_ - fm.width(layout.labelsep) - fm.width(str);
467
468         pi_.pain.text(int(x), yo_, str, font);
469 }
470
471
472 void RowPainter::paintTopLevelLabel() const
473 {
474         BufferParams const & bparams = pi_.base.bv->buffer().params();
475         bool const is_rtl = text_.isRTL(par_);
476         ParagraphParameters const & pparams = par_.params();
477         Layout const & layout = par_.layout();
478         FontInfo const font = labelFont();
479         docstring const str = par_.labelString();
480         if (str.empty())
481                 return;
482
483         double spacing_val = 1.0;
484         if (!pparams.spacing().isDefault())
485                 spacing_val = pparams.spacing().getValue();
486         else
487                 spacing_val = bparams.spacing().getValue();
488
489         FontMetrics const & fm = theFontMetrics(font);
490
491         int const labeladdon = int(fm.maxHeight()
492                 * layout.spacing.getValue() * spacing_val);
493
494         int maxdesc =
495                 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
496                 + (layout.labelbottomsep * defaultRowHeight()));
497
498         double x = x_;
499         if (layout.labeltype == LABEL_CENTERED) {
500                 x = row_.left_margin + (width_ - row_.left_margin - row_.right_margin) / 2;
501                 x -= fm.width(str) / 2;
502         } else if (is_rtl) {
503                 x = width_ - row_.right_margin - fm.width(str);
504         }
505         pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
506 }
507
508
509 /** Check if the current paragraph is the last paragraph in a
510     proof environment */
511 static int getEndLabel(pit_type p, Text const & text)
512 {
513         ParagraphList const & pars = text.paragraphs();
514         pit_type pit = p;
515         depth_type par_depth = pars[p].getDepth();
516         while (pit != pit_type(pars.size())) {
517                 Layout const & layout = pars[pit].layout();
518                 int const endlabeltype = layout.endlabeltype;
519
520                 if (endlabeltype != END_LABEL_NO_LABEL) {
521                         if (p + 1 == pit_type(pars.size()))
522                                 return endlabeltype;
523
524                         depth_type const next_depth =
525                                 pars[p + 1].getDepth();
526                         if (par_depth > next_depth ||
527                             (par_depth == next_depth && layout != pars[p + 1].layout()))
528                                 return endlabeltype;
529                         break;
530                 }
531                 if (par_depth == 0)
532                         break;
533                 pit = text.outerHook(pit);
534                 if (pit != pit_type(pars.size()))
535                         par_depth = pars[pit].getDepth();
536         }
537         return END_LABEL_NO_LABEL;
538 }
539
540
541 void RowPainter::paintLast()
542 {
543         bool const is_rtl = text_.isRTL(par_);
544         int const endlabel = getEndLabel(pit_, text_);
545
546         // paint imaginary end-of-paragraph character
547
548         Change const & change = par_.lookupChange(par_.size());
549         if (change.changed()) {
550                 FontMetrics const & fm =
551                         theFontMetrics(pi_.base.bv->buffer().params().getFont());
552                 int const length = fm.maxAscent() / 2;
553                 Color col = change.color();
554
555                 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
556                            Painter::line_solid, 3);
557
558                 if (change.deleted()) {
559                         pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
560                                 yo_ + 2, col, Painter::line_solid, 3);
561                 } else {
562                         pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
563                                 yo_ + 2, col, Painter::line_solid, 3);
564                 }
565         }
566
567         // draw an endlabel
568
569         switch (endlabel) {
570         case END_LABEL_BOX:
571         case END_LABEL_FILLED_BOX: {
572                 FontInfo const font = labelFont();
573                 FontMetrics const & fm = theFontMetrics(font);
574                 int const size = int(0.75 * fm.maxAscent());
575                 int const y = yo_ - size;
576                 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
577                 int x = is_rtl ? nestMargin() + changebarMargin()
578                         : max_row_width - row_.right_margin;
579
580                 // If needed, move the box a bit to avoid overlapping with text.
581                 int const rem = max_row_width - row_.width();
582                 if (rem <= 0)
583                         x += is_rtl ? rem : - rem;
584
585                 if (endlabel == END_LABEL_BOX)
586                         pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
587                 else
588                         pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
589                 break;
590         }
591
592         case END_LABEL_STATIC: {
593                 FontInfo const font = labelFont();
594                 FontMetrics const & fm = theFontMetrics(font);
595                 docstring const & str = par_.layout().endlabelstring();
596                 double const x = is_rtl ? x_ - fm.width(str) : x_;
597                 pi_.pain.text(int(x), yo_, str, font);
598                 break;
599         }
600
601         case END_LABEL_NO_LABEL:
602                 break;
603         }
604 }
605
606
607 void RowPainter::paintOnlyInsets()
608 {
609         CoordCache const & cache = pi_.base.bv->coordCache();
610         pos_type const end = row_.endpos();
611         for (pos_type pos = row_.pos(); pos != end; ++pos) {
612                 // If outer row has changed, nested insets are repaint completely.
613                 Inset const * inset = par_.getInset(pos);
614                 bool const nested_inset = inset &&
615                                 ((inset->asInsetMath() &&
616                                   !inset->asInsetMath()->asMacroTemplate())
617                                  || inset->asInsetText()
618                                  || inset->asInsetTabular());
619                 if (!nested_inset)
620                         continue;
621                 if (x_ > pi_.base.bv->workWidth()
622                     || !cache.getInsets().has(inset))
623                         continue;
624                 x_ = cache.getInsets().x(inset);
625                 Font const font = text_metrics_.displayFont(pit_, pos);
626                 paintInset(inset, font, par_.lookupChange(pos), pos);
627         }
628 }
629
630 void RowPainter::paintText()
631 {
632         Row::const_iterator cit = row_.begin();
633         Row::const_iterator const & end = row_.end();
634         for ( ; cit != end ; ++cit) {
635                 double const orig_x = x_;
636                 Row::Element const & e = *cit;
637                 int foreign_descent = 0;
638
639                 switch (e.type) {
640                 case Row::STRING:
641                 case Row::VIRTUAL:
642                         paintStringAndSel(e.str, e.font, e.change, e.pos, e.endpos);
643
644                         // Paint the spelling mark if needed.
645                         if (lyxrc.spellcheck_continuously && pi_.do_spellcheck
646                                 && par_.isMisspelled(e.pos)) {
647                                 paintMisspelledMark(orig_x, e.str, e.font, e.pos, e.change.changed());
648                         }
649                         break;
650                 case Row::INSET: {
651                         // If outer row has changed, nested insets are repaint completely.
652                         pi_.base.bv->coordCache().insets().add(e.inset, int(x_), yo_);
653                         paintInset(e.inset, e.font, e.change, e.pos);
654                         foreign_descent = e.dim.descent();
655                 }
656                         break;
657                 case Row::SEPARATOR:
658                 case Row::SPACE:
659                         paintSeparator(e.full_width(), e.font);
660                 }
661
662                 // The line that indicates word in a different language
663                 paintForeignMark(orig_x, e.font.language(), foreign_descent);
664
665                 // change tracking (not for insets that track their own changes)
666                 if (e.type != Row::INSET || ! e.inset->canTrackChanges())
667                         paintChange(orig_x, e.font, e.change);
668         }
669 }
670
671
672 void RowPainter::paintSelection() const
673 {
674         if (!row_.selection())
675                 return;
676         Cursor const & curs = pi_.base.bv->cursor();
677         DocIterator beg = curs.selectionBegin();
678         beg.pit() = pit_;
679         beg.pos() = row_.sel_beg;
680
681         DocIterator end = curs.selectionEnd();
682         end.pit() = pit_;
683         end.pos() = row_.sel_end;
684
685         bool const begin_boundary = beg.pos() >= row_.endpos();
686         bool const end_boundary = row_.sel_end == row_.endpos();
687
688         DocIterator cur = beg;
689         cur.boundary(begin_boundary);
690         int x1 = text_metrics_.cursorX(beg.top(), begin_boundary);
691         int x2 = text_metrics_.cursorX(end.top(), end_boundary);
692         int const y1 = yo_ - row_.ascent();
693         int const y2 = y1 + row_.height();
694
695         int const rm = text_.isMainText() ? pi_.base.bv->rightMargin() : 0;
696         int const lm = text_.isMainText() ? pi_.base.bv->leftMargin() : 0;
697
698         // draw the margins
699         if (row_.begin_margin_sel) {
700                 if (text_.isRTL(beg.paragraph())) {
701                         pi_.pain.fillRectangle(int(xo_ + x1), y1,
702                                 text_metrics_.width() - rm - x1, y2 - y1, Color_selection);
703                 } else {
704                         pi_.pain.fillRectangle(int(xo_ + lm), y1, x1 - lm, y2 - y1,
705                                 Color_selection);
706                 }
707         }
708
709         if (row_.end_margin_sel) {
710                 if (text_.isRTL(beg.paragraph())) {
711                         pi_.pain.fillRectangle(int(xo_ + lm), y1, x2 - lm, y2 - y1,
712                                 Color_selection);
713                 } else {
714                         pi_.pain.fillRectangle(int(xo_ + x2), y1, text_metrics_.width() - rm - x2,
715                                 y2 - y1, Color_selection);
716                 }
717         }
718
719         // if we are on a boundary from the beginning, it's probably
720         // a RTL boundary and we jump to the other side directly as this
721         // segement is 0-size and confuses the logic below
722         if (cur.boundary())
723                 cur.boundary(false);
724
725         // go through row and draw from RTL boundary to RTL boundary
726         while (cur < end) {
727                 bool draw_now = false;
728
729                 // simplified cursorForward code below which does not
730                 // descend into insets and which does not go into the
731                 // next line. Compare the logic with the original cursorForward
732
733                 // if left of boundary -> just jump to right side, but
734                 // for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
735                 if (cur.boundary()) {
736                         cur.boundary(false);
737                 }       else if (text_metrics_.isRTLBoundary(cur.pit(), cur.pos() + 1)) {
738                         // in front of RTL boundary -> Stay on this side of the boundary
739                         // because:  ab|cDDEEFFghi -> abc|DDEEFFghi
740                         ++cur.pos();
741                         cur.boundary(true);
742                         draw_now = true;
743                 } else {
744                         // move right
745                         ++cur.pos();
746
747                         // line end?
748                         if (cur.pos() == row_.endpos())
749                                 cur.boundary(true);
750                 }
751
752                 if (x1 == -1) {
753                         // the previous segment was just drawn, now the next starts
754                         x1 = text_metrics_.cursorX(cur.top(), cur.boundary());
755                 }
756
757                 if (!(cur < end) || draw_now) {
758                         x2 = text_metrics_.cursorX(cur.top(), cur.boundary());
759                         pi_.pain.fillRectangle(int(xo_ + min(x1, x2)), y1, abs(x2 - x1),
760                                 y2 - y1, Color_selection);
761
762                         // reset x1, so it is set again next round (which will be on the
763                         // right side of a boundary or at the selection end)
764                         x1 = -1;
765                 }
766         }
767 }
768
769
770 } // namespace lyx