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