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