]> git.lyx.org Git - features.git/blob - src/insets/InsetLayout.cpp
simplify Lexer handling a bit more
[features.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 "support/lstrings.h"
21
22 #include <vector>
23
24 using std::string;
25 using std::set;
26 using std::vector;
27
28 namespace lyx {
29
30 InsetLayout::InsetLayout() :
31         name_(from_ascii("undefined")), labelstring_(from_ascii("UNDEFINED")),
32         decoration_(InsetLayout::Default),
33         font_(sane_font), labelfont_(sane_font), bgcolor_(Color_error), 
34         multipar_(false), passthru_(false), needprotect_(false),
35         freespacing_(false), keepempty_(false), forceltr_(false)
36
37         labelfont_.setColor(Color_error); 
38 }
39
40
41 namespace {
42
43 InsetLayout::InsetDecoration translateDecoration(std::string const & str) 
44 {
45         if (str == "classic")
46                 return InsetLayout::Classic;
47         if (str == "minimalistic")
48                 return InsetLayout::Minimalistic;
49         if (str == "conglomerate")
50                 return InsetLayout::Conglomerate;
51         return InsetLayout::Default;
52 }
53
54 }
55
56
57 bool InsetLayout::read(Lexer & lexrc)
58 {
59         name_ = support::subst(lexrc.getDocString(), '_', ' ');
60
61         enum {
62                 IL_FONT,
63                 IL_BGCOLOR,
64                 IL_DECORATION,
65                 IL_FREESPACING,
66                 IL_FORCELTR,
67                 IL_LABELFONT,
68                 IL_LABELSTRING,
69                 IL_LATEXNAME,
70                 IL_LATEXPARAM,
71                 IL_LATEXTYPE,
72                 IL_LYXTYPE,
73                 IL_KEEPEMPTY,
74                 IL_MULTIPAR,
75                 IL_NEEDPROTECT,
76                 IL_PASSTHRU,
77                 IL_PREAMBLE,
78                 IL_REQUIRES,
79                 IL_END
80         };
81
82
83         LexerKeyword elementTags[] = {
84                 { "bgcolor", IL_BGCOLOR },
85                 { "decoration", IL_DECORATION },
86                 { "end", IL_END },
87                 { "font", IL_FONT },
88                 { "forceltr", IL_FORCELTR },
89                 { "freespacing", IL_FREESPACING },
90                 { "keepempty", IL_KEEPEMPTY },
91                 { "labelfont", IL_LABELFONT },
92                 { "labelstring", IL_LABELSTRING },
93                 { "latexname", IL_LATEXNAME },
94                 { "latexparam", IL_LATEXPARAM },
95                 { "latextype", IL_LATEXTYPE },
96                 { "lyxtype", IL_LYXTYPE },
97                 { "multipar", IL_MULTIPAR },
98                 { "needprotect", IL_NEEDPROTECT },
99                 { "passthru", IL_PASSTHRU },
100                 { "preamble", IL_PREAMBLE },
101                 { "requires", IL_REQUIRES }
102         };
103
104         lexrc.pushTable(elementTags);
105
106         FontInfo font = inherit_font;
107         labelfont_ = inherit_font;
108         bgcolor_ = Color_background;
109         bool getout = false;
110         
111         while (!getout && lexrc.isOK()) {
112                 int le = lexrc.lex();
113                 switch (le) {
114                 case Lexer::LEX_UNDEF:
115                         lexrc.printError("Unknown InsetLayout tag `$$Token'");
116                         continue;
117                 default: break;
118                 }
119                 switch (le) {
120                 case IL_LYXTYPE:
121                         lexrc.next();
122                         lyxtype_ = lexrc.getString();
123                         break;
124                 case IL_LATEXTYPE:
125                         lexrc.next();
126                         latextype_ = lexrc.getString();
127                         break;
128                 case IL_LABELSTRING:
129                         lexrc.next();
130                         labelstring_ = lexrc.getDocString();
131                         break;
132                 case IL_DECORATION:
133                         lexrc.next();
134                         decoration_ = translateDecoration(lexrc.getString());
135                         break;
136                 case IL_LATEXNAME:
137                         lexrc.next();
138                         latexname_ = lexrc.getString();
139                         break;
140                 case IL_LATEXPARAM:
141                         lexrc.next();
142                         latexparam_ = support::subst(lexrc.getString(), "&quot;", "\"");
143                         break;
144                 case IL_LABELFONT:
145                         labelfont_ = lyxRead(lexrc, inherit_font);
146                         break;
147                 case IL_FORCELTR:
148                         lexrc.next();
149                         forceltr_ = lexrc.getBool();
150                         break;
151                 case IL_MULTIPAR:
152                         lexrc.next();
153                         multipar_ = lexrc.getBool();
154                         break;
155                 case IL_PASSTHRU:
156                         lexrc.next();
157                         passthru_ = lexrc.getBool();
158                         break;
159                 case IL_KEEPEMPTY:
160                         lexrc.next();
161                         keepempty_ = lexrc.getBool();
162                         break;
163                 case IL_FREESPACING:
164                         lexrc.next();
165                         freespacing_ = lexrc.getBool();
166                         break;
167                 case IL_NEEDPROTECT:
168                         lexrc.next();
169                         needprotect_ = lexrc.getBool();
170                         break;
171                 case IL_FONT: {
172                         font_ = lyxRead(lexrc, inherit_font);
173                         // If you want to define labelfont, you need to do so after
174                         // font is defined.
175                         labelfont_ = font_;
176                         break;
177                 }
178                 case IL_BGCOLOR: {
179                         lexrc.next();
180                         string const token = lexrc.getString();
181                         bgcolor_ = lcolor.getFromLyXName(token);
182                         break;
183                 }
184                 case IL_PREAMBLE:
185                         preamble_ = lexrc.getLongString("EndPreamble");
186                         break;
187                 case IL_REQUIRES: {
188                         lexrc.eatLine();
189                         vector<string> const req 
190                                 = support::getVectorFromString(lexrc.getString());
191                         requires_.insert(req.begin(), req.end());
192                         break;
193                 }
194                 case IL_END:
195                         getout = true;
196                         break;
197                 }
198         }
199
200         // Here add element to list if getout == true
201         if (!getout)
202                 return false;
203         
204         // The label font is generally used as-is without
205         // any realization against a given context.
206         labelfont_.realize(sane_font);
207
208         lexrc.popTable();
209         return true;
210 }
211
212 } //namespace lyx