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