]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathColor.cpp
Simplify Changers interface
[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(mi, 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                                 break;
92                         default:
93                                 features.require("color");
94                                 break;
95                 }
96         }
97 }
98
99
100 void InsetMathColor::write(WriteStream & os) const
101 {
102         if (normalcolor(color_))
103                 // reset to default color inside another color inset
104                 os << "{\\normalcolor " << cell(0) << '}';
105         else if (oldstyle_)
106                 os << "{\\color{" << color_ << '}' << cell(0) << '}';
107         else
108                 os << "\\textcolor{" << color_ << "}{" << cell(0) << '}';
109 }
110
111
112 void InsetMathColor::normalize(NormalStream & os) const
113 {
114         os << "[color " << color_ << ' ' << cell(0) << ']';
115 }
116
117
118 void InsetMathColor::infoize(odocstream & os) const
119 {
120         os << bformat(_("Color: %1$s"), color_);
121 }
122
123
124 } // namespace lyx