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