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