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