]> git.lyx.org Git - lyx.git/blob - src/rowpainter.C
20975a51463003eb4b8130f0eb129818afbe7f3e
[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 lyx::pos_type;
42
43 extern int PAPER_MARGIN;
44 extern int CHANGEBAR_MARGIN;
45 extern int LEFT_MARGIN;
46
47 namespace {
48
49 // "temporary". We'll never get to use more
50 // references until we start adding hacks like
51 // these until other places catch up.
52 BufferView * perv(BufferView const & bv)
53 {
54         return const_cast<BufferView *>(&bv);
55 }
56
57 /**
58  * A class used for painting an individual row of text.
59  */
60 class RowPainter {
61 public:
62         /// initialise painter
63         RowPainter(BufferView const & bv, LyXText const & text,
64                 ParagraphList::iterator pit,
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(double 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         double xo_;
121         int yo_;
122         double x_;
123         int y_;
124         int width_;
125         double separator_;
126         double hfill_;
127         double label_hfill_;
128 };
129
130
131 RowPainter::RowPainter(BufferView const & bv, LyXText const & text,
132      ParagraphList::iterator pit, RowList::iterator rit,
133      int y_offset, int x_offset, int y)
134         : bv_(bv), pain_(bv_.painter()), text_(text), row_(rit),
135           pit_(pit), 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(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(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(pit_, *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         inset->draw(pi, int(x_), yo_ + row_->baseline());
185         x_ += inset->width();
186 }
187
188
189 void RowPainter::paintHebrewComposeChar(pos_type & vpos)
190 {
191         pos_type pos = text_.vis2log(vpos);
192
193         string str;
194
195         // first char
196         char c = pit_->getChar(pos);
197         str += c;
198         ++vpos;
199
200         LyXFont const & font = getFont(pos);
201         int const width = font_metrics::width(c, font);
202         int dx = 0;
203
204         for (pos_type i = pos - 1; i >= 0; --i) {
205                 c = pit_->getChar(i);
206                 if (!Encodings::IsComposeChar_hebrew(c)) {
207                         if (IsPrintableNonspace(c)) {
208                                 int const width2 =
209                                         singleWidth(i, c);
210                                 // dalet / resh
211                                 dx = (c == 'ø' || c == 'ã')
212                                         ? width2 - width
213                                         : (width2 - width) / 2;
214                         }
215                         break;
216                 }
217         }
218
219         // Draw nikud
220         pain_.text(int(x_) + dx, yo_ + row_->baseline(), str, font);
221 }
222
223
224 void RowPainter::paintArabicComposeChar(pos_type & vpos)
225 {
226         pos_type pos = text_.vis2log(vpos);
227         string str;
228
229         // first char
230         char c = pit_->getChar(pos);
231         c = transformChar(c, pos);
232         str +=c;
233         ++vpos;
234
235         LyXFont const & font = getFont(pos);
236         int const width = font_metrics::width(c, font);
237         int dx = 0;
238
239         for (pos_type i = pos - 1; i >= 0; --i) {
240                 c = pit_->getChar(i);
241                 if (!Encodings::IsComposeChar_arabic(c)) {
242                         if (IsPrintableNonspace(c)) {
243                                 int const width2 =
244                                         singleWidth(i, c);
245                                 dx = (width2 - width) / 2;
246                         }
247                         break;
248                 }
249         }
250         // Draw nikud
251         pain_.text(int(x_) + dx, yo_ + row_->baseline(), str, font);
252 }
253
254
255 void RowPainter::paintChars(pos_type & vpos, bool hebrew, bool arabic)
256 {
257         pos_type pos = text_.vis2log(vpos);
258         pos_type const last = lastPrintablePos(text_, pit_, row_);
259         LyXFont orig_font = getFont(pos);
260
261         // first character
262         string str;
263         str += pit_->getChar(pos);
264         if (arabic) {
265                 unsigned char c = str[0];
266                 str[0] = transformChar(c, pos);
267         }
268
269         bool prev_struckout(isDeletedText(*pit_, pos));
270         bool prev_newtext(isInsertedText(*pit_, pos));
271
272         ++vpos;
273
274         // collect as much similar chars as we can
275         while (vpos <= last && (pos = text_.vis2log(vpos)) >= 0) {
276                 char c = pit_->getChar(pos);
277
278                 if (!IsPrintableNonspace(c))
279                         break;
280
281                 if (prev_struckout != isDeletedText(*pit_, pos))
282                         break;
283
284                 if (prev_newtext != isInsertedText(*pit_, pos))
285                         break;
286
287                 if (arabic && Encodings::IsComposeChar_arabic(c))
288                         break;
289                 if (hebrew && Encodings::IsComposeChar_hebrew(c))
290                         break;
291
292                 if (orig_font != getFont(pos))
293                         break;
294
295                 if (arabic)
296                         c = transformChar(c, pos);
297                 str += c;
298                 ++vpos;
299         }
300
301         if (prev_struckout) {
302                 orig_font.setColor(LColor::strikeout);
303         } else if (prev_newtext) {
304                 orig_font.setColor(LColor::newtext);
305         }
306
307         // Draw text and set the new x position
308         //lyxerr << "paint row: yo_ " << yo_ << " baseline: " << row_->baseline()
309         //      << "\n";
310         pain_.text(int(x_), yo_ + row_->baseline(), str, orig_font);
311         x_ += font_metrics::width(str, orig_font);
312 }
313
314
315 void RowPainter::paintForeignMark(double orig_x, LyXFont const & orig_font)
316 {
317         if (!lyxrc.mark_foreign_language)
318                 return;
319         if (orig_font.language() == latex_language)
320                 return;
321         if (orig_font.language() == bv_.buffer()->params.language)
322                 return;
323
324         int const y = yo_ + row_->baseline() + 1;
325         pain_.line(int(orig_x), y, int(x_), y, LColor::language);
326 }
327
328
329 void RowPainter::paintFromPos(pos_type & vpos)
330 {
331         pos_type const pos = text_.vis2log(vpos);
332
333         LyXFont const & orig_font = getFont(pos);
334
335         double const orig_x = x_;
336
337         char const c = pit_->getChar(pos);
338
339         if (IsInsetChar(c)) {
340                 paintInset(pos);
341                 ++vpos;
342                 paintForeignMark(orig_x, orig_font);
343                 return;
344         }
345
346         // usual characters, no insets
347
348         // special case languages
349         bool const hebrew = (orig_font.language()->lang() == "hebrew");
350         bool const arabic =
351                 orig_font.language()->lang() == "arabic" &&
352                 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
353                 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
354
355         // draw as many chars as we can
356         if ((!hebrew && !arabic)
357                 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
358                 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
359                 paintChars(vpos, hebrew, arabic);
360         } else if (hebrew) {
361                 paintHebrewComposeChar(vpos);
362         } else if (arabic) {
363                 paintArabicComposeChar(vpos);
364         }
365
366         paintForeignMark(orig_x, orig_font);
367
368         return;
369 }
370
371
372 void RowPainter::paintBackground()
373 {
374         int const x = int(xo_);
375         int const y = yo_ < 0 ? 0 : yo_;
376         int const h = yo_ < 0 ? row_->height() + yo_ : row_->height();
377         pain_.fillRectangle(x, y, width_, h, text_.backgroundColor());
378 }
379
380
381 void RowPainter::paintSelection()
382 {
383         bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params);
384
385         // the current selection
386         int const startx = text_.selection.start.x();
387         int const endx = text_.selection.end.x();
388         int const starty = text_.selection.start.y();
389         int const endy = text_.selection.end.y();
390         RowList::iterator startrow = text_.getRow(text_.selection.start);
391         RowList::iterator endrow = text_.getRow(text_.selection.end);
392
393         if (text_.bidi_same_direction) {
394                 int x;
395                 int y = yo_;
396                 int w;
397                 int h = row_->height();
398
399                 if (startrow == row_ && endrow == row_) {
400                         if (startx < endx) {
401                                 x = int(xo_) + startx;
402                                 w = endx - startx;
403                                 pain_.fillRectangle(x, y, w, h, LColor::selection);
404                         } else {
405                                 x = int(xo_) + endx;
406                                 w = startx - endx;
407                                 pain_.fillRectangle(x, y, w, h, LColor::selection);
408                         }
409                 } else if (startrow == row_) {
410                         int const x = is_rtl ? int(xo_) : int(xo_ + startx);
411                         int const w = is_rtl ? startx : (width_ - startx);
412                         pain_.fillRectangle(x, y, w, h, LColor::selection);
413                 } else if (endrow == row_) {
414                         int const x = is_rtl ? int(xo_ + endx) : int(xo_);
415                         int const w = is_rtl ? (width_ - endx) : endx;
416                         pain_.fillRectangle(x, y, w, h, LColor::selection);
417                 } else if (y_ > starty && y_ < endy) {
418                         pain_.fillRectangle(int(xo_), y, width_, h, LColor::selection);
419                 }
420                 return;
421         } else if (startrow != row_ && endrow != row_) {
422                 if (y_ > starty && y_ < endy) {
423                         int w = width_;
424                         int h = row_->height();
425                         pain_.fillRectangle(int(xo_), yo_, w, h, LColor::selection);
426                 }
427                 return;
428         }
429
430         if ((startrow != row_ && !is_rtl) || (endrow != row_ && is_rtl))
431                 pain_.fillRectangle(int(xo_), yo_,
432                         int(x_), row_->height(), LColor::selection);
433
434         pos_type const body_pos = pit_->beginningOfBody();
435         pos_type const last = lastPrintablePos(text_, pit_, row_);
436         double tmpx = x_;
437
438         for (pos_type vpos = row_->pos(); vpos <= last; ++vpos)  {
439                 pos_type pos = text_.vis2log(vpos);
440                 double 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_, pit_, 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(int(xo_ + 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_, pit_, row_);
488
489         if (!pit_->isChanged(start, end))
490                 return;
491
492         int const height = (row_ == text_.lastRow())
493                 ? row_->baseline()
494                 : row_->height() + boost::next(row_)->top_of_text();
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_.firstRow()) {
527                 ParagraphList::iterator pit2 = pit_;
528                 if (row_ == text_.beginRow(pit2))
529                         --pit2;
530                 prev_depth = pit2->getDepth();
531         }
532
533         Paragraph::depth_type next_depth = 0;
534         if (row_ != text_.lastRow()) {
535                 ParagraphList::iterator pit2 = pit_;
536                 if (boost::next(row_) == text_.endRow(pit2))
537                         ++pit2;
538                 next_depth = pit2->getDepth();
539         }
540
541         for (Paragraph::depth_type i = 1; i <= depth; ++i) {
542                 int const w = PAPER_MARGIN / 5;
543                 int x = int(w * i + xo_);
544                 // only consider the changebar space if we're drawing outer left
545                 if (!xo_)
546                         x += CHANGEBAR_MARGIN;
547                 int const h = yo_ + row_->height() - 1 - (i - next_depth - 1) * 3;
548
549                 pain_.line(x, yo_, x, h, LColor::depthbar);
550
551                 if (i > prev_depth)
552                         pain_.fillRectangle(x, yo_, w, 2, LColor::depthbar);
553                 if (i > next_depth)
554                         pain_.fillRectangle(x, h, w, 2, LColor::depthbar);
555         }
556 }
557
558
559 int RowPainter::paintLengthMarker(string const & prefix, VSpace const & vsp, int start)
560 {
561         if (vsp.kind() == VSpace::NONE)
562                 return 0;
563
564         int const arrow_size = 4;
565         int const size = getLengthMarkerHeight(bv_, vsp);
566         int const end = start + size;
567
568         // the label to display (if any)
569         string str;
570         // y-values for top arrow
571         int ty1, ty2;
572         // y-values for bottom arrow
573         int by1, by2;
574
575         str = prefix + " (" + vsp.asLyXCommand() + ")";
576
577         if (vsp.kind() == VSpace::VFILL) {
578                 ty1 = ty2 = start;
579                 by1 = by2 = end;
580         } else {
581                 // adding or removing space
582                 bool const added = vsp.kind() != VSpace::LENGTH ||
583                                    vsp.length().len().value() > 0.0;
584                 ty1 = added ? (start + arrow_size) : start;
585                 ty2 = added ? start : (start + arrow_size);
586                 by1 = added ? (end - arrow_size) : end;
587                 by2 = added ? end : (end - arrow_size);
588         }
589
590         int const leftx = int(xo_) + leftMargin();
591         int const midx = leftx + arrow_size;
592         int const rightx = midx + arrow_size;
593
594         // first the string
595         int w = 0;
596         int a = 0;
597         int d = 0;
598
599         LyXFont font;
600         font.setColor(LColor::added_space).decSize().decSize();
601         font_metrics::rectText(str, font, w, a, d);
602
603         pain_.rectText(leftx + 2 * arrow_size + 5,
604                          start + ((end - start) / 2) + d,
605                          str, font);
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);
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);
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->labeltype == LABEL_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 = lastPrintablePos(text_, 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(text_, 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
1005         // FIXME: must be a cleaner way here. Aren't these calculations
1006         // belonging to row metrics ?
1007         text_.prepareToPrint(pit_, row_, x_, separator_, hfill_, 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(text_, pit_, row_))
1036                 paintLast();
1037
1038         // paint text
1039         paintText();
1040 }
1041
1042
1043 } // namespace anon
1044
1045
1046 int getLengthMarkerHeight(BufferView const & bv, VSpace const & vsp)
1047 {
1048         if (vsp.kind() == VSpace::NONE)
1049                 return 0;
1050
1051         int const arrow_size = 4;
1052         int const space_size = vsp.inPixels(bv);
1053
1054         LyXFont font;
1055         font.decSize();
1056         int const min_size = max(3 * arrow_size,
1057                 font_metrics::maxAscent(font)
1058                 + font_metrics::maxDescent(font));
1059
1060         if (vsp.length().len().value() < 0.0)
1061                 return min_size;
1062         else
1063                 return max(min_size, space_size);
1064 }
1065
1066
1067 void paintRow(BufferView const & bv, LyXText const & text,
1068         ParagraphList::iterator pit,
1069         RowList::iterator rit, int y_offset, int x_offset, int y)
1070 {
1071         RowPainter painter(bv, text, pit, rit, y_offset, x_offset, y);
1072         painter.paint();
1073 }
1074
1075
1076 int paintRows(BufferView const & bv, LyXText const & text,
1077         RowList::iterator rit, int xo, int y, int yf, int y2, int yo)
1078 {
1079         // fix up missing metrics() call for main LyXText
1080         // calling metrics() directly is (a) slow and (b) crashs
1081         if (&text == bv.text) {
1082 #if 1
1083                 // make sure all insets are updated
1084                 ParagraphList::iterator pit = text.ownerParagraphs().begin();
1085                 ParagraphList::iterator end = text.ownerParagraphs().end();
1086
1087                 // compute inset metrics
1088                 for (; pit != end; ++pit) {
1089                         InsetList & insetList = pit->insetlist;
1090                         InsetList::iterator ii = insetList.begin();
1091                         InsetList::iterator iend = insetList.end();
1092                         for (; ii != iend; ++ii) {
1093                                 Dimension dim;
1094                                 LyXFont font;
1095                                 MetricsInfo mi(perv(bv), font, text.workWidth());
1096                                 ii->inset->metrics(mi, dim);
1097                         }
1098                 }
1099 #else
1100                 LyXFont font;
1101                 Dimension dim;
1102                 MetricsInfo mi(perv(bv), font, text.workWidth());
1103                 const_cast<LyXText&>(text).metrics(mi, dim);
1104 #endif
1105         }
1106
1107         //lyxerr << "text: " << &text << " y: " << y << " yf: " << yf
1108         //      << " y2: " << y2 << " yo: " << yo << "\n";
1109         int yy = yf - y;
1110         RowList::iterator end = text.endRow();
1111         while (rit != end && yy + y < y2) {
1112                 //const_cast<LyXText &>(text).setHeightOfRow(rit);
1113                 ParagraphList::iterator pit = text.getPar(rit);
1114                 paintRow(bv, text, pit, rit, y + yo, xo, y + text.top_y());
1115                 y += rit->height();
1116                 rit = text.nextRow(rit);
1117         }
1118         return y;
1119 }
1120