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