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