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