]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathColor.cpp
Fix missing initialization
[lyx.git] / src / mathed / InsetMathColor.cpp
1 /**
2  * \file InsetMathColor.cpp
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 "ColorSet.h"
14
15 #include "InsetMathColor.h"
16 #include "LaTeXFeatures.h"
17 #include "MathData.h"
18 #include "MathStream.h"
19 #include "MathSupport.h"
20 #include "MetricsInfo.h"
21
22 #include "support/gettext.h"
23 #include "support/lstrings.h"
24
25 #include <ostream>
26
27 using namespace lyx::support;
28
29 namespace lyx {
30
31 InsetMathColor::InsetMathColor(Buffer * buf, bool oldstyle, ColorCode color)
32         : InsetMathNest(buf, 1), oldstyle_(oldstyle),
33           color_(from_utf8(lcolor.getLaTeXName(color))),
34           current_mode_(UNDECIDED_MODE)
35 {}
36
37
38 InsetMathColor::InsetMathColor(Buffer * buf, bool oldstyle,
39                 docstring const & color)
40         : InsetMathNest(buf, 1), oldstyle_(oldstyle), color_(color),
41           current_mode_(UNDECIDED_MODE)
42 {}
43
44
45 Inset * InsetMathColor::clone() const
46 {
47         return new InsetMathColor(*this);
48 }
49
50
51 void InsetMathColor::metrics(MetricsInfo & mi, Dimension & dim) const
52 {
53         current_mode_ = isTextFont(mi.base.fontname) ? TEXT_MODE : MATH_MODE;
54         Changer dummy = mi.base.changeEnsureMath(current_mode_);
55
56         cell(0).metrics(mi, dim);
57 }
58
59
60 void InsetMathColor::draw(PainterInfo & pi, int x, int y) const
61 {
62         current_mode_ = isTextFont(pi.base.fontname) ? TEXT_MODE : MATH_MODE;
63         Changer dummy = pi.base.changeEnsureMath(current_mode_);
64
65         ColorCode origcol = pi.base.font.color();
66         pi.base.font.setColor(lcolor.getFromLaTeXName(to_utf8(color_)));
67         cell(0).draw(pi, x, y);
68         pi.base.font.setColor(origcol);
69 }
70
71
72 /// color "none" (reset to default) needs special treatment
73 static bool normalcolor(docstring const & color)
74 {
75         return color == "none";
76 }
77
78
79 void InsetMathColor::validate(LaTeXFeatures & features) const
80 {
81         InsetMathNest::validate(features);
82         if (!normalcolor(color_)) {
83                 switch (lcolor.getFromLaTeXName(to_utf8(color_))) {
84                         case Color_brown:
85                         case Color_darkgray:
86                         case Color_gray:
87                         case Color_lightgray:
88                         case Color_lime:
89                         case Color_olive:
90                         case Color_orange:
91                         case Color_pink:
92                         case Color_purple:
93                         case Color_teal:
94                         case Color_violet:
95                                 features.require("xcolor");
96                                 break;
97                         default:
98                                 features.require("color");
99                                 break;
100                 }
101         }
102 }
103
104
105 void InsetMathColor::write(TeXMathStream & os) const
106 {
107         // We have to ensure correct spacing when the front and/or back
108         // atoms are not ordinary ones (bug 11827).
109         docstring const frontclass = class_to_string(cell(0).firstMathClass());
110         docstring const backclass = class_to_string(cell(0).lastMathClass());
111         bool adjchk = os.latex() && !os.inMathClass() && (normalcolor(color_) || oldstyle_);
112         bool adjust_front = frontclass != "mathord" && adjchk;
113         bool adjust_back = backclass != "mathord" && adjchk;
114         docstring const colswitch =
115                 oldstyle_ ? from_ascii("{\\color{") + color_ + from_ascii("}")
116                           : from_ascii("{\\normalcolor ");
117
118         if (adjust_front && adjust_back) {
119                 os << '\\' << frontclass << colswitch << cell(0).front() << '}';
120                 if (cell(0).size() > 2) {
121                         os << colswitch;
122                         for (size_t i = 1; i < cell(0).size() - 1; ++i)
123                                 os << cell(0)[i];
124                         os << '}';
125                 }
126                 if (cell(0).size() > 1)
127                         os << '\\' << backclass << colswitch << cell(0).back() << '}';
128         } else if (adjust_front) {
129                 os << '\\' << frontclass << colswitch << cell(0).front() << '}';
130                 if (cell(0).size() > 1) {
131                         os << colswitch;
132                         for (size_t i = 1; i < cell(0).size(); ++i)
133                                 os << cell(0)[i];
134                         os << '}';
135                 }
136         } else if (adjust_back) {
137                 os << colswitch;
138                 for (size_t i = 0; i < cell(0).size() - 1; ++i)
139                         os << cell(0)[i];
140                 os << '}' << '\\' << backclass << colswitch << cell(0).back()
141                    << '}';
142         } else if (normalcolor(color_))
143                 // reset to default color inside another color inset
144                 os << "{\\normalcolor " << cell(0) << '}';
145         else if (oldstyle_)
146                 os << "{\\color{" << color_ << '}' << cell(0) << '}';
147         else
148                 os << "\\textcolor{" << color_ << "}{" << cell(0) << '}';
149 }
150
151
152 void InsetMathColor::normalize(NormalStream & os) const
153 {
154         os << "[color " << color_ << ' ' << cell(0) << ']';
155 }
156
157
158 void InsetMathColor::infoize(odocstream & os) const
159 {
160         os << bformat(_("Color: %1$s"), color_);
161 }
162
163
164 } // namespace lyx