]> git.lyx.org Git - lyx.git/blob - src/rowpainter.C
make textWidth access a bit simpler
[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 "BufferView.h"
25 #include "buffer.h"
26 #include "gettext.h"
27 #include "language.h"
28 #include "encoding.h"
29 #include "lyxtext.h"
30 #include "lyxrow.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                 RowList::iterator rit, int y_offset, int x_offset, int y);
65
66         /// do the painting
67         void paint();
68 private:
69         // paint various parts
70         void paintBackground();
71         void paintSelection();
72         void paintAppendix();
73         void paintDepthBar();
74         void paintChangeBar();
75         void paintFirst();
76         void paintLast();
77         void paintForeignMark(int orig_x, LyXFont const & orig_font);
78         void paintHebrewComposeChar(lyx::pos_type & vpos);
79         void paintArabicComposeChar(lyx::pos_type & vpos);
80         void paintChars(lyx::pos_type & vpos, bool hebrew, bool arabic);
81         int paintPageBreak(string const & label, int y);
82         int paintAppendixStart(int y);
83         int paintLengthMarker(string const & prefix, VSpace const & vsp, int start);
84         void paintText();
85         void paintFromPos(lyx::pos_type & vpos);
86         void paintInset(lyx::pos_type const pos);
87
88         /// return left margin
89         int leftMargin() const;
90
91         /// return the font at the given pos
92         LyXFont const getFont(lyx::pos_type pos) const;
93
94         /// return the label font for this row
95         LyXFont const getLabelFont() const;
96
97         char const transformChar(char c, lyx::pos_type pos) const;
98
99         /// return pixel width for the given pos
100         int singleWidth(lyx::pos_type pos) const;
101         int singleWidth(lyx::pos_type pos, char c) const;
102
103         /// bufferview to paint on
104         BufferView const & bv_;
105
106         /// Painter to use
107         Painter & pain_;
108
109         /// LyXText for the row
110         LyXText const & text_;
111
112         /// The row to paint
113         RowList::iterator row_;
114
115         /// Row's paragraph
116         mutable ParagraphList::iterator  pit_;
117
118         // Looks ugly - is
119         int xo_;
120         int yo_;
121         int x_;
122         int y_;
123         int width_;
124         int separator_;
125         int hfill_;
126         int label_hfill_;
127 };
128
129 RowPainter::RowPainter(BufferView const & bv, LyXText const & text,
130      RowList::iterator rit,
131      int y_offset, int x_offset, int y)
132         : bv_(bv), pain_(bv_.painter()), text_(text), row_(rit),
133           pit_(rit->par()), 
134                 xo_(x_offset), yo_(y_offset), y_(y)
135 {}
136
137
138 /// "temporary"
139 LyXFont const RowPainter::getFont(pos_type pos) const
140 {
141         return text_.getFont(bv_.buffer(), pit_, pos);
142 }
143
144
145 int RowPainter::singleWidth(lyx::pos_type pos) const
146 {
147         return text_.singleWidth(pit_, pos);
148 }
149
150
151 int RowPainter::singleWidth(lyx::pos_type pos, char c) const
152 {
153         return text_.singleWidth(pit_, pos, c);
154 }
155
156
157 LyXFont const RowPainter::getLabelFont() const
158 {
159         return text_.getLabelFont(bv_.buffer(), pit_);
160 }
161
162
163 char const RowPainter::transformChar(char c, lyx::pos_type pos) const
164 {
165         return text_.transformChar(c, *pit_, pos);
166 }
167
168
169 int RowPainter::leftMargin() const
170 {
171         return text_.leftMargin(*row_);
172 }
173
174
175 void RowPainter::paintInset(pos_type const pos)
176 {
177         Inset * inset = const_cast<Inset*>(pit_->getInset(pos));
178
179         Assert(inset);
180
181         MetricsInfo mi(perv(bv_), getFont(pos), text_.workWidth());
182         Dimension dim; 
183         inset->metrics(mi, dim);
184
185         PainterInfo pi(perv(bv_));
186         pi.base.font = getFont(pos);
187 #warning metrics?
188         inset->draw(pi, int(x_), yo_ + row_->baseline());
189         x_ += dim.wid;
190 }
191
192
193 void RowPainter::paintHebrewComposeChar(pos_type & vpos)
194 {
195         pos_type pos = text_.vis2log(vpos);
196
197         string str;
198
199         // first char
200         char c = pit_->getChar(pos);
201         str += c;
202         ++vpos;
203
204         LyXFont const & font = getFont(pos);
205         int const width = font_metrics::width(c, font);
206         int dx = 0;
207
208         for (pos_type i = pos - 1; i >= 0; --i) {
209                 c = pit_->getChar(i);
210                 if (!Encodings::IsComposeChar_hebrew(c)) {
211                         if (IsPrintableNonspace(c)) {
212                                 int const width2 =
213                                         singleWidth(i, c);
214                                 // dalet / resh
215                                 dx = (c == 'ø' || c == 'ã')
216                                         ? width2 - width
217                                         : (width2 - width) / 2;
218                         }
219                         break;
220                 }
221         }
222
223         // Draw nikud
224         pain_.text(int(x_) + dx, yo_ + row_->baseline(), str, font);
225 }
226
227
228 void RowPainter::paintArabicComposeChar(pos_type & vpos)
229 {
230         pos_type pos = text_.vis2log(vpos);
231         string str;
232
233         // first char
234         char c = pit_->getChar(pos);
235         c = transformChar(c, pos);
236         str +=c;
237         ++vpos;
238
239         LyXFont const & font = getFont(pos);
240         int const width = font_metrics::width(c, font);
241         int dx = 0;
242
243         for (pos_type i = pos - 1; i >= 0; --i) {
244                 c = pit_->getChar(i);
245                 if (!Encodings::IsComposeChar_arabic(c)) {
246                         if (IsPrintableNonspace(c)) {
247                                 int const width2 =
248                                         singleWidth(i, c);
249                                 dx = (width2 - width) / 2;
250                         }
251                         break;
252                 }
253         }
254         // Draw nikud
255         pain_.text(int(x_) + dx, yo_ + row_->baseline(), str, font);
256 }
257
258
259 void RowPainter::paintChars(pos_type & vpos, bool hebrew, bool arabic)
260 {
261         pos_type pos = text_.vis2log(vpos);
262         pos_type const last = lastPrintablePos(text_, row_);
263         LyXFont orig_font = getFont(pos);
264
265         // first character
266         string str;
267         str += pit_->getChar(pos);
268         if (arabic) {
269                 unsigned char c = str[0];
270                 str[0] = transformChar(c, pos);
271         }
272
273         bool prev_struckout(isDeletedText(*pit_, pos));
274         bool prev_newtext(isInsertedText(*pit_, pos));
275
276         ++vpos;
277
278         // collect as much similar chars as we can
279         while (vpos <= last && (pos = text_.vis2log(vpos)) >= 0) {
280                 char c = pit_->getChar(pos);
281
282                 if (!IsPrintableNonspace(c))
283                         break;
284
285                 if (prev_struckout != isDeletedText(*pit_, pos))
286                         break;
287
288                 if (prev_newtext != isInsertedText(*pit_, pos))
289                         break;
290
291                 if (arabic && Encodings::IsComposeChar_arabic(c))
292                         break;
293                 if (hebrew && Encodings::IsComposeChar_hebrew(c))
294                         break;
295
296                 if (orig_font != getFont(pos))
297                         break;
298
299                 if (arabic)
300                         c = transformChar(c, pos);
301                 str += c;
302                 ++vpos;
303         }
304
305         if (prev_struckout) {
306                 orig_font.setColor(LColor::strikeout);
307         } else if (prev_newtext) {
308                 orig_font.setColor(LColor::newtext);
309         }
310
311         // Draw text and set the new x position
312         pain_.text(int(x_), yo_ + row_->baseline(), str, orig_font);
313         x_ += font_metrics::width(str, orig_font);
314 }
315
316
317 void RowPainter::paintForeignMark(int orig_x, LyXFont const & orig_font)
318 {
319         if (!lyxrc.mark_foreign_language)
320                 return;
321         if (orig_font.language() == latex_language)
322                 return;
323         if (orig_font.language() == bv_.buffer()->params.language)
324                 return;
325
326         int const y = yo_ + row_->baseline() + 1;
327         pain_.line(int(orig_x), y, int(x_), y, LColor::language);
328 }
329
330
331 void RowPainter::paintFromPos(pos_type & vpos)
332 {
333         pos_type const pos = text_.vis2log(vpos);
334
335         LyXFont const & orig_font = getFont(pos);
336
337         int const orig_x = x_;
338
339         char const c = pit_->getChar(pos);
340
341         if (IsInsetChar(c)) {
342                 paintInset(pos);
343                 ++vpos;
344                 paintForeignMark(orig_x, orig_font);
345                 return;
346         }
347
348         // usual characters, no insets
349
350         // special case languages
351         bool const hebrew = (orig_font.language()->lang() == "hebrew");
352         bool const arabic =
353                 orig_font.language()->lang() == "arabic" &&
354                 (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
355                 lyxrc.font_norm_type == LyXRC::ISO_10646_1);
356
357         // draw as many chars as we can
358         if ((!hebrew && !arabic)
359                 || (hebrew && !Encodings::IsComposeChar_hebrew(c))
360                 || (arabic && !Encodings::IsComposeChar_arabic(c))) {
361                 paintChars(vpos, hebrew, arabic);
362         } else if (hebrew) {
363                 paintHebrewComposeChar(vpos);
364         } else if (arabic) {
365                 paintArabicComposeChar(vpos);
366         }
367
368         paintForeignMark(orig_x, orig_font);
369
370         return;
371 }
372
373
374 void RowPainter::paintBackground()
375 {
376         int const x = xo_;
377         int const y = yo_ < 0 ? 0 : yo_;
378         int const h = yo_ < 0 ? row_->height() + yo_ : row_->height();
379         pain_.fillRectangle(x, y, width_, h, text_.backgroundColor());
380 }
381
382
383 void RowPainter::paintSelection()
384 {
385         bool const is_rtl = pit_->isRightToLeftPar(bv_.buffer()->params);
386
387         // the current selection
388         int const startx = text_.selection.start.x();
389         int const endx = text_.selection.end.x();
390         int const starty = text_.selection.start.y();
391         int const endy = text_.selection.end.y();
392         RowList::iterator startrow = text_.getRow(text_.selection.start);
393         RowList::iterator endrow = text_.getRow(text_.selection.end);
394
395         if (text_.bidi_same_direction) {
396                 int x;
397                 int y = yo_;
398                 int w;
399                 int h = row_->height();
400
401                 if (startrow == row_ && endrow == row_) {
402                         if (startx < endx) {
403                                 x = xo_ + startx;
404                                 w = endx - startx;
405                                 pain_.fillRectangle(x, y, w, h, LColor::selection);
406                         } else {
407                                 x = xo_ + endx;
408                                 w = startx - endx;
409                                 pain_.fillRectangle(x, y, w, h, LColor::selection);
410                         }
411                 } else if (startrow == row_) {
412                         int const x = (is_rtl) ? xo_ : (xo_ + startx);
413                         int const w = (is_rtl) ? startx : (width_ - startx);
414                         pain_.fillRectangle(x, y, w, h, LColor::selection);
415                 } else if (endrow == row_) {
416                         int const x = (is_rtl) ? (xo_ + endx) : xo_;
417                         int const w = (is_rtl) ? (width_ - endx) : endx;
418                         pain_.fillRectangle(x, y, w, h, LColor::selection);
419                 } else if (y_ > starty && y_ < endy) {
420                         pain_.fillRectangle(xo_, y, width_, h, LColor::selection);
421                 }
422                 return;
423         } else if (startrow != row_ && endrow != row_) {
424                 if (y_ > starty && y_ < endy) {
425                         int w = width_;
426                         int h = row_->height();
427                         pain_.fillRectangle(xo_, yo_, w, h, LColor::selection);
428                 }
429                 return;
430         }
431
432         if ((startrow != row_ && !is_rtl) || (endrow != row_ && is_rtl))
433                 pain_.fillRectangle(xo_, yo_, int(x_), row_->height(), LColor::selection);
434
435         pos_type const body_pos = pit_->beginningOfBody();
436         pos_type const last = lastPrintablePos(text_, row_);
437         int tmpx = x_;
438
439         for (pos_type vpos = row_->pos(); vpos <= last; ++vpos)  {
440                 pos_type pos = text_.vis2log(vpos);
441                 int 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(text_, 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(xo_ + int(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(text_, row_);
489
490         if (!pit_->isChanged(start, end))
491                 return;
492
493         int const height = (boost::next(row_) != text_.rows().end()
494                 ? row_->height() + boost::next(row_)->top_of_text()
495                 : row_->baseline());
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_.rows().begin())
528                 prev_depth = boost::prior(row_)->par()->getDepth();
529         Paragraph::depth_type next_depth = 0;
530
531         RowList::iterator next_row = boost::next(row_);
532         if (next_row != text_.rows().end())
533                 next_depth = next_row->par()->getDepth();
534
535         for (Paragraph::depth_type i = 1; i <= depth; ++i) {
536                 int x = (PAPER_MARGIN / 5) * i + xo_;
537                 // only consider the changebar space if we're drawing outer left
538                 if (!xo_)
539                         x += CHANGEBAR_MARGIN;
540                 int const h = yo_ + row_->height() - 1 - (i - next_depth - 1) * 3;
541
542                 pain_.line(x, yo_, x, h, LColor::depthbar);
543
544                 int const w = PAPER_MARGIN / 5;
545
546                 if (i > prev_depth) {
547                         pain_.fillRectangle(x, yo_, w, 2, LColor::depthbar);
548                 }
549                 if (i > next_depth) {
550                         pain_.fillRectangle(x, h, w, 2, LColor::depthbar);
551                 }
552         }
553 }
554
555
556 int RowPainter::paintLengthMarker(string const & prefix, VSpace const & vsp, int start)
557 {
558         if (vsp.kind() == VSpace::NONE)
559                 return 0;
560
561         int const arrow_size = 4;
562         int const size = getLengthMarkerHeight(bv_, vsp);
563         int const end = start + size;
564
565         // the label to display (if any)
566         string str;
567         // y-values for top arrow
568         int ty1, ty2;
569         // y-values for bottom arrow
570         int by1, by2;
571
572         str = prefix + " (" + vsp.asLyXCommand() + ")";
573
574         if (vsp.kind() == VSpace::VFILL) {
575                 ty1 = ty2 = start;
576                 by1 = by2 = end;
577         } else {
578                 // adding or removing space
579                 bool const added = vsp.kind() != VSpace::LENGTH ||
580                                    vsp.length().len().value() > 0.0;
581                 ty1 = added ? (start + arrow_size) : start;
582                 ty2 = added ? start : (start + arrow_size);
583                 by1 = added ? (end - arrow_size) : end;
584                 by2 = added ? end : (end - arrow_size);
585         }
586
587         int const leftx = xo_ + leftMargin();
588         int const midx = leftx + arrow_size;
589         int const rightx = midx + arrow_size;
590
591         // first the string
592         int w = 0;
593         int a = 0;
594         int d = 0;
595
596         LyXFont font;
597         font.setColor(LColor::added_space).decSize().decSize();
598         font_metrics::rectText(str, font, w, a, d);
599
600         pain_.rectText(leftx + 2 * arrow_size + 5,
601                          start + ((end - start) / 2) + d,
602                          str, font);
603
604         // top arrow
605         pain_.line(leftx, ty1, midx, ty2, LColor::added_space);
606         pain_.line(midx, ty2, rightx, ty1, LColor::added_space);
607
608         // bottom arrow
609         pain_.line(leftx, by1, midx, by2, LColor::added_space);
610         pain_.line(midx, by2, rightx, by1, LColor::added_space);
611
612         // joining line
613         pain_.line(midx, ty2, midx, by2, LColor::added_space);
614
615         return size;
616 }
617
618
619 int RowPainter::paintPageBreak(string const & label, int y)
620 {
621         LyXFont pb_font;
622         pb_font.setColor(LColor::pagebreak).decSize();
623
624         int w = 0;
625         int a = 0;
626         int d = 0;
627         font_metrics::rectText(label, pb_font, w, a, d);
628
629         int const text_start = xo_ + ((width_ - w) / 2);
630         int const text_end = text_start + w;
631
632         pain_.rectText(text_start, y + d, label, pb_font);
633
634         pain_.line(xo_, y, text_start, y,
635                 LColor::pagebreak, Painter::line_onoffdash);
636         pain_.line(text_end, y, xo_ + width_, y,
637                 LColor::pagebreak, Painter::line_onoffdash);
638
639         return 3 * defaultRowHeight();
640 }
641
642
643 int RowPainter::paintAppendixStart(int y)
644 {
645         LyXFont pb_font;
646         pb_font.setColor(LColor::appendix).decSize();
647
648         string const label = _("Appendix");
649         int w = 0;
650         int a = 0;
651         int d = 0;
652         font_metrics::rectText(label, pb_font, w, a, d);
653
654         int const text_start = xo_ + ((width_ - w) / 2);
655         int const text_end = text_start + w;
656
657         pain_.rectText(text_start, y + d, label, pb_font);
658
659         pain_.line(xo_ + 1, y, text_start, y, LColor::appendix);
660         pain_.line(text_end, y, xo_ + width_ - 2, y, LColor::appendix);
661
662         return 3 * defaultRowHeight();
663 }
664
665
666 void RowPainter::paintFirst()
667 {
668         ParagraphParameters const & parparams = pit_->params();
669
670         int y_top = 0;
671
672         // start of appendix?
673         if (parparams.startOfAppendix()) {
674                 y_top += paintAppendixStart(yo_ + y_top + 2 * defaultRowHeight());
675         }
676
677         // the top margin
678         if (row_ == text_.rows().begin() && !text_.isInInset())
679                 y_top += PAPER_MARGIN;
680
681         // draw a top pagebreak
682         if (parparams.pagebreakTop()) {
683                 y_top += paintPageBreak(_("Page Break (top)"),
684                         yo_ + y_top + 2 * defaultRowHeight());
685         }
686
687         // draw the additional space if needed:
688         y_top += paintLengthMarker(_("Space above"), parparams.spaceTop(),
689                         yo_ + y_top);
690
691         Buffer const * buffer = bv_.buffer();
692
693         LyXLayout_ptr const & layout = pit_->layout();
694
695         if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
696                 if (pit_ != text_.ownerParagraphs().begin()) {
697                         if (layout->latextype == LATEX_PARAGRAPH
698                                 && !pit_->getDepth()) {
699                                 y_top += buffer->params.getDefSkip().inPixels(bv_);
700                         } else {
701                                 LyXLayout_ptr const & playout =
702                                         boost::prior(pit_)->layout();
703                                 if (playout->latextype == LATEX_PARAGRAPH
704                                         && !boost::prior(pit_)->getDepth()) {
705                                         // is it right to use defskip here, too? (AS)
706                                         y_top += buffer->params.getDefSkip().inPixels(bv_);
707                                 }
708                         }
709                 }
710         }
711
712         int const ww = bv_.workWidth();
713
714         // draw a top line
715         if (parparams.lineTop()) {
716                 LyXFont font(LyXFont::ALL_SANE);
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                         int 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(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(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                         int x = x_;
803                         if (layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT) {
804                                 x = ((is_rtl ? leftMargin() : x_)
805                                          + ww - text_.rightMargin(*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(x, yo_ + row_->baseline()
812                                   - 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 (boost::next(row_) == text_.rows().end() && !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                 LyXFont font(LyXFont::ALL_SANE);
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 = static_cast<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                 }
879                 break;
880         }
881         case END_LABEL_STATIC:
882         {
883                 LyXFont font = getLabelFont();
884                 string const & str = pit_->layout()->endlabelstring();
885                 int const x = is_rtl ?
886                         x_ - font_metrics::width(str, font)
887                         : ww - text_.rightMargin(*bv_.buffer(), *row_) - row_->fill();
888                 pain_.text(x, yo_ + row_->baseline(), str, font);
889                 break;
890         }
891         case END_LABEL_NO_LABEL:
892                 break;
893         }
894 }
895
896
897 void RowPainter::paintText()
898 {
899         pos_type const last = lastPrintablePos(text_, row_);
900         pos_type body_pos = pit_->beginningOfBody();
901         if (body_pos > 0 &&
902                 (body_pos - 1 > last ||
903                 !pit_->isLineSeparator(body_pos - 1))) {
904                 body_pos = 0;
905         }
906
907         LyXLayout_ptr const & layout = pit_->layout();
908
909         bool running_strikeout = false;
910         bool is_struckout = false;
911         int last_strikeout_x = 0;
912
913         pos_type vpos = row_->pos();
914         while (vpos <= last) {
915                 if (x_ > bv_.workWidth())
916                         break;
917                 pos_type pos = text_.vis2log(vpos);
918
919                 if (pos >= pit_->size()) {
920                         ++vpos;
921                         continue;
922                 }
923
924                 if (x_ + singleWidth(pos) < 0) {
925                         x_ += singleWidth(pos);
926                         ++vpos;
927                         continue;
928                 }
929
930                 is_struckout = isDeletedText(*pit_, pos);
931
932                 if (is_struckout && !running_strikeout) {
933                         running_strikeout = true;
934                         last_strikeout_x = x_;
935                 }
936
937                 bool const highly_editable_inset = pit_->isInset(pos)
938                         && isHighlyEditableInset(pit_->getInset(pos));
939
940                 // if we reach the end of a struck out range, paint it
941                 // we also don't paint across things like tables
942                 if (running_strikeout && (highly_editable_inset || !is_struckout)) {
943                         int const middle = yo_ + row_->top_of_text()
944                                 + ((row_->baseline() - row_->top_of_text()) / 2);
945                         pain_.line(last_strikeout_x, middle, x_, middle,
946                                 LColor::strikeout, Painter::line_solid, Painter::line_thin);
947                         running_strikeout = false;
948                 }
949
950                 if (body_pos > 0 && pos == body_pos - 1) {
951                         int const lwidth = font_metrics::width(layout->labelsep,
952                                 getLabelFont());
953
954                         x_ += label_hfill_ + lwidth
955                                 - singleWidth(body_pos - 1);
956                 }
957
958                 if (pit_->isHfill(pos)) {
959                         x_ += 1;
960
961                         int const y0 = yo_ + row_->baseline();
962                         int const y1 = y0 - defaultRowHeight() / 2;
963
964                         pain_.line(x_, y1, x_, y0,
965                                      LColor::added_space);
966
967                         if (hfillExpansion(text_, row_, pos)) {
968                                 int const y2 = (y0 + y1) / 2;
969
970                                 if (pos >= body_pos) {
971                                         pain_.line(x_, y2, x_ + hfill_, y2,
972                                                   LColor::added_space,
973                                                   Painter::line_onoffdash);
974                                         x_ += hfill_;
975                                 } else {
976                                         pain_.line(x_, y2,
977                                                   x_ + label_hfill_, y2,
978                                                   LColor::added_space,
979                                                   Painter::line_onoffdash);
980                                         x_ += label_hfill_;
981                                 }
982                                 pain_.line(x_, y1, x_, y0, LColor::added_space);
983                         }
984                         x_ += 2;
985                         ++vpos;
986                 } else if (pit_->isSeparator(pos)) {
987                         x_ += singleWidth(pos);
988                         if (pos >= body_pos)
989                                 x_ += separator_;
990                         ++vpos;
991                 } else {
992                         paintFromPos(vpos);
993                 }
994         }
995
996         // if we reach the end of a struck out range, paint it
997         if (running_strikeout) {
998                 int const middle = yo_ + row_->top_of_text()
999                         + ((row_->baseline() - row_->top_of_text()) / 2);
1000                 pain_.line(last_strikeout_x, middle, x_, middle,
1001                         LColor::strikeout, Painter::line_solid, Painter::line_thin);
1002                 running_strikeout = false;
1003         }
1004         return;
1005 }
1006
1007
1008 void RowPainter::paint()
1009 {
1010         width_ = text_.workWidth();
1011
1012         // FIXME: must be a cleaner way here. Aren't these calculations
1013         // belonging to row metrics ?
1014         text_.prepareToPrint(row_, x_, separator_, hfill_, label_hfill_);
1015
1016         // FIXME: what is this fixing ?
1017         if (text_.isInInset() && (x_ < 0))
1018                 x_ = 0;
1019         x_ += xo_;
1020
1021         // If we're *not* at the top-level of rows, then the
1022         // background has already been cleared.
1023         if (&text_ == bv_.text)
1024                 paintBackground();
1025
1026         // paint the selection background
1027         if (text_.selection.set()) {
1028                 paintSelection();
1029         }
1030
1031         // vertical lines for appendix
1032         paintAppendix();
1033
1034         // environment depth brackets
1035         paintDepthBar();
1036
1037         // changebar
1038         paintChangeBar();
1039
1040         if (row_->isParStart()) {
1041                 paintFirst();
1042         }
1043
1044         if (isParEnd(text_, row_)) {
1045                 paintLast();
1046         }
1047
1048         // paint text
1049         paintText();
1050 }
1051
1052
1053 } // namespace anon
1054
1055
1056 int getLengthMarkerHeight(BufferView const & bv, VSpace const & vsp)
1057 {
1058         if (vsp.kind() == VSpace::NONE)
1059                 return 0;
1060
1061         int const arrow_size = 4;
1062         int const space_size = vsp.inPixels(bv);
1063
1064         LyXFont font;
1065         font.decSize();
1066         int const min_size = max(3 * arrow_size,
1067                 font_metrics::maxAscent(font)
1068                 + font_metrics::maxDescent(font));
1069
1070         if (vsp.length().len().value() < 0.0)
1071                 return min_size;
1072         else
1073                 return max(min_size, space_size);
1074 }
1075
1076
1077 void paintRows(BufferView const & bv, LyXText const & text,
1078         RowList::iterator rit, int y_offset, int x_offset, int y)
1079 {
1080         RowPainter painter(bv, text, rit, y_offset, x_offset, y);
1081         painter.paint();
1082 }