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