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