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