]> git.lyx.org Git - lyx.git/blob - src/rowpainter.cpp
forgot that
[lyx.git] / src / rowpainter.cpp
1 /**
2  * \file rowpainter.cpp
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 #include <algorithm>
14
15 #include "rowpainter.h"
16
17 #include "Bidi.h"
18 #include "Buffer.h"
19 #include "CoordCache.h"
20 #include "Cursor.h"
21 #include "BufferParams.h"
22 #include "BufferView.h"
23 #include "Changes.h"
24 #include "Encoding.h"
25 #include "support/gettext.h"
26 #include "Language.h"
27 #include "Layout.h"
28 #include "LyXRC.h"
29 #include "Row.h"
30 #include "MetricsInfo.h"
31 #include "Paragraph.h"
32 #include "ParagraphMetrics.h"
33 #include "paragraph_funcs.h"
34 #include "ParagraphParameters.h"
35 #include "TextMetrics.h"
36 #include "VSpace.h"
37
38 #include "frontends/FontMetrics.h"
39 #include "frontends/Painter.h"
40
41 #include "insets/InsetText.h"
42
43 #include "support/debug.h"
44 #include "support/textutils.h"
45
46 #include "support/lassert.h"
47 #include <boost/crc.hpp>
48
49 using namespace std;
50
51 namespace lyx {
52
53 using frontend::Painter;
54 using frontend::FontMetrics;
55
56 RowPainter::RowPainter(PainterInfo & pi,
57         Text const & text, pit_type pit, Row const & row, Bidi & bidi, int x, int y)
58         : pi_(pi), text_(text),
59           text_metrics_(pi_.base.bv->textMetrics(&text)),
60           pars_(text.paragraphs()),
61           row_(row), pit_(pit), par_(text.paragraphs()[pit]),
62           pm_(text_metrics_.parMetrics(pit)),
63           bidi_(bidi), erased_(pi_.erased_),
64           xo_(x), yo_(y), width_(text_metrics_.width())
65 {
66         bidi_.computeTables(par_, pi_.base.bv->buffer(), row_);
67         x_ = row_.x + xo_;
68
69         //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
70         //row_.dump();
71
72         LASSERT(pit >= 0, /**/);
73         LASSERT(pit < int(text.paragraphs().size()), /**/);
74 }
75
76
77 FontInfo RowPainter::labelFont() const
78 {
79         return text_.labelFont(pi_.base.bv->buffer(), par_);
80 }
81
82
83 int RowPainter::leftMargin() const
84 {
85         return text_metrics_.leftMargin(text_metrics_.width(), pit_,
86                 row_.pos());
87 }
88
89 // If you want to debug inset metrics uncomment the following line:
90 //#define DEBUG_METRICS
91 // This draws green lines around each inset.
92
93
94 void RowPainter::paintInset(Inset const * inset, pos_type const pos)
95 {
96         Font const font = text_metrics_.displayFont(pit_, pos);
97
98         LASSERT(inset, return);
99         // Backup full_repaint status because some insets (InsetTabular)
100         // requires a full repaint
101         bool pi_full_repaint = pi_.full_repaint;
102
103         // FIXME: We should always use font, see documentation of
104         // noFontChange() in Inset.h.
105         pi_.base.font = inset->noFontChange() ?
106                 pi_.base.bv->buffer().params().getFont().fontInfo() :
107                 font.fontInfo();
108         pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
109         pi_.erased_ = erased_ || par_.isDeleted(pos);
110         pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
111         // insets are painted completely. Recursive
112         inset->drawBackground(pi_, int(x_), yo_);
113         inset->drawSelection(pi_, int(x_), yo_);
114         inset->draw(pi_, int(x_), yo_);
115
116         Dimension const & dim = pm_.insetDimension(inset);
117
118         paintForeignMark(x_, font.language(), dim.descent());
119
120         x_ += dim.width();
121
122         // Restore full_repaint status.
123         pi_.full_repaint = pi_full_repaint;
124
125 #ifdef DEBUG_METRICS
126         int const x1 = int(x_ - dim.width());
127         Dimension dim2;
128         LASSERT(max_witdh_ > 0, /**/);
129         int right_margin = text_metrics_.rightMargin(pm_);
130         int const w = max_witdh_ - leftMargin() - right_margin;
131         MetricsInfo mi(pi_.base.bv, font.fontInfo(), w);
132         inset->metrics(mi, dim2);
133         if (dim.wid != dim2.wid)
134                 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
135                        << " draw width " << dim.width()
136                        << "> metrics width " << dim2.wid << "." << endl;
137         if (dim->asc != dim2.asc)
138                 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
139                        << " draw ascent " << dim.ascent()
140                        << "> metrics ascent " << dim2.asc << "." << endl;
141         if (dim2.descent() != dim.des)
142                 lyxerr << "Error: inset " << to_ascii(inset->getInsetName())
143                        << " draw ascent " << dim.descent()
144                        << "> metrics descent " << dim2.des << "." << endl;
145         LASSERT(dim2.wid == dim.wid, /**/);
146         LASSERT(dim2.asc == dim.asc, /**/);
147         LASSERT(dim2.des == dim.des, /**/);
148         int const x2 = x1 + dim.wid;
149         int const y1 = yo_ + dim.des;
150         int const y2 = yo_ - dim.asc;
151         pi_.pain.line(x1, y1, x1, y2, Color_green);
152         pi_.pain.line(x1, y1, x2, y1, Color_green);
153         pi_.pain.line(x2, y1, x2, y2, Color_green);
154         pi_.pain.line(x1, y2, x2, y2, Color_green);
155 #endif
156 }
157
158
159 void RowPainter::paintHebrewComposeChar(pos_type & vpos, FontInfo const & font)
160 {
161         pos_type pos = bidi_.vis2log(vpos);
162
163         docstring str;
164
165         // first char
166         char_type c = par_.getChar(pos);
167         str += c;
168         ++vpos;
169
170         int const width = theFontMetrics(font).width(c);
171         int dx = 0;
172
173         for (pos_type i = pos - 1; i >= 0; --i) {
174                 c = par_.getChar(i);
175                 if (!Encodings::isHebrewComposeChar(c)) {
176                         if (isPrintableNonspace(c)) {
177                                 int const width2 = pm_.singleWidth(i,
178                                         text_metrics_.displayFont(pit_, i));
179                                 dx = (c == 0x05e8 || // resh
180                                       c == 0x05d3)   // dalet
181                                         ? width2 - width
182                                         : (width2 - width) / 2;
183                         }
184                         break;
185                 }
186         }
187
188         // Draw nikud
189         pi_.pain.text(int(x_) + dx, yo_, str, font);
190 }
191
192
193 void RowPainter::paintArabicComposeChar(pos_type & vpos, FontInfo const & font)
194 {
195         pos_type pos = bidi_.vis2log(vpos);
196         docstring str;
197
198         // first char
199         char_type c = par_.getChar(pos);
200         c = par_.transformChar(c, pos);
201         str += c;
202         ++vpos;
203
204         int const width = theFontMetrics(font).width(c);
205         int dx = 0;
206
207         for (pos_type i = pos - 1; i >= 0; --i) {
208                 c = par_.getChar(i);
209                 if (!Encodings::isArabicComposeChar(c)) {
210                         if (isPrintableNonspace(c)) {
211                                 int const width2 = pm_.singleWidth(i,
212                                                 text_metrics_.displayFont(pit_, i));
213                                 dx = (width2 - width) / 2;
214                         }
215                         break;
216                 }
217         }
218         // Draw nikud
219         pi_.pain.text(int(x_) + dx, yo_, str, font);
220 }
221
222
223 void RowPainter::paintChars(pos_type & vpos, FontInfo const & font,
224                             bool hebrew, bool arabic)
225 {
226         // This method takes up 70% of time when typing
227         pos_type pos = bidi_.vis2log(vpos);
228         // first character
229         vector<char_type> str;
230         str.reserve(100);
231         str.push_back(par_.getChar(pos));
232
233         if (arabic) {
234                 char_type c = str[0];
235                 if (c == '(')
236                         c = ')';
237                 else if (c == ')')
238                         c = '(';
239                 str[0] = par_.transformChar(c, pos);
240         }
241
242         pos_type const end = row_.endpos();
243         FontSpan const font_span = par_.fontSpan(pos);
244         // Track-change status.
245         Change const & change_running = par_.lookupChange(pos);
246
247         // selected text?
248         bool const selection = pos >= row_.sel_beg && pos < row_.sel_end;
249
250         char_type prev_char = ' ';
251         // collect as much similar chars as we can
252         for (++vpos ; vpos < end ; ++vpos) {
253                 pos = bidi_.vis2log(vpos);
254                 if (pos < font_span.first || pos > font_span.last)
255                         break;
256
257                 bool const new_selection = pos >= row_.sel_beg && pos < row_.sel_end;
258                 if (new_selection != selection)
259                         // Selection ends or starts here.
260                         break;
261
262                 Change const & change = par_.lookupChange(pos);
263                 if (!change_running.isSimilarTo(change))
264                         // Track change type or author has changed.
265                         break;
266
267                 char_type c = par_.getChar(pos);
268
269                 if (c == '\t' || prev_char == '\t') {
270                         prev_char = c;
271                         break;
272                 }
273
274                 if (!isPrintableNonspace(c))
275                         break;
276
277                 /* Because we do our own bidi, at this point the strings are
278                  * already in visual order. However, Qt also applies its own
279                  * bidi algorithm to strings that it paints to the screen.
280                  * Therefore, if we were to paint Hebrew/Arabic words as a
281                  * single string, the letters in the words would get reversed
282                  * again. In order to avoid that, we don't collect Hebrew/
283                  * Arabic characters, but rather paint them one at a time.
284                  * See also http://thread.gmane.org/gmane.editors.lyx.devel/79740
285                  */
286                 if (hebrew)
287                         break;
288
289                 /* FIXME: these checks are irrelevant, since 'arabic' and
290                  * 'hebrew' alone are already going to trigger a break.
291                  * However, this should not be removed completely, because
292                  * if an alternative solution is found which allows grouping
293                  * of arabic and hebrew characters, then these breaks may have
294                  * to be re-applied.
295
296                 if (arabic && Encodings::isArabicComposeChar(c))
297                         break;
298
299                 if (hebrew && Encodings::isHebrewComposeChar(c))
300                         break;
301                 */
302
303                 if (arabic) {
304                         if (c == '(')
305                                 c = ')';
306                         else if (c == ')')
307                                 c = '(';
308                         c = par_.transformChar(c, pos);
309                         /* see comment in hebrew, explaining why we break */
310                         break;
311                 }
312
313                 str.push_back(c);
314         }
315
316         docstring s(&str[0], str.size());
317
318         if (s[0] == '\t')
319                 s.replace(0,1,from_ascii("    "));
320
321         if (!selection && !change_running.changed()) {
322                 x_ += pi_.pain.text(int(x_), yo_, s, font);
323                 return;
324         }
325         
326         FontInfo copy = font;
327         if (change_running.changed())
328                 copy.setColor(change_running.color());
329         else if (selection)
330                 copy.setColor(Color_selectiontext);
331
332         x_ += pi_.pain.text(int(x_), yo_, s, copy);
333 }
334
335
336 void RowPainter::paintForeignMark(double orig_x, Language const * lang,
337                 int desc)
338 {
339         if (!lyxrc.mark_foreign_language)
340                 return;
341         if (lang == latex_language)
342                 return;
343         if (lang == pi_.base.bv->buffer().params().language)
344                 return;
345
346         int const y = yo_ + 1 + desc;
347         pi_.pain.line(int(orig_x), y, int(x_), y, Color_language);
348 }
349
350
351 void RowPainter::paintFromPos(pos_type & vpos)
352 {
353         pos_type const pos = bidi_.vis2log(vpos);
354         Font const orig_font = text_metrics_.displayFont(pit_, pos);
355         double const orig_x = x_;
356
357         // usual characters, no insets
358         char_type const c = par_.getChar(pos);
359
360         // special case languages
361         string const & lang = orig_font.language()->lang();
362         bool const hebrew = lang == "hebrew";
363         bool const arabic = lang == "arabic_arabtex" || lang == "arabic_arabi" || 
364                                                 lang == "farsi";
365
366         // draw as many chars as we can
367         if ((!hebrew && !arabic)
368                 || (hebrew && !Encodings::isHebrewComposeChar(c))
369                 || (arabic && !Encodings::isArabicComposeChar(c))) {
370                 paintChars(vpos, orig_font.fontInfo(), hebrew, arabic);
371         } else if (hebrew) {
372                 paintHebrewComposeChar(vpos, orig_font.fontInfo());
373         } else if (arabic) {
374                 paintArabicComposeChar(vpos, orig_font.fontInfo());
375         }
376
377         paintForeignMark(orig_x, orig_font.language());
378 }
379
380
381 void RowPainter::paintChangeBar()
382 {
383         pos_type const start = row_.pos();
384         pos_type end = row_.endpos();
385
386         if (par_.size() == end) {
387                 // this is the last row of the paragraph;
388                 // thus, we must also consider the imaginary end-of-par character
389                 end++;
390         }
391
392         if (start == end || !par_.isChanged(start, end))
393                 return;
394
395         int const height = text_metrics_.isLastRow(pit_, row_)
396                 ? row_.ascent()
397                 : row_.height();
398
399         pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
400 }
401
402
403 void RowPainter::paintAppendix()
404 {
405         // only draw the appendix frame once (for the main text)
406         if (!par_.params().appendix() || !text_.isMainText(pi_.base.bv->buffer()))
407                 return;
408
409         int y = yo_ - row_.ascent();
410
411         if (par_.params().startOfAppendix())
412                 y += 2 * defaultRowHeight();
413
414         pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
415         pi_.pain.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), Color_appendix);
416 }
417
418
419 void RowPainter::paintDepthBar()
420 {
421         depth_type const depth = par_.getDepth();
422
423         if (depth <= 0)
424                 return;
425
426         depth_type prev_depth = 0;
427         if (!text_metrics_.isFirstRow(pit_, row_)) {
428                 pit_type pit2 = pit_;
429                 if (row_.pos() == 0)
430                         --pit2;
431                 prev_depth = pars_[pit2].getDepth();
432         }
433
434         depth_type next_depth = 0;
435         if (!text_metrics_.isLastRow(pit_, row_)) {
436                 pit_type pit2 = pit_;
437                 if (row_.endpos() >= pars_[pit2].size())
438                         ++pit2;
439                 next_depth = pars_[pit2].getDepth();
440         }
441
442         for (depth_type i = 1; i <= depth; ++i) {
443                 int const w = nestMargin() / 5;
444                 int x = int(xo_) + w * i;
445                 // only consider the changebar space if we're drawing outermost text
446                 if (text_.isMainText(pi_.base.bv->buffer()))
447                         x += changebarMargin();
448
449                 int const starty = yo_ - row_.ascent();
450                 int const h =  row_.height() - 1 - (i - next_depth - 1) * 3;
451
452                 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
453
454                 if (i > prev_depth)
455                         pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
456                 if (i > next_depth)
457                         pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
458         }
459 }
460
461
462 int RowPainter::paintAppendixStart(int y)
463 {
464         FontInfo pb_font = sane_font;
465         pb_font.setColor(Color_appendix);
466         pb_font.decSize();
467
468         int w = 0;
469         int a = 0;
470         int d = 0;
471
472         docstring const label = _("Appendix");
473         theFontMetrics(pb_font).rectText(label, w, a, d);
474
475         int const text_start = int(xo_ + (width_ - w) / 2);
476         int const text_end = text_start + w;
477
478         pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
479
480         pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
481         pi_.pain.line(text_end, y, int(xo_ + width_ - 2), y, Color_appendix);
482
483         return 3 * defaultRowHeight();
484 }
485
486
487 void RowPainter::paintFirst()
488 {
489         ParagraphParameters const & parparams = par_.params();
490
491         int y_top = 0;
492
493         // start of appendix?
494         if (parparams.startOfAppendix())
495                 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
496
497         Buffer const & buffer = pi_.base.bv->buffer();
498         Layout const & layout = par_.layout();
499
500         if (buffer.params().paragraph_separation == BufferParams::ParagraphSkipSeparation) {
501                 if (pit_ != 0) {
502                         if (layout.latextype == LATEX_PARAGRAPH
503                                 && !par_.getDepth()) {
504                                 y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
505                         } else {
506                                 Layout const & playout = pars_[pit_ - 1].layout();
507                                 if (playout.latextype == LATEX_PARAGRAPH
508                                         && !pars_[pit_ - 1].getDepth()) {
509                                         // is it right to use defskip here, too? (AS)
510                                         y_top += buffer.params().getDefSkip().inPixels(*pi_.base.bv);
511                                 }
512                         }
513                 }
514         }
515
516         bool const is_rtl = text_.isRTL(buffer, par_);
517         bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
518         //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
519
520         // should we print a label?
521         if (layout.labeltype >= LABEL_STATIC
522             && (layout.labeltype != LABEL_STATIC
523                       || layout.latextype != LATEX_ENVIRONMENT
524                       || is_seq)) {
525
526                 FontInfo const font = labelFont();
527                 FontMetrics const & fm = theFontMetrics(font);
528
529                 docstring const str = par_.labelString();
530                 if (!str.empty()) {
531                         double x = x_;
532
533                         // this is special code for the chapter layout. This is
534                         // printed in an extra row and has a pagebreak at
535                         // the top.
536                         if (layout.counter == "chapter") {
537                                 double spacing_val = 1.0;
538                                 if (!parparams.spacing().isDefault()) {
539                                         spacing_val = parparams.spacing().getValue();
540                                 } else {
541                                         spacing_val = buffer.params().spacing().getValue();
542                                 }
543
544                                 int const labeladdon = int(fm.maxHeight() * layout.spacing.getValue() * spacing_val);
545
546                                 int const maxdesc = int(fm.maxDescent() * layout.spacing.getValue() * spacing_val)
547                                         + int(layout.parsep) * defaultRowHeight();
548
549                                 if (is_rtl) {
550                                         x = width_ - leftMargin() -
551                                                 fm.width(str);
552                                 }
553
554                                 pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
555                         } else {
556                                 if (is_rtl) {
557                                         x = width_ - leftMargin()
558                                                 + fm.width(layout.labelsep);
559                                 } else {
560                                         x = x_ - fm.width(layout.labelsep)
561                                                 - fm.width(str);
562                                 }
563
564                                 pi_.pain.text(int(x), yo_, str, font);
565                         }
566                 }
567
568         // the labels at the top of an environment.
569         // More or less for bibliography
570         } else if (is_seq &&
571                 (layout.labeltype == LABEL_TOP_ENVIRONMENT ||
572                 layout.labeltype == LABEL_BIBLIO ||
573                 layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
574                 FontInfo const font = labelFont();
575                 docstring const str = par_.labelString();
576                 if (!str.empty()) {
577                         double spacing_val = 1.0;
578                         if (!parparams.spacing().isDefault())
579                                 spacing_val = parparams.spacing().getValue();
580                         else
581                                 spacing_val = buffer.params().spacing().getValue();
582
583                         FontMetrics const & fm = theFontMetrics(font);
584
585                         int const labeladdon = int(fm.maxHeight()
586                                 * layout.spacing.getValue() * spacing_val);
587
588                         int maxdesc =
589                                 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
590                                 + (layout.labelbottomsep * defaultRowHeight()));
591
592                         double x = x_;
593                         if (layout.labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
594                                 if (is_rtl)
595                                         x = leftMargin();
596                                 x += (width_ - text_metrics_.rightMargin(pm_) - leftMargin()) / 2;
597                                 x -= fm.width(str) / 2;
598                         } else if (is_rtl) {
599                                 x = width_ - leftMargin() -     fm.width(str);
600                         }
601                         pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
602                 }
603         }
604 }
605
606
607 void RowPainter::paintLast()
608 {
609         bool const is_rtl = text_.isRTL(pi_.base.bv->buffer(), par_);
610         int const endlabel = getEndLabel(pit_, text_.paragraphs());
611
612         // paint imaginary end-of-paragraph character
613
614         Change const & change = par_.lookupChange(par_.size());
615         if (change.changed()) {
616                 FontMetrics const & fm =
617                         theFontMetrics(pi_.base.bv->buffer().params().getFont());
618                 int const length = fm.maxAscent() / 2;
619                 ColorCode col = change.color();
620
621                 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
622                            Painter::line_solid, Painter::line_thick);
623
624                 if (change.deleted()) {
625                         pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
626                                 yo_ + 2, col, Painter::line_solid, Painter::line_thick);
627                 } else {
628                         pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
629                                 yo_ + 2, col, Painter::line_solid, Painter::line_thick);
630                 }
631
632         }
633
634         // draw an endlabel
635
636         switch (endlabel) {
637         case END_LABEL_BOX:
638         case END_LABEL_FILLED_BOX: {
639                 FontInfo const font = labelFont();
640                 FontMetrics const & fm = theFontMetrics(font);
641                 int const size = int(0.75 * fm.maxAscent());
642                 int const y = yo_ - size;
643                 int const max_row_width = width_ - size - Inset::TEXT_TO_INSET_OFFSET;
644                 int x = is_rtl ? nestMargin() + changebarMargin()
645                         : max_row_width - text_metrics_.rightMargin(pm_);
646
647                 // If needed, move the box a bit to avoid overlapping with text.
648                 int const rem = max_row_width - row_.width();
649                 if (rem <= 0)
650                         x += is_rtl ? rem : - rem;
651
652                 if (endlabel == END_LABEL_BOX)
653                         pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
654                 else
655                         pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
656                 break;
657         }
658
659         case END_LABEL_STATIC: {
660                 FontInfo const font = labelFont();
661                 FontMetrics const & fm = theFontMetrics(font);
662                 docstring const & str = par_.layout().endlabelstring();
663                 double const x = is_rtl ?
664                         x_ - fm.width(str)
665                         : - text_metrics_.rightMargin(pm_) - row_.width();
666                 pi_.pain.text(int(x), yo_, str, font);
667                 break;
668         }
669
670         case END_LABEL_NO_LABEL:
671                 break;
672         }
673 }
674
675
676 void RowPainter::paintOnlyInsets()
677 {
678         pos_type const end = row_.endpos();
679         for (pos_type pos = row_.pos(); pos != end; ++pos) {
680                 // If outer row has changed, nested insets are repaint completely.
681                 Inset const * inset = par_.getInset(pos);
682                 if (!inset)
683                         continue;
684                 if (x_ > pi_.base.bv->workWidth())
685                         continue;
686                 x_ = pi_.base.bv->coordCache().getInsets().x(inset);
687
688                 bool const pi_selected = pi_.selected;
689                 Cursor const & cur = pi_.base.bv->cursor();
690                 if (cur.selection() && cur.text() == &text_ 
691                           && cur.anchor().text() == &text_)
692                         pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos; 
693                 paintInset(inset, pos);
694                 pi_.selected = pi_selected;
695         }
696 }
697
698
699 void RowPainter::paintText()
700 {
701         pos_type const end = row_.endpos();
702         // Spaces at logical line breaks in bidi text must be skipped during 
703         // painting. However, they may appear visually in the middle
704         // of a row; they must be skipped, wherever they are...
705         // * logically "abc_[HEBREW_\nHEBREW]"
706         // * visually "abc_[_WERBEH\nWERBEH]"
707         pos_type skipped_sep_vpos = -1;
708         pos_type body_pos = par_.beginOfBody();
709         if (body_pos > 0 &&
710                 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
711                 body_pos = 0;
712         }
713
714         Layout const & layout = par_.layout();
715
716         Change change_running;
717         int change_last_x = 0;
718
719         // check for possible inline completion
720         DocIterator const & inlineCompletionPos = pi_.base.bv->inlineCompletionPos();
721         pos_type inlineCompletionVPos = -1;
722         if (inlineCompletionPos.inTexted()
723             && inlineCompletionPos.text() == &text_
724             && inlineCompletionPos.pit() == pit_
725             && inlineCompletionPos.pos() - 1 >= row_.pos()
726             && inlineCompletionPos.pos() - 1 < row_.endpos()) {
727                 // draw logically behind the previous character
728                 inlineCompletionVPos = bidi_.log2vis(inlineCompletionPos.pos() - 1);
729         }
730
731         // Use font span to speed things up, see below
732         FontSpan font_span;
733         Font font;
734
735         // If the last logical character is a separator, don't paint it, unless
736         // it's in the last row of a paragraph; see skipped_sep_vpos declaration
737         if (end > 0 && end < par_.size() && par_.isSeparator(end - 1))
738                 skipped_sep_vpos = bidi_.log2vis(end - 1);
739
740         for (pos_type vpos = row_.pos(); vpos < end; ) {
741                 if (x_ > pi_.base.bv->workWidth())
742                         break;
743
744                 // Skip the separator at the logical end of the row
745                 if (vpos == skipped_sep_vpos) {
746                         ++vpos;
747                         continue;
748                 }
749
750                 pos_type const pos = bidi_.vis2log(vpos);
751
752                 if (pos >= par_.size()) {
753                         ++vpos;
754                         continue;
755                 }
756
757                 // Use font span to speed things up, see above
758                 if (vpos < font_span.first || vpos > font_span.last) {
759                         font_span = par_.fontSpan(vpos);
760                         font = text_metrics_.displayFont(pit_, vpos);
761
762                         // split font span if inline completion is inside
763                         if (font_span.first <= inlineCompletionVPos
764                             && font_span.last > inlineCompletionVPos)
765                                 font_span.last = inlineCompletionVPos;
766                 }
767
768                 const int width_pos = pm_.singleWidth(pos, font);
769
770                 if (x_ + width_pos < 0) {
771                         x_ += width_pos;
772                         ++vpos;
773                         continue;
774                 }
775                 Change const & change = par_.lookupChange(pos);
776                 if (change.changed() && !change_running.changed()) {
777                         change_running = change;
778                         change_last_x = int(x_);
779                 }
780
781                 Inset const * inset = par_.getInset(pos);
782                 bool const highly_editable_inset = inset
783                         && inset->editable() == Inset::HIGHLY_EDITABLE;
784
785                 // If we reach the end of a change or if the author changes, paint it.
786                 // We also don't paint across things like tables
787                 if (change_running.changed() && (highly_editable_inset
788                         || !change.changed() || !change_running.isSimilarTo(change))) {
789                         // Calculate 1/3 height of the buffer's default font
790                         FontMetrics const & fm
791                                 = theFontMetrics(pi_.base.bv->buffer().params().getFont());
792                         int const y_bar = change_running.deleted() ?
793                                 yo_ - fm.maxAscent() / 3 : yo_ + fm.maxAscent() / 6;
794                         pi_.pain.line(change_last_x, y_bar, int(x_), y_bar,
795                                 change_running.color(), Painter::line_solid,
796                                 Painter::line_thin);
797
798                         // Change might continue with a different author or type
799                         if (change.changed() && !highly_editable_inset) {
800                                 change_running = change;
801                                 change_last_x = int(x_);
802                         } else
803                                 change_running.setUnchanged();
804                 }
805
806                 if (body_pos > 0 && pos == body_pos - 1) {
807                         int const lwidth = theFontMetrics(labelFont())
808                                 .width(layout.labelsep);
809
810                         x_ += row_.label_hfill + lwidth - width_pos;
811                 }
812                 
813                 // Is the inline completion in front of character?
814                 if (font.isRightToLeft() && vpos == inlineCompletionVPos)
815                         paintInlineCompletion(font);
816
817                 if (par_.isSeparator(pos)) {
818                         Font const orig_font = text_metrics_.displayFont(pit_, pos);
819                         double const orig_x = x_;
820                         x_ += width_pos;
821                         if (pos >= body_pos)
822                                 x_ += row_.separator;
823                         paintForeignMark(orig_x, orig_font.language());
824                         ++vpos;
825
826                 } else if (inset) {
827                         // If outer row has changed, nested insets are repaint completely.
828                         pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
829                         
830                         bool const pi_selected = pi_.selected;
831                         Cursor const & cur = pi_.base.bv->cursor();
832                         if (cur.selection() && cur.text() == &text_ 
833                                   && cur.anchor().text() == &text_)
834                                 pi_.selected = row_.sel_beg <= pos && row_.sel_end > pos; 
835                         paintInset(inset, pos);
836                         pi_.selected = pi_selected;
837                         ++vpos;
838
839                 } else {
840                         // paint as many characters as possible.
841                         paintFromPos(vpos);
842                 }
843
844                 // Is the inline completion after character?
845                 if (!font.isRightToLeft() && vpos - 1 == inlineCompletionVPos)
846                         paintInlineCompletion(font);
847         }
848
849         // if we reach the end of a struck out range, paint it
850         if (change_running.changed()) {
851                 FontMetrics const & fm
852                         = theFontMetrics(pi_.base.bv->buffer().params().getFont());
853                 int const y_bar = change_running.deleted() ?
854                                 yo_ - fm.maxAscent() / 3 : yo_ + fm.maxAscent() / 6;
855                 pi_.pain.line(change_last_x, y_bar, int(x_), y_bar,
856                         change_running.color(), Painter::line_solid, Painter::line_thin);
857                 change_running.setUnchanged();
858         }
859 }
860
861
862 void RowPainter::paintInlineCompletion(Font const & font)
863 {
864         docstring completion = pi_.base.bv->inlineCompletion();
865         FontInfo f = font.fontInfo();
866         bool rtl = font.isRightToLeft();
867         
868         // draw the unique and the non-unique completion part
869         // Note: this is not time-critical as it is
870         // only done once per screen.
871         size_t uniqueTo = pi_.base.bv->inlineCompletionUniqueChars();
872         docstring s1 = completion.substr(0, uniqueTo);
873         docstring s2 = completion.substr(uniqueTo);
874         ColorCode c1 = Color_inlinecompletion;
875         ColorCode c2 = Color_nonunique_inlinecompletion;
876         
877         // right to left?
878         if (rtl) {
879                 swap(s1, s2);
880                 swap(c1, c2);
881         }
882
883         if (s1.size() > 0) {
884                 f.setColor(c1);
885                 pi_.pain.text(int(x_), yo_, s1, f);
886                 x_ += theFontMetrics(font).width(s1);
887         }
888         
889         if (s2.size() > 0) {
890                 f.setColor(c2);
891                 pi_.pain.text(int(x_), yo_, s2, f);
892                 x_ += theFontMetrics(font).width(s2);
893         }
894 }
895
896 } // namespace lyx