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