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