]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathColor.cpp
In mathed, require xcolor instead of color when appropriate
[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), w_(0), 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), w_(0), 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         metricsMarkers(dim);
53 }
54
55
56 void InsetMathColor::draw(PainterInfo & pi, int x, int y) const
57 {
58         ColorCode origcol = pi.base.font.color();
59         pi.base.font.setColor(lcolor.getFromLaTeXName(to_utf8(color_)));
60         cell(0).draw(pi, x + 1, y);
61         pi.base.font.setColor(origcol);
62         drawMarkers(pi, x, y);
63         setPosCache(pi, x, y);
64 }
65
66
67 /// color "none" (reset to default) needs special treatment
68 static bool normalcolor(docstring const & color)
69 {
70         return color == "none";
71 }
72
73
74 void InsetMathColor::validate(LaTeXFeatures & features) const
75 {
76         InsetMathNest::validate(features);
77         if (!normalcolor(color_)) {
78                 switch (lcolor.getFromLaTeXName(to_utf8(color_))) {
79                         case Color_brown:
80                         case Color_darkgray:
81                         case Color_gray:
82                         case Color_lightgray:
83                         case Color_lime:
84                         case Color_olive:
85                         case Color_orange:
86                         case Color_pink:
87                         case Color_purple:
88                         case Color_teal:
89                         case Color_violet:
90                                 features.require("xcolor");
91                         default:
92                                 features.require("color");
93                 }
94         }
95 }
96
97
98 void InsetMathColor::write(WriteStream & os) const
99 {
100         if (normalcolor(color_))
101                 // reset to default color inside another color inset
102                 os << "{\\normalcolor " << cell(0) << '}';
103         else if (oldstyle_)
104                 os << "{\\color{" << color_ << '}' << cell(0) << '}';
105         else
106                 os << "\\textcolor{" << color_ << "}{" << cell(0) << '}';
107 }
108
109
110 void InsetMathColor::normalize(NormalStream & os) const
111 {
112         os << "[color " << color_ << ' ' << cell(0) << ']';
113 }
114
115
116 void InsetMathColor::infoize(odocstream & os) const
117 {
118         os << bformat(_("Color: %1$s"), color_);
119 }
120
121
122 } // namespace lyx