]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLayout.cpp
c4b6e896c6effb05334175422edb1e824ad68e86
[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 & tclass)
74 {
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.
80
81         enum {
82                 IL_BGCOLOR,
83                 IL_COPYSTYLE,
84                 IL_CUSTOMPARS,
85                 IL_DECORATION,
86                 IL_FONT,
87                 IL_FORCELTR,
88                 IL_FORCEPLAIN,
89                 IL_FREESPACING,
90                 IL_LABELFONT,
91                 IL_LABELSTRING,
92                 IL_LATEXNAME,
93                 IL_LATEXPARAM,
94                 IL_LATEXTYPE,
95                 IL_LYXTYPE,
96                 IL_KEEPEMPTY,
97                 IL_MULTIPAR,
98                 IL_NEEDPROTECT,
99                 IL_PASSTHRU,
100                 IL_PREAMBLE,
101                 IL_REQUIRES,
102                 IL_END
103         };
104
105
106         LexerKeyword elementTags[] = {
107                 { "bgcolor", IL_BGCOLOR },
108                 { "copystyle", IL_COPYSTYLE }, 
109                 { "custompars", IL_CUSTOMPARS },
110                 { "decoration", IL_DECORATION },
111                 { "end", IL_END },
112                 { "font", IL_FONT },
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 }
128         };
129
130         lex.pushTable(elementTags);
131
132         FontInfo font = inherit_font;
133         labelfont_ = inherit_font;
134         bgcolor_ = Color_none;
135         bool getout = false;
136         // whether we've read the CustomPars or ForcePlain tag
137         // for issuing a warning in case MultiPars comes later
138         bool readCustomOrPlain = false;
139
140         string tmp;     
141         while (!getout && lex.isOK()) {
142                 int le = lex.lex();
143                 switch (le) {
144                 case Lexer::LEX_UNDEF:
145                         lex.printError("Unknown InsetLayout tag");
146                         continue;
147                 default:
148                         break;
149                 }
150                 switch (le) {
151                 // FIXME
152                 // Perhaps a mroe elegant way to deal with the next two would be the
153                 // way this sort of thing is handled in Layout::read(), namely, by
154                 // using the Lexer.
155                 case IL_LYXTYPE: {
156                         string lt;
157                         lex >> lt;
158                         lyxtype_ = translateLyXType(lt);
159                         if (lyxtype_  == NOLYXTYPE)
160                                 LYXERR0("Unknown LyXType `" << lt << "'.");
161                         break;
162                 }
163                 case IL_LATEXTYPE:  {
164                         string lt;
165                         lex >> lt;
166                         latextype_ = translateLaTeXType(lt);
167                         if (latextype_  == ILT_ERROR)
168                                 LYXERR0("Unknown LaTeXType `" << lt << "'.");
169                         break;
170                 }
171                 case IL_LABELSTRING:
172                         lex >> labelstring_;
173                         break;
174                 case IL_DECORATION:
175                         lex >> tmp;
176                         decoration_ = translateDecoration(tmp);
177                         break;
178                 case IL_LATEXNAME:
179                         lex >> latexname_;
180                         break;
181                 case IL_LATEXPARAM:
182                         lex >> tmp;
183                         latexparam_ = support::subst(tmp, "&quot;", "\"");
184                         break;
185                 case IL_LABELFONT:
186                         labelfont_ = lyxRead(lex, inherit_font);
187                         break;
188                 case IL_FORCELTR:
189                         lex >> forceltr_;
190                         break;
191                 case IL_MULTIPAR:
192                         lex >> multipar_;
193                         // the defaults for these depend upon multipar_
194                         if (readCustomOrPlain)
195                                 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
196                                         "Previous value may be overwritten!");
197                         readCustomOrPlain = false;
198                         custompars_ = multipar_;
199                         forceplain_ = !multipar_;
200                         break;
201                 case IL_CUSTOMPARS:
202                         lex >> custompars_;
203                         readCustomOrPlain = true;
204                         break;
205                 case IL_FORCEPLAIN:
206                         lex >> forceplain_;
207                         break;
208                 case IL_PASSTHRU:
209                         lex >> passthru_;
210                         readCustomOrPlain = true;
211                         break;
212                 case IL_KEEPEMPTY:
213                         lex >> keepempty_;
214                         break;
215                 case IL_FREESPACING:
216                         lex >> freespacing_;
217                         break;
218                 case IL_NEEDPROTECT:
219                         lex >> needprotect_;
220                         break;
221                 case IL_COPYSTYLE: {     // initialize with a known style
222                         docstring style;
223                         lex >> style;
224                         style = support::subst(style, '_', ' ');
225
226                         // We don't want to apply the algorithm in DocumentClass::insetLayout()
227                         // here. So we do it the long way.
228                         TextClass::InsetLayouts::const_iterator it = 
229                                         tclass.insetLayouts().find(style);
230                         if (it != tclass.insetLayouts().end()) {
231                                 docstring const tmpname = name_;
232                                 this->operator=(it->second);
233                                 name_ = tmpname;
234                         } else {
235                                 LYXERR0("Cannot copy unknown InsetLayout `"
236                                         << style << "'\n"
237                                         << "All InsetLayouts so far:");
238                                 TextClass::InsetLayouts::const_iterator lit = 
239                                                 tclass.insetLayouts().begin();
240                                 TextClass::InsetLayouts::const_iterator len = 
241                                                 tclass.insetLayouts().end();
242                                 for (; lit != len; ++lit)
243                                         lyxerr << lit->second.name() << "\n";
244                         }
245                         break;
246                 }
247
248                 case IL_FONT: {
249                         font_ = lyxRead(lex, inherit_font);
250                         // If you want to define labelfont, you need to do so after
251                         // font is defined.
252                         labelfont_ = font_;
253                         break;
254                 }
255                 case IL_BGCOLOR:
256                         lex >> tmp;
257                         bgcolor_ = lcolor.getFromLyXName(tmp);
258                         break;
259                 case IL_PREAMBLE:
260                         preamble_ = lex.getLongString("EndPreamble");
261                         break;
262                 case IL_REQUIRES: {
263                         lex.eatLine();
264                         vector<string> const req 
265                                 = support::getVectorFromString(lex.getString());
266                         requires_.insert(req.begin(), req.end());
267                         break;
268                 }
269                 case IL_END:
270                         getout = true;
271                         break;
272                 }
273         }
274
275         // Here add element to list if getout == true
276         if (!getout)
277                 return false;
278         
279         // The label font is generally used as-is without
280         // any realization against a given context.
281         labelfont_.realize(sane_font);
282
283         lex.popTable();
284         return true;
285 }
286
287
288 InsetLayout::InsetLyXType translateLyXType(std::string const & str) 
289 {
290         
291         if (support::compare_ascii_no_case(str, "charstyle") == 0)
292                 return InsetLayout::CHARSTYLE;
293         if (support::compare_ascii_no_case(str, "custom") == 0)
294                 return InsetLayout::CUSTOM;
295         if (support::compare_ascii_no_case(str, "element") == 0)
296                 return InsetLayout::ELEMENT;
297         if (support::compare_ascii_no_case(str, "end") == 0)
298                 return InsetLayout::END;
299         if (support::compare_ascii_no_case(str, "standard") == 0)
300                 return InsetLayout::STANDARD;
301         return InsetLayout::NOLYXTYPE;
302 }
303
304 } //namespace lyx