]> git.lyx.org Git - lyx.git/blob - src/Layout.cpp
Allow to specify default argument for layout/inset argments
[lyx.git] / src / Layout.cpp
1 /**
2  * \file Layout.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author André Pönitz
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "Layout.h"
16 #include "FontInfo.h"
17 #include "Language.h"
18 #include "Lexer.h"
19 #include "output_xhtml.h"
20 #include "TextClass.h"
21
22 #include "support/debug.h"
23 #include "support/lassert.h"
24 #include "support/lstrings.h"
25 #include "support/Messages.h"
26 #include "support/textutils.h"
27
28
29 using namespace std;
30 using namespace lyx::support;
31
32 namespace lyx {
33
34 /// Special value of toclevel for layouts that to not belong in a TOC
35 const int Layout::NOT_IN_TOC = -1000;
36
37 //  The order of the LayoutTags enum is no more important. [asierra300396]
38 // Tags indexes.
39 enum LayoutTags {
40         LT_ALIGN = 1,
41         LT_ALIGNPOSSIBLE,
42         LT_ARGUMENT,
43         LT_MARGIN,
44         LT_BOTTOMSEP,
45         LT_CATEGORY,
46         LT_COMMANDDEPTH,
47         LT_COPYSTYLE,
48         LT_DEPENDSON,
49         LT_OBSOLETEDBY,
50         LT_END,
51         LT_FONT,
52         LT_FREE_SPACING,
53         LT_PASS_THRU,
54         LT_PARBREAK_IS_NEWLINE,
55         LT_ITEMCOMMAND,
56         LT_ITEMSEP,
57         LT_KEEPEMPTY,
58         LT_LABEL_BOTTOMSEP,
59         LT_LABELFONT,
60         LT_TEXTFONT,
61         LT_LABELINDENT,
62         LT_LABELSEP,
63         LT_LABELSTRING,
64         LT_LABELSTRING_APPENDIX,
65         LT_LABELCOUNTER,
66         LT_LABELTYPE,
67         LT_ENDLABELSTRING,
68         LT_ENDLABELTYPE,
69         LT_LATEXNAME,
70         LT_LATEXPARAM,
71         LT_LATEXTYPE,
72         LT_LEFTDELIM,
73         LT_LEFTMARGIN,
74         LT_NEED_PROTECT,
75         LT_NEWLINE,
76         LT_NEXTNOINDENT,
77         LT_PARINDENT,
78         LT_PARSEP,
79         LT_PARSKIP,
80         LT_PREAMBLE,
81         LT_LANGPREAMBLE,
82         LT_BABELPREAMBLE,
83         LT_REQUIRES,
84         LT_RIGHTMARGIN,
85         LT_SPACING,
86         LT_TOPSEP,
87         LT_TOCLEVEL,
88         LT_INNERTAG,
89         LT_LABELTAG,
90         LT_ITEMTAG,
91         LT_HTMLTAG,
92         LT_HTMLATTR,
93         LT_HTMLITEM,
94         LT_HTMLITEMATTR,
95         LT_HTMLLABEL,
96         LT_HTMLLABELATTR, 
97         LT_HTMLLABELFIRST,
98         LT_HTMLPREAMBLE,
99         LT_HTMLSTYLE,
100         LT_HTMLFORCECSS,
101         LT_INPREAMBLE,
102         LT_HTMLTITLE,
103         LT_SPELLCHECK,
104         LT_REFPREFIX,
105         LT_RESETARGS,
106         LT_RIGHTDELIM,
107         LT_INTITLE // keep this last!
108 };
109
110 /////////////////////
111
112 Layout::Layout()
113 {
114         unknown_ = false;
115         margintype = MARGIN_STATIC;
116         latextype = LATEX_PARAGRAPH;
117         intitle = false;
118         inpreamble = false;
119         needprotect = false;
120         keepempty = false;
121         font = inherit_font;
122         labelfont = inherit_font;
123         resfont = sane_font;
124         reslabelfont = sane_font;
125         nextnoindent = false;
126         parskip = 0.0;
127         itemsep = 0;
128         topsep = 0.0;
129         bottomsep = 0.0;
130         labelbottomsep = 0.0;
131         parsep = 0;
132         align = LYX_ALIGN_BLOCK;
133         alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
134         labeltype = LABEL_NO_LABEL;
135         endlabeltype = END_LABEL_NO_LABEL;
136         // Should or should not. That is the question.
137         // spacing.set(Spacing::OneHalf);
138         newline_allowed = true;
139         free_spacing = false;
140         pass_thru = false;
141         parbreak_is_newline = false;
142         toclevel = NOT_IN_TOC;
143         commanddepth = 0;
144         htmllabelfirst_ = false;
145         htmlforcecss_ = false;
146         htmltitle_ = false;
147         spellcheck = true;
148         itemcommand_ = "item";
149 }
150
151
152 bool Layout::read(Lexer & lex, TextClass const & tclass)
153 {
154         // This table is sorted alphabetically [asierra 30March96]
155         LexerKeyword layoutTags[] = {
156                 { "align",          LT_ALIGN },
157                 { "alignpossible",  LT_ALIGNPOSSIBLE },
158                 { "argument",       LT_ARGUMENT },
159                 { "babelpreamble",  LT_BABELPREAMBLE },
160                 { "bottomsep",      LT_BOTTOMSEP },
161                 { "category",       LT_CATEGORY },
162                 { "commanddepth",   LT_COMMANDDEPTH },
163                 { "copystyle",      LT_COPYSTYLE },
164                 { "dependson",      LT_DEPENDSON },
165                 { "end",            LT_END },
166                 { "endlabelstring", LT_ENDLABELSTRING },
167                 { "endlabeltype",   LT_ENDLABELTYPE },
168                 { "font",           LT_FONT },
169                 { "freespacing",    LT_FREE_SPACING },
170                 { "htmlattr",       LT_HTMLATTR },
171                 { "htmlforcecss",   LT_HTMLFORCECSS },
172                 { "htmlitem",       LT_HTMLITEM },
173                 { "htmlitemattr",   LT_HTMLITEMATTR },
174                 { "htmllabel",      LT_HTMLLABEL },
175                 { "htmllabelattr",  LT_HTMLLABELATTR },
176                 { "htmllabelfirst", LT_HTMLLABELFIRST },
177                 { "htmlpreamble",   LT_HTMLPREAMBLE },
178                 { "htmlstyle",      LT_HTMLSTYLE },
179                 { "htmltag",        LT_HTMLTAG },
180                 { "htmltitle",      LT_HTMLTITLE },
181                 { "innertag",       LT_INNERTAG },
182                 { "inpreamble",     LT_INPREAMBLE },
183                 { "intitle",        LT_INTITLE },
184                 { "itemcommand",    LT_ITEMCOMMAND },
185                 { "itemsep",        LT_ITEMSEP },
186                 { "itemtag",        LT_ITEMTAG },
187                 { "keepempty",      LT_KEEPEMPTY },
188                 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
189                 { "labelcounter",   LT_LABELCOUNTER },
190                 { "labelfont",      LT_LABELFONT },
191                 { "labelindent",    LT_LABELINDENT },
192                 { "labelsep",       LT_LABELSEP },
193                 { "labelstring",    LT_LABELSTRING },
194                 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
195                 { "labeltag",       LT_LABELTAG },
196                 { "labeltype",      LT_LABELTYPE },
197                 { "langpreamble",   LT_LANGPREAMBLE },
198                 { "latexname",      LT_LATEXNAME },
199                 { "latexparam",     LT_LATEXPARAM },
200                 { "latextype",      LT_LATEXTYPE },
201                 { "leftdelim",      LT_LEFTDELIM },
202                 { "leftmargin",     LT_LEFTMARGIN },
203                 { "margin",         LT_MARGIN },
204                 { "needprotect",    LT_NEED_PROTECT },
205                 { "newline",        LT_NEWLINE },
206                 { "nextnoindent",   LT_NEXTNOINDENT },
207                 { "obsoletedby",    LT_OBSOLETEDBY },
208                 { "parbreakisnewline", LT_PARBREAK_IS_NEWLINE },
209                 { "parindent",      LT_PARINDENT },
210                 { "parsep",         LT_PARSEP },
211                 { "parskip",        LT_PARSKIP },
212                 { "passthru",       LT_PASS_THRU },
213                 { "preamble",       LT_PREAMBLE },
214                 { "refprefix",      LT_REFPREFIX },
215                 { "requires",       LT_REQUIRES },
216                 { "resetargs",      LT_RESETARGS },
217                 { "rightdelim",     LT_RIGHTDELIM },
218                 { "rightmargin",    LT_RIGHTMARGIN },
219                 { "spacing",        LT_SPACING },
220                 { "spellcheck",     LT_SPELLCHECK },
221                 { "textfont",       LT_TEXTFONT },
222                 { "toclevel",       LT_TOCLEVEL },
223                 { "topsep",         LT_TOPSEP }
224         };
225
226         bool error = false;
227         bool finished = false;
228         lex.pushTable(layoutTags);
229
230         // parse style section
231         while (!finished && lex.isOK() && !error) {
232                 int le = lex.lex();
233                 // See comment in LyXRC.cpp.
234                 switch (le) {
235                 case Lexer::LEX_FEOF:
236                         continue;
237
238                 case Lexer::LEX_UNDEF:
239                         // parse error
240                         lex.printError("Unknown layout tag `$$Token'");
241                         error = true;
242                         continue;
243
244                 default: 
245                         break;
246                 }
247                 switch (static_cast<LayoutTags>(le)) {
248                 case LT_END:
249                         finished = true;
250                         break;
251
252                 case LT_CATEGORY:
253                         lex >> category_;
254                         break;
255
256                 case LT_COPYSTYLE: {
257                         docstring style;
258                         lex >> style;
259                         style = subst(style, '_', ' ');
260
261                         if (tclass.hasLayout(style)) {
262                                 docstring const tmpname = name_;
263                                 this->operator=(tclass[style]);
264                                 name_ = tmpname;
265                         } else {
266                                 LYXERR0("Cannot copy unknown style `"
267                                         << style << "'\n"
268                                         << "All layouts so far:");
269                                 DocumentClass::const_iterator lit = tclass.begin();
270                                 DocumentClass::const_iterator len = tclass.end();
271                                 for (; lit != len; ++lit)
272                                         LYXERR0(lit->name());
273                         }
274                         break;
275                         }
276
277                 case LT_OBSOLETEDBY: {
278                         docstring style;
279                         lex >> style;
280                         style = subst(style, '_', ' ');
281
282                         if (tclass.hasLayout(style)) {
283                                 docstring const tmpname = name_;
284                                 this->operator=(tclass[style]);
285                                 name_ = tmpname;
286                                 if (obsoleted_by().empty())
287                                         obsoleted_by_ = style;
288                         } else {
289                                 LYXERR0("Cannot replace with unknown style `" 
290                                         << style << '\'');
291
292                                 //lex.printError("Cannot replace with"
293                                 //               " unknown style "
294                                 //               "`$$Token'");
295                         }
296                         break;
297                 }
298
299                 case LT_DEPENDSON:
300                         lex >> depends_on_;
301                         depends_on_ = subst(depends_on_, '_', ' ');
302                         break;
303
304                 case LT_MARGIN:
305                         readMargin(lex);
306                         break;
307
308                 case LT_LATEXTYPE:
309                         readLatexType(lex);
310                         break;
311
312                 case LT_INTITLE:
313                         lex >> intitle;
314                         break;
315
316                 case LT_INPREAMBLE:
317                         lex >> inpreamble;
318                         break;
319
320                 case LT_TOCLEVEL:
321                         lex >> toclevel;
322                         break;
323
324                 case LT_RESETARGS:
325                         bool reset;
326                         lex >> reset;
327                         if (reset) {
328                                 latexargs_.clear();
329                                 itemargs_.clear();
330                                 postcommandargs_.clear();
331                         }
332                         break;
333
334                 case LT_ARGUMENT:
335                         readArgument(lex);
336                         break;
337
338                 case LT_NEED_PROTECT:
339                         lex >> needprotect;
340                         break;
341
342                 case LT_KEEPEMPTY:
343                         lex >> keepempty;
344                         break;
345
346                 case LT_FONT:
347                         font = lyxRead(lex, font);
348                         labelfont = font;
349                         break;
350
351                 case LT_TEXTFONT:
352                         font = lyxRead(lex, font);
353                         break;
354
355                 case LT_LABELFONT:
356                         labelfont = lyxRead(lex, labelfont);
357                         break;
358
359                 case LT_NEXTNOINDENT:
360                         lex >> nextnoindent;
361                         break;
362
363                 case LT_COMMANDDEPTH:
364                         lex >> commanddepth;
365                         break;
366
367                 case LT_LATEXNAME:
368                         lex >> latexname_;
369                         break;
370
371                 case LT_LATEXPARAM:
372                         lex >> latexparam_;
373                         latexparam_ = subst(latexparam_, "&quot;", "\"");
374                         break;
375
376                 case LT_LEFTDELIM:
377                         lex >> leftdelim_;
378                         leftdelim_ = support::subst(leftdelim_, from_ascii("<br/>"),
379                                                     from_ascii("\n"));
380                         break;
381
382                 case LT_RIGHTDELIM:
383                         lex >> rightdelim_;
384                         rightdelim_ = support::subst(rightdelim_, from_ascii("<br/>"),
385                                                      from_ascii("\n"));
386                         break;
387
388                 case LT_INNERTAG:
389                         lex >> innertag_;
390                         break;
391
392                 case LT_LABELTAG:
393                         lex >> labeltag_;
394                         break;
395
396                 case LT_ITEMTAG:
397                         lex >> itemtag_;
398                         break;
399
400                 case LT_ITEMCOMMAND:
401                         lex >> itemcommand_;
402                         break;
403
404                 case LT_PREAMBLE:
405                         preamble_ = from_utf8(lex.getLongString("EndPreamble"));
406                         break;
407
408                 case LT_LANGPREAMBLE:
409                         langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
410                         break;
411
412                 case LT_BABELPREAMBLE:
413                         babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
414                         break;
415
416                 case LT_LABELTYPE:
417                         readLabelType(lex);
418                         break;
419
420                 case LT_ENDLABELTYPE:
421                         readEndLabelType(lex);
422                         break;
423
424                 case LT_LEFTMARGIN:
425                         lex >> leftmargin;
426                         break;
427
428                 case LT_RIGHTMARGIN:
429                         lex >> rightmargin;
430                         break;
431
432                 case LT_LABELINDENT:
433                         lex >> labelindent;
434                         break;
435
436                 case LT_PARINDENT:
437                         lex >> parindent;
438                         break;
439
440                 case LT_PARSKIP:
441                         lex >> parskip;
442                         break;
443
444                 case LT_ITEMSEP:
445                         lex >> itemsep;
446                         break;
447
448                 case LT_TOPSEP:
449                         lex >> topsep;
450                         break;
451
452                 case LT_BOTTOMSEP:
453                         lex >> bottomsep;
454                         break;
455
456                 case LT_LABEL_BOTTOMSEP:
457                         lex >> labelbottomsep;
458                         break;
459
460                 case LT_LABELSEP:
461                         lex >> labelsep;
462                         labelsep = subst(labelsep, 'x', ' ');
463                         break;
464
465                 case LT_PARSEP:
466                         lex >> parsep;
467                         break;
468
469                 case LT_NEWLINE:
470                         lex >> newline_allowed;
471                         break;
472
473                 case LT_ALIGN:
474                         readAlign(lex);
475                         break;
476         
477                 case LT_ALIGNPOSSIBLE:
478                         readAlignPossible(lex);
479                         break;
480
481                 case LT_LABELSTRING:
482                         // FIXME: this means LT_ENDLABELSTRING may only
483                         // occur after LT_LABELSTRING
484                         lex >> labelstring_;
485                         labelstring_ = trim(labelstring_);
486                         labelstring_appendix_ = labelstring_;
487                         break;
488
489                 case LT_ENDLABELSTRING:
490                         lex >> endlabelstring_; 
491                         endlabelstring_ = trim(endlabelstring_);
492                         break;
493
494                 case LT_LABELSTRING_APPENDIX:
495                         lex >> labelstring_appendix_;   
496                         labelstring_appendix_ = trim(labelstring_appendix_);
497                         break;
498
499                 case LT_LABELCOUNTER:
500                         lex >> counter; 
501                         counter = trim(counter);
502                         break;
503
504                 case LT_FREE_SPACING:
505                         lex >> free_spacing;
506                         break;
507
508                 case LT_PASS_THRU:
509                         lex >> pass_thru;
510                         break;
511
512                 case LT_PARBREAK_IS_NEWLINE:
513                         lex >> parbreak_is_newline;
514                         break;
515
516                 case LT_SPACING:
517                         readSpacing(lex);
518                         break;
519
520                 case LT_REQUIRES: {
521                         lex.eatLine();
522                         vector<string> const req = 
523                                 getVectorFromString(lex.getString());
524                         requires_.insert(req.begin(), req.end());
525                         break;
526                 }
527                         
528                 case LT_REFPREFIX: {
529                         docstring arg;
530                         lex >> arg;
531                         if (arg == "OFF")
532                                 refprefix.clear();
533                         else
534                                 refprefix = arg;
535                         break;
536                 }
537
538                 case LT_HTMLTAG:
539                         lex >> htmltag_;
540                         break;
541         
542                 case LT_HTMLATTR:
543                         lex >> htmlattr_;
544                         break;
545
546                 case LT_HTMLITEM:
547                         lex >> htmlitemtag_;
548                         break;
549         
550                 case LT_HTMLITEMATTR:
551                         lex >> htmlitemattr_;
552                         break;
553         
554                 case LT_HTMLLABEL:
555                         lex >> htmllabeltag_;
556                         break;
557
558                 case LT_HTMLLABELATTR: 
559                         lex >> htmllabelattr_;
560                         break;
561
562                 case LT_HTMLLABELFIRST:
563                         lex >> htmllabelfirst_;
564                         break;
565                         
566                 case LT_HTMLSTYLE:
567                         htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
568                         break;
569
570                 case LT_HTMLFORCECSS:
571                         lex >> htmlforcecss_;
572                         break;
573
574                 case LT_HTMLPREAMBLE:
575                         htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
576                         break;
577                 
578                 case LT_HTMLTITLE:
579                         lex >> htmltitle_;
580                         break;
581
582                 case LT_SPELLCHECK:
583                         lex >> spellcheck;
584                         break;
585                 }
586         }
587         lex.popTable();
588         // make sure we only have inpreamble = true for commands
589         if (inpreamble && latextype != LATEX_COMMAND && latextype != LATEX_PARAGRAPH) {
590                 LYXERR0("InPreamble not permitted except with command and paragraph layouts.");
591                 LYXERR0("Layout name: " << name());
592                 inpreamble = false;
593         }
594
595         return finished && !error;
596 }
597
598
599 enum {
600         AT_BLOCK = 1,
601         AT_LEFT,
602         AT_RIGHT,
603         AT_CENTER,
604         AT_LAYOUT
605 };
606
607
608 LexerKeyword alignTags[] = {
609         { "block",  AT_BLOCK },
610         { "center", AT_CENTER },
611         { "layout", AT_LAYOUT },
612         { "left",   AT_LEFT },
613         { "right",  AT_RIGHT }
614 };
615
616
617 void Layout::readAlign(Lexer & lex)
618 {
619         PushPopHelper pph(lex, alignTags);
620         int le = lex.lex();
621         switch (le) {
622         case Lexer::LEX_UNDEF:
623                 lex.printError("Unknown alignment `$$Token'");
624                 return;
625         default: break;
626         };
627         switch (le) {
628         case AT_BLOCK:
629                 align = LYX_ALIGN_BLOCK;
630                 break;
631         case AT_LEFT:
632                 align = LYX_ALIGN_LEFT;
633                 break;
634         case AT_RIGHT:
635                 align = LYX_ALIGN_RIGHT;
636                 break;
637         case AT_CENTER:
638                 align = LYX_ALIGN_CENTER;
639                 break;
640         case AT_LAYOUT:
641                 align = LYX_ALIGN_LAYOUT;
642                 break;
643         }
644 }
645
646
647 void Layout::readAlignPossible(Lexer & lex)
648 {
649         lex.pushTable(alignTags);
650         alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
651         int lineno = lex.lineNumber();
652         do {
653                 int le = lex.lex();
654                 switch (le) {
655                 case Lexer::LEX_UNDEF:
656                         lex.printError("Unknown alignment `$$Token'");
657                         continue;
658                 default: break;
659                 };
660                 switch (le) {
661                 case AT_BLOCK:
662                         alignpossible |= LYX_ALIGN_BLOCK;
663                         break;
664                 case AT_LEFT:
665                         alignpossible |= LYX_ALIGN_LEFT;
666                         break;
667                 case AT_RIGHT:
668                         alignpossible |= LYX_ALIGN_RIGHT;
669                         break;
670                 case AT_CENTER:
671                         alignpossible |= LYX_ALIGN_CENTER;
672                         break;
673                 case AT_LAYOUT:
674                         alignpossible |= LYX_ALIGN_LAYOUT;
675                         break;
676                 }
677         } while (lineno == lex.lineNumber());
678         lex.popTable();
679 }
680
681
682 void Layout::readLabelType(Lexer & lex)
683 {
684         enum {
685                 LA_NO_LABEL = 1,
686                 LA_MANUAL,
687                 LA_ABOVE,
688                 LA_CENTERED,
689                 LA_STATIC,
690                 LA_SENSITIVE,
691                 LA_ENUMERATE,
692                 LA_ITEMIZE,
693                 LA_BIBLIO
694         };
695
696
697         LexerKeyword labelTypeTags[] = {
698           { "above",        LA_ABOVE },
699                 { "bibliography", LA_BIBLIO },
700                 { "centered",     LA_CENTERED },
701                 { "enumerate",    LA_ENUMERATE },
702                 { "itemize",      LA_ITEMIZE },
703                 { "manual",       LA_MANUAL },
704                 { "no_label",     LA_NO_LABEL },
705                 { "sensitive",    LA_SENSITIVE },
706                 { "static",       LA_STATIC }
707         };
708
709         PushPopHelper pph(lex, labelTypeTags);
710         int le = lex.lex();
711         switch (le) {
712         case Lexer::LEX_UNDEF:
713                 lex.printError("Unknown labeltype tag `$$Token'");
714                 return;
715         default: break;
716         }
717         switch (le) {
718         case LA_NO_LABEL:
719                 labeltype = LABEL_NO_LABEL;
720                 break;
721         case LA_MANUAL:
722                 labeltype = LABEL_MANUAL;
723                 break;
724         case LA_ABOVE:
725                 labeltype = LABEL_ABOVE;
726                 break;
727         case LA_CENTERED:
728                 labeltype = LABEL_CENTERED;
729                 break;
730         case LA_STATIC:
731                 labeltype = LABEL_STATIC;
732                 break;
733         case LA_SENSITIVE:
734                 labeltype = LABEL_SENSITIVE;
735                 break;
736         case LA_ENUMERATE:
737                 labeltype = LABEL_ENUMERATE;
738                 break;
739         case LA_ITEMIZE:
740                 labeltype = LABEL_ITEMIZE;
741                 break;
742         case LA_BIBLIO:
743                 labeltype = LABEL_BIBLIO;
744                 break;
745         }
746 }
747
748
749 void Layout::readEndLabelType(Lexer & lex)
750 {
751         static LexerKeyword endlabelTypeTags[] = {
752                 { "box",              END_LABEL_BOX },
753                 { "filled_box", END_LABEL_FILLED_BOX },
754                 { "no_label",     END_LABEL_NO_LABEL },
755                 { "static",     END_LABEL_STATIC }
756         };
757
758         PushPopHelper pph(lex, endlabelTypeTags);
759         int le = lex.lex();
760         switch (le) {
761         case Lexer::LEX_UNDEF:
762                 lex.printError("Unknown labeltype tag `$$Token'");
763                 break;
764         case END_LABEL_STATIC:
765         case END_LABEL_BOX:
766         case END_LABEL_FILLED_BOX:
767         case END_LABEL_NO_LABEL:
768                 endlabeltype = static_cast<EndLabelType>(le);
769                 break;
770         default:
771                 LYXERR0("Unhandled value " << le);
772                 break;
773         }
774 }
775
776
777 void Layout::readMargin(Lexer & lex)
778 {
779         LexerKeyword marginTags[] = {
780                 { "dynamic",           MARGIN_DYNAMIC },
781                 { "first_dynamic",     MARGIN_FIRST_DYNAMIC },
782                 { "manual",            MARGIN_MANUAL },
783                 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
784                 { "static",            MARGIN_STATIC }
785         };
786
787         PushPopHelper pph(lex, marginTags);
788
789         int le = lex.lex();
790         switch (le) {
791         case Lexer::LEX_UNDEF:
792                 lex.printError("Unknown margin type tag `$$Token'");
793                 return;
794         case MARGIN_STATIC:
795         case MARGIN_MANUAL:
796         case MARGIN_DYNAMIC:
797         case MARGIN_FIRST_DYNAMIC:
798         case MARGIN_RIGHT_ADDRESS_BOX:
799                 margintype = static_cast<MarginType>(le);
800                 break;
801         default:
802                 LYXERR0("Unhandled value " << le);
803                 break;
804         }
805 }
806
807
808 void Layout::readLatexType(Lexer & lex)
809 {
810         LexerKeyword latexTypeTags[] = {
811                 { "bib_environment",  LATEX_BIB_ENVIRONMENT },
812                 { "command",          LATEX_COMMAND },
813                 { "environment",      LATEX_ENVIRONMENT },
814                 { "item_environment", LATEX_ITEM_ENVIRONMENT },
815                 { "list_environment", LATEX_LIST_ENVIRONMENT },
816                 { "paragraph",        LATEX_PARAGRAPH }
817         };
818
819         PushPopHelper pph(lex, latexTypeTags);
820         int le = lex.lex();
821         switch (le) {
822         case Lexer::LEX_UNDEF:
823                 lex.printError("Unknown latextype tag `$$Token'");
824                 return;
825         case LATEX_PARAGRAPH:
826         case LATEX_COMMAND:
827         case LATEX_ENVIRONMENT:
828         case LATEX_ITEM_ENVIRONMENT:
829         case LATEX_BIB_ENVIRONMENT:
830         case LATEX_LIST_ENVIRONMENT:
831                 latextype = static_cast<LatexType>(le);
832                 break;
833         default:
834                 LYXERR0("Unhandled value " << le);
835                 break;
836         }
837 }
838
839
840 void Layout::readSpacing(Lexer & lex)
841 {
842         enum {
843                 ST_SPACING_SINGLE = 1,
844                 ST_SPACING_ONEHALF,
845                 ST_SPACING_DOUBLE,
846                 ST_OTHER
847         };
848
849         LexerKeyword spacingTags[] = {
850                 {"double",  ST_SPACING_DOUBLE },
851                 {"onehalf", ST_SPACING_ONEHALF },
852                 {"other",   ST_OTHER },
853                 {"single",  ST_SPACING_SINGLE }
854         };
855
856         PushPopHelper pph(lex, spacingTags);
857         int le = lex.lex();
858         switch (le) {
859         case Lexer::LEX_UNDEF:
860                 lex.printError("Unknown spacing token `$$Token'");
861                 return;
862         default: break;
863         }
864         switch (le) {
865         case ST_SPACING_SINGLE:
866                 spacing.set(Spacing::Single);
867                 break;
868         case ST_SPACING_ONEHALF:
869                 spacing.set(Spacing::Onehalf);
870                 break;
871         case ST_SPACING_DOUBLE:
872                 spacing.set(Spacing::Double);
873                 break;
874         case ST_OTHER:
875                 lex.next();
876                 spacing.set(Spacing::Other, lex.getString());
877                 break;
878         }
879 }
880
881
882 void Layout::readArgument(Lexer & lex)
883 {
884         latexarg arg;
885         arg.mandatory = false;
886         arg.autoinsert = false;
887         bool error = false;
888         bool finished = false;
889         arg.font = inherit_font;
890         arg.labelfont = inherit_font;
891         string id;
892         lex >> id;
893         bool const itemarg = prefixIs(id, "item:");
894         bool const postcmd = prefixIs(id, "post:");
895
896         while (!finished && lex.isOK() && !error) {
897                 lex.next();
898                 string const tok = ascii_lowercase(lex.getString());
899
900                 if (tok.empty()) {
901                         continue;
902                 } else if (tok == "endargument") {
903                         finished = true;
904                 } else if (tok == "labelstring") {
905                         lex.next();
906                         arg.labelstring = lex.getDocString();
907                 } else if (tok == "menustring") {
908                         lex.next();
909                         arg.menustring = lex.getDocString();
910                 } else if (tok == "mandatory") {
911                         lex.next();
912                         arg.mandatory = lex.getBool();
913                 } else if (tok == "autoinsert") {
914                         lex.next();
915                         arg.autoinsert = lex.getBool();
916                 } else if (tok == "leftdelim") {
917                         lex.next();
918                         arg.ldelim = lex.getDocString();
919                         arg.ldelim = support::subst(arg.ldelim, from_ascii("<br/>"),
920                                                     from_ascii("\n"));
921                 } else if (tok == "rightdelim") {
922                         lex.next();
923                         arg.rdelim = lex.getDocString();
924                         arg.rdelim = support::subst(arg.rdelim, from_ascii("<br/>"),
925                                                     from_ascii("\n"));
926                 } else if (tok == "defaultarg") {
927                         lex.next();
928                         arg.defaultarg = lex.getDocString();
929                 } else if (tok == "presetarg") {
930                         lex.next();
931                         arg.presetarg = lex.getDocString();
932                 } else if (tok == "tooltip") {
933                         lex.next();
934                         arg.tooltip = lex.getDocString();
935                 } else if (tok == "requires") {
936                         lex.next();
937                         arg.requires = lex.getString();
938                 } else if (tok == "decoration") {
939                         lex.next();
940                         arg.decoration = lex.getString();
941                 } else if (tok == "font") {
942                         arg.font = lyxRead(lex, arg.font);
943                 } else if (tok == "labelfont") {
944                         arg.labelfont = lyxRead(lex, arg.labelfont);
945                 } else {
946                         lex.printError("Unknown tag");
947                         error = true;
948                 }
949         }
950         if (arg.labelstring.empty())
951                 LYXERR0("Incomplete Argument definition!");
952         else if (itemarg)
953                 itemargs_[id] = arg;
954         else if (postcmd)
955                 postcommandargs_[id] = arg;
956         else
957                 latexargs_[id] = arg;
958 }
959
960
961 Layout::LaTeXArgMap Layout::args() const
962 {
963         LaTeXArgMap args = latexargs_;
964         if (!postcommandargs_.empty())
965                 args.insert(postcommandargs_.begin(), postcommandargs_.end());
966         if (!itemargs_.empty())
967                 args.insert(itemargs_.begin(), itemargs_.end());
968         return args;
969 }
970
971
972 int Layout::optArgs() const
973 {
974         int nr = 0;
975         LaTeXArgMap::const_iterator it = latexargs_.begin();
976         for (; it != latexargs_.end(); ++it) {
977                 if (!(*it).second.mandatory)
978                         ++nr;
979         }
980         LaTeXArgMap::const_iterator iit = postcommandargs_.begin();
981         for (; iit != postcommandargs_.end(); ++iit) {
982                 if (!(*iit).second.mandatory)
983                         ++nr;
984         }
985         return nr;
986 }
987
988
989 int Layout::requiredArgs() const
990 {
991         int nr = 0;
992         LaTeXArgMap::const_iterator it = latexargs_.begin();
993         for (; it != latexargs_.end(); ++it) {
994                 if ((*it).second.mandatory)
995                         ++nr;
996         }
997         LaTeXArgMap::const_iterator iit = postcommandargs_.begin();
998         for (; iit != postcommandargs_.end(); ++iit) {
999                 if (!(*iit).second.mandatory)
1000                         ++nr;
1001         }
1002         return nr;
1003 }
1004
1005
1006 string const & Layout::htmltag() const 
1007
1008         if (htmltag_.empty())
1009                 htmltag_ =  "div";
1010         return htmltag_;
1011 }
1012
1013
1014 string const & Layout::htmlattr() const 
1015
1016         if (htmlattr_.empty())
1017                 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
1018         return htmlattr_; 
1019 }
1020
1021
1022 string const & Layout::htmlitemtag() const 
1023
1024         if (htmlitemtag_.empty())
1025                 htmlitemtag_ = "div";
1026         return htmlitemtag_; 
1027 }
1028
1029
1030 string const & Layout::htmlitemattr() const 
1031
1032         if (htmlitemattr_.empty())
1033                 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
1034         return htmlitemattr_; 
1035 }
1036
1037
1038 string const & Layout::htmllabeltag() const 
1039
1040         if (htmllabeltag_.empty()) {
1041                 if (labeltype != LABEL_ABOVE &&
1042                     labeltype != LABEL_CENTERED)
1043                         htmllabeltag_ = "span";
1044                 else
1045                         htmllabeltag_ = "div";
1046         }
1047         return htmllabeltag_; 
1048 }
1049
1050
1051 string const & Layout::htmllabelattr() const 
1052
1053         if (htmllabelattr_.empty())
1054                 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
1055         return htmllabelattr_; 
1056 }
1057
1058
1059 docstring Layout::htmlstyle() const
1060 {
1061         if (!htmlstyle_.empty() && !htmlforcecss_)
1062                 return htmlstyle_;
1063         if (htmldefaultstyle_.empty()) 
1064                 makeDefaultCSS();
1065         docstring retval = htmldefaultstyle_;
1066         if (!htmlstyle_.empty())
1067                 retval += '\n' + htmlstyle_;
1068         return retval;
1069 }
1070
1071
1072 string Layout::defaultCSSClass() const
1073
1074         if (!defaultcssclass_.empty())
1075                 return defaultcssclass_;
1076         docstring d;
1077         docstring::const_iterator it = name().begin();
1078         docstring::const_iterator en = name().end();
1079         for (; it != en; ++it) {
1080                 char_type const c = *it;
1081                 if (!isAlphaASCII(c)) {
1082                         if (d.empty())
1083                                 // make sure we don't start with an underscore,
1084                                 // as that sometimes causes problems.
1085                                 d = from_ascii("lyx_");
1086                         else
1087                                 d += '_';
1088                 } else if (isLower(c))
1089                         d += c;
1090                 else
1091                         // this is slow, so do it only if necessary
1092                         d += lowercase(c);
1093         }
1094         defaultcssclass_ = to_utf8(d);
1095         return defaultcssclass_;
1096 }
1097
1098
1099 namespace {
1100
1101 string makeMarginValue(char const * side, double d)
1102 {
1103         ostringstream os;
1104         os << "margin-" << side << ": " << d << "ex;\n";
1105         return os.str();
1106 }
1107
1108 }
1109
1110
1111 void Layout::makeDefaultCSS() const
1112 {
1113         // this never needs to be redone, since reloading layouts will
1114         // wipe out what we did before.
1115         if (!htmldefaultstyle_.empty()) 
1116                 return;
1117         
1118         // main font
1119         htmldefaultstyle_ = font.asCSS();
1120         
1121         // bottom margins
1122         string tmp;
1123         if (topsep > 0)
1124                 tmp += makeMarginValue("top", topsep);
1125         if (bottomsep > 0)
1126                 tmp += makeMarginValue("bottom", bottomsep);
1127         if (!leftmargin.empty()) {
1128                 // we can't really do what LyX does with the margin, so 
1129                 // we'll just figure out how many characters it is
1130                 int const len = leftmargin.length();
1131                 tmp += makeMarginValue("left", len);
1132         }
1133         if (!rightmargin.empty()) {
1134                 int const len = rightmargin.length();
1135                 tmp += makeMarginValue("right", len);
1136         }
1137                 
1138         if (!tmp.empty()) {
1139                 if (!htmldefaultstyle_.empty())
1140                         htmldefaultstyle_ += from_ascii("\n");
1141                 htmldefaultstyle_ += from_ascii(tmp);
1142         }
1143
1144 // tex2lyx does not see output_xhtml.cpp
1145 #ifndef TEX2LYX
1146         // alignment
1147         string where = alignmentToCSS(align);
1148         if (!where.empty()) {
1149                 htmldefaultstyle_ += from_ascii("text-align: " + where + ";\n");
1150         }
1151 #endif
1152         
1153         // wrap up what we have, if anything
1154         if (!htmldefaultstyle_.empty())
1155                 htmldefaultstyle_ = 
1156                         from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
1157                         htmldefaultstyle_ + from_ascii("\n}\n");
1158         
1159         if (labeltype == LABEL_NO_LABEL || htmllabeltag() == "NONE")
1160                 return;
1161         
1162         docstring labelCSS;
1163         
1164         // label font
1165         if (labelfont != font)
1166                 labelCSS = labelfont.asCSS() + from_ascii("\n");
1167         if (labeltype == LABEL_CENTERED)
1168                 labelCSS += from_ascii("text-align: center;\n");
1169         
1170         if (!labelCSS.empty())
1171                 htmldefaultstyle_ +=
1172                         from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1173                         labelCSS + from_ascii("\n}\n");
1174 }
1175
1176
1177 bool Layout::operator==(Layout const & rhs) const
1178 {
1179         // This is enough for the applications we actually make,
1180         // at least at the moment. But we could check more.
1181         return name() == rhs.name()
1182                 && latexname() == rhs.latexname()
1183                 && latextype == rhs.latextype;
1184 }
1185
1186
1187 } // namespace lyx