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.
14 #include "rowpainter.h"
18 #include "CoordCache.h"
21 #include "BufferParams.h"
22 #include "BufferView.h"
29 #include "MetricsInfo.h"
30 #include "Paragraph.h"
31 #include "ParagraphMetrics.h"
32 #include "paragraph_funcs.h"
33 #include "ParagraphParameters.h"
34 #include "TextMetrics.h"
37 #include "frontends/FontMetrics.h"
38 #include "frontends/Painter.h"
40 #include "insets/InsetText.h"
42 #include "support/textutils.h"
44 #include <boost/crc.hpp>
49 using frontend::Painter;
50 using frontend::FontMetrics;
59 /// Flag: do a full redraw of inside text of inset
60 /// Working variable indicating a full screen refresh
64 * A class used for painting an individual row of text.
68 /// initialise and run painter
69 RowPainter(PainterInfo & pi, Text const & text,
70 pit_type pit, Row const & row, Bidi & bidi, int x, int y);
72 // paint various parts
75 void paintChangeBar();
79 int maxWidth() { return max_width_; }
82 void paintForeignMark(double orig_x, Font const & font, int desc = 0);
83 void paintHebrewComposeChar(pos_type & vpos, Font const & font);
84 void paintArabicComposeChar(pos_type & vpos, Font const & font);
85 void paintChars(pos_type & vpos, Font const & font,
86 bool hebrew, bool arabic);
87 int paintAppendixStart(int y);
88 void paintFromPos(pos_type & vpos);
89 void paintInset(pos_type const pos, Font const & font);
91 /// return left margin
92 int leftMargin() const;
94 /// return the label font for this row
95 Font const getLabelFont() const;
97 /// bufferview to paint on
105 TextMetrics & text_metrics_;
106 ParagraphList const & pars_;
113 Paragraph const & par_;
114 ParagraphMetrics const & pm_;
117 /// bidi cache, comes from outside the rowpainter because
118 /// rowpainters are normally created in a for loop and there only
119 /// one of them is active at a time.
122 /// is row erased? (change tracking)
127 int const yo_; // current baseline
136 RowPainter::RowPainter(PainterInfo & pi,
137 Text const & text, pit_type pit, Row const & row, Bidi & bidi, int x, int y)
138 : bv_(*pi.base.bv), pain_(pi.pain), text_(text),
139 text_metrics_(pi.base.bv->textMetrics(&text)),
140 pars_(text.paragraphs()),
141 row_(row), pit_(pit), par_(text.paragraphs()[pit]),
142 pm_(text_metrics_.parMetrics(pit)),
143 max_width_(bv_.workWidth()),
144 bidi_(bidi), erased_(pi.erased_),
145 xo_(x), yo_(y), width_(text_metrics_.width())
147 RowMetrics m = text_metrics_.computeRowMetrics(pit_, row_);
148 bidi_.computeTables(par_, *bv_.buffer(), row_);
151 //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
154 separator_ = m.separator;
156 label_hfill_ = m.label_hfill;
158 BOOST_ASSERT(pit >= 0);
159 BOOST_ASSERT(pit < int(text.paragraphs().size()));
163 Font const RowPainter::getLabelFont() const
165 return text_.getLabelFont(*bv_.buffer(), par_);
169 int RowPainter::leftMargin() const
171 return text_.leftMargin(*bv_.buffer(), max_width_, pit_, row_.pos());
175 // If you want to debug inset metrics uncomment the following line:
176 // #define DEBUG_METRICS
177 // This draws green lines around each inset.
180 void RowPainter::paintInset(pos_type const pos, Font const & font)
182 Inset const * inset = par_.getInset(pos);
184 PainterInfo pi(const_cast<BufferView *>(&bv_), pain_);
185 // FIXME: We should always use font, see documentation of
186 // noFontChange() in Inset.h.
187 pi.base.font = inset->noFontChange() ?
188 bv_.buffer()->params().getFont() :
190 pi.ltr_pos = (bidi_.level(pos) % 2 == 0);
191 pi.erased_ = erased_ || par_.isDeleted(pos);
193 int const x1 = int(x_);
195 bv_.coordCache().insets().add(inset, int(x_), yo_);
196 InsetText const * const in = inset->asTextInset();
197 // non-wide insets are painted completely. Recursive
198 bool tmp = refreshInside;
199 if (!in || !in->wide()) {
200 refreshInside = true;
201 LYXERR(Debug::PAINTING) << endl << "Paint inset fully" << endl;
204 inset->drawSelection(pi, int(x_), yo_);
205 inset->draw(pi, int(x_), yo_);
207 x_ += inset->width();
210 BOOST_ASSERT(max_witdh_ > 0);
211 int right_margin = text_metrics_.rightMargin(pm_);
212 int const w = max_witdh_ - leftMargin() - right_margin;
213 MetricsInfo mi(&bv_, font, w);
214 inset->metrics(mi, dim);
215 if (inset->width() > dim.wid)
216 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
217 << " draw width " << inset->width()
218 << "> metrics width " << dim.wid << "." << std::endl;
219 if (inset->ascent() > dim.asc)
220 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
221 << " draw ascent " << inset->ascent()
222 << "> metrics ascent " << dim.asc << "." << std::endl;
223 if (inset->descent() > dim.des)
224 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
225 << " draw ascent " << inset->descent()
226 << "> metrics descent " << dim.des << "." << std::endl;
227 BOOST_ASSERT(inset->width() <= dim.wid);
228 BOOST_ASSERT(inset->ascent() <= dim.asc);
229 BOOST_ASSERT(inset->descent() <= dim.des);
230 int const x2 = x1 + dim.wid;
231 int const y1 = yo_ + dim.des;
232 int const y2 = yo_ - dim.asc;
233 pi.pain.line(x1, y1, x1, y2, Color::green);
234 pi.pain.line(x1, y1, x2, y1, Color::green);
235 pi.pain.line(x2, y1, x2, y2, Color::green);
236 pi.pain.line(x1, y2, x2, y2, Color::green);
241 void RowPainter::paintHebrewComposeChar(pos_type & vpos, Font const & font)
243 pos_type pos = bidi_.vis2log(vpos);
248 char_type c = par_.getChar(pos);
252 int const width = theFontMetrics(font).width(c);
255 for (pos_type i = pos - 1; i >= 0; --i) {
257 if (!Encodings::isComposeChar_hebrew(c)) {
258 if (isPrintableNonspace(c)) {
259 int const width2 = text_.singleWidth(par_, i, c,
260 text_.getFont(*bv_.buffer(), par_, i));
261 dx = (c == 0x05e8 || // resh
262 c == 0x05d3) // dalet
264 : (width2 - width) / 2;
271 pain_.text(int(x_) + dx, yo_, str, font);
275 void RowPainter::paintArabicComposeChar(pos_type & vpos, Font const & font)
277 pos_type pos = bidi_.vis2log(vpos);
281 char_type c = par_.getChar(pos);
282 c = par_.transformChar(c, pos);
286 int const width = theFontMetrics(font).width(c);
289 for (pos_type i = pos - 1; i >= 0; --i) {
291 if (!Encodings::isComposeChar_arabic(c)) {
292 if (isPrintableNonspace(c)) {
293 int const width2 = text_.singleWidth(par_, i, c,
294 text_.getFont(*bv_.buffer(), par_, i));
295 dx = (width2 - width) / 2;
301 pain_.text(int(x_) + dx, yo_, str, font);
305 void RowPainter::paintChars(pos_type & vpos, Font const & font,
306 bool hebrew, bool arabic)
308 // This method takes up 70% of time when typing
309 pos_type pos = bidi_.vis2log(vpos);
310 pos_type const end = row_.endpos();
311 FontSpan const font_span = par_.fontSpan(pos);
312 Change::Type const prev_change = par_.lookupChange(pos).type;
315 std::vector<char_type> str;
317 str.push_back(par_.getChar(pos));
320 char_type c = str[0];
325 str[0] = par_.transformChar(c, pos);
328 // collect as much similar chars as we can
329 for (++vpos ; vpos < end ; ++vpos) {
330 pos = bidi_.vis2log(vpos);
331 if (pos < font_span.first || pos > font_span.last)
334 if (prev_change != par_.lookupChange(pos).type)
337 char_type c = par_.getChar(pos);
339 if (!isPrintableNonspace(c))
342 /* Because we do our own bidi, at this point the strings are
343 * already in visual order. However, Qt also applies its own
344 * bidi algorithm to strings that it paints to the screen.
345 * Therefore, if we were to paint Hebrew/Arabic words as a
346 * single string, the letters in the words would get reversed
347 * again. In order to avoid that, we don't collect Hebrew/
348 * Arabic characters, but rather paint them one at a time.
349 * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
354 /* FIXME: these checks are irrelevant, since 'arabic' and
355 * 'hebrew' alone are already going to trigger a break.
356 * However, this should not be removed completely, because
357 * if an alternative solution is found which allows grouping
358 * of arabic and hebrew characters, then these breaks may have
361 if (arabic && Encodings::isComposeChar_arabic(c))
364 if (hebrew && Encodings::isComposeChar_hebrew(c))
373 c = par_.transformChar(c, pos);
374 /* see comment in hebrew, explaining why we break */
381 docstring s(&str[0], str.size());
383 if (prev_change != Change::UNCHANGED) {
385 if (prev_change == Change::DELETED) {
386 copy.setColor(Color::deletedtext);
387 } else if (prev_change == Change::INSERTED) {
388 copy.setColor(Color::addedtext);
390 x_ += pain_.text(int(x_), yo_, s, copy);
392 x_ += pain_.text(int(x_), yo_, s, font);
397 void RowPainter::paintForeignMark(double orig_x, Font const & font, int desc)
399 if (!lyxrc.mark_foreign_language)
401 if (font.language() == latex_language)
403 if (font.language() == bv_.buffer()->params().language)
406 int const y = yo_ + 1 + desc;
407 pain_.line(int(orig_x), y, int(x_), y, Color::language);
411 void RowPainter::paintFromPos(pos_type & vpos)
413 pos_type const pos = bidi_.vis2log(vpos);
414 Font orig_font = text_.getFont(*bv_.buffer(), par_, pos);
416 double const orig_x = x_;
418 if (par_.isInset(pos)) {
419 paintInset(pos, orig_font);
421 paintForeignMark(orig_x, orig_font,
422 par_.getInset(pos)->descent());
426 // usual characters, no insets
427 char_type const c = par_.getChar(pos);
429 // special case languages
430 std::string const & lang = orig_font.language()->lang();
431 bool const hebrew = lang == "hebrew";
432 bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" ||
435 // draw as many chars as we can
436 if ((!hebrew && !arabic)
437 || (hebrew && !Encodings::isComposeChar_hebrew(c))
438 || (arabic && !Encodings::isComposeChar_arabic(c))) {
439 paintChars(vpos, orig_font, hebrew, arabic);
441 paintHebrewComposeChar(vpos, orig_font);
443 paintArabicComposeChar(vpos, orig_font);
446 paintForeignMark(orig_x, orig_font);
450 void RowPainter::paintChangeBar()
452 pos_type const start = row_.pos();
453 pos_type end = row_.endpos();
455 if (par_.size() == end) {
456 // this is the last row of the paragraph;
457 // thus, we must also consider the imaginary end-of-par character
461 if (start == end || !par_.isChanged(start, end))
464 int const height = text_.isLastRow(pit_, row_)
468 pain_.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color::changebar);
472 void RowPainter::paintAppendix()
474 // only draw the appendix frame once (for the main text)
475 if (!par_.params().appendix() || !text_.isMainText(*bv_.buffer()))
478 int y = yo_ - row_.ascent();
480 if (par_.params().startOfAppendix())
481 y += 2 * defaultRowHeight();
483 pain_.line(1, y, 1, yo_ + row_.height(), Color::appendix);
484 pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color::appendix);
488 void RowPainter::paintDepthBar()
490 depth_type const depth = par_.getDepth();
495 depth_type prev_depth = 0;
496 if (!text_.isFirstRow(pit_, row_)) {
497 pit_type pit2 = pit_;
500 prev_depth = pars_[pit2].getDepth();
503 depth_type next_depth = 0;
504 if (!text_.isLastRow(pit_, row_)) {
505 pit_type pit2 = pit_;
506 if (row_.endpos() >= pars_[pit2].size())
508 next_depth = pars_[pit2].getDepth();
511 for (depth_type i = 1; i <= depth; ++i) {
512 int const w = nestMargin() / 5;
513 int x = int(xo_) + w * i;
514 // only consider the changebar space if we're drawing outermost text
515 if (text_.isMainText(*bv_.buffer()))
516 x += changebarMargin();
518 int const starty = yo_ - row_.ascent();
519 int const h = row_.height() - 1 - (i - next_depth - 1) * 3;
521 pain_.line(x, starty, x, starty + h, Color::depthbar);
524 pain_.fillRectangle(x, starty, w, 2, Color::depthbar);
526 pain_.fillRectangle(x, starty + h, w, 2, Color::depthbar);
531 int RowPainter::paintAppendixStart(int y)
534 pb_font.setColor(Color::appendix);
541 docstring const label = _("Appendix");
542 theFontMetrics(pb_font).rectText(label, w, a, d);
544 int const text_start = int(xo_ + (width_ - w) / 2);
545 int const text_end = text_start + w;
547 pain_.rectText(text_start, y + d, label, pb_font, Color::none, Color::none);
549 pain_.line(int(xo_ + 1), y, text_start, y, Color::appendix);
550 pain_.line(text_end, y, int(xo_ + width_ - 2), y, Color::appendix);
552 return 3 * defaultRowHeight();
556 void RowPainter::paintFirst()
558 ParagraphParameters const & parparams = par_.params();
562 // start of appendix?
563 if (parparams.startOfAppendix())
564 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
566 Buffer const & buffer = *bv_.buffer();
568 Layout_ptr const & layout = par_.layout();
570 if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
572 if (layout->latextype == LATEX_PARAGRAPH
573 && !par_.getDepth()) {
574 y_top += buffer.params().getDefSkip().inPixels(bv_);
576 Layout_ptr const & playout = pars_[pit_ - 1].layout();
577 if (playout->latextype == LATEX_PARAGRAPH
578 && !pars_[pit_ - 1].getDepth()) {
579 // is it right to use defskip here, too? (AS)
580 y_top += buffer.params().getDefSkip().inPixels(bv_);
586 bool const is_rtl = text_.isRTL(buffer, par_);
587 bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
588 //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
590 // should we print a label?
591 if (layout->labeltype >= LABEL_STATIC
592 && (layout->labeltype != LABEL_STATIC
593 || layout->latextype != LATEX_ENVIRONMENT
596 Font const font = getLabelFont();
597 FontMetrics const & fm = theFontMetrics(font);
599 docstring const str = par_.getLabelstring();
603 // this is special code for the chapter layout. This is
604 // printed in an extra row and has a pagebreak at
606 if (layout->counter == "chapter") {
607 double spacing_val = 1.0;
608 if (!parparams.spacing().isDefault()) {
609 spacing_val = parparams.spacing().getValue();
611 spacing_val = buffer.params().spacing().getValue();
614 int const labeladdon = int(fm.maxHeight() * layout->spacing.getValue() * spacing_val);
616 int const maxdesc = int(fm.maxDescent() * layout->spacing.getValue() * spacing_val)
617 + int(layout->parsep) * defaultRowHeight();
620 x = width_ - leftMargin() -
624 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
627 x = width_ - leftMargin()
628 + fm.width(layout->labelsep);
630 x = x_ - fm.width(layout->labelsep)
634 pain_.text(int(x), yo_, str, font);
638 // the labels at the top of an environment.
639 // More or less for bibliography
641 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
642 layout->labeltype == LABEL_BIBLIO ||
643 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
644 Font font = getLabelFont();
645 if (!par_.getLabelstring().empty()) {
646 docstring const str = par_.getLabelstring();
647 double spacing_val = 1.0;
648 if (!parparams.spacing().isDefault())
649 spacing_val = parparams.spacing().getValue();
651 spacing_val = buffer.params().spacing().getValue();
653 FontMetrics const & fm = theFontMetrics(font);
655 int const labeladdon = int(fm.maxHeight()
656 * layout->spacing.getValue() * spacing_val);
659 int(fm.maxDescent() * layout->spacing.getValue() * spacing_val
660 + (layout->labelbottomsep * defaultRowHeight()));
663 if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
666 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
667 x -= fm.width(str) / 2;
669 x = width_ - leftMargin() - fm.width(str);
671 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
677 void RowPainter::paintLast()
679 bool const is_rtl = text_.isRTL(*bv_.buffer(), par_);
680 int const endlabel = getEndLabel(pit_, text_.paragraphs());
682 // paint imaginary end-of-paragraph character
684 if (par_.isInserted(par_.size()) || par_.isDeleted(par_.size())) {
685 FontMetrics const & fm = theFontMetrics(bv_.buffer()->params().getFont());
686 int const length = fm.maxAscent() / 2;
687 Color::color col = par_.isInserted(par_.size()) ? Color::addedtext : Color::deletedtext;
689 pain_.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
690 Painter::line_solid, Painter::line_thick);
691 pain_.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1, yo_ + 2, col,
692 Painter::line_solid, Painter::line_thick);
699 case END_LABEL_FILLED_BOX: {
700 Font const font = getLabelFont();
701 FontMetrics const & fm = theFontMetrics(font);
702 int const size = int(0.75 * fm.maxAscent());
703 int const y = yo_ - size;
704 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
706 if (width_ - int(row_.width()) <= size)
707 x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
709 if (endlabel == END_LABEL_BOX)
710 pain_.rectangle(x, y, size, size, Color::eolmarker);
712 pain_.fillRectangle(x, y, size, size, Color::eolmarker);
716 case END_LABEL_STATIC: {
717 Font font = getLabelFont();
718 FontMetrics const & fm = theFontMetrics(font);
719 docstring const & str = par_.layout()->endlabelstring();
720 double const x = is_rtl ?
722 : - text_metrics_.rightMargin(pm_) - row_.width();
723 pain_.text(int(x), yo_, str, font);
727 case END_LABEL_NO_LABEL:
733 void RowPainter::paintText()
735 pos_type const end = row_.endpos();
736 // Spaces at logical line breaks in bidi text must be skipped during
737 // painting. However, they may appear visually in the middle
738 // of a row; they must be skipped, wherever they are...
739 // * logically "abc_[HEBREW_\nHEBREW]"
740 // * visually "abc_[_WERBEH\nWERBEH]"
741 pos_type skipped_sep_vpos = -1;
742 pos_type body_pos = par_.beginOfBody();
744 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
748 Layout_ptr const & layout = par_.layout();
750 bool running_strikeout = false;
751 bool is_struckout = false;
752 int last_strikeout_x = 0;
754 // Use font span to speed things up, see below
757 Buffer const & buffer = *bv_.buffer();
759 // If the last logical character is a separator, don't paint it, unless
760 // it's in the last row of a paragraph; see skipped_sep_vpos declaration
761 if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
762 skipped_sep_vpos = bidi_.log2vis(end - 1);
764 for (pos_type vpos = row_.pos(); vpos < end; ) {
765 if (x_ > bv_.workWidth())
768 // Skip the separator at the logical end of the row
769 if (vpos == skipped_sep_vpos) {
774 pos_type const pos = bidi_.vis2log(vpos);
776 if (pos >= par_.size()) {
781 // Use font span to speed things up, see above
782 if (vpos < font_span.first || vpos > font_span.last) {
783 font_span = par_.fontSpan(vpos);
784 font = text_.getFont(buffer, par_, vpos);
787 const int width_pos =
788 text_.singleWidth(par_, pos, par_.getChar(pos), font);
790 if (x_ + width_pos < 0) {
796 is_struckout = par_.isDeleted(pos);
798 if (is_struckout && !running_strikeout) {
799 running_strikeout = true;
800 last_strikeout_x = int(x_);
803 bool const highly_editable_inset = par_.isInset(pos)
804 && isHighlyEditableInset(par_.getInset(pos));
806 // If we reach the end of a struck out range, paint it.
807 // We also don't paint across things like tables
808 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
809 // Calculate 1/3 height of the buffer's default font
810 FontMetrics const & fm
811 = theFontMetrics(bv_.buffer()->params().getFont());
812 int const middle = yo_ - fm.maxAscent() / 3;
813 pain_.line(last_strikeout_x, middle, int(x_), middle,
814 Color::deletedtext, Painter::line_solid, Painter::line_thin);
815 running_strikeout = false;
818 if (body_pos > 0 && pos == body_pos - 1) {
819 int const lwidth = theFontMetrics(getLabelFont())
820 .width(layout->labelsep);
822 x_ += label_hfill_ + lwidth - width_pos;
825 if (par_.isHfill(pos)) {
829 int const y1 = y0 - defaultRowHeight() / 2;
831 pain_.line(int(x_), y1, int(x_), y0, Color::added_space);
833 if (par_.hfillExpansion(row_, pos)) {
834 int const y2 = (y0 + y1) / 2;
836 if (pos >= body_pos) {
837 pain_.line(int(x_), y2, int(x_ + hfill_), y2,
839 Painter::line_onoffdash);
842 pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
844 Painter::line_onoffdash);
847 pain_.line(int(x_), y1, int(x_), y0, Color::added_space);
851 } else if (par_.isSeparator(pos)) {
852 Font orig_font = text_.getFont(*bv_.buffer(), par_, pos);
853 double const orig_x = x_;
858 paintForeignMark(orig_x, orig_font);
864 // if we reach the end of a struck out range, paint it
865 if (running_strikeout) {
866 // calculate 1/3 height of the buffer's default font
867 FontMetrics const & fm
868 = theFontMetrics(bv_.buffer()->params().getFont());
869 int const middle = yo_ - fm.maxAscent() / 3;
870 pain_.line(last_strikeout_x, middle, int(x_), middle,
871 Color::deletedtext, Painter::line_solid, Painter::line_thin);
872 running_strikeout = false;
877 bool CursorOnRow(PainterInfo & pi, pit_type const pit,
878 RowList::const_iterator rit, Text const & text)
880 // Is there a cursor on this row (or inside inset on row)
881 Cursor & cur = pi.base.bv->cursor();
882 for (size_type d = 0; d < cur.depth(); ++d) {
883 CursorSlice const & sl = cur[d];
884 if (sl.text() == &text
886 && sl.pos() >= rit->pos()
887 && sl.pos() <= rit->endpos())
894 bool innerCursorOnRow(PainterInfo & pi, pit_type pit,
895 RowList::const_iterator rit, Text const & text)
897 // Is there a cursor inside an inset on this row, and is this inset
898 // the only "character" on this row
899 Cursor & cur = pi.base.bv->cursor();
900 if (rit->pos() + 1 != rit->endpos())
902 for (size_type d = 0; d < cur.depth(); d++) {
903 CursorSlice const & sl = cur[d];
904 if (sl.text() == &text
906 && sl.pos() == rit->pos())
907 return d < cur.depth() - 1;
913 // FIXME: once wide() is obsolete, remove this as well!
914 bool inNarrowInset(PainterInfo & pi)
916 // check whether the current inset is nested in a non-wide inset
917 Cursor & cur = pi.base.bv->cursor();
918 Inset const * cur_in = &cur.inset();
919 // check all higher nested insets
920 for (size_type i = 1; i < cur.depth(); ++i) {
921 Inset * const in = &cur[i].inset();
923 // we reached the level of the current inset, so stop
926 if (in->hasFixedWidth())
929 const_cast<InsetText *>(in->asTextInset());
931 // OK, we are in a non-wide() inset
940 (PainterInfo & pi, Text const & text, pit_type pit, int x, int y,
943 // lyxerr << " paintPar: pit: " << pit << " at y: " << y << endl;
944 int const ww = pi.base.bv->workHeight();
946 pi.base.bv->coordCache().parPos()[&text][pit] = Point(x, y);
948 Paragraph const & par = text.paragraphs()[pit];
949 ParagraphMetrics const & pm = pi.base.bv->parMetrics(&text, pit);
950 if (pm.rows().empty())
953 RowList::const_iterator const rb = pm.rows().begin();
954 RowList::const_iterator const re = pm.rows().end();
960 for (RowList::const_iterator rit = rb; rit != re; ++rit, ++rowno) {
962 // Allow setting of refreshInside for nested insets in
964 bool tmp = refreshInside;
966 // Row signature; has row changed since last paint?
967 bool row_has_changed = pm.rowChangeStatus()[rowno];
969 bool cursor_on_row = CursorOnRow(pi, pit, rit, text);
970 bool in_inset_alone_on_row =
971 innerCursorOnRow(pi, pit, rit, text);
973 (par.getAlign() == LYX_ALIGN_LEFT ||
974 par.getAlign() == LYX_ALIGN_BLOCK);
975 bool inNarrowIns = inNarrowInset(pi);
977 // If this is the only object on the row, we can make it wide
979 // FIXME: there is a const_cast here because paintPar() is not supposed
980 // to touch the paragraph contents. So either we move this "wide"
981 // property out of InsetText or we localize the feature to the painting
983 // JSpitzm: We should aim at removing wide() altogether while retaining
984 // typing speed within insets.
985 for (pos_type i = rit->pos() ; i != rit->endpos(); ++i) {
986 Inset const * const in = par.getInset(i);
988 InsetText * t = const_cast<InsetText *>(in->asTextInset());
990 t->setWide(in_inset_alone_on_row
996 // If selection is on, the current row signature differs
997 // from cache, or cursor is inside an inset _on this row_,
998 // then paint the row
999 if (repaintAll || row_has_changed || cursor_on_row) {
1000 bool const inside = (y + rit->descent() >= 0
1001 && y - rit->ascent() < ww);
1002 // it is not needed to draw on screen if we are not inside.
1003 pi.pain.setDrawingEnabled(inside);
1004 RowPainter rp(pi, text, pit, *rit, bidi, x, y);
1005 // Clear background of this row
1006 // (if paragraph background was not cleared)
1008 (!(in_inset_alone_on_row && leftEdgeFixed && !inNarrowIns)
1009 || row_has_changed)) {
1010 pi.pain.fillRectangle(x, y - rit->ascent(),
1011 rp.maxWidth(), rit->height(),
1012 text.backgroundColor());
1013 // If outer row has changed, force nested
1014 // insets to repaint completely
1015 if (row_has_changed)
1016 refreshInside = true;
1019 // Instrumentation for testing row cache (see also
1021 if (lyxerr.debugging(Debug::PAINTING)) {
1022 if (text.isMainText(*pi.base.bv->buffer()))
1023 LYXERR(Debug::PAINTING) << "#";
1025 LYXERR(Debug::PAINTING) << "[" <<
1026 repaintAll << row_has_changed <<
1027 cursor_on_row << "]";
1031 rp.paintChangeBar();
1038 y += rit->descent();
1039 // Restore, see above
1040 refreshInside = tmp;
1042 // Re-enable screen drawing for future use of the painter.
1043 pi.pain.setDrawingEnabled(true);
1045 LYXERR(Debug::PAINTING) << "." << endl;
1051 void paintText(BufferView & bv,
1054 BOOST_ASSERT(bv.buffer());
1055 Buffer const & buffer = *bv.buffer();
1056 Text & text = buffer.text();
1057 bool const select = bv.cursor().selection();
1058 ViewMetricsInfo const & vi = bv.viewMetricsInfo();
1060 PainterInfo pi(const_cast<BufferView *>(&bv), pain);
1061 // Should the whole screen, including insets, be refreshed?
1062 // FIXME: We should also distinguish DecorationUpdate to avoid text
1063 // drawing if possible. This is not possible to do easily right now
1064 // because of the single backing pixmap.
1065 bool repaintAll = select || vi.update_strategy != SingleParUpdate;
1068 // Clear background (if not delegated to rows)
1069 pain.fillRectangle(0, vi.y1, bv.workWidth(), vi.y2 - vi.y1,
1070 text.backgroundColor());
1073 text.drawSelection(pi, 0, 0);
1078 for (pit_type pit = vi.p1; pit <= vi.p2; ++pit) {
1079 refreshInside = repaintAll;
1080 ParagraphMetrics const & pm = bv.parMetrics(&text, pit);
1082 paintPar(pi, text, pit, 0, yy, repaintAll);
1086 // and grey out above (should not happen later)
1087 // lyxerr << "par ascent: " << text.getPar(vi.p1).ascent() << endl;
1088 if (vi.y1 > 0 && vi.update_strategy == FullScreenUpdate)
1089 pain.fillRectangle(0, 0, bv.workWidth(), vi.y1, Color::bottomarea);
1091 // and possibly grey out below
1092 // lyxerr << "par descent: " << text.getPar(vi.p1).ascent() << endl;
1093 if (vi.y2 < bv.workHeight() && vi.update_strategy == FullScreenUpdate)
1094 pain.fillRectangle(0, vi.y2, bv.workWidth(), bv.workHeight() - vi.y2, Color::bottomarea);
1098 void paintTextInset(Text const & text, PainterInfo & pi, int x, int y)
1100 // lyxerr << " paintTextInset: y: " << y << endl;
1102 y -= pi.base.bv->parMetrics(&text, 0).ascent();
1103 // This flag cannot be set from within same inset:
1104 bool repaintAll = refreshInside;
1105 for (int pit = 0; pit < int(text.paragraphs().size()); ++pit) {
1106 ParagraphMetrics const & pmi
1107 = pi.base.bv->parMetrics(&text, pit);
1109 paintPar(pi, text, pit, x, y, repaintAll);