]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetLayout.cpp
Move isMultiCell() to Cursor, and use it.
[lyx.git] / src / insets / InsetLayout.cpp
index 1de4b1cbe29b2a9ad24671c954754a3530fd3ab0..5a317e01736b5b43cf5ebb2229c361a19f206cc4 100644 (file)
@@ -17,6 +17,9 @@
 #include "Color.h"
 #include "Font.h"
 #include "Lexer.h"
+#include "TextClass.h"
+
+#include "support/debug.h"
 #include "support/lstrings.h"
 
 #include <vector>
@@ -29,6 +32,7 @@ namespace lyx {
 
 InsetLayout::InsetLayout() :
        name_(from_ascii("undefined")), labelstring_(from_ascii("UNDEFINED")),
+       decoration_(InsetLayout::Default),
        font_(sane_font), labelfont_(sane_font), bgcolor_(Color_error), 
        multipar_(false), passthru_(false), needprotect_(false),
        freespacing_(false), keepempty_(false), forceltr_(false)
@@ -37,48 +41,52 @@ InsetLayout::InsetLayout() :
 }
 
 
-enum InsetLayoutTags {
-       IL_FONT = 1,
-       IL_BGCOLOR,
-       IL_DECORATION,
-       IL_FREESPACING,
-       IL_FORCELTR,
-       IL_LABELFONT,
-       IL_LABELSTRING,
-       IL_LATEXNAME,
-       IL_LATEXPARAM,
-       IL_LATEXTYPE,
-       IL_LYXTYPE,
-       IL_KEEPEMPTY,
-       IL_MULTIPAR,
-       IL_NEEDPROTECT,
-       IL_PASSTHRU,
-       IL_PREAMBLE,
-       IL_REQUIRES,
-       IL_END
-};
+namespace {
 
+InsetLayout::InsetDecoration translateDecoration(std::string const & str) 
+{
+       if (str == "classic")
+               return InsetLayout::Classic;
+       if (str == "minimalistic")
+               return InsetLayout::Minimalistic;
+       if (str == "conglomerate")
+               return InsetLayout::Conglomerate;
+       return InsetLayout::Default;
+}
 
-namespace {
-       InsetDecoration translateDecoration(std::string const & str) 
-       {
-               if (str == "classic")
-                       return Deco_Classic;
-               if (str == "minimalistic")
-                       return Deco_Minimalistic;
-               if (str == "conglomerate")
-                       return Deco_Conglomerate;
-               return Deco_Default;
-       }
 }
 
 
-bool InsetLayout::read(Lexer & lexrc)
+bool InsetLayout::read(Lexer & lex, TextClass & tclass)
 {
-       name_ = support::subst(lexrc.getDocString(), '_', ' ');
+       name_ = support::subst(lex.getDocString(), '_', ' ');
+
+       enum {
+               IL_BGCOLOR,
+               IL_COPYSTYLE,
+               IL_DECORATION,
+               IL_FONT,
+               IL_FORCELTR,
+               IL_FREESPACING,
+               IL_LABELFONT,
+               IL_LABELSTRING,
+               IL_LATEXNAME,
+               IL_LATEXPARAM,
+               IL_LATEXTYPE,
+               IL_LYXTYPE,
+               IL_KEEPEMPTY,
+               IL_MULTIPAR,
+               IL_NEEDPROTECT,
+               IL_PASSTHRU,
+               IL_PREAMBLE,
+               IL_REQUIRES,
+               IL_END
+       };
+
 
-       keyword_item elementTags[] = {
+       LexerKeyword elementTags[] = {
                { "bgcolor", IL_BGCOLOR },
+               { "copystyle", IL_COPYSTYLE}, 
                { "decoration", IL_DECORATION },
                { "end", IL_END },
                { "font", IL_FONT },
@@ -98,92 +106,110 @@ bool InsetLayout::read(Lexer & lexrc)
                { "requires", IL_REQUIRES }
        };
 
-       lexrc.pushTable(elementTags, IL_END);
+       lex.pushTable(elementTags);
 
        FontInfo font = inherit_font;
        labelfont_ = inherit_font;
        bgcolor_ = Color_background;
        bool getout = false;
-       
-       while (!getout && lexrc.isOK()) {
-               int le = lexrc.lex();
+
+       string tmp;     
+       while (!getout && lex.isOK()) {
+               int le = lex.lex();
                switch (le) {
                case Lexer::LEX_UNDEF:
-                       lexrc.printError("Unknown InsetLayout tag `$$Token'");
+                       lex.printError("Unknown InsetLayout tag");
                        continue;
-               default: break;
+               default:
+                       break;
                }
-               switch (static_cast<InsetLayoutTags>(le)) {
+               switch (le) {
                case IL_LYXTYPE:
-                       lexrc.next();
-                       lyxtype_ = lexrc.getString();
+                       lex >> lyxtype_;
                        break;
                case IL_LATEXTYPE:
-                       lexrc.next();
-                       latextype_ = lexrc.getString();
+                       lex >> latextype_;
                        break;
                case IL_LABELSTRING:
-                       lexrc.next();
-                       labelstring_ = lexrc.getDocString();
+                       lex >> labelstring_;
                        break;
                case IL_DECORATION:
-                       lexrc.next();
-                       decoration_ = translateDecoration(lexrc.getString());
+                       lex >> tmp;
+                       decoration_ = translateDecoration(tmp);
                        break;
                case IL_LATEXNAME:
-                       lexrc.next();
-                       latexname_ = lexrc.getString();
+                       lex >> latexname_;
                        break;
                case IL_LATEXPARAM:
-                       lexrc.next();
-                       latexparam_ = support::subst(lexrc.getString(), "&quot;", "\"");
+                       lex >> tmp;
+                       latexparam_ = support::subst(tmp, "&quot;", "\"");
                        break;
                case IL_LABELFONT:
-                       labelfont_ = lyxRead(lexrc, inherit_font);
+                       labelfont_ = lyxRead(lex, inherit_font);
                        break;
                case IL_FORCELTR:
-                       lexrc.next();
-                       forceltr_ = lexrc.getBool();
+                       lex >> forceltr_;
                        break;
                case IL_MULTIPAR:
-                       lexrc.next();
-                       multipar_ = lexrc.getBool();
+                       lex >> multipar_;
                        break;
                case IL_PASSTHRU:
-                       lexrc.next();
-                       passthru_ = lexrc.getBool();
+                       lex >> passthru_;
                        break;
                case IL_KEEPEMPTY:
-                       lexrc.next();
-                       keepempty_ = lexrc.getBool();
+                       lex >> keepempty_;
                        break;
                case IL_FREESPACING:
-                       lexrc.next();
-                       freespacing_ = lexrc.getBool();
+                       lex >> freespacing_;
                        break;
                case IL_NEEDPROTECT:
-                       lexrc.next();
-                       needprotect_ = lexrc.getBool();
+                       lex >> needprotect_;
+                       break;
+               case IL_COPYSTYLE: {     // initialize with a known style
+                       docstring style;
+                       lex >> style;
+                       style = support::subst(style, '_', ' ');
+
+                       // We don't want to apply the algorithm in DocumentClass::insetLayout()
+                       // here. So we do it the long way.
+                       TextClass::InsetLayouts::const_iterator it = 
+                                       tclass.insetLayouts().find(style);
+                       if (it != tclass.insetLayouts().end()) {
+                               docstring const tmpname = name_;
+                               this->operator=(it->second);
+                               name_ = tmpname;
+                       } else {
+                               LYXERR0("Cannot copy unknown InsetLayout `"
+                                       << style << "'\n"
+                                       << "All InsetLayouts so far:");
+                               TextClass::InsetLayouts::const_iterator lit = 
+                                               tclass.insetLayouts().begin();
+                               TextClass::InsetLayouts::const_iterator len = 
+                                               tclass.insetLayouts().end();
+                               for (; lit != len; ++lit)
+                                       lyxerr << lit->second.name() << "\n";
+                       }
                        break;
+               }
+
                case IL_FONT: {
-                       font = lyxRead(lexrc, inherit_font);
-                       // So: define font before labelfont
-                       labelfont_ = font;
+                       font_ = lyxRead(lex, inherit_font);
+                       // If you want to define labelfont, you need to do so after
+                       // font is defined.
+                       labelfont_ = font_;
                        break;
                }
-               case IL_BGCOLOR: {
-                       lexrc.next();
-                       string const token = lexrc.getString();
-                       bgcolor_ = lcolor.getFromLyXName(token);
+               case IL_BGCOLOR:
+                       lex >> tmp;
+                       bgcolor_ = lcolor.getFromLyXName(tmp);
                        break;
-               }
                case IL_PREAMBLE:
-                       preamble_ = lexrc.getLongString("EndPreamble");
+                       preamble_ = lex.getLongString("EndPreamble");
                        break;
                case IL_REQUIRES: {
-                       lexrc.eatLine();
+                       lex.eatLine();
                        vector<string> const req 
-                               = support::getVectorFromString(lexrc.getString());
+                               = support::getVectorFromString(lex.getString());
                        requires_.insert(req.begin(), req.end());
                        break;
                }
@@ -201,7 +227,7 @@ bool InsetLayout::read(Lexer & lexrc)
        // any realization against a given context.
        labelfont_.realize(sane_font);
 
-       lexrc.popTable();
+       lex.popTable();
        return true;
 }