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