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