]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLayout.cpp
4049512a0fede2cf69a117e76f4e4b99e2aaaeef
[lyx.git] / src / insets / InsetLayout.cpp
1 // -*- C++ -*-
2 /**
3  * \file InsetLayout.cpp
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Martin Vermeer
8  * \author Richard Heck
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetLayout.h"
16
17 #include "Color.h"
18 #include "Font.h"
19 #include "Lexer.h"
20 #include "TextClass.h"
21
22 #include "support/debug.h"
23 #include "support/lstrings.h"
24
25 #include <vector>
26
27 using std::string;
28 using std::set;
29 using std::vector;
30
31 namespace lyx {
32
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)
41
42         labelfont_.setColor(Color_error);
43 }
44
45
46 namespace {
47
48 InsetLayout::InsetDecoration translateDecoration(std::string const & str) 
49 {
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;
57 }
58
59 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
60 {
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;
68 }
69
70 }
71
72
73 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
74 {
75         enum {
76                 IL_BGCOLOR,
77                 IL_COPYSTYLE,
78                 IL_CUSTOMPARS,
79                 IL_DECORATION,
80                 IL_FONT,
81                 IL_FORCELTR,
82                 IL_FORCEPLAIN,
83                 IL_FREESPACING,
84                 IL_LABELFONT,
85                 IL_LABELSTRING,
86                 IL_LATEXNAME,
87                 IL_LATEXPARAM,
88                 IL_LATEXTYPE,
89                 IL_LYXTYPE,
90                 IL_KEEPEMPTY,
91                 IL_MULTIPAR,
92                 IL_NEEDPROTECT,
93                 IL_PASSTHRU,
94                 IL_PREAMBLE,
95                 IL_REQUIRES,
96                 IL_END
97         };
98
99
100         LexerKeyword elementTags[] = {
101                 { "bgcolor", IL_BGCOLOR },
102                 { "copystyle", IL_COPYSTYLE }, 
103                 { "custompars", IL_CUSTOMPARS },
104                 { "decoration", IL_DECORATION },
105                 { "end", IL_END },
106                 { "font", IL_FONT },
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 }
122         };
123
124         lex.pushTable(elementTags);
125
126         FontInfo font = inherit_font;
127         labelfont_ = inherit_font;
128         bgcolor_ = Color_none;
129         bool getout = false;
130         // whether we've read the CustomPars or ForcePlain tag
131         // for issuing a warning in case MultiPars comes later
132         bool readCustomOrPlain = false;
133
134         string tmp;     
135         while (!getout && lex.isOK()) {
136                 int le = lex.lex();
137                 switch (le) {
138                 case Lexer::LEX_UNDEF:
139                         lex.printError("Unknown InsetLayout tag");
140                         continue;
141                 default:
142                         break;
143                 }
144                 switch (le) {
145                 // FIXME
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
148                 // using the Lexer.
149                 case IL_LYXTYPE: {
150                         string lt;
151                         lex >> lt;
152                         lyxtype_ = translateLyXType(lt);
153                         if (lyxtype_  == NOLYXTYPE)
154                                 LYXERR0("Unknown LyXType `" << lt << "'.");
155                         break;
156                 }
157                 case IL_LATEXTYPE:  {
158                         string lt;
159                         lex >> lt;
160                         latextype_ = translateLaTeXType(lt);
161                         if (latextype_  == ILT_ERROR)
162                                 LYXERR0("Unknown LaTeXType `" << lt << "'.");
163                         break;
164                 }
165                 case IL_LABELSTRING:
166                         lex >> labelstring_;
167                         break;
168                 case IL_DECORATION:
169                         lex >> tmp;
170                         decoration_ = translateDecoration(tmp);
171                         break;
172                 case IL_LATEXNAME:
173                         lex >> latexname_;
174                         break;
175                 case IL_LATEXPARAM:
176                         lex >> tmp;
177                         latexparam_ = support::subst(tmp, "&quot;", "\"");
178                         break;
179                 case IL_LABELFONT:
180                         labelfont_ = lyxRead(lex, inherit_font);
181                         break;
182                 case IL_FORCELTR:
183                         lex >> forceltr_;
184                         break;
185                 case IL_MULTIPAR:
186                         lex >> multipar_;
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_;
194                         break;
195                 case IL_CUSTOMPARS:
196                         lex >> custompars_;
197                         readCustomOrPlain = true;
198                         break;
199                 case IL_FORCEPLAIN:
200                         lex >> forceplain_;
201                         break;
202                 case IL_PASSTHRU:
203                         lex >> passthru_;
204                         readCustomOrPlain = true;
205                         break;
206                 case IL_KEEPEMPTY:
207                         lex >> keepempty_;
208                         break;
209                 case IL_FREESPACING:
210                         lex >> freespacing_;
211                         break;
212                 case IL_NEEDPROTECT:
213                         lex >> needprotect_;
214                         break;
215                 case IL_COPYSTYLE: {     // initialize with a known style
216                         docstring style;
217                         lex >> style;
218                         style = support::subst(style, '_', ' ');
219
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);
227                                 name_ = tmpname;
228                         } else {
229                                 LYXERR0("Cannot copy unknown InsetLayout `"
230                                         << style << "'\n"
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";
238                         }
239                         break;
240                 }
241
242                 case IL_FONT: {
243                         font_ = lyxRead(lex, inherit_font);
244                         // If you want to define labelfont, you need to do so after
245                         // font is defined.
246                         labelfont_ = font_;
247                         break;
248                 }
249                 case IL_BGCOLOR:
250                         lex >> tmp;
251                         bgcolor_ = lcolor.getFromLyXName(tmp);
252                         break;
253                 case IL_PREAMBLE:
254                         preamble_ = lex.getLongString("EndPreamble");
255                         break;
256                 case IL_REQUIRES: {
257                         lex.eatLine();
258                         vector<string> const req 
259                                 = support::getVectorFromString(lex.getString());
260                         requires_.insert(req.begin(), req.end());
261                         break;
262                 }
263                 case IL_END:
264                         getout = true;
265                         break;
266                 }
267         }
268
269         // Here add element to list if getout == true
270         if (!getout)
271                 return false;
272         
273         // The label font is generally used as-is without
274         // any realization against a given context.
275         labelfont_.realize(sane_font);
276
277         lex.popTable();
278         return true;
279 }
280
281
282 InsetLayout::InsetLyXType translateLyXType(std::string const & str) 
283 {
284         
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;
296 }
297
298 } //namespace lyx