]> git.lyx.org Git - features.git/commitdiff
fix math color inset UI and parsing
authorGeorg Baum <Georg.Baum@post.rwth-aachen.de>
Fri, 17 Jun 2005 14:35:19 +0000 (14:35 +0000)
committerGeorg Baum <Georg.Baum@post.rwth-aachen.de>
Fri, 17 Jun 2005 14:35:19 +0000 (14:35 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@10083 a592a061-630c-0410-9148-cb99ea01b6c8

src/ChangeLog
src/LColor.C
src/LColor.h
src/mathed/ChangeLog
src/mathed/math_colorinset.C
src/mathed/math_colorinset.h
src/mathed/math_factory.C
src/mathed/math_hullinset.C
src/mathed/math_nestinset.C
src/mathed/math_parser.C

index 56f2cc53d4b7d01c9126963ee5f2c8a678f53ddc..d5448f5a247fc83f96ee07e2d4bc3c122c87b820 100644 (file)
@@ -1,3 +1,7 @@
+2005-06-16  Georg Baum  <Georg.Baum@post.rwth-aachen.de>
+
+       * LColor.[Ch] (getFromLaTeXName): new
+
 2005-06-16  Martin Vermeer  <martin.vermeer@hut.fi>
 
        * text.C (readParagraph): fix bug 1904 (GUI affects LaTeX)
index 2f24d5c9a5a0dc760e159765bec37f7897a1dfa7..33cf10686d3aaa1f3197ee6763e9f18f52e5f7b6 100644 (file)
@@ -33,7 +33,7 @@ using std::string;
 namespace {
 
 struct ColorEntry {
-       int lcolor;
+       LColor::color lcolor;
        char const * guiname;
        char const * latexname;
        char const * x11name;
@@ -61,22 +61,25 @@ public:
        void fill(ColorEntry const & entry)
        {
                information in;
-               in.lyxname   = string(entry.lyxname);
-               in.latexname = string(entry.latexname);
-               in.x11name   = string(entry.x11name);
-               in.guiname   = string(entry.guiname);
+               in.lyxname   = entry.lyxname;
+               in.latexname = entry.latexname;
+               in.x11name   = entry.x11name;
+               in.guiname   = entry.guiname;
                infotab[entry.lcolor] = in;
-               transform[string(entry.lyxname)] = int(entry.lcolor);
+               lyxcolors[entry.lyxname] = entry.lcolor;
+               latexcolors[entry.latexname] = entry.lcolor;
        }
 
        ///
-       typedef std::map<int, information> InfoTab;
+       typedef std::map<LColor::color, information> InfoTab;
        /// the table of color information
        InfoTab infotab;
 
-       typedef std::map<string, int> Transform;
-       /// the transform between colour name string and integer code.
-       Transform transform;
+       typedef std::map<string, LColor::color> Transform;
+       /// the transform between LyX color name string and integer code.
+       Transform lyxcolors;
+       /// the transform between LaTeX color name string and integer code.
+       Transform latexcolors;
 
 };
 
@@ -185,7 +188,7 @@ string const LColor::getX11Name(LColor::color c) const
                return it->second.x11name;
 
        lyxerr << "LyX internal error: Missing color"
-               " entry in LColor.C for " << int(c) << '\n'
+                 " entry in LColor.C for " << c << '\n'
               << "Using black." << endl;
        return "black";
 }
@@ -234,15 +237,14 @@ bool LColor::setColor(LColor::color col, string const & x11name)
 bool LColor::setColor(string const & lyxname, string const &x11name)
 {
        string const lcname = ascii_lowercase(lyxname);
-       if (pimpl_->transform.find(lcname) == pimpl_->transform.end()) {
+       if (pimpl_->lyxcolors.find(lcname) == pimpl_->lyxcolors.end()) {
                lyxerr[Debug::GUI]
                        << "LColor::setColor: Unknown color \""
                       << lyxname << '"' << endl;
                addColor(static_cast<color>(pimpl_->infotab.size()), lcname);
        }
 
-       return setColor(static_cast<LColor::color>(pimpl_->transform[lcname]),
-                       x11name);
+       return setColor(pimpl_->lyxcolors[lcname], x11name);
 }
 
 
@@ -252,7 +254,7 @@ LColor::color LColor::getFromGUIName(string const & guiname) const
        Pimpl::InfoTab::const_iterator end = pimpl_->infotab.end();
        for (; it != end; ++it) {
                if (!compare_ascii_no_case(_(it->second.guiname), guiname))
-                       return static_cast<LColor::color>(it->first);
+                       return it->first;
        }
        return LColor::inherit;
 }
@@ -268,13 +270,25 @@ void LColor::addColor(LColor::color c, string const & lyxname) const
 LColor::color LColor::getFromLyXName(string const & lyxname) const
 {
        string const lcname = ascii_lowercase(lyxname);
-       if (pimpl_->transform.find(lcname) == pimpl_->transform.end()) {
+       if (pimpl_->lyxcolors.find(lcname) == pimpl_->lyxcolors.end()) {
                lyxerr << "LColor::getFromLyXName: Unknown color \""
                       << lyxname << '"' << endl;
                return none;
        }
 
-       return static_cast<LColor::color>(pimpl_->transform[lcname]);
+       return pimpl_->lyxcolors[lcname];
+}
+
+
+LColor::color LColor::getFromLaTeXName(string const & latexname) const
+{
+       if (pimpl_->latexcolors.find(latexname) == pimpl_->latexcolors.end()) {
+               lyxerr << "LColor::getFromLaTeXName: Unknown color \""
+                      << latexname << '"' << endl;
+               return none;
+       }
+
+       return pimpl_->latexcolors[latexname];
 }
 
 
index ab0fdd3a24a7b234419dcf5b8b75e07691361145..b9595e393f061cf3efe547bf27498bed369689df 100644 (file)
@@ -218,6 +218,8 @@ public:
        LColor::color getFromGUIName(std::string const & guiname) const;
        /// \returns the LColor::color associated with the LyX name.
        LColor::color getFromLyXName(std::string const & lyxname) const;
+       /// \returns the LColor::color associated with the LaTeX name.
+       LColor::color getFromLaTeXName(std::string const & latexname) const;
 private:
        ///
        void addColor(LColor::color c, std::string const & lyxname) const;
index 2a91b72e915b9710a93ce645677f9991276751eb..b6e0915fa3bce6782f651b42d8d5fae4aac1a712 100644 (file)
@@ -1,3 +1,16 @@
+2005-06-16  Georg Baum  <Georg.Baum@post.rwth-aachen.de>
+
+       * math_colorinset.C (normalcolor): remove ...
+       * math_colorinset.C (latexcolor): ... and use this new function instead
+       * math_colorinset.C (metrics, draw): Don't draw cell(0), because it
+       is horrible UI
+       * math_colorinset.C (write): Don't write invalid colors
+       * math_colorinset.C: Store always the LaTeXName in cell(0)
+       * math_hullinset.C (handleFont2): adjust to the change above
+       * math_nestinset.C (handleFont2): ditto
+       * math_parser.C (parse): handle \textcolor and \normalcolor
+       * math_factory.C (createMathInset): ditto
+
 2005-06-08  Jean-Marc Lasgouttes  <lasgouttes@lyx.org>
 
        * math_hullinset.C (label): avoid warning when assertions are
index e25870700859fd62f55c2deeaeeeec9b1d722e1f..e630ebc95dc04a0208ddc99d80271bf02551a639 100644 (file)
 #include "math_colorinset.h"
 #include "math_data.h"
 #include "math_mathmlstream.h"
+#include "math_streamstr.h"
 #include "math_support.h"
 
 #include "LaTeXFeatures.h"
-#include "LColor.h"
 
 #include "support/std_ostream.h"
 
 using std::auto_ptr;
+using std::string;
 
 
 namespace {
 
-// color "none" (reset to default) needs special treatment
-bool normalcolor(MathArray const & ar)
+/// color "none" (reset to default) needs special treatment
+bool normalcolor(string const & color)
 {
-       return (asString(ar) == "none");
+       return color == "none";
 }
 
 } // namespace anon
 
 
-MathColorInset::MathColorInset(bool oldstyle)
-       : MathNestInset(2), oldstyle_(oldstyle)
+MathColorInset::MathColorInset(bool oldstyle, LColor_color const & color)
+       : MathNestInset(1), oldstyle_(oldstyle),
+         color_(lcolor.getLaTeXName(color))
+{}
+
+
+MathColorInset::MathColorInset(bool oldstyle, string const & color)
+       : MathNestInset(1), oldstyle_(oldstyle), color_(color)
 {}
 
 
@@ -47,70 +54,50 @@ auto_ptr<InsetBase> MathColorInset::doClone() const
 
 void MathColorInset::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-       cell(1).metrics(mi, dim);
-       if (editing(mi.base.bv)) {
-               FontSetChanger dummy(mi.base, "textnormal");
-               cell(0).metrics(mi);
-               dim  += cell(0).dim();
-               w_ = mathed_char_width(mi.base.font, '[');
-               dim.asc += 4;
-               dim.des += 4;
-               dim.wid += 2 * w_ + 4;
-               metricsMarkers(dim);
-       }
+       cell(0).metrics(mi, dim);
+       metricsMarkers(dim);
        dim_ = dim;
 }
 
 
 void MathColorInset::draw(PainterInfo & pi, int x, int y) const
 {
-       int const x0(x);
-       if (editing(pi.base.bv)) {
-               FontSetChanger dummy(pi.base, "textnormal");
-               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;
-       }
-
        LColor_color origcol = pi.base.font.color();
-       pi.base.font.setColor(lcolor.getFromGUIName(asString(cell(0))));
-       cell(1).draw(pi, x, y);
+       pi.base.font.setColor(lcolor.getFromLaTeXName(color_));
+       cell(0).draw(pi, x + 1, y);
        pi.base.font.setColor(origcol);
-       if (editing(pi.base.bv))
-               setPosCache(pi, x0, y);
+       drawMarkers(pi, x, y);
+       setPosCache(pi, x, y);
 }
 
 
 void MathColorInset::validate(LaTeXFeatures & features) const
 {
        MathNestInset::validate(features);
-       if (!normalcolor(cell(0)))
+       if (!normalcolor(color_))
                features.require("color");
 }
 
 
 void MathColorInset::write(WriteStream & os) const
 {
-       if (normalcolor(cell(0)))
-               os << "{\\normalcolor " << cell(1) << '}';
+       if (normalcolor(color_))
+               // reset to default color inside another color inset
+               os << "{\\normalcolor " << cell(0) << '}';
        else if (oldstyle_)
-               os << "{\\color" << '{' << cell(0) << '}' << cell(1) << '}';
+               os << "{\\color" << '{' << color_ << '}' << cell(0) << '}';
        else
-               os << "\\textcolor" << '{' << cell(0) << "}{" << cell(1) << '}';
+               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_;
 }
index 604fb08098782eb77f1a6a4767d196c37ff787ed..6eec693f4f90c4ad36ffbfb60711b6107946fc76 100644 (file)
 #ifndef MATH_COLORINSET_H
 #define MATH_COLORINSET_H
 
+#include "LColor.h"
+
 #include "math_nestinset.h"
 
 /// Change colours.
 
 class MathColorInset : public MathNestInset {
 public:
-       ///
-       explicit MathColorInset(bool oldstyle);
+       /// Create a color inset from LyX color number
+       explicit MathColorInset(bool oldstyle,
+               LColor_color const & color = LColor::none);
+       /// Create a color inset from LaTeX color name
+       explicit MathColorInset(bool oldstyle, std::string const & color);
        ///
        void metrics(MetricsInfo & mi, Dimension & dim) const;
        /// we write extra braces in any case...
+       /// FIXME Why? Are they necessary if oldstyle_ == false?
        bool extraBraces() const { return true; }
        ///
        void draw(PainterInfo & pi, int x, int y) const;
@@ -40,6 +46,8 @@ private:
        mutable int w_;
        ///
        bool oldstyle_;
+       /// Our color. Only valid LaTeX colors are allowed.
+       std::string color_;
 };
 
 #endif
index 045bf8d1f167b01aac9795ee35c86fec289b8421..98d4e110e8b68be8991e0c164263d53df5ab88c8 100644 (file)
@@ -323,7 +323,7 @@ MathAtom createMathInset(string const & s)
                return MathAtom(new MathLefteqnInset);
        if (s == "boldsymbol")
                return MathAtom(new MathBoldsymbolInset);
-       if (s == "color")
+       if (s == "color" || s == "normalcolor")
                return MathAtom(new MathColorInset(true));
        if (s == "textcolor")
                return MathAtom(new MathColorInset(false));
index f7c0902ea237569e57305dca3081c32dc8ee8fbc..9d3fb3e878e4bed540b6e9bb4faffacc2eaf5c83 100644 (file)
@@ -11,6 +11,7 @@
 #include <config.h>
 
 #include "math_charinset.h"
+#include "math_colorinset.h"
 #include "math_data.h"
 #include "math_extern.h"
 #include "math_hullinset.h"
@@ -1214,9 +1215,8 @@ void MathHullInset::handleFont2(LCursor & cur, string const & arg)
        bool b;
        bv_funcs::string2font(arg, font, b);
        if (font.color() != LColor::inherit) {
-               MathAtom at = createMathInset("color");
-               asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
-               cur.handleNest(at, 1);
+               MathAtom at = MathAtom(new MathColorInset(true, font.color()));
+               cur.handleNest(at, 0);
        }
 }
 
index 043d092ec205a89a721a43fc4522b10e77ef9b75..a22bed8089d9aa012c74133db2d6c9eff73c89fc 100644 (file)
@@ -15,6 +15,7 @@
 #include "math_arrayinset.h"
 #include "math_boxinset.h"
 #include "math_braceinset.h"
+#include "math_colorinset.h"
 #include "math_commentinset.h"
 #include "math_data.h"
 #include "math_deliminset.h"
@@ -394,9 +395,8 @@ void MathNestInset::handleFont2(LCursor & cur, string const & arg)
        bool b;
        bv_funcs::string2font(arg, font, b);
        if (font.color() != LColor::inherit) {
-               MathAtom at = createMathInset("color");
-               asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
-               cur.handleNest(at, 1);
+               MathAtom at = MathAtom(new MathColorInset(true, font.color()));
+               cur.handleNest(at, 0);
        }
 }
 
index 6855483c556bf4ea2b542769e7ace234b3cdb9e8..05d57e8d175bd01f13c67f72b0ed89edfba871fb 100644 (file)
@@ -42,6 +42,7 @@ following hack as starting point to write some macros:
 #include "math_arrayinset.h"
 #include "math_braceinset.h"
 #include "math_charinset.h"
+#include "math_colorinset.h"
 #include "math_commentinset.h"
 #include "math_deliminset.h"
 #include "math_envinset.h"
@@ -1207,10 +1208,21 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
                }
 
                else if (t.cs() == "color") {
-                       MathAtom at = createMathInset(t.cs());
-                       parse(at.nucleus()->cell(0), FLAG_ITEM, MathInset::TEXT_MODE);
-                       parse(at.nucleus()->cell(1), flags, mode);
-                       cell->push_back(at);
+                       string const color = parse_verbatim_item();
+                       cell->push_back(MathAtom(new MathColorInset(true, color)));
+                       parse(cell->back().nucleus()->cell(0), flags, mode);
+                       return;
+               }
+
+               else if (t.cs() == "textcolor") {
+                       string const color = parse_verbatim_item();
+                       cell->push_back(MathAtom(new MathColorInset(false, color)));
+                       parse(cell->back().nucleus()->cell(0), FLAG_ITEM, MathInset::TEXT_MODE);
+               }
+
+               else if (t.cs() == "normalcolor") {
+                       cell->push_back(createMathInset(t.cs()));
+                       parse(cell->back().nucleus()->cell(0), flags, mode);
                        return;
                }