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