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