]> git.lyx.org Git - lyx.git/blob - src/rowpainter.C
cf4d8bfe7d0d097caa68d1a303ed8612505dd5a9
[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_.cursor().selection() && &text_ == bv_.cursor().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         LCursor const & cur = bv_.cursor();
395         int const startx = text_.cursorX(cur.selStart());
396         int const endx = text_.cursorX(cur.selEnd());
397         int const starty = text_.cursorY(cur.selStart());
398         int const endy = text_.cursorY(cur.selEnd());
399         ParagraphList::iterator startpit = text_.getPar(cur.selStart());
400         ParagraphList::iterator endpit = text_.getPar(cur.selEnd());
401         RowList::iterator startrow = startpit->getRow(cur.selStart().pos());
402         RowList::iterator endrow = endpit->getRow(cur.selEnd().pos());
403         int const h = row_.height();
404
405         int const row_y = pit_->y + row_.y_offset();
406
407         bool const sel_starts_here = startpit == pit_ && startrow == rit_;
408         bool const sel_ends_here   = endpit == pit_ && endrow == rit_;
409         bool const sel_on_one_row  = sel_starts_here && sel_ends_here;
410
411         if (text_.bidi.same_direction()) {
412                 int x;
413                 int w;
414                 if (sel_on_one_row) {
415                         if (startx < endx) {
416                                 x = int(xo_) + startx;
417                                 w = endx - startx;
418                         } else {
419                                 x = int(xo_) + endx;
420                                 w = startx - endx;
421                         }
422                         pain_.fillRectangle(x, yo_, w, h, LColor::selection);
423                 } else if (sel_starts_here) {
424                         int const x = is_rtl ? int(xo_) : int(xo_ + startx);
425                         int const w = is_rtl ? startx : (width_ - startx);
426                         pain_.fillRectangle(x, yo_, w, h, LColor::selection);
427                 } else if (sel_ends_here) {
428                         int const x = is_rtl ? int(xo_ + endx) : int(xo_);
429                         int const w = is_rtl ? (width_ - endx) : endx;
430                         pain_.fillRectangle(x, yo_, w, h, LColor::selection);
431                 } else if (row_y > starty && row_y < endy) {
432                         pain_.fillRectangle(int(xo_), yo_, width_, h, LColor::selection);
433                 }
434                 return;
435         }
436
437         if ((startpit != pit_ && startrow != rit_ && !is_rtl)
438                 || (endpit != pit_ && endrow != rit_ && is_rtl))
439                 pain_.fillRectangle(int(xo_), yo_,
440                         int(x_), h, LColor::selection);
441
442         pos_type const body_pos = pit_->beginOfBody();
443         pos_type const end = row_.endpos();
444         double tmpx = x_;
445
446         for (pos_type vpos = row_.pos(); vpos < end; ++vpos)  {
447                 pos_type pos = text_.bidi.vis2log(vpos);
448                 double const old_tmpx = tmpx;
449                 if (body_pos > 0 && pos == body_pos - 1) {
450                         LyXLayout_ptr const & layout = pit_->layout();
451                         LyXFont const lfont = getLabelFont();
452
453                         tmpx += label_hfill_ + font_metrics::width(layout->labelsep, lfont);
454
455                         if (pit_->isLineSeparator(body_pos - 1))
456                                 tmpx -= singleWidth(body_pos - 1);
457                 }
458
459                 tmpx += singleWidth(pos);
460
461                 if (hfillExpansion(*pit_, row_, pos)) {
462                         if (pos >= body_pos)
463                                 tmpx += hfill_;
464                         else
465                                 tmpx += label_hfill_;
466                 } else {
467                         if (pit_->isSeparator(pos) && pos >= body_pos)
468                                 tmpx += separator_;
469                 }
470
471                 if (((startpit != pit_ && startrow != rit_)
472                                 || cur.selStart().pos() <= pos) &&
473                         ((endpit != pit_ && endrow != rit_)
474                                 || pos < cur.selEnd().pos())) {
475                         // Here we do not use x_ as xo_ was added to x_.
476                         pain_.fillRectangle(int(old_tmpx), yo_,
477                                 int(tmpx - old_tmpx + 1), h, LColor::selection);
478                 }
479         }
480
481         if ((startpit != pit_ && startrow != rit_ && is_rtl) ||
482             (endpit != pit_ && endrow != rit_ && !is_rtl)) {
483                 pain_.fillRectangle(int(xo_ + tmpx),
484                         yo_, int(width_ - tmpx), h, 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 = width_;
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 = NEST_MARGIN / 5;
548                 int x = int(w * i + xo_);
549                 // only consider the changebar space if we're drawing outer left
550                 if (xo_ == 0)
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::paintAppendixStart(int y)
565 {
566         LyXFont pb_font;
567         pb_font.setColor(LColor::appendix);
568         pb_font.decSize();
569
570         string const label = _("Appendix");
571         int w = 0;
572         int a = 0;
573         int d = 0;
574         font_metrics::rectText(label, pb_font, w, a, d);
575
576         int const text_start = int(xo_ + (width_ - w) / 2);
577         int const text_end = text_start + w;
578
579         pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
580
581         pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
582         pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
583
584         return 3 * defaultRowHeight();
585 }
586
587
588 void RowPainter::paintFirst()
589 {
590         ParagraphParameters const & parparams = pit_->params();
591
592         int y_top = 0;
593
594         // start of appendix?
595         if (parparams.startOfAppendix())
596                 y_top += paintAppendixStart(yo_ + y_top + 2 * defaultRowHeight());
597
598         Buffer const & buffer = *bv_.buffer();
599
600         LyXLayout_ptr const & layout = pit_->layout();
601
602         if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
603                 if (pit_ != text_.paragraphs().begin()) {
604                         if (layout->latextype == LATEX_PARAGRAPH
605                                 && !pit_->getDepth()) {
606                                 y_top += buffer.params().getDefSkip().inPixels(bv_);
607                         } else {
608                                 LyXLayout_ptr const & playout =
609                                         boost::prior(pit_)->layout();
610                                 if (playout->latextype == LATEX_PARAGRAPH
611                                         && !boost::prior(pit_)->getDepth()) {
612                                         // is it right to use defskip here, too? (AS)
613                                         y_top += buffer.params().getDefSkip().inPixels(bv_);
614                                 }
615                         }
616                 }
617         }
618
619         int const ww = bv_.workWidth();
620
621         bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params());
622         bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
623         //lyxerr << "paintFirst: " << pit_->id() << " is_seq: " << is_seq << std::endl;
624
625         // should we print a label?
626         if (layout->labeltype >= LABEL_STATIC
627             && (layout->labeltype != LABEL_STATIC
628                       || layout->latextype != LATEX_ENVIRONMENT
629                       || is_seq)) {
630
631                 LyXFont font = getLabelFont();
632                 if (!pit_->getLabelstring().empty()) {
633                         double x = x_;
634                         string const str = pit_->getLabelstring();
635
636                         // this is special code for the chapter layout. This is
637                         // printed in an extra row and has a pagebreak at
638                         // the top.
639                         if (layout->counter == "chapter") {
640                                 if (buffer.params().secnumdepth >= 0) {
641                                         float spacing_val = 1.0;
642                                         if (!parparams.spacing().isDefault()) {
643                                                 spacing_val = parparams.spacing().getValue();
644                                         } else {
645                                                 spacing_val = buffer.params().spacing().getValue();
646                                         }
647
648                                         int const maxdesc =
649                                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
650                                                 + int(layout->parsep) * defaultRowHeight();
651
652                                         if (is_rtl) {
653                                                 x = ww - leftMargin() -
654                                                         font_metrics::width(str, font);
655                                         }
656
657                                         pain_.text(int(x),
658                                                 yo_ + row_.baseline() -
659                                                 row_.ascent_of_text() - maxdesc,
660                                                 str, font);
661                                 }
662                         } else {
663                                 if (is_rtl) {
664                                         x = ww - leftMargin()
665                                                 + font_metrics::width(layout->labelsep, font);
666                                 } else {
667                                         x = x_ - font_metrics::width(layout->labelsep, font)
668                                                 - font_metrics::width(str, font);
669                                 }
670
671                                 pain_.text(int(x), yo_ + row_.baseline(), str, font);
672                         }
673                 }
674
675         // the labels at the top of an environment.
676         // More or less for bibliography
677         } else if (is_seq &&
678                 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
679                 layout->labeltype == LABEL_BIBLIO ||
680                 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
681                 LyXFont font = getLabelFont();
682                 if (!pit_->getLabelstring().empty()) {
683                         string const str = pit_->getLabelstring();
684                         float spacing_val = 1.0;
685                         if (!parparams.spacing().isDefault()) {
686                                 spacing_val = parparams.spacing().getValue();
687                         } else {
688                                 spacing_val = buffer.params().spacing().getValue();
689                         }
690
691                         int maxdesc =
692                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
693                                 + (layout->labelbottomsep * defaultRowHeight()));
694
695                         double x = x_;
696                         if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
697                                 x = ((is_rtl ? leftMargin() : x_)
698                                          + ww - text_.rightMargin(*pit_)) / 2;
699                                 x -= font_metrics::width(str, font) / 2;
700                         } else if (is_rtl) {
701                                 x = ww - leftMargin() -
702                                         font_metrics::width(str, font);
703                         }
704                         pain_.text(int(x),
705                             yo_ + row_.baseline() - row_.ascent_of_text() - maxdesc,
706                                   str, font);
707                 }
708         }
709 }
710
711
712 void RowPainter::paintLast()
713 {
714         int const ww = bv_.workWidth();
715         bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params());
716         int const endlabel = getEndLabel(pit_, text_.paragraphs());
717
718         // draw an endlabel
719         switch (endlabel) {
720         case END_LABEL_BOX:
721         case END_LABEL_FILLED_BOX: {
722                 LyXFont const font = getLabelFont();
723                 int const size = int(0.75 * font_metrics::maxAscent(font));
724                 int const y = yo_ + row_.baseline() - size;
725                 int x = is_rtl ? NEST_MARGIN + CHANGEBAR_MARGIN: ww - size;
726
727                 if (row_.fill() <= size)
728                         x += (size - row_.fill() + 1) * (is_rtl ? -1 : 1);
729
730                 if (endlabel == END_LABEL_BOX)
731                         pain_.rectangle(x, y, size, size, LColor::eolmarker);
732                 else
733                         pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
734                 break;
735         }
736
737         case END_LABEL_STATIC: {
738                 LyXFont font = getLabelFont();
739                 string const & str = pit_->layout()->endlabelstring();
740                 double const x = is_rtl ?
741                         x_ - font_metrics::width(str, font)
742                         : ww - text_.rightMargin(*pit_) - row_.fill();
743                 pain_.text(int(x), yo_ + row_.baseline(), str, font);
744                 break;
745         }
746
747         case END_LABEL_NO_LABEL:
748                 break;
749         }
750 }
751
752
753 void RowPainter::paintText()
754 {
755         pos_type const end = row_.endpos();
756         pos_type body_pos = pit_->beginOfBody();
757         if (body_pos > 0 &&
758                 (body_pos > end || !pit_->isLineSeparator(body_pos - 1))) {
759                 body_pos = 0;
760         }
761
762         LyXLayout_ptr const & layout = pit_->layout();
763
764         bool running_strikeout = false;
765         bool is_struckout = false;
766         int last_strikeout_x = 0;
767
768         for (pos_type vpos = row_.pos(); vpos < end; ) {
769                 if (x_ > bv_.workWidth())
770                         break;
771
772                 pos_type pos = text_.bidi.vis2log(vpos);
773
774                 if (pos >= pit_->size()) {
775                         ++vpos;
776                         continue;
777                 }
778
779                 if (x_ + singleWidth(pos) < 0) {
780                         x_ += singleWidth(pos);
781                         ++vpos;
782                         continue;
783                 }
784
785                 is_struckout = isDeletedText(*pit_, pos);
786
787                 if (is_struckout && !running_strikeout) {
788                         running_strikeout = true;
789                         last_strikeout_x = int(x_);
790                 }
791
792                 bool const highly_editable_inset = pit_->isInset(pos)
793                         && isHighlyEditableInset(pit_->getInset(pos));
794
795                 // if we reach the end of a struck out range, paint it
796                 // we also don't paint across things like tables
797                 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
798                         int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
799                         pain_.line(last_strikeout_x, middle, int(x_), middle,
800                                 LColor::strikeout, Painter::line_solid, Painter::line_thin);
801                         running_strikeout = false;
802                 }
803
804                 if (body_pos > 0 && pos == body_pos - 1) {
805                         int const lwidth = font_metrics::width(layout->labelsep,
806                                 getLabelFont());
807
808                         x_ += label_hfill_ + lwidth - singleWidth(body_pos - 1);
809                 }
810
811                 if (pit_->isHfill(pos)) {
812                         x_ += 1;
813
814                         int const y0 = yo_ + row_.baseline();
815                         int const y1 = y0 - defaultRowHeight() / 2;
816
817                         pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
818
819                         if (hfillExpansion(*pit_, row_, pos)) {
820                                 int const y2 = (y0 + y1) / 2;
821
822                                 if (pos >= body_pos) {
823                                         pain_.line(int(x_), y2, int(x_ + hfill_), y2,
824                                                   LColor::added_space,
825                                                   Painter::line_onoffdash);
826                                         x_ += hfill_;
827                                 } else {
828                                         pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
829                                                   LColor::added_space,
830                                                   Painter::line_onoffdash);
831                                         x_ += label_hfill_;
832                                 }
833                                 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
834                         }
835                         x_ += 2;
836                         ++vpos;
837                 } else if (pit_->isSeparator(pos)) {
838                         x_ += singleWidth(pos);
839                         if (pos >= body_pos)
840                                 x_ += separator_;
841                         ++vpos;
842                 } else {
843                         paintFromPos(vpos);
844                 }
845         }
846
847         // if we reach the end of a struck out range, paint it
848         if (running_strikeout) {
849                 int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
850                 pain_.line(last_strikeout_x, middle, int(x_), middle,
851                         LColor::strikeout, Painter::line_solid, Painter::line_thin);
852                 running_strikeout = false;
853         }
854 }
855
856
857 int paintPars(BufferView const & bv, LyXText const & text,
858         ParagraphList::iterator pit, int xo, int yo, int y)
859 {
860         //lyxerr << "  paintRows: pit: " << &*pit << endl;
861         int const y2 = bv.painter().paperHeight();
862         y -= bv.top_y();
863
864         ParagraphList::iterator end = text.paragraphs().end();
865
866         for ( ; pit != end; ++pit) {
867                 RowList::iterator row = pit->rows.begin();
868                 RowList::iterator rend = pit->rows.end();
869
870                 for ( ; row != rend; ++row) {
871                         RowPainter(bv, text, pit, row, xo, y + yo);
872                         y += row->height();
873                 }
874                 if (yo + y >= y2)
875                         break;
876         }
877
878         return y;
879 }
880
881 } // namespace anon
882
883
884 int paintText(BufferView const & bv)
885 {
886         ParagraphList::iterator pit;
887         bv.text()->getRowNearY(bv.top_y(), pit);
888         return paintPars(bv, *bv.text(), pit, 0, 0, pit->y);
889 }
890
891
892 void paintTextInset(LyXText const & text, PainterInfo & pi, int xo, int yo)
893 {
894         paintPars(*pi.base.bv, text, text.paragraphs().begin(), xo, yo, 0);
895 }