]> git.lyx.org Git - features.git/blob - src/rowpainter.C
remove references to Qt3/Gtk
[features.git] / src / rowpainter.C
1 /**
2  * \file rowpainter.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author various
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "rowpainter.h"
15
16 #include "buffer.h"
17 #include "coordcache.h"
18 #include "cursor.h"
19 #include "debug.h"
20 #include "bufferparams.h"
21 #include "BufferView.h"
22 #include "encoding.h"
23 #include "gettext.h"
24 #include "language.h"
25 #include "LColor.h"
26 #include "lyxrc.h"
27 #include "lyxrow.h"
28 #include "metricsinfo.h"
29 #include "paragraph.h"
30 #include "paragraph_funcs.h"
31 #include "ParagraphParameters.h"
32 #include "vspace.h"
33
34 #include "frontends/FontMetrics.h"
35 #include "frontends/Painter.h"
36
37 #include "insets/insettext.h"
38
39 #include "support/textutils.h"
40
41 #include <boost/crc.hpp>
42
43
44 namespace lyx {
45
46 using frontend::Painter;
47 using frontend::FontMetrics;
48
49 using std::endl;
50 using std::max;
51 using std::min;
52 using std::string;
53
54
55 namespace {
56
57 /// Flag: do a full redraw of inside text of inset
58 /// Working variable indicating a full screen refresh
59 bool refreshInside;
60
61 /**
62  * A class used for painting an individual row of text.
63  */
64 class RowPainter {
65 public:
66         /// initialise and run painter
67         RowPainter(PainterInfo & pi, LyXText const & text,
68                 pit_type pit, Row const & row, int x, int y);
69
70         // paint various parts
71         void paintAppendix();
72         void paintDepthBar();
73         void paintChangeBar();
74         void paintFirst();
75         void paintLast();
76         void paintText();
77
78 private:
79         void paintForeignMark(double orig_x, LyXFont const & font, int desc = 0);
80         void paintHebrewComposeChar(pos_type & vpos, LyXFont const & font);
81         void paintArabicComposeChar(pos_type & vpos, LyXFont const & font);
82         void paintChars(pos_type & vpos, LyXFont const & font,
83                         bool hebrew, bool arabic);
84         int paintAppendixStart(int y);
85         void paintFromPos(pos_type & vpos);
86         void paintInset(pos_type const pos, LyXFont const & font);
87
88         /// return left margin
89         int leftMargin() const;
90
91         /// return the label font for this row
92         LyXFont const getLabelFont() const;
93
94         /// bufferview to paint on
95         BufferView & bv_;
96
97         /// Painter to use
98         Painter & pain_;
99
100         /// LyXText for the row
101         LyXText const & text_;
102         ParagraphList const & pars_;
103
104         /// The row to paint
105         Row const & row_;
106
107         /// Row's paragraph
108         pit_type const pit_;
109         Paragraph const & par_;
110
111         /// is row erased? (change tracking)
112         bool erased_;
113
114         // Looks ugly - is
115         double const xo_;
116         int const yo_;    // current baseline
117         double x_;
118         int width_;
119         double separator_;
120         double hfill_;
121         double label_hfill_;
122 };
123
124
125 RowPainter::RowPainter(PainterInfo & pi,
126         LyXText const & text, pit_type pit, Row const & row, int x, int y)
127         : bv_(*pi.base.bv), pain_(pi.pain), text_(text), pars_(text.paragraphs()),
128           row_(row), pit_(pit), par_(text.paragraphs()[pit]),
129           erased_(pi.erased_),
130           xo_(x), yo_(y), width_(text_.width())
131 {
132         RowMetrics m = text_.computeRowMetrics(*bv_.buffer(), pit, row_);
133         x_ = m.x + xo_;
134
135         //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
136         //row_.dump();
137
138         separator_ = m.separator;
139         hfill_ = m.hfill;
140         label_hfill_ = m.label_hfill;
141
142         BOOST_ASSERT(pit >= 0);
143         BOOST_ASSERT(pit < int(text.paragraphs().size()));
144 }
145
146
147 LyXFont const RowPainter::getLabelFont() const
148 {
149         return text_.getLabelFont(*bv_.buffer(), par_);
150 }
151
152
153 int RowPainter::leftMargin() const
154 {
155         return text_.leftMargin(*bv_.buffer(), pit_, row_.pos());
156 }
157
158
159 void RowPainter::paintInset(pos_type const pos, LyXFont const & font)
160 {
161         InsetBase const * inset = par_.getInset(pos);
162         BOOST_ASSERT(inset);
163         PainterInfo pi(const_cast<BufferView *>(&bv_), pain_);
164         // FIXME: We should always use font, see documentation of
165         // noFontChange() in insetbase.h.
166         pi.base.font = inset->noFontChange() ?
167                 bv_.buffer()->params().getFont() :
168                 font;
169         pi.ltr_pos = (text_.bidi.level(pos) % 2 == 0);
170         pi.erased_ = erased_ || par_.isDeleted(pos);
171         bv_.coordCache().insets().add(inset, int(x_), yo_);
172         InsetText const * const in = inset->asTextInset();
173         // non-wide insets are painted completely. Recursive
174         bool tmp = refreshInside;
175         if (!in || !in->Wide()) {
176                 refreshInside = true;
177                 lyxerr[Debug::PAINTING] << endl << "Paint inset fully" << endl;
178         }
179         if (refreshInside)
180                 inset->drawSelection(pi, int(x_), yo_);
181         inset->draw(pi, int(x_), yo_);
182         refreshInside = tmp;
183         x_ += inset->width();
184 }
185
186
187 void RowPainter::paintHebrewComposeChar(pos_type & vpos, LyXFont const & font)
188 {
189         pos_type pos = text_.bidi.vis2log(vpos);
190
191         docstring str;
192
193         // first char
194         char_type c = par_.getChar(pos);
195         str += c;
196         ++vpos;
197
198         int const width = theFontMetrics(font).width(c);
199         int dx = 0;
200
201         for (pos_type i = pos - 1; i >= 0; --i) {
202                 c = par_.getChar(i);
203                 if (!Encodings::isComposeChar_hebrew(c)) {
204                         if (isPrintableNonspace(c)) {
205                                 int const width2 = text_.singleWidth(par_, i, c,
206                                         text_.getFont(*bv_.buffer(), par_, i));
207                                 // FIXME UNICODE
208                                 // This does not work anymore, and non-ascii
209                                 // characters in source files are forbidden
210                                 // anyway.
211                                 // dalet / resh
212                                 dx = (c == 'ø' || c == 'ã')
213                                         ? width2 - width
214                                         : (width2 - width) / 2;
215                         }
216                         break;
217                 }
218         }
219
220         // Draw nikud
221         // FIXME UNICODE
222         pain_.text(int(x_) + dx, yo_, str, font);
223 }
224
225
226 void RowPainter::paintArabicComposeChar(pos_type & vpos, LyXFont const & font)
227 {
228         pos_type pos = text_.bidi.vis2log(vpos);
229         docstring str;
230
231         // first char
232         char_type c = par_.getChar(pos);
233         c = par_.transformChar(c, pos);
234         str += c;
235         ++vpos;
236
237         int const width = theFontMetrics(font).width(c);
238         int dx = 0;
239
240         for (pos_type i = pos - 1; i >= 0; --i) {
241                 c = par_.getChar(i);
242                 if (!Encodings::isComposeChar_arabic(c)) {
243                         if (isPrintableNonspace(c)) {
244                                 int const width2 = text_.singleWidth(par_, i, c,
245                                                 text_.getFont(*bv_.buffer(), par_, i));
246                                 dx = (width2 - width) / 2;
247                         }
248                         break;
249                 }
250         }
251         // Draw nikud
252         pain_.text(int(x_) + dx, yo_, str, font);
253 }
254
255 void RowPainter::paintChars(pos_type & vpos, LyXFont const & font,
256                             bool hebrew, bool arabic)
257 {
258         // This method takes up 70% of time when typing
259         pos_type pos = text_.bidi.vis2log(vpos);
260         pos_type const end = row_.endpos();
261         FontSpan const font_span = par_.fontSpan(pos);
262         Change::Type const prev_change = par_.lookupChange(pos).type;
263
264         // first character
265         std::vector<char_type> str;
266         str.reserve(100);
267         str.push_back(par_.getChar(pos));
268
269         if (arabic) {
270                 char_type c = str[0];
271                 str[0] = par_.transformChar(c, pos);
272         }
273
274         // collect as much similar chars as we can
275         for (++vpos ; vpos < end ; ++vpos) {
276                 pos = text_.bidi.vis2log(vpos);
277                 if (pos < font_span.first || pos > font_span.last)
278                         break;
279
280                 if (prev_change != par_.lookupChange(pos).type)
281                         break;
282
283                 char_type c = par_.getChar(pos);
284
285                 if (!isPrintableNonspace(c))
286                         break;
287
288                 if (arabic && Encodings::isComposeChar_arabic(c))
289                         break;
290
291                 if (hebrew && Encodings::isComposeChar_hebrew(c))
292                         break;
293
294                 if (arabic)
295                         c = par_.transformChar(c, pos);
296
297                 str.push_back(c);
298         }
299
300         if (prev_change != Change::UNCHANGED) {
301                 LyXFont copy(font);
302                 if (prev_change == Change::DELETED) {
303                         copy.setColor(LColor::strikeout);
304                 } else if (prev_change == Change::INSERTED) {
305                         copy.setColor(LColor::newtext);
306                 }
307                 x_ += pain_.text(int(x_), yo_, &str[0], str.size(), copy);
308         } else {
309                 x_ += pain_.text(int(x_), yo_, &str[0], str.size(), font);
310         }
311 }
312
313
314 void RowPainter::paintForeignMark(double orig_x, LyXFont const & font, int desc)
315 {
316         if (!lyxrc.mark_foreign_language)
317                 return;
318         if (font.language() == latex_language)
319                 return;
320         if (font.language() == bv_.buffer()->params().language)
321                 return;
322
323         int const y = yo_ + 1 + desc;
324         pain_.line(int(orig_x), y, int(x_), y, LColor::language);
325 }
326
327
328 void RowPainter::paintFromPos(pos_type & vpos)
329 {
330         pos_type const pos = text_.bidi.vis2log(vpos);
331         LyXFont orig_font = text_.getFont(*bv_.buffer(), par_, pos);
332
333         double const orig_x = x_;
334
335         if (par_.isInset(pos)) {
336                 paintInset(pos, orig_font);
337                 ++vpos;
338                 paintForeignMark(orig_x, orig_font,
339                         par_.getInset(pos)->descent());
340                 return;
341         }
342
343         // usual characters, no insets
344         char_type const c = par_.getChar(pos);
345
346         // special case languages
347         std::string const & lang = orig_font.language()->lang();
348         bool const hebrew = lang == "hebrew";
349         bool const arabic = lang == "arabic" &&
350                 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
351                 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
352
353         // draw as many chars as we can
354         if ((!hebrew && !arabic)
355                 || (hebrew && !Encodings::isComposeChar_hebrew(c))
356                 || (arabic && !Encodings::isComposeChar_arabic(c))) {
357                 paintChars(vpos, orig_font, hebrew, arabic);
358         } else if (hebrew) {
359                 paintHebrewComposeChar(vpos, orig_font);
360         } else if (arabic) {
361                 paintArabicComposeChar(vpos, orig_font);
362         }
363
364         paintForeignMark(orig_x, orig_font);
365 }
366
367
368 void RowPainter::paintChangeBar()
369 {
370         pos_type const start = row_.pos();
371         pos_type const end = row_.endpos();
372
373         if (start == end || !par_.isChanged(start, end))
374                 return;
375
376         int const height = text_.isLastRow(pit_, row_)
377                 ? row_.ascent()
378                 : row_.height();
379
380         pain_.fillRectangle(5, yo_ - row_.ascent(), 3, height, LColor::changebar);
381 }
382
383
384 void RowPainter::paintAppendix()
385 {
386         if (!par_.params().appendix())
387                 return;
388
389         int y = yo_ - row_.ascent();
390
391         if (par_.params().startOfAppendix())
392                 y += 2 * defaultRowHeight();
393
394         pain_.line(1, y, 1, yo_ + row_.height(), LColor::appendix);
395         pain_.line(width_ - 2, y, width_ - 2, yo_ + row_.height(), LColor::appendix);
396 }
397
398
399 void RowPainter::paintDepthBar()
400 {
401         depth_type const depth = par_.getDepth();
402
403         if (depth <= 0)
404                 return;
405
406         depth_type prev_depth = 0;
407         if (!text_.isFirstRow(pit_, row_)) {
408                 pit_type pit2 = pit_;
409                 if (row_.pos() == 0)
410                         --pit2;
411                 prev_depth = pars_[pit2].getDepth();
412         }
413
414         depth_type next_depth = 0;
415         if (!text_.isLastRow(pit_, row_)) {
416                 pit_type pit2 = pit_;
417                 if (row_.endpos() >= pars_[pit2].size())
418                         ++pit2;
419                 next_depth = pars_[pit2].getDepth();
420         }
421
422         for (depth_type i = 1; i <= depth; ++i) {
423                 int const w = nestMargin() / 5;
424                 int x = int(xo_) + w * i;
425                 // only consider the changebar space if we're drawing outermost text
426                 if (text_.isMainText(*bv_.buffer()))
427                         x += changebarMargin();
428
429                 int const starty = yo_ - row_.ascent();
430                 int const h =  row_.height() - 1 - (i - next_depth - 1) * 3;
431
432                 pain_.line(x, starty, x, starty + h, LColor::depthbar);
433
434                 if (i > prev_depth)
435                         pain_.fillRectangle(x, starty, w, 2, LColor::depthbar);
436                 if (i > next_depth)
437                         pain_.fillRectangle(x, starty + h, w, 2, LColor::depthbar);
438         }
439 }
440
441
442 int RowPainter::paintAppendixStart(int y)
443 {
444         LyXFont pb_font;
445         pb_font.setColor(LColor::appendix);
446         pb_font.decSize();
447
448         int w = 0;
449         int a = 0;
450         int d = 0;
451
452         docstring const label = _("Appendix");
453         theFontMetrics(pb_font).rectText(label, w, a, d);
454
455         int const text_start = int(xo_ + (width_ - w) / 2);
456         int const text_end = text_start + w;
457
458         pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
459
460         pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
461         pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
462
463         return 3 * defaultRowHeight();
464 }
465
466
467 void RowPainter::paintFirst()
468 {
469         ParagraphParameters const & parparams = par_.params();
470
471         int y_top = 0;
472
473         // start of appendix?
474         if (parparams.startOfAppendix())
475                 y_top += paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
476
477         Buffer const & buffer = *bv_.buffer();
478
479         LyXLayout_ptr const & layout = par_.layout();
480
481         if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
482                 if (pit_ != 0) {
483                         if (layout->latextype == LATEX_PARAGRAPH
484                                 && !par_.getDepth()) {
485                                 y_top += buffer.params().getDefSkip().inPixels(bv_);
486                         } else {
487                                 LyXLayout_ptr const & playout = pars_[pit_ - 1].layout();
488                                 if (playout->latextype == LATEX_PARAGRAPH
489                                         && !pars_[pit_ - 1].getDepth()) {
490                                         // is it right to use defskip here, too? (AS)
491                                         y_top += buffer.params().getDefSkip().inPixels(bv_);
492                                 }
493                         }
494                 }
495         }
496
497         bool const is_rtl = text_.isRTL(buffer, par_);
498         bool const is_seq = isFirstInSequence(pit_, text_.paragraphs());
499         //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << std::endl;
500
501         // should we print a label?
502         if (layout->labeltype >= LABEL_STATIC
503             && (layout->labeltype != LABEL_STATIC
504                       || layout->latextype != LATEX_ENVIRONMENT
505                       || is_seq)) {
506
507                 LyXFont const font = getLabelFont();
508                 FontMetrics const & fm = theFontMetrics(font);
509
510                 docstring const str = par_.getLabelstring();
511                 if (!str.empty()) {
512                         double x = x_;
513
514                         // this is special code for the chapter layout. This is
515                         // printed in an extra row and has a pagebreak at
516                         // the top.
517                         if (layout->counter == "chapter") {
518                                 double spacing_val = 1.0;
519                                 if (!parparams.spacing().isDefault()) {
520                                         spacing_val = parparams.spacing().getValue();
521                                 } else {
522                                         spacing_val = buffer.params().spacing().getValue();
523                                 }
524
525                                 int const labeladdon = int(fm.maxHeight() * layout->spacing.getValue() * spacing_val);
526
527                                 int const maxdesc = int(fm.maxDescent() * layout->spacing.getValue() * spacing_val)
528                                         + int(layout->parsep) * defaultRowHeight();
529
530                                 if (is_rtl) {
531                                         x = width_ - leftMargin() -
532                                                 fm.width(str);
533                                 }
534
535                                 pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
536                         } else {
537                                 // FIXME UNICODE
538                                 docstring lab = from_utf8(layout->labelsep);
539                                 if (is_rtl) {
540                                         x = width_ - leftMargin()
541                                                 + fm.width(lab);
542                                 } else {
543                                         x = x_ - fm.width(lab)
544                                                 - fm.width(str);
545                                 }
546
547                                 pain_.text(int(x), yo_, str, font);
548                         }
549                 }
550
551         // the labels at the top of an environment.
552         // More or less for bibliography
553         } else if (is_seq &&
554                 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
555                 layout->labeltype == LABEL_BIBLIO ||
556                 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
557                 LyXFont font = getLabelFont();
558                 if (!par_.getLabelstring().empty()) {
559                         docstring const str = par_.getLabelstring();
560                         double spacing_val = 1.0;
561                         if (!parparams.spacing().isDefault())
562                                 spacing_val = parparams.spacing().getValue();
563                         else
564                                 spacing_val = buffer.params().spacing().getValue();
565
566                         FontMetrics const & fm = theFontMetrics(font);
567
568                         int const labeladdon = int(fm.maxHeight()
569                                 * layout->spacing.getValue() * spacing_val);
570
571                         int maxdesc =
572                                 int(fm.maxDescent() * layout->spacing.getValue() * spacing_val
573                                 + (layout->labelbottomsep * defaultRowHeight()));
574
575                         double x = x_;
576                         if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
577                                 if (is_rtl)
578                                         x = leftMargin();
579                                 x += (width_ - text_.rightMargin(buffer, par_) - leftMargin()) / 2;
580                                 x -= fm.width(str) / 2;
581                         } else if (is_rtl) {
582                                 x = width_ - leftMargin() -     fm.width(str);
583                         }
584                         pain_.text(int(x), yo_ - maxdesc - labeladdon, str, font);
585                 }
586         }
587 }
588
589
590 void RowPainter::paintLast()
591 {
592         bool const is_rtl = text_.isRTL(*bv_.buffer(), par_);
593         int const endlabel = getEndLabel(pit_, text_.paragraphs());
594
595         // draw an endlabel
596         switch (endlabel) {
597         case END_LABEL_BOX:
598         case END_LABEL_FILLED_BOX: {
599                 LyXFont const font = getLabelFont();
600                 FontMetrics const & fm = theFontMetrics(font);
601                 int const size = int(0.75 * fm.maxAscent());
602                 int const y = yo_ - size;
603                 int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size;
604
605                 if (width_ - int(row_.width()) <= size)
606                         x += (size - width_ + row_.width() + 1) * (is_rtl ? -1 : 1);
607
608                 if (endlabel == END_LABEL_BOX)
609                         pain_.rectangle(x, y, size, size, LColor::eolmarker);
610                 else
611                         pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
612                 break;
613         }
614
615         case END_LABEL_STATIC: {
616                 LyXFont font = getLabelFont();
617                 FontMetrics const & fm = theFontMetrics(font);
618                 docstring const & str = par_.layout()->endlabelstring();
619                 double const x = is_rtl ?
620                         x_ - fm.width(str)
621                         : - text_.rightMargin(*bv_.buffer(), par_) - row_.width();
622                 pain_.text(int(x), yo_, str, font);
623                 break;
624         }
625
626         case END_LABEL_NO_LABEL:
627                 break;
628         }
629 }
630
631
632 void RowPainter::paintText()
633 {
634         pos_type const end = row_.endpos();
635         pos_type body_pos = par_.beginOfBody();
636         if (body_pos > 0 &&
637                 (body_pos > end || !par_.isLineSeparator(body_pos - 1))) {
638                 body_pos = 0;
639         }
640
641         LyXLayout_ptr const & layout = par_.layout();
642
643         bool running_strikeout = false;
644         bool is_struckout = false;
645         int last_strikeout_x = 0;
646
647         // Use font span to speed things up, see below
648         FontSpan font_span;
649         LyXFont font;
650         Buffer const & buffer = *bv_.buffer();
651
652         for (pos_type vpos = row_.pos(); vpos < end; ) {
653                 if (x_ > bv_.workWidth())
654                         break;
655
656                 pos_type const pos = text_.bidi.vis2log(vpos);
657
658                 if (pos >= par_.size()) {
659                         ++vpos;
660                         continue;
661                 }
662
663                 // Use font span to speed things up, see above
664                 if (vpos < font_span.first || vpos > font_span.last) {
665                         font_span = par_.fontSpan(vpos);
666                         font = text_.getFont(buffer, par_, vpos);
667                 }
668
669                 const int width_pos =
670                         text_.singleWidth(par_, pos, par_.getChar(pos), font);
671
672                 if (x_ + width_pos < 0) {
673                         x_ += width_pos;
674                         ++vpos;
675                         continue;
676                 }
677
678                 is_struckout = par_.isDeleted(pos);
679
680                 if (is_struckout && !running_strikeout) {
681                         running_strikeout = true;
682                         last_strikeout_x = int(x_);
683                 }
684
685                 bool const highly_editable_inset = par_.isInset(pos)
686                         && isHighlyEditableInset(par_.getInset(pos));
687
688                 // If we reach the end of a struck out range, paint it.
689                 // We also don't paint across things like tables
690                 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
691                         // Calculate 1/3 height of the buffer's default font
692                         FontMetrics const & fm 
693                                 = theFontMetrics(bv_.buffer()->params().getFont());
694                         int const middle = yo_ - fm.maxAscent() / 3;
695                         pain_.line(last_strikeout_x, middle, int(x_), middle,
696                                 LColor::strikeout, Painter::line_solid, Painter::line_thin);
697                         running_strikeout = false;
698                 }
699
700                 if (body_pos > 0 && pos == body_pos - 1) {
701                         // FIXME UNICODE
702                         int const lwidth = theFontMetrics(getLabelFont())
703                                 .width(from_utf8(layout->labelsep));
704
705                         x_ += label_hfill_ + lwidth - width_pos;
706                 }
707
708                 if (par_.isHfill(pos)) {
709                         x_ += 1;
710
711                         int const y0 = yo_;
712                         int const y1 = y0 - defaultRowHeight() / 2;
713
714                         pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
715
716                         if (par_.hfillExpansion(row_, pos)) {
717                                 int const y2 = (y0 + y1) / 2;
718
719                                 if (pos >= body_pos) {
720                                         pain_.line(int(x_), y2, int(x_ + hfill_), y2,
721                                                   LColor::added_space,
722                                                   Painter::line_onoffdash);
723                                         x_ += hfill_;
724                                 } else {
725                                         pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
726                                                   LColor::added_space,
727                                                   Painter::line_onoffdash);
728                                         x_ += label_hfill_;
729                                 }
730                                 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
731                         }
732                         x_ += 2;
733                         ++vpos;
734                 } else if (par_.isSeparator(pos)) {
735                         x_ += width_pos;
736                         if (pos >= body_pos)
737                                 x_ += separator_;
738                         ++vpos;
739                 } else {
740                         paintFromPos(vpos);
741                 }
742         }
743
744         // if we reach the end of a struck out range, paint it
745         if (running_strikeout) {
746                 // calculate 1/3 height of the buffer's default font
747                 FontMetrics const & fm 
748                         = theFontMetrics(bv_.buffer()->params().getFont());
749                 int const middle = yo_ - fm.maxAscent() / 3;
750                 pain_.line(last_strikeout_x, middle, int(x_), middle,
751                         LColor::strikeout, Painter::line_solid, Painter::line_thin);
752                 running_strikeout = false;
753         }
754 }
755
756
757 size_type calculateRowSignature(Row const & row, Paragraph const & par,
758         int x, int y)
759 {
760         boost::crc_32_type crc;
761         for (pos_type i = row.pos(); i < row.endpos(); ++i) {
762                 char_type const b[] = { par.getChar(i) };
763                 crc.process_bytes(b, 1);
764         }
765         char_type const b[] = { x, y, row.width() };
766         crc.process_bytes(b, 3);
767         return crc.checksum();
768 }
769
770
771 bool CursorOnRow(PainterInfo & pi, pit_type const pit,
772         RowList::const_iterator rit, LyXText const & text)
773 {
774         // Is there a cursor on this row (or inside inset on row)
775         LCursor & cur = pi.base.bv->cursor();
776         for (size_type d = 0; d < cur.depth(); ++d) {
777                 CursorSlice const & sl = cur[d];
778                 if (sl.text() == &text
779                     && sl.pit() == pit
780                     && sl.pos() >= rit->pos()
781                     && sl.pos() <= rit->endpos())
782                         return true;
783         }
784         return false;
785 }
786
787
788 bool innerCursorOnRow(PainterInfo & pi, pit_type pit,
789         RowList::const_iterator rit, LyXText const & text)
790 {
791         // Is there a cursor inside an inset on this row, and is this inset
792         // the only "character" on this row
793         LCursor & cur = pi.base.bv->cursor();
794         if (rit->pos() + 1 != rit->endpos())
795                 return false;
796         for (size_type d = 0; d < cur.depth(); d++) {
797                 CursorSlice const & sl = cur[d];
798                 if (sl.text() == &text
799                     && sl.pit() == pit
800                     && sl.pos() == rit->pos())
801                         return d < cur.depth() - 1;
802         }
803         return false;
804 }
805
806
807 void paintPar
808         (PainterInfo & pi, LyXText const & text, pit_type pit, int x, int y,
809          bool repaintAll)
810 {
811 //      lyxerr << "  paintPar: pit: " << pit << " at y: " << y << endl;
812         int const ww = pi.base.bv->workHeight();
813
814         pi.base.bv->coordCache().parPos()[&text][pit] = Point(x, y);
815
816         Paragraph const & par = text.paragraphs()[pit];
817         if (par.rows().empty())
818                 return;
819
820         RowList::const_iterator const rb = par.rows().begin();
821         RowList::const_iterator const re = par.rows().end();
822
823         y -= rb->ascent();
824         size_type rowno = 0;
825         for (RowList::const_iterator rit = rb; rit != re; ++rit, ++rowno) {
826                 y += rit->ascent();
827                 // Allow setting of refreshInside for nested insets in
828                 // this row only
829                 bool tmp = refreshInside;
830
831                 // Row signature; has row changed since last paint?
832                 size_type const row_sig = calculateRowSignature(*rit, par, x, y);
833                 bool row_has_changed = par.rowSignature()[rowno] != row_sig;
834
835                 bool cursor_on_row = CursorOnRow(pi, pit, rit, text);
836                 bool in_inset_alone_on_row = innerCursorOnRow(pi, pit, rit,
837                         text);
838
839                 // If this is the only object on the row, we can make it wide
840                 for (pos_type i = rit->pos() ; i != rit->endpos(); ++i) {
841                         InsetBase const * const in = par.getInset(i);
842                         if (in) {
843                                 InsetText const * const t = in->asTextInset();
844                                 if (t)
845                                         t->Wide() = in_inset_alone_on_row;
846                         }
847                 }
848
849                 // If selection is on, the current row signature differs
850                 // from cache, or cursor is inside an inset _on this row_,
851                 // then paint the row
852                 if (repaintAll || row_has_changed || cursor_on_row) {
853                         // Add to row signature cache
854                         par.rowSignature()[rowno] = row_sig;
855                         
856                         bool const inside = (y + rit->descent() >= 0
857                                 && y - rit->ascent() < ww);
858                         // it is not needed to draw on screen if we are not inside.
859                         pi.pain.setDrawingEnabled(inside);
860                         RowPainter rp(pi, text, pit, *rit, x, y);
861                         // Clear background of this row
862                         // (if paragraph background was not cleared)
863                         if (!repaintAll &&
864                             (!in_inset_alone_on_row || row_has_changed)) {
865                                 pi.pain.fillRectangle(x, y - rit->ascent(),
866                                     text.maxwidth_, rit->height(),
867                                     text.backgroundColor());
868                                 // If outer row has changed, force nested
869                                 // insets to repaint completely
870                                 if (row_has_changed)
871                                         refreshInside = true;
872                         }
873
874                         // Instrumentation for testing row cache (see also
875                         // 12 lines lower):
876                         if (text.isMainText(*pi.base.bv->buffer()))
877                                 lyxerr[Debug::PAINTING] << "#";
878                         else
879                                 lyxerr[Debug::PAINTING] << "[" <<
880                                     repaintAll << row_has_changed <<
881                                     cursor_on_row << "]";
882                         rp.paintAppendix();
883                         rp.paintDepthBar();
884                         rp.paintChangeBar();
885                         if (rit == rb)
886                                 rp.paintFirst();
887                         if (rit + 1 == re)
888                                 rp.paintLast();
889                         rp.paintText();
890                 }
891                 y += rit->descent();
892                 // Restore, see above
893                 refreshInside = tmp;
894         }
895         // Re-enable screen drawing for future use of the painter.
896         pi.pain.setDrawingEnabled(true);
897
898         lyxerr[Debug::PAINTING] << "." << endl;
899 }
900
901 } // namespace anon
902
903
904 void paintText(BufferView & bv,
905                Painter & pain)
906 {
907         BOOST_ASSERT(bv.buffer());
908         Buffer const & buffer = *bv.buffer();
909         LyXText & text = buffer.text();
910         bool const select = bv.cursor().selection();
911         ViewMetricsInfo const & vi = bv.viewMetricsInfo();
912         
913         PainterInfo pi(const_cast<BufferView *>(&bv), pain);
914         // Should the whole screen, including insets, be refreshed?
915         bool repaintAll = select || !vi.singlepar;
916
917         if (repaintAll) {
918                 // Clear background (if not delegated to rows)
919                 pain.fillRectangle(0, vi.y1, bv.workWidth(), vi.y2 - vi.y1,
920                         text.backgroundColor());
921         }
922         if (select) {
923                 text.drawSelection(pi, 0, 0);
924         }
925
926         int yy = vi.y1;
927         // draw contents
928         for (pit_type pit = vi.p1; pit <= vi.p2; ++pit) {
929                 refreshInside = repaintAll;
930                 Paragraph const & par = text.getPar(pit);
931                 yy += par.ascent();
932                 paintPar(pi, text, pit, 0, yy, repaintAll);
933                 yy += par.descent();
934         }
935
936         // Cache one paragraph above and one below
937         // Note MV: this cannot be suppressed even for singlepar.
938         // Try viewing the User Guide Mobius figure
939
940         if (vi.p1 > 0) {
941                 text.redoParagraph(bv, vi.p1 - 1);
942                 bv.coordCache().parPos()[&text][vi.p1 - 1] =
943                         Point(0, vi.y1 - text.getPar(vi.p1 - 1).descent());
944         }
945
946         if (vi.p2 < pit_type(text.paragraphs().size()) - 1) {
947                 text.redoParagraph(bv, vi.p2 + 1);
948                 bv.coordCache().parPos()[&text][vi.p2 + 1] =
949                         Point(0, vi.y2 + text.getPar(vi.p2 + 1).ascent());
950         }
951
952         // and grey out above (should not happen later)
953 //      lyxerr << "par ascent: " << text.getPar(vi.p1).ascent() << endl;
954         if (vi.y1 > 0 && !vi.singlepar)
955                 pain.fillRectangle(0, 0, bv.workWidth(), vi.y1, LColor::bottomarea);
956
957         // and possibly grey out below
958 //      lyxerr << "par descent: " << text.getPar(vi.p1).ascent() << endl;
959         if (vi.y2 < bv.workHeight() && !vi.singlepar)
960                 pain.fillRectangle(0, vi.y2, bv.workWidth(), bv.workHeight() - vi.y2, LColor::bottomarea);
961 }
962
963
964 void paintTextInset(LyXText const & text, PainterInfo & pi, int x, int y)
965 {
966 //      lyxerr << "  paintTextInset: y: " << y << endl;
967
968         y -= text.getPar(0).ascent();
969         // This flag can not be set from within same inset:
970         bool repaintAll = refreshInside;
971         for (int pit = 0; pit < int(text.paragraphs().size()); ++pit) {
972                 y += text.getPar(pit).ascent();
973                 paintPar(pi, text, pit, x, y, repaintAll);
974                 y += text.getPar(pit).descent();
975         }
976 }
977
978
979 } // namespace lyx