]> git.lyx.org Git - lyx.git/blob - src/RowPainter.cpp
Copy modules into the Advanced F&R pane as well.
[lyx.git] / src / RowPainter.cpp
1 /**
2  * \file RowPainter.cpp
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 #include <algorithm>
14
15 #include "RowPainter.h"
16
17 #include "Buffer.h"
18 #include "CoordCache.h"
19 #include "Cursor.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
22 #include "Changes.h"
23 #include "Language.h"
24 #include "Layout.h"
25 #include "LyXRC.h"
26 #include "Row.h"
27 #include "MetricsInfo.h"
28 #include "Paragraph.h"
29 #include "ParagraphParameters.h"
30 #include "TextMetrics.h"
31 #include "VSpace.h"
32
33 #include "frontends/FontMetrics.h"
34 #include "frontends/Painter.h"
35
36 #include "insets/InsetText.h"
37
38 #include "mathed/InsetMath.h"
39
40 #include "support/debug.h"
41 #include "support/gettext.h"
42 #include "support/textutils.h"
43
44 #include "support/lassert.h"
45 #include <boost/crc.hpp>
46
47 #include <stdlib.h>
48
49 using namespace std;
50
51 namespace lyx {
52
53 using frontend::Painter;
54 using frontend::FontMetrics;
55
56
57 RowPainter::RowPainter(PainterInfo & pi,
58         Text const & text, Row const & row, int x, int y)
59         : pi_(pi), text_(text),
60           tm_(pi_.base.bv->textMetrics(&text)),
61           pars_(text.paragraphs()),
62           row_(row), par_(text.paragraphs()[row.pit()]),
63           change_(pi_.change_),
64           xo_(x), yo_(y)
65 {
66         x_ = row_.left_margin + xo_;
67
68         //lyxerr << "RowPainter: x: " << x_ << " xo: " << xo_ << " yo: " << yo_ << endl;
69         //row_.dump();
70
71         LBUFERR(row.pit() >= 0);
72         LBUFERR(row.pit() < int(text.paragraphs().size()));
73 }
74
75
76 FontInfo RowPainter::labelFont() const
77 {
78         FontInfo f = text_.labelFont(par_);
79         // selected text?
80         if ((row_.selection() && row_.begin_margin_sel) || pi_.selected)
81                 f.setPaintColor(Color_selectiontext);
82         return f;
83 }
84
85
86 // If you want to debug inset metrics uncomment the following line:
87 //#define DEBUG_METRICS
88 // This draws green lines around each inset.
89
90
91 void RowPainter::paintInset(Row::Element const & e) const
92 {
93         // Handle selection
94         bool const pi_selected = pi_.selected;
95         Cursor const & cur = pi_.base.bv->cursor();
96         if (cur.selection() && cur.text() == &text_
97                 && cur.normalAnchor().text() == &text_)
98                 pi_.selected = row_.sel_beg <= e.pos && row_.sel_end > e.pos;
99
100         LASSERT(e.inset, return);
101         // Backup full_repaint status because some insets (InsetTabular)
102         // requires a full repaint
103         bool const pi_full_repaint = pi_.full_repaint;
104         bool const pi_do_spellcheck = pi_.do_spellcheck;
105         Change const pi_change = pi_.change_;
106
107         pi_.base.font = e.inset->inheritFont() ? e.font.fontInfo() :
108                 pi_.base.bv->buffer().params().getFont().fontInfo();
109         pi_.ltr_pos = !e.font.isVisibleRightToLeft();
110         pi_.change_ = change_.changed() ? change_ : e.change;
111         pi_.do_spellcheck &= e.inset->allowSpellCheck();
112
113         int const x1 = int(x_);
114         pi_.base.bv->coordCache().insets().add(e.inset, x1, yo_);
115         // insets are painted completely. Recursive
116         // FIXME: it is wrong to completely paint the background
117         // if we want to do single row painting.
118         e.inset->drawBackground(pi_, x1, yo_);
119         e.inset->drawSelection(pi_, x1, yo_);
120         e.inset->draw(pi_, x1, yo_);
121
122         // Restore full_repaint status.
123         pi_.full_repaint = pi_full_repaint;
124         pi_.change_ = pi_change;
125         pi_.do_spellcheck = pi_do_spellcheck;
126         pi_.selected = pi_selected;
127
128 #ifdef DEBUG_METRICS
129         Dimension const & dim = pi_.base.bv->coordCache().insets().dim(e.inset);
130         int const x2 = x1 + dim.wid;
131         int const y1 = yo_ + dim.des;
132         int const y2 = yo_ - dim.asc;
133         pi_.pain.line(x1, y1, x1, y2, Color_green);
134         pi_.pain.line(x1, y1, x2, y1, Color_green);
135         pi_.pain.line(x2, y1, x2, y2, Color_green);
136         pi_.pain.line(x1, y2, x2, y2, Color_green);
137 #endif
138 }
139
140
141 void RowPainter::paintForeignMark(Row::Element const & e) const
142 {
143         Language const * lang = e.font.language();
144         if (!lyxrc.mark_foreign_language)
145                 return;
146         if (lang == latex_language)
147                 return;
148         if (lang == pi_.base.bv->buffer().params().language)
149                 return;
150
151         int const desc = e.inset ? e.dim.descent() : 0;
152         int const y = yo_ + min(3 * pi_.base.solidLineOffset() / 2 + desc,
153                                 row_.descent() - 1);
154         pi_.pain.line(int(x_), y, int(x_ + e.full_width()), y, Color_language,
155                       Painter::line_solid, pi_.base.solidLineThickness());
156 }
157
158
159 void RowPainter::paintMisspelledMark(Row::Element const & e) const
160 {
161         // if changed the misspelled marker gets placed slightly lower than normal
162         // to avoid drawing at the same vertical offset
163         FontMetrics const & fm = theFontMetrics(e.font);
164         int const thickness = max(fm.lineWidth(), 2);
165         int const y = yo_ + pi_.base.solidLineOffset() + pi_.base.solidLineThickness()
166                 + (e.change.changed() ? pi_.base.solidLineThickness() + 1 : 0)
167                 + 1 + thickness / 2;
168
169         //FIXME: this could be computed only once, it is probably not costly.
170         // check for cursor position
171         // don't draw misspelled marker for words at cursor position
172         // we don't want to disturb the process of text editing
173         DocIterator const nw = pi_.base.bv->cursor().newWord();
174         pos_type cpos = -1;
175         if (!nw.empty() && par_.id() == nw.paragraph().id()) {
176                 cpos = nw.pos();
177                 if (cpos > 0 && cpos == par_.size() && !par_.isWordSeparator(cpos-1))
178                         --cpos;
179                 else if (cpos > 0 && par_.isWordSeparator(cpos))
180                         --cpos;
181         }
182
183         pos_type pos = e.pos;
184         while (pos < e.pos + pos_type(e.str.length())) {
185                 if (!par_.isMisspelled(pos)) {
186                         ++pos;
187                         continue;
188                 }
189
190                 FontSpan const & range = par_.getSpellRange(pos);
191
192                 // Skip element which are being edited
193                 if (range.contains(cpos)) {
194                         // the range includes the last element
195                         pos = range.last + 1;
196                         continue;
197                 }
198
199                 FontMetrics const & fm = theFontMetrics(e.font);
200                 int x1 = fm.pos2x(e.str, range.first - e.pos,
201                                   e.isRTL(), e.extra);
202                 int x2 = fm.pos2x(e.str, min(range.last - e.pos + 1,
203                                                                          pos_type(e.str.length())),
204                                                                          e.isRTL(), e.extra);
205                 if (x1 > x2)
206                         swap(x1, x2);
207
208                 pi_.pain.line(int(x_ + x1), y, int(x_ + x2), y,
209                               Color_error,
210                               Painter::line_onoffdash, thickness);
211                 pos = range.last + 1;
212         }
213 }
214
215
216 void RowPainter::paintStringAndSel(Row::Element const & e) const
217 {
218         // at least part of text selected?
219         bool const some_sel = (e.endpos >= row_.sel_beg && e.pos < row_.sel_end)
220                 || pi_.selected;
221         // all the text selected?
222         bool const all_sel = (e.pos >= row_.sel_beg && e.endpos < row_.sel_end)
223                 || pi_.selected;
224
225         if (all_sel || e.change.changed()) {
226                 Font copy = e.font;
227                 Color const col = e.change.changed() ? e.change.color()
228                                                      : Color_selectiontext;
229                 copy.fontInfo().setPaintColor(col);
230                 pi_.pain.text(int(x_), yo_, e.str, copy, e.extra, e.full_width());
231         } else if (!some_sel) {
232                 pi_.pain.text(int(x_), yo_, e.str, e.font, e.extra, e.full_width());
233         } else {
234                 pi_.pain.text(int(x_), yo_, e.str, e.font, Color_selectiontext,
235                               max(row_.sel_beg, e.pos) - e.pos,
236                               min(row_.sel_end, e.endpos) - e.pos,
237                               e.extra, e.full_width());
238         }
239 }
240
241
242 void RowPainter::paintTextDecoration(Row::Element const & e) const
243 {
244         // element selected?
245         bool const sel = (e.pos >= row_.sel_beg && e.endpos <= row_.sel_end)
246                 || pi_.selected;
247         FontInfo copy = e.font.fontInfo();
248         if (sel || e.change.changed()) {
249                 Color const col = e.change.changed() ? e.change.color()
250                                                      : Color_selectiontext;
251                 copy.setPaintColor(col);
252         }
253         pi_.pain.textDecoration(copy, int(x_), yo_, int(e.full_width()));
254 }
255
256
257 void RowPainter::paintChange(Row::Element const & e) const
258 {
259         e.change.paintCue(pi_, x_, yo_, x_ + e.full_width(), e.font.fontInfo());
260 }
261
262
263 void RowPainter::paintChangeBar() const
264 {
265         int const height = tm_.isLastRow(row_)
266                 ? row_.ascent()
267                 : row_.height();
268
269         pi_.pain.fillRectangle(5, yo_ - row_.ascent(), 3, height, Color_changebar);
270 }
271
272
273 void RowPainter::paintAppendix() const
274 {
275         // only draw the appendix frame once (for the main text)
276         if (!par_.params().appendix() || !text_.isMainText())
277                 return;
278
279         int y = yo_ - row_.ascent();
280
281         if (par_.params().startOfAppendix())
282                 y += 2 * defaultRowHeight();
283
284         pi_.pain.line(1, y, 1, yo_ + row_.height(), Color_appendix);
285         pi_.pain.line(tm_.width() - 2, y, tm_.width() - 2, yo_ + row_.height(), Color_appendix);
286 }
287
288
289 void RowPainter::paintDepthBar() const
290 {
291         depth_type const depth = par_.getDepth();
292
293         if (depth <= 0)
294                 return;
295
296         depth_type prev_depth = 0;
297         if (!tm_.isFirstRow(row_)) {
298                 pit_type pit2 = row_.pit();
299                 if (row_.pos() == 0)
300                         --pit2;
301                 prev_depth = pars_[pit2].getDepth();
302         }
303
304         depth_type next_depth = 0;
305         if (!tm_.isLastRow(row_)) {
306                 pit_type pit2 = row_.pit();
307                 if (row_.endpos() >= pars_[pit2].size())
308                         ++pit2;
309                 next_depth = pars_[pit2].getDepth();
310         }
311
312         for (depth_type i = 1; i <= depth; ++i) {
313                 int const w = nestMargin() / 5;
314                 int x = int(xo_) + w * i;
315                 // only consider the changebar space if we're drawing outermost text
316                 if (text_.isMainText())
317                         x += changebarMargin();
318
319                 int const starty = yo_ - row_.ascent();
320                 int const h =  row_.height() - 1 - (i - next_depth - 1) * 3;
321
322                 pi_.pain.line(x, starty, x, starty + h, Color_depthbar);
323
324                 if (i > prev_depth)
325                         pi_.pain.fillRectangle(x, starty, w, 2, Color_depthbar);
326                 if (i > next_depth)
327                         pi_.pain.fillRectangle(x, starty + h, w, 2, Color_depthbar);
328         }
329 }
330
331
332 void RowPainter::paintAppendixStart(int y) const
333 {
334         FontInfo pb_font = sane_font;
335         pb_font.setColor(Color_appendix);
336         pb_font.decSize();
337
338         int w = 0;
339         int a = 0;
340         int d = 0;
341
342         docstring const label = _("Appendix");
343         theFontMetrics(pb_font).rectText(label, w, a, d);
344
345         int const text_start = int(xo_ + (tm_.width() - w) / 2);
346         int const text_end = text_start + w;
347
348         pi_.pain.rectText(text_start, y + d, label, pb_font, Color_none, Color_none);
349
350         pi_.pain.line(int(xo_ + 1), y, text_start, y, Color_appendix);
351         pi_.pain.line(text_end, y, int(xo_ + tm_.width() - 2), y, Color_appendix);
352 }
353
354
355 void RowPainter::paintTooLargeMarks(bool const left, bool const right) const
356 {
357         if (left)
358                 pi_.pain.line(pi_.base.dottedLineThickness(), yo_ - row_.ascent(),
359                                           pi_.base.dottedLineThickness(), yo_ + row_.descent(),
360                                           Color_scroll, Painter::line_onoffdash,
361                               pi_.base.dottedLineThickness());
362         if (right) {
363                 int const wwidth =
364                         pi_.base.bv->workWidth() - pi_.base.dottedLineThickness();
365                 pi_.pain.line(wwidth, yo_ - row_.ascent(),
366                                           wwidth, yo_ + row_.descent(),
367                                           Color_scroll, Painter::line_onoffdash,
368                               pi_.base.dottedLineThickness());
369         }
370 }
371
372
373 void RowPainter::paintFirst() const
374 {
375         Layout const & layout = par_.layout();
376
377         // start of appendix?
378         if (par_.params().startOfAppendix())
379             paintAppendixStart(yo_ - row_.ascent() + 2 * defaultRowHeight());
380
381         bool const is_first =
382                 text_.isFirstInSequence(row_.pit()) || !layout.isParagraphGroup();
383         //lyxerr << "paintFirst: " << par_.id() << " is_seq: " << is_seq << endl;
384
385         if (layout.labelIsInline()
386             && (layout.labeltype != LABEL_STATIC || is_first))
387                 paintLabel();
388         else if (is_first && layout.labelIsAbove())
389                 paintTopLevelLabel();
390 }
391
392
393 void RowPainter::paintLabel() const
394 {
395         docstring const & str = par_.labelString();
396         if (str.empty())
397                 return;
398
399         Layout const & layout = par_.layout();
400         FontInfo const font = labelFont();
401         FontMetrics const & fm = theFontMetrics(font);
402         double x = x_;
403
404         if (row_.isRTL())
405                 x = tm_.width() - row_.right_margin + fm.width(layout.labelsep);
406         else
407                 x = x_ - fm.width(layout.labelsep) - fm.width(str);
408
409         pi_.pain.text(int(x), yo_, str, font);
410 }
411
412
413 void RowPainter::paintTopLevelLabel() const
414 {
415         BufferParams const & bparams = pi_.base.bv->buffer().params();
416         ParagraphParameters const & pparams = par_.params();
417         Layout const & layout = par_.layout();
418         FontInfo const font = labelFont();
419         docstring const str = par_.labelString();
420         if (str.empty())
421                 return;
422
423         double spacing_val = 1.0;
424         if (!pparams.spacing().isDefault())
425                 spacing_val = pparams.spacing().getValue();
426         else
427                 spacing_val = bparams.spacing().getValue();
428
429         FontMetrics const & fm = theFontMetrics(font);
430
431         int const labeladdon = int(fm.maxHeight()
432                 * layout.spacing.getValue() * spacing_val);
433
434         int maxdesc =
435                 int(fm.maxDescent() * layout.spacing.getValue() * spacing_val
436                 + (layout.labelbottomsep * defaultRowHeight()));
437
438         double x = x_;
439         if (layout.labeltype == LABEL_CENTERED) {
440                 x = row_.left_margin + (tm_.width() - row_.left_margin - row_.right_margin) / 2;
441                 x -= fm.width(str) / 2;
442         } else if (row_.isRTL()) {
443                 x = tm_.width() - row_.right_margin - fm.width(str);
444         }
445         pi_.pain.text(int(x), yo_ - maxdesc - labeladdon, str, font);
446 }
447
448
449 /** Check if the current paragraph is the last paragraph in a
450     proof environment */
451 static int getEndLabel(pit_type p, Text const & text)
452 {
453         ParagraphList const & pars = text.paragraphs();
454         pit_type pit = p;
455         depth_type par_depth = pars[p].getDepth();
456         while (pit != pit_type(pars.size())) {
457                 Layout const & layout = pars[pit].layout();
458                 int const endlabeltype = layout.endlabeltype;
459
460                 if (endlabeltype != END_LABEL_NO_LABEL) {
461                         if (p + 1 == pit_type(pars.size()))
462                                 return endlabeltype;
463
464                         depth_type const next_depth =
465                                 pars[p + 1].getDepth();
466                         if (par_depth > next_depth ||
467                             (par_depth == next_depth && layout != pars[p + 1].layout()))
468                                 return endlabeltype;
469                         break;
470                 }
471                 if (par_depth == 0)
472                         break;
473                 pit = text.outerHook(pit);
474                 if (pit != pit_type(pars.size()))
475                         par_depth = pars[pit].getDepth();
476         }
477         return END_LABEL_NO_LABEL;
478 }
479
480
481 void RowPainter::paintLast() const
482 {
483         int const endlabel = getEndLabel(row_.pit(), text_);
484
485         // paint imaginary end-of-paragraph character
486
487         Change const & change = par_.lookupChange(par_.size());
488         if (change.changed()) {
489                 FontMetrics const & fm =
490                         theFontMetrics(pi_.base.bv->buffer().params().getFont());
491                 int const length = fm.maxAscent() / 2;
492                 Color col = change.color();
493
494                 pi_.pain.line(int(x_) + 1, yo_ + 2, int(x_) + 1, yo_ + 2 - length, col,
495                            Painter::line_solid, 3);
496
497                 if (change.deleted()) {
498                         pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1 + length,
499                                 yo_ + 2, col, Painter::line_solid, 3);
500                 } else {
501                         pi_.pain.line(int(x_) + 1 - length, yo_ + 2, int(x_) + 1,
502                                 yo_ + 2, col, Painter::line_solid, 3);
503                 }
504         }
505
506         // draw an endlabel
507
508         switch (endlabel) {
509         case END_LABEL_BOX:
510         case END_LABEL_FILLED_BOX: {
511                 FontInfo const font = labelFont();
512                 FontMetrics const & fm = theFontMetrics(font);
513                 int const size = int(0.75 * fm.maxAscent());
514                 int const y = yo_ - size;
515
516                 // If needed, move the box a bit to avoid overlapping with text.
517                 int x = 0;
518                 if (row_.isRTL()) {
519                         int const normal_x = nestMargin() + changebarMargin();
520                         x = min(normal_x, row_.left_margin - size - Inset::TEXT_TO_INSET_OFFSET);
521                 } else {
522                         int const normal_x = tm_.width() - row_.right_margin
523                                 - size - Inset::TEXT_TO_INSET_OFFSET;
524                         x = max(normal_x, row_.width());
525                 }
526
527                 if (endlabel == END_LABEL_BOX)
528                         pi_.pain.rectangle(x, y, size, size, Color_eolmarker);
529                 else
530                         pi_.pain.fillRectangle(x, y, size, size, Color_eolmarker);
531                 break;
532         }
533
534         case END_LABEL_STATIC: {
535                 FontInfo const font = labelFont();
536                 FontMetrics const & fm = theFontMetrics(font);
537                 docstring const & str = par_.layout().endlabelstring();
538                 double const x = row_.isRTL() ? x_ - fm.width(str) : x_;
539                 pi_.pain.text(int(x), yo_, str, font);
540                 break;
541         }
542
543         case END_LABEL_NO_LABEL:
544                 break;
545         }
546 }
547
548
549 void RowPainter::paintOnlyInsets()
550 {
551         Row::const_iterator cit = row_.begin();
552         Row::const_iterator const & end = row_.end();
553         for ( ; cit != end ; ++cit) {
554                 Row::Element const & e = *cit;
555                 if (e.type == Row::INSET) {
556                         paintInset(e);
557                         // The line that indicates word in a different language
558                         paintForeignMark(e);
559                         // change tracking (not for insets that handle it themselves)
560                         if (!e.inset->canPaintChange(*pi_.base.bv))
561                                 paintChange(e);
562                 }
563
564                 x_ += e.full_width();
565         }
566 }
567
568
569 void RowPainter::paintText()
570 {
571         Row::const_iterator cit = row_.begin();
572         Row::const_iterator const & end = row_.end();
573         for ( ; cit != end ; ++cit) {
574                 Row::Element const & e = *cit;
575
576                 switch (e.type) {
577                 case Row::STRING:
578                 case Row::VIRTUAL:
579                         paintStringAndSel(e);
580
581                         // Paint the spelling marks if enabled.
582                         if (lyxrc.spellcheck_continuously && pi_.do_spellcheck && !pi_.pain.isNull())
583                                 paintMisspelledMark(e);
584                         break;
585
586                 case Row::INSET:
587                         paintInset(e);
588                         paintTextDecoration(e);
589                         break;
590
591                 case Row::SPACE:
592                         paintTextDecoration(e);
593                 }
594
595                 // The line that indicates word in a different language
596                 paintForeignMark(e);
597
598                 // change tracking (not for insets that handle it themselves)
599                 if (e.type != Row::INSET || ! e.inset->canPaintChange(*pi_.base.bv))
600                         paintChange(e);
601
602                 x_ += e.full_width();
603         }
604 }
605
606
607 void RowPainter::paintSelection() const
608 {
609         if (!row_.selection())
610                 return;
611
612         int const y1 = yo_ - row_.ascent();
613         int const y2 = y1 + row_.height();
614
615         // draw the margins
616         if (row_.isRTL() ? row_.end_margin_sel : row_.begin_margin_sel)
617                 pi_.pain.fillRectangle(int(xo_), y1, row_.left_margin, y2 - y1,
618                                        Color_selection);
619
620         // go through row and draw from RTL boundary to RTL boundary
621         double x = xo_ + row_.left_margin;
622         for (auto const & e : row_) {
623                 // These are the same tests as in paintStringAndSel, except
624                 // that all_sel has an additional clause that triggers for end
625                 // of paragraph markers. The clause was not used in
626                 // paintStringAndSel to avoid changing the drawing color.
627                 // at least part of text selected?
628                 bool const some_sel = (e.endpos >= row_.sel_beg && e.pos < row_.sel_end)
629                         || pi_.selected;
630                 // all the text selected?
631                 bool const all_sel = (e.pos >= row_.sel_beg && e.endpos < row_.sel_end)
632                     || (e.isVirtual() && e.pos == row_.endpos() && row_.end_margin_sel)
633                     || pi_.selected;
634
635                 if (all_sel) {
636                         // the 3rd argument is written like that to avoid rounding issues
637                         pi_.pain.fillRectangle(int(x), y1,
638                                                int(x + e.full_width()) - int(x), y2 - y1,
639                                                Color_selection);
640                 } else if (some_sel) {
641                         pos_type const from = min(max(row_.sel_beg, e.pos), e.endpos);
642                         pos_type const to = max(min(row_.sel_end, e.endpos), e.pos);
643                         double x1 = e.pos2x(from);
644                         double x2 = e.pos2x(to);
645                         if (x1 > x2)
646                                 swap(x1, x2);
647                         // the 3rd argument is written like that to avoid rounding issues
648                         pi_.pain.fillRectangle(int(x + x1), y1, int(x2 + x) - int(x1 + x),
649                                                y2 - y1, Color_selection);
650                 }
651                 x += e.full_width();
652         }
653
654         if (row_.isRTL() ? row_.begin_margin_sel : row_.end_margin_sel)
655                 pi_.pain.fillRectangle(int(x), y1,
656                                        int(xo_ + tm_.width()) - int(x), y2 - y1,
657                                        Color_selection);
658
659 }
660
661
662 } // namespace lyx