]> git.lyx.org Git - lyx.git/blob - src/rowpainter.C
9f970230c3ffb6f0bce875cde810776043ece499
[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 "coordcache.h"
18 #include "cursor.h"
19 #include "debug.h"
20 #include "bufferparams.h"
21 #include "BufferView.h"
22 #include "encoding.h"
23 #include "gettext.h"
24 #include "language.h"
25 #include "LColor.h"
26 #include "lyxrc.h"
27 #include "lyxrow.h"
28 #include "lyxrow_funcs.h"
29 #include "metricsinfo.h"
30 #include "paragraph.h"
31 #include "paragraph_funcs.h"
32 #include "ParagraphParameters.h"
33 #include "vspace.h"
34
35 #include "frontends/font_metrics.h"
36 #include "frontends/nullpainter.h"
37 #include "frontends/Painter.h"
38
39 #include "insets/insettext.h"
40
41 #include "support/textutils.h"
42
43 using lyx::pos_type;
44 using lyx::par_type;
45
46 using std::endl;
47 using std::max;
48 using std::string;
49
50
51 namespace {
52
53 /**
54  * A class used for painting an individual row of text.
55  */
56 class RowPainter {
57 public:
58         /// initialise and run painter
59         RowPainter(BufferView const & bv, Painter & pain, LyXText const & text,
60                 par_type pit, RowList::iterator rit, int y);
61 private:
62         // paint various parts
63         void paintBackground();
64         void paintSelection();
65         void paintAppendix();
66         void paintDepthBar();
67         void paintChangeBar();
68         void paintFirst();
69         void paintLast();
70         void paintForeignMark(double orig_x, LyXFont const & orig_font);
71         void paintHebrewComposeChar(lyx::pos_type & vpos);
72         void paintArabicComposeChar(lyx::pos_type & vpos);
73         void paintChars(lyx::pos_type & vpos, bool hebrew, bool arabic);
74         int paintAppendixStart(int y);
75         void paintText();
76         void paintFromPos(lyx::pos_type & vpos);
77         void paintInset(lyx::pos_type const pos);
78
79         /// return left margin
80         int leftMargin() const;
81
82         /// return the font at the given pos
83         LyXFont const getFont(lyx::pos_type pos) const;
84
85         /// return the label font for this row
86         LyXFont const getLabelFont() const;
87
88         /// return pixel width for the given pos
89         int singleWidth(lyx::pos_type pos) const;
90         int singleWidth(lyx::pos_type pos, char c) const;
91
92         /// bufferview to paint on
93         BufferView const & bv_;
94
95         /// Painter to use
96         Painter & pain_;
97
98         /// LyXText for the row
99         LyXText const & text_;
100         ParagraphList & pars_;
101
102         /// The row to paint
103         RowList::iterator const rit_;
104         Row & row_;
105
106         /// Row's paragraph
107         mutable par_type pit_;
108
109         // Looks ugly - is
110         double xo_;
111         int yo_;
112         double x_;
113         int width_;
114         double separator_;
115         double hfill_;
116         double label_hfill_;
117 };
118
119
120 RowPainter::RowPainter(BufferView const & bv, Painter & pain,
121         LyXText const & text, par_type pit, RowList::iterator rit, int y)
122         : bv_(bv), pain_(pain), text_(text), pars_(text.paragraphs()),
123           rit_(rit), row_(*rit), pit_(pit),
124           xo_(text_.xo_), yo_(y), width_(text_.width())
125 {
126         //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo << " yo: " << yo
127         //      << " pit->y: " << pit_->y
128         //      << " row: " << (pars_[pit_].size() ? pars_[pit_].getChar(row_.pos()) : 'X') << endl;
129
130         RowMetrics m = text_.computeRowMetrics(pit, row_);
131         x_ = m.x + xo_;
132
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         theCoords.insets_.add(inset, int(x_), yo_ + row_.baseline());
204         inset->drawSelection(pi, int(x_), yo_ + row_.baseline());
205         inset->draw(pi, int(x_), yo_ + row_.baseline());
206         x_ += inset->width();
207 }
208
209
210 void RowPainter::paintHebrewComposeChar(pos_type & vpos)
211 {
212         pos_type pos = text_.bidi.vis2log(vpos);
213
214         string str;
215
216         // first char
217         char c = pars_[pit_].getChar(pos);
218         str += c;
219         ++vpos;
220
221         LyXFont const & font = getFont(pos);
222         int const width = font_metrics::width(c, font);
223         int dx = 0;
224
225         for (pos_type i = pos - 1; i >= 0; --i) {
226                 c = pars_[pit_].getChar(i);
227                 if (!Encodings::IsComposeChar_hebrew(c)) {
228                         if (IsPrintableNonspace(c)) {
229                                 int const width2 = singleWidth(i, c);
230                                 // dalet / resh
231                                 dx = (c == 'ø' || c == 'ã')
232                                         ? width2 - width
233                                         : (width2 - width) / 2;
234                         }
235                         break;
236                 }
237         }
238
239         // Draw nikud
240         pain_.text(int(x_) + dx, yo_ + row_.baseline(), str, font);
241 }
242
243
244 void RowPainter::paintArabicComposeChar(pos_type & vpos)
245 {
246         pos_type pos = text_.bidi.vis2log(vpos);
247         string str;
248
249         // first char
250         char c = pars_[pit_].getChar(pos);
251         c = pars_[pit_].transformChar(c, pos);
252         str += c;
253         ++vpos;
254
255         LyXFont const & font = getFont(pos);
256         int const width = font_metrics::width(c, font);
257         int dx = 0;
258
259         for (pos_type i = pos - 1; i >= 0; --i) {
260                 c = pars_[pit_].getChar(i);
261                 if (!Encodings::IsComposeChar_arabic(c)) {
262                         if (IsPrintableNonspace(c)) {
263                                 int const width2 = singleWidth(i, c);
264                                 dx = (width2 - width) / 2;
265                         }
266                         break;
267                 }
268         }
269         // Draw nikud
270         pain_.text(int(x_) + dx, yo_ + row_.baseline(), str, font);
271 }
272
273
274 void RowPainter::paintChars(pos_type & vpos, bool hebrew, bool arabic)
275 {
276         pos_type pos = text_.bidi.vis2log(vpos);
277         pos_type const end = row_.endpos();
278         LyXFont orig_font = getFont(pos);
279
280         // first character
281         string str;
282         str += pars_[pit_].getChar(pos);
283         if (arabic) {
284                 unsigned char c = str[0];
285                 str[0] = pars_[pit_].transformChar(c, pos);
286         }
287
288         bool prev_struckout = isDeletedText(pars_[pit_], pos);
289         bool prev_newtext = isInsertedText(pars_[pit_], pos);
290
291         // collect as much similar chars as we can
292         for (++vpos; vpos < end && (pos = text_.bidi.vis2log(vpos)) >= 0; ++vpos) {
293                 char c = pars_[pit_].getChar(pos);
294
295                 if (!IsPrintableNonspace(c))
296                         break;
297
298                 if (prev_struckout != isDeletedText(pars_[pit_], pos))
299                         break;
300
301                 if (prev_newtext != isInsertedText(pars_[pit_], pos))
302                         break;
303
304                 if (arabic && Encodings::IsComposeChar_arabic(c))
305                         break;
306
307                 if (hebrew && Encodings::IsComposeChar_hebrew(c))
308                         break;
309
310                 if (orig_font != getFont(pos))
311                         break;
312
313                 if (arabic)
314                         c = pars_[pit_].transformChar(c, pos);
315
316                 str += c;
317         }
318
319         if (prev_struckout)
320                 orig_font.setColor(LColor::strikeout);
321         else if (prev_newtext)
322                 orig_font.setColor(LColor::newtext);
323
324         // Draw text and set the new x position
325         //lyxerr << "paint row: yo_ " << yo_ << " baseline: " << row_.baseline()
326         //      << "\n";
327         pain_.text(int(x_), yo_ + row_.baseline(), str, orig_font);
328         x_ += font_metrics::width(str, orig_font);
329 }
330
331
332 void RowPainter::paintForeignMark(double orig_x, LyXFont const & orig_font)
333 {
334         if (!lyxrc.mark_foreign_language)
335                 return;
336         if (orig_font.language() == latex_language)
337                 return;
338         if (orig_font.language() == bv_.buffer()->params().language)
339                 return;
340
341         int const y = yo_ + row_.baseline() + 1;
342         pain_.line(int(orig_x), y, int(x_), y, LColor::language);
343 }
344
345
346 void RowPainter::paintFromPos(pos_type & vpos)
347 {
348         pos_type const pos = text_.bidi.vis2log(vpos);
349
350         LyXFont const & orig_font = getFont(pos);
351
352         double const orig_x = x_;
353
354         char const c = pars_[pit_].getChar(pos);
355
356         if (c == Paragraph::META_INSET) {
357                 paintInset(pos);
358                 ++vpos;
359                 paintForeignMark(orig_x, orig_font);
360                 return;
361         }
362
363         // usual characters, no insets
364
365         // special case languages
366         bool const hebrew = (orig_font.language()->lang() == "hebrew");
367         bool const arabic =
368                 orig_font.language()->lang() == "arabic" &&
369                 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
370                 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
371
372         // draw as many chars as we can
373         if ((!hebrew && !arabic)
374                 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
375                 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
376                 paintChars(vpos, hebrew, arabic);
377         } else if (hebrew) {
378                 paintHebrewComposeChar(vpos);
379         } else if (arabic) {
380                 paintArabicComposeChar(vpos);
381         }
382
383         paintForeignMark(orig_x, orig_font);
384 }
385
386
387 void RowPainter::paintBackground()
388 {
389         int const x = int(xo_);
390         int const y = yo_ < 0 ? 0 : yo_;
391         int const h = yo_ < 0 ? row_.height() + yo_ : row_.height();
392         pain_.fillRectangle(x, y, width_, h, text_.backgroundColor());
393 }
394
395
396 void RowPainter::paintSelection()
397 {
398         bool const is_rtl = text_.isRTL(pars_[pit_]);
399
400         // the current selection
401         LCursor const & cur = bv_.cursor();
402         int const starty = text_.cursorY(cur.selBegin());
403         int const endy = text_.cursorY(cur.selEnd());
404         par_type startpit = cur.selBegin().par();
405         par_type endpit = cur.selEnd().par();
406         RowList::iterator startrow = pars_[startpit].getRow(cur.selBegin().pos());
407         RowList::iterator endrow = pars_[endpit].getRow(cur.selEnd().pos());
408         int const h = row_.height();
409
410         int const row_y = text_.yo_ + pars_[pit_].y + row_.y_offset();
411
412         bool const sel_starts_here = startpit == pit_ && startrow == rit_;
413         bool const sel_ends_here   = endpit == pit_ && endrow == rit_;
414         bool const sel_on_one_row  = sel_starts_here && sel_ends_here;
415
416         if (text_.bidi.same_direction()) {
417                 if (sel_on_one_row) {
418                         int const startx = text_.cursorX(cur.selBegin());
419                         int const endx = text_.cursorX(cur.selEnd());
420                         int const x1 = is_rtl ? endx : startx;
421                         int const x2 = is_rtl ? startx : endx;
422                         pain_.fillRectangle(x1, yo_, x2 - x1, h, LColor::selection);
423                 } else if (sel_starts_here) {
424                         int const startx = text_.cursorX(cur.selBegin());
425                         int const x1 = is_rtl ? int(xo_) : startx;
426                         int const x2 = is_rtl ? startx : int(xo_) + width_;
427                         pain_.fillRectangle(x1, yo_, x2 - x1, h, LColor::selection);
428                 } else if (sel_ends_here) {
429                         int const endx = text_.cursorX(cur.selEnd());
430                         int const x1 = is_rtl ? endx : int(xo_);
431                         int const x2 = is_rtl ? int(xo_) + width_ : endx;
432                         pain_.fillRectangle(x1, yo_, x2 - x1, h, LColor::selection);
433                 } else if (row_y > starty && row_y < endy) {
434                         pain_.fillRectangle(int(xo_), yo_, width_, h, LColor::selection);
435                 }
436                 return;
437         }
438
439         if ((startpit != pit_ && startrow != rit_ && !is_rtl)
440                 || (endpit != pit_ && endrow != rit_ && is_rtl))
441                 pain_.fillRectangle(int(xo_), yo_,
442                         int(x_), h, LColor::selection);
443
444         pos_type const body_pos = pars_[pit_].beginOfBody();
445         pos_type const end = row_.endpos();
446         double tmpx = x_;
447
448         for (pos_type vpos = row_.pos(); vpos < end; ++vpos)  {
449                 pos_type pos = text_.bidi.vis2log(vpos);
450                 double const old_tmpx = tmpx;
451                 if (body_pos > 0 && pos == body_pos - 1) {
452                         LyXLayout_ptr const & layout = pars_[pit_].layout();
453                         LyXFont const lfont = getLabelFont();
454
455                         tmpx += label_hfill_ + font_metrics::width(layout->labelsep, lfont);
456
457                         if (pars_[pit_].isLineSeparator(body_pos - 1))
458                                 tmpx -= singleWidth(body_pos - 1);
459                 }
460
461                 tmpx += singleWidth(pos);
462
463                 if (hfillExpansion(pars_[pit_], row_, pos)) {
464                         if (pos >= body_pos)
465                                 tmpx += hfill_;
466                         else
467                                 tmpx += label_hfill_;
468                 } else {
469                         if (pars_[pit_].isSeparator(pos) && pos >= body_pos)
470                                 tmpx += separator_;
471                 }
472
473                 if (((startpit != pit_ && startrow != rit_)
474                                 || cur.selBegin().pos() <= pos) &&
475                         ((endpit != pit_ && endrow != rit_)
476                                 || pos < cur.selEnd().pos())) {
477                         // Here we do not use x_ as xo_ was added to x_.
478                         pain_.fillRectangle(int(old_tmpx), yo_,
479                                 int(tmpx - old_tmpx + 1), h, LColor::selection);
480                 }
481         }
482
483         if ((startpit != pit_ && startrow != rit_ && is_rtl) ||
484             (endpit != pit_ && endrow != rit_ && !is_rtl)) {
485                 pain_.fillRectangle(int(xo_ + tmpx),
486                         yo_, int(width_ - tmpx), h, LColor::selection);
487         }
488 }
489
490
491 void RowPainter::paintChangeBar()
492 {
493         pos_type const start = row_.pos();
494         pos_type const end = row_.endpos();
495
496         if (start == end || !pars_[pit_].isChanged(start, end - 1))
497                 return;
498
499         int const height = text_.isLastRow(pit_, row_)
500                 ? row_.baseline()
501                 : row_.height() + boost::next(rit_)->top_of_text();
502
503         pain_.fillRectangle(4, yo_, 5, height, LColor::changebar);
504 }
505
506
507 void RowPainter::paintAppendix()
508 {
509         if (!pars_[pit_].params().appendix())
510                 return;
511
512         int y = yo_;
513
514         if (pars_[pit_].params().startOfAppendix())
515                 y += 2 * defaultRowHeight();
516
517         pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
518         pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), LColor::appendix);
519 }
520
521
522 void RowPainter::paintDepthBar()
523 {
524         Paragraph::depth_type const depth = pars_[pit_].getDepth();
525
526         if (depth <= 0)
527                 return;
528
529         Paragraph::depth_type prev_depth = 0;
530         if (!text_.isFirstRow(pit_, row_)) {
531                 par_type pit2 = pit_;
532                 if (row_.pos() == 0)
533                         --pit2;
534                 prev_depth = pars_[pit2].getDepth();
535         }
536
537         Paragraph::depth_type next_depth = 0;
538         if (!text_.isLastRow(pit_, row_)) {
539                 par_type pit2 = pit_;
540                 if (row_.endpos() >= pars_[pit2].size())
541                         ++pit2;
542                 next_depth = pars_[pit2].getDepth();
543         }
544
545         for (Paragraph::depth_type i = 1; i <= depth; ++i) {
546                 int const w = nestMargin() / 5;
547                 int x = xo_ + w * i;
548                 // only consider the changebar space if we're drawing outer left
549                 if (xo_ == 0)
550                         x += changebarMargin();
551
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 = pars_[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 = pars_[pit_].layout();
601
602         if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
603                 if (pit_ != 0) {
604                         if (layout->latextype == LATEX_PARAGRAPH
605                                 && !pars_[pit_].getDepth()) {
606                                 y_top += buffer.params().getDefSkip().inPixels(bv_);
607                         } else {
608                                 LyXLayout_ptr const & playout = pars_[pit_ - 1].layout();
609                                 if (playout->latextype == LATEX_PARAGRAPH
610                                         && !pars_[pit_ - 1].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         bool const is_rtl = text_.isRTL(pars_[pit_]);
619         bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
620         //lyxerr << "paintFirst: " << pars_[pit_].id() << " is_seq: " << is_seq << std::endl;
621
622         // should we print a label?
623         if (layout->labeltype >= LABEL_STATIC
624             && (layout->labeltype != LABEL_STATIC
625                       || layout->latextype != LATEX_ENVIRONMENT
626                       || is_seq)) {
627
628                 LyXFont font = getLabelFont();
629                 if (!pars_[pit_].getLabelstring().empty()) {
630                         double x = x_;
631                         string const str = pars_[pit_].getLabelstring();
632
633                         // this is special code for the chapter layout. This is
634                         // printed in an extra row and has a pagebreak at
635                         // the top.
636                         if (layout->counter == "chapter") {
637                                 if (buffer.params().secnumdepth >= 0) {
638                                         float spacing_val = 1.0;
639                                         if (!parparams.spacing().isDefault()) {
640                                                 spacing_val = parparams.spacing().getValue();
641                                         } else {
642                                                 spacing_val = buffer.params().spacing().getValue();
643                                         }
644
645                                         int const maxdesc =
646                                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
647                                                 + int(layout->parsep) * defaultRowHeight();
648
649                                         if (is_rtl) {
650                                                 x = width_ - leftMargin() -
651                                                         font_metrics::width(str, font);
652                                         }
653
654                                         pain_.text(int(x),
655                                                 yo_ + row_.baseline() -
656                                                 row_.ascent_of_text() - maxdesc,
657                                                 str, font);
658                                 }
659                         } else {
660                                 if (is_rtl) {
661                                         x = width_ - leftMargin()
662                                                 + font_metrics::width(layout->labelsep, font);
663                                 } else {
664                                         x = x_ - font_metrics::width(layout->labelsep, font)
665                                                 - font_metrics::width(str, font);
666                                 }
667
668                                 pain_.text(int(x), yo_ + row_.baseline(), str, font);
669                         }
670                 }
671
672         // the labels at the top of an environment.
673         // More or less for bibliography
674         } else if (is_seq &&
675                 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
676                 layout->labeltype == LABEL_BIBLIO ||
677                 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
678                 LyXFont font = getLabelFont();
679                 if (!pars_[pit_].getLabelstring().empty()) {
680                         string const str = pars_[pit_].getLabelstring();
681                         float spacing_val = 1.0;
682                         if (!parparams.spacing().isDefault()) {
683                                 spacing_val = parparams.spacing().getValue();
684                         } else {
685                                 spacing_val = buffer.params().spacing().getValue();
686                         }
687
688                         int maxdesc =
689                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
690                                 + (layout->labelbottomsep * defaultRowHeight()));
691
692                         double x = x_;
693                         if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
694                                 x = ((is_rtl ? leftMargin() : x_)
695                                          + width_ - text_.rightMargin(pars_[pit_])) / 2;
696                                 x -= font_metrics::width(str, font) / 2;
697                         } else if (is_rtl) {
698                                 x = width_ - leftMargin() -
699                                         font_metrics::width(str, font);
700                         }
701                         pain_.text(int(x),
702                             yo_ + row_.baseline() - row_.ascent_of_text() - maxdesc,
703                                   str, font);
704                 }
705         }
706 }
707
708
709 void RowPainter::paintLast()
710 {
711         bool const is_rtl = text_.isRTL(pars_[pit_]);
712         int const endlabel = getEndLabel(pit_, text_.paragraphs());
713
714         // draw an endlabel
715         switch (endlabel) {
716         case END_LABEL_BOX:
717         case END_LABEL_FILLED_BOX: {
718                 LyXFont const font = getLabelFont();
719                 int const size = int(0.75 * font_metrics::maxAscent(font));
720                 int const y = yo_ + row_.baseline() - size;
721                 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
722
723                 if (width_ - int(row_.width()) <= size)
724                         x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
725
726                 if (endlabel == END_LABEL_BOX)
727                         pain_.rectangle(x, y, size, size, LColor::eolmarker);
728                 else
729                         pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
730                 break;
731         }
732
733         case END_LABEL_STATIC: {
734                 LyXFont font = getLabelFont();
735                 string const & str = pars_[pit_].layout()->endlabelstring();
736                 double const x = is_rtl ?
737                         x_ - font_metrics::width(str, font)
738                         : - text_.rightMargin(pars_[pit_]) - row_.width();
739                 pain_.text(int(x), yo_ + row_.baseline(), str, font);
740                 break;
741         }
742
743         case END_LABEL_NO_LABEL:
744                 break;
745         }
746 }
747
748
749 void RowPainter::paintText()
750 {
751         pos_type const end = row_.endpos();
752         pos_type body_pos = pars_[pit_].beginOfBody();
753         if (body_pos > 0 &&
754                 (body_pos > end || !pars_[pit_].isLineSeparator(body_pos - 1))) {
755                 body_pos = 0;
756         }
757
758         LyXLayout_ptr const & layout = pars_[pit_].layout();
759
760         bool running_strikeout = false;
761         bool is_struckout = false;
762         int last_strikeout_x = 0;
763
764         for (pos_type vpos = row_.pos(); vpos < end; ) {
765                 if (x_ > bv_.workWidth())
766                         break;
767
768                 pos_type pos = text_.bidi.vis2log(vpos);
769
770                 if (pos >= pars_[pit_].size()) {
771                         ++vpos;
772                         continue;
773                 }
774
775                 if (x_ + singleWidth(pos) < 0) {
776                         x_ += singleWidth(pos);
777                         ++vpos;
778                         continue;
779                 }
780
781                 is_struckout = isDeletedText(pars_[pit_], pos);
782
783                 if (is_struckout && !running_strikeout) {
784                         running_strikeout = true;
785                         last_strikeout_x = int(x_);
786                 }
787
788                 bool const highly_editable_inset = pars_[pit_].isInset(pos)
789                         && isHighlyEditableInset(pars_[pit_].getInset(pos));
790
791                 // if we reach the end of a struck out range, paint it
792                 // we also don't paint across things like tables
793                 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
794                         int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
795                         pain_.line(last_strikeout_x, middle, int(x_), middle,
796                                 LColor::strikeout, Painter::line_solid, Painter::line_thin);
797                         running_strikeout = false;
798                 }
799
800                 if (body_pos > 0 && pos == body_pos - 1) {
801                         int const lwidth = font_metrics::width(layout->labelsep,
802                                 getLabelFont());
803
804                         x_ += label_hfill_ + lwidth - singleWidth(body_pos - 1);
805                 }
806
807                 if (pars_[pit_].isHfill(pos)) {
808                         x_ += 1;
809
810                         int const y0 = yo_ + row_.baseline();
811                         int const y1 = y0 - defaultRowHeight() / 2;
812
813                         pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
814
815                         if (hfillExpansion(pars_[pit_], row_, pos)) {
816                                 int const y2 = (y0 + y1) / 2;
817
818                                 if (pos >= body_pos) {
819                                         pain_.line(int(x_), y2, int(x_ + hfill_), y2,
820                                                   LColor::added_space,
821                                                   Painter::line_onoffdash);
822                                         x_ += hfill_;
823                                 } else {
824                                         pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
825                                                   LColor::added_space,
826                                                   Painter::line_onoffdash);
827                                         x_ += label_hfill_;
828                                 }
829                                 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
830                         }
831                         x_ += 2;
832                         ++vpos;
833                 } else if (pars_[pit_].isSeparator(pos)) {
834                         x_ += singleWidth(pos);
835                         if (pos >= body_pos)
836                                 x_ += separator_;
837                         ++vpos;
838                 } else {
839                         paintFromPos(vpos);
840                 }
841         }
842
843         // if we reach the end of a struck out range, paint it
844         if (running_strikeout) {
845                 int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
846                 pain_.line(last_strikeout_x, middle, int(x_), middle,
847                         LColor::strikeout, Painter::line_solid, Painter::line_thin);
848                 running_strikeout = false;
849         }
850 }
851
852
853 int paintPars(BufferView const & bv, Painter & pain,
854               LyXText const & text, par_type pit, par_type end)
855 {
856         //lyxerr << "  paintRows: pit: " << &*pit << endl;
857         ParagraphList & pars = text.paragraphs();
858
859         int y = pars[pit].y + text.yo_ - bv.top_y();
860
861         for (; pit != end; ++pit) {
862                 RowList::iterator row = pars[pit].rows.begin();
863                 RowList::iterator rend = pars[pit].rows.end();
864
865                 // We draw full paragraphs to get the (xo, yo) cache of all
866                 // contained insets right. This is needed for properly working
867                 // editXY. And maybe not even sufficient.
868                 // FIXME: Alfredo, please have a look at the coordinate business
869                 // again.
870                 for ( ; row != rend; ++row) {
871                         RowPainter(bv, pain, text, pit, row, y);
872                         y += row->height();
873                 }
874
875         }
876         return y;
877 }
878
879 } // namespace anon
880
881
882 void refreshPar(BufferView const & bv, LyXText const & text, par_type pit)
883 {
884         static NullPainter nop;
885         paintPars(bv, nop, text, pit, pit + 1);
886 }
887
888
889 int paintText(BufferView const & bv)
890 {
891         par_type pit, end;
892         getParsInRange(bv.text()->paragraphs(), bv.top_y(),
893                        bv.top_y() + bv.workHeight(), pit, end);
894         //lyxerr << "top_y: " << bv.top_y() << " y: " << pit->y << endl;
895         return paintPars(bv, bv.painter(), *bv.text(), pit, end);
896 }
897
898
899 void paintTextInset(LyXText const & text, PainterInfo & pi)
900 {
901         paintPars(*pi.base.bv, pi.pain, text, 0, text.paragraphs().size());
902 }