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