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