]> git.lyx.org Git - lyx.git/blob - src/rowpainter.C
012cdc4dad7559a7b6c0cbbb5e796a1fc01a26fb
[lyx.git] / src / rowpainter.C
1 /**
2  * \file rowpainter.C
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
14 #include "rowpainter.h"
15
16 #include "buffer.h"
17 #include "coordcache.h"
18 #include "cursor.h"
19 #include "debug.h"
20 #include "bufferparams.h"
21 #include "BufferView.h"
22 #include "encoding.h"
23 #include "gettext.h"
24 #include "language.h"
25 #include "LColor.h"
26 #include "lyxrc.h"
27 #include "lyxrow.h"
28 #include "lyxrow_funcs.h"
29 #include "metricsinfo.h"
30 #include "paragraph.h"
31 #include "paragraph_funcs.h"
32 #include "ParagraphParameters.h"
33 #include "vspace.h"
34
35 #include "frontends/font_metrics.h"
36 #include "frontends/nullpainter.h"
37 #include "frontends/Painter.h"
38
39 #include "insets/insettext.h"
40
41 #include "support/textutils.h"
42
43 #include <boost/crc.hpp>
44
45 using lyx::docstring;
46 using lyx::frontend::Painter;
47 using lyx::frontend::NullPainter;
48 using lyx::char_type;
49 using lyx::pos_type;
50 using lyx::pit_type;
51
52 using std::endl;
53 using std::max;
54 using std::min;
55 using std::string;
56
57
58 namespace {
59
60 /// Flag: do a full redraw of inside text of inset
61 /// Working variable indicating a full screen refresh
62 bool refreshInside;
63
64 /**
65  * A class used for painting an individual row of text.
66  */
67 class RowPainter {
68 public:
69         /// initialise and run painter
70         RowPainter(PainterInfo & pi, LyXText const & text,
71                 pit_type pit, Row const & row, int x, int y);
72
73         // paint various parts
74         void paintAppendix();
75         void paintDepthBar();
76         void paintChangeBar();
77         void paintFirst();
78         void paintLast();
79         void paintText();
80
81 private:
82         void paintForeignMark(double orig_x, LyXFont const & font, int desc = 0);
83         void paintHebrewComposeChar(lyx::pos_type & vpos, LyXFont const & font);
84         void paintArabicComposeChar(lyx::pos_type & vpos, LyXFont const & font);
85         void paintChars(lyx::pos_type & vpos, LyXFont font,
86                         bool hebrew, bool arabic);
87         int paintAppendixStart(int y);
88         void paintFromPos(lyx::pos_type & vpos);
89         void paintInset(lyx::pos_type const pos, LyXFont const & font);
90
91         /// return left margin
92         int leftMargin() const;
93
94         /// return the label font for this row
95         LyXFont const getLabelFont() const;
96
97         /// bufferview to paint on
98         BufferView const & bv_;
99
100         /// Painter to use
101         Painter & pain_;
102
103         /// LyXText for the row
104         LyXText const & text_;
105         ParagraphList const & pars_;
106
107         /// The row to paint
108         Row const & row_;
109
110         /// Row's paragraph
111         pit_type const pit_;
112         Paragraph const & par_;
113
114         /// is row erased? (change tracking)
115         bool erased_;
116
117         // Looks ugly - is
118         double const xo_;
119         int const yo_;    // current baseline
120         double x_;
121         int width_;
122         double separator_;
123         double hfill_;
124         double label_hfill_;
125 };
126
127
128 RowPainter::RowPainter(PainterInfo & pi,
129         LyXText const & text, pit_type pit, Row const & row, int x, int y)
130         : bv_(*pi.base.bv), pain_(pi.pain), text_(text), pars_(text.paragraphs()),
131           row_(row), pit_(pit), par_(text.paragraphs()[pit]),
132           erased_(pi.erased_),
133           xo_(x), yo_(y), width_(text_.width())
134 {
135         RowMetrics m = text_.computeRowMetrics(pit, row_);
136         x_ = m.x + xo_;
137
138         //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
139         //row_.dump();
140
141         separator_ = m.separator;
142         hfill_ = m.hfill;
143         label_hfill_ = m.label_hfill;
144
145         BOOST_ASSERT(pit >= 0);
146         BOOST_ASSERT(pit < int(text.paragraphs().size()));
147 }
148
149
150 LyXFont const RowPainter::getLabelFont() const
151 {
152         return text_.getLabelFont(par_);
153 }
154
155
156 int RowPainter::leftMargin() const
157 {
158         return text_.leftMargin(pit_, row_.pos());
159 }
160
161
162 void RowPainter::paintInset(pos_type const pos, LyXFont const & font)
163 {
164         InsetBase const * inset = par_.getInset(pos);
165         BOOST_ASSERT(inset);
166         PainterInfo pi(const_cast<BufferView *>(&bv_), pain_);
167         // FIXME: We should always use font, see documentation of
168         // noFontChange() in insetbase.h.
169         pi.base.font = inset->noFontChange() ?
170                 bv_.buffer()->params().getFont() :
171                 font;
172         pi.ltr_pos = (text_.bidi.level(pos) % 2 == 0);
173         pi.erased_ = erased_ || isDeletedText(par_, pos);
174         theCoords.insets().add(inset, int(x_), yo_);
175         InsetText const * const in = inset->asTextInset();
176         // non-wide insets are painted completely. Recursive
177         bool tmp = refreshInside;
178         if (!in || !in->Wide()) {
179                 refreshInside = true;
180                 lyxerr[Debug::PAINTING] << endl << "Paint inset fully" << endl;
181         }
182         if (refreshInside)
183                 inset->drawSelection(pi, int(x_), yo_);
184         inset->draw(pi, int(x_), yo_);
185         refreshInside = tmp;
186         x_ += inset->width();
187 }
188
189
190 void RowPainter::paintHebrewComposeChar(pos_type & vpos, LyXFont const & font)
191 {
192         pos_type pos = text_.bidi.vis2log(vpos);
193
194         string str;
195
196         // first char
197         char_type c = par_.getChar(pos);
198         str += c;
199         ++vpos;
200
201         int const width = font_metrics::width(c, font);
202         int dx = 0;
203
204         for (pos_type i = pos - 1; i >= 0; --i) {
205                 c = par_.getChar(i);
206                 if (!Encodings::isComposeChar_hebrew(c)) {
207                         if (isPrintableNonspace(c)) {
208                                 int const width2 =
209                                         text_.singleWidth(par_, i, c, text_.getFont(par_, i));
210                                 // dalet / resh
211                                 dx = (c == 'ø' || c == 'ã')
212                                         ? width2 - width
213                                         : (width2 - width) / 2;
214                         }
215                         break;
216                 }
217         }
218
219         // Draw nikud
220         docstring dstr(str.begin(), str.end());
221         pain_.text(int(x_) + dx, yo_, dstr, font);
222 }
223
224
225 void RowPainter::paintArabicComposeChar(pos_type & vpos, LyXFont const & font)
226 {
227         pos_type pos = text_.bidi.vis2log(vpos);
228         string str;
229
230         // first char
231         char_type c = par_.getChar(pos);
232         c = par_.transformChar(c, pos);
233         str += c;
234         ++vpos;
235
236         int const width = font_metrics::width(c, font);
237         int dx = 0;
238
239         for (pos_type i = pos - 1; i >= 0; --i) {
240                 c = par_.getChar(i);
241                 if (!Encodings::isComposeChar_arabic(c)) {
242                         if (isPrintableNonspace(c)) {
243                                 int const width2 =
244                                         text_.singleWidth(par_, i, c, text_.getFont(par_, i));
245                                 dx = (width2 - width) / 2;
246                         }
247                         break;
248                 }
249         }
250         // Draw nikud
251         docstring dstr(str.begin(), str.end());
252         pain_.text(int(x_) + dx, yo_, dstr, font);
253 }
254
255
256 void RowPainter::paintChars(pos_type & vpos, LyXFont font,
257                             bool hebrew, bool arabic)
258 {
259         pos_type pos = text_.bidi.vis2log(vpos);
260         pos_type const end = row_.endpos();
261         FontSpan const font_span = par_.fontSpan(pos);
262         Change::Type const prev_change = par_.lookupChange(pos).type;
263
264         // first character
265 #if 0
266         string str;
267         str += par_.getChar(pos);
268 #else
269         std::vector<char_type> str;
270         str.push_back(par_.getChar(pos));
271 #endif
272         if (arabic) {
273                 char_type c = str[0];
274                 str[0] = par_.transformChar(c, pos);
275         }
276
277         // collect as much similar chars as we can
278         for (++vpos ; vpos < end ; ++vpos) {
279                 pos = text_.bidi.vis2log(vpos);
280                 if (pos < font_span.first || pos > font_span.last)
281                         break;
282
283                 if (prev_change != par_.lookupChange(pos))
284                         break;
285
286                 char_type c = par_.getChar(pos);
287
288                 if (!isPrintableNonspace(c))
289                         break;
290
291                 if (arabic && Encodings::isComposeChar_arabic(c))
292                         break;
293
294                 if (hebrew && Encodings::isComposeChar_hebrew(c))
295                         break;
296
297                 if (arabic)
298                         c = par_.transformChar(c, pos);
299
300 #if 0
301                 str += c;
302 #else
303                 str.push_back(c);
304 #endif
305         }
306
307         if (prev_change == Change::DELETED)
308                 font.setColor(LColor::strikeout);
309         else if (prev_change == Change::INSERTED)
310                 font.setColor(LColor::newtext);
311
312         // Draw text and set the new x position
313         //lyxerr << "paint row: yo_ " << yo_ << "\n";
314 #if 0
315         pain_.text(int(x_), yo_, str, font);
316         x_ += font_metrics::width(str, font);
317 #else
318         pain_.text(int(x_), yo_, &str[0], str.size(), font);
319         x_ += font_metrics::width(&str[0], str.size(), font);
320 #endif
321 }
322
323
324 void RowPainter::paintForeignMark(double orig_x, LyXFont const & font, int desc)
325 {
326         if (!lyxrc.mark_foreign_language)
327                 return;
328         if (font.language() == latex_language)
329                 return;
330         if (font.language() == bv_.buffer()->params().language)
331                 return;
332
333         int const y = yo_ + 1 + desc;
334         pain_.line(int(orig_x), y, int(x_), y, LColor::language);
335 }
336
337
338 void RowPainter::paintFromPos(pos_type & vpos)
339 {
340         pos_type const pos = text_.bidi.vis2log(vpos);
341         LyXFont orig_font = text_.getFont(par_, pos);
342
343         double const orig_x = x_;
344
345         if (par_.isInset(pos)) {
346                 paintInset(pos, orig_font);
347                 ++vpos;
348                 paintForeignMark(orig_x, orig_font,
349                         par_.getInset(pos)->descent());
350                 return;
351         }
352
353         // usual characters, no insets
354         char_type const c = par_.getChar(pos);
355
356         // special case languages
357         std::string const & lang = orig_font.language()->lang();
358         bool const hebrew = lang == "hebrew";
359         bool const arabic = lang == "arabic" &&
360                 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
361                 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
362
363         // draw as many chars as we can
364         if ((!hebrew && !arabic)
365                 || (hebrew && !Encodings::isComposeChar_hebrew(c))
366                 || (arabic && !Encodings::isComposeChar_arabic(c))) {
367                 paintChars(vpos, orig_font, hebrew, arabic);
368         } else if (hebrew) {
369                 paintHebrewComposeChar(vpos, orig_font);
370         } else if (arabic) {
371                 paintArabicComposeChar(vpos, orig_font);
372         }
373
374         paintForeignMark(orig_x, orig_font);
375 }
376
377
378 void RowPainter::paintChangeBar()
379 {
380         pos_type const start = row_.pos();
381         pos_type const end = row_.endpos();
382
383         if (start == end || !par_.isChanged(start, end))
384                 return;
385
386         int const height = text_.isLastRow(pit_, row_)
387                 ? row_.ascent()
388                 : row_.height();
389
390         pain_.fillRectangle(4, yo_ - row_.ascent(), 5, height, LColor::changebar);
391 }
392
393
394 void RowPainter::paintAppendix()
395 {
396         if (!par_.params().appendix())
397                 return;
398
399         int y = yo_ - row_.ascent();
400
401         if (par_.params().startOfAppendix())
402                 y += 2 * defaultRowHeight();
403
404         pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
405         pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), LColor::appendix);
406 }
407
408
409 void RowPainter::paintDepthBar()
410 {
411         Paragraph::depth_type const depth = par_.getDepth();
412
413         if (depth <= 0)
414                 return;
415
416         Paragraph::depth_type prev_depth = 0;
417         if (!text_.isFirstRow(pit_, row_)) {
418                 pit_type pit2 = pit_;
419                 if (row_.pos() == 0)
420                         --pit2;
421                 prev_depth = pars_[pit2].getDepth();
422         }
423
424         Paragraph::depth_type next_depth = 0;
425         if (!text_.isLastRow(pit_, row_)) {
426                 pit_type pit2 = pit_;
427                 if (row_.endpos() >= pars_[pit2].size())
428                         ++pit2;
429                 next_depth = pars_[pit2].getDepth();
430         }
431
432         for (Paragraph::depth_type i = 1; i <= depth; ++i) {
433                 int const w = nestMargin() / 5;
434                 int x = int(xo_) + w * i;
435                 // only consider the changebar space if we're drawing outermost text
436                 if (text_.isMainText())
437                         x += changebarMargin();
438
439                 int const starty = yo_ - row_.ascent();
440                 int const h =  row_.height() - 1 - (i - next_depth - 1) * 3;
441
442                 pain_.line(x, starty, x, starty + h, LColor::depthbar);
443
444                 if (i > prev_depth)
445                         pain_.fillRectangle(x, starty, w, 2, LColor::depthbar);
446                 if (i > next_depth)
447                         pain_.fillRectangle(x, starty + h, w, 2, LColor::depthbar);
448         }
449 }
450
451
452 int RowPainter::paintAppendixStart(int y)
453 {
454         LyXFont pb_font;
455         pb_font.setColor(LColor::appendix);
456         pb_font.decSize();
457
458         string const label = lyx::to_utf8(_("Appendix"));
459         int w = 0;
460         int a = 0;
461         int d = 0;
462         docstring dlab(label.begin(), label.end());
463         font_metrics::rectText(dlab, pb_font, w, a, d);
464
465         int const text_start = int(xo_ + (width_ - w) / 2);
466         int const text_end = text_start + w;
467
468         pain_.rectText(text_start, y + d, dlab, pb_font, LColor::none, LColor::none);
469
470         pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
471         pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
472
473         return 3 * defaultRowHeight();
474 }
475
476
477 void RowPainter::paintFirst()
478 {
479         ParagraphParameters const & parparams = par_.params();
480
481         int y_top = 0;
482
483         // start of appendix?
484         if (parparams.startOfAppendix())
485                 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
486
487         Buffer const & buffer = *bv_.buffer();
488
489         LyXLayout_ptr const & layout = par_.layout();
490
491         if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
492                 if (pit_ != 0) {
493                         if (layout->latextype == LATEX_PARAGRAPH
494                                 && !par_.getDepth()) {
495                                 y_top += buffer.params().getDefSkip().inPixels(bv_);
496                         } else {
497                                 LyXLayout_ptr const & playout = pars_[pit_ - 1].layout();
498                                 if (playout->latextype == LATEX_PARAGRAPH
499                                         && !pars_[pit_ - 1].getDepth()) {
500                                         // is it right to use defskip here, too? (AS)
501                                         y_top += buffer.params().getDefSkip().inPixels(bv_);
502                                 }
503                         }
504                 }
505         }
506
507         bool const is_rtl = text_.isRTL(par_);
508         bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
509         //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
510
511         // should we print a label?
512         if (layout->labeltype >= LABEL_STATIC
513             && (layout->labeltype != LABEL_STATIC
514                       || layout->latextype != LATEX_ENVIRONMENT
515                       || is_seq)) {
516
517                 LyXFont const font = getLabelFont();
518                 string const str = par_.getLabelstring();
519                 if (!str.empty()) {
520                         double x = x_;
521                         docstring dstr(str.begin(), str.end());
522
523                         // this is special code for the chapter layout. This is
524                         // printed in an extra row and has a pagebreak at
525                         // the top.
526                         if (layout->counter == "chapter") {
527                                 double spacing_val = 1.0;
528                                 if (!parparams.spacing().isDefault()) {
529                                         spacing_val = parparams.spacing().getValue();
530                                 } else {
531                                         spacing_val = buffer.params().spacing().getValue();
532                                 }
533
534                                 int const labeladdon = int(font_metrics::maxHeight(font) * layout->spacing.getValue() * spacing_val);
535
536                                 int const maxdesc = int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
537                                         + int(layout->parsep) * defaultRowHeight();
538
539                                 if (is_rtl) {
540                                         x = width_ - leftMargin() -
541                                                 font_metrics::width(dstr, font);
542                                 }
543
544                                 pain_.text(int(x), yo_ - maxdesc - labeladdon, dstr, font);
545                         } else {
546                                 string lab = layout->labelsep;
547                                 docstring dlab(lab.begin(), lab.end());
548                                 if (is_rtl) {
549                                         x = width_ - leftMargin()
550                                                 + font_metrics::width(dlab, font);
551                                 } else {
552                                         x = x_ - font_metrics::width(dlab, font)
553                                                 - font_metrics::width(dstr, font);
554                                 }
555
556                                 pain_.text(int(x), yo_, dstr, font);
557                         }
558                 }
559
560         // the labels at the top of an environment.
561         // More or less for bibliography
562         } else if (is_seq &&
563                 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
564                 layout->labeltype == LABEL_BIBLIO ||
565                 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
566                 LyXFont font = getLabelFont();
567                 if (!par_.getLabelstring().empty()) {
568                         string const str = par_.getLabelstring();
569                         docstring dstr(str.begin(), str.end());
570                         double spacing_val = 1.0;
571                         if (!parparams.spacing().isDefault())
572                                 spacing_val = parparams.spacing().getValue();
573                         else
574                                 spacing_val = buffer.params().spacing().getValue();
575
576                         int const labeladdon = int(font_metrics::maxHeight(font) * layout->spacing.getValue() * spacing_val);
577
578                         int maxdesc =
579                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
580                                 + (layout->labelbottomsep * defaultRowHeight()));
581
582                         double x = x_;
583                         if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
584                                 if (is_rtl)
585                                         x = leftMargin();
586                                 x += (width_ - text_.rightMargin(par_) - leftMargin()) / 2;
587                                 x -= font_metrics::width(dstr, font) / 2;
588                         } else if (is_rtl) {
589                                 x = width_ - leftMargin() -
590                                         font_metrics::width(dstr, font);
591                         }
592                         pain_.text(int(x), yo_ - maxdesc - labeladdon, dstr, font);
593                 }
594         }
595 }
596
597
598 void RowPainter::paintLast()
599 {
600         bool const is_rtl = text_.isRTL(par_);
601         int const endlabel = getEndLabel(pit_, text_.paragraphs());
602
603         // draw an endlabel
604         switch (endlabel) {
605         case END_LABEL_BOX:
606         case END_LABEL_FILLED_BOX: {
607                 LyXFont const font = getLabelFont();
608                 int const size = int(0.75 * font_metrics::maxAscent(font));
609                 int const y = yo_ - size;
610                 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
611
612                 if (width_ - int(row_.width()) <= size)
613                         x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
614
615                 if (endlabel == END_LABEL_BOX)
616                         pain_.rectangle(x, y, size, size, LColor::eolmarker);
617                 else
618                         pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
619                 break;
620         }
621
622         case END_LABEL_STATIC: {
623                 LyXFont font = getLabelFont();
624                 string const & str = par_.layout()->endlabelstring();
625                 docstring dstr(str.begin(), str.end());
626                 double const x = is_rtl ?
627                         x_ - font_metrics::width(dstr, font)
628                         : - text_.rightMargin(par_) - row_.width();
629                 pain_.text(int(x), yo_, dstr, font);
630                 break;
631         }
632
633         case END_LABEL_NO_LABEL:
634                 break;
635         }
636 }
637
638
639 void RowPainter::paintText()
640 {
641         pos_type const end = row_.endpos();
642         pos_type body_pos = par_.beginOfBody();
643         if (body_pos > 0 &&
644                 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
645                 body_pos = 0;
646         }
647
648         LyXLayout_ptr const & layout = par_.layout();
649
650         bool running_strikeout = false;
651         bool is_struckout = false;
652         int last_strikeout_x = 0;
653
654         // Use font span to speed things up, see below
655         FontSpan font_span;
656         LyXFont font;
657
658         for (pos_type vpos = row_.pos(); vpos < end; ) {
659                 if (x_ > bv_.workWidth())
660                         break;
661
662                 pos_type const pos = text_.bidi.vis2log(vpos);
663
664                 if (pos >= par_.size()) {
665                         ++vpos;
666                         continue;
667                 }
668
669                 // Use font span to speed things up, see above
670                 if (vpos < font_span.first || vpos > font_span.last) {
671                         font_span = par_.fontSpan(vpos);
672                         font = text_.getFont(par_, vpos);
673                 }
674
675                 const int width_pos =
676                         text_.singleWidth(par_, pos, par_.getChar(pos), font);
677
678                 if (x_ + width_pos < 0) {
679                         x_ += width_pos;
680                         ++vpos;
681                         continue;
682                 }
683
684                 is_struckout = isDeletedText(par_, pos);
685
686                 if (is_struckout && !running_strikeout) {
687                         running_strikeout = true;
688                         last_strikeout_x = int(x_);
689                 }
690
691                 bool const highly_editable_inset = par_.isInset(pos)
692                         && isHighlyEditableInset(par_.getInset(pos));
693
694                 // If we reach the end of a struck out range, paint it.
695                 // We also don't paint across things like tables
696                 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
697                         // Calculate 1/3 height of the buffer's default font
698                         int const middle =
699                                 yo_ -
700                                 font_metrics::maxAscent(bv_.buffer()->params().getFont()) / 3;
701                         pain_.line(last_strikeout_x, middle, int(x_), middle,
702                                 LColor::strikeout, Painter::line_solid, Painter::line_thin);
703                         running_strikeout = false;
704                 }
705
706                 if (body_pos > 0 && pos == body_pos - 1) {
707                         string lab = layout->labelsep;
708                         docstring dlab(lab.begin(), lab.end());
709                         int const lwidth = font_metrics::width(dlab,
710                                 getLabelFont());
711
712                         x_ += label_hfill_ + lwidth - width_pos;
713                 }
714
715                 if (par_.isHfill(pos)) {
716                         x_ += 1;
717
718                         int const y0 = yo_;
719                         int const y1 = y0 - defaultRowHeight() / 2;
720
721                         pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
722
723                         if (hfillExpansion(par_, row_, pos)) {
724                                 int const y2 = (y0 + y1) / 2;
725
726                                 if (pos >= body_pos) {
727                                         pain_.line(int(x_), y2, int(x_ + hfill_), y2,
728                                                   LColor::added_space,
729                                                   Painter::line_onoffdash);
730                                         x_ += hfill_;
731                                 } else {
732                                         pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
733                                                   LColor::added_space,
734                                                   Painter::line_onoffdash);
735                                         x_ += label_hfill_;
736                                 }
737                                 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
738                         }
739                         x_ += 2;
740                         ++vpos;
741                 } else if (par_.isSeparator(pos)) {
742                         x_ += width_pos;
743                         if (pos >= body_pos)
744                                 x_ += separator_;
745                         ++vpos;
746                 } else {
747                         paintFromPos(vpos);
748                 }
749         }
750
751         // if we reach the end of a struck out range, paint it
752         if (running_strikeout) {
753                 // calculate 1/3 height of the buffer's default font
754                 int const middle =
755                         yo_ -
756                         font_metrics::maxAscent(bv_.buffer()->params().getFont()) / 3;
757                 pain_.line(last_strikeout_x, middle, int(x_), middle,
758                         LColor::strikeout, Painter::line_solid, Painter::line_thin);
759                 running_strikeout = false;
760         }
761 }
762
763
764 lyx::size_type calculateRowSignature(Row const & row, Paragraph const & par,
765         int x, int y)
766 {
767         boost::crc_32_type crc;
768         for (lyx::pos_type i = row.pos(); i < row.endpos(); ++i) {
769                 const unsigned char b[] = { par.getChar(i) };
770                 crc.process_bytes(b, 1);
771         }
772         const unsigned char b[] = { x, y, row.width() };
773         crc.process_bytes(b, 3);
774         return crc.checksum();
775 }
776
777
778 bool CursorOnRow(PainterInfo & pi, pit_type const pit,
779         RowList::const_iterator rit, LyXText const & text)
780 {
781         // Is there a cursor on this row (or inside inset on row)
782         LCursor & cur = pi.base.bv->cursor();
783         for (lyx::size_type d = 0; d < cur.depth(); d++) {
784                 CursorSlice const & sl = cur[d];
785                 if (sl.text() == &text
786                     && sl.pit() == pit
787                     && sl.pos() >= rit->pos()
788                     && sl.pos() <= rit->endpos())
789                         return true;
790         }
791         return false;
792 }
793
794
795 bool innerCursorOnRow(PainterInfo & pi, pit_type pit,
796         RowList::const_iterator rit, LyXText const & text)
797 {
798         // Is there a cursor inside an inset on this row, and is this inset
799         // the only "character" on this row
800         LCursor & cur = pi.base.bv->cursor();
801         if (rit->pos() + 1 != rit->endpos())
802                 return false;
803         for (lyx::size_type d = 0; d < cur.depth(); d++) {
804                 CursorSlice const & sl = cur[d];
805                 if (sl.text() == &text
806                     && sl.pit() == pit
807                     && sl.pos() == rit->pos())
808                         return d < cur.depth() - 1;
809         }
810         return false;
811 }
812
813
814 void paintPar
815         (PainterInfo & pi, LyXText const & text, pit_type pit, int x, int y,
816          bool repaintAll)
817 {
818 //      lyxerr << "  paintPar: pit: " << pit << " at y: " << y << endl;
819         static NullPainter nop;
820         static PainterInfo nullpi(pi.base.bv, nop);
821         int const ww = pi.base.bv->workHeight();
822
823         theCoords.parPos()[&text][pit] = Point(x, y);
824
825         Paragraph const & par = text.paragraphs()[pit];
826         if (par.rows().empty())
827                 return;
828
829         RowList::const_iterator const rb = par.rows().begin();
830         RowList::const_iterator const re = par.rows().end();
831
832         y -= rb->ascent();
833         lyx::size_type rowno(0);
834         for (RowList::const_iterator rit = rb; rit != re; ++rit, ++rowno) {
835                 y += rit->ascent();
836                 // Allow setting of refreshInside for nested insets in
837                 // this row only
838                 bool tmp = refreshInside;
839
840                 // Row signature; has row changed since last paint?
841                 lyx::size_type const row_sig = calculateRowSignature(*rit, par, x, y);
842                 bool row_has_changed = par.rowSignature()[rowno] != row_sig;
843
844                 bool cursor_on_row = CursorOnRow(pi, pit, rit, text);
845                 bool in_inset_alone_on_row = innerCursorOnRow(pi, pit, rit,
846                         text);
847
848                 // If this is the only object on the row, we can make it wide
849                 for (pos_type i = rit->pos() ; i != rit->endpos(); ++i) {
850                         InsetBase const * const in = par.getInset(i);
851                         if (in) {
852                                 InsetText const * const t = in->asTextInset();
853                                 if (t)
854                                         t->Wide() = in_inset_alone_on_row;
855                         }
856                 }
857
858                 // If selection is on, the current row signature differs
859                 // from cache, or cursor is inside an inset _on this row_,
860                 // then paint the row
861                 if (repaintAll || row_has_changed || cursor_on_row) {
862                         // Add to row signature cache
863                         par.rowSignature()[rowno] = row_sig;
864
865                         bool const inside = (y + rit->descent() >= 0
866                                        && y - rit->ascent() < ww);
867                         RowPainter rp(inside ? pi : nullpi, text, pit, *rit, x, y);
868                         // Clear background of this row
869                         // (if paragraph background was not cleared)
870                         if (!repaintAll &&
871                             (!in_inset_alone_on_row || row_has_changed)) {
872                                 pi.pain.fillRectangle(x, y - rit->ascent(),
873                                     text.maxwidth_, rit->height(),
874                                     text.backgroundColor());
875                                 // If outer row has changed, force nested
876                                 // insets to repaint completely
877                                 if (row_has_changed)
878                                         refreshInside = true;
879                         }
880
881                         // Instrumentation for testing row cache (see also
882                         // 12 lines lower):
883                         if (text.isMainText())
884                                 lyxerr[Debug::PAINTING] << "#";
885                         else
886                                 lyxerr[Debug::PAINTING] << "[" <<
887                                     repaintAll << row_has_changed <<
888                                     cursor_on_row << "]";
889                         rp.paintAppendix();
890                         rp.paintDepthBar();
891                         rp.paintChangeBar();
892                         if (rit == rb)
893                                 rp.paintFirst();
894                         if (rit + 1 == re)
895                                 rp.paintLast();
896                         rp.paintText();
897                 }
898                 y += rit->descent();
899                 // Restore, see above
900                 refreshInside = tmp;
901         }
902         lyxerr[Debug::PAINTING] << "." << endl;
903 }
904
905 } // namespace anon
906
907
908 void paintText(BufferView const & bv, ViewMetricsInfo const & vi,
909                Painter & pain)
910 {
911         LyXText & text = bv.buffer()->text();
912         bool const select = bv.cursor().selection();
913
914         PainterInfo pi(const_cast<BufferView *>(&bv), pain);
915         // Should the whole screen, including insets, be refreshed?
916         bool repaintAll = select || !vi.singlepar;
917
918         if (repaintAll) {
919                 // Clear background (if not delegated to rows)
920                 pain.fillRectangle(0, vi.y1, bv.workWidth(), vi.y2 - vi.y1,
921                         text.backgroundColor());
922         }
923         if (select) {
924                 text.drawSelection(pi, 0, 0);
925         }
926
927         int yy = vi.y1;
928         // draw contents
929         for (pit_type pit = vi.p1; pit <= vi.p2; ++pit) {
930                 refreshInside = repaintAll;
931                 Paragraph const & par = text.getPar(pit);
932                 yy += par.ascent();
933                 paintPar(pi, text, pit, 0, yy, repaintAll);
934                 yy += par.descent();
935         }
936
937         // Cache one paragraph above and one below
938         // Note MV: this cannot be suppressed even for singlepar.
939         // Try viewing the User Guide Mobius figure
940
941         if (vi.p1 > 0) {
942                 text.redoParagraph(vi.p1 - 1);
943                 theCoords.parPos()[&text][vi.p1 - 1] =
944                         Point(0, vi.y1 - text.getPar(vi.p1 - 1).descent());
945         }
946
947         if (vi.p2 < lyx::pit_type(text.paragraphs().size()) - 1) {
948                 text.redoParagraph(vi.p2 + 1);
949                 theCoords.parPos()[&text][vi.p2 + 1] =
950                         Point(0, vi.y2 + text.getPar(vi.p2 + 1).ascent());
951         }
952
953         // and grey out above (should not happen later)
954 //      lyxerr << "par ascent: " << text.getPar(vi.p1).ascent() << endl;
955         if (vi.y1 > 0 && !vi.singlepar)
956                 pain.fillRectangle(0, 0, bv.workWidth(), vi.y1, LColor::bottomarea);
957
958         // and possibly grey out below
959 //      lyxerr << "par descent: " << text.getPar(vi.p1).ascent() << endl;
960         if (vi.y2 < bv.workHeight() && !vi.singlepar)
961                 pain.fillRectangle(0, vi.y2, bv.workWidth(), bv.workHeight() - vi.y2, LColor::bottomarea);
962 }
963
964
965 void paintTextInset(LyXText const & text, PainterInfo & pi, int x, int y)
966 {
967 //      lyxerr << "  paintTextInset: y: " << y << endl;
968
969         y -= text.getPar(0).ascent();
970         // This flag can not be set from within same inset:
971         bool repaintAll = refreshInside;
972         for (int pit = 0; pit < int(text.paragraphs().size()); ++pit) {
973                 y += text.getPar(pit).ascent();
974                 paintPar(pi, text, pit, x, y, repaintAll);
975                 y += text.getPar(pit).descent();
976         }
977 }