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