]> git.lyx.org Git - features.git/commitdiff
undefined charstyle fixes from Martin and me
authorGeorg Baum <Georg.Baum@post.rwth-aachen.de>
Wed, 4 May 2005 11:21:14 +0000 (11:21 +0000)
committerGeorg Baum <Georg.Baum@post.rwth-aachen.de>
Wed, 4 May 2005 11:21:14 +0000 (11:21 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@9903 a592a061-630c-0410-9148-cb99ea01b6c8

src/ChangeLog
src/CutAndPaste.C
src/CutAndPaste.h
src/factory.C
src/insets/ChangeLog
src/insets/insetcharstyle.C
src/insets/insetcharstyle.h
src/lyxfunc.C

index e0c4d5b2864067fbf7c0198354f0b204a802bba0..e790041f349fdb414f369a24a4c9961092b5ab78 100644 (file)
@@ -1,3 +1,13 @@
+2005-05-03  Georg Baum  <Georg.Baum@post.rwth-aachen.de>
+
+       * CutAndPaste.[Ch] (SwitchLayoutsBetweenClasses): rename to
+       SwitchBetweenClasses and remove the unused return value.
+       Handle character styles, too
+
+2005-05-03  Martin Vermeer  <martin.vermeer@hut.fi>
+
+       * factory.C (createInset): handle undefined character styles
+
 2005-05-02  Angus Leeming  <leeming@lyx.org>
 
        * buffer.C: protect the #include of utime.h with a preprocessor
index 5dd91447f06a9fdbf164e8cf446accd49ff1d3a3..e5d2cdb8b624794092c61b114e16c8d88174f08a 100644 (file)
@@ -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<InsetCharStyle &>(*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;
 }
 
 
index 6383e50fa4076e081f14009a3928d415f52ad821..cb384b868b5e27e96ea031e592c31e271b42e528 100644 (file)
@@ -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 &);
index 03a285a3c3e0e007b6eea7644f1f28878602148c..ad810c39af9f66d60e7e43c21de736c0773f222a 100644 (file)
@@ -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()));
index 5424ab08b7ab3d75e6865f9517ef18ddea49bc59..922573ecfe8ad1a0dc2664cdc3446d697a37fc68 100644 (file)
@@ -1,3 +1,11 @@
+2005-05-03  Martin Vermeer  <martin.vermeer@hut.fi>
+2005-05-03  Georg Baum  <Georg.Baum@post.rwth-aachen.de>
+
+       * 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  <j.spitzmueller@gmx.de>
 
        * insettabular.C: handle the LFUN_UNSET_* longtabular methods
index 5a8a0118271483154b4f3ab5bef1faad377cc2ce..e98f78bc9ae5440e42bcd4a18029831afe1c8571 100644 (file)
@@ -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<InsetBase> 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);
 }
index fd25885c65920809b90215c1f4d26f2bd3d67ba1..3b2453e4bdc96f276fa211c88b7c05b7c81fddcc 100644 (file)
@@ -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;
        ///
index 5ed0a660daef65c05fdf43fbdb6a14b36620a28e..2104c4a859a01f8522b4a2dafa8bcd082bea19e2 100644 (file)
@@ -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);