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