]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLayout.cpp
40873cb49e2680d2a689553668e46f7771e00a20
[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 "Color.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_(InsetLayout::DEFAULT),
36         font_(sane_font), labelfont_(sane_font), bgcolor_(Color_error), 
37         multipar_(false), custompars_(false), forceplain_(true), 
38         passthru_(false), needprotect_(false), freespacing_(false), 
39         keepempty_(false), forceltr_(false)
40
41         labelfont_.setColor(Color_error);
42 }
43
44
45 namespace {
46
47 InsetLayout::InsetDecoration translateDecoration(std::string const & str) 
48 {
49         if (support::compare_ascii_no_case(str, "classic") == 0)
50                 return InsetLayout::CLASSIC;
51         if (support::compare_ascii_no_case(str, "minimalistic") == 0)
52                 return InsetLayout::MINIMALISTIC;
53         if (support::compare_ascii_no_case(str, "conglomerate") == 0)
54                 return InsetLayout::CONGLOMERATE;
55         return InsetLayout::DEFAULT;
56 }
57
58 }
59
60
61 bool InsetLayout::read(Lexer & lex, TextClass & tclass)
62 {
63         name_ = support::subst(lex.getDocString(), '_', ' ');
64         // FIXME We need to check for name_.empty() here, and
65         // take the same sort of action as in TextClass::read()
66         // if it is empty. Or, better, we could read name_ there,
67         // take action there, etc.
68
69         enum {
70                 IL_BGCOLOR,
71                 IL_COPYSTYLE,
72                 IL_CUSTOMPARS,
73                 IL_DECORATION,
74                 IL_FONT,
75                 IL_FORCELTR,
76                 IL_FORCEPLAIN,
77                 IL_FREESPACING,
78                 IL_LABELFONT,
79                 IL_LABELSTRING,
80                 IL_LATEXNAME,
81                 IL_LATEXPARAM,
82                 IL_LATEXTYPE,
83                 IL_LYXTYPE,
84                 IL_KEEPEMPTY,
85                 IL_MULTIPAR,
86                 IL_NEEDPROTECT,
87                 IL_PASSTHRU,
88                 IL_PREAMBLE,
89                 IL_REQUIRES,
90                 IL_END
91         };
92
93
94         LexerKeyword elementTags[] = {
95                 { "bgcolor", IL_BGCOLOR },
96                 { "copystyle", IL_COPYSTYLE }, 
97                 { "custompars", IL_CUSTOMPARS },
98                 { "decoration", IL_DECORATION },
99                 { "end", IL_END },
100                 { "font", IL_FONT },
101                 { "forceltr", IL_FORCELTR },
102                 { "forceplain", IL_FORCEPLAIN },
103                 { "freespacing", IL_FREESPACING },
104                 { "keepempty", IL_KEEPEMPTY },
105                 { "labelfont", IL_LABELFONT },
106                 { "labelstring", IL_LABELSTRING },
107                 { "latexname", IL_LATEXNAME },
108                 { "latexparam", IL_LATEXPARAM },
109                 { "latextype", IL_LATEXTYPE },
110                 { "lyxtype", IL_LYXTYPE },
111                 { "multipar", IL_MULTIPAR },
112                 { "needprotect", IL_NEEDPROTECT },
113                 { "passthru", IL_PASSTHRU },
114                 { "preamble", IL_PREAMBLE },
115                 { "requires", IL_REQUIRES }
116         };
117
118         lex.pushTable(elementTags);
119
120         FontInfo font = inherit_font;
121         labelfont_ = inherit_font;
122         bgcolor_ = Color_none;
123         bool getout = false;
124         // whether we've read the CustomPars or ForcePlain tag
125         // for issuing a warning in case MultiPars comes later
126         bool readCustomOrPlain = false;
127
128         string tmp;     
129         while (!getout && lex.isOK()) {
130                 int le = lex.lex();
131                 switch (le) {
132                 case Lexer::LEX_UNDEF:
133                         lex.printError("Unknown InsetLayout tag");
134                         continue;
135                 default:
136                         break;
137                 }
138                 switch (le) {
139                 case IL_LYXTYPE: {
140                         string lt;
141                         lex >> lt;
142                         lyxtype_ = translateLyXType(lt);
143                         if (lyxtype_  == NOLYXTYPE)
144                                 LYXERR0("Unknown LyXType `" << lt << "'.");
145                         break;
146                 }
147                 case IL_LATEXTYPE:
148                         lex >> latextype_;
149                         break;
150                 case IL_LABELSTRING:
151                         lex >> labelstring_;
152                         break;
153                 case IL_DECORATION:
154                         lex >> tmp;
155                         decoration_ = translateDecoration(tmp);
156                         break;
157                 case IL_LATEXNAME:
158                         lex >> latexname_;
159                         break;
160                 case IL_LATEXPARAM:
161                         lex >> tmp;
162                         latexparam_ = support::subst(tmp, "&quot;", "\"");
163                         break;
164                 case IL_LABELFONT:
165                         labelfont_ = lyxRead(lex, inherit_font);
166                         break;
167                 case IL_FORCELTR:
168                         lex >> forceltr_;
169                         break;
170                 case IL_MULTIPAR:
171                         lex >> multipar_;
172                         // the defaults for these depend upon multipar_
173                         if (readCustomOrPlain)
174                                 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
175                                         "Previous value may be overwritten!");
176                         readCustomOrPlain = false;
177                         custompars_ = multipar_;
178                         forceplain_ = !multipar_;
179                         break;
180                 case IL_CUSTOMPARS:
181                         lex >> custompars_;
182                         readCustomOrPlain = true;
183                         break;
184                 case IL_FORCEPLAIN:
185                         lex >> forceplain_;
186                         break;
187                 case IL_PASSTHRU:
188                         lex >> passthru_;
189                         readCustomOrPlain = true;
190                         break;
191                 case IL_KEEPEMPTY:
192                         lex >> keepempty_;
193                         break;
194                 case IL_FREESPACING:
195                         lex >> freespacing_;
196                         break;
197                 case IL_NEEDPROTECT:
198                         lex >> needprotect_;
199                         break;
200                 case IL_COPYSTYLE: {     // initialize with a known style
201                         docstring style;
202                         lex >> style;
203                         style = support::subst(style, '_', ' ');
204
205                         // We don't want to apply the algorithm in DocumentClass::insetLayout()
206                         // here. So we do it the long way.
207                         TextClass::InsetLayouts::const_iterator it = 
208                                         tclass.insetLayouts().find(style);
209                         if (it != tclass.insetLayouts().end()) {
210                                 docstring const tmpname = name_;
211                                 this->operator=(it->second);
212                                 name_ = tmpname;
213                         } else {
214                                 LYXERR0("Cannot copy unknown InsetLayout `"
215                                         << style << "'\n"
216                                         << "All InsetLayouts so far:");
217                                 TextClass::InsetLayouts::const_iterator lit = 
218                                                 tclass.insetLayouts().begin();
219                                 TextClass::InsetLayouts::const_iterator len = 
220                                                 tclass.insetLayouts().end();
221                                 for (; lit != len; ++lit)
222                                         lyxerr << lit->second.name() << "\n";
223                         }
224                         break;
225                 }
226
227                 case IL_FONT: {
228                         font_ = lyxRead(lex, inherit_font);
229                         // If you want to define labelfont, you need to do so after
230                         // font is defined.
231                         labelfont_ = font_;
232                         break;
233                 }
234                 case IL_BGCOLOR:
235                         lex >> tmp;
236                         bgcolor_ = lcolor.getFromLyXName(tmp);
237                         break;
238                 case IL_PREAMBLE:
239                         preamble_ = lex.getLongString("EndPreamble");
240                         break;
241                 case IL_REQUIRES: {
242                         lex.eatLine();
243                         vector<string> const req 
244                                 = support::getVectorFromString(lex.getString());
245                         requires_.insert(req.begin(), req.end());
246                         break;
247                 }
248                 case IL_END:
249                         getout = true;
250                         break;
251                 }
252         }
253
254         // Here add element to list if getout == true
255         if (!getout)
256                 return false;
257         
258         // The label font is generally used as-is without
259         // any realization against a given context.
260         labelfont_.realize(sane_font);
261
262         lex.popTable();
263         return true;
264 }
265
266
267 InsetLayout::InsetLyXType translateLyXType(std::string const & str) 
268 {
269         
270         if (support::compare_ascii_no_case(str, "charstyle") == 0)
271                 return InsetLayout::CHARSTYLE;
272         if (support::compare_ascii_no_case(str, "custom") == 0)
273                 return InsetLayout::CUSTOM;
274         if (support::compare_ascii_no_case(str, "element") == 0)
275                 return InsetLayout::ELEMENT;
276         if (support::compare_ascii_no_case(str, "end") == 0)
277                 return InsetLayout::END;
278         if (support::compare_ascii_no_case(str, "standard") == 0)
279                 return InsetLayout::STANDARD;
280         return InsetLayout::NOLYXTYPE;
281 }
282
283 } //namespace lyx