]> git.lyx.org Git - lyx.git/blob - src/rowpainter.C
architectural changes to tex2lyx
[lyx.git] / src / rowpainter.C
1 /**
2  * \file rowpainter.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author various
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13
14 #include "frontends/Painter.h"
15 #include "frontends/screen.h"
16 #include "frontends/font_metrics.h"
17 #include "support/LAssert.h"
18 #include "paragraph.h"
19 #include "paragraph_funcs.h"
20 #include "support/textutils.h"
21
22 #include "insets/insettext.h"
23 #include "ParagraphParameters.h"
24 #include "debug.h"
25 #include "BufferView.h"
26 #include "buffer.h"
27 #include "gettext.h"
28 #include "language.h"
29 #include "encoding.h"
30 #include "lyxtext.h"
31 #include "lyxrow.h"
32 #include "rowpainter.h"
33 #include "lyxrc.h"
34 #include "lyxrow_funcs.h"
35 #include "metricsinfo.h"
36
37 #include <algorithm>
38
39 using namespace lyx::support;
40
41 using std::max;
42 using lyx::pos_type;
43
44 extern int PAPER_MARGIN;
45 extern int CHANGEBAR_MARGIN;
46 extern int LEFT_MARGIN;
47
48 namespace {
49
50 // "temporary". We'll never get to use more
51 // references until we start adding hacks like
52 // these until other places catch up.
53 BufferView * perv(BufferView const & bv)
54 {
55         return const_cast<BufferView *>(&bv);
56 }
57
58 /**
59  * A class used for painting an individual row of text.
60  */
61 class RowPainter {
62 public:
63         /// initialise painter
64         RowPainter(BufferView const & bv, LyXText const & text,
65                 RowList::iterator rit, int y_offset, int x_offset, int y);
66
67         /// do the painting
68         void paint();
69 private:
70         // paint various parts
71         void paintBackground();
72         void paintSelection();
73         void paintAppendix();
74         void paintDepthBar();
75         void paintChangeBar();
76         void paintFirst();
77         void paintLast();
78         void paintForeignMark(double orig_x, LyXFont const & orig_font);
79         void paintHebrewComposeChar(lyx::pos_type & vpos);
80         void paintArabicComposeChar(lyx::pos_type & vpos);
81         void paintChars(lyx::pos_type & vpos, bool hebrew, bool arabic);
82         int paintPageBreak(string const & label, int y);
83         int paintAppendixStart(int y);
84         int paintLengthMarker(string const & prefix, VSpace const & vsp, int start);
85         void paintText();
86         void paintFromPos(lyx::pos_type & vpos);
87         void paintInset(lyx::pos_type const pos);
88
89         /// return left margin
90         int leftMargin() const;
91
92         /// return the font at the given pos
93         LyXFont const getFont(lyx::pos_type pos) const;
94
95         /// return the label font for this row
96         LyXFont const getLabelFont() const;
97
98         char const transformChar(char c, lyx::pos_type pos) const;
99
100         /// return pixel width for the given pos
101         int singleWidth(lyx::pos_type pos) const;
102         int singleWidth(lyx::pos_type pos, char c) const;
103
104         /// bufferview to paint on
105         BufferView const & bv_;
106
107         /// Painter to use
108         Painter & pain_;
109
110         /// LyXText for the row
111         LyXText const & text_;
112
113         /// The row to paint
114         RowList::iterator row_;
115
116         /// Row's paragraph
117         mutable ParagraphList::iterator  pit_;
118
119         // Looks ugly - is
120         double xo_;
121         int yo_;
122         double x_;
123         int y_;
124         int width_;
125         double separator_;
126         double hfill_;
127         double label_hfill_;
128 };
129
130
131 RowPainter::RowPainter(BufferView const & bv, LyXText const & text,
132      RowList::iterator rit,
133      int y_offset, int x_offset, int y)
134         : bv_(bv), pain_(bv_.painter()), text_(text), row_(rit),
135           pit_(rit->par()),
136                 xo_(x_offset), yo_(y_offset), y_(y)
137 {}
138
139
140 /// "temporary"
141 LyXFont const RowPainter::getFont(pos_type pos) const
142 {
143         return text_.getFont(pit_, pos);
144 }
145
146
147 int RowPainter::singleWidth(lyx::pos_type pos) const
148 {
149         return text_.singleWidth(pit_, pos);
150 }
151
152
153 int RowPainter::singleWidth(lyx::pos_type pos, char c) const
154 {
155         return text_.singleWidth(pit_, pos, c);
156 }
157
158
159 LyXFont const RowPainter::getLabelFont() const
160 {
161         return text_.getLabelFont(pit_);
162 }
163
164
165 char const RowPainter::transformChar(char c, lyx::pos_type pos) const
166 {
167         return text_.transformChar(c, *pit_, pos);
168 }
169
170
171 int RowPainter::leftMargin() const
172 {
173         return text_.leftMargin(*row_);
174 }
175
176
177 void RowPainter::paintInset(pos_type const pos)
178 {
179         InsetOld * inset = const_cast<InsetOld*>(pit_->getInset(pos));
180
181         Assert(inset);
182
183         PainterInfo pi(perv(bv_));
184         pi.base.font = getFont(pos);
185         inset->draw(pi, int(x_), yo_ + row_->baseline());
186         x_ += inset->width();
187 }
188
189
190 void RowPainter::paintHebrewComposeChar(pos_type & vpos)
191 {
192         pos_type pos = text_.vis2log(vpos);
193
194         string str;
195
196         // first char
197         char c = pit_->getChar(pos);
198         str += c;
199         ++vpos;
200
201         LyXFont const & font = getFont(pos);
202         int const width = font_metrics::width(c, font);
203         int dx = 0;
204
205         for (pos_type i = pos - 1; i >= 0; --i) {
206                 c = pit_->getChar(i);
207                 if (!Encodings::IsComposeChar_hebrew(c)) {
208                         if (IsPrintableNonspace(c)) {
209                                 int const width2 =
210                                         singleWidth(i, c);
211                                 // dalet / resh
212                                 dx = (c == 'ø' || c == 'ã')
213                                         ? width2 - width
214                                         : (width2 - width) / 2;
215                         }
216                         break;
217                 }
218         }
219
220         // Draw nikud
221         pain_.text(int(x_) + dx, yo_ + row_->baseline(), str, font);
222 }
223
224
225 void RowPainter::paintArabicComposeChar(pos_type & vpos)
226 {
227         pos_type pos = text_.vis2log(vpos);
228         string str;
229
230         // first char
231         char c = pit_->getChar(pos);
232         c = transformChar(c, pos);
233         str +=c;
234         ++vpos;
235
236         LyXFont const & font = getFont(pos);
237         int const width = font_metrics::width(c, font);
238         int dx = 0;
239
240         for (pos_type i = pos - 1; i >= 0; --i) {
241                 c = pit_->getChar(i);
242                 if (!Encodings::IsComposeChar_arabic(c)) {
243                         if (IsPrintableNonspace(c)) {
244                                 int const width2 =
245                                         singleWidth(i, c);
246                                 dx = (width2 - width) / 2;
247                         }
248                         break;
249                 }
250         }
251         // Draw nikud
252         pain_.text(int(x_) + dx, yo_ + row_->baseline(), str, font);
253 }
254
255
256 void RowPainter::paintChars(pos_type & vpos, bool hebrew, bool arabic)
257 {
258         pos_type pos = text_.vis2log(vpos);
259         pos_type const last = lastPrintablePos(text_, row_);
260         LyXFont orig_font = getFont(pos);
261
262         // first character
263         string str;
264         str += pit_->getChar(pos);
265         if (arabic) {
266                 unsigned char c = str[0];
267                 str[0] = transformChar(c, pos);
268         }
269
270         bool prev_struckout(isDeletedText(*pit_, pos));
271         bool prev_newtext(isInsertedText(*pit_, pos));
272
273         ++vpos;
274
275         // collect as much similar chars as we can
276         while (vpos <= last && (pos = text_.vis2log(vpos)) >= 0) {
277                 char c = pit_->getChar(pos);
278
279                 if (!IsPrintableNonspace(c))
280                         break;
281
282                 if (prev_struckout != isDeletedText(*pit_, pos))
283                         break;
284
285                 if (prev_newtext != isInsertedText(*pit_, pos))
286                         break;
287
288                 if (arabic && Encodings::IsComposeChar_arabic(c))
289                         break;
290                 if (hebrew && Encodings::IsComposeChar_hebrew(c))
291                         break;
292
293                 if (orig_font != getFont(pos))
294                         break;
295
296                 if (arabic)
297                         c = transformChar(c, pos);
298                 str += c;
299                 ++vpos;
300         }
301
302         if (prev_struckout) {
303                 orig_font.setColor(LColor::strikeout);
304         } else if (prev_newtext) {
305                 orig_font.setColor(LColor::newtext);
306         }
307
308         // Draw text and set the new x position
309         //lyxerr << "paint row: yo_ " << yo_ << " baseline: " << row_->baseline()
310         //      << "\n";
311         pain_.text(int(x_), yo_ + row_->baseline(), str, orig_font);
312         x_ += font_metrics::width(str, orig_font);
313 }
314
315
316 void RowPainter::paintForeignMark(double orig_x, LyXFont const & orig_font)
317 {
318         if (!lyxrc.mark_foreign_language)
319                 return;
320         if (orig_font.language() == latex_language)
321                 return;
322         if (orig_font.language() == bv_.buffer()->params.language)
323                 return;
324
325         int const y = yo_ + row_->baseline() + 1;
326         pain_.line(int(orig_x), y, int(x_), y, LColor::language);
327 }
328
329
330 void RowPainter::paintFromPos(pos_type & vpos)
331 {
332         pos_type const pos = text_.vis2log(vpos);
333
334         LyXFont const & orig_font = getFont(pos);
335
336         double const orig_x = x_;
337
338         char const c = pit_->getChar(pos);
339
340         if (IsInsetChar(c)) {
341                 paintInset(pos);
342                 ++vpos;
343                 paintForeignMark(orig_x, orig_font);
344                 return;
345         }
346
347         // usual characters, no insets
348
349         // special case languages
350         bool const hebrew = (orig_font.language()->lang() == "hebrew");
351         bool const arabic =
352                 orig_font.language()->lang() == "arabic" &&
353                 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
354                 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
355
356         // draw as many chars as we can
357         if ((!hebrew && !arabic)
358                 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
359                 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
360                 paintChars(vpos, hebrew, arabic);
361         } else if (hebrew) {
362                 paintHebrewComposeChar(vpos);
363         } else if (arabic) {
364                 paintArabicComposeChar(vpos);
365         }
366
367         paintForeignMark(orig_x, orig_font);
368
369         return;
370 }
371
372
373 void RowPainter::paintBackground()
374 {
375         int const x = int(xo_);
376         int const y = yo_ < 0 ? 0 : yo_;
377         int const h = yo_ < 0 ? row_->height() + yo_ : row_->height();
378         pain_.fillRectangle(x, y, width_, h, text_.backgroundColor());
379 }
380
381
382 void RowPainter::paintSelection()
383 {
384         bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params);
385
386         // the current selection
387         int const startx = text_.selection.start.x();
388         int const endx = text_.selection.end.x();
389         int const starty = text_.selection.start.y();
390         int const endy = text_.selection.end.y();
391         RowList::iterator startrow = text_.getRow(text_.selection.start);
392         RowList::iterator endrow = text_.getRow(text_.selection.end);
393
394         if (text_.bidi_same_direction) {
395                 int x;
396                 int y = yo_;
397                 int w;
398                 int h = row_->height();
399
400                 if (startrow == row_ && endrow == row_) {
401                         if (startx < endx) {
402                                 x = int(xo_) + startx;
403                                 w = endx - startx;
404                                 pain_.fillRectangle(x, y, w, h, LColor::selection);
405                         } else {
406                                 x = int(xo_) + endx;
407                                 w = startx - endx;
408                                 pain_.fillRectangle(x, y, w, h, LColor::selection);
409                         }
410                 } else if (startrow == row_) {
411                         int const x = is_rtl ? int(xo_) : int(xo_ + startx);
412                         int const w = is_rtl ? startx : (width_ - startx);
413                         pain_.fillRectangle(x, y, w, h, LColor::selection);
414                 } else if (endrow == row_) {
415                         int const x = is_rtl ? int(xo_ + endx) : int(xo_);
416                         int const w = is_rtl ? (width_ - endx) : endx;
417                         pain_.fillRectangle(x, y, w, h, LColor::selection);
418                 } else if (y_ > starty && y_ < endy) {
419                         pain_.fillRectangle(int(xo_), y, width_, h, LColor::selection);
420                 }
421                 return;
422         } else if (startrow != row_ && endrow != row_) {
423                 if (y_ > starty && y_ < endy) {
424                         int w = width_;
425                         int h = row_->height();
426                         pain_.fillRectangle(int(xo_), yo_, w, h, LColor::selection);
427                 }
428                 return;
429         }
430
431         if ((startrow != row_ && !is_rtl) || (endrow != row_ && is_rtl))
432                 pain_.fillRectangle(int(xo_), yo_,
433                         int(x_), row_->height(), LColor::selection);
434
435         pos_type const body_pos = pit_->beginningOfBody();
436         pos_type const last = lastPrintablePos(text_, row_);
437         double tmpx = x_;
438
439         for (pos_type vpos = row_->pos(); vpos <= last; ++vpos)  {
440                 pos_type pos = text_.vis2log(vpos);
441                 double const old_tmpx = tmpx;
442                 if (body_pos > 0 && pos == body_pos - 1) {
443                         LyXLayout_ptr const & layout = pit_->layout();
444                         LyXFont const lfont = getLabelFont();
445
446                         tmpx += label_hfill_ + font_metrics::width(layout->labelsep, lfont);
447
448                         if (pit_->isLineSeparator(body_pos - 1))
449                                 tmpx -= singleWidth(body_pos - 1);
450                 }
451
452                 if (hfillExpansion(text_, row_, pos)) {
453                         tmpx += singleWidth(pos);
454                         if (pos >= body_pos)
455                                 tmpx += hfill_;
456                         else
457                                 tmpx += label_hfill_;
458                 }
459
460                 else if (pit_->isSeparator(pos)) {
461                         tmpx += singleWidth(pos);
462                         if (pos >= body_pos)
463                                 tmpx += separator_;
464                 } else {
465                         tmpx += singleWidth(pos);
466                 }
467
468                 if ((startrow != row_ || text_.selection.start.pos() <= pos) &&
469                         (endrow != row_ || pos < text_.selection.end.pos())) {
470                         // Here we do not use x_ as xo_ was added to x_.
471                         pain_.fillRectangle(int(old_tmpx), yo_,
472                                 int(tmpx - old_tmpx + 1),
473                                 row_->height(), LColor::selection);
474                 }
475         }
476
477         if ((startrow != row_ && is_rtl) || (endrow != row_ && !is_rtl)) {
478                 pain_.fillRectangle(int(xo_ + tmpx),
479                                       yo_, int(bv_.workWidth() - tmpx),
480                                       row_->height(), LColor::selection);
481         }
482 }
483
484
485 void RowPainter::paintChangeBar()
486 {
487         pos_type const start = row_->pos();
488         pos_type const end = lastPrintablePos(text_, row_);
489
490         if (!pit_->isChanged(start, end))
491                 return;
492
493         int const height = (boost::next(row_) != text_.rows().end()
494                 ? row_->height() + boost::next(row_)->top_of_text()
495                 : row_->baseline());
496
497         pain_.fillRectangle(4, yo_, 5, height, LColor::changebar);
498 }
499
500
501 void RowPainter::paintAppendix()
502 {
503         if (!pit_->params().appendix())
504                 return;
505
506         // FIXME: can be just width_ ?
507         int const ww = bv_.workWidth();
508
509         int y = yo_;
510
511         if (pit_->params().startOfAppendix())
512                 y += 2 * defaultRowHeight();
513
514         pain_.line(1, y, 1, yo_ + row_->height(), LColor::appendix);
515         pain_.line(ww - 2, y, ww - 2, yo_ + row_->height(), LColor::appendix);
516 }
517
518
519 void RowPainter::paintDepthBar()
520 {
521         Paragraph::depth_type const depth = pit_->getDepth();
522
523         if (depth <= 0)
524                 return;
525
526         Paragraph::depth_type prev_depth = 0;
527         if (row_ != text_.rows().begin())
528                 prev_depth = boost::prior(row_)->par()->getDepth();
529         Paragraph::depth_type next_depth = 0;
530
531         RowList::iterator next_row = boost::next(row_);
532         if (next_row != text_.rows().end())
533                 next_depth = next_row->par()->getDepth();
534
535         for (Paragraph::depth_type i = 1; i <= depth; ++i) {
536                 int const w = PAPER_MARGIN / 5;
537                 int x = int(w * i + xo_);
538                 // only consider the changebar space if we're drawing outer left
539                 if (!xo_)
540                         x += CHANGEBAR_MARGIN;
541                 int const h = yo_ + row_->height() - 1 - (i - next_depth - 1) * 3;
542
543                 pain_.line(x, yo_, x, h, LColor::depthbar);
544
545                 if (i > prev_depth)
546                         pain_.fillRectangle(x, yo_, w, 2, LColor::depthbar);
547                 if (i > next_depth)
548                         pain_.fillRectangle(x, h, w, 2, LColor::depthbar);
549         }
550 }
551
552
553 int RowPainter::paintLengthMarker(string const & prefix, VSpace const & vsp, int start)
554 {
555         if (vsp.kind() == VSpace::NONE)
556                 return 0;
557
558         int const arrow_size = 4;
559         int const size = getLengthMarkerHeight(bv_, vsp);
560         int const end = start + size;
561
562         // the label to display (if any)
563         string str;
564         // y-values for top arrow
565         int ty1, ty2;
566         // y-values for bottom arrow
567         int by1, by2;
568
569         str = prefix + " (" + vsp.asLyXCommand() + ")";
570
571         if (vsp.kind() == VSpace::VFILL) {
572                 ty1 = ty2 = start;
573                 by1 = by2 = end;
574         } else {
575                 // adding or removing space
576                 bool const added = vsp.kind() != VSpace::LENGTH ||
577                                    vsp.length().len().value() > 0.0;
578                 ty1 = added ? (start + arrow_size) : start;
579                 ty2 = added ? start : (start + arrow_size);
580                 by1 = added ? (end - arrow_size) : end;
581                 by2 = added ? end : (end - arrow_size);
582         }
583
584         int const leftx = int(xo_) + leftMargin();
585         int const midx = leftx + arrow_size;
586         int const rightx = midx + arrow_size;
587
588         // first the string
589         int w = 0;
590         int a = 0;
591         int d = 0;
592
593         LyXFont font;
594         font.setColor(LColor::added_space).decSize().decSize();
595         font_metrics::rectText(str, font, w, a, d);
596
597         pain_.rectText(leftx + 2 * arrow_size + 5,
598                          start + ((end - start) / 2) + d,
599                          str, font);
600
601         // top arrow
602         pain_.line(leftx, ty1, midx, ty2, LColor::added_space);
603         pain_.line(midx, ty2, rightx, ty1, LColor::added_space);
604
605         // bottom arrow
606         pain_.line(leftx, by1, midx, by2, LColor::added_space);
607         pain_.line(midx, by2, rightx, by1, LColor::added_space);
608
609         // joining line
610         pain_.line(midx, ty2, midx, by2, LColor::added_space);
611
612         return size;
613 }
614
615
616 int RowPainter::paintPageBreak(string const & label, int y)
617 {
618         LyXFont pb_font;
619         pb_font.setColor(LColor::pagebreak).decSize();
620
621         int w = 0;
622         int a = 0;
623         int d = 0;
624         font_metrics::rectText(label, pb_font, w, a, d);
625
626         int const text_start = int(xo_ + (width_ - w) / 2);
627         int const text_end = text_start + w;
628
629         pain_.rectText(text_start, y + d, label, pb_font);
630
631         pain_.line(int(xo_), y, text_start, y,
632                 LColor::pagebreak, Painter::line_onoffdash);
633         pain_.line(text_end, y, int(xo_ + width_), y,
634                 LColor::pagebreak, Painter::line_onoffdash);
635
636         return 3 * defaultRowHeight();
637 }
638
639
640 int RowPainter::paintAppendixStart(int y)
641 {
642         LyXFont pb_font;
643         pb_font.setColor(LColor::appendix).decSize();
644
645         string const label = _("Appendix");
646         int w = 0;
647         int a = 0;
648         int d = 0;
649         font_metrics::rectText(label, pb_font, w, a, d);
650
651         int const text_start = int(xo_ + (width_ - w) / 2);
652         int const text_end = text_start + w;
653
654         pain_.rectText(text_start, y + d, label, pb_font);
655
656         pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
657         pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
658
659         return 3 * defaultRowHeight();
660 }
661
662
663 void RowPainter::paintFirst()
664 {
665         ParagraphParameters const & parparams = pit_->params();
666
667         int y_top = 0;
668
669         // start of appendix?
670         if (parparams.startOfAppendix())
671                 y_top += paintAppendixStart(yo_ + y_top + 2 * defaultRowHeight());
672
673         // the top margin
674         if (row_ == text_.rows().begin() && !text_.isInInset())
675                 y_top += PAPER_MARGIN;
676
677         // draw a top pagebreak
678         if (parparams.pagebreakTop())
679                 y_top += paintPageBreak(_("Page Break (top)"),
680                         yo_ + y_top + 2 * defaultRowHeight());
681
682         // draw the additional space if needed:
683         y_top += paintLengthMarker(_("Space above"), parparams.spaceTop(),
684                         yo_ + y_top);
685
686         Buffer const * buffer = bv_.buffer();
687
688         LyXLayout_ptr const & layout = pit_->layout();
689
690         if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
691                 if (pit_ != text_.ownerParagraphs().begin()) {
692                         if (layout->latextype == LATEX_PARAGRAPH
693                                 && !pit_->getDepth()) {
694                                 y_top += buffer->params.getDefSkip().inPixels(bv_);
695                         } else {
696                                 LyXLayout_ptr const & playout =
697                                         boost::prior(pit_)->layout();
698                                 if (playout->latextype == LATEX_PARAGRAPH
699                                         && !boost::prior(pit_)->getDepth()) {
700                                         // is it right to use defskip here, too? (AS)
701                                         y_top += buffer->params.getDefSkip().inPixels(bv_);
702                                 }
703                         }
704                 }
705         }
706
707         int const ww = bv_.workWidth();
708
709         // draw a top line
710         if (parparams.lineTop()) {
711                 int const asc = font_metrics::ascent('x', getFont(0));
712
713                 y_top += asc;
714
715                 int const w = (text_.isInInset() ? text_.inset_owner->width() : ww);
716                 int const xp = static_cast<int>(text_.isInInset() ? xo_ : 0);
717                 pain_.line(xp, yo_ + y_top, xp + w, yo_ + y_top,
718                         LColor::topline, Painter::line_solid,
719                         Painter::line_thick);
720
721                 y_top += asc;
722         }
723
724         bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params);
725
726         // should we print a label?
727         if (layout->labeltype >= LABEL_STATIC
728             && (layout->labeltype != LABEL_STATIC
729                 || layout->latextype != LATEX_ENVIRONMENT
730                 || isFirstInSequence(pit_, text_.ownerParagraphs()))) {
731
732                 LyXFont font = getLabelFont();
733                 if (!pit_->getLabelstring().empty()) {
734                         double x = x_;
735                         string const str = pit_->getLabelstring();
736
737                         // this is special code for the chapter layout. This is
738                         // printed in an extra row and has a pagebreak at
739                         // the top.
740                         if (layout->labeltype == LABEL_COUNTER_CHAPTER) {
741                                 if (buffer->params.secnumdepth >= 0) {
742                                         float spacing_val = 1.0;
743                                         if (!parparams.spacing().isDefault()) {
744                                                 spacing_val = parparams.spacing().getValue();
745                                         } else {
746                                                 spacing_val = buffer->params.spacing.getValue();
747                                         }
748
749                                         int const maxdesc =
750                                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
751                                                 + int(layout->parsep) * defaultRowHeight();
752
753                                         if (is_rtl) {
754                                                 x = ww - leftMargin() -
755                                                         font_metrics::width(str, font);
756                                         }
757
758                                         pain_.text(int(x),
759                                                 yo_ + row_->baseline() -
760                                                 row_->ascent_of_text() - maxdesc,
761                                                 str, font);
762                                 }
763                         } else {
764                                 if (is_rtl) {
765                                         x = ww - leftMargin()
766                                                 + font_metrics::width(layout->labelsep, font);
767                                 } else {
768                                         x = x_ - font_metrics::width(layout->labelsep, font)
769                                                 - font_metrics::width(str, font);
770                                 }
771
772                                 pain_.text(int(x), yo_ + row_->baseline(), str, font);
773                         }
774                 }
775
776         // the labels at the top of an environment.
777         // More or less for bibliography
778         } else if (isFirstInSequence(pit_, text_.ownerParagraphs()) &&
779                 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
780                 layout->labeltype == LABEL_BIBLIO ||
781                 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
782                 LyXFont font = getLabelFont();
783                 if (!pit_->getLabelstring().empty()) {
784                         string const str = pit_->getLabelstring();
785                         float spacing_val = 1.0;
786                         if (!parparams.spacing().isDefault()) {
787                                 spacing_val = parparams.spacing().getValue();
788                         } else {
789                                 spacing_val = buffer->params.spacing.getValue();
790                         }
791
792                         int maxdesc =
793                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
794                                 + (layout->labelbottomsep * defaultRowHeight()));
795
796                         double x = x_;
797                         if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
798                                 x = ((is_rtl ? leftMargin() : x_)
799                                          + ww - text_.rightMargin(*bv_.buffer(), *row_)) / 2;
800                                 x -= font_metrics::width(str, font) / 2;
801                         } else if (is_rtl) {
802                                 x = ww - leftMargin() -
803                                         font_metrics::width(str, font);
804                         }
805                         pain_.text(int(x),
806                             yo_ + row_->baseline() - row_->ascent_of_text() - maxdesc,
807                                   str, font);
808                 }
809         }
810 }
811
812
813 void RowPainter::paintLast()
814 {
815         ParagraphParameters const & parparams = pit_->params();
816         int y_bottom = row_->height() - 1;
817
818         // the bottom margin
819         if (boost::next(row_) == text_.rows().end() && !text_.isInInset())
820                 y_bottom -= PAPER_MARGIN;
821
822         int const ww = bv_.workWidth();
823
824         // draw a bottom pagebreak
825         if (parparams.pagebreakBottom()) {
826                 y_bottom -= paintPageBreak(_("Page Break (bottom)"),
827                         yo_ + y_bottom - 2 * defaultRowHeight());
828         }
829
830         // draw the additional space if needed:
831         int const height = getLengthMarkerHeight(bv_, parparams.spaceBottom());
832         y_bottom -= paintLengthMarker(_("Space below"), parparams.spaceBottom(),
833                              yo_ + y_bottom - height);
834
835         // draw a bottom line
836         if (parparams.lineBottom()) {
837                 int const asc = font_metrics::ascent('x',
838                         getFont(max(pos_type(0), pit_->size() - 1)));
839
840                 y_bottom -= asc;
841
842                 int const w = text_.isInInset() ? text_.inset_owner->width() : ww;
843                 int const xp = int(text_.isInInset() ? xo_ : 0);
844                 int const y = yo_ + y_bottom;
845                 pain_.line(xp, y, xp + w, y, LColor::topline, Painter::line_solid,
846                           Painter::line_thick);
847
848                 y_bottom -= asc;
849         }
850
851         bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params);
852         int const endlabel = getEndLabel(pit_, text_.ownerParagraphs());
853
854         // draw an endlabel
855         switch (endlabel) {
856         case END_LABEL_BOX:
857         case END_LABEL_FILLED_BOX:
858         {
859                 LyXFont const font = getLabelFont();
860                 int const size = int(0.75 * font_metrics::maxAscent(font));
861                 int const y = (yo_ + row_->baseline()) - size;
862                 int x = is_rtl ? LEFT_MARGIN : ww - PAPER_MARGIN - size;
863
864                 if (row_->fill() <= size)
865                         x += (size - row_->fill() + 1) * (is_rtl ? -1 : 1);
866
867                 if (endlabel == END_LABEL_BOX)
868                         pain_.rectangle(x, y, size, size, LColor::eolmarker);
869                 else
870                         pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
871                 break;
872         }
873         case END_LABEL_STATIC:
874         {
875                 LyXFont font = getLabelFont();
876                 string const & str = pit_->layout()->endlabelstring();
877                 double const x = is_rtl ?
878                         x_ - font_metrics::width(str, font)
879                         : ww - text_.rightMargin(*bv_.buffer(), *row_) - row_->fill();
880                 pain_.text(int(x), yo_ + row_->baseline(), str, font);
881                 break;
882         }
883         case END_LABEL_NO_LABEL:
884                 break;
885         }
886 }
887
888
889 void RowPainter::paintText()
890 {
891         pos_type const last = lastPrintablePos(text_, row_);
892         pos_type body_pos = pit_->beginningOfBody();
893         if (body_pos > 0 &&
894                 (body_pos - 1 > last || !pit_->isLineSeparator(body_pos - 1))) {
895                 body_pos = 0;
896         }
897
898         LyXLayout_ptr const & layout = pit_->layout();
899
900         bool running_strikeout = false;
901         bool is_struckout = false;
902         int last_strikeout_x = 0;
903
904         pos_type vpos = row_->pos();
905         while (vpos <= last) {
906                 if (x_ > bv_.workWidth())
907                         break;
908                 pos_type pos = text_.vis2log(vpos);
909
910                 if (pos >= pit_->size()) {
911                         ++vpos;
912                         continue;
913                 }
914
915                 if (x_ + singleWidth(pos) < 0) {
916                         x_ += singleWidth(pos);
917                         ++vpos;
918                         continue;
919                 }
920
921                 is_struckout = isDeletedText(*pit_, pos);
922
923                 if (is_struckout && !running_strikeout) {
924                         running_strikeout = true;
925                         last_strikeout_x = int(x_);
926                 }
927
928                 bool const highly_editable_inset = pit_->isInset(pos)
929                         && isHighlyEditableInset(pit_->getInset(pos));
930
931                 // if we reach the end of a struck out range, paint it
932                 // we also don't paint across things like tables
933                 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
934                         int const middle = yo_ + row_->top_of_text()
935                                 + (row_->baseline() - row_->top_of_text()) / 2;
936                         pain_.line(last_strikeout_x, middle, int(x_), middle,
937                                 LColor::strikeout, Painter::line_solid, Painter::line_thin);
938                         running_strikeout = false;
939                 }
940
941                 if (body_pos > 0 && pos == body_pos - 1) {
942                         int const lwidth = font_metrics::width(layout->labelsep,
943                                 getLabelFont());
944
945                         x_ += label_hfill_ + lwidth - singleWidth(body_pos - 1);
946                 }
947
948                 if (pit_->isHfill(pos)) {
949                         x_ += 1;
950
951                         int const y0 = yo_ + row_->baseline();
952                         int const y1 = y0 - defaultRowHeight() / 2;
953
954                         pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
955
956                         if (hfillExpansion(text_, row_, pos)) {
957                                 int const y2 = (y0 + y1) / 2;
958
959                                 if (pos >= body_pos) {
960                                         pain_.line(int(x_), y2, int(x_ + hfill_), y2,
961                                                   LColor::added_space,
962                                                   Painter::line_onoffdash);
963                                         x_ += hfill_;
964                                 } else {
965                                         pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
966                                                   LColor::added_space,
967                                                   Painter::line_onoffdash);
968                                         x_ += label_hfill_;
969                                 }
970                                 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
971                         }
972                         x_ += 2;
973                         ++vpos;
974                 } else if (pit_->isSeparator(pos)) {
975                         x_ += singleWidth(pos);
976                         if (pos >= body_pos)
977                                 x_ += separator_;
978                         ++vpos;
979                 } else {
980                         paintFromPos(vpos);
981                 }
982         }
983
984         // if we reach the end of a struck out range, paint it
985         if (running_strikeout) {
986                 int const middle = yo_ + row_->top_of_text()
987                         + ((row_->baseline() - row_->top_of_text()) / 2);
988                 pain_.line(last_strikeout_x, middle, int(x_), middle,
989                         LColor::strikeout, Painter::line_solid, Painter::line_thin);
990                 running_strikeout = false;
991         }
992 }
993
994
995 void RowPainter::paint()
996 {
997         width_ = text_.workWidth();
998
999         // FIXME: must be a cleaner way here. Aren't these calculations
1000         // belonging to row metrics ?
1001         text_.prepareToPrint(row_, x_, separator_, hfill_, label_hfill_);
1002
1003         // FIXME: what is this fixing ?
1004         if (text_.isInInset() && x_ < 0)
1005                 x_ = 0;
1006         x_ += xo_;
1007
1008         // If we're *not* at the top-level of rows, then the
1009         // background has already been cleared.
1010         if (&text_ == bv_.text)
1011                 paintBackground();
1012
1013         // paint the selection background
1014         if (text_.selection.set())
1015                 paintSelection();
1016
1017         // vertical lines for appendix
1018         paintAppendix();
1019
1020         // environment depth brackets
1021         paintDepthBar();
1022
1023         // changebar
1024         paintChangeBar();
1025
1026         if (row_->isParStart())
1027                 paintFirst();
1028
1029         if (isParEnd(text_, row_))
1030                 paintLast();
1031
1032         // paint text
1033         paintText();
1034 }
1035
1036
1037 } // namespace anon
1038
1039
1040 int getLengthMarkerHeight(BufferView const & bv, VSpace const & vsp)
1041 {
1042         if (vsp.kind() == VSpace::NONE)
1043                 return 0;
1044
1045         int const arrow_size = 4;
1046         int const space_size = vsp.inPixels(bv);
1047
1048         LyXFont font;
1049         font.decSize();
1050         int const min_size = max(3 * arrow_size,
1051                 font_metrics::maxAscent(font)
1052                 + font_metrics::maxDescent(font));
1053
1054         if (vsp.length().len().value() < 0.0)
1055                 return min_size;
1056         else
1057                 return max(min_size, space_size);
1058 }
1059
1060
1061 void paintRowsHelper(BufferView const & bv, LyXText const & text,
1062         RowList::iterator rit, int y_offset, int x_offset, int y)
1063 {
1064         // fix up missing metrics() call for main LyXText
1065         // calling metrics() directly is (a) slow and (b) crashs
1066         if (&text == bv.text) {
1067 #if 1
1068                 // make sure all insets are updated
1069                 ParagraphList::iterator pit = text.ownerParagraphs().begin();
1070                 ParagraphList::iterator end = text.ownerParagraphs().end();
1071
1072                 // compute inset metrics
1073                 for (; pit != end; ++pit) {
1074                         InsetList & insetList = pit->insetlist;
1075                         InsetList::iterator ii = insetList.begin();
1076                         InsetList::iterator iend = insetList.end();
1077                         for (; ii != iend; ++ii) {
1078                                 Dimension dim;
1079                                 LyXFont font;
1080                                 MetricsInfo mi(perv(bv), font, text.workWidth());
1081                                 ii->inset->metrics(mi, dim);
1082                         }
1083                 }
1084 #else
1085                 LyXFont font;
1086                 Dimension dim;
1087                 MetricsInfo mi(perv(bv), font, text.workWidth());
1088                 const_cast<LyXText&>(text).metrics(mi, dim);
1089 #endif
1090         }
1091
1092         RowPainter painter(bv, text, rit, y_offset, x_offset, y);
1093         painter.paint();
1094 }
1095
1096
1097 int paintRows(BufferView const & bv, LyXText const & text,
1098         RowList::iterator rit, int xo, int y, int yf, int y2, int yo)
1099 {
1100         RowList::iterator end = text.rows().end();
1101         while (rit != end && yf < y2) {
1102                 paintRowsHelper(bv, text, rit, y + yo, xo, y + text.top_y());
1103                 y += rit->height();
1104                 yf += rit->height();
1105                 ++rit;
1106         }
1107         return y;
1108 }
1109