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