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