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