]> git.lyx.org Git - lyx.git/blob - src/mathed/math_colorinset.C
Fix event loop to no longer eat CPU
[lyx.git] / src / mathed / math_colorinset.C
1 /**
2  * \file math_colorinset.C
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 "math_colorinset.h"
14 #include "math_data.h"
15 #include "math_mathmlstream.h"
16 #include "math_streamstr.h"
17 #include "math_support.h"
18
19 #include "LaTeXFeatures.h"
20
21 #include "support/std_ostream.h"
22
23 using std::auto_ptr;
24 using std::string;
25
26
27 namespace {
28
29 /// color "none" (reset to default) needs special treatment
30 bool normalcolor(string const & color)
31 {
32         return color == "none";
33 }
34
35 } // namespace anon
36
37
38 MathColorInset::MathColorInset(bool oldstyle, LColor_color const & color)
39         : MathNestInset(1), oldstyle_(oldstyle),
40           color_(lcolor.getLaTeXName(color))
41 {}
42
43
44 MathColorInset::MathColorInset(bool oldstyle, string const & color)
45         : MathNestInset(1), oldstyle_(oldstyle), color_(color)
46 {}
47
48
49 auto_ptr<InsetBase> MathColorInset::doClone() const
50 {
51         return auto_ptr<InsetBase>(new MathColorInset(*this));
52 }
53
54
55 void MathColorInset::metrics(MetricsInfo & mi, Dimension & dim) const
56 {
57         cell(0).metrics(mi, dim);
58         metricsMarkers(dim);
59         dim_ = dim;
60 }
61
62
63 void MathColorInset::draw(PainterInfo & pi, int x, int y) const
64 {
65         LColor_color origcol = pi.base.font.color();
66         pi.base.font.setColor(lcolor.getFromLaTeXName(color_));
67         cell(0).draw(pi, x + 1, y);
68         pi.base.font.setColor(origcol);
69         drawMarkers(pi, x, y);
70         setPosCache(pi, x, y);
71 }
72
73
74 void MathColorInset::validate(LaTeXFeatures & features) const
75 {
76         MathNestInset::validate(features);
77         if (!normalcolor(color_))
78                 features.require("color");
79 }
80
81
82 void MathColorInset::write(WriteStream & os) const
83 {
84         if (normalcolor(color_))
85                 // reset to default color inside another color inset
86                 os << "{\\normalcolor " << cell(0) << '}';
87         else if (oldstyle_)
88                 os << "{\\color" << '{' << color_ << '}' << cell(0) << '}';
89         else
90                 os << "\\textcolor" << '{' << color_ << "}{" << cell(0) << '}';
91 }
92
93
94 void MathColorInset::normalize(NormalStream & os) const
95 {
96         os << "[color " << color_ << ' ' << cell(0) << ']';
97 }
98
99
100 void MathColorInset::infoize(std::ostream & os) const
101 {
102         os << "Color: " << color_;
103 }