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