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