From: Georg Baum Date: Wed, 4 May 2005 11:21:14 +0000 (+0000) Subject: undefined charstyle fixes from Martin and me X-Git-Tag: 1.6.10~14326 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=674038dc8c97bd00e9c41aa4b6270aef31f57657;p=features.git undefined charstyle fixes from Martin and me git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@9903 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/ChangeLog b/src/ChangeLog index e0c4d5b286..e790041f34 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,13 @@ +2005-05-03 Georg Baum + + * CutAndPaste.[Ch] (SwitchLayoutsBetweenClasses): rename to + SwitchBetweenClasses and remove the unused return value. + Handle character styles, too + +2005-05-03 Martin Vermeer + + * factory.C (createInset): handle undefined character styles + 2005-05-02 Angus Leeming * buffer.C: protect the #include of utime.h with a preprocessor diff --git a/src/CutAndPaste.C b/src/CutAndPaste.C index 5dd91447f0..e5d2cdb8b6 100644 --- a/src/CutAndPaste.C +++ b/src/CutAndPaste.C @@ -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 +133,7 @@ pasteSelectionHelper(Buffer const & buffer, ParagraphList & pars, } // 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 +373,12 @@ string grabAndEraseSelection(LCursor & cur) } -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 +386,7 @@ int SwitchLayoutsBetweenClasses(textclass_type c1, textclass_type c2, 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 +398,48 @@ int SwitchLayoutsBetweenClasses(textclass_type c1, textclass_type c2, 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(*it); + string const name = inset.params().type; + CharStyles::iterator const found_cs = + tclass2.charstyle(name); + if (found_cs == tclass2.charstyles().end()) { + // The character style is undefined in tclass2 + 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)); + } else if (inset.undefined()) { + // The character style is undefined in + // tclass1 and is defined in tclass2 + inset.setDefined(found_cs); + } + } + } + std::swap(in.paragraphs(), pars); - return ret; } diff --git a/src/CutAndPaste.h b/src/CutAndPaste.h index 6383e50fa4..cb384b868b 100644 --- a/src/CutAndPaste.h +++ b/src/CutAndPaste.h @@ -55,10 +55,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 &); diff --git a/src/factory.C b/src/factory.C index 03a285a3c3..ad810c39af 100644 --- a/src/factory.C +++ b/src/factory.C @@ -88,8 +88,12 @@ InsetBase * createInset(BufferView * bv, FuncRequest const & cmd) 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 +421,12 @@ InsetBase * readInset(LyXLex & lex, Buffer const & buf) 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())); diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index 5424ab08b7..922573ecfe 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -1,3 +1,11 @@ +2005-05-03 Martin Vermeer +2005-05-03 Georg Baum + + * insetcharstyle.[Ch] (undefined, setUndefined, setDefined): new, + handle undefined character styles + * insetcharstyle.[Ch] (latex, linuxdoc, docbook, plaintext, validate): + handle undefined character styles + 2005-04-23 Jürgen Spitzmüller * insettabular.C: handle the LFUN_UNSET_* longtabular methods diff --git a/src/insets/insetcharstyle.C b/src/insets/insetcharstyle.C index 5a8a011827..e98f78bc9a 100644 --- a/src/insets/insetcharstyle.C +++ b/src/insets/insetcharstyle.C @@ -51,16 +51,21 @@ 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) { params_.type = cs->name; - params_.latextype = cs->latextype; - params_.latexname = cs->latexname; - params_.latexparam = cs->latexparam; - params_.font = cs->font; - params_.labelfont = cs->labelfont; + setDefined(cs); init(); } @@ -78,6 +83,33 @@ auto_ptr InsetCharStyle::doClone() const } +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); +} + + +void InsetCharStyle::setDefined(CharStyles::iterator cs) +{ + params_.latextype = cs->latextype; + params_.latexname = cs->latexname; + params_.latexparam = cs->latexparam; + params_.font = cs->font; + params_.labelfont = cs->labelfont; +} + + string const InsetCharStyle::editMessage() const { return _("Opened CharStyle Inset"); @@ -147,9 +179,12 @@ void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const 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 +245,15 @@ bool InsetCharStyle::getStatus(LCursor & cur, FuncRequest const & cmd, 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 +261,11 @@ int InsetCharStyle::latex(Buffer const & buf, ostream & os, 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 +276,19 @@ int InsetCharStyle::docbook(Buffer const & buf, ostream & os, 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 +302,7 @@ int InsetCharStyle::plaintext(Buffer const & buf, ostream & os, void InsetCharStyle::validate(LaTeXFeatures & features) const { + // Force inclusion of preamble snippet in layout file features.require(params_.type); InsetText::validate(features); } diff --git a/src/insets/insetcharstyle.h b/src/insets/insetcharstyle.h index fd25885c65..3b2453e4bd 100644 --- a/src/insets/insetcharstyle.h +++ b/src/insets/insetcharstyle.h @@ -43,8 +43,17 @@ public: */ class InsetCharStyle : public InsetCollapsable { public: + /// Construct an undefined character style + 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(); + /// (Re-)set the character style parameters from \p cs + void setDefined(CharStyles::iterator cs); /// std::string const editMessage() const; /// diff --git a/src/lyxfunc.C b/src/lyxfunc.C index 5ed0a660da..2104c4a859 100644 --- a/src/lyxfunc.C +++ b/src/lyxfunc.C @@ -1468,7 +1468,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd) owner->message(_("Converting document to new document class...")); ErrorList el; - lyx::cap::SwitchLayoutsBetweenClasses( + lyx::cap::SwitchBetweenClasses( old_class, new_class, buffer->paragraphs(), el);