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 & tclass)
75 name_ = support::subst(lex.getDocString(), '_', ' ');
76 // FIXME We need to check for name_.empty() here, and
77 // take the same sort of action as in TextClass::read()
78 // if it is empty. Or, better, we could read name_ there,
79 // take action there, etc.
106 LexerKeyword elementTags[] = {
107 { "bgcolor", IL_BGCOLOR },
108 { "copystyle", IL_COPYSTYLE },
109 { "custompars", IL_CUSTOMPARS },
110 { "decoration", IL_DECORATION },
113 { "forceltr", IL_FORCELTR },
114 { "forceplain", IL_FORCEPLAIN },
115 { "freespacing", IL_FREESPACING },
116 { "keepempty", IL_KEEPEMPTY },
117 { "labelfont", IL_LABELFONT },
118 { "labelstring", IL_LABELSTRING },
119 { "latexname", IL_LATEXNAME },
120 { "latexparam", IL_LATEXPARAM },
121 { "latextype", IL_LATEXTYPE },
122 { "lyxtype", IL_LYXTYPE },
123 { "multipar", IL_MULTIPAR },
124 { "needprotect", IL_NEEDPROTECT },
125 { "passthru", IL_PASSTHRU },
126 { "preamble", IL_PREAMBLE },
127 { "requires", IL_REQUIRES }
130 lex.pushTable(elementTags);
132 FontInfo font = inherit_font;
133 labelfont_ = inherit_font;
134 bgcolor_ = Color_none;
136 // whether we've read the CustomPars or ForcePlain tag
137 // for issuing a warning in case MultiPars comes later
138 bool readCustomOrPlain = false;
141 while (!getout && lex.isOK()) {
144 case Lexer::LEX_UNDEF:
145 lex.printError("Unknown InsetLayout tag");
154 lyxtype_ = translateLyXType(lt);
155 if (lyxtype_ == NOLYXTYPE)
156 LYXERR0("Unknown LyXType `" << lt << "'.");
162 latextype_ = translateLaTeXType(lt);
163 if (latextype_ == ILT_ERROR)
164 LYXERR0("Unknown LaTeXType `" << lt << "'.");
172 decoration_ = translateDecoration(tmp);
179 latexparam_ = support::subst(tmp, """, "\"");
182 labelfont_ = lyxRead(lex, inherit_font);
189 // the defaults for these depend upon multipar_
190 if (readCustomOrPlain)
191 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
192 "Previous value may be overwritten!");
193 readCustomOrPlain = false;
194 custompars_ = multipar_;
195 forceplain_ = !multipar_;
199 readCustomOrPlain = true;
206 readCustomOrPlain = true;
217 case IL_COPYSTYLE: { // initialize with a known style
220 style = support::subst(style, '_', ' ');
222 // We don't want to apply the algorithm in DocumentClass::insetLayout()
223 // here. So we do it the long way.
224 TextClass::InsetLayouts::const_iterator it =
225 tclass.insetLayouts().find(style);
226 if (it != tclass.insetLayouts().end()) {
227 docstring const tmpname = name_;
228 this->operator=(it->second);
231 LYXERR0("Cannot copy unknown InsetLayout `"
233 << "All InsetLayouts so far:");
234 TextClass::InsetLayouts::const_iterator lit =
235 tclass.insetLayouts().begin();
236 TextClass::InsetLayouts::const_iterator len =
237 tclass.insetLayouts().end();
238 for (; lit != len; ++lit)
239 lyxerr << lit->second.name() << "\n";
245 font_ = lyxRead(lex, inherit_font);
246 // If you want to define labelfont, you need to do so after
253 bgcolor_ = lcolor.getFromLyXName(tmp);
256 preamble_ = lex.getLongString("EndPreamble");
260 vector<string> const req
261 = support::getVectorFromString(lex.getString());
262 requires_.insert(req.begin(), req.end());
271 // Here add element to list if getout == true
275 // The label font is generally used as-is without
276 // any realization against a given context.
277 labelfont_.realize(sane_font);
284 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
287 if (support::compare_ascii_no_case(str, "charstyle") == 0)
288 return InsetLayout::CHARSTYLE;
289 if (support::compare_ascii_no_case(str, "custom") == 0)
290 return InsetLayout::CUSTOM;
291 if (support::compare_ascii_no_case(str, "element") == 0)
292 return InsetLayout::ELEMENT;
293 if (support::compare_ascii_no_case(str, "end") == 0)
294 return InsetLayout::END;
295 if (support::compare_ascii_no_case(str, "standard") == 0)
296 return InsetLayout::STANDARD;
297 return InsetLayout::NOLYXTYPE;