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