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