3 * \file InsetLayout.cpp
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Martin Vermeer
10 * Full author contact details are available in file CREDITS.
15 #include "InsetLayout.h"
20 #include "TextClass.h"
22 #include "support/debug.h"
23 #include "support/lstrings.h"
33 InsetLayout::InsetLayout() :
34 name_(from_ascii("undefined")), lyxtype_(STANDARD),
35 labelstring_(from_ascii("UNDEFINED")), decoration_(DEFAULT),
36 latextype_(NOLATEXTYPE), font_(sane_font),
37 labelfont_(sane_font), bgcolor_(Color_error),
38 multipar_(false), custompars_(false), forceplain_(true),
39 passthru_(false), needprotect_(false), freespacing_(false),
40 keepempty_(false), forceltr_(false)
42 labelfont_.setColor(Color_error);
48 InsetLayout::InsetDecoration translateDecoration(std::string const & str)
50 if (support::compare_ascii_no_case(str, "classic") == 0)
51 return InsetLayout::CLASSIC;
52 if (support::compare_ascii_no_case(str, "minimalistic") == 0)
53 return InsetLayout::MINIMALISTIC;
54 if (support::compare_ascii_no_case(str, "conglomerate") == 0)
55 return InsetLayout::CONGLOMERATE;
56 return InsetLayout::DEFAULT;
59 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
61 if (support::compare_ascii_no_case(str, "command") == 0)
62 return InsetLayout::COMMAND;
63 if (support::compare_ascii_no_case(str, "environment") == 0)
64 return InsetLayout::ENVIRONMENT;
65 if (support::compare_ascii_no_case(str, "none") == 0)
66 return InsetLayout::NOLATEXTYPE;
67 return InsetLayout::ILT_ERROR;
73 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
100 LexerKeyword elementTags[] = {
101 { "bgcolor", IL_BGCOLOR },
102 { "copystyle", IL_COPYSTYLE },
103 { "custompars", IL_CUSTOMPARS },
104 { "decoration", IL_DECORATION },
107 { "forceltr", IL_FORCELTR },
108 { "forceplain", IL_FORCEPLAIN },
109 { "freespacing", IL_FREESPACING },
110 { "keepempty", IL_KEEPEMPTY },
111 { "labelfont", IL_LABELFONT },
112 { "labelstring", IL_LABELSTRING },
113 { "latexname", IL_LATEXNAME },
114 { "latexparam", IL_LATEXPARAM },
115 { "latextype", IL_LATEXTYPE },
116 { "lyxtype", IL_LYXTYPE },
117 { "multipar", IL_MULTIPAR },
118 { "needprotect", IL_NEEDPROTECT },
119 { "passthru", IL_PASSTHRU },
120 { "preamble", IL_PREAMBLE },
121 { "requires", IL_REQUIRES }
124 lex.pushTable(elementTags);
126 FontInfo font = inherit_font;
127 labelfont_ = inherit_font;
128 bgcolor_ = Color_none;
130 // whether we've read the CustomPars or ForcePlain tag
131 // for issuing a warning in case MultiPars comes later
132 bool readCustomOrPlain = false;
135 while (!getout && lex.isOK()) {
138 case Lexer::LEX_UNDEF:
139 lex.printError("Unknown InsetLayout tag");
146 // Perhaps a more elegant way to deal with the next two would be the
147 // way this sort of thing is handled in Layout::read(), namely, by
152 lyxtype_ = translateLyXType(lt);
153 if (lyxtype_ == NOLYXTYPE)
154 LYXERR0("Unknown LyXType `" << lt << "'.");
160 latextype_ = translateLaTeXType(lt);
161 if (latextype_ == ILT_ERROR)
162 LYXERR0("Unknown LaTeXType `" << lt << "'.");
170 decoration_ = translateDecoration(tmp);
177 latexparam_ = support::subst(tmp, """, "\"");
180 labelfont_ = lyxRead(lex, inherit_font);
187 // the defaults for these depend upon multipar_
188 if (readCustomOrPlain)
189 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
190 "Previous value may be overwritten!");
191 readCustomOrPlain = false;
192 custompars_ = multipar_;
193 forceplain_ = !multipar_;
197 readCustomOrPlain = true;
204 readCustomOrPlain = true;
215 case IL_COPYSTYLE: { // initialize with a known style
218 style = support::subst(style, '_', ' ');
220 // We don't want to apply the algorithm in DocumentClass::insetLayout()
221 // here. So we do it the long way.
222 TextClass::InsetLayouts::const_iterator it =
223 tclass.insetLayouts().find(style);
224 if (it != tclass.insetLayouts().end()) {
225 docstring const tmpname = name_;
226 this->operator=(it->second);
229 LYXERR0("Cannot copy unknown InsetLayout `"
231 << "All InsetLayouts so far:");
232 TextClass::InsetLayouts::const_iterator lit =
233 tclass.insetLayouts().begin();
234 TextClass::InsetLayouts::const_iterator len =
235 tclass.insetLayouts().end();
236 for (; lit != len; ++lit)
237 lyxerr << lit->second.name() << "\n";
243 font_ = lyxRead(lex, inherit_font);
244 // If you want to define labelfont, you need to do so after
251 bgcolor_ = lcolor.getFromLyXName(tmp);
254 preamble_ = lex.getLongString("EndPreamble");
258 vector<string> const req
259 = support::getVectorFromString(lex.getString());
260 requires_.insert(req.begin(), req.end());
269 // Here add element to list if getout == true
273 // The label font is generally used as-is without
274 // any realization against a given context.
275 labelfont_.realize(sane_font);
282 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
285 if (support::compare_ascii_no_case(str, "charstyle") == 0)
286 return InsetLayout::CHARSTYLE;
287 if (support::compare_ascii_no_case(str, "custom") == 0)
288 return InsetLayout::CUSTOM;
289 if (support::compare_ascii_no_case(str, "element") == 0)
290 return InsetLayout::ELEMENT;
291 if (support::compare_ascii_no_case(str, "end") == 0)
292 return InsetLayout::END;
293 if (support::compare_ascii_no_case(str, "standard") == 0)
294 return InsetLayout::STANDARD;
295 return InsetLayout::NOLYXTYPE;