]> git.lyx.org Git - lyx.git/blob - src/rowpainter.C
The inset newline patch I sent plus the requisite fixes. I couldn't reproduce a problem.
[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                 && par_.isInset(row_.pos())) {
316                 inset = par_.getInset(row_.pos());
317                 clear_area = inset->doClearArea();
318         }
319
320         if (cleared_) {
321                 return true;
322         }
323
324         if (clear_area) {
325                 int const x = xo_;
326                 int const y = yo_ < 0 ? 0 : yo_;
327                 int const h = yo_ < 0 ? row_.height() + yo_ : row_.height();
328                 pain_.fillRectangle(x, y, width_, h, text_.backgroundColor());
329                 return true;
330         }
331
332         if (!inset)
333                 return false;
334
335         LyXFont font(LyXFont::ALL_SANE);
336
337         // FIXME
338         BufferView * bv = perv(bv_);
339
340         int h = row_.baseline() - inset->ascent(bv, font);
341
342         // first clear the whole row above the inset!
343         if (h > 0) {
344                 pain_.fillRectangle(xo_, yo_, width_, h, text_.backgroundColor());
345         }
346
347         // clear the space below the inset!
348         h += inset->ascent(bv, font) + inset->descent(bv, font);
349         if ((row_.height() - h) > 0) {
350                 pain_.fillRectangle(xo_, yo_ + h,
351                         width_, row_.height() - h, text_.backgroundColor());
352         }
353
354         // clear the space behind the inset, if needed
355         if (!inset->display() && !inset->needFullRow()) {
356                 int const xp = int(x_) + inset->width(bv, font);
357                 if (width_ - xp > 0) {
358                         pain_.fillRectangle(xp, yo_, width_ - xp,
359                                 row_.height(), text_.backgroundColor());
360                 }
361         }
362
363         return false;
364 }
365
366
367 void RowPainter::paintSelection()
368 {
369         bool const is_rtl = par_.isRightToLeftPar(bv_.buffer()->params);
370
371         // the current selection
372         int const startx = text_.selection.start.x();
373         int const endx = text_.selection.end.x();
374         int const starty = text_.selection.start.y();
375         int const endy = text_.selection.end.y();
376         Row const * startrow = text_.selection.start.row();
377         Row const * endrow = text_.selection.end.row();
378
379         // Bleh.
380         Row const * row = &row_;
381
382         if (text_.bidi_same_direction) {
383                 int x;
384                 int y = yo_;
385                 int w;
386                 int h = row_.height();
387
388                 if (startrow == row && endrow == row) {
389                         if (startx < endx) {
390                                 x = xo_ + startx;
391                                 w = endx - startx;
392                                 pain_.fillRectangle(x, y, w, h, LColor::selection);
393                         } else {
394                                 x = xo_ + endx;
395                                 w = startx - endx;
396                                 pain_.fillRectangle(x, y, w, h, LColor::selection);
397                         }
398                 } else if (startrow == row) {
399                         int const x = (is_rtl) ? xo_ : (xo_ + startx);
400                         int const w = (is_rtl) ? startx : (width_ - startx);
401                         pain_.fillRectangle(x, y, w, h, LColor::selection);
402                 } else if (endrow == row) {
403                         int const x = (is_rtl) ? (xo_ + endx) : xo_;
404                         int const w = (is_rtl) ? (width_ - endx) : endx;
405                         pain_.fillRectangle(x, y, w, h, LColor::selection);
406                 } else if (y_ > starty && y_ < endy) {
407                         pain_.fillRectangle(xo_, y, width_, h, LColor::selection);
408                 }
409                 return;
410         } else if (startrow != row && endrow != row) {
411                 if (y_ > starty && y_ < endy) {
412                         int w = width_;
413                         int h = row_.height();
414                         pain_.fillRectangle(xo_, yo_, w, h, LColor::selection);
415                 }
416                 return;
417         }
418
419         if ((startrow != row && !is_rtl) || (endrow != row && is_rtl))
420                 pain_.fillRectangle(xo_, yo_, int(x_), row_.height(), LColor::selection);
421
422         pos_type const body_pos = par_.beginningOfBody();
423         pos_type const last = row_.lastPrintablePos();
424         float tmpx = x_;
425
426         for (pos_type vpos = row_.pos(); vpos <= last; ++vpos)  {
427                 pos_type pos = text_.vis2log(vpos);
428                 float const old_tmpx = tmpx;
429                 if (body_pos > 0 && pos == body_pos - 1) {
430                         LyXLayout_ptr const & layout = par_.layout();
431                         LyXFont const lfont = getLabelFont();
432
433                         tmpx += label_hfill_ + font_metrics::width(layout->labelsep, lfont);
434
435                         if (par_.isLineSeparator(body_pos - 1))
436                                 tmpx -= singleWidth(body_pos - 1);
437                 }
438
439                 if (row_.hfillExpansion(pos)) {
440                         tmpx += singleWidth(pos);
441                         if (pos >= body_pos)
442                                 tmpx += hfill_;
443                         else
444                                 tmpx += label_hfill_;
445                 }
446
447                 else if (par_.isSeparator(pos)) {
448                         tmpx += singleWidth(pos);
449                         if (pos >= body_pos)
450                                 tmpx += separator_;
451                 } else {
452                         tmpx += singleWidth(pos);
453                 }
454
455                 if ((startrow != row || text_.selection.start.pos() <= pos) &&
456                         (endrow != row || pos < text_.selection.end.pos())) {
457                         // Here we do not use x_ as xo_ was added to x_.
458                         pain_.fillRectangle(int(old_tmpx), yo_,
459                                 int(tmpx - old_tmpx + 1),
460                                 row_.height(), LColor::selection);
461                 }
462         }
463
464         if ((startrow != row && is_rtl) || (endrow != row && !is_rtl)) {
465                 pain_.fillRectangle(xo_ + int(tmpx),
466                                       yo_, int(bv_.workWidth() - tmpx),
467                                       row_.height(), LColor::selection);
468         }
469 }
470
471
472 void RowPainter::paintChangeBar()
473 {
474         pos_type const start = row_.pos();
475         pos_type const end = row_.lastPrintablePos();
476
477         if (!par_.isChanged(start, end))
478                 return;
479
480         int const height = (row_.next()
481                 ? row_.height() + row_.next()->top_of_text()
482                 : row_.baseline());
483
484         pain_.fillRectangle(4, yo_, 5, height, LColor::changebar);
485 }
486
487
488 void RowPainter::paintAppendix()
489 {
490         // FIXME: can be just width_ ?
491         int const ww = bv_.workWidth();
492
493         if (par_.params().appendix()) {
494                 pain_.line(1, yo_, 1, yo_ + row_.height(), LColor::appendixline);
495                 pain_.line(ww - 2, yo_, ww - 2, yo_ + row_.height(), LColor::appendixline);
496         }
497 }
498
499
500 void RowPainter::paintDepthBar()
501 {
502         Paragraph::depth_type const depth = par_.getDepth();
503
504         if (depth <= 0)
505                 return;
506
507         Paragraph::depth_type prev_depth = 0;
508         if (row_.previous())
509                 prev_depth = row_.previous()->par()->getDepth();
510         Paragraph::depth_type next_depth = 0;
511         if (row_.next())
512                 next_depth = row_.next()->par()->getDepth();
513
514         for (Paragraph::depth_type i = 1; i <= depth; ++i) {
515                 int x = (PAPER_MARGIN / 5) * i + xo_;
516                 // only consider the changebar space if we're drawing outer left
517                 if (!xo_)
518                         x += CHANGEBAR_MARGIN;
519                 int const h = yo_ + row_.height() - 1 - (i - next_depth - 1) * 3;
520
521                 pain_.line(x, yo_, x, h, LColor::depthbar);
522
523                 int const w = PAPER_MARGIN / 5;
524
525                 if (i > prev_depth) {
526                         pain_.fillRectangle(x, yo_, w, 2, LColor::depthbar);
527                 }
528                 if (i > next_depth) {
529                         pain_.fillRectangle(x, h, w, 2, LColor::depthbar);
530                 }
531         }
532 }
533
534
535 int getLengthMarkerHeight(BufferView const & bv, VSpace const & vsp)
536 {
537         if (vsp.kind() == VSpace::NONE)
538                 return 0;
539
540         int const arrow_size = 4;
541         int const space_size = int(vsp.inPixels(bv));
542
543         LyXFont font;
544         font.decSize();
545         int const min_size = max(3 * arrow_size,
546                 font_metrics::maxAscent(font)
547                 + font_metrics::maxDescent(font));
548
549         if (vsp.length().len().value() < 0.0)
550                 return min_size;
551         else
552                 return max(min_size, space_size);
553 }
554
555
556 int RowPainter::paintLengthMarker(string const & prefix, VSpace const & vsp, int start)
557 {
558         if (vsp.kind() == VSpace::NONE)
559                 return 0;
560
561         int const arrow_size = 4;
562         int const size = getLengthMarkerHeight(bv_, vsp);
563         int const end = start + size;
564
565         // the label to display (if any)
566         string str;
567         // y-values for top arrow
568         int ty1, ty2;
569         // y-values for bottom arrow
570         int by1, by2;
571
572         str = prefix + " (" + vsp.asLyXCommand() + ")";
573
574         if (vsp.kind() == VSpace::VFILL) {
575                 ty1 = ty2 = start;
576                 by1 = by2 = end;
577         } else {
578                 // adding or removing space
579                 bool const added = vsp.kind() != VSpace::LENGTH ||
580                                    vsp.length().len().value() > 0.0;
581                 ty1 = added ? (start + arrow_size) : start;
582                 ty2 = added ? start : (start + arrow_size);
583                 by1 = added ? (end - arrow_size) : end;
584                 by2 = added ? end : (end - arrow_size);
585         }
586
587         int const leftx = xo_ + leftMargin();
588         int const midx = leftx + arrow_size;
589         int const rightx = midx + arrow_size;
590
591         // first the string
592         int w = 0;
593         int a = 0;
594         int d = 0;
595
596         LyXFont font;
597         font.setColor(LColor::added_space).decSize().decSize();
598         font_metrics::rectText(str, font, w, a, d);
599
600         pain_.rectText(leftx + 2 * arrow_size + 5,
601                          start + ((end - start) / 2) + d,
602                          str, font);
603
604         // top arrow
605         pain_.line(leftx, ty1, midx, ty2, LColor::added_space);
606         pain_.line(midx, ty2, rightx, ty1, LColor::added_space);
607
608         // bottom arrow
609         pain_.line(leftx, by1, midx, by2, LColor::added_space);
610         pain_.line(midx, by2, rightx, by1, LColor::added_space);
611
612         // joining line
613         pain_.line(midx, ty2, midx, by2, LColor::added_space);
614
615         return size;
616 }
617
618
619 int RowPainter::paintPageBreak(string const & label, int y)
620 {
621         LyXFont pb_font;
622         pb_font.setColor(LColor::pagebreak).decSize();
623
624         int w = 0;
625         int a = 0;
626         int d = 0;
627         font_metrics::rectText(label, pb_font, w, a, d);
628
629         int const text_start = xo_ + ((width_ - w) / 2);
630         int const text_end = text_start + w;
631
632         pain_.rectText(text_start, y + d, label, pb_font);
633
634         pain_.line(xo_, y, text_start, y,
635                 LColor::pagebreak, Painter::line_onoffdash);
636         pain_.line(text_end, y, xo_ + width_, y,
637                 LColor::pagebreak, Painter::line_onoffdash);
638
639         return 3 * defaultRowHeight();
640 }
641
642
643 void RowPainter::paintFirst()
644 {
645         ParagraphParameters const & parparams = par_.params();
646
647         // start of appendix?
648         if (parparams.startOfAppendix()) {
649                 pain_.line(1, yo_, width_ - 2, yo_, LColor::appendixline);
650         }
651
652         int y_top = 0;
653
654         // the top margin
655         if (!row_.previous() && text_.isTopLevel())
656                 y_top += PAPER_MARGIN;
657
658         // draw a top pagebreak
659         if (parparams.pagebreakTop()) {
660                 y_top += paintPageBreak(_("Page Break (top)"),
661                         yo_ + y_top + 2 * defaultRowHeight());
662         }
663
664         // draw the additional space if needed:
665         y_top += paintLengthMarker(_("Space above"), parparams.spaceTop(),
666                         yo_ + y_top);
667
668         Buffer const * buffer = bv_.buffer();
669
670         LyXLayout_ptr const & layout = par_.layout();
671
672         if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
673                 if (par_.previous()) {
674                         if (layout->latextype == LATEX_PARAGRAPH
675                                 && !par_.getDepth()) {
676                                 y_top += buffer->params.getDefSkip().inPixels(bv_);
677                         } else {
678                                 LyXLayout_ptr const & playout =
679                                         par_.previous()->layout();
680                                 if (playout->latextype == LATEX_PARAGRAPH
681                                         && !par_.previous()->getDepth()) {
682                                         // is it right to use defskip here, too? (AS)
683                                         y_top += buffer->params.getDefSkip().inPixels(bv_);
684                                 }
685                         }
686                 }
687         }
688
689         int const ww = bv_.workWidth();
690
691         // draw a top line
692         if (parparams.lineTop()) {
693                 LyXFont font(LyXFont::ALL_SANE);
694                 int const asc = font_metrics::ascent('x', getFont(0));
695
696                 y_top += asc;
697
698                 int const w = (text_.isInInset() ? text_.inset_owner->width(perv(bv_), font) : ww);
699                 int const xp = static_cast<int>(text_.isInInset() ? xo_ : 0);
700                 pain_.line(xp, yo_ + y_top, xp + w, yo_ + y_top,
701                         LColor::topline, Painter::line_solid,
702                         Painter::line_thick);
703
704                 y_top += asc;
705         }
706
707         bool const is_rtl = par_.isRightToLeftPar(bv_.buffer()->params);
708
709         // should we print a label?
710         if (layout->labeltype >= LABEL_STATIC
711             && (layout->labeltype != LABEL_STATIC
712                 || layout->latextype != LATEX_ENVIRONMENT
713                 || par_.isFirstInSequence())) {
714
715                 LyXFont font = getLabelFont();
716                 if (!par_.getLabelstring().empty()) {
717                         float x = x_;
718                         string const str = par_.getLabelstring();
719
720                         // this is special code for the chapter layout. This is
721                         // printed in an extra row and has a pagebreak at
722                         // the top.
723                         if (layout->labeltype == LABEL_COUNTER_CHAPTER) {
724                                 if (buffer->params.secnumdepth >= 0) {
725                                         float spacing_val = 1.0;
726                                         if (!parparams.spacing().isDefault()) {
727                                                 spacing_val = parparams.spacing().getValue();
728                                         } else {
729                                                 spacing_val = buffer->params.spacing.getValue();
730                                         }
731
732                                         int const maxdesc =
733                                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
734                                                 + int(layout->parsep) * defaultRowHeight();
735
736                                         if (is_rtl) {
737                                                 x = ww - leftMargin() -
738                                                         font_metrics::width(str, font);
739                                         }
740
741                                         pain_.text(int(x),
742                                                 yo_ + row_.baseline() -
743                                                 row_.ascent_of_text() - maxdesc,
744                                                 str, font);
745                                 }
746                         } else {
747                                 if (is_rtl) {
748                                         x = ww - leftMargin()
749                                                 + font_metrics::width(layout->labelsep, font);
750                                 } else {
751                                         x = x_ - font_metrics::width(layout->labelsep, font)
752                                                 - font_metrics::width(str, font);
753                                 }
754
755                                 pain_.text(int(x), yo_ + row_.baseline(), str, font);
756                         }
757                 }
758
759         // the labels at the top of an environment.
760         // More or less for bibliography
761         } else if (par_.isFirstInSequence() &&
762                 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
763                 layout->labeltype == LABEL_BIBLIO ||
764                 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
765                 LyXFont font = getLabelFont();
766                 if (!par_.getLabelstring().empty()) {
767                         string const str = par_.getLabelstring();
768                         float spacing_val = 1.0;
769                         if (!parparams.spacing().isDefault()) {
770                                 spacing_val = parparams.spacing().getValue();
771                         } else {
772                                 spacing_val = buffer->params.spacing.getValue();
773                         }
774
775                         int maxdesc =
776                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
777                                 + (layout->labelbottomsep * defaultRowHeight()));
778
779                         float x = x_;
780                         if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
781                                 x = ((is_rtl ? leftMargin() : x_)
782                                          + ww - text_.rightMargin(*bv_.buffer(), row_)) / 2;
783                                 x -= font_metrics::width(str, font) / 2;
784                         } else if (is_rtl) {
785                                 x = ww - leftMargin() -
786                                         font_metrics::width(str, font);
787                         }
788                         pain_.text(int(x), yo_ + row_.baseline()
789                                   - row_.ascent_of_text() - maxdesc,
790                                   str, font);
791                 }
792         }
793 }
794
795
796 void RowPainter::paintLast()
797 {
798         ParagraphParameters const & parparams = par_.params();
799         int y_bottom = row_.height() - 1;
800
801         // the bottom margin
802         if (!row_.next() && text_.isTopLevel())
803                 y_bottom -= PAPER_MARGIN;
804
805         int const ww = bv_.workWidth();
806
807         // draw a bottom pagebreak
808         if (parparams.pagebreakBottom()) {
809                 y_bottom -= paintPageBreak(_("Page Break (bottom)"),
810                         yo_ + y_bottom - 2 * defaultRowHeight());
811         }
812
813         // draw the additional space if needed:
814         int const height = getLengthMarkerHeight(bv_, parparams.spaceBottom());
815         y_bottom -= paintLengthMarker(_("Space below"), parparams.spaceBottom(),
816                              yo_ + y_bottom - height);
817
818         // draw a bottom line
819         if (parparams.lineBottom()) {
820                 LyXFont font(LyXFont::ALL_SANE);
821                 int const asc = font_metrics::ascent('x',
822                         getFont(max(pos_type(0), par_.size() - 1)));
823
824                 y_bottom -= asc;
825
826                 int const w = (text_.isInInset() ? text_.inset_owner->width(perv(bv_), font) : ww);
827                 int const xp = static_cast<int>(text_.isInInset() ? xo_ : 0);
828                 int const y = yo_ + y_bottom;
829                 pain_.line(xp, y, xp + w, y, LColor::topline, Painter::line_solid,
830                           Painter::line_thick);
831
832                 y_bottom -= asc;
833         }
834
835         bool const is_rtl = par_.isRightToLeftPar(bv_.buffer()->params);
836         int const endlabel = par_.getEndLabel();
837
838         // draw an endlabel
839         switch (endlabel) {
840         case END_LABEL_BOX:
841         case END_LABEL_FILLED_BOX:
842         {
843                 LyXFont const font = getLabelFont();
844                 int const size = int(0.75 * font_metrics::maxAscent(font));
845                 int const y = (yo_ + row_.baseline()) - size;
846                 int x = is_rtl ? LEFT_MARGIN : ww - PAPER_MARGIN - size;
847
848                 if (row_.fill() <= size)
849                         x += (size - row_.fill() + 1) * (is_rtl ? -1 : 1);
850
851                 if (endlabel == END_LABEL_BOX) {
852                         pain_.rectangle(x, y, size, size, LColor::eolmarker);
853                 } else {
854                         pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
855                 }
856                 break;
857         }
858         case END_LABEL_STATIC:
859         {
860 #if 0
861                 LyXFont font(LyXFont::ALL_SANE);
862                 font = getLabelFont();
863 #else
864                 LyXFont font = getLabelFont();
865 #endif
866                 string const & str = par_.layout()->endlabelstring();
867                 int const x = is_rtl ?
868                         int(x_) - font_metrics::width(str, font)
869                         : ww - text_.rightMargin(*bv_.buffer(), row_) - row_.fill();
870                 pain_.text(x, yo_ + row_.baseline(), str, font);
871                 break;
872         }
873         case END_LABEL_NO_LABEL:
874                 break;
875         }
876 }
877
878
879 bool RowPainter::paintText()
880 {
881         pos_type const last = row_.lastPrintablePos();
882         pos_type body_pos = par_.beginningOfBody();
883         if (body_pos > 0 &&
884                 (body_pos - 1 > last ||
885                 !par_.isLineSeparator(body_pos - 1))) {
886                 body_pos = 0;
887         }
888
889         LyXLayout_ptr const & layout = par_.layout();
890
891         bool running_strikeout = false;
892         bool is_struckout = false;
893         float last_strikeout_x = 0.0;
894
895         pos_type vpos = row_.pos();
896         while (vpos <= last) {
897                 if (x_ > bv_.workWidth())
898                         break;
899                 pos_type pos = text_.vis2log(vpos);
900
901                 if (x_ + singleWidth(pos) < 0) {
902                         x_ += singleWidth(pos);
903                         ++vpos;
904                         continue;
905                 }
906
907                 is_struckout = isDeletedText(par_, pos);
908
909                 if (is_struckout && !running_strikeout) {
910                         running_strikeout = true;
911                         last_strikeout_x = x_;
912                 }
913
914                 bool const highly_editable_inset = par_.isInset(pos)
915                         && isHighlyEditableInset(par_.getInset(pos));
916
917                 // if we reach the end of a struck out range, paint it
918                 // we also don't paint across things like tables
919                 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
920                         int const middle = yo_ + row_.top_of_text()
921                                 + ((row_.baseline() - row_.top_of_text()) / 2);
922                         pain_.line(int(last_strikeout_x), middle, int(x_), middle,
923                                 LColor::strikeout, Painter::line_solid, Painter::line_thin);
924                         running_strikeout = false;
925                 }
926
927                 if (body_pos > 0 && pos == body_pos - 1) {
928                         int const lwidth = font_metrics::width(layout->labelsep,
929                                 getLabelFont());
930
931                         x_ += label_hfill_ + lwidth
932                                 - singleWidth(body_pos - 1);
933                 }
934
935                 if (par_.isHfill(pos)) {
936                         x_ += 1;
937
938                         int const y0 = yo_ + row_.baseline();
939                         int const y1 = y0 - defaultRowHeight() / 2;
940
941                         pain_.line(int(x_), y1, int(x_), y0,
942                                      LColor::added_space);
943
944                         if (row_.hfillExpansion(pos)) {
945                                 int const y2 = (y0 + y1) / 2;
946
947                                 if (pos >= body_pos) {
948                                         pain_.line(int(x_), y2,
949                                                   int(x_ + hfill_), y2,
950                                                   LColor::added_space,
951                                                   Painter::line_onoffdash);
952                                         x_ += hfill_;
953                                 } else {
954                                         pain_.line(int(x_), y2,
955                                                   int(x_ + label_hfill_), y2,
956                                                   LColor::added_space,
957                                                   Painter::line_onoffdash);
958                                         x_ += label_hfill_;
959                                 }
960                                 pain_.line(int(x_), y1,
961                                              int(x_), y0,
962                                              LColor::added_space);
963                         }
964                         x_ += 2;
965                         ++vpos;
966                 } else if (par_.isSeparator(pos)) {
967                         x_ += singleWidth(pos);
968                         if (pos >= body_pos)
969                                 x_ += separator_;
970                         ++vpos;
971                 } else {
972                         if (paintFromPos(vpos))
973                                 return true;
974                 }
975         }
976
977         // if we reach the end of a struck out range, paint it
978         if (running_strikeout) {
979                 int const middle = yo_ + row_.top_of_text()
980                         + ((row_.baseline() - row_.top_of_text()) / 2);
981                 pain_.line(int(last_strikeout_x), middle, int(x_), middle,
982                         LColor::strikeout, Painter::line_solid, Painter::line_thin);
983                 running_strikeout = false;
984         }
985         return false;
986 }
987
988
989 bool RowPainter::paint(int y_offset, int x_offset, int y, bool cleared)
990 {
991         xo_ = x_offset;
992         yo_ = y_offset;
993         y_ = y;
994         cleared_ = cleared;
995         width_ = text_.isInInset()
996                 ? text_.inset_owner->textWidth(perv(bv_), true) : bv_.workWidth();
997
998         // FIXME: must be a cleaner way here. Aren't these calculations
999         // belonging to row metrics ?
1000         BufferView * bv(const_cast<BufferView *>(&bv_));
1001         Row * row(const_cast<Row *>(&row_));
1002         text_.prepareToPrint(bv, row, x_, separator_, hfill_, label_hfill_);
1003
1004         // FIXME: what is this fixing ?
1005         if (text_.isInInset() && (x_ < 0))
1006                 x_ = 0;
1007         x_ += xo_;
1008
1009         // clear to background if necessary
1010         cleared_ = paintBackground();
1011
1012         // paint the selection background
1013         if (text_.selection.set()) {
1014                 paintSelection();
1015         }
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
1030         if (row_.isParEnd()) {
1031                 paintLast();
1032         }
1033
1034         // paint text
1035         return paintText();
1036 }