]> git.lyx.org Git - lyx.git/blob - src/rowpainter.C
18f92e9b17bbe763c9c9c8b7fef98c438ac08302
[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 "bufferparams.h"
18 #include "BufferView.h"
19 #include "encoding.h"
20 #include "gettext.h"
21 #include "language.h"
22 #include "lyxrc.h"
23 #include "lyxrow.h"
24 #include "lyxrow_funcs.h"
25 #include "metricsinfo.h"
26 #include "paragraph_funcs.h"
27 #include "ParagraphParameters.h"
28 #include "vspace.h"
29
30 #include "frontends/font_metrics.h"
31 #include "frontends/Painter.h"
32
33 #include "insets/insettext.h"
34
35 #include "support/textutils.h"
36
37
38 using std::max;
39 using lyx::pos_type;
40
41 extern int PAPER_MARGIN;
42 extern int CHANGEBAR_MARGIN;
43 extern int LEFT_MARGIN;
44
45 namespace {
46
47 // "temporary". We'll never get to use more
48 // references until we start adding hacks like
49 // these until other places catch up.
50 BufferView * perv(BufferView const & bv)
51 {
52         return const_cast<BufferView *>(&bv);
53 }
54
55 /**
56  * A class used for painting an individual row of text.
57  */
58 class RowPainter {
59 public:
60         /// initialise painter
61         RowPainter(BufferView const & bv, LyXText const & text,
62                 ParagraphList::iterator pit,
63                 RowList::iterator rit, int y_offset, int x_offset, int y);
64
65         /// do the painting
66         void paint();
67 private:
68         // paint various parts
69         void paintBackground();
70         void paintSelection();
71         void paintAppendix();
72         void paintDepthBar();
73         void paintChangeBar();
74         void paintFirst();
75         void paintLast();
76         void paintForeignMark(double orig_x, LyXFont const & orig_font);
77         void paintHebrewComposeChar(lyx::pos_type & vpos);
78         void paintArabicComposeChar(lyx::pos_type & vpos);
79         void paintChars(lyx::pos_type & vpos, bool hebrew, bool arabic);
80         int paintPageBreak(string const & label, int y);
81         int paintAppendixStart(int y);
82         int paintLengthMarker(string const & prefix, VSpace const & vsp, int start);
83         void paintText();
84         void paintFromPos(lyx::pos_type & vpos);
85         void paintInset(lyx::pos_type const pos);
86
87         /// return left margin
88         int leftMargin() const;
89
90         /// return the font at the given pos
91         LyXFont const getFont(lyx::pos_type pos) const;
92
93         /// return the label font for this row
94         LyXFont const getLabelFont() const;
95
96         char const transformChar(char c, lyx::pos_type pos) const;
97
98         /// return pixel width for the given pos
99         int singleWidth(lyx::pos_type pos) const;
100         int singleWidth(lyx::pos_type pos, char c) const;
101
102         /// bufferview to paint on
103         BufferView const & bv_;
104
105         /// Painter to use
106         Painter & pain_;
107
108         /// LyXText for the row
109         LyXText const & text_;
110
111         /// The row to paint
112         RowList::iterator row_;
113
114         /// Row's paragraph
115         mutable ParagraphList::iterator  pit_;
116
117         // Looks ugly - is
118         double xo_;
119         int yo_;
120         double x_;
121         int y_;
122         int width_;
123         double separator_;
124         double hfill_;
125         double label_hfill_;
126 };
127
128
129 RowPainter::RowPainter(BufferView const & bv, LyXText const & text,
130      ParagraphList::iterator pit, RowList::iterator rit,
131      int y_offset, int x_offset, int y)
132         : bv_(bv), pain_(bv_.painter()), text_(text), row_(rit),
133           pit_(pit), xo_(x_offset), yo_(y_offset), y_(y)
134 {}
135
136
137 /// "temporary"
138 LyXFont const RowPainter::getFont(pos_type pos) const
139 {
140         return text_.getFont(pit_, pos);
141 }
142
143
144 int RowPainter::singleWidth(lyx::pos_type pos) const
145 {
146         return text_.singleWidth(pit_, pos);
147 }
148
149
150 int RowPainter::singleWidth(lyx::pos_type pos, char c) const
151 {
152         LyXFont const & font = text_.getFont(pit_, pos);
153         return text_.singleWidth(pit_, pos, c, font);
154 }
155
156
157 LyXFont const RowPainter::getLabelFont() const
158 {
159         return text_.getLabelFont(pit_);
160 }
161
162
163 char const RowPainter::transformChar(char c, lyx::pos_type pos) const
164 {
165         return text_.transformChar(c, *pit_, pos);
166 }
167
168
169 int RowPainter::leftMargin() const
170 {
171         return text_.leftMargin(pit_, *row_);
172 }
173
174
175 void RowPainter::paintInset(pos_type const pos)
176 {
177         InsetOld * inset = const_cast<InsetOld*>(pit_->getInset(pos));
178
179         BOOST_ASSERT(inset);
180
181         PainterInfo pi(perv(bv_));
182         pi.base.font = getFont(pos);
183         inset->draw(pi, int(x_), yo_ + row_->baseline());
184         x_ += inset->width();
185 }
186
187
188 void RowPainter::paintHebrewComposeChar(pos_type & vpos)
189 {
190         pos_type pos = text_.vis2log(vpos);
191
192         string str;
193
194         // first char
195         char c = pit_->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 = pit_->getChar(i);
205                 if (!Encodings::IsComposeChar_hebrew(c)) {
206                         if (IsPrintableNonspace(c)) {
207                                 int const width2 =
208                                         singleWidth(i, c);
209                                 // dalet / resh
210                                 dx = (c == 'ø' || c == 'ã')
211                                         ? width2 - width
212                                         : (width2 - width) / 2;
213                         }
214                         break;
215                 }
216         }
217
218         // Draw nikud
219         pain_.text(int(x_) + dx, yo_ + row_->baseline(), str, font);
220 }
221
222
223 void RowPainter::paintArabicComposeChar(pos_type & vpos)
224 {
225         pos_type pos = text_.vis2log(vpos);
226         string str;
227
228         // first char
229         char c = pit_->getChar(pos);
230         c = transformChar(c, pos);
231         str +=c;
232         ++vpos;
233
234         LyXFont const & font = getFont(pos);
235         int const width = font_metrics::width(c, font);
236         int dx = 0;
237
238         for (pos_type i = pos - 1; i >= 0; --i) {
239                 c = pit_->getChar(i);
240                 if (!Encodings::IsComposeChar_arabic(c)) {
241                         if (IsPrintableNonspace(c)) {
242                                 int const width2 =
243                                         singleWidth(i, c);
244                                 dx = (width2 - width) / 2;
245                         }
246                         break;
247                 }
248         }
249         // Draw nikud
250         pain_.text(int(x_) + dx, yo_ + row_->baseline(), str, font);
251 }
252
253
254 void RowPainter::paintChars(pos_type & vpos, bool hebrew, bool arabic)
255 {
256         pos_type pos = text_.vis2log(vpos);
257         pos_type const last = lastPos(*pit_, row_);
258         LyXFont orig_font = getFont(pos);
259
260         // first character
261         string str;
262         str += pit_->getChar(pos);
263         if (arabic) {
264                 unsigned char c = str[0];
265                 str[0] = transformChar(c, pos);
266         }
267
268         bool prev_struckout(isDeletedText(*pit_, pos));
269         bool prev_newtext(isInsertedText(*pit_, pos));
270
271         ++vpos;
272
273         // collect as much similar chars as we can
274         while (vpos <= last && (pos = text_.vis2log(vpos)) >= 0) {
275                 char c = pit_->getChar(pos);
276
277                 if (!IsPrintableNonspace(c))
278                         break;
279
280                 if (prev_struckout != isDeletedText(*pit_, pos))
281                         break;
282
283                 if (prev_newtext != isInsertedText(*pit_, pos))
284                         break;
285
286                 if (arabic && Encodings::IsComposeChar_arabic(c))
287                         break;
288                 if (hebrew && Encodings::IsComposeChar_hebrew(c))
289                         break;
290
291                 if (orig_font != getFont(pos))
292                         break;
293
294                 if (arabic)
295                         c = transformChar(c, pos);
296                 str += c;
297                 ++vpos;
298         }
299
300         if (prev_struckout) {
301                 orig_font.setColor(LColor::strikeout);
302         } else if (prev_newtext) {
303                 orig_font.setColor(LColor::newtext);
304         }
305
306         // Draw text and set the new x position
307         //lyxerr << "paint row: yo_ " << yo_ << " baseline: " << row_->baseline()
308         //      << "\n";
309         pain_.text(int(x_), yo_ + row_->baseline(), str, orig_font);
310         x_ += font_metrics::width(str, orig_font);
311 }
312
313
314 void RowPainter::paintForeignMark(double orig_x, LyXFont const & orig_font)
315 {
316         if (!lyxrc.mark_foreign_language)
317                 return;
318         if (orig_font.language() == latex_language)
319                 return;
320         if (orig_font.language() == bv_.buffer()->params().language)
321                 return;
322
323         int const y = yo_ + row_->baseline() + 1;
324         pain_.line(int(orig_x), y, int(x_), y, LColor::language);
325 }
326
327
328 void RowPainter::paintFromPos(pos_type & vpos)
329 {
330         pos_type const pos = text_.vis2log(vpos);
331
332         LyXFont const & orig_font = getFont(pos);
333
334         double const orig_x = x_;
335
336         char const c = pit_->getChar(pos);
337
338         if (IsInsetChar(c)) {
339                 paintInset(pos);
340                 ++vpos;
341                 paintForeignMark(orig_x, orig_font);
342                 return;
343         }
344
345         // usual characters, no insets
346
347         // special case languages
348         bool const hebrew = (orig_font.language()->lang() == "hebrew");
349         bool const arabic =
350                 orig_font.language()->lang() == "arabic" &&
351                 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
352                 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
353
354         // draw as many chars as we can
355         if ((!hebrew && !arabic)
356                 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
357                 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
358                 paintChars(vpos, hebrew, arabic);
359         } else if (hebrew) {
360                 paintHebrewComposeChar(vpos);
361         } else if (arabic) {
362                 paintArabicComposeChar(vpos);
363         }
364
365         paintForeignMark(orig_x, orig_font);
366
367         return;
368 }
369
370
371 void RowPainter::paintBackground()
372 {
373         int const x = int(xo_);
374         int const y = yo_ < 0 ? 0 : yo_;
375         int const h = yo_ < 0 ? row_->height() + yo_ : row_->height();
376         pain_.fillRectangle(x, y, width_, h, text_.backgroundColor());
377 }
378
379
380 void RowPainter::paintSelection()
381 {
382         bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params());
383
384         // the current selection
385         int const startx = text_.selection.start.x();
386         int const endx = text_.selection.end.x();
387         int const starty = text_.selection.start.y();
388         int const endy = text_.selection.end.y();
389         RowList::iterator startrow = text_.getRow(text_.selection.start);
390         RowList::iterator endrow = text_.getRow(text_.selection.end);
391
392         if (text_.bidi_same_direction) {
393                 int x;
394                 int y = yo_;
395                 int w;
396                 int h = row_->height();
397
398                 if (startrow == row_ && endrow == row_) {
399                         if (startx < endx) {
400                                 x = int(xo_) + startx;
401                                 w = endx - startx;
402                                 pain_.fillRectangle(x, y, w, h, LColor::selection);
403                         } else {
404                                 x = int(xo_) + endx;
405                                 w = startx - endx;
406                                 pain_.fillRectangle(x, y, w, h, LColor::selection);
407                         }
408                 } else if (startrow == row_) {
409                         int const x = is_rtl ? int(xo_) : int(xo_ + startx);
410                         int const w = is_rtl ? startx : (width_ - startx);
411                         pain_.fillRectangle(x, y, w, h, LColor::selection);
412                 } else if (endrow == row_) {
413                         int const x = is_rtl ? int(xo_ + endx) : int(xo_);
414                         int const w = is_rtl ? (width_ - endx) : endx;
415                         pain_.fillRectangle(x, y, w, h, LColor::selection);
416                 } else if (y_ > starty && y_ < endy) {
417                         pain_.fillRectangle(int(xo_), y, width_, h, LColor::selection);
418                 }
419                 return;
420         } else if (startrow != row_ && endrow != row_) {
421                 if (y_ > starty && y_ < endy) {
422                         int w = width_;
423                         int h = row_->height();
424                         pain_.fillRectangle(int(xo_), yo_, w, h, LColor::selection);
425                 }
426                 return;
427         }
428
429         if ((startrow != row_ && !is_rtl) || (endrow != row_ && is_rtl))
430                 pain_.fillRectangle(int(xo_), yo_,
431                         int(x_), row_->height(), LColor::selection);
432
433         pos_type const body_pos = pit_->beginningOfBody();
434         pos_type const last = lastPos(*pit_, row_);
435         double tmpx = x_;
436
437         for (pos_type vpos = row_->pos(); vpos <= last; ++vpos)  {
438                 pos_type pos = text_.vis2log(vpos);
439                 double const old_tmpx = tmpx;
440                 if (body_pos > 0 && pos == body_pos - 1) {
441                         LyXLayout_ptr const & layout = pit_->layout();
442                         LyXFont const lfont = getLabelFont();
443
444                         tmpx += label_hfill_ + font_metrics::width(layout->labelsep, lfont);
445
446                         if (pit_->isLineSeparator(body_pos - 1))
447                                 tmpx -= singleWidth(body_pos - 1);
448                 }
449
450                 if (hfillExpansion(*pit_, row_, pos)) {
451                         tmpx += singleWidth(pos);
452                         if (pos >= body_pos)
453                                 tmpx += hfill_;
454                         else
455                                 tmpx += label_hfill_;
456                 }
457
458                 else if (pit_->isSeparator(pos)) {
459                         tmpx += singleWidth(pos);
460                         if (pos >= body_pos)
461                                 tmpx += separator_;
462                 } else {
463                         tmpx += singleWidth(pos);
464                 }
465
466                 if ((startrow != row_ || text_.selection.start.pos() <= pos) &&
467                         (endrow != row_ || pos < text_.selection.end.pos())) {
468                         // Here we do not use x_ as xo_ was added to x_.
469                         pain_.fillRectangle(int(old_tmpx), yo_,
470                                 int(tmpx - old_tmpx + 1),
471                                 row_->height(), LColor::selection);
472                 }
473         }
474
475         if ((startrow != row_ && is_rtl) || (endrow != row_ && !is_rtl)) {
476                 pain_.fillRectangle(int(xo_ + tmpx),
477                                       yo_, int(bv_.workWidth() - tmpx),
478                                       row_->height(), LColor::selection);
479         }
480 }
481
482
483 void RowPainter::paintChangeBar()
484 {
485         pos_type const start = row_->pos();
486         pos_type const end = lastPos(*pit_, row_);
487
488         if (!pit_->isChanged(start, end))
489                 return;
490
491         int const height = (row_ == text_.lastRow())
492                 ? row_->baseline()
493                 : row_->height() + boost::next(row_)->top_of_text();
494
495         pain_.fillRectangle(4, yo_, 5, height, LColor::changebar);
496 }
497
498
499 void RowPainter::paintAppendix()
500 {
501         if (!pit_->params().appendix())
502                 return;
503
504         // FIXME: can be just width_ ?
505         int const ww = bv_.workWidth();
506
507         int y = yo_;
508
509         if (pit_->params().startOfAppendix())
510                 y += 2 * defaultRowHeight();
511
512         pain_.line(1, y, 1, yo_ + row_->height(), LColor::appendix);
513         pain_.line(ww - 2, y, ww - 2, yo_ + row_->height(), LColor::appendix);
514 }
515
516
517 void RowPainter::paintDepthBar()
518 {
519         Paragraph::depth_type const depth = pit_->getDepth();
520
521         if (depth <= 0)
522                 return;
523
524         Paragraph::depth_type prev_depth = 0;
525         if (row_ != text_.firstRow()) {
526                 ParagraphList::iterator pit2 = pit_;
527                 if (row_ == pit2->rows.begin())
528                         --pit2;
529                 prev_depth = pit2->getDepth();
530         }
531
532         Paragraph::depth_type next_depth = 0;
533         if (row_ != text_.lastRow()) {
534                 ParagraphList::iterator pit2 = pit_;
535                 if (boost::next(row_) == pit2->rows.end())
536                         ++pit2;
537                 next_depth = pit2->getDepth();
538         }
539
540         for (Paragraph::depth_type i = 1; i <= depth; ++i) {
541                 int const w = PAPER_MARGIN / 5;
542                 int x = int(w * i + xo_);
543                 // only consider the changebar space if we're drawing outer left
544                 if (!xo_)
545                         x += CHANGEBAR_MARGIN;
546                 int const h = yo_ + row_->height() - 1 - (i - next_depth - 1) * 3;
547
548                 pain_.line(x, yo_, x, h, LColor::depthbar);
549
550                 if (i > prev_depth)
551                         pain_.fillRectangle(x, yo_, w, 2, LColor::depthbar);
552                 if (i > next_depth)
553                         pain_.fillRectangle(x, h, w, 2, LColor::depthbar);
554         }
555 }
556
557
558 int RowPainter::paintLengthMarker(string const & prefix, VSpace const & vsp, int start)
559 {
560         if (vsp.kind() == VSpace::NONE)
561                 return 0;
562
563         int const arrow_size = 4;
564         int const size = getLengthMarkerHeight(bv_, vsp);
565         int const end = start + size;
566
567         // the label to display (if any)
568         string str;
569         // y-values for top arrow
570         int ty1, ty2;
571         // y-values for bottom arrow
572         int by1, by2;
573
574         str = prefix + " (" + vsp.asLyXCommand() + ")";
575
576         if (vsp.kind() == VSpace::VFILL) {
577                 ty1 = ty2 = start;
578                 by1 = by2 = end;
579         } else {
580                 // adding or removing space
581                 bool const added = vsp.kind() != VSpace::LENGTH ||
582                                    vsp.length().len().value() > 0.0;
583                 ty1 = added ? (start + arrow_size) : start;
584                 ty2 = added ? start : (start + arrow_size);
585                 by1 = added ? (end - arrow_size) : end;
586                 by2 = added ? end : (end - arrow_size);
587         }
588
589         int const leftx = int(xo_) + leftMargin();
590         int const midx = leftx + arrow_size;
591         int const rightx = midx + arrow_size;
592
593         // first the string
594         int w = 0;
595         int a = 0;
596         int d = 0;
597
598         LyXFont font;
599         font.setColor(LColor::added_space);
600         font.decSize();
601         font.decSize();
602         font_metrics::rectText(str, font, w, a, d);
603
604         pain_.rectText(leftx + 2 * arrow_size + 5,
605                        start + ((end - start) / 2) + d,
606                        str, font,
607                        LColor::none, LColor::none);
608
609         // top arrow
610         pain_.line(leftx, ty1, midx, ty2, LColor::added_space);
611         pain_.line(midx, ty2, rightx, ty1, LColor::added_space);
612
613         // bottom arrow
614         pain_.line(leftx, by1, midx, by2, LColor::added_space);
615         pain_.line(midx, by2, rightx, by1, LColor::added_space);
616
617         // joining line
618         pain_.line(midx, ty2, midx, by2, LColor::added_space);
619
620         return size;
621 }
622
623
624 int RowPainter::paintPageBreak(string const & label, int y)
625 {
626         LyXFont pb_font;
627         pb_font.setColor(LColor::pagebreak);
628         pb_font.decSize();
629
630         int w = 0;
631         int a = 0;
632         int d = 0;
633         font_metrics::rectText(label, pb_font, w, a, d);
634
635         int const text_start = int(xo_ + (width_ - w) / 2);
636         int const text_end = text_start + w;
637
638         pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
639
640         pain_.line(int(xo_), y, text_start, y,
641                    LColor::pagebreak, Painter::line_onoffdash);
642         pain_.line(text_end, y, int(xo_ + width_), y,
643                    LColor::pagebreak, Painter::line_onoffdash);
644
645         return 3 * defaultRowHeight();
646 }
647
648
649 int RowPainter::paintAppendixStart(int y)
650 {
651         LyXFont pb_font;
652         pb_font.setColor(LColor::appendix);
653         pb_font.decSize();
654
655         string const label = _("Appendix");
656         int w = 0;
657         int a = 0;
658         int d = 0;
659         font_metrics::rectText(label, pb_font, w, a, d);
660
661         int const text_start = int(xo_ + (width_ - w) / 2);
662         int const text_end = text_start + w;
663
664         pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
665
666         pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
667         pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
668
669         return 3 * defaultRowHeight();
670 }
671
672
673 void RowPainter::paintFirst()
674 {
675         ParagraphParameters const & parparams = pit_->params();
676
677         int y_top = 0;
678
679         // start of appendix?
680         if (parparams.startOfAppendix())
681                 y_top += paintAppendixStart(yo_ + y_top + 2 * defaultRowHeight());
682
683         // the top margin
684         if (row_ == text_.firstRow() && !text_.isInInset())
685                 y_top += PAPER_MARGIN;
686
687         // draw a top pagebreak
688         if (parparams.pagebreakTop())
689                 y_top += paintPageBreak(_("Page Break (top)"),
690                         yo_ + y_top + 2 * defaultRowHeight());
691
692         // draw the additional space if needed:
693         y_top += paintLengthMarker(_("Space above"), parparams.spaceTop(),
694                         yo_ + y_top);
695
696         Buffer const & buffer = *bv_.buffer();
697
698         LyXLayout_ptr const & layout = pit_->layout();
699
700         if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
701                 if (pit_ != text_.ownerParagraphs().begin()) {
702                         if (layout->latextype == LATEX_PARAGRAPH
703                                 && !pit_->getDepth()) {
704                                 y_top += buffer.params().getDefSkip().inPixels(bv_);
705                         } else {
706                                 LyXLayout_ptr const & playout =
707                                         boost::prior(pit_)->layout();
708                                 if (playout->latextype == LATEX_PARAGRAPH
709                                         && !boost::prior(pit_)->getDepth()) {
710                                         // is it right to use defskip here, too? (AS)
711                                         y_top += buffer.params().getDefSkip().inPixels(bv_);
712                                 }
713                         }
714                 }
715         }
716
717         int const ww = bv_.workWidth();
718
719         // draw a top line
720         if (parparams.lineTop()) {
721                 int const asc = font_metrics::ascent('x', getFont(0));
722
723                 y_top += asc;
724
725                 int const w = (text_.isInInset() ? text_.inset_owner->width() : ww);
726                 int const xp = static_cast<int>(text_.isInInset() ? xo_ : 0);
727                 pain_.line(xp, yo_ + y_top, xp + w, yo_ + y_top,
728                         LColor::topline, Painter::line_solid,
729                         Painter::line_thick);
730
731                 y_top += asc;
732         }
733
734         bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params());
735
736         // should we print a label?
737         if (layout->labeltype >= LABEL_STATIC
738             && (layout->labeltype != LABEL_STATIC
739                 || layout->latextype != LATEX_ENVIRONMENT
740                 || isFirstInSequence(pit_, text_.ownerParagraphs()))) {
741
742                 LyXFont font = getLabelFont();
743                 if (!pit_->getLabelstring().empty()) {
744                         double x = x_;
745                         string const str = pit_->getLabelstring();
746
747                         // this is special code for the chapter layout. This is
748                         // printed in an extra row and has a pagebreak at
749                         // the top.
750                         if (layout->counter == "chapter") {
751                                 if (buffer.params().secnumdepth >= 0) {
752                                         float spacing_val = 1.0;
753                                         if (!parparams.spacing().isDefault()) {
754                                                 spacing_val = parparams.spacing().getValue();
755                                         } else {
756                                                 spacing_val = buffer.params().spacing().getValue();
757                                         }
758
759                                         int const maxdesc =
760                                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
761                                                 + int(layout->parsep) * defaultRowHeight();
762
763                                         if (is_rtl) {
764                                                 x = ww - leftMargin() -
765                                                         font_metrics::width(str, font);
766                                         }
767
768                                         pain_.text(int(x),
769                                                 yo_ + row_->baseline() -
770                                                 row_->ascent_of_text() - maxdesc,
771                                                 str, font);
772                                 }
773                         } else {
774                                 if (is_rtl) {
775                                         x = ww - leftMargin()
776                                                 + font_metrics::width(layout->labelsep, font);
777                                 } else {
778                                         x = x_ - font_metrics::width(layout->labelsep, font)
779                                                 - font_metrics::width(str, font);
780                                 }
781
782                                 pain_.text(int(x), yo_ + row_->baseline(), str, font);
783                         }
784                 }
785
786         // the labels at the top of an environment.
787         // More or less for bibliography
788         } else if (isFirstInSequence(pit_, text_.ownerParagraphs()) &&
789                 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
790                 layout->labeltype == LABEL_BIBLIO ||
791                 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
792                 LyXFont font = getLabelFont();
793                 if (!pit_->getLabelstring().empty()) {
794                         string const str = pit_->getLabelstring();
795                         float spacing_val = 1.0;
796                         if (!parparams.spacing().isDefault()) {
797                                 spacing_val = parparams.spacing().getValue();
798                         } else {
799                                 spacing_val = buffer.params().spacing().getValue();
800                         }
801
802                         int maxdesc =
803                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
804                                 + (layout->labelbottomsep * defaultRowHeight()));
805
806                         double x = x_;
807                         if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
808                                 x = ((is_rtl ? leftMargin() : x_)
809                                          + ww - text_.rightMargin(pit_, *bv_.buffer(), *row_)) / 2;
810                                 x -= font_metrics::width(str, font) / 2;
811                         } else if (is_rtl) {
812                                 x = ww - leftMargin() -
813                                         font_metrics::width(str, font);
814                         }
815                         pain_.text(int(x),
816                             yo_ + row_->baseline() - row_->ascent_of_text() - maxdesc,
817                                   str, font);
818                 }
819         }
820 }
821
822
823 void RowPainter::paintLast()
824 {
825         ParagraphParameters const & parparams = pit_->params();
826         int y_bottom = row_->height() - 1;
827
828         // the bottom margin
829         if (row_ == text_.lastRow() && !text_.isInInset())
830                 y_bottom -= PAPER_MARGIN;
831
832         int const ww = bv_.workWidth();
833
834         // draw a bottom pagebreak
835         if (parparams.pagebreakBottom()) {
836                 y_bottom -= paintPageBreak(_("Page Break (bottom)"),
837                         yo_ + y_bottom - 2 * defaultRowHeight());
838         }
839
840         // draw the additional space if needed:
841         int const height = getLengthMarkerHeight(bv_, parparams.spaceBottom());
842         y_bottom -= paintLengthMarker(_("Space below"), parparams.spaceBottom(),
843                              yo_ + y_bottom - height);
844
845         // draw a bottom line
846         if (parparams.lineBottom()) {
847                 int const asc = font_metrics::ascent('x',
848                         getFont(max(pos_type(0), pit_->size() - 1)));
849
850                 y_bottom -= asc;
851
852                 int const w = text_.isInInset() ? text_.inset_owner->width() : ww;
853                 int const xp = int(text_.isInInset() ? xo_ : 0);
854                 int const y = yo_ + y_bottom;
855                 pain_.line(xp, y, xp + w, y, LColor::topline, Painter::line_solid,
856                           Painter::line_thick);
857
858                 y_bottom -= asc;
859         }
860
861         bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params());
862         int const endlabel = getEndLabel(pit_, text_.ownerParagraphs());
863
864         // draw an endlabel
865         switch (endlabel) {
866         case END_LABEL_BOX:
867         case END_LABEL_FILLED_BOX:
868         {
869                 LyXFont const font = getLabelFont();
870                 int const size = int(0.75 * font_metrics::maxAscent(font));
871                 int const y = (yo_ + row_->baseline()) - size;
872                 int x = is_rtl ? LEFT_MARGIN : ww - PAPER_MARGIN - size;
873
874                 if (row_->fill() <= size)
875                         x += (size - row_->fill() + 1) * (is_rtl ? -1 : 1);
876
877                 if (endlabel == END_LABEL_BOX)
878                         pain_.rectangle(x, y, size, size, LColor::eolmarker);
879                 else
880                         pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
881                 break;
882         }
883         case END_LABEL_STATIC:
884         {
885                 LyXFont font = getLabelFont();
886                 string const & str = pit_->layout()->endlabelstring();
887                 double const x = is_rtl ?
888                         x_ - font_metrics::width(str, font)
889                         : ww - text_.rightMargin(pit_, *bv_.buffer(), *row_) - row_->fill();
890                 pain_.text(int(x), yo_ + row_->baseline(), str, font);
891                 break;
892         }
893         case END_LABEL_NO_LABEL:
894                 break;
895         }
896 }
897
898
899 void RowPainter::paintText()
900 {
901         pos_type const last = lastPos(*pit_, row_);
902         pos_type body_pos = pit_->beginningOfBody();
903         if (body_pos > 0 &&
904                 (body_pos - 1 > last || !pit_->isLineSeparator(body_pos - 1))) {
905                 body_pos = 0;
906         }
907
908         LyXLayout_ptr const & layout = pit_->layout();
909
910         bool running_strikeout = false;
911         bool is_struckout = false;
912         int last_strikeout_x = 0;
913
914         pos_type vpos = row_->pos();
915         while (vpos <= last) {
916                 if (x_ > bv_.workWidth())
917                         break;
918                 pos_type pos = text_.vis2log(vpos);
919
920                 if (pos >= pit_->size()) {
921                         ++vpos;
922                         continue;
923                 }
924
925                 if (x_ + singleWidth(pos) < 0) {
926                         x_ += singleWidth(pos);
927                         ++vpos;
928                         continue;
929                 }
930
931                 is_struckout = isDeletedText(*pit_, pos);
932
933                 if (is_struckout && !running_strikeout) {
934                         running_strikeout = true;
935                         last_strikeout_x = int(x_);
936                 }
937
938                 bool const highly_editable_inset = pit_->isInset(pos)
939                         && isHighlyEditableInset(pit_->getInset(pos));
940
941                 // if we reach the end of a struck out range, paint it
942                 // we also don't paint across things like tables
943                 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
944                         int const middle = yo_ + row_->top_of_text()
945                                 + (row_->baseline() - row_->top_of_text()) / 2;
946                         pain_.line(last_strikeout_x, middle, int(x_), middle,
947                                 LColor::strikeout, Painter::line_solid, Painter::line_thin);
948                         running_strikeout = false;
949                 }
950
951                 if (body_pos > 0 && pos == body_pos - 1) {
952                         int const lwidth = font_metrics::width(layout->labelsep,
953                                 getLabelFont());
954
955                         x_ += label_hfill_ + lwidth - singleWidth(body_pos - 1);
956                 }
957
958                 if (pit_->isHfill(pos)) {
959                         x_ += 1;
960
961                         int const y0 = yo_ + row_->baseline();
962                         int const y1 = y0 - defaultRowHeight() / 2;
963
964                         pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
965
966                         if (hfillExpansion(*pit_, row_, pos)) {
967                                 int const y2 = (y0 + y1) / 2;
968
969                                 if (pos >= body_pos) {
970                                         pain_.line(int(x_), y2, int(x_ + hfill_), y2,
971                                                   LColor::added_space,
972                                                   Painter::line_onoffdash);
973                                         x_ += hfill_;
974                                 } else {
975                                         pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
976                                                   LColor::added_space,
977                                                   Painter::line_onoffdash);
978                                         x_ += label_hfill_;
979                                 }
980                                 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
981                         }
982                         x_ += 2;
983                         ++vpos;
984                 } else if (pit_->isSeparator(pos)) {
985                         x_ += singleWidth(pos);
986                         if (pos >= body_pos)
987                                 x_ += separator_;
988                         ++vpos;
989                 } else {
990                         paintFromPos(vpos);
991                 }
992         }
993
994         // if we reach the end of a struck out range, paint it
995         if (running_strikeout) {
996                 int const middle = yo_ + row_->top_of_text()
997                         + ((row_->baseline() - row_->top_of_text()) / 2);
998                 pain_.line(last_strikeout_x, middle, int(x_), middle,
999                         LColor::strikeout, Painter::line_solid, Painter::line_thin);
1000                 running_strikeout = false;
1001         }
1002 }
1003
1004
1005 void RowPainter::paint()
1006 {
1007         width_       = text_.workWidth();
1008         x_           = row_->x();
1009         separator_   = row_->fill_separator();
1010         hfill_       = row_->fill_hfill();
1011         label_hfill_ = row_->fill_label_hfill();
1012
1013         // FIXME: what is this fixing ?
1014         if (text_.isInInset() && x_ < 0)
1015                 x_ = 0;
1016         x_ += xo_;
1017
1018         // If we're *not* at the top-level of rows, then the
1019         // background has already been cleared.
1020         if (&text_ == bv_.text)
1021                 paintBackground();
1022
1023         // paint the selection background
1024         if (text_.selection.set())
1025                 paintSelection();
1026
1027         // vertical lines for appendix
1028         paintAppendix();
1029
1030         // environment depth brackets
1031         paintDepthBar();
1032
1033         // changebar
1034         paintChangeBar();
1035
1036         if (row_->isParStart())
1037                 paintFirst();
1038
1039         if (isParEnd(*pit_, row_))
1040                 paintLast();
1041
1042         // paint text
1043         paintText();
1044 }
1045
1046
1047 int paintRows(BufferView const & bv, LyXText const & text,
1048         ParagraphList::iterator pit, RowList::iterator rit,
1049         int xo, int y, int yf, int yo)
1050 {
1051         //lyxerr << "  paintRows: rit: " << &*rit << endl;
1052         //const_cast<LyXText&>(text).updateRowPositions();
1053         int const yy = yf - y;
1054         int const y2 = bv.painter().paperHeight();
1055
1056         ParagraphList::iterator end = text.ownerParagraphs().end();
1057         bool active = false;
1058
1059         for ( ; pit != end; ++pit) {
1060                 RowList::iterator row = pit->rows.begin();
1061                 RowList::iterator rend = pit->rows.end();
1062
1063                 for ( ; row != rend; ++row) {
1064                         if (row == rit)
1065                                 active = true;
1066                         if (active) {
1067                                 RowPainter painter(bv, text, pit, row, y + yo, xo, y + bv.top_y());
1068                                 painter.paint();
1069                                 y += row->height();
1070                                 if (yy + y >= y2)
1071                                         return y;
1072                         } else {
1073                                 //lyxerr << "   paintRows: row: " << &*row << " ignored" << endl;
1074                         }
1075                 }
1076         }
1077
1078         return y;
1079 }
1080
1081 } // namespace anon
1082
1083
1084 int paintText(BufferView & bv)
1085 {
1086         int const topy = bv.top_y();
1087         ParagraphList::iterator pit;
1088         RowList::iterator rit = bv.text->getRowNearY(topy, pit);
1089         int y = rit->y() - topy;
1090         return paintRows(bv, *bv.text, pit, rit, 0, y, y, 0);
1091 }
1092
1093
1094 void paintTextInset(BufferView & bv, LyXText & text, int x, int baseline)
1095 {
1096         RowList::iterator rit = text.firstRow();
1097         RowList::iterator end = text.endRow();
1098         ParagraphList::iterator pit = text.ownerParagraphs().begin();
1099
1100         int y_offset = baseline - rit->ascent_of_text();
1101         int y = y_offset;
1102         while (rit != end && y + rit->height() <= 0) {
1103                 y += rit->height();
1104                 text.nextRow(pit, rit);
1105         }
1106         if (y_offset < 0)
1107                 paintRows(bv, text, pit, rit, x, 0, y, y);
1108         else
1109                 paintRows(bv, text, pit, rit, x, 0, y_offset, y_offset);
1110 }
1111
1112
1113 int getLengthMarkerHeight(BufferView const & bv, VSpace const & vsp)
1114 {
1115         if (vsp.kind() == VSpace::NONE)
1116                 return 0;
1117
1118         int const arrow_size = 4;
1119         int const space_size = vsp.inPixels(bv);
1120
1121         LyXFont font;
1122         font.decSize();
1123         int const min_size = max(3 * arrow_size,
1124                 font_metrics::maxAscent(font)
1125                 + font_metrics::maxDescent(font));
1126
1127         if (vsp.length().len().value() < 0.0)
1128                 return min_size;
1129         else
1130                 return max(min_size, space_size);
1131 }