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