]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLayout.cpp
Remove all BufferParam arguments in InsetXXX methods (since insets know about their...
[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 "ColorSet.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), intoc_(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 } // namespace anon
71
72
73 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
74 {
75         enum {
76                 IL_BGCOLOR,
77                 IL_COPYSTYLE,
78                 IL_COUNTER,
79                 IL_CUSTOMPARS,
80                 IL_DECORATION,
81                 IL_FONT,
82                 IL_FORCELTR,
83                 IL_FORCEPLAIN,
84                 IL_FREESPACING,
85                 IL_HTMLTAG,
86                 IL_HTMLATTR,
87                 IL_HTMLINNERTAG,
88                 IL_HTMLINNERATTR,
89                 IL_HTMLLABEL,
90                 IL_HTMLSTYLE,
91                 IL_HTMLPREAMBLE,
92                 IL_INTOC,
93                 IL_LABELFONT,
94                 IL_LABELSTRING,
95                 IL_LATEXNAME,
96                 IL_LATEXPARAM,
97                 IL_LATEXTYPE,
98                 IL_LYXTYPE,
99                 IL_KEEPEMPTY,
100                 IL_MULTIPAR,
101                 IL_NEEDPROTECT,
102                 IL_PASSTHRU,
103                 IL_PREAMBLE,
104                 IL_REQUIRES,
105                 IL_END
106         };
107
108
109         LexerKeyword elementTags[] = {
110                 { "bgcolor", IL_BGCOLOR },
111                 { "copystyle", IL_COPYSTYLE }, 
112                 { "counter", IL_COUNTER},
113                 { "custompars", IL_CUSTOMPARS },
114                 { "decoration", IL_DECORATION },
115                 { "end", IL_END },
116                 { "font", IL_FONT },
117                 { "forceltr", IL_FORCELTR },
118                 { "forceplain", IL_FORCEPLAIN },
119                 { "freespacing", IL_FREESPACING },
120                 { "htmlattr", IL_HTMLATTR },
121                 { "htmlinnerattr", IL_HTMLINNERATTR},
122                 { "htmlinnertag", IL_HTMLINNERTAG},
123                 { "htmllabel", IL_HTMLLABEL },
124                 { "htmlpreamble", IL_HTMLPREAMBLE },
125                 { "htmlstyle", IL_HTMLSTYLE },
126                 { "htmltag", IL_HTMLTAG },
127                 { "intoc", IL_INTOC },
128                 { "keepempty", IL_KEEPEMPTY },
129                 { "labelfont", IL_LABELFONT },
130                 { "labelstring", IL_LABELSTRING },
131                 { "latexname", IL_LATEXNAME },
132                 { "latexparam", IL_LATEXPARAM },
133                 { "latextype", IL_LATEXTYPE },
134                 { "lyxtype", IL_LYXTYPE },
135                 { "multipar", IL_MULTIPAR },
136                 { "needprotect", IL_NEEDPROTECT },
137                 { "passthru", IL_PASSTHRU },
138                 { "preamble", IL_PREAMBLE },
139                 { "requires", IL_REQUIRES }
140         };
141
142         lex.pushTable(elementTags);
143
144         FontInfo font = inherit_font;
145         labelfont_ = inherit_font;
146         bgcolor_ = Color_none;
147         bool getout = false;
148         // whether we've read the CustomPars or ForcePlain tag
149         // for issuing a warning in case MultiPars comes later
150         bool readCustomOrPlain = false;
151
152         string tmp;     
153         while (!getout && lex.isOK()) {
154                 int le = lex.lex();
155                 switch (le) {
156                 case Lexer::LEX_UNDEF:
157                         lex.printError("Unknown InsetLayout tag");
158                         continue;
159                 default:
160                         break;
161                 }
162                 switch (le) {
163                 // FIXME
164                 // Perhaps a more elegant way to deal with the next two would be the
165                 // way this sort of thing is handled in Layout::read(), namely, by
166                 // using the Lexer.
167                 case IL_LYXTYPE: {
168                         string lt;
169                         lex >> lt;
170                         lyxtype_ = translateLyXType(lt);
171                         if (lyxtype_  == NOLYXTYPE)
172                                 LYXERR0("Unknown LyXType `" << lt << "'.");
173                         break;
174                 }
175                 case IL_LATEXTYPE:  {
176                         string lt;
177                         lex >> lt;
178                         latextype_ = translateLaTeXType(lt);
179                         if (latextype_  == ILT_ERROR)
180                                 LYXERR0("Unknown LaTeXType `" << lt << "'.");
181                         break;
182                 }
183                 case IL_LABELSTRING:
184                         lex >> labelstring_;
185                         break;
186                 case IL_DECORATION:
187                         lex >> tmp;
188                         decoration_ = translateDecoration(tmp);
189                         break;
190                 case IL_LATEXNAME:
191                         lex >> latexname_;
192                         break;
193                 case IL_LATEXPARAM:
194                         lex >> tmp;
195                         latexparam_ = support::subst(tmp, "&quot;", "\"");
196                         break;
197                 case IL_LABELFONT:
198                         labelfont_ = lyxRead(lex, inherit_font);
199                         break;
200                 case IL_FORCELTR:
201                         lex >> forceltr_;
202                         break;
203                 case IL_INTOC:
204                         lex >> intoc_;
205                         break;
206                 case IL_MULTIPAR:
207                         lex >> multipar_;
208                         // the defaults for these depend upon multipar_
209                         if (readCustomOrPlain)
210                                 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
211                                         "Previous value may be overwritten!");
212                         readCustomOrPlain = false;
213                         custompars_ = multipar_;
214                         forceplain_ = !multipar_;
215                         break;
216                 case IL_COUNTER:
217                         lex >> counter_;
218                         break;
219                 case IL_CUSTOMPARS:
220                         lex >> custompars_;
221                         readCustomOrPlain = true;
222                         break;
223                 case IL_FORCEPLAIN:
224                         lex >> forceplain_;
225                         break;
226                 case IL_PASSTHRU:
227                         lex >> passthru_;
228                         readCustomOrPlain = true;
229                         break;
230                 case IL_KEEPEMPTY:
231                         lex >> keepempty_;
232                         break;
233                 case IL_FREESPACING:
234                         lex >> freespacing_;
235                         break;
236                 case IL_NEEDPROTECT:
237                         lex >> needprotect_;
238                         break;
239                 case IL_COPYSTYLE: {     // initialize with a known style
240                         docstring style;
241                         lex >> style;
242                         style = support::subst(style, '_', ' ');
243
244                         // We don't want to apply the algorithm in DocumentClass::insetLayout()
245                         // here. So we do it the long way.
246                         TextClass::InsetLayouts::const_iterator it = 
247                                         tclass.insetLayouts().find(style);
248                         if (it != tclass.insetLayouts().end()) {
249                                 docstring const tmpname = name_;
250                                 this->operator=(it->second);
251                                 name_ = tmpname;
252                         } else {
253                                 LYXERR0("Cannot copy unknown InsetLayout `"
254                                         << style << "'\n"
255                                         << "All InsetLayouts so far:");
256                                 TextClass::InsetLayouts::const_iterator lit = 
257                                                 tclass.insetLayouts().begin();
258                                 TextClass::InsetLayouts::const_iterator len = 
259                                                 tclass.insetLayouts().end();
260                                 for (; lit != len; ++lit)
261                                         lyxerr << lit->second.name() << "\n";
262                         }
263                         break;
264                 }
265
266                 case IL_FONT: {
267                         font_ = lyxRead(lex, inherit_font);
268                         // If you want to define labelfont, you need to do so after
269                         // font is defined.
270                         labelfont_ = font_;
271                         break;
272                 }
273                 case IL_BGCOLOR:
274                         lex >> tmp;
275                         bgcolor_ = lcolor.getFromLyXName(tmp);
276                         break;
277                 case IL_PREAMBLE:
278                         preamble_ = from_utf8(lex.getLongString("EndPreamble"));
279                         break;
280                 case IL_HTMLTAG:
281                         lex >> htmltag_;
282                         break;
283                 case IL_HTMLATTR:
284                         lex >> htmlattr_;
285                         break;
286                 case IL_HTMLINNERTAG:
287                         lex >> htmlinnertag_;
288                         break;
289                 case IL_HTMLINNERATTR:
290                         lex >> htmlinnerattr_;
291                         break;
292                 case IL_HTMLLABEL:
293                         lex >> htmllabel_;
294                         break;
295                 case IL_HTMLSTYLE:
296                         htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
297                         break;
298                 case IL_HTMLPREAMBLE:
299                         htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
300                         break;
301                 case IL_REQUIRES: {
302                         lex.eatLine();
303                         vector<string> const req 
304                                 = support::getVectorFromString(lex.getString());
305                         requires_.insert(req.begin(), req.end());
306                         break;
307                 }
308                 case IL_END:
309                         getout = true;
310                         break;
311                 }
312         }
313
314         // Here add element to list if getout == true
315         if (!getout)
316                 return false;
317         
318         // The label font is generally used as-is without
319         // any realization against a given context.
320         labelfont_.realize(sane_font);
321
322         lex.popTable();
323         return true;
324 }
325
326
327 InsetLayout::InsetLyXType translateLyXType(std::string const & str) 
328 {
329         
330         if (support::compare_ascii_no_case(str, "charstyle") == 0)
331                 return InsetLayout::CHARSTYLE;
332         if (support::compare_ascii_no_case(str, "custom") == 0)
333                 return InsetLayout::CUSTOM;
334         if (support::compare_ascii_no_case(str, "element") == 0)
335                 return InsetLayout::ELEMENT;
336         if (support::compare_ascii_no_case(str, "end") == 0)
337                 return InsetLayout::END;
338         if (support::compare_ascii_no_case(str, "standard") == 0)
339                 return InsetLayout::STANDARD;
340         return InsetLayout::NOLYXTYPE;
341 }
342
343 } //namespace lyx