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