Juergen Spitzmueller wrote:

> Yes, that would be the best (i.e. implement getFromLaTeXName).

I did that, and even did it properly.

>> if (normalcolor(cell(0)))
>>         os << "{\\normalcolor " << cell(1) << '}';
>> else if (!latexcolor(cell(0)))
>>         os << cell(1);
>> else if (oldstyle_)
>>         os << "{\\color" << '{' << cell(0) << '}' << cell(1) << '}';
>> else
>>         os << "\\textcolor" << '{' << cell(0) << "}{" << cell(1) << '}';
> 
> Looks good IMO.

Actually we should not check for invalid colors, since we don't know the
user defined ones.

>> And we need to modify math_parser.C and/or math_factory.C in order to be
>> able to read that in.
> 
> Yes, but that's your world anyway :-)

Done. Please test. I also changed inheritance from MathNestInset to
MathGridInset in order to get the corner markers. Otherwise it is
impossible to delete a color inset by pressing backspace at the beginning.

The disadvantage of this patch is that user defined colors are not editable
in LyX anymore. They are correctly read and written, so that you don't lose
data. IMHO the better UI for supported colors outweighs this disadvantage.
What do you think?

The only remaining drawback is that the inherited color is wrong (black
instead of blue) if we are not inside a text box. Does anybody know how to
fix that?


Georg
Index: src/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/ChangeLog,v
retrieving revision 1.2199
diff -u -p -r1.2199 ChangeLog
--- src/ChangeLog	9 Jun 2005 15:19:06 -0000	1.2199
+++ src/ChangeLog	14 Jun 2005 09:58:47 -0000
@@ -1,3 +1,7 @@
+2005-06-14  Georg Baum  <[EMAIL PROTECTED]>
+
+	* LColor.[Ch] (getFromLaTeXName): new
+
 2005-06-11  Georg Baum  <[EMAIL PROTECTED]>
 
 	* LaTeXFeatures.C (getPackages): solve amsmath-wasysym conflict
Index: src/LColor.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/LColor.C,v
retrieving revision 1.55
diff -u -p -r1.55 LColor.C
--- src/LColor.C	19 Jan 2005 15:03:27 -0000	1.55
+++ src/LColor.C	14 Jun 2005 09:58:47 -0000
@@ -66,7 +66,8 @@ public:
 		in.x11name   = string(entry.x11name);
 		in.guiname   = string(entry.guiname);
 		infotab[entry.lcolor] = in;
-		transform[string(entry.lyxname)] = int(entry.lcolor);
+		LyXTransform[string(entry.lyxname)] = int(entry.lcolor);
+		LaTeXTransform[string(entry.latexname)] = int(entry.lcolor);
 	}
 
 	///
@@ -75,8 +76,10 @@ public:
 	InfoTab infotab;
 
 	typedef std::map<string, int> Transform;
-	/// the transform between colour name string and integer code.
-	Transform transform;
+	/// the transform between LyX color name string and integer code.
+	Transform LyXTransform;
+	/// the transform between LaTeX color name string and integer code.
+	Transform LaTeXTransform;
 
 };
 
@@ -234,14 +237,14 @@ bool LColor::setColor(LColor::color col,
 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_->LyXTransform.find(lcname) == pimpl_->LyXTransform.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]),
+	return setColor(static_cast<LColor::color>(pimpl_->LyXTransform[lcname]),
 			x11name);
 }
 
@@ -268,13 +271,25 @@ void LColor::addColor(LColor::color c, s
 LColor::color LColor::getFromLyXName(string const & lyxname) const
 {
 	string const lcname = ascii_lowercase(lyxname);
-	if (pimpl_->transform.find(lcname) == pimpl_->transform.end()) {
+	if (pimpl_->LyXTransform.find(lcname) == pimpl_->LyXTransform.end()) {
 		lyxerr << "LColor::getFromLyXName: Unknown color \""
 		       << lyxname << '"' << endl;
 		return none;
 	}
 
-	return static_cast<LColor::color>(pimpl_->transform[lcname]);
+	return static_cast<LColor::color>(pimpl_->LyXTransform[lcname]);
+}
+
+
+LColor::color LColor::getFromLaTeXName(string const & latexname) const
+{
+	if (pimpl_->LaTeXTransform.find(latexname) == pimpl_->LaTeXTransform.end()) {
+		lyxerr << "LColor::getFromLaTeXName: Unknown color \""
+		       << latexname << '"' << endl;
+		return none;
+	}
+
+	return static_cast<LColor::color>(pimpl_->LaTeXTransform[latexname]);
 }
 
 
Index: src/LColor.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/LColor.h,v
retrieving revision 1.44
diff -u -p -r1.44 LColor.h
--- src/LColor.h	19 Jan 2005 15:03:27 -0000	1.44
+++ src/LColor.h	14 Jun 2005 09:58:47 -0000
@@ -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: src/mathed/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/ChangeLog,v
retrieving revision 1.499
diff -u -p -r1.499 ChangeLog
--- src/mathed/ChangeLog	9 Jun 2005 12:17:50 -0000	1.499
+++ src/mathed/ChangeLog	14 Jun 2005 10:00:07 -0000
@@ -1,3 +1,23 @@
+2005-06-14  Georg Baum  <[EMAIL PROTECTED]>
+
+	* 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  <[EMAIL PROTECTED]>
 
 	* math_hullinset.C (label): avoid warning when assertions are
Index: src/mathed/math_colorinset.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_colorinset.C,v
retrieving revision 1.12
diff -u -p -r1.12 math_colorinset.C
--- src/mathed/math_colorinset.C	9 Apr 2005 11:13:21 -0000	1.12
+++ src/mathed/math_colorinset.C	14 Jun 2005 10:00:07 -0000
@@ -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"
@@ -21,21 +22,28 @@
 #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)
+	: MathGridInset(1, 1), oldstyle_(oldstyle),
+	  color_(lcolor.getLaTeXName(color))
+{}
+
+
+MathColorInset::MathColorInset(bool oldstyle, string const & color)
+	: MathGridInset(1, 1), oldstyle_(oldstyle), color_(color)
 {}
 
 
@@ -47,70 +55,63 @@ auto_ptr<InsetBase> MathColorInset::doCl
 
 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);
-	}
+	MathGridInset::metrics(mi, 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_));
+	MathGridInset::draw(pi, x, y);
 	pi.base.font.setColor(origcol);
-	if (editing(pi.base.bv))
-		setPosCache(pi, x0, y);
+	setPosCache(pi, x, y);
 }
 
 
 void MathColorInset::validate(LaTeXFeatures & features) const
 {
-	MathNestInset::validate(features);
-	if (!normalcolor(cell(0)))
+	MathGridInset::validate(features);
+	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_;
+}
+
+
+void MathColorInset::doDispatch(LCursor & cur, FuncRequest & cmd)
+{
+	// Short circuit MathGridInset::doDispatch
+	MathNestInset::doDispatch(cur, cmd);
+}
+
+
+bool MathColorInset::getStatus(LCursor & cur, FuncRequest const & cmd,
+		FuncStatus & flag) const
+{
+	// Short circuit MathGridInset::getStatus
+	return MathNestInset::getStatus(cur, cmd, flag);
 }
Index: src/mathed/math_colorinset.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_colorinset.h,v
retrieving revision 1.4
diff -u -p -r1.4 math_colorinset.h
--- src/mathed/math_colorinset.h	23 Nov 2004 23:04:50 -0000	1.4
+++ src/mathed/math_colorinset.h	14 Jun 2005 10:00:07 -0000
@@ -12,18 +12,24 @@
 #ifndef MATH_COLORINSET_H
 #define MATH_COLORINSET_H
 
-#include "math_nestinset.h"
+#include "LColor.h"
+
+#include "math_gridinset.h"
 
 /// Change colours.
 
-class MathColorInset : public MathNestInset {
+class MathColorInset : public MathGridInset {
 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...
-	bool extraBraces() const { return true; }
+	/// FIXME Why? Are they necessary if oldstyle_ == false?
+	bool extraBraces() const { return true; }
 	///
 	void draw(PainterInfo & pi, int x, int y) const;
 	/// we need package color
@@ -34,12 +40,20 @@ public:
 	void normalize(NormalStream & ns) const;
 	///
 	void infoize(std::ostream & os) const;
+protected:
+	///
+	virtual void doDispatch(LCursor & cur, FuncRequest & cmd);
+	///
+	bool getStatus(LCursor & cur, FuncRequest const & cmd,
+		FuncStatus & flag) const;
 private:
 	virtual std::auto_ptr<InsetBase> doClone() const;
 	/// width of '[' in current font
 	mutable int w_;
 	///
 	bool oldstyle_;
+	/// Our color. Only valid LaTeX colors are allowed.
+	std::string color_;
 };
 
 #endif
Index: src/mathed/math_factory.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_factory.C,v
retrieving revision 1.111
diff -u -p -r1.111 math_factory.C
--- src/mathed/math_factory.C	7 Jun 2005 17:21:51 -0000	1.111
+++ src/mathed/math_factory.C	14 Jun 2005 10:00:07 -0000
@@ -323,7 +328,7 @@ MathAtom createMathInset(string const & 
 		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: src/mathed/math_hullinset.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_hullinset.C,v
retrieving revision 1.166
diff -u -p -r1.166 math_hullinset.C
--- src/mathed/math_hullinset.C	9 Jun 2005 12:17:50 -0000	1.166
+++ src/mathed/math_hullinset.C	14 Jun 2005 10:00:08 -0000
@@ -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 +1303,8 @@ void MathHullInset::handleFont2(LCursor 
 	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: src/mathed/math_nestinset.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_nestinset.C,v
retrieving revision 1.155
diff -u -p -r1.155 math_nestinset.C
--- src/mathed/math_nestinset.C	9 Jun 2005 09:58:07 -0000	1.155
+++ src/mathed/math_nestinset.C	14 Jun 2005 10:00:08 -0000
@@ -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 +396,8 @@ void MathNestInset::handleFont2(LCursor 
 	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: src/mathed/math_parser.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_parser.C,v
retrieving revision 1.312
diff -u -p -r1.312 math_parser.C
--- src/mathed/math_parser.C	2 Mar 2005 14:26:12 -0000	1.312
+++ src/mathed/math_parser.C	14 Jun 2005 10:00:08 -0000
@@ -42,6 +42,7 @@ following hack as starting point to writ
 #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 +1224,21 @@ void Parser::parse1(MathGridInset & grid
 		}
 
 		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;
 		}
 

Reply via email to