]> git.lyx.org Git - lyx.git/blob - src/rowpainter.C
1ac042d13c1c28be2c754f5df3e90a6f582277c3
[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 using lyx::par_type;
43
44 using std::endl;
45 using std::max;
46 using std::string;
47
48 extern int NEST_MARGIN;
49 extern int CHANGEBAR_MARGIN;
50
51
52 namespace {
53
54 /**
55  * A class used for painting an individual row of text.
56  */
57 class RowPainter {
58 public:
59         /// initialise and run painter
60         RowPainter(BufferView const & bv, LyXText const & text,
61                 par_type pit, RowList::iterator rit, int xo, int yo);
62 private:
63         // paint various parts
64         void paintBackground();
65         void paintSelection();
66         void paintAppendix();
67         void paintDepthBar();
68         void paintChangeBar();
69         void paintFirst();
70         void paintLast();
71         void paintForeignMark(double orig_x, LyXFont const & orig_font);
72         void paintHebrewComposeChar(lyx::pos_type & vpos);
73         void paintArabicComposeChar(lyx::pos_type & vpos);
74         void paintChars(lyx::pos_type & vpos, bool hebrew, bool arabic);
75         int paintAppendixStart(int y);
76         void paintText();
77         void paintFromPos(lyx::pos_type & vpos);
78         void paintInset(lyx::pos_type const pos);
79
80         /// return left margin
81         int leftMargin() const;
82
83         /// return the font at the given pos
84         LyXFont const getFont(lyx::pos_type pos) const;
85
86         /// return the label font for this row
87         LyXFont const getLabelFont() const;
88
89         /// return pixel width for the given pos
90         int singleWidth(lyx::pos_type pos) const;
91         int singleWidth(lyx::pos_type pos, char c) const;
92
93         /// bufferview to paint on
94         BufferView const & bv_;
95
96         /// Painter to use
97         Painter & pain_;
98
99         /// LyXText for the row
100         LyXText const & text_;
101         ParagraphList & pars_;
102
103         /// The row to paint
104         RowList::iterator const rit_;
105         Row & row_;
106
107         /// Row's paragraph
108         mutable par_type pit_;
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, LyXText const & text,
122      par_type pit, RowList::iterator rit, int xo, int yo)
123         : bv_(bv), pain_(bv_.painter()), text_(text), pars_(text.paragraphs()),
124                 rit_(rit), row_(*rit), pit_(pit), xo_(xo), yo_(yo), width_(text_.width())
125 {
126         //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo << " yo: " << yo
127         //      << " pit->y: " << pit_->y
128         //      << " row: " << (pars_[pit_].size() ? pars_[pit_].getChar(row_.pos()) : 'X') << endl;
129         RowMetrics m = text_.computeRowMetrics(pit, row_);
130         x_ = m.x + xo_;
131         separator_ = m.separator;
132         hfill_ = m.hfill;
133         label_hfill_ = m.label_hfill;
134
135         // background has already been cleared.
136         if (&text_ == bv_.text())
137                 paintBackground();
138
139         // paint the selection background
140         if (bv_.cursor().selection() && &text_ == bv_.cursor().text())
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() >= pars_[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 = pars_[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 = pars_[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 = pars_[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 = pars_[pit_].getChar(pos);
248         c = pars_[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 = pars_[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 += pars_[pit_].getChar(pos);
280         if (arabic) {
281                 unsigned char c = str[0];
282                 str[0] = pars_[pit_].transformChar(c, pos);
283         }
284
285         bool prev_struckout = isDeletedText(pars_[pit_], pos);
286         bool prev_newtext = isInsertedText(pars_[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 = pars_[pit_].getChar(pos);
291
292                 if (!IsPrintableNonspace(c))
293                         break;
294
295                 if (prev_struckout != isDeletedText(pars_[pit_], pos))
296                         break;
297
298                 if (prev_newtext != isInsertedText(pars_[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 = pars_[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 = pars_[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 = text_.isRTL(pars_[pit_]);
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         par_type startpit = cur.selBegin().par();
404         par_type endpit = cur.selEnd().par();
405         RowList::iterator startrow = pars_[startpit].getRow(cur.selBegin().pos());
406         RowList::iterator endrow = pars_[endpit].getRow(cur.selEnd().pos());
407         int const h = row_.height();
408
409         int const row_y = text_.yo_ + pars_[pit_].y + row_.y_offset();
410
411         bool const sel_starts_here = startpit == pit_ && startrow == rit_;
412         bool const sel_ends_here   = endpit == pit_ && endrow == rit_;
413         bool const sel_on_one_row  = sel_starts_here && sel_ends_here;
414
415         if (text_.bidi.same_direction()) {
416                 if (sel_on_one_row) {
417                         int const x1 = is_rtl ? endx : startx;
418                         int const x2 = is_rtl ? startx : endx;
419                         pain_.fillRectangle(x1, yo_, x2 - x1, h, LColor::selection);
420                 } else if (sel_starts_here) {
421                         int const x1 = is_rtl ? int(xo_) : startx;
422                         int const x2 = is_rtl ? startx : int(xo_) + width_;
423                         pain_.fillRectangle(x1, yo_, x2 - x1, h, LColor::selection);
424                 } else if (sel_ends_here) {
425                         int const x1 = is_rtl ? endx : int(xo_);
426                         int const x2 = is_rtl ? int(xo_) + width_ : endx;
427                         pain_.fillRectangle(x1, yo_, x2 - x1, h, LColor::selection);
428                 } else if (row_y > starty && row_y < endy) {
429                         pain_.fillRectangle(int(xo_), yo_, width_, h, LColor::selection);
430                 }
431                 return;
432         }
433
434         if ((startpit != pit_ && startrow != rit_ && !is_rtl)
435                 || (endpit != pit_ && endrow != rit_ && is_rtl))
436                 pain_.fillRectangle(int(xo_), yo_,
437                         int(x_), h, LColor::selection);
438
439         pos_type const body_pos = pars_[pit_].beginOfBody();
440         pos_type const end = row_.endpos();
441         double tmpx = x_;
442
443         for (pos_type vpos = row_.pos(); vpos < end; ++vpos)  {
444                 pos_type pos = text_.bidi.vis2log(vpos);
445                 double const old_tmpx = tmpx;
446                 if (body_pos > 0 && pos == body_pos - 1) {
447                         LyXLayout_ptr const & layout = pars_[pit_].layout();
448                         LyXFont const lfont = getLabelFont();
449
450                         tmpx += label_hfill_ + font_metrics::width(layout->labelsep, lfont);
451
452                         if (pars_[pit_].isLineSeparator(body_pos - 1))
453                                 tmpx -= singleWidth(body_pos - 1);
454                 }
455
456                 tmpx += singleWidth(pos);
457
458                 if (hfillExpansion(pars_[pit_], row_, pos)) {
459                         if (pos >= body_pos)
460                                 tmpx += hfill_;
461                         else
462                                 tmpx += label_hfill_;
463                 } else {
464                         if (pars_[pit_].isSeparator(pos) && pos >= body_pos)
465                                 tmpx += separator_;
466                 }
467
468                 if (((startpit != pit_ && startrow != rit_)
469                                 || cur.selBegin().pos() <= pos) &&
470                         ((endpit != pit_ && endrow != rit_)
471                                 || pos < cur.selEnd().pos())) {
472                         // Here we do not use x_ as xo_ was added to x_.
473                         pain_.fillRectangle(int(old_tmpx), yo_,
474                                 int(tmpx - old_tmpx + 1), h, LColor::selection);
475                 }
476         }
477
478         if ((startpit != pit_ && startrow != rit_ && is_rtl) ||
479             (endpit != pit_ && endrow != rit_ && !is_rtl)) {
480                 pain_.fillRectangle(int(xo_ + tmpx),
481                         yo_, int(width_ - tmpx), h, LColor::selection);
482         }
483 }
484
485
486 void RowPainter::paintChangeBar()
487 {
488         pos_type const start = row_.pos();
489         pos_type const end = row_.endpos();
490
491         if (start == end || !pars_[pit_].isChanged(start, end - 1))
492                 return;
493
494         int const height = text_.isLastRow(pit_, row_)
495                 ? row_.baseline()
496                 : row_.height() + boost::next(rit_)->top_of_text();
497
498         pain_.fillRectangle(4, yo_, 5, height, LColor::changebar);
499 }
500
501
502 void RowPainter::paintAppendix()
503 {
504         if (!pars_[pit_].params().appendix())
505                 return;
506
507         int y = yo_;
508
509         if (pars_[pit_].params().startOfAppendix())
510                 y += 2 * defaultRowHeight();
511
512         pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
513         pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), LColor::appendix);
514 }
515
516
517 void RowPainter::paintDepthBar()
518 {
519         Paragraph::depth_type const depth = pars_[pit_].getDepth();
520
521         if (depth <= 0)
522                 return;
523
524         Paragraph::depth_type prev_depth = 0;
525         if (!text_.isFirstRow(pit_, row_)) {
526                 par_type pit2 = pit_;
527                 if (row_.pos() == 0)
528                         --pit2;
529                 prev_depth = pars_[pit2].getDepth();
530         }
531
532         Paragraph::depth_type next_depth = 0;
533         if (!text_.isLastRow(pit_, row_)) {
534                 par_type pit2 = pit_;
535                 if (row_.endpos() >= pars_[pit2].size())
536                         ++pit2;
537                 next_depth = pars_[pit2].getDepth();
538         }
539
540         for (Paragraph::depth_type i = 1; i <= depth; ++i) {
541                 int const w = NEST_MARGIN / 5;
542                 int x = int(w * i + xo_);
543                 // only consider the changebar space if we're drawing outer left
544                 if (xo_ == 0)
545                         x += CHANGEBAR_MARGIN;
546                 int const h = yo_ + row_.height() - 1 - (i - next_depth - 1) * 3;
547
548                 pain_.line(x, yo_, x, h, LColor::depthbar);
549
550                 if (i > prev_depth)
551                         pain_.fillRectangle(x, yo_, w, 2, LColor::depthbar);
552                 if (i > next_depth)
553                         pain_.fillRectangle(x, h, w, 2, LColor::depthbar);
554         }
555 }
556
557
558 int RowPainter::paintAppendixStart(int y)
559 {
560         LyXFont pb_font;
561         pb_font.setColor(LColor::appendix);
562         pb_font.decSize();
563
564         string const label = _("Appendix");
565         int w = 0;
566         int a = 0;
567         int d = 0;
568         font_metrics::rectText(label, pb_font, w, a, d);
569
570         int const text_start = int(xo_ + (width_ - w) / 2);
571         int const text_end = text_start + w;
572
573         pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
574
575         pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
576         pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
577
578         return 3 * defaultRowHeight();
579 }
580
581
582 void RowPainter::paintFirst()
583 {
584         ParagraphParameters const & parparams = pars_[pit_].params();
585
586         int y_top = 0;
587
588         // start of appendix?
589         if (parparams.startOfAppendix())
590                 y_top += paintAppendixStart(yo_ + y_top + 2 * defaultRowHeight());
591
592         Buffer const & buffer = *bv_.buffer();
593
594         LyXLayout_ptr const & layout = pars_[pit_].layout();
595
596         if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
597                 if (pit_ != 0) {
598                         if (layout->latextype == LATEX_PARAGRAPH
599                                 && !pars_[pit_].getDepth()) {
600                                 y_top += buffer.params().getDefSkip().inPixels(bv_);
601                         } else {
602                                 LyXLayout_ptr const & playout = pars_[pit_ - 1].layout();
603                                 if (playout->latextype == LATEX_PARAGRAPH
604                                         && !pars_[pit_ - 1].getDepth()) {
605                                         // is it right to use defskip here, too? (AS)
606                                         y_top += buffer.params().getDefSkip().inPixels(bv_);
607                                 }
608                         }
609                 }
610         }
611
612         bool const is_rtl = text_.isRTL(pars_[pit_]);
613         bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
614         //lyxerr << "paintFirst: " << pars_[pit_].id() << " is_seq: " << is_seq << std::endl;
615
616         // should we print a label?
617         if (layout->labeltype >= LABEL_STATIC
618             && (layout->labeltype != LABEL_STATIC
619                       || layout->latextype != LATEX_ENVIRONMENT
620                       || is_seq)) {
621
622                 LyXFont font = getLabelFont();
623                 if (!pars_[pit_].getLabelstring().empty()) {
624                         double x = x_;
625                         string const str = pars_[pit_].getLabelstring();
626
627                         // this is special code for the chapter layout. This is
628                         // printed in an extra row and has a pagebreak at
629                         // the top.
630                         if (layout->counter == "chapter") {
631                                 if (buffer.params().secnumdepth >= 0) {
632                                         float spacing_val = 1.0;
633                                         if (!parparams.spacing().isDefault()) {
634                                                 spacing_val = parparams.spacing().getValue();
635                                         } else {
636                                                 spacing_val = buffer.params().spacing().getValue();
637                                         }
638
639                                         int const maxdesc =
640                                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
641                                                 + int(layout->parsep) * defaultRowHeight();
642
643                                         if (is_rtl) {
644                                                 x = width_ - leftMargin() -
645                                                         font_metrics::width(str, font);
646                                         }
647
648                                         pain_.text(int(x),
649                                                 yo_ + row_.baseline() -
650                                                 row_.ascent_of_text() - maxdesc,
651                                                 str, font);
652                                 }
653                         } else {
654                                 if (is_rtl) {
655                                         x = width_ - leftMargin()
656                                                 + font_metrics::width(layout->labelsep, font);
657                                 } else {
658                                         x = x_ - font_metrics::width(layout->labelsep, font)
659                                                 - font_metrics::width(str, font);
660                                 }
661
662                                 pain_.text(int(x), yo_ + row_.baseline(), str, font);
663                         }
664                 }
665
666         // the labels at the top of an environment.
667         // More or less for bibliography
668         } else if (is_seq &&
669                 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
670                 layout->labeltype == LABEL_BIBLIO ||
671                 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
672                 LyXFont font = getLabelFont();
673                 if (!pars_[pit_].getLabelstring().empty()) {
674                         string const str = pars_[pit_].getLabelstring();
675                         float spacing_val = 1.0;
676                         if (!parparams.spacing().isDefault()) {
677                                 spacing_val = parparams.spacing().getValue();
678                         } else {
679                                 spacing_val = buffer.params().spacing().getValue();
680                         }
681
682                         int maxdesc =
683                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
684                                 + (layout->labelbottomsep * defaultRowHeight()));
685
686                         double x = x_;
687                         if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
688                                 x = ((is_rtl ? leftMargin() : x_)
689                                          + width_ - text_.rightMargin(pars_[pit_])) / 2;
690                                 x -= font_metrics::width(str, font) / 2;
691                         } else if (is_rtl) {
692                                 x = width_ - leftMargin() -
693                                         font_metrics::width(str, font);
694                         }
695                         pain_.text(int(x),
696                             yo_ + row_.baseline() - row_.ascent_of_text() - maxdesc,
697                                   str, font);
698                 }
699         }
700 }
701
702
703 void RowPainter::paintLast()
704 {
705         bool const is_rtl = text_.isRTL(pars_[pit_]);
706         int const endlabel = getEndLabel(pit_, text_.paragraphs());
707
708         // draw an endlabel
709         switch (endlabel) {
710         case END_LABEL_BOX:
711         case END_LABEL_FILLED_BOX: {
712                 LyXFont const font = getLabelFont();
713                 int const size = int(0.75 * font_metrics::maxAscent(font));
714                 int const y = yo_ + row_.baseline() - size;
715                 int x = is_rtl ? NEST_MARGIN + CHANGEBAR_MARGIN: width_ - size;
716
717                 if (width_ - int(row_.width()) <= size)
718                         x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
719
720                 if (endlabel == END_LABEL_BOX)
721                         pain_.rectangle(x, y, size, size, LColor::eolmarker);
722                 else
723                         pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
724                 break;
725         }
726
727         case END_LABEL_STATIC: {
728                 LyXFont font = getLabelFont();
729                 string const & str = pars_[pit_].layout()->endlabelstring();
730                 double const x = is_rtl ?
731                         x_ - font_metrics::width(str, font)
732                         : - text_.rightMargin(pars_[pit_]) - row_.width();
733                 pain_.text(int(x), yo_ + row_.baseline(), str, font);
734                 break;
735         }
736
737         case END_LABEL_NO_LABEL:
738                 break;
739         }
740 }
741
742
743 void RowPainter::paintText()
744 {
745         pos_type const end = row_.endpos();
746         pos_type body_pos = pars_[pit_].beginOfBody();
747         if (body_pos > 0 &&
748                 (body_pos > end || !pars_[pit_].isLineSeparator(body_pos - 1))) {
749                 body_pos = 0;
750         }
751
752         LyXLayout_ptr const & layout = pars_[pit_].layout();
753
754         bool running_strikeout = false;
755         bool is_struckout = false;
756         int last_strikeout_x = 0;
757
758         for (pos_type vpos = row_.pos(); vpos < end; ) {
759                 if (x_ > bv_.workWidth())
760                         break;
761
762                 pos_type pos = text_.bidi.vis2log(vpos);
763
764                 if (pos >= pars_[pit_].size()) {
765                         ++vpos;
766                         continue;
767                 }
768
769                 if (x_ + singleWidth(pos) < 0) {
770                         x_ += singleWidth(pos);
771                         ++vpos;
772                         continue;
773                 }
774
775                 is_struckout = isDeletedText(pars_[pit_], pos);
776
777                 if (is_struckout && !running_strikeout) {
778                         running_strikeout = true;
779                         last_strikeout_x = int(x_);
780                 }
781
782                 bool const highly_editable_inset = pars_[pit_].isInset(pos)
783                         && isHighlyEditableInset(pars_[pit_].getInset(pos));
784
785                 // if we reach the end of a struck out range, paint it
786                 // we also don't paint across things like tables
787                 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
788                         int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
789                         pain_.line(last_strikeout_x, middle, int(x_), middle,
790                                 LColor::strikeout, Painter::line_solid, Painter::line_thin);
791                         running_strikeout = false;
792                 }
793
794                 if (body_pos > 0 && pos == body_pos - 1) {
795                         int const lwidth = font_metrics::width(layout->labelsep,
796                                 getLabelFont());
797
798                         x_ += label_hfill_ + lwidth - singleWidth(body_pos - 1);
799                 }
800
801                 if (pars_[pit_].isHfill(pos)) {
802                         x_ += 1;
803
804                         int const y0 = yo_ + row_.baseline();
805                         int const y1 = y0 - defaultRowHeight() / 2;
806
807                         pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
808
809                         if (hfillExpansion(pars_[pit_], row_, pos)) {
810                                 int const y2 = (y0 + y1) / 2;
811
812                                 if (pos >= body_pos) {
813                                         pain_.line(int(x_), y2, int(x_ + hfill_), y2,
814                                                   LColor::added_space,
815                                                   Painter::line_onoffdash);
816                                         x_ += hfill_;
817                                 } else {
818                                         pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
819                                                   LColor::added_space,
820                                                   Painter::line_onoffdash);
821                                         x_ += label_hfill_;
822                                 }
823                                 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
824                         }
825                         x_ += 2;
826                         ++vpos;
827                 } else if (pars_[pit_].isSeparator(pos)) {
828                         x_ += singleWidth(pos);
829                         if (pos >= body_pos)
830                                 x_ += separator_;
831                         ++vpos;
832                 } else {
833                         paintFromPos(vpos);
834                 }
835         }
836
837         // if we reach the end of a struck out range, paint it
838         if (running_strikeout) {
839                 int const middle = yo_ + (row_.baseline() + row_.top_of_text()) / 2;
840                 pain_.line(last_strikeout_x, middle, int(x_), middle,
841                         LColor::strikeout, Painter::line_solid, Painter::line_thin);
842                 running_strikeout = false;
843         }
844 }
845
846
847 int paintPars(BufferView const & bv, LyXText const & text,
848         par_type pit, int xo, int yo, int y)
849 {
850         //lyxerr << "  paintRows: pit: " << &*pit << endl;
851         int const y2 = bv.painter().paperHeight();
852         y -= bv.top_y();
853
854         ParagraphList & pars = text.paragraphs();
855         for ( ; pit != par_type(pars.size()); ++pit) {
856                 RowList::iterator row = pars[pit].rows.begin();
857                 RowList::iterator rend = pars[pit].rows.end();
858
859                 for ( ; row != rend; ++row) {
860                         RowPainter(bv, text, pit, row, xo, y + yo);
861                         y += row->height();
862                 }
863                 if (yo + y >= y2)
864                         break;
865         }
866
867         return y;
868 }
869
870 } // namespace anon
871
872
873 int paintText(BufferView const & bv)
874 {
875         par_type pit;
876         bv.text()->updateParPositions();
877         bv.text()->getRowNearY(bv.top_y(), pit);
878         //lyxerr << "top_y: " << bv.top_y() << " y: " << pit->y << endl;
879         return paintPars(bv, *bv.text(), pit, 0, 0, bv.text()->paragraphs()[pit].y);
880 }
881
882
883 void paintTextInset(LyXText const & text, PainterInfo & pi, int xo, int yo)
884 {
885         paintPars(*pi.base.bv, text, 0, xo, yo, 0);
886 }