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