]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLayout.cpp
59eaeecb9feba56a9f0142e25bc440b697a9d666
[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         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 & lex)
58 {
59         name_ = support::subst(lex.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         lex.pushTable(elementTags);
105
106         FontInfo font = inherit_font;
107         labelfont_ = inherit_font;
108         bgcolor_ = Color_background;
109         bool getout = false;
110
111         string tmp;     
112         while (!getout && lex.isOK()) {
113                 int le = lex.lex();
114                 switch (le) {
115                 case Lexer::LEX_UNDEF:
116                         lex.printError("Unknown InsetLayout tag `$$Token'");
117                         continue;
118                 default: break;
119                 }
120                 switch (le) {
121                 case IL_LYXTYPE:
122                         lex >> lyxtype_;
123                         break;
124                 case IL_LATEXTYPE:
125                         lex >> latextype_;
126                         break;
127                 case IL_LABELSTRING:
128                         lex >> labelstring_;
129                         break;
130                 case IL_DECORATION:
131                         lex >> tmp;
132                         decoration_ = translateDecoration(tmp);
133                         break;
134                 case IL_LATEXNAME:
135                         lex >> latexname_;
136                         break;
137                 case IL_LATEXPARAM:
138                         lex >> tmp;
139                         latexparam_ = support::subst(tmp, "&quot;", "\"");
140                         break;
141                 case IL_LABELFONT:
142                         labelfont_ = lyxRead(lex, inherit_font);
143                         break;
144                 case IL_FORCELTR:
145                         lex >> forceltr_;
146                         break;
147                 case IL_MULTIPAR:
148                         lex >> multipar_;
149                         break;
150                 case IL_PASSTHRU:
151                         lex >> passthru_;
152                         break;
153                 case IL_KEEPEMPTY:
154                         lex >> keepempty_;
155                         break;
156                 case IL_FREESPACING:
157                         lex >> freespacing_;
158                         break;
159                 case IL_NEEDPROTECT:
160                         lex >> needprotect_;
161                         break;
162                 case IL_FONT: {
163                         font_ = lyxRead(lex, inherit_font);
164                         // If you want to define labelfont, you need to do so after
165                         // font is defined.
166                         labelfont_ = font_;
167                         break;
168                 }
169                 case IL_BGCOLOR:
170                         lex >> tmp;
171                         bgcolor_ = lcolor.getFromLyXName(tmp);
172                         break;
173                 case IL_PREAMBLE:
174                         preamble_ = lex.getLongString("EndPreamble");
175                         break;
176                 case IL_REQUIRES: {
177                         lex.eatLine();
178                         vector<string> const req 
179                                 = support::getVectorFromString(lex.getString());
180                         requires_.insert(req.begin(), req.end());
181                         break;
182                 }
183                 case IL_END:
184                         getout = true;
185                         break;
186                 }
187         }
188
189         // Here add element to list if getout == true
190         if (!getout)
191                 return false;
192         
193         // The label font is generally used as-is without
194         // any realization against a given context.
195         labelfont_.realize(sane_font);
196
197         lex.popTable();
198         return true;
199 }
200
201 } //namespace lyx