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