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