]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLayout.cpp
f71fd582a2c0711f219a9f7df11bb1dfb122186d
[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" || str == "Classic")
50                 return InsetLayout::Classic;
51         if (str == "minimalistic" || str == "Minimalistic")
52                 return InsetLayout::Minimalistic;
53         if (str == "conglomerate" || 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         // 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_background;
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                         lex >> lyxtype_;
141                         break;
142                 case IL_LATEXTYPE:
143                         lex >> latextype_;
144                         break;
145                 case IL_LABELSTRING:
146                         lex >> labelstring_;
147                         break;
148                 case IL_DECORATION:
149                         lex >> tmp;
150                         decoration_ = translateDecoration(tmp);
151                         break;
152                 case IL_LATEXNAME:
153                         lex >> latexname_;
154                         break;
155                 case IL_LATEXPARAM:
156                         lex >> tmp;
157                         latexparam_ = support::subst(tmp, "&quot;", "\"");
158                         break;
159                 case IL_LABELFONT:
160                         labelfont_ = lyxRead(lex, inherit_font);
161                         break;
162                 case IL_FORCELTR:
163                         lex >> forceltr_;
164                         break;
165                 case IL_MULTIPAR:
166                         lex >> multipar_;
167                         // the defaults for these depend upon multipar_
168                         if (readCustomOrPlain)
169                                 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
170                                         "Previous value may be overwritten!");
171                         readCustomOrPlain = false;
172                         custompars_ = multipar_;
173                         forceplain_ = !multipar_;
174                         break;
175                 case IL_CUSTOMPARS:
176                         lex >> custompars_;
177                         readCustomOrPlain = true;
178                         break;
179                 case IL_FORCEPLAIN:
180                         lex >> forceplain_;
181                         break;
182                 case IL_PASSTHRU:
183                         lex >> passthru_;
184                         readCustomOrPlain = true;
185                         break;
186                 case IL_KEEPEMPTY:
187                         lex >> keepempty_;
188                         break;
189                 case IL_FREESPACING:
190                         lex >> freespacing_;
191                         break;
192                 case IL_NEEDPROTECT:
193                         lex >> needprotect_;
194                         break;
195                 case IL_COPYSTYLE: {     // initialize with a known style
196                         docstring style;
197                         lex >> style;
198                         style = support::subst(style, '_', ' ');
199
200                         // We don't want to apply the algorithm in DocumentClass::insetLayout()
201                         // here. So we do it the long way.
202                         TextClass::InsetLayouts::const_iterator it = 
203                                         tclass.insetLayouts().find(style);
204                         if (it != tclass.insetLayouts().end()) {
205                                 docstring const tmpname = name_;
206                                 this->operator=(it->second);
207                                 name_ = tmpname;
208                         } else {
209                                 LYXERR0("Cannot copy unknown InsetLayout `"
210                                         << style << "'\n"
211                                         << "All InsetLayouts so far:");
212                                 TextClass::InsetLayouts::const_iterator lit = 
213                                                 tclass.insetLayouts().begin();
214                                 TextClass::InsetLayouts::const_iterator len = 
215                                                 tclass.insetLayouts().end();
216                                 for (; lit != len; ++lit)
217                                         lyxerr << lit->second.name() << "\n";
218                         }
219                         break;
220                 }
221
222                 case IL_FONT: {
223                         font_ = lyxRead(lex, inherit_font);
224                         // If you want to define labelfont, you need to do so after
225                         // font is defined.
226                         labelfont_ = font_;
227                         break;
228                 }
229                 case IL_BGCOLOR:
230                         lex >> tmp;
231                         bgcolor_ = lcolor.getFromLyXName(tmp);
232                         break;
233                 case IL_PREAMBLE:
234                         preamble_ = lex.getLongString("EndPreamble");
235                         break;
236                 case IL_REQUIRES: {
237                         lex.eatLine();
238                         vector<string> const req 
239                                 = support::getVectorFromString(lex.getString());
240                         requires_.insert(req.begin(), req.end());
241                         break;
242                 }
243                 case IL_END:
244                         getout = true;
245                         break;
246                 }
247         }
248
249         // Here add element to list if getout == true
250         if (!getout)
251                 return false;
252         
253         // The label font is generally used as-is without
254         // any realization against a given context.
255         labelfont_.realize(sane_font);
256
257         lex.popTable();
258         return true;
259 }
260
261 } //namespace lyx