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