]> git.lyx.org Git - lyx.git/blob - src/rowpainter.C
cosmetic fix
[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 "lyxrow.h"
32 #include "rowpainter.h"
33 #include "lyxrc.h"
34 #include "lyxrow_funcs.h"
35 #include "metricsinfo.h"
36
37 #include <algorithm>
38
39 using namespace lyx::support;
40
41 using std::max;
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                 RowList::iterator rit, int y_offset, int x_offset, int y);
66
67         /// do the painting
68         void paint();
69 private:
70         // paint various parts
71         void paintBackground();
72         void paintSelection();
73         void paintAppendix();
74         void paintDepthBar();
75         void paintChangeBar();
76         void paintFirst();
77         void paintLast();
78         void paintForeignMark(int orig_x, LyXFont const & orig_font);
79         void paintHebrewComposeChar(lyx::pos_type & vpos);
80         void paintArabicComposeChar(lyx::pos_type & vpos);
81         void paintChars(lyx::pos_type & vpos, bool hebrew, bool arabic);
82         int paintPageBreak(string const & label, int y);
83         int paintAppendixStart(int y);
84         int paintLengthMarker(string const & prefix, VSpace const & vsp, int start);
85         void paintText();
86         void paintFromPos(lyx::pos_type & vpos);
87         void paintInset(lyx::pos_type const pos);
88
89         /// return left margin
90         int leftMargin() const;
91
92         /// return the font at the given pos
93         LyXFont const getFont(lyx::pos_type pos) const;
94
95         /// return the label font for this row
96         LyXFont const getLabelFont() const;
97
98         char const transformChar(char c, lyx::pos_type pos) const;
99
100         /// return pixel width for the given pos
101         int singleWidth(lyx::pos_type pos) const;
102         int singleWidth(lyx::pos_type pos, char c) const;
103
104         /// bufferview to paint on
105         BufferView const & bv_;
106
107         /// Painter to use
108         Painter & pain_;
109
110         /// LyXText for the row
111         LyXText const & text_;
112
113         /// The row to paint
114         RowList::iterator row_;
115
116         /// Row's paragraph
117         mutable ParagraphList::iterator  pit_;
118
119         // Looks ugly - is
120         int xo_;
121         int yo_;
122         int x_;
123         int y_;
124         int width_;
125         int separator_;
126         int hfill_;
127         int label_hfill_;
128 };
129
130 RowPainter::RowPainter(BufferView const & bv, LyXText const & text,
131      RowList::iterator rit,
132      int y_offset, int x_offset, int y)
133         : bv_(bv), pain_(bv_.painter()), text_(text), row_(rit),
134           pit_(rit->par()),
135                 xo_(x_offset), yo_(y_offset), y_(y)
136 {}
137
138
139 /// "temporary"
140 LyXFont const RowPainter::getFont(pos_type pos) const
141 {
142         return text_.getFont(bv_.buffer(), pit_, pos);
143 }
144
145
146 int RowPainter::singleWidth(lyx::pos_type pos) const
147 {
148         return text_.singleWidth(pit_, pos);
149 }
150
151
152 int RowPainter::singleWidth(lyx::pos_type pos, char c) const
153 {
154         return text_.singleWidth(pit_, pos, c);
155 }
156
157
158 LyXFont const RowPainter::getLabelFont() const
159 {
160         return text_.getLabelFont(bv_.buffer(), pit_);
161 }
162
163
164 char const RowPainter::transformChar(char c, lyx::pos_type pos) const
165 {
166         return text_.transformChar(c, *pit_, pos);
167 }
168
169
170 int RowPainter::leftMargin() const
171 {
172         return text_.leftMargin(*row_);
173 }
174
175
176 void RowPainter::paintInset(pos_type const pos)
177 {
178         InsetOld * inset = const_cast<InsetOld*>(pit_->getInset(pos));
179
180         Assert(inset);
181
182         PainterInfo pi(perv(bv_));
183         pi.base.font = getFont(pos);
184 #warning metrics?
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(text_, 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(int 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         int 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 = 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 = xo_ + startx;
403                                 w = endx - startx;
404                                 pain_.fillRectangle(x, y, w, h, LColor::selection);
405                         } else {
406                                 x = 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) ? xo_ : (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) ? (xo_ + endx) : 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(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(xo_, yo_, w, h, LColor::selection);
427                 }
428                 return;
429         }
430
431         if ((startrow != row_ && !is_rtl) || (endrow != row_ && is_rtl))
432                 pain_.fillRectangle(xo_, yo_, int(x_), row_->height(), LColor::selection);
433
434         pos_type const body_pos = pit_->beginningOfBody();
435         pos_type const last = lastPrintablePos(text_, row_);
436         int tmpx = x_;
437
438         for (pos_type vpos = row_->pos(); vpos <= last; ++vpos)  {
439                 pos_type pos = text_.vis2log(vpos);
440                 int const old_tmpx = tmpx;
441                 if (body_pos > 0 && pos == body_pos - 1) {
442                         LyXLayout_ptr const & layout = pit_->layout();
443                         LyXFont const lfont = getLabelFont();
444
445                         tmpx += label_hfill_ + font_metrics::width(layout->labelsep, lfont);
446
447                         if (pit_->isLineSeparator(body_pos - 1))
448                                 tmpx -= singleWidth(body_pos - 1);
449                 }
450
451                 if (hfillExpansion(text_, row_, pos)) {
452                         tmpx += singleWidth(pos);
453                         if (pos >= body_pos)
454                                 tmpx += hfill_;
455                         else
456                                 tmpx += label_hfill_;
457                 }
458
459                 else if (pit_->isSeparator(pos)) {
460                         tmpx += singleWidth(pos);
461                         if (pos >= body_pos)
462                                 tmpx += separator_;
463                 } else {
464                         tmpx += singleWidth(pos);
465                 }
466
467                 if ((startrow != row_ || text_.selection.start.pos() <= pos) &&
468                         (endrow != row_ || pos < text_.selection.end.pos())) {
469                         // Here we do not use x_ as xo_ was added to x_.
470                         pain_.fillRectangle(int(old_tmpx), yo_,
471                                 int(tmpx - old_tmpx + 1),
472                                 row_->height(), LColor::selection);
473                 }
474         }
475
476         if ((startrow != row_ && is_rtl) || (endrow != row_ && !is_rtl)) {
477                 pain_.fillRectangle(xo_ + int(tmpx),
478                                       yo_, int(bv_.workWidth() - tmpx),
479                                       row_->height(), LColor::selection);
480         }
481 }
482
483
484 void RowPainter::paintChangeBar()
485 {
486         pos_type const start = row_->pos();
487         pos_type const end = lastPrintablePos(text_, row_);
488
489         if (!pit_->isChanged(start, end))
490                 return;
491
492         int const height = (boost::next(row_) != text_.rows().end()
493                 ? row_->height() + boost::next(row_)->top_of_text()
494                 : row_->baseline());
495
496         pain_.fillRectangle(4, yo_, 5, height, LColor::changebar);
497 }
498
499
500 void RowPainter::paintAppendix()
501 {
502         if (!pit_->params().appendix())
503                 return;
504
505         // FIXME: can be just width_ ?
506         int const ww = bv_.workWidth();
507
508         int y = yo_;
509
510         if (pit_->params().startOfAppendix())
511                 y += 2 * defaultRowHeight();
512
513         pain_.line(1, y, 1, yo_ + row_->height(), LColor::appendix);
514         pain_.line(ww - 2, y, ww - 2, yo_ + row_->height(), LColor::appendix);
515 }
516
517
518 void RowPainter::paintDepthBar()
519 {
520         Paragraph::depth_type const depth = pit_->getDepth();
521
522         if (depth <= 0)
523                 return;
524
525         Paragraph::depth_type prev_depth = 0;
526         if (row_ != text_.rows().begin())
527                 prev_depth = boost::prior(row_)->par()->getDepth();
528         Paragraph::depth_type next_depth = 0;
529
530         RowList::iterator next_row = boost::next(row_);
531         if (next_row != text_.rows().end())
532                 next_depth = next_row->par()->getDepth();
533
534         for (Paragraph::depth_type i = 1; i <= depth; ++i) {
535                 int x = (PAPER_MARGIN / 5) * i + xo_;
536                 // only consider the changebar space if we're drawing outer left
537                 if (!xo_)
538                         x += CHANGEBAR_MARGIN;
539                 int const h = yo_ + row_->height() - 1 - (i - next_depth - 1) * 3;
540
541                 pain_.line(x, yo_, x, h, LColor::depthbar);
542
543                 int const w = PAPER_MARGIN / 5;
544
545                 if (i > prev_depth) {
546                         pain_.fillRectangle(x, yo_, w, 2, LColor::depthbar);
547                 }
548                 if (i > next_depth) {
549                         pain_.fillRectangle(x, h, w, 2, LColor::depthbar);
550                 }
551         }
552 }
553
554
555 int RowPainter::paintLengthMarker(string const & prefix, VSpace const & vsp, int start)
556 {
557         if (vsp.kind() == VSpace::NONE)
558                 return 0;
559
560         int const arrow_size = 4;
561         int const size = getLengthMarkerHeight(bv_, vsp);
562         int const end = start + size;
563
564         // the label to display (if any)
565         string str;
566         // y-values for top arrow
567         int ty1, ty2;
568         // y-values for bottom arrow
569         int by1, by2;
570
571         str = prefix + " (" + vsp.asLyXCommand() + ")";
572
573         if (vsp.kind() == VSpace::VFILL) {
574                 ty1 = ty2 = start;
575                 by1 = by2 = end;
576         } else {
577                 // adding or removing space
578                 bool const added = vsp.kind() != VSpace::LENGTH ||
579                                    vsp.length().len().value() > 0.0;
580                 ty1 = added ? (start + arrow_size) : start;
581                 ty2 = added ? start : (start + arrow_size);
582                 by1 = added ? (end - arrow_size) : end;
583                 by2 = added ? end : (end - arrow_size);
584         }
585
586         int const leftx = xo_ + leftMargin();
587         int const midx = leftx + arrow_size;
588         int const rightx = midx + arrow_size;
589
590         // first the string
591         int w = 0;
592         int a = 0;
593         int d = 0;
594
595         LyXFont font;
596         font.setColor(LColor::added_space).decSize().decSize();
597         font_metrics::rectText(str, font, w, a, d);
598
599         pain_.rectText(leftx + 2 * arrow_size + 5,
600                          start + ((end - start) / 2) + d,
601                          str, font);
602
603         // top arrow
604         pain_.line(leftx, ty1, midx, ty2, LColor::added_space);
605         pain_.line(midx, ty2, rightx, ty1, LColor::added_space);
606
607         // bottom arrow
608         pain_.line(leftx, by1, midx, by2, LColor::added_space);
609         pain_.line(midx, by2, rightx, by1, LColor::added_space);
610
611         // joining line
612         pain_.line(midx, ty2, midx, by2, LColor::added_space);
613
614         return size;
615 }
616
617
618 int RowPainter::paintPageBreak(string const & label, int y)
619 {
620         LyXFont pb_font;
621         pb_font.setColor(LColor::pagebreak).decSize();
622
623         int w = 0;
624         int a = 0;
625         int d = 0;
626         font_metrics::rectText(label, pb_font, w, a, d);
627
628         int const text_start = xo_ + ((width_ - w) / 2);
629         int const text_end = text_start + w;
630
631         pain_.rectText(text_start, y + d, label, pb_font);
632
633         pain_.line(xo_, y, text_start, y,
634                 LColor::pagebreak, Painter::line_onoffdash);
635         pain_.line(text_end, y, xo_ + width_, y,
636                 LColor::pagebreak, Painter::line_onoffdash);
637
638         return 3 * defaultRowHeight();
639 }
640
641
642 int RowPainter::paintAppendixStart(int y)
643 {
644         LyXFont pb_font;
645         pb_font.setColor(LColor::appendix).decSize();
646
647         string const label = _("Appendix");
648         int w = 0;
649         int a = 0;
650         int d = 0;
651         font_metrics::rectText(label, pb_font, w, a, d);
652
653         int const text_start = xo_ + ((width_ - w) / 2);
654         int const text_end = text_start + w;
655
656         pain_.rectText(text_start, y + d, label, pb_font);
657
658         pain_.line(xo_ + 1, y, text_start, y, LColor::appendix);
659         pain_.line(text_end, y, xo_ + width_ - 2, y, LColor::appendix);
660
661         return 3 * defaultRowHeight();
662 }
663
664
665 void RowPainter::paintFirst()
666 {
667         ParagraphParameters const & parparams = pit_->params();
668
669         int y_top = 0;
670
671         // start of appendix?
672         if (parparams.startOfAppendix()) {
673                 y_top += paintAppendixStart(yo_ + y_top + 2 * defaultRowHeight());
674         }
675
676         // the top margin
677         if (row_ == text_.rows().begin() && !text_.isInInset())
678                 y_top += PAPER_MARGIN;
679
680         // draw a top pagebreak
681         if (parparams.pagebreakTop()) {
682                 y_top += paintPageBreak(_("Page Break (top)"),
683                         yo_ + y_top + 2 * defaultRowHeight());
684         }
685
686         // draw the additional space if needed:
687         y_top += paintLengthMarker(_("Space above"), parparams.spaceTop(),
688                         yo_ + y_top);
689
690         Buffer const * buffer = bv_.buffer();
691
692         LyXLayout_ptr const & layout = pit_->layout();
693
694         if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
695                 if (pit_ != text_.ownerParagraphs().begin()) {
696                         if (layout->latextype == LATEX_PARAGRAPH
697                                 && !pit_->getDepth()) {
698                                 y_top += buffer->params.getDefSkip().inPixels(bv_);
699                         } else {
700                                 LyXLayout_ptr const & playout =
701                                         boost::prior(pit_)->layout();
702                                 if (playout->latextype == LATEX_PARAGRAPH
703                                         && !boost::prior(pit_)->getDepth()) {
704                                         // is it right to use defskip here, too? (AS)
705                                         y_top += buffer->params.getDefSkip().inPixels(bv_);
706                                 }
707                         }
708                 }
709         }
710
711         int const ww = bv_.workWidth();
712
713         // draw a top line
714         if (parparams.lineTop()) {
715                 int const asc = font_metrics::ascent('x', getFont(0));
716
717                 y_top += asc;
718
719                 int const w = (text_.isInInset() ? text_.inset_owner->width() : ww);
720                 int const xp = static_cast<int>(text_.isInInset() ? xo_ : 0);
721                 pain_.line(xp, yo_ + y_top, xp + w, yo_ + y_top,
722                         LColor::topline, Painter::line_solid,
723                         Painter::line_thick);
724
725                 y_top += asc;
726         }
727
728         bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params);
729
730         // should we print a label?
731         if (layout->labeltype >= LABEL_STATIC
732             && (layout->labeltype != LABEL_STATIC
733                 || layout->latextype != LATEX_ENVIRONMENT
734                 || isFirstInSequence(pit_, text_.ownerParagraphs()))) {
735
736                 LyXFont font = getLabelFont();
737                 if (!pit_->getLabelstring().empty()) {
738                         int x = x_;
739                         string const str = pit_->getLabelstring();
740
741                         // this is special code for the chapter layout. This is
742                         // printed in an extra row and has a pagebreak at
743                         // the top.
744                         if (layout->labeltype == LABEL_COUNTER_CHAPTER) {
745                                 if (buffer->params.secnumdepth >= 0) {
746                                         float spacing_val = 1.0;
747                                         if (!parparams.spacing().isDefault()) {
748                                                 spacing_val = parparams.spacing().getValue();
749                                         } else {
750                                                 spacing_val = buffer->params.spacing.getValue();
751                                         }
752
753                                         int const maxdesc =
754                                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val)
755                                                 + int(layout->parsep) * defaultRowHeight();
756
757                                         if (is_rtl) {
758                                                 x = ww - leftMargin() -
759                                                         font_metrics::width(str, font);
760                                         }
761
762                                         pain_.text(x,
763                                                 yo_ + row_->baseline() -
764                                                 row_->ascent_of_text() - maxdesc,
765                                                 str, font);
766                                 }
767                         } else {
768                                 if (is_rtl) {
769                                         x = ww - leftMargin()
770                                                 + font_metrics::width(layout->labelsep, font);
771                                 } else {
772                                         x = x_ - font_metrics::width(layout->labelsep, font)
773                                                 - font_metrics::width(str, font);
774                                 }
775
776                                 pain_.text(x, yo_ + row_->baseline(), str, font);
777                         }
778                 }
779
780         // the labels at the top of an environment.
781         // More or less for bibliography
782         } else if (isFirstInSequence(pit_, text_.ownerParagraphs()) &&
783                 (layout->labeltype == LABEL_TOP_ENVIRONMENT ||
784                 layout->labeltype == LABEL_BIBLIO ||
785                 layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)) {
786                 LyXFont font = getLabelFont();
787                 if (!pit_->getLabelstring().empty()) {
788                         string const str = pit_->getLabelstring();
789                         float spacing_val = 1.0;
790                         if (!parparams.spacing().isDefault()) {
791                                 spacing_val = parparams.spacing().getValue();
792                         } else {
793                                 spacing_val = buffer->params.spacing.getValue();
794                         }
795
796                         int maxdesc =
797                                 int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val
798                                 + (layout->labelbottomsep * defaultRowHeight()));
799
800                         int x = x_;
801                         if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
802                                 x = ((is_rtl ? leftMargin() : x_)
803                                          + ww - text_.rightMargin(*bv_.buffer(), *row_)) / 2;
804                                 x -= font_metrics::width(str, font) / 2;
805                         } else if (is_rtl) {
806                                 x = ww - leftMargin() -
807                                         font_metrics::width(str, font);
808                         }
809                         pain_.text(x, yo_ + row_->baseline()
810                                   - row_->ascent_of_text() - maxdesc,
811                                   str, font);
812                 }
813         }
814 }
815
816
817 void RowPainter::paintLast()
818 {
819         ParagraphParameters const & parparams = pit_->params();
820         int y_bottom = row_->height() - 1;
821
822         // the bottom margin
823         if (boost::next(row_) == text_.rows().end() && !text_.isInInset())
824                 y_bottom -= PAPER_MARGIN;
825
826         int const ww = bv_.workWidth();
827
828         // draw a bottom pagebreak
829         if (parparams.pagebreakBottom()) {
830                 y_bottom -= paintPageBreak(_("Page Break (bottom)"),
831                         yo_ + y_bottom - 2 * defaultRowHeight());
832         }
833
834         // draw the additional space if needed:
835         int const height = getLengthMarkerHeight(bv_, parparams.spaceBottom());
836         y_bottom -= paintLengthMarker(_("Space below"), parparams.spaceBottom(),
837                              yo_ + y_bottom - height);
838
839         // draw a bottom line
840         if (parparams.lineBottom()) {
841                 int const asc = font_metrics::ascent('x',
842                         getFont(max(pos_type(0), pit_->size() - 1)));
843
844                 y_bottom -= asc;
845
846                 int const w = text_.isInInset() ? text_.inset_owner->width() : ww;
847                 int const xp = static_cast<int>(text_.isInInset() ? xo_ : 0);
848                 int const y = yo_ + y_bottom;
849                 pain_.line(xp, y, xp + w, y, LColor::topline, Painter::line_solid,
850                           Painter::line_thick);
851
852                 y_bottom -= asc;
853         }
854
855         bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params);
856         int const endlabel = getEndLabel(pit_, text_.ownerParagraphs());
857
858         // draw an endlabel
859         switch (endlabel) {
860         case END_LABEL_BOX:
861         case END_LABEL_FILLED_BOX:
862         {
863                 LyXFont const font = getLabelFont();
864                 int const size = int(0.75 * font_metrics::maxAscent(font));
865                 int const y = (yo_ + row_->baseline()) - size;
866                 int x = is_rtl ? LEFT_MARGIN : ww - PAPER_MARGIN - size;
867
868                 if (row_->fill() <= size)
869                         x += (size - row_->fill() + 1) * (is_rtl ? -1 : 1);
870
871                 if (endlabel == END_LABEL_BOX) {
872                         pain_.rectangle(x, y, size, size, LColor::eolmarker);
873                 } else {
874                         pain_.fillRectangle(x, y, size, size, LColor::eolmarker);
875                 }
876                 break;
877         }
878         case END_LABEL_STATIC:
879         {
880                 LyXFont font = getLabelFont();
881                 string const & str = pit_->layout()->endlabelstring();
882                 int const x = is_rtl ?
883                         x_ - font_metrics::width(str, font)
884                         : ww - text_.rightMargin(*bv_.buffer(), *row_) - row_->fill();
885                 pain_.text(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 = lastPrintablePos(text_, row_);
897         pos_type body_pos = pit_->beginningOfBody();
898         if (body_pos > 0 &&
899                 (body_pos - 1 > last ||
900                 !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 = 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, 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(x_, y1, x_, y0, LColor::added_space);
961
962                         if (hfillExpansion(text_, row_, pos)) {
963                                 int const y2 = (y0 + y1) / 2;
964
965                                 if (pos >= body_pos) {
966                                         pain_.line(x_, y2, x_ + hfill_, y2,
967                                                   LColor::added_space,
968                                                   Painter::line_onoffdash);
969                                         x_ += hfill_;
970                                 } else {
971                                         pain_.line(x_, y2,
972                                                   x_ + label_hfill_, y2,
973                                                   LColor::added_space,
974                                                   Painter::line_onoffdash);
975                                         x_ += label_hfill_;
976                                 }
977                                 pain_.line(x_, y1, 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, x_, middle,
996                         LColor::strikeout, Painter::line_solid, Painter::line_thin);
997                 running_strikeout = false;
998         }
999         return;
1000 }
1001
1002
1003 void RowPainter::paint()
1004 {
1005         width_ = text_.workWidth();
1006
1007         // FIXME: must be a cleaner way here. Aren't these calculations
1008         // belonging to row metrics ?
1009         text_.prepareToPrint(row_, x_, separator_, hfill_, label_hfill_);
1010
1011         // FIXME: what is this fixing ?
1012         if (text_.isInInset() && x_ < 0)
1013                 x_ = 0;
1014         x_ += xo_;
1015
1016         // If we're *not* at the top-level of rows, then the
1017         // background has already been cleared.
1018         if (&text_ == bv_.text)
1019                 paintBackground();
1020
1021         // paint the selection background
1022         if (text_.selection.set()) {
1023                 paintSelection();
1024         }
1025
1026         // vertical lines for appendix
1027         paintAppendix();
1028
1029         // environment depth brackets
1030         paintDepthBar();
1031
1032         // changebar
1033         paintChangeBar();
1034
1035         if (row_->isParStart()) {
1036                 paintFirst();
1037         }
1038
1039         if (isParEnd(text_, row_)) {
1040                 paintLast();
1041         }
1042
1043         // paint text
1044         paintText();
1045 }
1046
1047
1048 } // namespace anon
1049
1050
1051 int getLengthMarkerHeight(BufferView const & bv, VSpace const & vsp)
1052 {
1053         if (vsp.kind() == VSpace::NONE)
1054                 return 0;
1055
1056         int const arrow_size = 4;
1057         int const space_size = vsp.inPixels(bv);
1058
1059         LyXFont font;
1060         font.decSize();
1061         int const min_size = max(3 * arrow_size,
1062                 font_metrics::maxAscent(font)
1063                 + font_metrics::maxDescent(font));
1064
1065         if (vsp.length().len().value() < 0.0)
1066                 return min_size;
1067         else
1068                 return max(min_size, space_size);
1069 }
1070
1071
1072 void paintRows(BufferView const & bv, LyXText const & text,
1073         RowList::iterator rit, int y_offset, int x_offset, int y)
1074 {
1075         // fix up missing metrics() call for main LyXText
1076         // calling metrics() directly is (a) slow and (b) crashs
1077         if (&text == bv.text) {
1078 #if 1
1079                 // make sure all insets are updated
1080                 ParagraphList::iterator pit = text.ownerParagraphs().begin();
1081                 ParagraphList::iterator end = text.ownerParagraphs().end();
1082
1083                 // compute inset metrics
1084                 for (; pit != end; ++pit) {
1085                         InsetList & insetList = pit->insetlist;
1086                         InsetList::iterator ii = insetList.begin();
1087                         InsetList::iterator iend = insetList.end();
1088                         for (; ii != iend; ++ii) {
1089                                 Dimension dim;
1090                                 LyXFont font;
1091                                 MetricsInfo mi(perv(bv), font, text.workWidth());
1092                                 ii->inset->metrics(mi, dim);
1093                         }
1094                 }
1095 #else
1096                 LyXFont font;
1097                 Dimension dim;
1098                 MetricsInfo mi(perv(bv), font, text.workWidth());
1099                 const_cast<LyXText&>(text).metrics(mi, dim);
1100 #endif
1101         }
1102
1103         RowPainter painter(bv, text, rit, y_offset, x_offset, y);
1104         painter.paint();
1105 }