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.0), solid_line_offset_(1),
65 dotted_line_thickness_(1.0), 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_ = (float)(int((lyxrc.zoom + 50) / 200.0));
74 // adjust line_offset_ too
75 solid_line_offset_ = 1 + int(0.5 * solid_line_thickness_);
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_ = (float)(int((lyxrc.zoom + 50) / 100.0));
82 // adjust line_offset_ too
83 dotted_line_offset_ = int(0.5 * dotted_line_thickness_) + 1;
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 pi_full_repaint = pi_.full_repaint;
126 pi_.base.font = inset->inheritFont() ? font.fontInfo() :
127 pi_.base.bv->buffer().params().getFont().fontInfo();
128 pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
129 Change prev_change = change_;
130 pi_.change_ = change_.changed() ? change_ : par_.lookupChange(pos);
132 int const x1 = int(x_);
133 pi_.base.bv->coordCache().insets().add(inset, x1, yo_);
134 // insets are painted completely. Recursive
135 // FIXME: it is wrong to completely paint the background
136 // if we want to do single row painting.
137 inset->drawBackground(pi_, x1, yo_);
138 inset->drawSelection(pi_, x1, yo_);
139 inset->draw(pi_, x1, yo_);
141 Dimension const & dim = pm_.insetDimension(inset);
143 paintForeignMark(x_, font.language(), dim.descent());
147 // Restore full_repaint status.
148 pi_.full_repaint = pi_full_repaint;
149 pi_.change_ = prev_change;
152 int const x2 = x1 + dim.wid;
153 int const y1 = yo_ + dim.des;
154 int const y2 = yo_ - dim.asc;
155 pi_.pain.line(x1, y1, x1, y2, Color_green);
156 pi_.pain.line(x1, y1, x2, y1, Color_green);
157 pi_.pain.line(x2, y1, x2, y2, Color_green);
158 pi_.pain.line(x1, y2, x2, y2, Color_green);
163 void RowPainter::paintChars(pos_type & vpos, Font const & font)
165 // This method takes up 70% of time when typing
166 pos_type pos = bidi_.vis2log(vpos);
168 char_type prev_char = par_.getChar(pos);
169 vector<char_type> str;
171 str.push_back(prev_char);
173 // special case for arabic
174 string const & lang = font.language()->lang();
175 bool const swap_paren = lang == "arabic_arabtex"
176 || lang == "arabic_arabi"
179 // FIXME: Why only round brackets and why the difference to
180 // Hebrew? See also Paragraph::getUChar
182 char_type c = str[0];
190 pos_type const end = row_.endpos();
191 FontSpan const font_span = par_.fontSpan(pos);
192 // Track-change status.
193 Change const & change_running = par_.lookupChange(pos);
196 bool const selection = (pos >= row_.sel_beg && pos < row_.sel_end)
200 bool const spell_state =
201 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
203 // collect as much similar chars as we can
204 for (++vpos ; vpos < end ; ++vpos) {
205 if (lyxrc.force_paint_single_char)
208 pos = bidi_.vis2log(vpos);
209 if (pos < font_span.first || pos > font_span.last)
212 bool const new_selection = pos >= row_.sel_beg && pos < row_.sel_end;
213 if (new_selection != selection)
214 // Selection ends or starts here.
217 bool const new_spell_state =
218 lyxrc.spellcheck_continuously && par_.isMisspelled(pos);
219 if (new_spell_state != spell_state)
220 // Spell checker state changed here.
223 Change const & change = par_.lookupChange(pos);
224 if (!change_running.isSimilarTo(change))
225 // Track change type or author has changed.
228 char_type c = par_.getChar(pos);
233 if (!isPrintableNonspace(c))
236 // FIXME: Why only round brackets and why the difference to
237 // Hebrew? See also Paragraph::getUChar
249 docstring s(&str[0], str.size());
252 s.replace(0,1,from_ascii(" "));
254 /* Because we do our own bidi, at this point the strings are
255 * already in visual order. However, Qt also applies its own
256 * bidi algorithm to strings that it paints to the screen.
257 * Therefore, if we were to paint Hebrew/Arabic words as a
258 * single string, the letters in the words would get reversed
259 * again. In order to avoid that, we force LTR drawing.
260 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
261 * for an earlier thread on the subject
263 // Left-to-right override: forces to draw text left-to-right
264 char_type const LRO = 0x202D;
265 // Right-to-left override: forces to draw text right-to-left
266 char_type const RLO = 0x202E;
267 // Pop directional formatting: return to previous state
268 char_type const PDF = 0x202C;
269 if (font.isVisibleRightToLeft()) {
270 reverse(s.begin(), s.end());
275 if (!selection && !change_running.changed()) {
276 x_ += pi_.pain.text(int(x_), yo_, s, font.fontInfo());
280 FontInfo copy = font.fontInfo();
281 if (change_running.changed())
282 copy.setPaintColor(change_running.color());
284 copy.setPaintColor(Color_selectiontext);
286 x_ += pi_.pain.text(int(x_), yo_, s, copy);
290 void RowPainter::paintSeparator(double orig_x, double width,
291 FontInfo const & font)
293 pi_.pain.textDecoration(font, int(orig_x), yo_, int(width));
298 void RowPainter::paintForeignMark(double orig_x, Language const * lang,
301 if (!lyxrc.mark_foreign_language)
303 if (lang == latex_language)
305 if (lang == pi_.base.bv->buffer().params().language)
308 int const y = yo_ + solid_line_offset_ + desc + int(solid_line_thickness_/2);
309 pi_.pain.line(int(orig_x), y, int(x_), y, Color_language,
310 Painter::line_solid, solid_line_thickness_);
314 void RowPainter::paintMisspelledMark(double orig_x, bool changed)
316 // if changed the misspelled marker gets placed slightly lower than normal
317 // to avoid drawing at the same vertical offset
318 float const y = yo_ + solid_line_offset_ + solid_line_thickness_
319 + (changed ? solid_line_thickness_ + 1 : 0)
320 + dotted_line_offset_;
321 pi_.pain.line(int(orig_x), int(y), int(x_), int(y), Color_error,
322 Painter::line_onoffdash, dotted_line_thickness_);
326 void RowPainter::paintFromPos(pos_type & vpos, bool changed)
328 pos_type const pos = bidi_.vis2log(vpos);
329 Font const font = text_metrics_.displayFont(pit_, pos);
330 double const orig_x = x_;
332 paintChars(vpos, font);
333 paintForeignMark(orig_x, font.language());
335 // Paint the spelling mark if needed.
336 if (lyxrc.spellcheck_continuously && par_.isMisspelled(pos)) {
337 // check for cursor position
338 // don't draw misspelled marker for words at cursor position
339 // we don't want to disturb the process of text editing
340 BufferView const * bv = pi_.base.bv;
341 DocIterator const nw = bv->cursor().newWord();
342 bool new_word = false;
343 if (!nw.empty() && par_.id() == nw.paragraph().id()) {
344 pos_type cpos = nw.pos();
345 if (cpos > 0 && cpos == par_.size() && !par_.isWordSeparator(cpos-1))
347 else if (cpos > 0 && par_.isWordSeparator(cpos))
349 new_word = par_.isSameSpellRange(pos, cpos) ;
352 paintMisspelledMark(orig_x, changed);
357 void RowPainter::paintChangeBar()
359 pos_type const start = row_.pos();
360 pos_type end = row_.endpos();
362 if (par_.size() == end) {
363 // this is the last row of the paragraph;
364 // thus, we must also consider the imaginary end-of-par character
368 if (start == end || !par_.isChanged(start, end))
371 int const height = text_metrics_.isLastRow(pit_, row_)
375 pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
379 void RowPainter::paintAppendix()
381 // only draw the appendix frame once (for the main text)
382 if (!par_.params().appendix() || !text_.isMainText())
385 int y = yo_ - row_.ascent();
387 if (par_.params().startOfAppendix())
388 y += 2 * defaultRowHeight();
390 pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
391 pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
395 void RowPainter::paintDepthBar()
397 depth_type const depth = par_.getDepth();
402 depth_type prev_depth = 0;
403 if (!text_metrics_.isFirstRow(pit_, row_)) {
404 pit_type pit2 = pit_;
407 prev_depth = pars_[pit2].getDepth();
410 depth_type next_depth = 0;
411 if (!text_metrics_.isLastRow(pit_, row_)) {
412 pit_type pit2 = pit_;
413 if (row_.endpos() >= pars_[pit2].size())
415 next_depth = pars_[pit2].getDepth();
418 for (depth_type i = 1; i <= depth; ++i) {
419 int const w = nestMargin() / 5;
420 int x = int(xo_) + w * i;
421 // only consider the changebar space if we're drawing outermost text
422 if (text_.isMainText())
423 x += changebarMargin();
425 int const starty = yo_ - row_.ascent();
426 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
428 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
431 pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
433 pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
438 int RowPainter::paintAppendixStart(int y)
440 FontInfo pb_font = sane_font;
441 pb_font.setColor(Color_appendix);
448 docstring const label = _("Appendix");
449 theFontMetrics(pb_font).rectText(label, w, a, d);
451 int const text_start = int(xo_ + (width_ - w) / 2);
452 int const text_end = text_start + w;
454 pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
456 pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
457 pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
459 return 3 * defaultRowHeight();
463 void RowPainter::paintFirst()
465 BufferParams const & bparams = pi_.base.bv->buffer().params();
466 Layout const & layout = par_.layout();
470 // start of appendix?
471 if (par_.params().startOfAppendix())
472 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
474 if (bparams.paragraph_separation == BufferParams::ParagraphSkipSeparation
476 if (layout.latextype == LATEX_PARAGRAPH
477 && !par_.getDepth()) {
478 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
480 Layout const & playout = pars_[pit_ - 1].layout();
481 if (playout.latextype == LATEX_PARAGRAPH
482 && !pars_[pit_ - 1].getDepth()) {
483 // is it right to use defskip here, too? (AS)
484 y_top += bparams.getDefSkip().inPixels(*pi_.base.bv);
489 bool const is_first =
490 text_.isFirstInSequence(pit_) || !layout.isParagraphGroup();
491 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
493 if (layout.labelIsInline()
494 && (layout.labeltype != LABEL_STATIC || is_first)) {
496 } else if (is_first && layout.labelIsAbove()) {
497 paintTopLevelLabel();
502 void RowPainter::paintLabel()
504 docstring const str = par_.labelString();
508 bool const is_rtl = text_.isRTL(par_);
509 Layout const & layout = par_.layout();
510 FontInfo const font = labelFont();
511 FontMetrics const & fm = theFontMetrics(font);
515 x = width_ - leftMargin()
516 + fm.width(layout.labelsep);
518 x = x_ - fm.width(layout.labelsep)
522 pi_.pain.text(int(x), yo_, str, font);
526 void RowPainter::paintTopLevelLabel()
528 BufferParams const & bparams = pi_.base.bv->buffer().params();
529 bool const is_rtl = text_.isRTL(par_);
530 ParagraphParameters const & pparams = par_.params();
531 Layout const & layout = par_.layout();
532 FontInfo const font = labelFont();
533 docstring const str = par_.labelString();
537 double spacing_val = 1.0;
538 if (!pparams.spacing().isDefault())
539 spacing_val = pparams.spacing().getValue();
541 spacing_val = bparams.spacing().getValue();
543 FontMetrics const & fm = theFontMetrics(font);
545 int const labeladdon = int(fm.maxHeight()
546 * layout.spacing.getValue() * spacing_val);
549 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
550 + (layout.labelbottomsep * defaultRowHeight()));
553 if (layout.labeltype == LABEL_CENTERED) {
556 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
557 x -= fm.width(str) / 2;
559 x = width_ - leftMargin() - fm.width(str);
561 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
565 /** Check if the current paragraph is the last paragraph in a
567 static int getEndLabel(pit_type p, Text const & text)
569 ParagraphList const & pars = text.paragraphs();
571 depth_type par_depth = pars[p].getDepth();
572 while (pit != pit_type(pars.size())) {
573 Layout const & layout = pars[pit].layout();
574 int const endlabeltype = layout.endlabeltype;
576 if (endlabeltype != END_LABEL_NO_LABEL) {
577 if (p + 1 == pit_type(pars.size()))
580 depth_type const next_depth =
581 pars[p + 1].getDepth();
582 if (par_depth > next_depth ||
583 (par_depth == next_depth && layout != pars[p + 1].layout()))
589 pit = text.outerHook(pit);
590 if (pit != pit_type(pars.size()))
591 par_depth = pars[pit].getDepth();
593 return END_LABEL_NO_LABEL;
597 void RowPainter::paintLast()
599 bool const is_rtl = text_.isRTL(par_);
600 int const endlabel = getEndLabel(pit_, text_);
602 // paint imaginary end-of-paragraph character
604 Change const & change = par_.lookupChange(par_.size());
605 if (change.changed()) {
606 FontMetrics const & fm =
607 theFontMetrics(pi_.base.bv->buffer().params().getFont());
608 int const length = fm.maxAscent() / 2;
609 Color col = change.color();
611 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
612 Painter::line_solid, 3);
614 if (change.deleted()) {
615 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
616 yo_ + 2, col, Painter::line_solid, 3);
618 pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
619 yo_ + 2, col, Painter::line_solid, 3);
627 case END_LABEL_FILLED_BOX: {
628 FontInfo const font = labelFont();
629 FontMetrics const & fm = theFontMetrics(font);
630 int const size = int(0.75 * fm.maxAscent());
631 int const y = yo_ - size;
632 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
633 int x = is_rtl ? nestMargin() + changebarMargin()
634 : max_row_width - text_metrics_.rightMargin(pm_);
636 // If needed, move the box a bit to avoid overlapping with text.
637 int const rem = max_row_width - row_.width();
639 x += is_rtl ? rem : - rem;
641 if (endlabel == END_LABEL_BOX)
642 pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
644 pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
648 case END_LABEL_STATIC: {
649 FontInfo const font = labelFont();
650 FontMetrics const & fm = theFontMetrics(font);
651 docstring const & str = par_.layout().endlabelstring();
652 double const x = is_rtl ? x_ - fm.width(str) : x_;
653 pi_.pain.text(int(x), yo_, str, font);
657 case END_LABEL_NO_LABEL:
658 if (lyxrc.paragraph_markers && size_type(pit_ + 1) < pars_.size()) {
659 docstring const s = docstring(1, char_type(0x00B6));
660 FontInfo f = FontInfo(text_.layoutFont(pit_));
661 f.setColor(Color_paragraphmarker);
662 pi_.pain.text(int(x_), yo_, s, f);
663 x_ += theFontMetrics(f).width(s);
670 void RowPainter::paintOnlyInsets()
672 CoordCache const & cache = pi_.base.bv->coordCache();
673 pos_type const end = row_.endpos();
674 for (pos_type pos = row_.pos(); pos != end; ++pos) {
675 // If outer row has changed, nested insets are repaint completely.
676 Inset const * inset = par_.getInset(pos);
677 bool const nested_inset = inset &&
678 ((inset->asInsetMath() &&
679 !inset->asInsetMath()->asMacroTemplate())
680 || inset->asInsetText()
681 || inset->asInsetTabular());
684 if (x_ > pi_.base.bv->workWidth()
685 || !cache.getInsets().has(inset))
687 x_ = cache.getInsets().x(inset);
689 bool const pi_selected = pi_.selected;
690 Cursor const & cur = pi_.base.bv->cursor();
691 if (cur.selection() && cur.text() == &text_
692 && cur.normalAnchor().text() == &text_)
693 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
694 paintInset(inset, pos);
695 pi_.selected = pi_selected;
700 void RowPainter::paintText()
702 pos_type const end = row_.endpos();
703 // Spaces at logical line breaks in bidi text must be skipped during
704 // painting. However, they may appear visually in the middle
705 // of a row; they must be skipped, wherever they are...
706 // * logically "abc_[HEBREW_\nHEBREW]"
707 // * visually "abc_[_WERBEH\nWERBEH]"
708 pos_type skipped_sep_vpos = -1;
709 pos_type body_pos = par_.beginOfBody();
711 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
715 Layout const & layout = par_.layout();
717 Change change_running;
718 int change_last_x = 0;
720 // check for possible inline completion
721 DocIterator const & inlineCompletionPos = pi_.base.bv->inlineCompletionPos();
722 pos_type inlineCompletionVPos = -1;
723 if (inlineCompletionPos.inTexted()
724 && inlineCompletionPos.text() == &text_
725 && inlineCompletionPos.pit() == pit_
726 && inlineCompletionPos.pos() - 1 >= row_.pos()
727 && inlineCompletionPos.pos() - 1 < row_.endpos()) {
728 // draw logically behind the previous character
729 inlineCompletionVPos = bidi_.log2vis(inlineCompletionPos.pos() - 1);
732 // Use font span to speed things up, see below
736 // If the last logical character is a separator, don't paint it, unless
737 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
738 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
739 skipped_sep_vpos = bidi_.log2vis(end - 1);
741 for (pos_type vpos = row_.pos(); vpos < end; ) {
742 if (x_ > pi_.base.bv->workWidth())
745 // Skip the separator at the logical end of the row
746 if (vpos == skipped_sep_vpos) {
751 pos_type const pos = bidi_.vis2log(vpos);
753 if (pos >= par_.size()) {
758 // Use font span to speed things up, see above
759 if (vpos < font_span.first || vpos > font_span.last) {
760 font_span = par_.fontSpan(vpos);
761 font = text_metrics_.displayFont(pit_, vpos);
763 // split font span if inline completion is inside
764 if (font_span.first <= inlineCompletionVPos
765 && font_span.last > inlineCompletionVPos)
766 font_span.last = inlineCompletionVPos;
769 const int width_pos = pm_.singleWidth(pos, font);
771 if (x_ + width_pos < 0) {
776 Change const & change = par_.lookupChange(pos);
777 if (change.changed() && !change_running.changed()) {
778 change_running = change;
779 change_last_x = int(x_);
782 Inset const * inset = par_.getInset(pos);
783 bool const highly_editable_inset = inset
784 && inset->editable();
786 // If we reach the end of a change or if the author changes, paint it.
787 // We also don't paint across things like tables
788 if (change_running.changed() && (highly_editable_inset
789 || !change.changed() || !change_running.isSimilarTo(change))) {
790 // Calculate 1/3 height of the buffer's default font
791 FontMetrics const & fm
792 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
793 float const y_bar = change_running.deleted() ?
794 yo_ - fm.maxAscent() / 3 : yo_ + 2 * solid_line_offset_ + solid_line_thickness_;
795 pi_.pain.line(change_last_x, int(y_bar), int(x_), int(y_bar),
796 change_running.color(), Painter::line_solid, solid_line_thickness_);
798 // Change might continue with a different author or type
799 if (change.changed() && !highly_editable_inset) {
800 change_running = change;
801 change_last_x = int(x_);
803 change_running.setUnchanged();
806 if (body_pos > 0 && pos == body_pos - 1) {
807 int const lwidth = theFontMetrics(labelFont())
808 .width(layout.labelsep);
810 x_ += row_.label_hfill + lwidth - width_pos;
813 // Is the inline completion in front of character?
814 if (font.isRightToLeft() && vpos == inlineCompletionVPos)
815 paintInlineCompletion(font);
817 if (par_.isSeparator(pos)) {
818 Font const orig_font = text_metrics_.displayFont(pit_, pos);
819 double const orig_x = x_;
820 double separator_width = width_pos;
822 separator_width += row_.separator;
823 paintSeparator(orig_x, separator_width, orig_font.fontInfo());
824 paintForeignMark(orig_x, orig_font.language());
828 // If outer row has changed, nested insets are repaint completely.
829 pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
831 bool const pi_selected = pi_.selected;
832 Cursor const & cur = pi_.base.bv->cursor();
833 if (cur.selection() && cur.text() == &text_
834 && cur.normalAnchor().text() == &text_)
835 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos;
836 paintInset(inset, pos);
837 pi_.selected = pi_selected;
841 // paint as many characters as possible.
842 paintFromPos(vpos, change_running.changed());
845 // Is the inline completion after character?
846 if (!font.isRightToLeft() && vpos - 1 == inlineCompletionVPos)
847 paintInlineCompletion(font);
850 // if we reach the end of a struck out range, paint it
851 if (change_running.changed()) {
852 FontMetrics const & fm
853 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
854 float const y_bar = change_running.deleted() ?
855 yo_ - fm.maxAscent() / 3 : yo_ + 2 * solid_line_offset_ + solid_line_thickness_;
856 pi_.pain.line(change_last_x, int(y_bar), int(x_), int(y_bar),
857 change_running.color(), Painter::line_solid, solid_line_thickness_);
858 change_running.setUnchanged();
863 void RowPainter::paintSelection()
865 if (!row_.selection())
867 Cursor const & curs = pi_.base.bv->cursor();
868 DocIterator beg = curs.selectionBegin();
870 beg.pos() = row_.sel_beg;
872 DocIterator end = curs.selectionEnd();
874 end.pos() = row_.sel_end;
876 bool const begin_boundary = beg.pos() >= row_.endpos();
877 bool const end_boundary = row_.sel_end == row_.endpos();
879 DocIterator cur = beg;
880 cur.boundary(begin_boundary);
881 int x1 = text_metrics_.cursorX(beg.top(), begin_boundary);
882 int x2 = text_metrics_.cursorX(end.top(), end_boundary);
883 int const y1 = yo_ - row_.ascent();
884 int const y2 = y1 + row_.height();
886 int const rm = text_.isMainText() ? pi_.base.bv->rightMargin() : 0;
887 int const lm = text_.isMainText() ? pi_.base.bv->leftMargin() : 0;
890 if (row_.begin_margin_sel) {
891 if (text_.isRTL(beg.paragraph())) {
892 pi_.pain.fillRectangle(int(xo_ + x1), y1,
893 text_metrics_.width() - rm - x1, y2 - y1, Color_selection);
895 pi_.pain.fillRectangle(int(xo_ + lm), y1, x1 - lm, y2 - y1,
900 if (row_.end_margin_sel) {
901 if (text_.isRTL(beg.paragraph())) {
902 pi_.pain.fillRectangle(int(xo_ + lm), y1, x2 - lm, y2 - y1,
905 pi_.pain.fillRectangle(int(xo_ + x2), y1, text_metrics_.width() - rm - x2,
906 y2 - y1, Color_selection);
910 // if we are on a boundary from the beginning, it's probably
911 // a RTL boundary and we jump to the other side directly as this
912 // segement is 0-size and confuses the logic below
916 // go through row and draw from RTL boundary to RTL boundary
918 bool draw_now = false;
920 // simplified cursorForward code below which does not
921 // descend into insets and which does not go into the
922 // next line. Compare the logic with the original cursorForward
924 // if left of boundary -> just jump to right side, but
925 // for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
926 if (cur.boundary()) {
928 } else if (text_metrics_.isRTLBoundary(cur.pit(), cur.pos() + 1)) {
929 // in front of RTL boundary -> Stay on this side of the boundary
930 // because: ab|cDDEEFFghi -> abc|DDEEFFghi
939 if (cur.pos() == row_.endpos())
944 // the previous segment was just drawn, now the next starts
945 x1 = text_metrics_.cursorX(cur.top(), cur.boundary());
948 if (!(cur < end) || draw_now) {
949 x2 = text_metrics_.cursorX(cur.top(), cur.boundary());
950 pi_.pain.fillRectangle(int(xo_ + min(x1, x2)), y1, abs(x2 - x1),
951 y2 - y1, Color_selection);
953 // reset x1, so it is set again next round (which will be on the
954 // right side of a boundary or at the selection end)
961 void RowPainter::paintInlineCompletion(Font const & font)
963 docstring completion = pi_.base.bv->inlineCompletion();
964 FontInfo f = font.fontInfo();
965 bool rtl = font.isRightToLeft();
967 // draw the unique and the non-unique completion part
968 // Note: this is not time-critical as it is
969 // only done once per screen.
970 size_t uniqueTo = pi_.base.bv->inlineCompletionUniqueChars();
971 docstring s1 = completion.substr(0, uniqueTo);
972 docstring s2 = completion.substr(uniqueTo);
973 ColorCode c1 = Color_inlinecompletion;
974 ColorCode c2 = Color_nonunique_inlinecompletion;
984 pi_.pain.text(int(x_), yo_, s1, f);
985 x_ += theFontMetrics(font).width(s1);
990 pi_.pain.text(int(x_), yo_, s2, f);
991 x_ += theFontMetrics(font).width(s2);