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