From: Georg Baum Date: Fri, 17 Jun 2005 14:35:19 +0000 (+0000) Subject: fix math color inset UI and parsing X-Git-Tag: 1.6.10~14209 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=02ab1bf675bec2f1f2f101da1a22bbb2aeee01ca;p=lyx.git fix math color inset UI and parsing git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@10083 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/ChangeLog b/src/ChangeLog index 56f2cc53d4..d5448f5a24 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,7 @@ +2005-06-16 Georg Baum + + * LColor.[Ch] (getFromLaTeXName): new + 2005-06-16 Martin Vermeer * text.C (readParagraph): fix bug 1904 (GUI affects LaTeX) diff --git a/src/LColor.C b/src/LColor.C index 2f24d5c9a5..33cf10686d 100644 --- a/src/LColor.C +++ b/src/LColor.C @@ -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 InfoTab; + typedef std::map InfoTab; /// the table of color information InfoTab infotab; - typedef std::map Transform; - /// the transform between colour name string and integer code. - Transform transform; + typedef std::map 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(pimpl_->infotab.size()), lcname); } - return setColor(static_cast(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(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(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]; } diff --git a/src/LColor.h b/src/LColor.h index ab0fdd3a24..b9595e393f 100644 --- a/src/LColor.h +++ b/src/LColor.h @@ -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; diff --git a/src/mathed/ChangeLog b/src/mathed/ChangeLog index 2a91b72e91..b6e0915fa3 100644 --- a/src/mathed/ChangeLog +++ b/src/mathed/ChangeLog @@ -1,3 +1,16 @@ +2005-06-16 Georg Baum + + * 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 * math_hullinset.C (label): avoid warning when assertions are diff --git a/src/mathed/math_colorinset.C b/src/mathed/math_colorinset.C index e258707008..e630ebc95d 100644 --- a/src/mathed/math_colorinset.C +++ b/src/mathed/math_colorinset.C @@ -13,29 +13,36 @@ #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 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_; } diff --git a/src/mathed/math_colorinset.h b/src/mathed/math_colorinset.h index 604fb08098..6eec693f4f 100644 --- a/src/mathed/math_colorinset.h +++ b/src/mathed/math_colorinset.h @@ -12,17 +12,23 @@ #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 diff --git a/src/mathed/math_factory.C b/src/mathed/math_factory.C index 045bf8d1f1..98d4e110e8 100644 --- a/src/mathed/math_factory.C +++ b/src/mathed/math_factory.C @@ -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)); diff --git a/src/mathed/math_hullinset.C b/src/mathed/math_hullinset.C index f7c0902ea2..9d3fb3e878 100644 --- a/src/mathed/math_hullinset.C +++ b/src/mathed/math_hullinset.C @@ -11,6 +11,7 @@ #include #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); } } diff --git a/src/mathed/math_nestinset.C b/src/mathed/math_nestinset.C index 043d092ec2..a22bed8089 100644 --- a/src/mathed/math_nestinset.C +++ b/src/mathed/math_nestinset.C @@ -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); } } diff --git a/src/mathed/math_parser.C b/src/mathed/math_parser.C index 6855483c55..05d57e8d17 100644 --- a/src/mathed/math_parser.C +++ b/src/mathed/math_parser.C @@ -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; }