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