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