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