]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_colorinset.C
Andreas' patch to prevent crash on click on previewd inset
[lyx.git] / src / mathed / math_colorinset.C
index 6c4bdf527d6be213bcf45c817f5a7600f679791e..e630ebc95dc04a0208ddc99d80271bf02551a639 100644 (file)
@@ -13,6 +13,7 @@
 #include "math_colorinset.h"
 #include "math_data.h"
 #include "math_mathmlstream.h"
+#include "math_streamstr.h"
 #include "math_support.h"
 
 #include "LaTeXFeatures.h"
 #include "support/std_ostream.h"
 
 using std::auto_ptr;
+using std::string;
 
 
-MathColorInset::MathColorInset()
-       : MathNestInset(2)
+namespace {
+
+/// color "none" (reset to default) needs special treatment
+bool normalcolor(string const & color)
+{
+       return color == "none";
+}
+
+} // namespace anon
+
+
+MathColorInset::MathColorInset(bool oldstyle, LColor_color const & color)
+       : MathNestInset(1), oldstyle_(oldstyle),
+         color_(lcolor.getLaTeXName(color))
 {}
 
 
-auto_ptr<InsetBase> MathColorInset::clone() const
+MathColorInset::MathColorInset(bool oldstyle, string const & color)
+       : MathNestInset(1), oldstyle_(oldstyle), color_(color)
+{}
+
+
+auto_ptr<InsetBase> MathColorInset::doClone() const
 {
        return auto_ptr<InsetBase>(new MathColorInset(*this));
 }
@@ -35,54 +54,50 @@ auto_ptr<InsetBase> MathColorInset::clone() const
 
 void MathColorInset::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-       FontSetChanger dummy(mi.base, "textnormal");
-       w_ = mathed_char_width(mi.base.font, '[');
-       MathNestInset::metrics(mi);
-       dim_   = cell(0).dim();
-       dim_  += cell(1).dim();
-       dim_.wid += 2 * w_ + 4;
-       metricsMarkers();
-       dim = dim_;
+       cell(0).metrics(mi, dim);
+       metricsMarkers(dim);
+       dim_ = dim;
 }
 
 
 void MathColorInset::draw(PainterInfo & pi, int x, int y) const
 {
-       FontSetChanger dummy(pi.base, "textnormal");
+       LColor_color origcol = pi.base.font.color();
+       pi.base.font.setColor(lcolor.getFromLaTeXName(color_));
+       cell(0).draw(pi, x + 1, y);
+       pi.base.font.setColor(origcol);
        drawMarkers(pi, x, y);
-
-       drawStrBlack(pi, x, y, "[");
-       x += w_;
-       cell(0).draw(pi, x, y);
-       x += cell(0).width();
-       drawStrBlack(pi, x, y, "]");
-       x += w_ + 2;
-
-       ColorChanger dummy1(pi.base.font, asString(cell(0)));
-       cell(1).draw(pi, x, y);
+       setPosCache(pi, x, y);
 }
 
 
 void MathColorInset::validate(LaTeXFeatures & features) const
 {
        MathNestInset::validate(features);
-       features.require("color");
+       if (!normalcolor(color_))
+               features.require("color");
 }
 
 
 void MathColorInset::write(WriteStream & os) const
 {
-       os << "\\color" << '{' << cell(0) << '}' << '{' << cell(1) << '}';
+       if (normalcolor(color_))
+               // reset to default color inside another color inset
+               os << "{\\normalcolor " << cell(0) << '}';
+       else if (oldstyle_)
+               os << "{\\color" << '{' << color_ << '}' << cell(0) << '}';
+       else
+               os << "\\textcolor" << '{' << color_ << "}{" << cell(0) << '}';
 }
 
 
 void MathColorInset::normalize(NormalStream & os) const
 {
-       os << "[color " << cell(0) << ' ' << cell(1) << ']';
+       os << "[color " << color_ << ' ' << cell(0) << ']';
 }
 
 
 void MathColorInset::infoize(std::ostream & os) const
 {
-       os << "Color: " << cell(0);
+       os << "Color: " << color_;
 }