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