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