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