Martin Vermeer wrote:

> On Mon, May 02, 2005 at 03:25:39PM +0200, Juergen Spitzmueller wrote:
>> I kind of like the idea (untested), but I think you should trigger an
>> Alert which tells the user that some charstyle insets haven't been found
>> etc.
> 
> Unfortunately that's not easy to do under this approach... you don't
> know it happened until after you saved and re-loaded. That's precisely
> the weakness of this approach.

It is easy. Proof attached (replaces your patch). This is a case where the
dociterator stuff pays off. And I found a missing _() as well.
I think this should go in if it works for you, too (I have not tested it
thoroughly).


Georg
Index: src/CutAndPaste.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/CutAndPaste.C,v
retrieving revision 1.151
diff -u -p -r1.151 CutAndPaste.C
--- src/CutAndPaste.C	26 Apr 2005 11:12:09 -0000	1.151
+++ src/CutAndPaste.C	2 May 2005 16:42:38 -0000
@@ -23,6 +23,7 @@
 #include "errorlist.h"
 #include "funcrequest.h"
 #include "gettext.h"
+#include "insetiterator.h"
 #include "lfuns.h"
 #include "lyxrc.h"
 #include "lyxtext.h"
@@ -34,6 +35,7 @@
 #include "pariterator.h"
 #include "undo.h"
 
+#include "insets/insetcharstyle.h"
 #include "insets/insettabular.h"
 
 #include "mathed/math_data.h"
@@ -131,8 +140,7 @@ pasteSelectionHelper(Buffer const & buff
 	}
 
 	// Make sure there is no class difference.
-	lyx::cap::SwitchLayoutsBetweenClasses(textclass, tc, insertion,
-				    errorlist);
+	lyx::cap::SwitchBetweenClasses(textclass, tc, insertion, errorlist);
 
 	ParagraphList::iterator tmpbuf = insertion.begin();
 	int depth_delta = pars[pit].params().depth() - tmpbuf->params().depth();
@@ -372,13 +383,12 @@ string grabAndEraseSelection(LCursor & c
 }
 
 
-int SwitchLayoutsBetweenClasses(textclass_type c1, textclass_type c2,
+void SwitchBetweenClasses(textclass_type c1, textclass_type c2,
 	ParagraphList & pars, ErrorList & errorlist)
 {
 	BOOST_ASSERT(!pars.empty());
-	int ret = 0;
 	if (c1 == c2)
-		return ret;
+		return;
 
 	LyXTextClass const & tclass1 = textclasslist[c1];
 	LyXTextClass const & tclass2 = textclasslist[c2];
@@ -386,6 +396,7 @@ int SwitchLayoutsBetweenClasses(textclas
 	InsetText in;
 	std::swap(in.paragraphs(), pars);
 
+	// layouts
 	ParIterator end = par_iterator_end(in);
 	for (ParIterator it = par_iterator_begin(in); it != end; ++it) {
 		string const name = it->layout()->name();
@@ -397,19 +408,43 @@ int SwitchLayoutsBetweenClasses(textclas
 			it->layout(tclass2.defaultLayout());
 
 		if (!hasLayout && name != tclass1.defaultLayoutName()) {
-			++ret;
 			string const s = bformat(
 				_("Layout had to be changed from\n%1$s to %2$s\n"
 				"because of class conversion from\n%3$s to %4$s"),
 			 name, it->layout()->name(), tclass1.name(), tclass2.name());
 			// To warn the user that something had to be done.
-			errorlist.push_back(ErrorItem("Changed Layout", s,
+			errorlist.push_back(ErrorItem(_("Changed Layout"), s,
 						      it->id(), 0,
 						      it->size()));
 		}
 	}
+
+	// character styles
+	InsetIterator const i_end = inset_iterator_end(in);
+	for (InsetIterator it = inset_iterator_begin(in); it != i_end; ++it) {
+		if (it->lyxCode() == InsetBase::CHARSTYLE_CODE) {
+			InsetCharStyle & inset =
+				static_cast<InsetCharStyle &>(*it);
+			string const name = inset.params().type;
+			CharStyles::iterator const found_cs =
+				tclass2.charstyle(name);
+			if (found_cs == tclass2.charstyles().end()) {
+				inset.setUndefined();
+				string const s = bformat(_(
+					"Character style %1$s is "
+					"undefined because of class "
+					"conversion from\n%2$s to %3$s"),
+					 name, tclass1.name(), tclass2.name());
+				// To warn the user that something had to be done.
+				errorlist.push_back(ErrorItem(
+						_("Undefined character style"),
+						s, it.paragraph().id(),
+						it.pos(), it.pos() + 1));
+			}
+		}
+	}
+
 	std::swap(in.paragraphs(), pars);
-	return ret;
 }
 
 
Index: src/CutAndPaste.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/CutAndPaste.h,v
retrieving revision 1.38
diff -u -p -r1.38 CutAndPaste.h
--- src/CutAndPaste.h	5 Oct 2004 10:11:26 -0000	1.38
+++ src/CutAndPaste.h	2 May 2005 16:42:38 -0000
@@ -55,10 +58,10 @@ void copySelection(LCursor & cur);
 void pasteSelection(LCursor & cur, size_t sel_index = 0);
 
 /** Needed to switch between different classes. This works
- *  for a list of paragraphs beginning with the specified par
- *  return value is the number of wrong conversions.
+ *  for a list of paragraphs beginning with the specified par.
+ *  It changes layouts and character styles.
  */
-int SwitchLayoutsBetweenClasses(lyx::textclass_type c1,
+void SwitchBetweenClasses(lyx::textclass_type c1,
 				lyx::textclass_type c2,
 				ParagraphList & par,
 				ErrorList &);
Index: src/factory.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/factory.C,v
retrieving revision 1.95
diff -u -p -r1.95 factory.C
--- src/factory.C	14 Aug 2004 15:55:17 -0000	1.95
+++ src/factory.C	2 May 2005 16:42:39 -0000
@@ -88,8 +90,12 @@ InsetBase * createInset(BufferView * bv,
 
 	case LFUN_INSERT_CHARSTYLE: {
 		string s = cmd.getArg(0);
-		CharStyles::iterator found_cs = params.getLyXTextClass().charstyle(s);
-		return new InsetCharStyle(params, found_cs);
+		LyXTextClass tclass = params.getLyXTextClass();
+		CharStyles::iterator found_cs = tclass.charstyle(s);
+		if (found_cs != tclass.charstyles().end())
+			return new InsetCharStyle(params, found_cs);
+		else
+			return new InsetCharStyle(params, s);
 	}
 
 	case LFUN_INSERT_NOTE: {
@@ -417,7 +434,12 @@ InsetBase * readInset(LyXLex & lex, Buff
 			lex.next();
 			string s = lex.getString();
 			CharStyles::iterator found_cs = tclass.charstyle(s);
-			inset.reset(new InsetCharStyle(buf.params(), found_cs));
+			if (found_cs != tclass.charstyles().end())
+				inset.reset(new InsetCharStyle(buf.params(), found_cs));
+			else {
+				// "Undefined" inset
+				inset.reset(new InsetCharStyle(buf.params(), s));
+			}
 		} else if (tmptok == "Branch") {
 			inset.reset(new InsetBranch(buf.params(),
 						    InsetBranchParams()));
Index: src/lyxfunc.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/lyxfunc.C,v
retrieving revision 1.652
diff -u -p -r1.652 lyxfunc.C
--- src/lyxfunc.C	19 Apr 2005 09:04:24 -0000	1.652
+++ src/lyxfunc.C	2 May 2005 16:42:39 -0000
@@ -1468,7 +1471,7 @@ void LyXFunc::dispatch(FuncRequest const
 
 			owner->message(_("Converting document to new document class..."));
 			ErrorList el;
-			lyx::cap::SwitchLayoutsBetweenClasses(
+			lyx::cap::SwitchBetweenClasses(
 				old_class, new_class,
 				buffer->paragraphs(), el);
 
Index: src/insets/insetcharstyle.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insetcharstyle.C,v
retrieving revision 1.33
diff -u -p -r1.33 insetcharstyle.C
--- src/insets/insetcharstyle.C	26 Apr 2005 11:12:19 -0000	1.33
+++ src/insets/insetcharstyle.C	2 May 2005 16:42:39 -0000
@@ -51,6 +51,15 @@ void InsetCharStyle::init()
 }
 
 
+InsetCharStyle::InsetCharStyle(BufferParams const & bp, string const s)
+	: InsetCollapsable(bp)
+{
+	params_.type = s;
+	setUndefined();
+	init();
+}
+
+
 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
 				CharStyles::iterator cs)
 	: InsetCollapsable(bp)
@@ -78,6 +87,23 @@ auto_ptr<InsetBase> InsetCharStyle::doCl
 }
 
 
+bool InsetCharStyle::undefined() const
+{
+	return params_.latexname.empty();
+}
+
+
+void InsetCharStyle::setUndefined()
+{
+	params_.latextype.clear();
+	params_.latexname.clear();
+	params_.latexparam.clear();
+	params_.font = LyXFont(LyXFont::ALL_INHERIT);
+	params_.labelfont = LyXFont(LyXFont::ALL_INHERIT);
+	params_.labelfont.setColor(LColor::red);
+}
+
+
 string const InsetCharStyle::editMessage() const
 {
 	return _("Opened CharStyle Inset");
@@ -147,9 +173,12 @@ void InsetCharStyle::draw(PainterInfo & 
 		int w = 0;
 		int a = 0;
 		int d = 0;
+		string s(params_.type);
+		if (undefined())
+			s = _("Undef: ") + s;
 		font_metrics::rectText(params_.type, font, w, a, d);
 		pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
-			params_.type, font, LColor::none, LColor::none);
+			s, font, LColor::none, LColor::none);
 	}
 
 	// a visual clue when the cursor is inside the inset
@@ -210,12 +239,15 @@ bool InsetCharStyle::getStatus(LCursor &
 int InsetCharStyle::latex(Buffer const & buf, ostream & os,
 		     OutputParams const & runparams) const
 {
-	os << "\\" << params_.latexname;
-	if (!params_.latexparam.empty())
-		os << params_.latexparam;
-	os << "{";
+	if (!undefined()) {
+		os << "\\" << params_.latexname;
+		if (!params_.latexparam.empty())
+			os << params_.latexparam;
+		os << "{";
+	}
 	int i = InsetText::latex(buf, os, runparams);
-	os << "}";
+	if (!undefined())
+		os << "}";
 	return i;
 }
 
@@ -223,9 +255,11 @@ int InsetCharStyle::latex(Buffer const &
 int InsetCharStyle::linuxdoc(Buffer const & buf, ostream & os,
 			     OutputParams const & runparams) const
 {
-	sgml::openTag(os, params_.latexname, params_.latexparam);
+	if (!undefined())
+		sgml::openTag(os, params_.latexname, params_.latexparam);
 	int i = InsetText::linuxdoc(buf, os, runparams);
-	sgml::closeTag(os, params_.latexname);
+	if (!undefined())
+		sgml::closeTag(os, params_.latexname);
 	return i;
 }
 
@@ -236,15 +270,19 @@ int InsetCharStyle::docbook(Buffer const
 	ParagraphList::const_iterator par = paragraphs().begin();
         ParagraphList::const_iterator end = paragraphs().end();
 
-	sgml::openTag(os, params_.latexname, par->getID(buf, runparams) + params_.latexparam);
+	if (!undefined())
+		sgml::openTag(os, params_.latexname, 
+			par->getID(buf, runparams) + params_.latexparam);
 
         for (; par != end; ++par) {
 		par->simpleDocBookOnePar(buf, os, runparams,
-					 outerFont(par - paragraphs().begin(),
+				 outerFont(par - paragraphs().begin(),
 						   paragraphs()));
         }
 
-	sgml::closeTag(os, params_.latexname);
+	if (!undefined())
+		sgml::closeTag(os, params_.latexname);
+
 	return 0;
 }
 
@@ -258,6 +296,7 @@ int InsetCharStyle::plaintext(Buffer con
 
 void InsetCharStyle::validate(LaTeXFeatures & features) const
 {
+	// Force inclusion of preamble snippet in layout file
 	features.require(params_.type);
 	InsetText::validate(features);
 }
Index: src/insets/insetcharstyle.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insetcharstyle.h,v
retrieving revision 1.13
diff -u -p -r1.13 insetcharstyle.h
--- src/insets/insetcharstyle.h	19 Jan 2005 15:03:30 -0000	1.13
+++ src/insets/insetcharstyle.h	2 May 2005 16:42:39 -0000
@@ -44,7 +44,14 @@ public:
 class InsetCharStyle : public InsetCollapsable {
 public:
 	///
+	InsetCharStyle::InsetCharStyle(BufferParams const &, std::string const);
+	///
 	InsetCharStyle(BufferParams const &, CharStyles::iterator);
+	/// Is this character style defined in the document's textclass?
+	/// May be wrong after textclass change or paste from another document
+	bool undefined() const;
+	/// Set the character style to "undefined"
+	void setUndefined();
 	///
 	std::string const editMessage() const;
 	///

Reply via email to