]> git.lyx.org Git - lyx.git/blob - src/rowpainter.C
fix crash when collapsing ert with cursor inside
[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         LyXFont pf(text_.getFont(par_, pos));
146         text_.applyOuterFont(pf);
147         return pf;
148 }
149
150
151 int RowPainter::singleWidth(lyx::pos_type pos) const
152 {
153         return text_.singleWidth(par_, pos);
154 }
155
156
157 int RowPainter::singleWidth(lyx::pos_type pos, char c) const
158 {
159         LyXFont const & font = text_.getFont(par_, pos);
160         return text_.singleWidth(par_, pos, c, font);
161 }
162
163
164 LyXFont const RowPainter::getLabelFont() const
165 {
166         return text_.getLabelFont(par_);
167 }
168
169
170 int RowPainter::leftMargin() const
171 {
172         return text_.leftMargin(pit_, row_.pos());
173 }
174
175
176 void RowPainter::paintInset(pos_type const pos)
177 {
178         InsetBase const * inset = par_.getInset(pos);
179         BOOST_ASSERT(inset);
180         PainterInfo pi(const_cast<BufferView *>(&bv_), pain_);
181         pi.base.font = getFont(pos);
182         pi.ltr_pos = (text_.bidi.level(pos) % 2 == 0);
183         theCoords.insets().add(inset, int(x_), yo_);
184         inset->drawSelection(pi, int(x_), yo_);
185         inset->draw(pi, int(x_), yo_);
186         x_ += inset->width();
187 }
188
189
190 void RowPainter::paintHebrewComposeChar(pos_type & vpos)
191 {
192         pos_type pos = text_.bidi.vis2log(vpos);
193
194         string str;
195
196         // first char
197         char c = par_.getChar(pos);
198         str += c;
199         ++vpos;
200
201         LyXFont const & font = getFont(pos);
202         int const width = font_metrics::width(c, font);
203         int dx = 0;
204
205         for (pos_type i = pos - 1; i >= 0; --i) {
206                 c = par_.getChar(i);
207                 if (!Encodings::IsComposeChar_hebrew(c)) {
208                         if (IsPrintableNonspace(c)) {
209                                 int const width2 = singleWidth(i, c);
210                                 // dalet / resh
211                                 dx = (c == 'ø' || c == 'ã')
212                                         ? width2 - width
213                                         : (width2 - width) / 2;
214                         }
215                         break;
216                 }
217         }
218
219         // Draw nikud
220         pain_.text(int(x_) + dx, yo_, str, font);
221 }
222
223
224 void RowPainter::paintArabicComposeChar(pos_type & vpos)
225 {
226         pos_type pos = text_.bidi.vis2log(vpos);
227         string str;
228
229         // first char
230         char c = par_.getChar(pos);
231         c = par_.transformChar(c, pos);
232         str += c;
233         ++vpos;
234
235         LyXFont const & font = getFont(pos);
236         int const width = font_metrics::width(c, font);
237         int dx = 0;
238
239         for (pos_type i = pos - 1; i >= 0; --i) {
240                 c = par_.getChar(i);
241                 if (!Encodings::IsComposeChar_arabic(c)) {
242                         if (IsPrintableNonspace(c)) {
243                                 int const width2 = singleWidth(i, c);
244                                 dx = (width2 - width) / 2;
245                         }
246                         break;
247                 }
248         }
249         // Draw nikud
250         pain_.text(int(x_) + dx, yo_, str, font);
251 }
252
253
254 void RowPainter::paintChars(pos_type & vpos, bool hebrew, bool arabic)
255 {
256         pos_type pos = text_.bidi.vis2log(vpos);
257         pos_type const end = row_.endpos();
258         LyXFont orig_font = getFont(pos);
259
260         // first character
261         string str;
262         str += par_.getChar(pos);
263         if (arabic) {
264                 unsigned char c = str[0];
265                 str[0] = par_.transformChar(c, pos);
266         }
267
268         bool prev_struckout = isDeletedText(par_, pos);
269         bool prev_newtext = isInsertedText(par_, pos);
270
271         // collect as much similar chars as we can
272         for (++vpos; vpos < end && (pos = text_.bidi.vis2log(vpos)) >= 0; ++vpos) {
273                 char c = par_.getChar(pos);
274
275                 if (!IsPrintableNonspace(c))
276                         break;
277
278                 if (prev_struckout != isDeletedText(par_, pos))
279                         break;
280
281                 if (prev_newtext != isInsertedText(par_, pos))
282                         break;
283
284                 if (arabic && Encodings::IsComposeChar_arabic(c))
285                         break;
286
287                 if (hebrew && Encodings::IsComposeChar_hebrew(c))
288                         break;
289
290                 if (orig_font != getFont(pos))
291                         break;
292
293                 if (arabic)
294                         c = par_.transformChar(c, pos);
295
296                 str += c;
297         }
298
299         if (prev_struckout)
300                 orig_font.setColor(LColor::strikeout);
301         else if (prev_newtext)
302                 orig_font.setColor(LColor::newtext);
303
304         // Draw text and set the new x position
305         //lyxerr << "paint row: yo_ " << yo_ << "\n";
306         pain_.text(int(x_), yo_, str, orig_font);
307         x_ += font_metrics::width(str, orig_font);
308 }
309
310
311 void RowPainter::paintForeignMark(double orig_x, LyXFont const & orig_font)
312 {
313         if (!lyxrc.mark_foreign_language)
314                 return;
315         if (orig_font.language() == latex_language)
316                 return;
317         if (orig_font.language() == bv_.buffer()->params().language)
318                 return;
319
320         int const y = yo_ + 1;
321         pain_.line(int(orig_x), y, int(x_), y, LColor::language);
322 }
323
324
325 void RowPainter::paintFromPos(pos_type & vpos)
326 {
327         pos_type const pos = text_.bidi.vis2log(vpos);
328
329         LyXFont const & orig_font = getFont(pos);
330
331         double const orig_x = x_;
332
333         char const c = par_.getChar(pos);
334
335         if (c == Paragraph::META_INSET) {
336                 paintInset(pos);
337                 ++vpos;
338                 paintForeignMark(orig_x, orig_font);
339                 return;
340         }
341
342         // usual characters, no insets
343
344         // special case languages
345         bool const hebrew = (orig_font.language()->lang() == "hebrew");
346         bool const arabic =
347                 orig_font.language()->lang() == "arabic" &&
348                 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
349                 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
350
351         // draw as many chars as we can
352         if ((!hebrew && !arabic)
353                 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
354                 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
355                 paintChars(vpos, hebrew, arabic);
356         } else if (hebrew) {
357                 paintHebrewComposeChar(vpos);
358         } else if (arabic) {
359                 paintArabicComposeChar(vpos);
360         }
361
362         paintForeignMark(orig_x, orig_font);
363 }
364
365
366 void RowPainter::paintChangeBar()
367 {
368         pos_type const start = row_.pos();
369         pos_type const end = row_.endpos();
370
371         if (start == end || !par_.isChanged(start, end - 1))
372                 return;
373
374         int const height = text_.isLastRow(pit_, row_)
375                 ? row_.ascent()
376                 : row_.height();
377
378         pain_.fillRectangle(4, yo_ - row_.ascent(), 5, height, LColor::changebar);
379 }
380
381
382 void RowPainter::paintAppendix()
383 {
384         if (!par_.params().appendix())
385                 return;
386
387         int y = yo_ - row_.ascent();
388
389         if (par_.params().startOfAppendix())
390                 y += 2 * defaultRowHeight();
391
392         pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
393         pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), LColor::appendix);
394 }
395
396
397 void RowPainter::paintDepthBar()
398 {
399         Paragraph::depth_type const depth = par_.getDepth();
400
401         if (depth <= 0)
402                 return;
403
404         Paragraph::depth_type prev_depth = 0;
405         if (!text_.isFirstRow(pit_, row_)) {
406                 pit_type pit2 = pit_;
407                 if (row_.pos() == 0)
408                         --pit2;
409                 prev_depth = pars_[pit2].getDepth();
410         }
411
412         Paragraph::depth_type next_depth = 0;
413         if (!text_.isLastRow(pit_, row_)) {
414                 pit_type pit2 = pit_;
415                 if (row_.endpos() >= pars_[pit2].size())
416                         ++pit2;
417                 next_depth = pars_[pit2].getDepth();
418         }
419
420         for (Paragraph::depth_type i = 1; i <= depth; ++i) {
421                 int const w = nestMargin() / 5;
422                 int x = int(xo_) + w * i;
423                 // only consider the changebar space if we're drawing outermost text
424                 if (text_.isMainText())
425                         x += changebarMargin();
426
427                 int const starty = yo_ - row_.ascent();
428                 int const h =  row_.height() - 1 - (i - next_depth - 1) * 3;
429
430                 pain_.line(x, starty, x, starty + h, LColor::depthbar);
431
432                 if (i > prev_depth)
433                         pain_.fillRectangle(x, starty, w, 2, LColor::depthbar);
434                 if (i > next_depth)
435                         pain_.fillRectangle(x, starty + h, w, 2, LColor::depthbar);
436         }
437 }
438
439
440 int RowPainter::paintAppendixStart(int y)
441 {
442         LyXFont pb_font;
443         pb_font.setColor(LColor::appendix);
444         pb_font.decSize();
445
446         string const label = _("Appendix");
447         int w = 0;
448         int a = 0;
449         int d = 0;
450         font_metrics::rectText(label, pb_font, w, a, d);
451
452         int const text_start = int(xo_ + (width_ - w) / 2);
453         int const text_end = text_start + w;
454
455         pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
456
457         pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
458         pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
459
460         return 3 * defaultRowHeight();
461 }
462
463
464 void RowPainter::paintFirst()
465 {
466         ParagraphParameters const & parparams = par_.params();
467
468         int y_top = 0;
469
470         // start of appendix?
471         if (parparams.startOfAppendix())
472                 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
473
474         Buffer const & buffer = *bv_.buffer();
475
476         LyXLayout_ptr const & layout = par_.layout();
477
478         if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
479                 if (pit_ != 0) {
480                         if (layout->latextype == LATEX_PARAGRAPH
481                                 && !par_.getDepth()) {
482                                 y_top += buffer.params().getDefSkip().inPixels(bv_);
483                         } else {
484                                 LyXLayout_ptr const & playout = pars_[pit_ - 1].layout();
485                                 if (playout->latextype == LATEX_PARAGRAPH
486                                         && !pars_[pit_ - 1].getDepth()) {
487                                         // is it right to use defskip here, too? (AS)
488                                         y_top += buffer.params().getDefSkip().inPixels(bv_);
489                                 }
490                         }
491                 }
492         }
493
494         bool const is_rtl = text_.isRTL(par_);
495         bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
496         //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
497
498         // should we print a label?
499         if (layout->labeltype >= LABEL_STATIC
500             && (layout->labeltype != LABEL_STATIC
501                       || layout->latextype != LATEX_ENVIRONMENT
502                       || is_seq)) {
503
504                 LyXFont const font = getLabelFont();
505                 string const str = par_.getLabelstring();
506                 if (!str.empty()) {
507                         double x = x_;
508
509                         // this is special code for the chapter layout. This is
510                         // printed in an extra row and has a pagebreak at
511                         // the top.
512                         if (layout->counter == "chapter") {
513                                 double spacing_val = 1.0;
514                                 if (!parparams.spacing().isDefault()) {
515                                         spacing_val = parparams.spacing().getValue();
516                                 } else {
517                                         spacing_val = buffer.params().spacing().getValue();
518                                 }
519 #ifdef WITH_WARNINGS
520 #warning Look is this correct?
521 #endif
522                                 int const labeladdon = int(font_metrics::maxHeight(font) * layout->spacing.getValue() * spacing_val);
523
524                                 int const maxdesc = int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
525                                         + int(layout->parsep) * defaultRowHeight();
526
527                                 if (is_rtl) {
528                                         x = width_ - leftMargin() -
529                                                 font_metrics::width(str, font);
530                                 }
531
532                                 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
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 }