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