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