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