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