]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLayout.cpp
41180c4a0cc356ad13baab91c8cbe4e70cdbba03
[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 "ColorSet.h"
18 #include "Lexer.h"
19 #include "TextClass.h"
20
21 #include "support/debug.h"
22 #include "support/lstrings.h"
23
24 #include <vector>
25
26 using std::string;
27 using std::set;
28 using std::vector;
29
30 namespace lyx {
31
32 InsetLayout::InsetLayout() :
33         name_(from_ascii("undefined")), lyxtype_(STANDARD),
34         labelstring_(from_ascii("UNDEFINED")), decoration_(DEFAULT),
35         latextype_(NOLATEXTYPE), font_(sane_font), 
36         labelfont_(sane_font), bgcolor_(Color_error), 
37         htmlforcecss_ (false), htmlisblock_(true),
38         multipar_(true), custompars_(true), forceplain_(false), 
39         passthru_(false), needprotect_(false), freespacing_(false), 
40         keepempty_(false), forceltr_(false), intoc_(false)
41
42         labelfont_.setColor(Color_error);
43 }
44
45
46 namespace {
47
48 InsetLayout::InsetDecoration translateDecoration(std::string const & str) 
49 {
50         if (support::compare_ascii_no_case(str, "classic") == 0)
51                 return InsetLayout::CLASSIC;
52         if (support::compare_ascii_no_case(str, "minimalistic") == 0)
53                 return InsetLayout::MINIMALISTIC;
54         if (support::compare_ascii_no_case(str, "conglomerate") == 0)
55                 return InsetLayout::CONGLOMERATE;
56         return InsetLayout::DEFAULT;
57 }
58
59 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
60 {
61         if (support::compare_ascii_no_case(str, "command") == 0)
62                 return InsetLayout::COMMAND;
63         if (support::compare_ascii_no_case(str, "environment") == 0)
64                 return InsetLayout::ENVIRONMENT;
65         if (support::compare_ascii_no_case(str, "none") == 0)
66                 return InsetLayout::NOLATEXTYPE;
67         return InsetLayout::ILT_ERROR;
68 }
69
70 } // namespace anon
71
72
73 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
74 {
75         enum {
76                 IL_BGCOLOR,
77                 IL_COPYSTYLE,
78                 IL_COUNTER,
79                 IL_CUSTOMPARS,
80                 IL_DECORATION,
81                 IL_FONT,
82                 IL_FORCELTR,
83                 IL_FORCEPLAIN,
84                 IL_FREESPACING,
85                 IL_HTMLTAG,
86                 IL_HTMLATTR,
87                 IL_HTMLFORCECSS,
88                 IL_HTMLINNERTAG,
89                 IL_HTMLINNERATTR,
90                 IL_HTMLISBLOCK,
91                 IL_HTMLLABEL,
92                 IL_HTMLSTYLE,
93                 IL_HTMLPREAMBLE,
94                 IL_INTOC,
95                 IL_LABELFONT,
96                 IL_LABELSTRING,
97                 IL_LATEXNAME,
98                 IL_LATEXPARAM,
99                 IL_LATEXTYPE,
100                 IL_LYXTYPE,
101                 IL_KEEPEMPTY,
102                 IL_MULTIPAR,
103                 IL_NEEDPROTECT,
104                 IL_PASSTHRU,
105                 IL_PREAMBLE,
106                 IL_REQUIRES,
107                 IL_END
108         };
109
110
111         LexerKeyword elementTags[] = {
112                 { "bgcolor", IL_BGCOLOR },
113                 { "copystyle", IL_COPYSTYLE }, 
114                 { "counter", IL_COUNTER},
115                 { "custompars", IL_CUSTOMPARS },
116                 { "decoration", IL_DECORATION },
117                 { "end", IL_END },
118                 { "font", IL_FONT },
119                 { "forceltr", IL_FORCELTR },
120                 { "forceplain", IL_FORCEPLAIN },
121                 { "freespacing", IL_FREESPACING },
122                 { "htmlattr", IL_HTMLATTR },
123                 { "htmlforcecss", IL_HTMLFORCECSS },
124                 { "htmlinnerattr", IL_HTMLINNERATTR},
125                 { "htmlinnertag", IL_HTMLINNERTAG},
126                 { "htmlisblock", IL_HTMLISBLOCK},
127                 { "htmllabel", IL_HTMLLABEL },
128                 { "htmlpreamble", IL_HTMLPREAMBLE },
129                 { "htmlstyle", IL_HTMLSTYLE },
130                 { "htmltag", IL_HTMLTAG },
131                 { "intoc", IL_INTOC },
132                 { "keepempty", IL_KEEPEMPTY },
133                 { "labelfont", IL_LABELFONT },
134                 { "labelstring", IL_LABELSTRING },
135                 { "latexname", IL_LATEXNAME },
136                 { "latexparam", IL_LATEXPARAM },
137                 { "latextype", IL_LATEXTYPE },
138                 { "lyxtype", IL_LYXTYPE },
139                 { "multipar", IL_MULTIPAR },
140                 { "needprotect", IL_NEEDPROTECT },
141                 { "passthru", IL_PASSTHRU },
142                 { "preamble", IL_PREAMBLE },
143                 { "requires", IL_REQUIRES }
144         };
145
146         lex.pushTable(elementTags);
147
148         FontInfo font = inherit_font;
149         labelfont_ = inherit_font;
150         bgcolor_ = Color_none;
151         bool getout = false;
152         // whether we've read the CustomPars or ForcePlain tag
153         // for issuing a warning in case MultiPars comes later
154         bool readCustomOrPlain = false;
155
156         string tmp;     
157         while (!getout && lex.isOK()) {
158                 int le = lex.lex();
159                 switch (le) {
160                 case Lexer::LEX_UNDEF:
161                         lex.printError("Unknown InsetLayout tag");
162                         continue;
163                 default:
164                         break;
165                 }
166                 switch (le) {
167                 // FIXME
168                 // Perhaps a more elegant way to deal with the next two would be the
169                 // way this sort of thing is handled in Layout::read(), namely, by
170                 // using the Lexer.
171                 case IL_LYXTYPE: {
172                         string lt;
173                         lex >> lt;
174                         lyxtype_ = translateLyXType(lt);
175                         if (lyxtype_  == NOLYXTYPE)
176                                 LYXERR0("Unknown LyXType `" << lt << "'.");
177                         break;
178                 }
179                 case IL_LATEXTYPE:  {
180                         string lt;
181                         lex >> lt;
182                         latextype_ = translateLaTeXType(lt);
183                         if (latextype_  == ILT_ERROR)
184                                 LYXERR0("Unknown LaTeXType `" << lt << "'.");
185                         break;
186                 }
187                 case IL_LABELSTRING:
188                         lex >> labelstring_;
189                         break;
190                 case IL_DECORATION:
191                         lex >> tmp;
192                         decoration_ = translateDecoration(tmp);
193                         break;
194                 case IL_LATEXNAME:
195                         lex >> latexname_;
196                         break;
197                 case IL_LATEXPARAM:
198                         lex >> tmp;
199                         latexparam_ = support::subst(tmp, "&quot;", "\"");
200                         break;
201                 case IL_LABELFONT:
202                         labelfont_ = lyxRead(lex, inherit_font);
203                         break;
204                 case IL_FORCELTR:
205                         lex >> forceltr_;
206                         break;
207                 case IL_INTOC:
208                         lex >> intoc_;
209                         break;
210                 case IL_MULTIPAR:
211                         lex >> multipar_;
212                         // the defaults for these depend upon multipar_
213                         if (readCustomOrPlain)
214                                 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
215                                         "Previous value may be overwritten!");
216                         readCustomOrPlain = false;
217                         custompars_ = multipar_;
218                         forceplain_ = !multipar_;
219                         break;
220                 case IL_COUNTER:
221                         lex >> counter_;
222                         break;
223                 case IL_CUSTOMPARS:
224                         lex >> custompars_;
225                         readCustomOrPlain = true;
226                         break;
227                 case IL_FORCEPLAIN:
228                         lex >> forceplain_;
229                         break;
230                 case IL_PASSTHRU:
231                         lex >> passthru_;
232                         readCustomOrPlain = true;
233                         break;
234                 case IL_KEEPEMPTY:
235                         lex >> keepempty_;
236                         break;
237                 case IL_FREESPACING:
238                         lex >> freespacing_;
239                         break;
240                 case IL_NEEDPROTECT:
241                         lex >> needprotect_;
242                         break;
243                 case IL_COPYSTYLE: {     // initialize with a known style
244                         docstring style;
245                         lex >> style;
246                         style = support::subst(style, '_', ' ');
247
248                         // We don't want to apply the algorithm in DocumentClass::insetLayout()
249                         // here. So we do it the long way.
250                         TextClass::InsetLayouts::const_iterator it = 
251                                         tclass.insetLayouts().find(style);
252                         if (it != tclass.insetLayouts().end()) {
253                                 docstring const tmpname = name_;
254                                 this->operator=(it->second);
255                                 name_ = tmpname;
256                         } else {
257                                 LYXERR0("Cannot copy unknown InsetLayout `"
258                                         << style << "'\n"
259                                         << "All InsetLayouts so far:");
260                                 TextClass::InsetLayouts::const_iterator lit = 
261                                                 tclass.insetLayouts().begin();
262                                 TextClass::InsetLayouts::const_iterator len = 
263                                                 tclass.insetLayouts().end();
264                                 for (; lit != len; ++lit)
265                                         lyxerr << lit->second.name() << "\n";
266                         }
267                         break;
268                 }
269
270                 case IL_FONT: {
271                         font_ = lyxRead(lex, inherit_font);
272                         // If you want to define labelfont, you need to do so after
273                         // font is defined.
274                         labelfont_ = font_;
275                         break;
276                 }
277                 case IL_BGCOLOR:
278                         lex >> tmp;
279                         bgcolor_ = lcolor.getFromLyXName(tmp);
280                         break;
281                 case IL_PREAMBLE:
282                         preamble_ = from_utf8(lex.getLongString("EndPreamble"));
283                         break;
284                 case IL_HTMLTAG:
285                         lex >> htmltag_;
286                         break;
287                 case IL_HTMLATTR:
288                         lex >> htmlattr_;
289                         break;
290                 case IL_HTMLFORCECSS:
291                         lex >> htmlforcecss_;
292                         break;
293                 case IL_HTMLINNERTAG:
294                         lex >> htmlinnertag_;
295                         break;
296                 case IL_HTMLINNERATTR:
297                         lex >> htmlinnerattr_;
298                         break;
299                 case IL_HTMLLABEL:
300                         lex >> htmllabel_;
301                         break;
302                 case IL_HTMLISBLOCK:
303                         lex >> htmlisblock_;
304                         break;
305                 case IL_HTMLSTYLE:
306                         htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
307                         break;
308                 case IL_HTMLPREAMBLE:
309                         htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
310                         break;
311                 case IL_REQUIRES: {
312                         lex.eatLine();
313                         vector<string> const req 
314                                 = support::getVectorFromString(lex.getString());
315                         requires_.insert(req.begin(), req.end());
316                         break;
317                 }
318                 case IL_END:
319                         getout = true;
320                         break;
321                 }
322         }
323
324         // Here add element to list if getout == true
325         if (!getout)
326                 return false;
327         
328         // The label font is generally used as-is without
329         // any realization against a given context.
330         labelfont_.realize(sane_font);
331
332         lex.popTable();
333         return true;
334 }
335
336
337 InsetLayout::InsetLyXType translateLyXType(std::string const & str) 
338 {
339         
340         if (support::compare_ascii_no_case(str, "charstyle") == 0)
341                 return InsetLayout::CHARSTYLE;
342         if (support::compare_ascii_no_case(str, "custom") == 0)
343                 return InsetLayout::CUSTOM;
344         if (support::compare_ascii_no_case(str, "element") == 0)
345                 return InsetLayout::ELEMENT;
346         if (support::compare_ascii_no_case(str, "end") == 0)
347                 return InsetLayout::END;
348         if (support::compare_ascii_no_case(str, "standard") == 0)
349                 return InsetLayout::STANDARD;
350         return InsetLayout::NOLYXTYPE;
351 }
352
353
354 string const & InsetLayout::htmltag() const
355 {
356         if (htmltag_.empty())
357                 htmltag_ = "span";
358         return htmltag_; 
359 }
360
361
362 string const & InsetLayout::htmlattr() const
363 {
364         if (htmlattr_.empty())
365                 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
366         return htmlattr_; 
367 }
368
369
370 string const & InsetLayout::htmlinnerattr() const
371 {
372         if (htmlinnerattr_.empty())
373                 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
374         return htmlinnerattr_; 
375 }
376
377
378 string InsetLayout::defaultCSSClass() const
379
380         if (!defaultcssclass_.empty())
381                 return defaultcssclass_;
382         string d;
383         string n = to_utf8(name());
384         string::const_iterator it = n.begin();
385         string::const_iterator en = n.end();
386         for (; it != en; ++it) {
387                 if (!isalpha(*it))
388                         d += "_";
389                 else if (islower(*it))
390                         d += *it;
391                 else
392                         d += support::lowercase(*it);
393         }
394         // are there other characters we need to remove?
395         defaultcssclass_ = d;
396         return defaultcssclass_;
397 }
398
399
400 void InsetLayout::makeDefaultCSS() const
401 {
402         if (!htmldefaultstyle_.empty()) 
403                 return;
404         docstring const mainfontCSS = font_.asCSS();
405         if (!mainfontCSS.empty())
406                 htmldefaultstyle_ = 
407                                 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
408                                 mainfontCSS + from_ascii("\n}\n");
409 }
410
411
412 docstring InsetLayout::htmlstyle() const 
413
414         if (!htmlstyle_.empty() && !htmlforcecss_)
415                 return htmlstyle_;
416         if (htmldefaultstyle_.empty())
417                 makeDefaultCSS();
418         docstring retval = htmldefaultstyle_;
419         if (!htmlstyle_.empty())
420                 retval += '\n' + htmlstyle_ + '\n';
421         return retval;
422 }
423
424
425 } //namespace lyx