]> git.lyx.org Git - lyx.git/blob - src/rowpainter.C
CoordBranch merge
[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 & 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 & 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 & 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                                         float 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 #warning Look is this correct?                                  
519                                         int const labeladdon = int(font_metrics::maxHeight(font) * layout->spacing.getValue() * spacing_val);
520
521                                         int const maxdesc = int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
522                                                 + int(layout->parsep) * defaultRowHeight();
523
524                                         if (is_rtl) {
525                                                 x = width_ - leftMargin() -
526                                                         font_metrics::width(str, font);
527                                         }
528
529                                         pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
530                                 }
531                         } else {
532                                 if (is_rtl) {
533                                         x = width_ - leftMargin()
534                                                 + font_metrics::width(layout->labelsep, font);
535                                 } else {
536                                         x = x_ - font_metrics::width(layout->labelsep, font)
537                                                 - font_metrics::width(str, font);
538                                 }
539
540                                 pain_.text(int(x), yo_, str, font);
541                         }
542                 }
543
544         // the labels at the top of an environment.
545         // More or less for bibliography
546         } else if (is_seq &&
547                 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
548                 layout->labeltype == LABEL_BIBLIO ||
549                 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
550                 LyXFont font = getLabelFont();
551                 if (!par_.getLabelstring().empty()) {
552                         string const str = par_.getLabelstring();
553                         float spacing_val = 1.0;
554                         if (!parparams.spacing().isDefault()) {
555                                 spacing_val = parparams.spacing().getValue();
556                         } else {
557                                 spacing_val = buffer.params().spacing().getValue();
558                         }
559
560                         int maxdesc =
561                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
562                                 + (layout->labelbottomsep * defaultRowHeight()));
563
564                         double x = x_;
565                         if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
566                                 x = ((is_rtl ? leftMargin() : x_)
567                                          + width_ - text_.rightMargin(par_)) / 2;
568                                 x -= font_metrics::width(str, font) / 2;
569                         } else if (is_rtl) {
570                                 x = width_ - leftMargin() -
571                                         font_metrics::width(str, font);
572                         }
573                         pain_.text(int(x), yo_ - maxdesc, str, font);
574                 }
575         }
576 }
577
578
579 void RowPainter::paintLast()
580 {
581         bool const is_rtl = text_.isRTL(par_);
582         int const endlabel = getEndLabel(pit_, text_.paragraphs());
583
584         // draw an endlabel
585         switch (endlabel) {
586         case END_LABEL_BOX:
587         case END_LABEL_FILLED_BOX: {
588                 LyXFont const font = getLabelFont();
589                 int const size = int(0.75 * font_metrics::maxAscent(font));
590                 int const y = yo_ - size;
591                 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
592
593                 if (width_ - int(row_.width()) <= size)
594                         x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
595
596                 if (endlabel == END_LABEL_BOX)
597                         pain_.rectangle(x, y, size, size, LColor::eolmarker);
598                 else
599                         pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
600                 break;
601         }
602
603         case END_LABEL_STATIC: {
604                 LyXFont font = getLabelFont();
605                 string const & str = par_.layout()->endlabelstring();
606                 double const x = is_rtl ?
607                         x_ - font_metrics::width(str, font)
608                         : - text_.rightMargin(par_) - row_.width();
609                 pain_.text(int(x), yo_, str, font);
610                 break;
611         }
612
613         case END_LABEL_NO_LABEL:
614                 break;
615         }
616 }
617
618
619 void RowPainter::paintText()
620 {
621         pos_type const end = row_.endpos();
622         pos_type body_pos = par_.beginOfBody();
623         if (body_pos > 0 &&
624                 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
625                 body_pos = 0;
626         }
627
628         LyXLayout_ptr const & layout = par_.layout();
629
630         bool running_strikeout = false;
631         bool is_struckout = false;
632         int last_strikeout_x = 0;
633
634         for (pos_type vpos = row_.pos(); vpos < end; ) {
635                 if (x_ > bv_.workWidth())
636                         break;
637
638                 pos_type const pos = text_.bidi.vis2log(vpos);
639
640                 if (pos >= par_.size()) {
641                         ++vpos;
642                         continue;
643                 }
644
645                 const int width_pos = singleWidth(pos);
646                 if (x_ + width_pos < 0) {
647                         x_ += width_pos;
648                         ++vpos;
649                         continue;
650                 }
651
652                 is_struckout = isDeletedText(par_, pos);
653
654                 if (is_struckout && !running_strikeout) {
655                         running_strikeout = true;
656                         last_strikeout_x = int(x_);
657                 }
658
659                 bool const highly_editable_inset = par_.isInset(pos)
660                         && isHighlyEditableInset(par_.getInset(pos));
661
662                 // if we reach the end of a struck out range, paint it
663                 // we also don't paint across things like tables
664                 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
665                         // FIXME this should take real text height into account, not 
666                         // the whole row including padding whitespace
667                         //int const middle = yo_ + (- row_.ascent() + row_.top_of_text()) / 2;
668                         int const middle = yo_ - row_.ascent() / 2;
669                         pain_.line(last_strikeout_x, middle, int(x_), middle,
670                                 LColor::strikeout, Painter::line_solid, Painter::line_thin);
671                         running_strikeout = false;
672                 }
673
674                 if (body_pos > 0 && pos == body_pos - 1) {
675                         int const lwidth = font_metrics::width(layout->labelsep,
676                                 getLabelFont());
677
678                         x_ += label_hfill_ + lwidth - width_pos;
679                 }
680
681                 if (par_.isHfill(pos)) {
682                         x_ += 1;
683
684                         int const y0 = yo_;
685                         int const y1 = y0 - defaultRowHeight() / 2;
686
687                         pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
688
689                         if (hfillExpansion(par_, row_, pos)) {
690                                 int const y2 = (y0 + y1) / 2;
691
692                                 if (pos >= body_pos) {
693                                         pain_.line(int(x_), y2, int(x_ + hfill_), y2,
694                                                   LColor::added_space,
695                                                   Painter::line_onoffdash);
696                                         x_ += hfill_;
697                                 } else {
698                                         pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
699                                                   LColor::added_space,
700                                                   Painter::line_onoffdash);
701                                         x_ += label_hfill_;
702                                 }
703                                 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
704                         }
705                         x_ += 2;
706                         ++vpos;
707                 } else if (par_.isSeparator(pos)) {
708                         x_ += width_pos;
709                         if (pos >= body_pos)
710                                 x_ += separator_;
711                         ++vpos;
712                 } else {
713                         paintFromPos(vpos);
714                 }
715         }
716
717         // if we reach the end of a struck out range, paint it
718         if (running_strikeout) {
719                 //top_of_text = font_metrics::maxAscent(font);
720                 //int const middle = yo_ - top_of_text() / 2;
721                 int const middle = yo_ - row_.ascent() / 2;
722                 pain_.line(last_strikeout_x, middle, int(x_), middle,
723                         LColor::strikeout, Painter::line_solid, Painter::line_thin);
724                 running_strikeout = false;
725         }
726 }
727
728
729 void paintPar
730         (PainterInfo & pi, LyXText const & text, pit_type pit, int x, int y)
731 {
732 //      lyxerr << "  paintPar: pit: " << pit << " at y: " << y << endl;
733         static NullPainter nop;
734         static PainterInfo nullpi(pi.base.bv, nop);
735         int const ww = pi.base.bv->workHeight();
736         
737         Paragraph & par = text.paragraphs()[pit];
738
739         RowList::iterator const rb = par.rows().begin();
740         RowList::iterator const re = par.rows().end();
741         theCoords.pars_[&text][pit] = Point(x, y);
742
743         y -= rb->ascent();
744         for (RowList::iterator rit = rb; rit != re; ++rit) {
745                 y += rit->ascent();
746                 bool const inside = (y + rit->descent() >= 0
747                                        && y - rit->ascent() < ww);
748                 RowPainter rp(inside ? pi : nullpi, text, pit, *rit, x, y);
749                         
750                 y += rit->descent();
751                 rp.paintAppendix();
752                 rp.paintDepthBar();
753                 rp.paintChangeBar();
754                 if (rit == rb)
755                         rp.paintFirst();
756                 if (rit + 1 == re)
757                         rp.paintLast();
758                 rp.paintText();
759         }
760 }
761
762 } // namespace anon
763
764
765 void paintText(BufferView const & bv, ViewMetricsInfo const & vi)
766 {
767         Painter & pain = bv.painter();
768         LyXText * const text = bv.text();
769
770         // clear background
771         pain.fillRectangle(0, vi.y1, bv.workWidth(), vi.y2 - vi.y1,
772                            LColor::background);
773
774         // draw selection
775         PainterInfo pi(const_cast<BufferView *>(&bv), pain);
776
777         text->drawSelection(pi, 0, 0);
778
779         int yy = vi.y1;
780         // draw contents
781         for (pit_type pit = vi.p1; pit <= vi.p2; ++pit) {
782                 yy += text->getPar(pit).ascent();
783                 paintPar(pi, *bv.text(), pit, 0, yy);
784                 yy += text->getPar(pit).descent();
785         }
786         
787
788         // paint one paragraph above and one below
789         if (vi.p1 > 0) {
790                 text->redoParagraph(vi.p1 - 1);
791                 paintPar(pi, *bv.text(), vi.p1 - 1, 0,
792                          vi.y1 -  text->getPar(vi.p1 - 1).descent());
793         }
794
795         if (vi.p2 < text->paragraphs().size() - 1) {
796                 text->redoParagraph(vi.p2 + 1);
797                 paintPar(pi, *bv.text(), vi.p2 + 1, 0,
798                          vi.y2 + text->getPar(vi.p2 + 1).ascent());
799         }
800
801         // and grey out above (should not happen later)
802         lyxerr << "par ascent: " << text->getPar(vi.p1).ascent() << endl;
803         if (vi.y1 > 0)
804                 pain.fillRectangle(0, 0, bv.workWidth(), vi.y1, LColor::bottomarea);
805
806         // and possibly grey out below
807         lyxerr << "par descent: " << text->getPar(vi.p1).ascent() << endl;
808         if (vi.y2 < bv.workHeight())
809                 pain.fillRectangle(0, vi.y2, bv.workWidth(), bv.workHeight() - vi.y2, LColor::bottomarea);
810 }
811
812
813 void paintTextInset(LyXText const & text, PainterInfo & pi, int x, int y)
814 {
815 //      lyxerr << "  paintTextInset: y: " << y << endl;
816
817         y -= text.getPar(0).ascent();
818         for (int pit = 0; pit < int(text.paragraphs().size()); ++pit) {
819                 y += text.getPar(pit).ascent();
820                 paintPar(pi, text, pit, x, y);
821                 y += text.getPar(pit).descent();
822         }
823 }