]> git.lyx.org Git - lyx.git/blob - src/rowpainter.C
980bac698a982c434a9724ace696cd3ce29e4505
[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         ++vpos;
271
272         // collect as much similar chars as we can
273         while (vpos < end && (pos = text_.bidi.vis2log(vpos)) >= 0) {
274                 char c = pit_->getChar(pos);
275
276                 if (!IsPrintableNonspace(c))
277                         break;
278
279                 if (prev_struckout != isDeletedText(*pit_, pos))
280                         break;
281
282                 if (prev_newtext != isInsertedText(*pit_, pos))
283                         break;
284
285                 if (arabic && Encodings::IsComposeChar_arabic(c))
286                         break;
287
288                 if (hebrew && Encodings::IsComposeChar_hebrew(c))
289                         break;
290
291                 if (orig_font != getFont(pos))
292                         break;
293
294                 if (arabic)
295                         c = pit_->transformChar(c, pos);
296                 str += c;
297                 ++vpos;
298         }
299
300         if (prev_struckout)
301                 orig_font.setColor(LColor::strikeout);
302         else if (prev_newtext)
303                 orig_font.setColor(LColor::newtext);
304
305         // Draw text and set the new x position
306         //lyxerr << "paint row: yo_ " << yo_ << " baseline: " << row_.baseline()
307         //      << "\n";
308         pain_.text(int(x_), yo_ + row_.baseline(), str, orig_font);
309         x_ += font_metrics::width(str, orig_font);
310 }
311
312
313 void RowPainter::paintForeignMark(double orig_x, LyXFont const & orig_font)
314 {
315         if (!lyxrc.mark_foreign_language)
316                 return;
317         if (orig_font.language() == latex_language)
318                 return;
319         if (orig_font.language() == bv_.buffer()->params().language)
320                 return;
321
322         int const y = yo_ + row_.baseline() + 1;
323         pain_.line(int(orig_x), y, int(x_), y, LColor::language);
324 }
325
326
327 void RowPainter::paintFromPos(pos_type & vpos)
328 {
329         pos_type const pos = text_.bidi.vis2log(vpos);
330
331         LyXFont const & orig_font = getFont(pos);
332
333         double const orig_x = x_;
334
335         char const c = pit_->getChar(pos);
336
337         if (c == Paragraph::META_INSET) {
338                 paintInset(pos);
339                 ++vpos;
340                 paintForeignMark(orig_x, orig_font);
341                 return;
342         }
343
344         // usual characters, no insets
345
346         // special case languages
347         bool const hebrew = (orig_font.language()->lang() == "hebrew");
348         bool const arabic =
349                 orig_font.language()->lang() == "arabic" &&
350                 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
351                 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
352
353         // draw as many chars as we can
354         if ((!hebrew && !arabic)
355                 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
356                 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
357                 paintChars(vpos, hebrew, arabic);
358         } else if (hebrew) {
359                 paintHebrewComposeChar(vpos);
360         } else if (arabic) {
361                 paintArabicComposeChar(vpos);
362         }
363
364         paintForeignMark(orig_x, orig_font);
365
366         return;
367 }
368
369
370 void RowPainter::paintBackground()
371 {
372         int const x = int(xo_);
373         int const y = yo_ < 0 ? 0 : yo_;
374         int const h = yo_ < 0 ? row_.height() + yo_ : row_.height();
375         pain_.fillRectangle(x, y, width_, h, text_.backgroundColor());
376 }
377
378
379 void RowPainter::paintSelection()
380 {
381         bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params());
382
383         // the current selection
384         int const startx = text_.selection.start.x();
385         int const endx = text_.selection.end.x();
386         int const starty = text_.selection.start.y();
387         int const endy = text_.selection.end.y();
388         ParagraphList::iterator startpit = text_.getPar(text_.selection.start);
389         ParagraphList::iterator endpit = text_.getPar(text_.selection.end);
390         RowList::iterator startrow = startpit->getRow(text_.selection.start.pos());
391         RowList::iterator endrow = endpit->getRow(text_.selection.end.pos());
392         int const h = row_.height();
393
394         if (text_.bidi.same_direction()) {
395                 int x;
396                 int y = yo_;
397                 int w;
398
399                 if (startrow == rit_ && endrow == rit_) {
400                         if (startx < endx) {
401                                 x = int(xo_) + startx;
402                                 w = endx - startx;
403                         } else {
404                                 x = int(xo_) + endx;
405                                 w = startx - endx;
406                         }
407                         pain_.fillRectangle(x, y, w, h, LColor::selection);
408                 } else if (startrow == rit_) {
409                         int const x = is_rtl ? int(xo_) : int(xo_ + startx);
410                         int const w = is_rtl ? startx : (width_ - startx);
411                         pain_.fillRectangle(x, y, w, h, LColor::selection);
412                 } else if (endrow == rit_) {
413                         int const x = is_rtl ? int(xo_ + endx) : int(xo_);
414                         int const w = is_rtl ? (width_ - endx) : endx;
415                         pain_.fillRectangle(x, y, w, h, LColor::selection);
416                 } else if (y_ > starty && y_ < endy) {
417                         pain_.fillRectangle(int(xo_), y, width_, h, LColor::selection);
418                 }
419                 return;
420         }
421         
422         if (startrow != rit_ && endrow != rit_) {
423                 if (y_ > starty && y_ < endy) {
424                         int w = width_;
425                         pain_.fillRectangle(int(xo_), yo_, w, h, LColor::selection);
426                 }
427                 return;
428         }
429
430         if ((startrow != rit_ && !is_rtl) || (endrow != rit_ && is_rtl))
431                 pain_.fillRectangle(int(xo_), yo_,
432                         int(x_), h, LColor::selection);
433
434         pos_type const body_pos = pit_->beginningOfBody();
435         pos_type const end = row_.endpos();
436         double tmpx = x_;
437
438         for (pos_type vpos = row_.pos(); vpos < end; ++vpos)  {
439                 pos_type pos = text_.bidi.vis2log(vpos);
440                 double const old_tmpx = tmpx;
441                 if (body_pos > 0 && pos == body_pos - 1) {
442                         LyXLayout_ptr const & layout = pit_->layout();
443                         LyXFont const lfont = getLabelFont();
444
445                         tmpx += label_hfill_ + font_metrics::width(layout->labelsep, lfont);
446
447                         if (pit_->isLineSeparator(body_pos - 1))
448                                 tmpx -= singleWidth(body_pos - 1);
449                 }
450
451                 if (hfillExpansion(*pit_, row_, pos)) {
452                         tmpx += singleWidth(pos);
453                         if (pos >= body_pos)
454                                 tmpx += hfill_;
455                         else
456                                 tmpx += label_hfill_;
457                 }
458
459                 else if (pit_->isSeparator(pos)) {
460                         tmpx += singleWidth(pos);
461                         if (pos >= body_pos)
462                                 tmpx += separator_;
463                 } else {
464                         tmpx += singleWidth(pos);
465                 }
466
467                 if ((startrow != rit_ || text_.selection.start.pos() <= pos) &&
468                         (endrow != rit_ || pos < text_.selection.end.pos())) {
469                         // Here we do not use x_ as xo_ was added to x_.
470                         pain_.fillRectangle(int(old_tmpx), yo_,
471                                 int(tmpx - old_tmpx + 1), h, LColor::selection);
472                 }
473         }
474
475         if ((startrow != rit_ && is_rtl) || (endrow != rit_ && !is_rtl)) {
476                 pain_.fillRectangle(int(xo_ + tmpx),
477                         yo_, int(bv_.workWidth() - tmpx), h, LColor::selection);
478         }
479 }
480
481
482 void RowPainter::paintChangeBar()
483 {
484         pos_type const start = row_.pos();
485         pos_type const end = row_.endpos();
486
487         if (start == end || !pit_->isChanged(start, end - 1))
488                 return;
489
490         int const height = text_.isLastRow(pit_, row_)
491                 ? row_.baseline()
492                 : row_.height() + boost::next(rit_)->top_of_text();
493
494         pain_.fillRectangle(4, yo_, 5, height, LColor::changebar);
495 }
496
497
498 void RowPainter::paintAppendix()
499 {
500         if (!pit_->params().appendix())
501                 return;
502
503         // FIXME: can be just width_ ?
504         int const ww = bv_.workWidth();
505
506         int y = yo_;
507
508         if (pit_->params().startOfAppendix())
509                 y += 2 * defaultRowHeight();
510
511         pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
512         pain_.line(ww - 2, y, ww - 2, yo_ + row_.height(), LColor::appendix);
513 }
514
515
516 void RowPainter::paintDepthBar()
517 {
518         Paragraph::depth_type const depth = pit_->getDepth();
519
520         if (depth <= 0)
521                 return;
522
523         Paragraph::depth_type prev_depth = 0;
524         if (!text_.isFirstRow(pit_, row_)) {
525                 ParagraphList::iterator pit2 = pit_;
526                 if (row_.pos() == 0)
527                         --pit2;
528                 prev_depth = pit2->getDepth();
529         }
530
531         Paragraph::depth_type next_depth = 0;
532         if (!text_.isLastRow(pit_, row_)) {
533                 ParagraphList::iterator pit2 = pit_;
534                 if (row_.endpos() >= pit2->size())
535                         ++pit2;
536                 next_depth = pit2->getDepth();
537         }
538
539         for (Paragraph::depth_type i = 1; i <= depth; ++i) {
540                 int const w = PAPER_MARGIN / 5;
541                 int x = int(w * i + xo_);
542                 // only consider the changebar space if we're drawing outer left
543                 if (xo_ == 0)
544                         x += CHANGEBAR_MARGIN;
545                 int const h = yo_ + row_.height() - 1 - (i - next_depth - 1) * 3;
546
547                 pain_.line(x, yo_, x, h, LColor::depthbar);
548
549                 if (i > prev_depth)
550                         pain_.fillRectangle(x, yo_, w, 2, LColor::depthbar);
551                 if (i > next_depth)
552                         pain_.fillRectangle(x, h, w, 2, LColor::depthbar);
553         }
554 }
555
556
557 int RowPainter::paintLengthMarker(string const & prefix, VSpace const & vsp,
558         int start)
559 {
560         if (vsp.kind() == VSpace::NONE)
561                 return 0;
562
563         int const arrow_size = 4;
564         int const size = getLengthMarkerHeight(bv_, vsp);
565         int const end = start + size;
566
567         // the label to display (if any)
568         string str;
569         // y-values for top arrow
570         int ty1, ty2;
571         // y-values for bottom arrow
572         int by1, by2;
573
574         str = prefix + " (" + vsp.asLyXCommand() + ")";
575
576         if (vsp.kind() == VSpace::VFILL) {
577                 ty1 = ty2 = start;
578                 by1 = by2 = end;
579         } else {
580                 // adding or removing space
581                 bool const added = vsp.kind() != VSpace::LENGTH ||
582                                    vsp.length().len().value() > 0.0;
583                 ty1 = added ? (start + arrow_size) : start;
584                 ty2 = added ? start : (start + arrow_size);
585                 by1 = added ? (end - arrow_size) : end;
586                 by2 = added ? end : (end - arrow_size);
587         }
588
589         int const leftx = int(xo_) + leftMargin();
590         int const midx = leftx + arrow_size;
591         int const rightx = midx + arrow_size;
592
593         // first the string
594         int w = 0;
595         int a = 0;
596         int d = 0;
597
598         LyXFont font;
599         font.setColor(LColor::added_space);
600         font.decSize();
601         font.decSize();
602         font_metrics::rectText(str, font, w, a, d);
603
604         pain_.rectText(leftx + 2 * arrow_size + 5,
605                        start + ((end - start) / 2) + d,
606                        str, font,
607                        LColor::none, LColor::none);
608
609         // top arrow
610         pain_.line(leftx, ty1, midx, ty2, LColor::added_space);
611         pain_.line(midx, ty2, rightx, ty1, LColor::added_space);
612
613         // bottom arrow
614         pain_.line(leftx, by1, midx, by2, LColor::added_space);
615         pain_.line(midx, by2, rightx, by1, LColor::added_space);
616
617         // joining line
618         pain_.line(midx, ty2, midx, by2, LColor::added_space);
619
620         return size;
621 }
622
623
624 int RowPainter::paintAppendixStart(int y)
625 {
626         LyXFont pb_font;
627         pb_font.setColor(LColor::appendix);
628         pb_font.decSize();
629
630         string const label = _("Appendix");
631         int w = 0;
632         int a = 0;
633         int d = 0;
634         font_metrics::rectText(label, pb_font, w, a, d);
635
636         int const text_start = int(xo_ + (width_ - w) / 2);
637         int const text_end = text_start + w;
638
639         pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
640
641         pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
642         pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
643
644         return 3 * defaultRowHeight();
645 }
646
647
648 void RowPainter::paintFirst()
649 {
650         ParagraphParameters const & parparams = pit_->params();
651
652         int y_top = 0;
653
654         // start of appendix?
655         if (parparams.startOfAppendix())
656                 y_top += paintAppendixStart(yo_ + y_top + 2 * defaultRowHeight());
657
658         // the top margin
659         if (text_.isFirstRow(pit_, row_) && !text_.isInInset())
660                 y_top += PAPER_MARGIN;
661
662         // draw the additional space if needed:
663         y_top += paintLengthMarker(_("Space above"), parparams.spaceTop(),
664                         yo_ + y_top);
665
666         Buffer const & buffer = *bv_.buffer();
667
668         LyXLayout_ptr const & layout = pit_->layout();
669
670         if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
671                 if (pit_ != text_.ownerParagraphs().begin()) {
672                         if (layout->latextype == LATEX_PARAGRAPH
673                                 && !pit_->getDepth()) {
674                                 y_top += buffer.params().getDefSkip().inPixels(bv_);
675                         } else {
676                                 LyXLayout_ptr const & playout =
677                                         boost::prior(pit_)->layout();
678                                 if (playout->latextype == LATEX_PARAGRAPH
679                                         && !boost::prior(pit_)->getDepth()) {
680                                         // is it right to use defskip here, too? (AS)
681                                         y_top += buffer.params().getDefSkip().inPixels(bv_);
682                                 }
683                         }
684                 }
685         }
686
687         int const ww = bv_.workWidth();
688
689         bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params());
690         bool const is_seq = isFirstInSequence(pit_, text_.ownerParagraphs());
691         //lyxerr << "paintFirst: " << pit_->id() << " is_seq: " << is_seq << std::endl;
692
693         // should we print a label?
694         if (layout->labeltype >= LABEL_STATIC
695             && (layout->labeltype != LABEL_STATIC
696                       || layout->latextype != LATEX_ENVIRONMENT
697                       || is_seq)) {
698
699                 LyXFont font = getLabelFont();
700                 if (!pit_->getLabelstring().empty()) {
701                         double x = x_;
702                         string const str = pit_->getLabelstring();
703
704                         // this is special code for the chapter layout. This is
705                         // printed in an extra row and has a pagebreak at
706                         // the top.
707                         if (layout->counter == "chapter") {
708                                 if (buffer.params().secnumdepth >= 0) {
709                                         float spacing_val = 1.0;
710                                         if (!parparams.spacing().isDefault()) {
711                                                 spacing_val = parparams.spacing().getValue();
712                                         } else {
713                                                 spacing_val = buffer.params().spacing().getValue();
714                                         }
715
716                                         int const maxdesc =
717                                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
718                                                 + int(layout->parsep) * defaultRowHeight();
719
720                                         if (is_rtl) {
721                                                 x = ww - leftMargin() -
722                                                         font_metrics::width(str, font);
723                                         }
724
725                                         pain_.text(int(x),
726                                                 yo_ + row_.baseline() -
727                                                 row_.ascent_of_text() - maxdesc,
728                                                 str, font);
729                                 }
730                         } else {
731                                 if (is_rtl) {
732                                         x = ww - leftMargin()
733                                                 + font_metrics::width(layout->labelsep, font);
734                                 } else {
735                                         x = x_ - font_metrics::width(layout->labelsep, font)
736                                                 - font_metrics::width(str, font);
737                                 }
738
739                                 pain_.text(int(x), yo_ + row_.baseline(), str, font);
740                         }
741                 }
742
743         // the labels at the top of an environment.
744         // More or less for bibliography
745         } else if (is_seq &&
746                 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
747                 layout->labeltype == LABEL_BIBLIO ||
748                 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
749                 LyXFont font = getLabelFont();
750                 if (!pit_->getLabelstring().empty()) {
751                         string const str = pit_->getLabelstring();
752                         float spacing_val = 1.0;
753                         if (!parparams.spacing().isDefault()) {
754                                 spacing_val = parparams.spacing().getValue();
755                         } else {
756                                 spacing_val = buffer.params().spacing().getValue();
757                         }
758
759                         int maxdesc =
760                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
761                                 + (layout->labelbottomsep * defaultRowHeight()));
762
763                         double x = x_;
764                         if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
765                                 x = ((is_rtl ? leftMargin() : x_)
766                                          + ww - text_.rightMargin(*pit_, *bv_.buffer(), row_)) / 2;
767                                 x -= font_metrics::width(str, font) / 2;
768                         } else if (is_rtl) {
769                                 x = ww - leftMargin() -
770                                         font_metrics::width(str, font);
771                         }
772                         pain_.text(int(x),
773                             yo_ + row_.baseline() - row_.ascent_of_text() - maxdesc,
774                                   str, font);
775                 }
776         }
777 }
778
779
780 void RowPainter::paintLast()
781 {
782         ParagraphParameters const & parparams = pit_->params();
783         int y_bottom = row_.height() - 1;
784
785         // the bottom margin
786         if (text_.isLastRow(pit_, row_) && !text_.isInInset())
787                 y_bottom -= PAPER_MARGIN;
788
789         int const ww = bv_.workWidth();
790
791         // draw the additional space if needed:
792         int const height = getLengthMarkerHeight(bv_, parparams.spaceBottom());
793         y_bottom -= paintLengthMarker(_("Space below"), parparams.spaceBottom(),
794                              yo_ + y_bottom - height);
795
796         bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params());
797         int const endlabel = getEndLabel(pit_, text_.ownerParagraphs());
798
799         // draw an endlabel
800         switch (endlabel) {
801         case END_LABEL_BOX:
802         case END_LABEL_FILLED_BOX:
803         {
804                 LyXFont const font = getLabelFont();
805                 int const size = int(0.75 * font_metrics::maxAscent(font));
806                 int const y = (yo_ + row_.baseline()) - size;
807                 int x = is_rtl ? LEFT_MARGIN : ww - PAPER_MARGIN - size;
808
809                 if (row_.fill() <= size)
810                         x += (size - row_.fill() + 1) * (is_rtl ? -1 : 1);
811
812                 if (endlabel == END_LABEL_BOX)
813                         pain_.rectangle(x, y, size, size, LColor::eolmarker);
814                 else
815                         pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
816                 break;
817         }
818         case END_LABEL_STATIC:
819         {
820                 LyXFont font = getLabelFont();
821                 string const & str = pit_->layout()->endlabelstring();
822                 double const x = is_rtl ?
823                         x_ - font_metrics::width(str, font)
824                         : ww - text_.rightMargin(*pit_, *bv_.buffer(), row_) - row_.fill();
825                 pain_.text(int(x), yo_ + row_.baseline(), str, font);
826                 break;
827         }
828         case END_LABEL_NO_LABEL:
829                 break;
830         }
831 }
832
833
834 void RowPainter::paintText()
835 {
836         pos_type const end = row_.endpos();
837         pos_type body_pos = pit_->beginningOfBody();
838         if (body_pos > 0 &&
839                 (body_pos > end || !pit_->isLineSeparator(body_pos - 1))) {
840                 body_pos = 0;
841         }
842
843         LyXLayout_ptr const & layout = pit_->layout();
844
845         bool running_strikeout = false;
846         bool is_struckout = false;
847         int last_strikeout_x = 0;
848
849         for (pos_type vpos = row_.pos(); vpos <= end; ) {
850                 if (x_ > bv_.workWidth())
851                         break;
852
853                 pos_type pos = text_.bidi.vis2log(vpos);
854
855                 if (pos >= pit_->size()) {
856                         ++vpos;
857                         continue;
858                 }
859
860                 if (x_ + singleWidth(pos) < 0) {
861                         x_ += singleWidth(pos);
862                         ++vpos;
863                         continue;
864                 }
865
866                 is_struckout = isDeletedText(*pit_, pos);
867
868                 if (is_struckout && !running_strikeout) {
869                         running_strikeout = true;
870                         last_strikeout_x = int(x_);
871                 }
872
873                 bool const highly_editable_inset = pit_->isInset(pos)
874                         && isHighlyEditableInset(pit_->getInset(pos));
875
876                 // if we reach the end of a struck out range, paint it
877                 // we also don't paint across things like tables
878                 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
879                         int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
880                         pain_.line(last_strikeout_x, middle, int(x_), middle,
881                                 LColor::strikeout, Painter::line_solid, Painter::line_thin);
882                         running_strikeout = false;
883                 }
884
885                 if (body_pos > 0 && pos == body_pos - 1) {
886                         int const lwidth = font_metrics::width(layout->labelsep,
887                                 getLabelFont());
888
889                         x_ += label_hfill_ + lwidth - singleWidth(body_pos - 1);
890                 }
891
892                 if (pit_->isHfill(pos)) {
893                         x_ += 1;
894
895                         int const y0 = yo_ + row_.baseline();
896                         int const y1 = y0 - defaultRowHeight() / 2;
897
898                         pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
899
900                         if (hfillExpansion(*pit_, row_, pos)) {
901                                 int const y2 = (y0 + y1) / 2;
902
903                                 if (pos >= body_pos) {
904                                         pain_.line(int(x_), y2, int(x_ + hfill_), y2,
905                                                   LColor::added_space,
906                                                   Painter::line_onoffdash);
907                                         x_ += hfill_;
908                                 } else {
909                                         pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
910                                                   LColor::added_space,
911                                                   Painter::line_onoffdash);
912                                         x_ += label_hfill_;
913                                 }
914                                 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
915                         }
916                         x_ += 2;
917                         ++vpos;
918                 } else if (pit_->isSeparator(pos)) {
919                         x_ += singleWidth(pos);
920                         if (pos >= body_pos)
921                                 x_ += separator_;
922                         ++vpos;
923                 } else {
924                         paintFromPos(vpos);
925                 }
926         }
927
928         // if we reach the end of a struck out range, paint it
929         if (running_strikeout) {
930                 int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
931                 pain_.line(last_strikeout_x, middle, int(x_), middle,
932                         LColor::strikeout, Painter::line_solid, Painter::line_thin);
933                 running_strikeout = false;
934         }
935 }
936
937
938 void RowPainter::paint()
939 {
940         // FIXME: what is this fixing ?
941         if (text_.isInInset() && x_ < 0)
942                 x_ = 0;
943         x_ += xo_;
944
945         // background has already been cleared.
946         if (&text_ == bv_.text)
947                 paintBackground();
948
949         // paint the selection background
950         if (text_.selection.set())
951                 paintSelection();
952
953         // vertical lines for appendix
954         paintAppendix();
955
956         // environment depth brackets
957         paintDepthBar();
958
959         // changebar
960         paintChangeBar();
961
962         if (row_.pos() == 0)
963                 paintFirst();
964
965         if (row_.endpos() >= pit_->size())
966                 paintLast();
967
968         // paint text
969         paintText();
970 }
971
972
973 int paintRows(BufferView const & bv, LyXText const & text,
974         ParagraphList::iterator pit, RowList::iterator rit,
975         int xo, int y, int yf, int yo)
976 {
977         //lyxerr << "  paintRows: rit: " << &*rit << endl;
978         int const yy = yf - y;
979         int const y2 = bv.painter().paperHeight();
980
981         ParagraphList::iterator end = text.ownerParagraphs().end();
982         bool active = false;
983
984         for ( ; pit != end; ++pit) {
985                 RowList::iterator row = pit->rows.begin();
986                 RowList::iterator rend = pit->rows.end();
987
988                 for ( ; row != rend; ++row) {
989                         if (row == rit)
990                                 active = true;
991                         if (active) {
992                                 RowPainter painter(bv, text, pit, row, y + yo, xo, y + bv.top_y());
993                                 painter.paint();
994                                 y += row->height();
995                                 if (yy + y >= y2)
996                                         return y;
997                         } else {
998                                 //lyxerr << "   paintRows: row: " << &*row << " ignored" << endl;
999                         }
1000                 }
1001         }
1002
1003         return y;
1004 }
1005
1006 } // namespace anon
1007
1008
1009 int paintText(BufferView & bv)
1010 {
1011         int const topy = bv.top_y();
1012         ParagraphList::iterator pit;
1013         RowList::iterator rit = bv.text->getRowNearY(topy, pit);
1014         int const y = pit->y + rit->y_offset() - topy;
1015         return paintRows(bv, *bv.text, pit, rit, 0, y, y, 0);
1016 }
1017
1018
1019 void paintTextInset(BufferView & bv, LyXText & text, int x, int baseline)
1020 {
1021         RowList::iterator rit = text.firstRow();
1022         RowList::iterator end = text.endRow();
1023         ParagraphList::iterator pit = text.ownerParagraphs().begin();
1024
1025         int y_offset = baseline - rit->ascent_of_text();
1026         int y = y_offset;
1027         while (rit != end && y + rit->height() <= 0) {
1028                 y += rit->height();
1029                 text.nextRow(pit, rit);
1030         }
1031         if (y_offset < 0)
1032                 paintRows(bv, text, pit, rit, x, 0, y, y);
1033         else
1034                 paintRows(bv, text, pit, rit, x, 0, y_offset, y_offset);
1035 }
1036
1037
1038 int getLengthMarkerHeight(BufferView const & bv, VSpace const & vsp)
1039 {
1040         if (vsp.kind() == VSpace::NONE)
1041                 return 0;
1042
1043         int const arrow_size = 4;
1044         int const space_size = vsp.inPixels(bv);
1045
1046         LyXFont font;
1047         font.decSize();
1048         int const min_size = max(3 * arrow_size,
1049                 font_metrics::maxAscent(font)
1050                 + font_metrics::maxDescent(font));
1051
1052         if (vsp.length().len().value() < 0.0)
1053                 return min_size;
1054         else
1055                 return max(min_size, space_size);
1056 }