3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
15 #include "RowPainter.h"
18 #include "CoordCache.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
27 #include "MetricsInfo.h"
28 #include "Paragraph.h"
29 #include "ParagraphMetrics.h"
30 #include "ParagraphParameters.h"
31 #include "TextMetrics.h"
34 #include "frontends/FontMetrics.h"
35 #include "frontends/Painter.h"
37 #include "insets/InsetText.h"
39 #include "mathed/InsetMath.h"
41 #include "support/debug.h"
42 #include "support/gettext.h"
43 #include "support/textutils.h"
45 #include "support/lassert.h"
46 #include <boost/crc.hpp>
52 using frontend::Painter;
53 using frontend::FontMetrics;
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)
67 bidi_.computeTables(par_, pi_.base.bv->buffer(), row_);
69 if (lyxrc.zoom >= 200) {
70 // derive the line thickness from zoom factor
71 // the zoom is given in percent
72 // (increase thickness at 250%, 450% etc.)
73 solid_line_thickness_ = (lyxrc.zoom + 50) / 200;
74 // adjust line_offset_ too
75 solid_line_offset_ = 1 + solid_line_thickness_ / 2;
77 if (lyxrc.zoom >= 100) {
78 // derive the line thickness from zoom factor
79 // the zoom is given in percent
80 // (increase thickness at 150%, 250% etc.)
81 dotted_line_thickness_ = (lyxrc.zoom + 50) / 100;
82 // adjust line_offset_ too
83 dotted_line_offset_ = 1 + dotted_line_thickness_ / 2;
86 x_ = row_.left_margin + xo_;
88 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
92 LBUFERR(pit < int(text.paragraphs().size()));
96 FontInfo RowPainter::labelFont() const
98 FontInfo f = text_.labelFont(par_);
100 if (row_.begin_margin_sel || pi_.selected)
101 f.setPaintColor(Color_selectiontext);
106 int RowPainter::leftMargin() const
108 return text_metrics_.leftMargin(text_metrics_.width(), pit_,
112 // If you want to debug inset metrics uncomment the following line:
113 //#define DEBUG_METRICS
114 // This draws green lines around each inset.
117 void RowPainter::paintInset(Inset const * inset, pos_type const pos)
119 Font const font = text_metrics_.displayFont(pit_, pos);
121 LASSERT(inset, return);
122 // Backup full_repaint status because some insets (InsetTabular)
123 // requires a full repaint
124 bool const pi_full_repaint = pi_.full_repaint;
125 bool const pi_do_spellcheck = pi_.do_spellcheck;
127 pi_.base.font = inset->inheritFont() ? font.fontInfo() :
128 pi_.base.bv->buffer().params().getFont().fontInfo();
129 pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
130 Change prev_change = change_;
131 pi_.change_ = change_.changed() ? change_ : par_.lookupChange(pos);
132 pi_.do_spellcheck &= inset->allowSpellCheck();
134 int const x1 = int(x_);
135 pi_.base.bv->coordCache().insets().add(inset, x1, yo_);
136 // insets are painted completely. Recursive
137 // FIXME: it is wrong to completely paint the background
138 // if we want to do single row painting.
139 inset->drawBackground(pi_, x1, yo_);
140 inset->drawSelection(pi_, x1, yo_);
141 inset->draw(pi_, x1, yo_);
143 Dimension const & dim = pm_.insetDimension(inset);
145 paintForeignMark(x_, font.language(), dim.descent());
149 // Restore full_repaint status.
150 pi_.full_repaint = pi_full_repaint;
151 pi_.change_ = prev_change;
152 pi_.do_spellcheck = pi_do_spellcheck;
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);
166 void RowPainter::paintSeparator(double orig_x, double width,
167 FontInfo const & font)
169 pi_.pain.textDecoration(font, int(orig_x), yo_, int(width));
174 void RowPainter::paintForeignMark(double orig_x, Language const * lang, int desc) const
176 if (!lyxrc.mark_foreign_language)
178 if (lang == latex_language)
180 if (lang == pi_.base.bv->buffer().params().language)
183 int const y = yo_ + solid_line_offset_ + desc + solid_line_thickness_ / 2;
184 pi_.pain.line(int(orig_x), y, int(x_), y, Color_language,
185 Painter::line_solid, solid_line_thickness_);
189 void RowPainter::paintMisspelledMark(double const orig_x,
190 docstring const & str, Font const & font,
191 pos_type const start_pos,
192 bool const changed) const
194 // if changed the misspelled marker gets placed slightly lower than normal
195 // to avoid drawing at the same vertical offset
196 int const y = yo_ + solid_line_offset_ + solid_line_thickness_
197 + (changed ? solid_line_thickness_ + 1 : 0)
198 + dotted_line_offset_;
200 //FIXME: this could be computed only once, it is probably not costly.
201 // check for cursor position
202 // don't draw misspelled marker for words at cursor position
203 // we don't want to disturb the process of text editing
204 DocIterator const nw = pi_.base.bv->cursor().newWord();
206 if (!nw.empty() && par_.id() == nw.paragraph().id()) {
208 if (cpos > 0 && cpos == par_.size() && !par_.isWordSeparator(cpos-1))
210 else if (cpos > 0 && par_.isWordSeparator(cpos))
214 pos_type pos = start_pos;
215 while (pos < start_pos + pos_type(str.length())) {
216 if (!par_.isMisspelled(pos)) {
221 FontSpan const & range = par_.getSpellRange(pos);
223 // Skip element which are being edited
224 if (range.contains(cpos)) {
225 // the range includes the last element
226 pos = range.last + 1;
230 FontMetrics const & fm = theFontMetrics(font);
231 int x1 = fm.pos2x(str, range.first - start_pos,
232 font.isVisibleRightToLeft());
233 int x2 = fm.pos2x(str, min(range.last - start_pos + 1,
234 pos_type(str.length())),
235 font.isVisibleRightToLeft());
239 pi_.pain.line(int(orig_x) + x1, y, int(orig_x) + x2, y,
241 Painter::line_onoffdash, dotted_line_thickness_);
242 pos = range.last + 1;
247 void RowPainter::paintStringAndSel(docstring const & str, Font const & font,
248 Change const & change,
249 pos_type start_pos, pos_type end_pos)
251 // at least part of text selected?
252 bool const some_sel = (end_pos >= row_.sel_beg && start_pos < row_.sel_end)
254 // all the text selected?
255 bool const all_sel = (start_pos >= row_.sel_beg && end_pos < row_.sel_end)
260 copy.fontInfo().setPaintColor(Color_selectiontext);
261 x_ += pi_.pain.text(int(x_), yo_, str, copy);
262 } else if (change.changed()) {
264 copy.fontInfo().setPaintColor(change.color());
265 x_ += pi_.pain.text(int(x_), yo_, str, copy);
266 } else if (!some_sel) {
267 x_ += pi_.pain.text(int(x_), yo_, str, font);
269 x_ += pi_.pain.text(int(x_), yo_, str, font, Color_selectiontext,
270 max(row_.sel_beg, start_pos) - start_pos,
271 min(row_.sel_end, end_pos) - start_pos);
276 void RowPainter::paintFromPos(pos_type & vpos, bool changed)
278 pos_type pos = bidi_.vis2log(vpos);
279 pos_type start_pos = pos;
283 char_type const c = par_.getChar(pos);
286 double const orig_x = x_;
288 Font const font = text_metrics_.displayFont(pit_, pos);
289 FontSpan const font_span = par_.fontSpan(pos);
290 // Track-change status.
291 Change const & change_running = par_.lookupChange(pos);
293 // collect as much similar chars as we can
294 pos_type const end = row_.endpos();
295 for (++vpos ; vpos < end ; ++vpos) {
296 pos = bidi_.vis2log(vpos);
298 if (!font_span.contains(pos))
301 Change const & change = par_.lookupChange(pos);
302 if (!change_running.isSimilarTo(change))
303 // Track change type or author has changed.
306 char_type const c = par_.getChar(pos);
311 if (!isPrintableNonspace(c))
317 // Make pos point to the last character in the string.
318 // Using "pos = bidi_.vis2log(vpos)" does not work for some reason.
320 pos = bidi_.vis2log(vpos - 1);
322 // Now make pos point to the position _after_ the string.
323 // Using vis2log for that is not a good idea in general, we
324 // want logical ordering.
325 if (font.isVisibleRightToLeft())
331 str.replace(0,1,from_ascii(" "));
333 /* Because we do our own bidi, at this point the strings are
334 * already in visual order. However, Qt also applies its own
335 * bidi algorithm to strings that it paints to the screen.
336 * Therefore, if we were to paint Hebrew/Arabic words as a
337 * single string, the letters in the words would get reversed
338 * again. In order to avoid that, we reverse our string.
339 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
340 * for an earlier thread on the subject
342 if (font.isVisibleRightToLeft()) {
343 reverse(str.begin(), str.end());
344 // If the string is reversed, the positions need to be adjusted
347 swap(start_pos, pos);
350 // Actually paint the text, taking care about the selection
351 paintStringAndSel(str, font, change_running, start_pos, pos);
353 // The line that indicates word in a different language
354 paintForeignMark(orig_x, font.language());
356 // Paint the spelling mark if needed.
357 if (lyxrc.spellcheck_continuously && pi_.do_spellcheck
358 && par_.isMisspelled(start_pos)) {
359 paintMisspelledMark(orig_x, str, font, start_pos, changed);
364 void RowPainter::paintChangeBar() const
366 pos_type const start = row_.pos();
367 pos_type end = row_.endpos();
369 if (par_.size() == end) {
370 // this is the last row of the paragraph;
371 // thus, we must also consider the imaginary end-of-par character
375 if (start == end || !par_.isChanged(start, end))
378 int const height = text_metrics_.isLastRow(pit_, row_)
382 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
386 void RowPainter::paintAppendix() const
388 // only draw the appendix frame once (for the main text)
389 if (!par_.params().appendix() || !text_.isMainText())
392 int y = yo_ - row_.ascent();
394 if (par_.params().startOfAppendix())
395 y += 2 * defaultRowHeight();
397 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
398 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
402 void RowPainter::paintDepthBar() const
404 depth_type const depth = par_.getDepth();
409 depth_type prev_depth = 0;
410 if (!text_metrics_.isFirstRow(pit_, row_)) {
411 pit_type pit2 = pit_;
414 prev_depth = pars_[pit2].getDepth();
417 depth_type next_depth = 0;
418 if (!text_metrics_.isLastRow(pit_, row_)) {
419 pit_type pit2 = pit_;
420 if (row_.endpos() >= pars_[pit2].size())
422 next_depth = pars_[pit2].getDepth();
425 for (depth_type i = 1; i <= depth; ++i) {
426 int const w = nestMargin() / 5;
427 int x = int(xo_) + w * i;
428 // only consider the changebar space if we're drawing outermost text
429 if (text_.isMainText())
430 x += changebarMargin();
432 int const starty = yo_ - row_.ascent();
433 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
435 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
438 pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
440 pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
445 int RowPainter::paintAppendixStart(int y) const
447 FontInfo pb_font = sane_font;
448 pb_font.setColor(Color_appendix);
455 docstring const label = _("Appendix");
456 theFontMetrics(pb_font).rectText(label, w, a, d);
458 int const text_start = int(xo_ + (width_ - w) / 2);
459 int const text_end = text_start + w;
461 pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
463 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
464 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
466 return 3 * defaultRowHeight();
470 void RowPainter::paintTooLargeMarks(bool const left, bool const right) const
473 pi_.pain.line(dotted_line_thickness_, yo_ - row_.ascent(),
474 dotted_line_thickness_, yo_ + row_.descent(),
476 Painter::line_onoffdash, dotted_line_thickness_);
478 int const wwidth = pi_.base.bv->workWidth() - dotted_line_thickness_;
479 pi_.pain.line(wwidth, yo_ - row_.ascent(),
480 wwidth, yo_ + row_.descent(),
482 Painter::line_onoffdash, dotted_line_thickness_);
487 void RowPainter::paintFirst() const
489 BufferParams const & bparams = pi_.base.bv->buffer().params();
490 Layout const & layout = par_.layout();
494 // start of appendix?
495 if (par_.params().startOfAppendix())
496 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
498 if (bparams.paragraph_separation == BufferParams::ParagraphSkipSeparation
500 if (layout.latextype == LATEX_PARAGRAPH
501 && !par_.getDepth()) {
502 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
504 Layout const & playout = pars_[pit_ - 1].layout();
505 if (playout.latextype == LATEX_PARAGRAPH
506 && !pars_[pit_ - 1].getDepth()) {
507 // is it right to use defskip here, too? (AS)
508 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
513 bool const is_first =
514 text_.isFirstInSequence(pit_) || !layout.isParagraphGroup();
515 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
517 if (layout.labelIsInline()
518 && (layout.labeltype != LABEL_STATIC || is_first)) {
520 } else if (is_first && layout.labelIsAbove()) {
521 paintTopLevelLabel();
526 void RowPainter::paintLabel() const
528 docstring const str = par_.labelString();
532 bool const is_rtl = text_.isRTL(par_);
533 Layout const & layout = par_.layout();
534 FontInfo const font = labelFont();
535 FontMetrics const & fm = theFontMetrics(font);
539 x = width_ - leftMargin()
540 + fm.width(layout.labelsep);
542 x = x_ - fm.width(layout.labelsep)
546 pi_.pain.text(int(x), yo_, str, font);
550 void RowPainter::paintTopLevelLabel() const
552 BufferParams const & bparams = pi_.base.bv->buffer().params();
553 bool const is_rtl = text_.isRTL(par_);
554 ParagraphParameters const & pparams = par_.params();
555 Layout const & layout = par_.layout();
556 FontInfo const font = labelFont();
557 docstring const str = par_.labelString();
561 double spacing_val = 1.0;
562 if (!pparams.spacing().isDefault())
563 spacing_val = pparams.spacing().getValue();
565 spacing_val = bparams.spacing().getValue();
567 FontMetrics const & fm = theFontMetrics(font);
569 int const labeladdon = int(fm.maxHeight()
570 * layout.spacing.getValue() * spacing_val);
573 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
574 + (layout.labelbottomsep * defaultRowHeight()));
577 if (layout.labeltype == LABEL_CENTERED) {
580 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
581 x -= fm.width(str) / 2;
583 x = width_ - leftMargin() - fm.width(str);
585 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
589 /** Check if the current paragraph is the last paragraph in a
591 static int getEndLabel(pit_type p, Text const & text)
593 ParagraphList const & pars = text.paragraphs();
595 depth_type par_depth = pars[p].getDepth();
596 while (pit != pit_type(pars.size())) {
597 Layout const & layout = pars[pit].layout();
598 int const endlabeltype = layout.endlabeltype;
600 if (endlabeltype != END_LABEL_NO_LABEL) {
601 if (p + 1 == pit_type(pars.size()))
604 depth_type const next_depth =
605 pars[p + 1].getDepth();
606 if (par_depth > next_depth ||
607 (par_depth == next_depth && layout != pars[p + 1].layout()))
613 pit = text.outerHook(pit);
614 if (pit != pit_type(pars.size()))
615 par_depth = pars[pit].getDepth();
617 return END_LABEL_NO_LABEL;
621 void RowPainter::paintLast()
623 bool const is_rtl = text_.isRTL(par_);
624 int const endlabel = getEndLabel(pit_, text_);
626 // paint imaginary end-of-paragraph character
628 Change const & change = par_.lookupChange(par_.size());
629 if (change.changed()) {
630 FontMetrics const & fm =
631 theFontMetrics(pi_.base.bv->buffer().params().getFont());
632 int const length = fm.maxAscent() / 2;
633 Color col = change.color();
635 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
636 Painter::line_solid, 3);
638 if (change.deleted()) {
639 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
640 yo_ + 2, col, Painter::line_solid, 3);
642 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
643 yo_ + 2, col, Painter::line_solid, 3);
651 case END_LABEL_FILLED_BOX: {
652 FontInfo const font = labelFont();
653 FontMetrics const & fm = theFontMetrics(font);
654 int const size = int(0.75 * fm.maxAscent());
655 int const y = yo_ - size;
656 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
657 int x = is_rtl ? nestMargin() + changebarMargin()
658 : max_row_width - text_metrics_.rightMargin(pm_);
660 // If needed, move the box a bit to avoid overlapping with text.
661 int const rem = max_row_width - row_.width();
663 x += is_rtl ? rem : - rem;
665 if (endlabel == END_LABEL_BOX)
666 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
668 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
672 case END_LABEL_STATIC: {
673 FontInfo const font = labelFont();
674 FontMetrics const & fm = theFontMetrics(font);
675 docstring const & str = par_.layout().endlabelstring();
676 double const x = is_rtl ? x_ - fm.width(str) : x_;
677 pi_.pain.text(int(x), yo_, str, font);
681 case END_LABEL_NO_LABEL:
682 if (lyxrc.paragraph_markers && size_type(pit_ + 1) < pars_.size()) {
683 docstring const s = docstring(1, char_type(0x00B6));
684 FontInfo f = FontInfo(text_.layoutFont(pit_));
685 f.setColor(Color_paragraphmarker);
686 pi_.pain.text(int(x_), yo_, s, f);
687 x_ += theFontMetrics(f).width(s);
694 void RowPainter::paintOnlyInsets()
696 CoordCache const & cache = pi_.base.bv->coordCache();
697 pos_type const end = row_.endpos();
698 for (pos_type pos = row_.pos(); pos != end; ++pos) {
699 // If outer row has changed, nested insets are repaint completely.
700 Inset const * inset = par_.getInset(pos);
701 bool const nested_inset = inset &&
702 ((inset->asInsetMath() &&
703 !inset->asInsetMath()->asMacroTemplate())
704 || inset->asInsetText()
705 || inset->asInsetTabular());
708 if (x_ > pi_.base.bv->workWidth()
709 || !cache.getInsets().has(inset))
711 x_ = cache.getInsets().x(inset);
713 bool const pi_selected = pi_.selected;
714 Cursor const & cur = pi_.base.bv->cursor();
715 if (cur.selection() && cur.text() == &text_
716 && cur.normalAnchor().text() == &text_)
717 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
718 paintInset(inset, pos);
719 pi_.selected = pi_selected;
724 void RowPainter::paintText()
726 //LYXERR0("-------------------------------------------------------");
727 pos_type const end = row_.endpos();
728 // Spaces at logical line breaks in bidi text must be skipped during
729 // painting. However, they may appear visually in the middle
730 // of a row; they must be skipped, wherever they are...
731 // * logically "abc_[HEBREW_\nHEBREW]"
732 // * visually "abc_[_WERBEH\nWERBEH]"
733 pos_type skipped_sep_vpos = -1;
734 pos_type body_pos = par_.beginOfBody();
736 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
740 Layout const & layout = par_.layout();
742 Change change_running;
743 int change_last_x = 0;
745 // check for possible inline completion
746 DocIterator const & inlineCompletionPos = pi_.base.bv->inlineCompletionPos();
747 pos_type inlineCompletionVPos = -1;
748 if (inlineCompletionPos.inTexted()
749 && inlineCompletionPos.text() == &text_
750 && inlineCompletionPos.pit() == pit_
751 && inlineCompletionPos.pos() - 1 >= row_.pos()
752 && inlineCompletionPos.pos() - 1 < row_.endpos()) {
753 // draw logically behind the previous character
754 inlineCompletionVPos = bidi_.log2vis(inlineCompletionPos.pos() - 1);
757 // Use font span to speed things up, see below
761 // If the last logical character is a separator, don't paint it, unless
762 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
763 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
764 skipped_sep_vpos = bidi_.log2vis(end - 1);
766 for (pos_type vpos = row_.pos(); vpos < end; ) {
767 if (x_ > pi_.base.bv->workWidth())
770 // Skip the separator at the logical end of the row
771 if (vpos == skipped_sep_vpos) {
776 pos_type const pos = bidi_.vis2log(vpos);
778 if (pos >= par_.size()) {
783 // Use font span to speed things up, see above
784 if (!font_span.contains(pos)) {
785 font_span = par_.fontSpan(pos);
786 font = text_metrics_.displayFont(pit_, pos);
788 // split font span if inline completion is inside
789 if (inlineCompletionVPos != -1
790 && font_span.contains(inlineCompletionPos.pos()))
791 font_span.last = inlineCompletionPos.pos();
794 // Note that this value will only be used in
795 // situations where no ligature of composition of
796 // characters is needed. (see comments in uses of width_pos).
797 const int width_pos = pm_.singleWidth(pos, font);
799 Change const & change = par_.lookupChange(pos);
800 if (change.changed() && !change_running.changed()) {
801 change_running = change;
802 change_last_x = int(x_);
805 Inset const * inset = par_.getInset(pos);
806 bool const highly_editable_inset = inset
807 && inset->editable();
809 // If we reach the end of a change or if the author changes, paint it.
810 // We also don't paint across things like tables
811 if (change_running.changed() && (highly_editable_inset
812 || !change.changed() || !change_running.isSimilarTo(change))) {
813 // Calculate 1/3 height of the buffer's default font
814 FontMetrics const & fm
815 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
816 int const y_bar = change_running.deleted() ?
817 yo_ - fm.maxAscent() / 3 : yo_ + 2 * solid_line_offset_ + solid_line_thickness_;
818 pi_.pain.line(change_last_x, y_bar, int(x_), y_bar,
819 change_running.color(), Painter::line_solid, solid_line_thickness_);
821 // Change might continue with a different author or type
822 if (change.changed() && !highly_editable_inset) {
823 change_running = change;
824 change_last_x = int(x_);
826 change_running.setUnchanged();
829 if (body_pos > 0 && pos == body_pos - 1) {
830 int const lwidth = theFontMetrics(labelFont())
831 .width(layout.labelsep);
833 // width_pos is either the width of a space or an inset
834 x_ += row_.label_hfill + lwidth - width_pos;
837 // Is the inline completion in front of character?
838 if (font.isRightToLeft() && vpos == inlineCompletionVPos)
839 paintInlineCompletion(font);
841 if (par_.isSeparator(pos)) {
842 Font const orig_font = text_metrics_.displayFont(pit_, pos);
843 double const orig_x = x_;
844 // width_pos is the width of a space
845 double separator_width = width_pos;
847 separator_width += row_.separator;
848 paintSeparator(orig_x, separator_width, orig_font.fontInfo());
849 paintForeignMark(orig_x, orig_font.language());
853 // If outer row has changed, nested insets are repaint completely.
854 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
856 bool const pi_selected = pi_.selected;
857 Cursor const & cur = pi_.base.bv->cursor();
858 if (cur.selection() && cur.text() == &text_
859 && cur.normalAnchor().text() == &text_)
860 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
861 paintInset(inset, pos);
862 pi_.selected = pi_selected;
866 // paint as many characters as possible.
867 paintFromPos(vpos, change_running.changed());
870 // Is the inline completion after character?
871 if (!font.isRightToLeft() && vpos - 1 == inlineCompletionVPos)
872 paintInlineCompletion(font);
875 // if we reach the end of a struck out range, paint it
876 if (change_running.changed()) {
877 FontMetrics const & fm
878 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
879 int const y_bar = change_running.deleted() ?
880 yo_ - fm.maxAscent() / 3 : yo_ + 2 * solid_line_offset_ + solid_line_thickness_;
881 pi_.pain.line(change_last_x, y_bar, int(x_), y_bar,
882 change_running.color(), Painter::line_solid, solid_line_thickness_);
883 change_running.setUnchanged();
888 void RowPainter::paintSelection() const
890 if (!row_.selection())
892 Cursor const & curs = pi_.base.bv->cursor();
893 DocIterator beg = curs.selectionBegin();
895 beg.pos() = row_.sel_beg;
897 DocIterator end = curs.selectionEnd();
899 end.pos() = row_.sel_end;
901 bool const begin_boundary = beg.pos() >= row_.endpos();
902 bool const end_boundary = row_.sel_end == row_.endpos();
904 DocIterator cur = beg;
905 cur.boundary(begin_boundary);
906 int x1 = text_metrics_.cursorX(beg.top(), begin_boundary);
907 int x2 = text_metrics_.cursorX(end.top(), end_boundary);
908 int const y1 = yo_ - row_.ascent();
909 int const y2 = y1 + row_.height();
911 int const rm = text_.isMainText() ? pi_.base.bv->rightMargin() : 0;
912 int const lm = text_.isMainText() ? pi_.base.bv->leftMargin() : 0;
915 if (row_.begin_margin_sel) {
916 if (text_.isRTL(beg.paragraph())) {
917 pi_.pain.fillRectangle(int(xo_ + x1), y1,
918 text_metrics_.width() - rm - x1, y2 - y1, Color_selection);
920 pi_.pain.fillRectangle(int(xo_ + lm), y1, x1 - lm, y2 - y1,
925 if (row_.end_margin_sel) {
926 if (text_.isRTL(beg.paragraph())) {
927 pi_.pain.fillRectangle(int(xo_ + lm), y1, x2 - lm, y2 - y1,
930 pi_.pain.fillRectangle(int(xo_ + x2), y1, text_metrics_.width() - rm - x2,
931 y2 - y1, Color_selection);
935 // if we are on a boundary from the beginning, it's probably
936 // a RTL boundary and we jump to the other side directly as this
937 // segement is 0-size and confuses the logic below
941 // go through row and draw from RTL boundary to RTL boundary
943 bool draw_now = false;
945 // simplified cursorForward code below which does not
946 // descend into insets and which does not go into the
947 // next line. Compare the logic with the original cursorForward
949 // if left of boundary -> just jump to right side, but
950 // for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
951 if (cur.boundary()) {
953 } else if (text_metrics_.isRTLBoundary(cur.pit(), cur.pos() + 1)) {
954 // in front of RTL boundary -> Stay on this side of the boundary
955 // because: ab|cDDEEFFghi -> abc|DDEEFFghi
964 if (cur.pos() == row_.endpos())
969 // the previous segment was just drawn, now the next starts
970 x1 = text_metrics_.cursorX(cur.top(), cur.boundary());
973 if (!(cur < end) || draw_now) {
974 x2 = text_metrics_.cursorX(cur.top(), cur.boundary());
975 pi_.pain.fillRectangle(int(xo_ + min(x1, x2)), y1, abs(x2 - x1),
976 y2 - y1, Color_selection);
978 // reset x1, so it is set again next round (which will be on the
979 // right side of a boundary or at the selection end)
986 void RowPainter::paintInlineCompletion(Font const & font)
988 docstring completion = pi_.base.bv->inlineCompletion();
989 FontInfo f = font.fontInfo();
990 bool rtl = font.isRightToLeft();
992 // draw the unique and the non-unique completion part
993 // Note: this is not time-critical as it is
994 // only done once per screen.
995 size_t uniqueTo = pi_.base.bv->inlineCompletionUniqueChars();
996 docstring s1 = completion.substr(0, uniqueTo);
997 docstring s2 = completion.substr(uniqueTo);
998 ColorCode c1 = Color_inlinecompletion;
999 ColorCode c2 = Color_nonunique_inlinecompletion;
1009 pi_.pain.text(int(x_), yo_, s1, f);
1010 x_ += theFontMetrics(font).width(s1);
1015 pi_.pain.text(int(x_), yo_, s2, f);
1016 x_ += theFontMetrics(font).width(s2);