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