]> git.lyx.org Git - lyx.git/blob - src/rowpainter.C
ws changes only
[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 "rowpainter.h"
15
16 #include "buffer.h"
17 #include "bufferparams.h"
18 #include "BufferView.h"
19 #include "encoding.h"
20 #include "gettext.h"
21 #include "language.h"
22 #include "LColor.h"
23 #include "lyxrc.h"
24 #include "lyxrow.h"
25 #include "lyxrow_funcs.h"
26 #include "metricsinfo.h"
27 #include "paragraph.h"
28 #include "paragraph_funcs.h"
29 #include "ParagraphParameters.h"
30 #include "vspace.h"
31
32 #include "frontends/font_metrics.h"
33 #include "frontends/Painter.h"
34
35 #include "insets/insettext.h"
36
37 #include "support/textutils.h"
38
39
40 using lyx::pos_type;
41 using std::max;
42 using std::string;
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         BOOST_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 = lastPos(*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 (c == Paragraph::META_INSET) {
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 = lastPos(*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 = lastPos(*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);
603         font.decSize();
604         font.decSize();
605         font_metrics::rectText(str, font, w, a, d);
606
607         pain_.rectText(leftx + 2 * arrow_size + 5,
608                        start + ((end - start) / 2) + d,
609                        str, font,
610                        LColor::none, LColor::none);
611
612         // top arrow
613         pain_.line(leftx, ty1, midx, ty2, LColor::added_space);
614         pain_.line(midx, ty2, rightx, ty1, LColor::added_space);
615
616         // bottom arrow
617         pain_.line(leftx, by1, midx, by2, LColor::added_space);
618         pain_.line(midx, by2, rightx, by1, LColor::added_space);
619
620         // joining line
621         pain_.line(midx, ty2, midx, by2, LColor::added_space);
622
623         return size;
624 }
625
626
627 int RowPainter::paintPageBreak(string const & label, int y)
628 {
629         LyXFont pb_font;
630         pb_font.setColor(LColor::pagebreak);
631         pb_font.decSize();
632
633         int w = 0;
634         int a = 0;
635         int d = 0;
636         font_metrics::rectText(label, pb_font, w, a, d);
637
638         int const text_start = int(xo_ + (width_ - w) / 2);
639         int const text_end = text_start + w;
640
641         pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
642
643         pain_.line(int(xo_), y, text_start, y,
644                    LColor::pagebreak, Painter::line_onoffdash);
645         pain_.line(text_end, y, int(xo_ + width_), y,
646                    LColor::pagebreak, Painter::line_onoffdash);
647
648         return 3 * defaultRowHeight();
649 }
650
651
652 int RowPainter::paintAppendixStart(int y)
653 {
654         LyXFont pb_font;
655         pb_font.setColor(LColor::appendix);
656         pb_font.decSize();
657
658         string const label = _("Appendix");
659         int w = 0;
660         int a = 0;
661         int d = 0;
662         font_metrics::rectText(label, pb_font, w, a, d);
663
664         int const text_start = int(xo_ + (width_ - w) / 2);
665         int const text_end = text_start + w;
666
667         pain_.rectText(text_start, y + d, label, pb_font, LColor::none, LColor::none);
668
669         pain_.line(int(xo_ + 1), y, text_start, y, LColor::appendix);
670         pain_.line(text_end, y, int(xo_ + width_ - 2), y, LColor::appendix);
671
672         return 3 * defaultRowHeight();
673 }
674
675
676 void RowPainter::paintFirst()
677 {
678         ParagraphParameters const & parparams = pit_->params();
679
680         int y_top = 0;
681
682         // start of appendix?
683         if (parparams.startOfAppendix())
684                 y_top += paintAppendixStart(yo_ + y_top + 2 * defaultRowHeight());
685
686         // the top margin
687         if (row_ == text_.firstRow() && !text_.isInInset())
688                 y_top += PAPER_MARGIN;
689
690         // draw a top pagebreak
691         if (parparams.pagebreakTop())
692                 y_top += paintPageBreak(_("Page Break (top)"),
693                         yo_ + y_top + 2 * defaultRowHeight());
694
695         // draw the additional space if needed:
696         y_top += paintLengthMarker(_("Space above"), parparams.spaceTop(),
697                         yo_ + y_top);
698
699         Buffer const & buffer = *bv_.buffer();
700
701         LyXLayout_ptr const & layout = pit_->layout();
702
703         if (buffer.params().paragraph_separation == BufferParams::PARSEP_SKIP) {
704                 if (pit_ != text_.ownerParagraphs().begin()) {
705                         if (layout->latextype == LATEX_PARAGRAPH
706                                 && !pit_->getDepth()) {
707                                 y_top += buffer.params().getDefSkip().inPixels(bv_);
708                         } else {
709                                 LyXLayout_ptr const & playout =
710                                         boost::prior(pit_)->layout();
711                                 if (playout->latextype == LATEX_PARAGRAPH
712                                         && !boost::prior(pit_)->getDepth()) {
713                                         // is it right to use defskip here, too? (AS)
714                                         y_top += buffer.params().getDefSkip().inPixels(bv_);
715                                 }
716                         }
717                 }
718         }
719
720         int const ww = bv_.workWidth();
721
722         // draw a top line
723         if (parparams.lineTop()) {
724                 int const asc = font_metrics::ascent('x', getFont(0));
725
726                 y_top += asc;
727
728                 int const w = (text_.isInInset() ? text_.inset_owner->width() : ww);
729                 int const xp = static_cast<int>(text_.isInInset() ? xo_ : 0);
730                 pain_.line(xp, yo_ + y_top, xp + w, yo_ + y_top,
731                         LColor::topline, Painter::line_solid,
732                         Painter::line_thick);
733
734                 y_top += asc;
735         }
736
737         bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params());
738
739         // should we print a label?
740         if (layout->labeltype >= LABEL_STATIC
741             && (layout->labeltype != LABEL_STATIC
742                 || layout->latextype != LATEX_ENVIRONMENT
743                 || isFirstInSequence(pit_, text_.ownerParagraphs()))) {
744
745                 LyXFont font = getLabelFont();
746                 if (!pit_->getLabelstring().empty()) {
747                         double x = x_;
748                         string const str = pit_->getLabelstring();
749
750                         // this is special code for the chapter layout. This is
751                         // printed in an extra row and has a pagebreak at
752                         // the top.
753                         if (layout->counter == "chapter") {
754                                 if (buffer.params().secnumdepth >= 0) {
755                                         float spacing_val = 1.0;
756                                         if (!parparams.spacing().isDefault()) {
757                                                 spacing_val = parparams.spacing().getValue();
758                                         } else {
759                                                 spacing_val = buffer.params().spacing().getValue();
760                                         }
761
762                                         int const maxdesc =
763                                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
764                                                 + int(layout->parsep) * defaultRowHeight();
765
766                                         if (is_rtl) {
767                                                 x = ww - leftMargin() -
768                                                         font_metrics::width(str, font);
769                                         }
770
771                                         pain_.text(int(x),
772                                                 yo_ + row_->baseline() -
773                                                 row_->ascent_of_text() - maxdesc,
774                                                 str, font);
775                                 }
776                         } else {
777                                 if (is_rtl) {
778                                         x = ww - leftMargin()
779                                                 + font_metrics::width(layout->labelsep, font);
780                                 } else {
781                                         x = x_ - font_metrics::width(layout->labelsep, font)
782                                                 - font_metrics::width(str, font);
783                                 }
784
785                                 pain_.text(int(x), yo_ + row_->baseline(), str, font);
786                         }
787                 }
788
789         // the labels at the top of an environment.
790         // More or less for bibliography
791         } else if (isFirstInSequence(pit_, text_.ownerParagraphs()) &&
792                 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
793                 layout->labeltype == LABEL_BIBLIO ||
794                 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
795                 LyXFont font = getLabelFont();
796                 if (!pit_->getLabelstring().empty()) {
797                         string const str = pit_->getLabelstring();
798                         float spacing_val = 1.0;
799                         if (!parparams.spacing().isDefault()) {
800                                 spacing_val = parparams.spacing().getValue();
801                         } else {
802                                 spacing_val = buffer.params().spacing().getValue();
803                         }
804
805                         int maxdesc =
806                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
807                                 + (layout->labelbottomsep * defaultRowHeight()));
808
809                         double x = x_;
810                         if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
811                                 x = ((is_rtl ? leftMargin() : x_)
812                                          + ww - text_.rightMargin(pit_, *bv_.buffer(), *row_)) / 2;
813                                 x -= font_metrics::width(str, font) / 2;
814                         } else if (is_rtl) {
815                                 x = ww - leftMargin() -
816                                         font_metrics::width(str, font);
817                         }
818                         pain_.text(int(x),
819                             yo_ + row_->baseline() - row_->ascent_of_text() - maxdesc,
820                                   str, font);
821                 }
822         }
823 }
824
825
826 void RowPainter::paintLast()
827 {
828         ParagraphParameters const & parparams = pit_->params();
829         int y_bottom = row_->height() - 1;
830
831         // the bottom margin
832         if (row_ == text_.lastRow() && !text_.isInInset())
833                 y_bottom -= PAPER_MARGIN;
834
835         int const ww = bv_.workWidth();
836
837         // draw a bottom pagebreak
838         if (parparams.pagebreakBottom()) {
839                 y_bottom -= paintPageBreak(_("Page Break (bottom)"),
840                         yo_ + y_bottom - 2 * defaultRowHeight());
841         }
842
843         // draw the additional space if needed:
844         int const height = getLengthMarkerHeight(bv_, parparams.spaceBottom());
845         y_bottom -= paintLengthMarker(_("Space below"), parparams.spaceBottom(),
846                              yo_ + y_bottom - height);
847
848         // draw a bottom line
849         if (parparams.lineBottom()) {
850                 int const asc = font_metrics::ascent('x',
851                         getFont(max(pos_type(0), pit_->size() - 1)));
852
853                 y_bottom -= asc;
854
855                 int const w = text_.isInInset() ? text_.inset_owner->width() : ww;
856                 int const xp = int(text_.isInInset() ? xo_ : 0);
857                 int const y = yo_ + y_bottom;
858                 pain_.line(xp, y, xp + w, y, LColor::topline, Painter::line_solid,
859                           Painter::line_thick);
860
861                 y_bottom -= asc;
862         }
863
864         bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params());
865         int const endlabel = getEndLabel(pit_, text_.ownerParagraphs());
866
867         // draw an endlabel
868         switch (endlabel) {
869         case END_LABEL_BOX:
870         case END_LABEL_FILLED_BOX:
871         {
872                 LyXFont const font = getLabelFont();
873                 int const size = int(0.75 * font_metrics::maxAscent(font));
874                 int const y = (yo_ + row_->baseline()) - size;
875                 int x = is_rtl ? LEFT_MARGIN : ww - PAPER_MARGIN - size;
876
877                 if (row_->fill() <= size)
878                         x += (size - row_->fill() + 1) * (is_rtl ? -1 : 1);
879
880                 if (endlabel == END_LABEL_BOX)
881                         pain_.rectangle(x, y, size, size, LColor::eolmarker);
882                 else
883                         pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
884                 break;
885         }
886         case END_LABEL_STATIC:
887         {
888                 LyXFont font = getLabelFont();
889                 string const & str = pit_->layout()->endlabelstring();
890                 double const x = is_rtl ?
891                         x_ - font_metrics::width(str, font)
892                         : ww - text_.rightMargin(pit_, *bv_.buffer(), *row_) - row_->fill();
893                 pain_.text(int(x), yo_ + row_->baseline(), str, font);
894                 break;
895         }
896         case END_LABEL_NO_LABEL:
897                 break;
898         }
899 }
900
901
902 void RowPainter::paintText()
903 {
904         pos_type const last = lastPos(*pit_, row_);
905         pos_type body_pos = pit_->beginningOfBody();
906         if (body_pos > 0 &&
907                 (body_pos - 1 > last || !pit_->isLineSeparator(body_pos - 1))) {
908                 body_pos = 0;
909         }
910
911         LyXLayout_ptr const & layout = pit_->layout();
912
913         bool running_strikeout = false;
914         bool is_struckout = false;
915         int last_strikeout_x = 0;
916
917         pos_type vpos = row_->pos();
918         while (vpos <= last) {
919                 if (x_ > bv_.workWidth())
920                         break;
921                 pos_type pos = text_.vis2log(vpos);
922
923                 if (pos >= pit_->size()) {
924                         ++vpos;
925                         continue;
926                 }
927
928                 if (x_ + singleWidth(pos) < 0) {
929                         x_ += singleWidth(pos);
930                         ++vpos;
931                         continue;
932                 }
933
934                 is_struckout = isDeletedText(*pit_, pos);
935
936                 if (is_struckout && !running_strikeout) {
937                         running_strikeout = true;
938                         last_strikeout_x = int(x_);
939                 }
940
941                 bool const highly_editable_inset = pit_->isInset(pos)
942                         && isHighlyEditableInset(pit_->getInset(pos));
943
944                 // if we reach the end of a struck out range, paint it
945                 // we also don't paint across things like tables
946                 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
947                         int const middle = yo_ + row_->top_of_text()
948                                 + (row_->baseline() - row_->top_of_text()) / 2;
949                         pain_.line(last_strikeout_x, middle, int(x_), middle,
950                                 LColor::strikeout, Painter::line_solid, Painter::line_thin);
951                         running_strikeout = false;
952                 }
953
954                 if (body_pos > 0 && pos == body_pos - 1) {
955                         int const lwidth = font_metrics::width(layout->labelsep,
956                                 getLabelFont());
957
958                         x_ += label_hfill_ + lwidth - singleWidth(body_pos - 1);
959                 }
960
961                 if (pit_->isHfill(pos)) {
962                         x_ += 1;
963
964                         int const y0 = yo_ + row_->baseline();
965                         int const y1 = y0 - defaultRowHeight() / 2;
966
967                         pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
968
969                         if (hfillExpansion(*pit_, row_, pos)) {
970                                 int const y2 = (y0 + y1) / 2;
971
972                                 if (pos >= body_pos) {
973                                         pain_.line(int(x_), y2, int(x_ + hfill_), y2,
974                                                   LColor::added_space,
975                                                   Painter::line_onoffdash);
976                                         x_ += hfill_;
977                                 } else {
978                                         pain_.line(int(x_), y2, int(x_ + label_hfill_), y2,
979                                                   LColor::added_space,
980                                                   Painter::line_onoffdash);
981                                         x_ += label_hfill_;
982                                 }
983                                 pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
984                         }
985                         x_ += 2;
986                         ++vpos;
987                 } else if (pit_->isSeparator(pos)) {
988                         x_ += singleWidth(pos);
989                         if (pos >= body_pos)
990                                 x_ += separator_;
991                         ++vpos;
992                 } else {
993                         paintFromPos(vpos);
994                 }
995         }
996
997         // if we reach the end of a struck out range, paint it
998         if (running_strikeout) {
999                 int const middle = yo_ + row_->top_of_text()
1000                         + ((row_->baseline() - row_->top_of_text()) / 2);
1001                 pain_.line(last_strikeout_x, middle, int(x_), middle,
1002                         LColor::strikeout, Painter::line_solid, Painter::line_thin);
1003                 running_strikeout = false;
1004         }
1005 }
1006
1007
1008 void RowPainter::paint()
1009 {
1010         width_       = text_.workWidth();
1011         x_           = row_->x();
1012         separator_   = row_->fill_separator();
1013         hfill_       = row_->fill_hfill();
1014         label_hfill_ = row_->fill_label_hfill();
1015
1016         // FIXME: what is this fixing ?
1017         if (text_.isInInset() && x_ < 0)
1018                 x_ = 0;
1019         x_ += xo_;
1020
1021         // If we're *not* at the top-level of rows, then the
1022         // background has already been cleared.
1023         if (&text_ == bv_.text)
1024                 paintBackground();
1025
1026         // paint the selection background
1027         if (text_.selection.set())
1028                 paintSelection();
1029
1030         // vertical lines for appendix
1031         paintAppendix();
1032
1033         // environment depth brackets
1034         paintDepthBar();
1035
1036         // changebar
1037         paintChangeBar();
1038
1039         if (row_->isParStart())
1040                 paintFirst();
1041
1042         if (isParEnd(*pit_, row_))
1043                 paintLast();
1044
1045         // paint text
1046         paintText();
1047 }
1048
1049
1050 int paintRows(BufferView const & bv, LyXText const & text,
1051         ParagraphList::iterator pit, RowList::iterator rit,
1052         int xo, int y, int yf, int yo)
1053 {
1054         //lyxerr << "  paintRows: rit: " << &*rit << endl;
1055         //const_cast<LyXText&>(text).updateRowPositions();
1056         int const yy = yf - y;
1057         int const y2 = bv.painter().paperHeight();
1058
1059         ParagraphList::iterator end = text.ownerParagraphs().end();
1060         bool active = false;
1061
1062         for ( ; pit != end; ++pit) {
1063                 RowList::iterator row = pit->rows.begin();
1064                 RowList::iterator rend = pit->rows.end();
1065
1066                 for ( ; row != rend; ++row) {
1067                         if (row == rit)
1068                                 active = true;
1069                         if (active) {
1070                                 RowPainter painter(bv, text, pit, row, y + yo, xo, y + bv.top_y());
1071                                 painter.paint();
1072                                 y += row->height();
1073                                 if (yy + y >= y2)
1074                                         return y;
1075                         } else {
1076                                 //lyxerr << "   paintRows: row: " << &*row << " ignored" << endl;
1077                         }
1078                 }
1079         }
1080
1081         return y;
1082 }
1083
1084 } // namespace anon
1085
1086
1087 int paintText(BufferView & bv)
1088 {
1089         int const topy = bv.top_y();
1090         ParagraphList::iterator pit;
1091         RowList::iterator rit = bv.text->getRowNearY(topy, pit);
1092         int y = rit->y() - topy;
1093         return paintRows(bv, *bv.text, pit, rit, 0, y, y, 0);
1094 }
1095
1096
1097 void paintTextInset(BufferView & bv, LyXText & text, int x, int baseline)
1098 {
1099         RowList::iterator rit = text.firstRow();
1100         RowList::iterator end = text.endRow();
1101         ParagraphList::iterator pit = text.ownerParagraphs().begin();
1102
1103         int y_offset = baseline - rit->ascent_of_text();
1104         int y = y_offset;
1105         while (rit != end && y + rit->height() <= 0) {
1106                 y += rit->height();
1107                 text.nextRow(pit, rit);
1108         }
1109         if (y_offset < 0)
1110                 paintRows(bv, text, pit, rit, x, 0, y, y);
1111         else
1112                 paintRows(bv, text, pit, rit, x, 0, y_offset, y_offset);
1113 }
1114
1115
1116 int getLengthMarkerHeight(BufferView const & bv, VSpace const & vsp)
1117 {
1118         if (vsp.kind() == VSpace::NONE)
1119                 return 0;
1120
1121         int const arrow_size = 4;
1122         int const space_size = vsp.inPixels(bv);
1123
1124         LyXFont font;
1125         font.decSize();
1126         int const min_size = max(3 * arrow_size,
1127                 font_metrics::maxAscent(font)
1128                 + font_metrics::maxDescent(font));
1129
1130         if (vsp.length().len().value() < 0.0)
1131                 return min_size;
1132         else
1133                 return max(min_size, space_size);
1134 }