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