]> git.lyx.org Git - lyx.git/blob - src/mathed/math_colorinset.C
Make Helge happy: no more crash on arrow up/down in math macro
[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_support.h"
17
18 #include "LaTeXFeatures.h"
19 #include "LColor.h"
20
21 #include "support/std_ostream.h"
22
23 using std::auto_ptr;
24
25
26 namespace {
27
28 // color "none" (reset to default) needs special treatment
29 bool normalcolor(MathArray const & ar)
30 {
31         return (asString(ar) == "none");
32 }
33
34 } // namespace anon
35
36
37 MathColorInset::MathColorInset(bool oldstyle)
38         : MathNestInset(2), oldstyle_(oldstyle)
39 {}
40
41
42 auto_ptr<InsetBase> MathColorInset::doClone() const
43 {
44         return auto_ptr<InsetBase>(new MathColorInset(*this));
45 }
46
47
48 void MathColorInset::metrics(MetricsInfo & mi, Dimension & dim) const
49 {
50         cell(1).metrics(mi, dim);
51         if (editing(mi.base.bv)) {
52                 FontSetChanger dummy(mi.base, "textnormal");
53                 cell(0).metrics(mi);
54                 dim  += cell(0).dim();
55                 w_ = mathed_char_width(mi.base.font, '[');
56                 dim.asc += 4;
57                 dim.des += 4;
58                 dim.wid += 2 * w_ + 4;
59                 metricsMarkers(dim);
60         }
61         dim_ = dim;
62 }
63
64
65 void MathColorInset::draw(PainterInfo & pi, int x, int y) const
66 {
67         if (editing(pi.base.bv)) {
68                 FontSetChanger dummy(pi.base, "textnormal");
69                 drawMarkers(pi, x, y);
70                 drawStrBlack(pi, x, y, "[");
71                 x += w_;
72                 cell(0).draw(pi, x, y);
73                 x += cell(0).width();
74                 drawStrBlack(pi, x, y, "]");
75                 x += w_ + 2;
76         }
77
78         LColor_color origcol = pi.base.font.color();
79         pi.base.font.setColor(lcolor.getFromGUIName(asString(cell(0))));
80         cell(1).draw(pi, x, y);
81         pi.base.font.setColor(origcol);
82         setPosCache(pi, x, y);
83 }
84
85
86 void MathColorInset::validate(LaTeXFeatures & features) const
87 {
88         MathNestInset::validate(features);
89         if (!normalcolor(cell(0)))
90                 features.require("color");
91 }
92
93
94 void MathColorInset::write(WriteStream & os) const
95 {
96         if (normalcolor(cell(0)))
97                 os << "{\\normalcolor " << cell(1) << '}';
98         else if (oldstyle_)
99                 os << "{\\color" << '{' << cell(0) << '}' << cell(1) << '}';
100         else
101                 os << "\\textcolor" << '{' << cell(0) << "}{" << cell(1) << '}';
102 }
103
104
105 void MathColorInset::normalize(NormalStream & os) const
106 {
107         os << "[color " << cell(0) << ' ' << cell(1) << ']';
108 }
109
110
111 void MathColorInset::infoize(std::ostream & os) const
112 {
113         os << "Color: " << cell(0);
114 }