]> git.lyx.org Git - lyx.git/blob - src/mathed/math_colorinset.C
Fix crash and cursor positioning in math colour inset
[lyx.git] / src / mathed / math_colorinset.C
1 /**
2  * \file math_colorinset.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "math_colorinset.h"
14 #include "math_data.h"
15 #include "math_mathmlstream.h"
16 #include "math_support.h"
17
18 #include "LaTeXFeatures.h"
19 #include "LColor.h"
20
21 #include "support/std_ostream.h"
22
23 using std::auto_ptr;
24
25
26 namespace {
27
28 // color "none" (reset to default) needs special treatment
29 bool normalcolor(MathArray const & ar)
30 {
31         return (asString(ar) == "none");
32 }
33
34 } // namespace anon
35
36
37 MathColorInset::MathColorInset(bool oldstyle)
38         : MathNestInset(2), oldstyle_(oldstyle)
39 {}
40
41
42 auto_ptr<InsetBase> MathColorInset::doClone() const
43 {
44         return auto_ptr<InsetBase>(new MathColorInset(*this));
45 }
46
47
48 void MathColorInset::metrics(MetricsInfo & mi, Dimension & dim) const
49 {
50         cell(1).metrics(mi, dim);
51         if (editing(mi.base.bv)) {
52                 FontSetChanger dummy(mi.base, "textnormal");
53                 cell(0).metrics(mi);
54                 dim  += cell(0).dim();
55                 w_ = mathed_char_width(mi.base.font, '[');
56                 dim.asc += 4;
57                 dim.des += 4;
58                 dim.wid += 2 * w_ + 4;
59                 metricsMarkers(dim);
60         }
61         dim_ = dim;
62 }
63
64
65 void MathColorInset::draw(PainterInfo & pi, int x, int y) const
66 {
67         int const x0(x);
68         if (editing(pi.base.bv)) {
69                 FontSetChanger dummy(pi.base, "textnormal");
70                 drawMarkers(pi, x, y);
71                 drawStrBlack(pi, x, y, "[");
72                 x += w_;
73                 cell(0).draw(pi, x, y);
74                 x += cell(0).width();
75                 drawStrBlack(pi, x, y, "]");
76                 x += w_ + 2;
77         }
78
79         LColor_color origcol = pi.base.font.color();
80         pi.base.font.setColor(lcolor.getFromGUIName(asString(cell(0))));
81         cell(1).draw(pi, x, y);
82         pi.base.font.setColor(origcol);
83         if (editing(pi.base.bv))
84                 setPosCache(pi, x0, y);
85 }
86
87
88 void MathColorInset::validate(LaTeXFeatures & features) const
89 {
90         MathNestInset::validate(features);
91         if (!normalcolor(cell(0)))
92                 features.require("color");
93 }
94
95
96 void MathColorInset::write(WriteStream & os) const
97 {
98         if (normalcolor(cell(0)))
99                 os << "{\\normalcolor " << cell(1) << '}';
100         else if (oldstyle_)
101                 os << "{\\color" << '{' << cell(0) << '}' << cell(1) << '}';
102         else
103                 os << "\\textcolor" << '{' << cell(0) << "}{" << cell(1) << '}';
104 }
105
106
107 void MathColorInset::normalize(NormalStream & os) const
108 {
109         os << "[color " << cell(0) << ' ' << cell(1) << ']';
110 }
111
112
113 void MathColorInset::infoize(std::ostream & os) const
114 {
115         os << "Color: " << cell(0);
116 }