]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLayout.cpp
Remove unused Counters::copy
[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 #include "support/textutils.h"
24
25 #include <vector>
26
27 using std::string;
28 using std::set;
29 using std::vector;
30
31 using namespace lyx::support;
32
33 namespace lyx {
34
35 InsetLayout::InsetDecoration translateDecoration(std::string const & str)
36 {
37         if (compare_ascii_no_case(str, "classic") == 0)
38                 return InsetLayout::CLASSIC;
39         if (compare_ascii_no_case(str, "minimalistic") == 0)
40                 return InsetLayout::MINIMALISTIC;
41         if (compare_ascii_no_case(str, "conglomerate") == 0)
42                 return InsetLayout::CONGLOMERATE;
43         return InsetLayout::DEFAULT;
44 }
45
46 namespace {
47
48 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
49 {
50         if (compare_ascii_no_case(str, "command") == 0)
51                 return InsetLayout::COMMAND;
52         if (compare_ascii_no_case(str, "environment") == 0)
53                 return InsetLayout::ENVIRONMENT;
54         if (compare_ascii_no_case(str, "none") == 0)
55                 return InsetLayout::NOLATEXTYPE;
56         return InsetLayout::ILT_ERROR;
57 }
58
59 } // namespace
60
61
62 bool InsetLayout::read(Lexer & lex, TextClass const & tclass,
63         bool validating)
64 {
65         enum {
66                 IL_ADDTOTOC,
67                 IL_ARGUMENT,
68                 IL_BABELPREAMBLE,
69                 IL_BGCOLOR,
70                 IL_CONTENTASLABEL,
71                 IL_COPYSTYLE,
72                 IL_COUNTER,
73                 IL_CUSTOMPARS,
74                 IL_DECORATION,
75                 IL_DISPLAY,
76                 IL_EDITEXTERNAL,
77                 IL_FIXEDWIDTH_PREAMBLE_ENCODING,
78                 IL_FONT,
79                 IL_FORCE_LOCAL_FONT_SWITCH,
80                 IL_FORCELTR,
81                 IL_FORCEOWNLINES,
82                 IL_FORCEPLAIN,
83                 IL_FREESPACING,
84                 IL_HTMLTAG,
85                 IL_HTMLATTR,
86                 IL_HTMLFORCECSS,
87                 IL_HTMLINNERTAG,
88                 IL_HTMLINNERATTR,
89                 IL_HTMLISBLOCK,
90                 IL_HTMLLABEL,
91                 IL_HTMLSTYLE,
92                 IL_HTMLPREAMBLE,
93                 IL_DOCBOOKTAG,
94                 IL_DOCBOOKATTR,
95                 IL_DOCBOOKTAGTYPE,
96                 IL_DOCBOOKSECTION,
97                 IL_DOCBOOKININFO,
98                 IL_DOCBOOKWRAPPERTAG,
99                 IL_DOCBOOKWRAPPERTAGTYPE,
100                 IL_DOCBOOKWRAPPERATTR,
101                 IL_DOCBOOKITEMTAG,
102                 IL_DOCBOOKITEMTAGTYPE,
103                 IL_DOCBOOKITEMATTR,
104                 IL_DOCBOOKITEMWRAPPERTAG,
105                 IL_DOCBOOKITEMWRAPPERTAGTYPE,
106                 IL_DOCBOOKITEMWRAPPERATTR,
107                 IL_INTOC,
108                 IL_ISTOCCAPTION,
109                 IL_LABELFONT,
110                 IL_LABELSTRING,
111                 IL_LANGPREAMBLE,
112                 IL_LATEXNAME,
113                 IL_LATEXPARAM,
114                 IL_LATEXTYPE,
115                 IL_LEFTDELIM,
116                 IL_LYXTYPE,
117                 IL_OBSOLETEDBY,
118                 IL_KEEPEMPTY,
119                 IL_MENUSTRING,
120                 IL_MULTIPAR,
121                 IL_NEEDCPROTECT,
122                 IL_NEEDMBOXPROTECT,
123                 IL_NEEDPROTECT,
124                 IL_NEWLINE_CMD,
125                 IL_PASSTHRU,
126                 IL_PASSTHRU_CHARS,
127                 IL_PARBREAKIGNORED,
128                 IL_PARBREAKISNEWLINE,
129                 IL_PREAMBLE,
130                 IL_REQUIRES,
131                 IL_RIGHTDELIM,
132                 IL_REFPREFIX,
133                 IL_RESETARGS,
134                 IL_RESETSFONT,
135                 IL_SPELLCHECK,
136                 IL_END
137         };
138
139
140         LexerKeyword elementTags[] = {
141                 { "addtotoc", IL_ADDTOTOC },
142                 { "argument", IL_ARGUMENT },
143                 { "babelpreamble", IL_BABELPREAMBLE },
144                 { "bgcolor", IL_BGCOLOR },
145                 { "contentaslabel", IL_CONTENTASLABEL },
146                 { "copystyle", IL_COPYSTYLE },
147                 { "counter", IL_COUNTER},
148                 { "custompars", IL_CUSTOMPARS },
149                 { "decoration", IL_DECORATION },
150                 { "display", IL_DISPLAY },
151                 { "docbookattr", IL_DOCBOOKATTR },
152                 { "docbookininfo", IL_DOCBOOKININFO },
153                 { "docbookitemattr", IL_DOCBOOKITEMATTR },
154                 { "docbookitemtag", IL_DOCBOOKITEMTAG },
155                 { "docbookitemtagtype", IL_DOCBOOKITEMTAGTYPE },
156                 { "docbookitemwrapperattr", IL_DOCBOOKITEMWRAPPERATTR },
157                 { "docbookitemwrappertag", IL_DOCBOOKITEMWRAPPERTAG },
158                 { "docbookitemwrappertagtype", IL_DOCBOOKITEMWRAPPERTAGTYPE },
159                 { "docbooksection", IL_DOCBOOKSECTION },
160                 { "docbooktag", IL_DOCBOOKTAG },
161                 { "docbooktagtype", IL_DOCBOOKTAGTYPE },
162                 { "docbookwrapperattr", IL_DOCBOOKWRAPPERATTR },
163                 { "docbookwrappertag", IL_DOCBOOKWRAPPERTAG },
164                 { "docbookwrappertagtype", IL_DOCBOOKWRAPPERTAGTYPE },
165                 { "editexternal", IL_EDITEXTERNAL },
166                 { "end", IL_END },
167                 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
168                 { "font", IL_FONT },
169                 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
170                 { "forceltr", IL_FORCELTR },
171                 { "forceownlines", IL_FORCEOWNLINES },
172                 { "forceplain", IL_FORCEPLAIN },
173                 { "freespacing", IL_FREESPACING },
174                 { "htmlattr", IL_HTMLATTR },
175                 { "htmlforcecss", IL_HTMLFORCECSS },
176                 { "htmlinnerattr", IL_HTMLINNERATTR},
177                 { "htmlinnertag", IL_HTMLINNERTAG},
178                 { "htmlisblock", IL_HTMLISBLOCK},
179                 { "htmllabel", IL_HTMLLABEL },
180                 { "htmlpreamble", IL_HTMLPREAMBLE },
181                 { "htmlstyle", IL_HTMLSTYLE },
182                 { "htmltag", IL_HTMLTAG },
183                 { "intoc", IL_INTOC },
184                 { "istoccaption", IL_ISTOCCAPTION },
185                 { "keepempty", IL_KEEPEMPTY },
186                 { "labelfont", IL_LABELFONT },
187                 { "labelstring", IL_LABELSTRING },
188                 { "langpreamble", IL_LANGPREAMBLE },
189                 { "latexname", IL_LATEXNAME },
190                 { "latexparam", IL_LATEXPARAM },
191                 { "latextype", IL_LATEXTYPE },
192                 { "leftdelim", IL_LEFTDELIM },
193                 { "lyxtype", IL_LYXTYPE },
194                 { "menustring", IL_MENUSTRING },
195                 { "multipar", IL_MULTIPAR },
196                 { "needcprotect", IL_NEEDCPROTECT },
197                 { "needmboxprotect", IL_NEEDMBOXPROTECT },
198                 { "needprotect", IL_NEEDPROTECT },
199                 { "newlinecmd", IL_NEWLINE_CMD },
200                 { "obsoletedby", IL_OBSOLETEDBY },
201                 { "parbreakignored", IL_PARBREAKIGNORED },
202                 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
203                 { "passthru", IL_PASSTHRU },
204                 { "passthruchars", IL_PASSTHRU_CHARS },
205                 { "preamble", IL_PREAMBLE },
206                 { "refprefix", IL_REFPREFIX },
207                 { "requires", IL_REQUIRES },
208                 { "resetargs", IL_RESETARGS },
209                 { "resetsfont", IL_RESETSFONT },
210                 { "rightdelim", IL_RIGHTDELIM },
211                 { "spellcheck", IL_SPELLCHECK }
212         };
213
214         lex.pushTable(elementTags);
215
216         labelfont_ = inherit_font;
217         bgcolor_ = Color_none;
218         bool getout = false;
219         // whether we've read the CustomPars or ForcePlain tag
220         // for issuing a warning in case MultiPars comes later
221         bool readCustomOrPlain = false;
222
223         string tmp;
224         while (!getout && lex.isOK()) {
225                 int le = lex.lex();
226                 switch (le) {
227                 case Lexer::LEX_UNDEF:
228                         lex.printError("Unknown InsetLayout tag");
229                         if (validating)
230                                 return false;
231                         continue;
232                 default:
233                         break;
234                 }
235                 switch (le) {
236                 // FIXME
237                 // Perhaps a more elegant way to deal with the next two would be the
238                 // way this sort of thing is handled in Layout::read(), namely, by
239                 // using the Lexer.
240                 case IL_LYXTYPE: {
241                         // make sure that we have the right sort of name.
242                         if (name_ != from_ascii("undefined")
243                             && name_.substr(0,5) != from_ascii("Flex:")) {
244                                 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
245                                         "This one has the name `" << to_utf8(name_) << "'\n"
246                                         "Ignoring LyXType declaration.");
247                                 // this is not really a reason to abort
248                                 if (validating)
249                                         return false;
250                                 break;
251                         }
252                         string lt;
253                         lex >> lt;
254                         lyxtype_ = translateLyXType(lt);
255                         if (lyxtype_  == NOLYXTYPE) {
256                                 LYXERR0("Unknown LyXType `" << lt << "'.");
257                                 // this is not really a reason to abort
258                                 if (validating)
259                                         return false;
260                         }
261                         if (lyxtype_ == CHARSTYLE) {
262                                 // by default, charstyles force the plain layout
263                                 multipar_ = false;
264                                 forceplain_ = true;
265                         }
266                         break;
267                 }
268                 case IL_LATEXTYPE:  {
269                         string lt;
270                         lex >> lt;
271                         latextype_ = translateLaTeXType(lt);
272                         if (latextype_  == ILT_ERROR) {
273                                 LYXERR0("Unknown LaTeXType `" << lt << "'.");
274                                 // this is not really a reason to abort
275                                 if (validating)
276                                         return false;
277                         }
278                         break;
279                 }
280                 case IL_LABELSTRING:
281                         lex >> labelstring_;
282                         break;
283                 case IL_MENUSTRING:
284                         lex >> menustring_;
285                         break;
286                 case IL_DECORATION:
287                         lex >> tmp;
288                         decoration_ = translateDecoration(tmp);
289                         break;
290                 case IL_LATEXNAME:
291                         lex >> latexname_;
292                         break;
293                 case IL_LATEXPARAM:
294                         lex >> tmp;
295                         latexparam_ = subst(tmp, "&quot;", "\"");
296                         break;
297                 case IL_LEFTDELIM:
298                         lex >> leftdelim_;
299                         leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
300                                                     from_ascii("\n"));
301                         break;
302                 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
303                         lex >> fixedwidthpreambleencoding_;
304                         break;
305                 case IL_FORCE_LOCAL_FONT_SWITCH:
306                         lex >> forcelocalfontswitch_;
307                         break;
308                 case IL_RIGHTDELIM:
309                         lex >> rightdelim_;
310                         rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
311                                                      from_ascii("\n"));
312                         break;
313                 case IL_LABELFONT:
314                         labelfont_ = lyxRead(lex, inherit_font);
315                         break;
316                 case IL_FORCELTR:
317                         lex >> forceltr_;
318                         break;
319                 case IL_FORCEOWNLINES:
320                         lex >> forceownlines_;
321                         break;
322                 case IL_INTOC:
323                         lex >> intoc_;
324                         break;
325                 case IL_MULTIPAR:
326                         lex >> multipar_;
327                         // the defaults for these depend upon multipar_
328                         if (readCustomOrPlain)
329                                 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
330                                         "Previous value may be overwritten!");
331                         readCustomOrPlain = false;
332                         custompars_ = multipar_;
333                         forceplain_ = !multipar_;
334                         break;
335                 case IL_COUNTER:
336                         lex >> counter_;
337                         break;
338                 case IL_CUSTOMPARS:
339                         lex >> custompars_;
340                         readCustomOrPlain = true;
341                         break;
342                 case IL_FORCEPLAIN:
343                         lex >> forceplain_;
344                         readCustomOrPlain = true;
345                         break;
346                 case IL_PASSTHRU:
347                         lex >> passthru_;
348                         break;
349                 case IL_PASSTHRU_CHARS:
350                         lex >> passthru_chars_;
351                         break;
352                 case IL_NEWLINE_CMD:
353                         lex >> newline_cmd_;
354                         break;
355                 case IL_PARBREAKIGNORED:
356                         lex >> parbreakignored_;
357                         break;
358                 case IL_PARBREAKISNEWLINE:
359                         lex >> parbreakisnewline_;
360                         break;
361                 case IL_KEEPEMPTY:
362                         lex >> keepempty_;
363                         break;
364                 case IL_FREESPACING:
365                         lex >> freespacing_;
366                         break;
367                 case IL_NEEDPROTECT:
368                         lex >> needprotect_;
369                         break;
370                 case IL_NEEDCPROTECT:
371                         lex >> needcprotect_;
372                         break;
373                 case IL_NEEDMBOXPROTECT:
374                         lex >> needmboxprotect_;
375                         break;
376                 case IL_CONTENTASLABEL:
377                         lex >> contentaslabel_;
378                         break;
379                 case IL_COPYSTYLE: {
380                         // initialize with a known style
381                         docstring style;
382                         lex >> style;
383                         style = subst(style, '_', ' ');
384
385                         // We don't want to apply the algorithm in DocumentClass::insetLayout()
386                         // here. So we do it the long way.
387                         TextClass::InsetLayouts::const_iterator it =
388                                         tclass.insetLayouts().find(style);
389                         if (it != tclass.insetLayouts().end()) {
390                                 docstring const tmpname = name_;
391                                 this->operator=(it->second);
392                                 name_ = tmpname;
393                         } else {
394                                 LYXERR0("Cannot copy unknown InsetLayout `"
395                                         << style << "' to InsetLayout `"
396                                         << name() << "'\n"
397                                         << "All InsetLayouts so far:");
398                                 TextClass::InsetLayouts::const_iterator lit =
399                                                 tclass.insetLayouts().begin();
400                                 TextClass::InsetLayouts::const_iterator len =
401                                                 tclass.insetLayouts().end();
402                                 for (; lit != len; ++lit)
403                                         lyxerr << lit->second.name() << "\n";
404                                 // this is not really a reason to abort
405                                 if (validating)
406                                         return false;
407                         }
408                         break;
409                 }
410                 case IL_OBSOLETEDBY: {
411                         docstring style;
412                         lex >> style;
413                         style = subst(style, '_', ' ');
414
415                         // We don't want to apply the algorithm in DocumentClass::insetLayout()
416                         // here. So we do it the long way.
417                         TextClass::InsetLayouts::const_iterator it =
418                                         tclass.insetLayouts().find(style);
419                         if (it != tclass.insetLayouts().end()) {
420                                 docstring const tmpname = name_;
421                                 this->operator=(it->second);
422                                 name_ = tmpname;
423                                 if (obsoleted_by().empty())
424                                         obsoleted_by_ = style;
425                         } else {
426                                 LYXERR0("Cannot replace InsetLayout `"
427                                         << name()
428                                         << "' with unknown InsetLayout `"
429                                         << style << "'\n"
430                                         << "All InsetLayouts so far:");
431                                 TextClass::InsetLayouts::const_iterator lit =
432                                                 tclass.insetLayouts().begin();
433                                 TextClass::InsetLayouts::const_iterator len =
434                                                 tclass.insetLayouts().end();
435                                 for (; lit != len; ++lit)
436                                         lyxerr << lit->second.name() << "\n";
437                                 // this is not really a reason to abort
438                                 if (validating)
439                                         return false;
440                         }
441                         break;
442                 }
443
444                 case IL_FONT: {
445                         font_ = lyxRead(lex, inherit_font);
446                         // If you want to define labelfont, you need to do so after
447                         // font is defined.
448                         labelfont_ = font_;
449                         break;
450                 }
451                 case IL_RESETARGS:
452                         bool reset;
453                         lex >> reset;
454                         if (reset) {
455                                 latexargs_.clear();
456                                 postcommandargs_.clear();
457                         }
458                         break;
459                 case IL_ARGUMENT:
460                         readArgument(lex);
461                         break;
462                 case IL_BGCOLOR:
463                         lex >> tmp;
464                         bgcolor_ = lcolor.getFromLyXName(tmp);
465                         break;
466                 case IL_PREAMBLE:
467                         preamble_ = lex.getLongString(from_ascii("EndPreamble"));
468                         break;
469                 case IL_BABELPREAMBLE:
470                         babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
471                         break;
472                 case IL_LANGPREAMBLE:
473                         langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
474                         break;
475                 case IL_REFPREFIX:
476                         lex >> refprefix_;
477                         break;
478                 case IL_HTMLTAG:
479                         lex >> htmltag_;
480                         break;
481                 case IL_HTMLATTR:
482                         lex >> htmlattr_;
483                         break;
484                 case IL_HTMLFORCECSS:
485                         lex >> htmlforcecss_;
486                         break;
487                 case IL_HTMLINNERTAG:
488                         lex >> htmlinnertag_;
489                         break;
490                 case IL_HTMLINNERATTR:
491                         lex >> htmlinnerattr_;
492                         break;
493                 case IL_HTMLLABEL:
494                         lex >> htmllabel_;
495                         break;
496                 case IL_HTMLISBLOCK:
497                         lex >> htmlisblock_;
498                         break;
499                 case IL_HTMLSTYLE:
500                         htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
501                         break;
502                 case IL_HTMLPREAMBLE:
503                         htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
504                         break;
505                 case IL_DOCBOOKTAG:
506                         lex >> docbooktag_;
507                         break;
508                 case IL_DOCBOOKATTR:
509                         lex >> docbookattr_;
510                         break;
511                 case IL_DOCBOOKTAGTYPE:
512                         lex >> docbooktagtype_;
513                         break;
514                 case IL_DOCBOOKININFO:
515                         lex >> docbookininfo_;
516                         break;
517                 case IL_DOCBOOKSECTION:
518                         lex >> docbooksection_;
519                         break;
520                 case IL_DOCBOOKITEMTAG:
521                         lex >> docbookitemtag_;
522                         break;
523                 case IL_DOCBOOKITEMTAGTYPE:
524                         lex >> docbookitemtagtype_;
525                         break;
526                 case IL_DOCBOOKITEMATTR:
527                         lex >> docbookitemattr_;
528                         break;
529                 case IL_DOCBOOKITEMWRAPPERTAG:
530                         lex >> docbookitemwrappertag_;
531                         break;
532                 case IL_DOCBOOKITEMWRAPPERTAGTYPE:
533                         lex >> docbookitemwrappertagtype_;
534                         break;
535                 case IL_DOCBOOKITEMWRAPPERATTR:
536                         lex >> docbookitemwrapperattr_;
537                         break;
538                 case IL_DOCBOOKWRAPPERTAG:
539                         lex >> docbookwrappertag_;
540                         break;
541                 case IL_DOCBOOKWRAPPERTAGTYPE:
542                         lex >> docbookwrappertagtype_;
543                         break;
544                 case IL_DOCBOOKWRAPPERATTR:
545                         lex >> docbookwrapperattr_;
546                         break;
547                 case IL_REQUIRES: {
548                         lex.eatLine();
549                         vector<string> const req
550                                 = getVectorFromString(lex.getString(true));
551                         required_.insert(req.begin(), req.end());
552                         break;
553                 }
554                 case IL_SPELLCHECK:
555                         lex >> spellcheck_;
556                         break;
557                 case IL_RESETSFONT:
558                         lex >> resetsfont_;
559                         break;
560                 case IL_DISPLAY:
561                         lex >> display_;
562                         break;
563                 case IL_ADDTOTOC:
564                         lex >> toc_type_;
565                         add_to_toc_ = !toc_type_.empty();
566                         break;
567                 case IL_ISTOCCAPTION:
568                         lex >> is_toc_caption_;
569                         break;
570                 case IL_EDITEXTERNAL:
571                         lex >> edit_external_;
572                         break;
573                 case IL_END:
574                         getout = true;
575                         break;
576                 }
577         }
578
579         // Here add element to list if getout == true
580         if (!getout)
581                 return false;
582
583         // The label font is generally used as-is without
584         // any realization against a given context.
585         labelfont_.realize(sane_font);
586
587         lex.popTable();
588         return true;
589 }
590
591
592 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
593 {
594         if (compare_ascii_no_case(str, "charstyle") == 0)
595                 return InsetLayout::CHARSTYLE;
596         if (compare_ascii_no_case(str, "custom") == 0)
597                 return InsetLayout::CUSTOM;
598         if (compare_ascii_no_case(str, "end") == 0)
599                 return InsetLayout::END;
600         if (compare_ascii_no_case(str, "standard") == 0)
601                 return InsetLayout::STANDARD;
602         return InsetLayout::NOLYXTYPE;
603 }
604
605
606 string const & InsetLayout::htmltag() const
607 {
608         if (htmltag_.empty())
609                 htmltag_ = multipar_ ? "div" : "span";
610         return htmltag_;
611 }
612
613
614 string const & InsetLayout::htmlattr() const
615 {
616         if (htmlattr_.empty())
617                 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
618         return htmlattr_;
619 }
620
621
622 string const & InsetLayout::htmlinnerattr() const
623 {
624         if (htmlinnerattr_.empty())
625                 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
626         return htmlinnerattr_;
627 }
628
629
630 string InsetLayout::defaultCSSClass() const
631 {
632         if (!defaultcssclass_.empty())
633                 return defaultcssclass_;
634         string d;
635         string n = to_utf8(name());
636         string::const_iterator it = n.begin();
637         string::const_iterator en = n.end();
638         for (; it != en; ++it) {
639                 if (!isAlphaASCII(*it))
640                         d += "_";
641                 else if (isLower(*it))
642                         d += *it;
643                 else
644                         d += lowercase(*it);
645         }
646         // are there other characters we need to remove?
647         defaultcssclass_ = d;
648         return defaultcssclass_;
649 }
650
651
652 void InsetLayout::makeDefaultCSS() const
653 {
654         if (!htmldefaultstyle_.empty())
655                 return;
656         docstring const mainfontCSS = font_.asCSS();
657         if (!mainfontCSS.empty())
658                 htmldefaultstyle_ =
659                                 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
660                                 mainfontCSS + from_ascii("\n}\n");
661 }
662
663
664 docstring InsetLayout::htmlstyle() const
665 {
666         if (!htmlstyle_.empty() && !htmlforcecss_)
667                 return htmlstyle_;
668         if (htmldefaultstyle_.empty())
669                 makeDefaultCSS();
670         docstring retval = htmldefaultstyle_;
671         if (!htmlstyle_.empty())
672                 retval += '\n' + htmlstyle_ + '\n';
673         return retval;
674 }
675
676
677 std::string const & InsetLayout::docbookininfo() const
678 {
679         // Same as Layout::docbookininfo.
680         // Indeed, a trilean. Only titles should be "maybe": otherwise, metadata is "always", content is "never".
681         if (docbookininfo_.empty() || (docbookininfo_ != "never" && docbookininfo_ != "always" && docbookininfo_ != "maybe"))
682                 docbookininfo_ = "never";
683         return docbookininfo_;
684 }
685
686
687 std::string InsetLayout::docbooktagtype() const
688 {
689         if (docbooktagtype_ != "block" && docbooktagtype_ != "paragraph" && docbooktagtype_ != "inline")
690                 docbooktagtype_ = "block";
691         return docbooktagtype_;
692 }
693
694
695 std::string InsetLayout::docbookwrappertagtype() const
696 {
697         if (docbookwrappertagtype_ != "block" && docbookwrappertagtype_ != "paragraph" && docbookwrappertagtype_ != "inline")
698                 docbookwrappertagtype_ = "block";
699         return docbookwrappertagtype_;
700 }
701
702
703 std::string InsetLayout::docbookitemtagtype() const
704 {
705         if (docbookitemtagtype_ != "block" && docbookitemtagtype_ != "paragraph" && docbookitemtagtype_ != "inline")
706                 docbookitemtagtype_ = "block";
707         return docbookitemtagtype_;
708 }
709
710
711 std::string InsetLayout::docbookitemwrappertagtype() const
712 {
713         if (docbookitemwrappertagtype_ != "block" && docbookitemwrappertagtype_ != "paragraph" && docbookitemwrappertagtype_ != "inline")
714                 docbookitemwrappertagtype_ = "block";
715         return docbookitemwrappertagtype_;
716 }
717
718
719 void InsetLayout::readArgument(Lexer & lex)
720 {
721         Layout::latexarg arg;
722         arg.mandatory = false;
723         arg.autoinsert = false;
724         arg.insertcotext = false;
725         arg.insertonnewline = false;
726         bool error = false;
727         bool finished = false;
728         arg.font = inherit_font;
729         arg.labelfont = inherit_font;
730         arg.is_toc_caption = false;
731         arg.free_spacing = false;
732         arg.passthru = PT_INHERITED;
733         arg.nodelims = false;
734         string nr;
735         lex >> nr;
736         bool const postcmd = prefixIs(nr, "post:");
737         while (!finished && lex.isOK() && !error) {
738                 lex.next();
739                 string const tok = ascii_lowercase(lex.getString());
740
741                 if (tok.empty()) {
742                         continue;
743                 } else if (tok == "endargument") {
744                         finished = true;
745                 } else if (tok == "labelstring") {
746                         lex.next();
747                         arg.labelstring = lex.getDocString();
748                 } else if (tok == "menustring") {
749                         lex.next();
750                         arg.menustring = lex.getDocString();
751                 } else if (tok == "mandatory") {
752                         lex.next();
753                         arg.mandatory = lex.getBool();
754                 } else if (tok == "autoinsert") {
755                         lex.next();
756                         arg.autoinsert = lex.getBool();
757                 } else if (tok == "insertcotext") {
758                         lex.next();
759                         arg.insertcotext = lex.getBool();
760                 } else if (tok == "insertonnewline") {
761                         lex.next();
762                         arg.insertonnewline = lex.getBool();
763                 } else if (tok == "leftdelim") {
764                         lex.next();
765                         arg.ldelim = lex.getDocString();
766                         arg.ldelim = subst(arg.ldelim,
767                                                     from_ascii("<br/>"), from_ascii("\n"));
768                 } else if (tok == "rightdelim") {
769                         lex.next();
770                         arg.rdelim = lex.getDocString();
771                         arg.rdelim = subst(arg.rdelim,
772                                                     from_ascii("<br/>"), from_ascii("\n"));
773                 } else if (tok == "defaultarg") {
774                         lex.next();
775                         arg.defaultarg = lex.getDocString();
776                 } else if (tok == "presetarg") {
777                         lex.next();
778                         arg.presetarg = lex.getDocString();
779                 } else if (tok == "tooltip") {
780                         lex.next();
781                         arg.tooltip = lex.getDocString();
782                 } else if (tok == "requires") {
783                         lex.next();
784                         arg.required = lex.getString();
785                 } else if (tok == "decoration") {
786                         lex.next();
787                         arg.decoration = lex.getString();
788                 } else if (tok == "newlinecmd") {
789                         lex.next();
790                         arg.newlinecmd = lex.getString();
791                 } else if (tok == "font") {
792                         arg.font = lyxRead(lex, arg.font);
793                 } else if (tok == "labelfont") {
794                         arg.labelfont = lyxRead(lex, arg.labelfont);
795                 } else if (tok == "passthruchars") {
796                         lex.next();
797                         arg.pass_thru_chars = lex.getDocString();
798                 } else if (tok == "passthru") {
799                         lex.next();
800                         docstring value = lex.getDocString();
801                         if (value == "true" || value == "1")
802                                 arg.passthru = PT_TRUE;
803                         else if (value == "false" || value == "0")
804                                 arg.passthru = PT_FALSE;
805                         else
806                                 arg.passthru = PT_INHERITED;
807                 } else if (tok == "istoccaption") {
808                         lex.next();
809                         arg.is_toc_caption = lex.getBool();
810                 } else if (tok == "freespacing") {
811                         lex.next();
812                         arg.free_spacing = lex.getBool();
813                 } else if (tok == "docbooktag") {
814                         lex.next();
815                         arg.docbooktag = lex.getDocString();
816                 } else if (tok == "docbookattr") {
817                         lex.next();
818                         arg.docbookattr = lex.getDocString();
819                 } else if (tok == "docbooktagtype") {
820                         lex.next();
821                         arg.docbooktagtype = lex.getDocString();
822                 } else {
823                         lex.printError("Unknown tag");
824                         error = true;
825                 }
826         }
827         if (arg.labelstring.empty())
828                 LYXERR0("Incomplete Argument definition!");
829         else if (postcmd)
830                 postcommandargs_[nr] = arg;
831         else
832                 latexargs_[nr] = arg;
833 }
834
835
836 Layout::LaTeXArgMap InsetLayout::args() const
837 {
838         Layout::LaTeXArgMap args = latexargs_;
839         if (!postcommandargs_.empty())
840                 args.insert(postcommandargs_.begin(), postcommandargs_.end());
841         return args;
842 }
843
844
845 int InsetLayout::optArgs() const
846 {
847         int nr = 0;
848         Layout::LaTeXArgMap const args = InsetLayout::args();
849         Layout::LaTeXArgMap::const_iterator it = args.begin();
850         for (; it != args.end(); ++it) {
851                 if (!(*it).second.mandatory)
852                         ++nr;
853         }
854         return nr;
855 }
856
857
858 int InsetLayout::requiredArgs() const
859 {
860         int nr = 0;
861         Layout::LaTeXArgMap const args = InsetLayout::args();
862         Layout::LaTeXArgMap::const_iterator it = args.begin();
863         for (; it != args.end(); ++it) {
864                 if ((*it).second.mandatory)
865                         ++nr;
866         }
867         return nr;
868 }
869
870
871 } //namespace lyx