]> git.lyx.org Git - lyx.git/blob - src/TextClass.cpp
remove unused macro
[lyx.git] / src / TextClass.cpp
1 /**
2  * \file TextClass.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 Angus Leeming
9  * \author John Levon
10  * \author André Pönitz
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "TextClass.h"
18
19 #include "Color.h"
20 #include "Counters.h"
21 #include "support/debug.h"
22 #include "support/gettext.h"
23 #include "Floating.h"
24 #include "FloatList.h"
25 #include "Layout.h"
26 #include "Lexer.h"
27 #include "Font.h"
28
29 #include "frontends/alert.h"
30
31 #include "support/ExceptionMessage.h"
32 #include "support/lstrings.h"
33 #include "support/FileName.h"
34 #include "support/filetools.h"
35 #include "support/os.h"
36
37 #include <sstream>
38
39 using namespace std;
40 using namespace lyx::support;
41
42 namespace lyx {
43
44 namespace {
45
46 class LayoutNamesEqual : public unary_function<LayoutPtr, bool> {
47 public:
48         LayoutNamesEqual(docstring const & name)
49                 : name_(name)
50         {}
51         bool operator()(LayoutPtr const & c) const
52         {
53                 return c->name() == name_;
54         }
55 private:
56         docstring name_;
57 };
58
59
60 int const FORMAT = 6;
61
62
63 bool layout2layout(FileName const & filename, FileName const & tempfile)
64 {
65         FileName const script = libFileSearch("scripts", "layout2layout.py");
66         if (script.empty()) {
67                 lyxerr << "Could not find layout conversion "
68                           "script layout2layout.py." << endl;
69                 return false;
70         }
71
72         ostringstream command;
73         command << os::python() << ' ' << quoteName(script.toFilesystemEncoding())
74                 << ' ' << quoteName(filename.toFilesystemEncoding())
75                 << ' ' << quoteName(tempfile.toFilesystemEncoding());
76         string const command_str = command.str();
77
78         LYXERR(Debug::TCLASS, "Running `" << command_str << '\'');
79
80         cmd_ret const ret =
81                 runCommand(command_str);
82         if (ret.first != 0) {
83                 lyxerr << "Could not run layout conversion "
84                           "script layout2layout.py." << endl;
85                 return false;
86         }
87         return true;
88 }
89
90
91 std::string translateRT(TextClass::ReadType rt) 
92 {
93         switch (rt) {
94         case TextClass::BASECLASS:
95                 return "textclass";
96         case TextClass::MERGE:
97                 return "input file";
98         case TextClass::MODULE:
99                 return "module file";
100         }
101         // shutup warning
102         return string();
103 }
104
105 } // namespace anon
106
107
108 TextClass::TextClass(string const & fn, string const & cln,
109                            string const & desc, bool texClassAvail )
110         : name_(fn), latexname_(cln), description_(desc),
111           floatlist_(new FloatList), counters_(new Counters),
112           texClassAvail_(texClassAvail)
113 {
114         modular_ = false;
115         outputType_ = LATEX;
116         columns_ = 1;
117         sides_ = OneSide;
118         secnumdepth_ = 3;
119         tocdepth_ = 3;
120         pagestyle_ = "default";
121         defaultfont_ = sane_font;
122         opt_fontsize_ = "10|11|12";
123         opt_pagestyle_ = "empty|plain|headings|fancy";
124         titletype_ = TITLE_COMMAND_AFTER;
125         titlename_ = "maketitle";
126         loaded_ = false;
127 }
128
129
130 bool TextClass::isTeXClassAvailable() const
131 {
132         return texClassAvail_;
133 }
134
135
136 bool TextClass::readStyle(Lexer & lexrc, Layout & lay)
137 {
138         LYXERR(Debug::TCLASS, "Reading style " << to_utf8(lay.name()));
139         if (!lay.read(lexrc, *this)) {
140                 // Resolve fonts
141                 lay.resfont = lay.font;
142                 lay.resfont.realize(defaultfont());
143                 lay.reslabelfont = lay.labelfont;
144                 lay.reslabelfont.realize(defaultfont());
145                 return false; // no errors
146         }
147         lyxerr << "Error parsing style `" << to_utf8(lay.name()) << '\'' << endl;
148         return true;
149 }
150
151
152 enum TextClassTags {
153         TC_OUTPUTTYPE = 1,
154         TC_INPUT,
155         TC_STYLE,
156         TC_DEFAULTSTYLE,
157         TC_INSETLAYOUT,
158         TC_ENVIRONMENT,
159         TC_NOSTYLE,
160         TC_COLUMNS,
161         TC_SIDES,
162         TC_PAGESTYLE,
163         TC_DEFAULTFONT,
164         TC_SECNUMDEPTH,
165         TC_TOCDEPTH,
166         TC_CLASSOPTIONS,
167         TC_PREAMBLE,
168         TC_PROVIDES,
169         TC_REQUIRES,
170         TC_LEFTMARGIN,
171         TC_RIGHTMARGIN,
172         TC_FLOAT,
173         TC_COUNTER,
174         TC_NOFLOAT,
175         TC_TITLELATEXNAME,
176         TC_TITLELATEXTYPE,
177         TC_FORMAT
178 };
179
180
181 // Reads a textclass structure from file.
182 bool TextClass::read(FileName const & filename, ReadType rt)
183 {
184         if (!filename.isReadableFile()) {
185                 lyxerr << "Cannot read layout file `" << filename << "'."
186                        << endl;
187                 return true;
188         }
189
190         keyword_item textClassTags[] = {
191                 { "classoptions",    TC_CLASSOPTIONS },
192                 { "columns",         TC_COLUMNS },
193                 { "counter",         TC_COUNTER },
194                 { "defaultfont",     TC_DEFAULTFONT },
195                 { "defaultstyle",    TC_DEFAULTSTYLE },
196                 { "environment",     TC_ENVIRONMENT },
197                 { "float",           TC_FLOAT },
198                 { "format",          TC_FORMAT },
199                 { "input",           TC_INPUT },
200                 { "insetlayout",     TC_INSETLAYOUT },
201                 { "leftmargin",      TC_LEFTMARGIN },
202                 { "nofloat",         TC_NOFLOAT },
203                 { "nostyle",         TC_NOSTYLE },
204                 { "outputtype",      TC_OUTPUTTYPE },
205                 { "pagestyle",       TC_PAGESTYLE },
206                 { "preamble",        TC_PREAMBLE },
207                 { "provides",        TC_PROVIDES },
208                 { "requires",        TC_REQUIRES },
209                 { "rightmargin",     TC_RIGHTMARGIN },
210                 { "secnumdepth",     TC_SECNUMDEPTH },
211                 { "sides",           TC_SIDES },
212                 { "style",           TC_STYLE },
213                 { "titlelatexname",  TC_TITLELATEXNAME },
214                 { "titlelatextype",  TC_TITLELATEXTYPE },
215                 { "tocdepth",        TC_TOCDEPTH }
216         };
217
218         LYXERR(Debug::TCLASS, "Reading " + translateRT(rt) + ": " +
219                 to_utf8(makeDisplayPath(filename.absFilename())));
220
221         Lexer lexrc(textClassTags,
222                 sizeof(textClassTags) / sizeof(textClassTags[0]));
223
224         lexrc.setFile(filename);
225         bool error = !lexrc.isOK();
226
227         // Format of files before the 'Format' tag was introduced
228         int format = 1;
229
230         // parsing
231         while (lexrc.isOK() && !error) {
232                 int le = lexrc.lex();
233
234                 switch (le) {
235                 case Lexer::LEX_FEOF:
236                         continue;
237
238                 case Lexer::LEX_UNDEF:
239                         lexrc.printError("Unknown TextClass tag `$$Token'");
240                         error = true;
241                         continue;
242
243                 default:
244                         break;
245                 }
246
247                 switch (static_cast<TextClassTags>(le)) {
248
249                 case TC_FORMAT:
250                         if (lexrc.next())
251                                 format = lexrc.getInteger();
252                         break;
253
254                 case TC_OUTPUTTYPE:   // output type definition
255                         readOutputType(lexrc);
256                         break;
257
258                 case TC_INPUT: // Include file
259                         if (lexrc.next()) {
260                                 string const inc = lexrc.getString();
261                                 FileName tmp = libFileSearch("layouts", inc,
262                                                             "layout");
263
264                                 if (tmp.empty()) {
265                                         lexrc.printError("Could not find input file: " + inc);
266                                         error = true;
267                                 } else if (read(tmp, MERGE)) {
268                                         lexrc.printError("Error reading input"
269                                                          "file: " + tmp.absFilename());
270                                         error = true;
271                                 }
272                         }
273                         break;
274
275                 case TC_DEFAULTSTYLE:
276                         if (lexrc.next()) {
277                                 docstring const name = from_utf8(subst(lexrc.getString(),
278                                                           '_', ' '));
279                                 defaultlayout_ = name;
280                         }
281                         break;
282
283                 case TC_ENVIRONMENT:
284                 case TC_STYLE:
285                         if (lexrc.next()) {
286                                 docstring const name = from_utf8(subst(lexrc.getString(),
287                                                     '_', ' '));
288                                 if (name.empty()) {
289                                         string s = "Could not read name for style: `$$Token' "
290                                                 + lexrc.getString() + " is probably not valid UTF-8!";
291                                         lexrc.printError(s.c_str());
292                                         Layout lay;
293                                         error = readStyle(lexrc, lay);
294                                 } else if (hasLayout(name)) {
295                                         Layout * lay = operator[](name).get();
296                                         error = readStyle(lexrc, *lay);
297                                 } else {
298                                         Layout lay;
299                                         lay.setName(name);
300                                         if (le == TC_ENVIRONMENT)
301                                                 lay.is_environment = true;
302                                         error = readStyle(lexrc, lay);
303                                         if (!error)
304                                                 layoutlist_.push_back(
305                                                         boost::shared_ptr<Layout>(new Layout(lay))
306                                                         );
307
308                                         if (defaultlayout_.empty()) {
309                                                 // We do not have a default
310                                                 // layout yet, so we choose
311                                                 // the first layout we
312                                                 // encounter.
313                                                 defaultlayout_ = name;
314                                         }
315                                 }
316                         }
317                         else {
318                                 lexrc.printError("No name given for style: `$$Token'.");
319                                 error = true;
320                         }
321                         break;
322
323                 case TC_NOSTYLE:
324                         if (lexrc.next()) {
325                                 docstring const style = from_utf8(subst(lexrc.getString(),
326                                                      '_', ' '));
327                                 if (!deleteLayout(style))
328                                         lyxerr << "Cannot delete style `"
329                                                << to_utf8(style) << '\'' << endl;
330 //                                      lexrc.printError("Cannot delete style"
331 //                                                       " `$$Token'");
332                         }
333                         break;
334
335                 case TC_COLUMNS:
336                         if (lexrc.next())
337                                 columns_ = lexrc.getInteger();
338                         break;
339
340                 case TC_SIDES:
341                         if (lexrc.next()) {
342                                 switch (lexrc.getInteger()) {
343                                 case 1: sides_ = OneSide; break;
344                                 case 2: sides_ = TwoSides; break;
345                                 default:
346                                         lyxerr << "Impossible number of page"
347                                                 " sides, setting to one."
348                                                << endl;
349                                         sides_ = OneSide;
350                                         break;
351                                 }
352                         }
353                         break;
354
355                 case TC_PAGESTYLE:
356                         lexrc.next();
357                         pagestyle_ = rtrim(lexrc.getString());
358                         break;
359
360                 case TC_DEFAULTFONT:
361                         defaultfont_ = lyxRead(lexrc);
362                         if (!defaultfont_.resolved()) {
363                                 lexrc.printError("Warning: defaultfont should "
364                                                  "be fully instantiated!");
365                                 defaultfont_.realize(sane_font);
366                         }
367                         break;
368
369                 case TC_SECNUMDEPTH:
370                         lexrc.next();
371                         secnumdepth_ = lexrc.getInteger();
372                         break;
373
374                 case TC_TOCDEPTH:
375                         lexrc.next();
376                         tocdepth_ = lexrc.getInteger();
377                         break;
378
379                         // First step to support options
380                 case TC_CLASSOPTIONS:
381                         readClassOptions(lexrc);
382                         break;
383
384                 case TC_PREAMBLE:
385                         preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
386                         break;
387
388                 case TC_PROVIDES: {
389                         lexrc.next();
390                         string const feature = lexrc.getString();
391                         lexrc.next();
392                         if (lexrc.getInteger())
393                                 provides_.insert(feature);
394                         else
395                                 provides_.erase(feature);
396                         break;
397                 }
398
399                 case TC_REQUIRES: {
400                         lexrc.eatLine();
401                         vector<string> const req 
402                                 = getVectorFromString(lexrc.getString());
403                         requires_.insert(req.begin(), req.end());
404                         break;
405                 }
406
407                 case TC_LEFTMARGIN:     // left margin type
408                         if (lexrc.next())
409                                 leftmargin_ = lexrc.getDocString();
410                         break;
411
412                 case TC_RIGHTMARGIN:    // right margin type
413                         if (lexrc.next())
414                                 rightmargin_ = lexrc.getDocString();
415                         break;
416                 case TC_INSETLAYOUT:
417                         if (lexrc.next()) {
418                                 docstring const name = subst(lexrc.getDocString(), '_', ' ');
419                                 readInsetLayout(lexrc, name);
420                         }
421                         break;
422                 case TC_FLOAT:
423                         readFloat(lexrc);
424                         break;
425                 case TC_COUNTER:
426                         readCounter(lexrc);
427                         break;
428                 case TC_TITLELATEXTYPE:
429                         readTitleType(lexrc);
430                         break;
431                 case TC_TITLELATEXNAME:
432                         if (lexrc.next())
433                                 titlename_ = lexrc.getString();
434                         break;
435                 case TC_NOFLOAT:
436                         if (lexrc.next()) {
437                                 string const nofloat = lexrc.getString();
438                                 floatlist_->erase(nofloat);
439                         }
440                         break;
441                 }
442                 if (format != FORMAT)
443                         break;
444         }
445
446         if (format != FORMAT) {
447                 LYXERR(Debug::TCLASS, "Converting layout file from format "
448                                       << format << " to " << FORMAT);
449                 FileName const tempfile = FileName::tempName();
450                 error = !layout2layout(filename, tempfile);
451                 if (!error)
452                         error = read(tempfile, rt);
453                 tempfile.removeFile();
454                 return error;
455         }
456
457         LYXERR(Debug::TCLASS, "Finished reading " + translateRT(rt) + ": " +
458                         to_utf8(makeDisplayPath(filename.absFilename())));
459
460         if (rt != BASECLASS) 
461                 return error;
462
463         if (defaultlayout_.empty()) {
464                 lyxerr << "Error: Textclass '" << name_
465                                                 << "' is missing a defaultstyle." << endl;
466                 error = true;
467         }
468                 
469         //Try to erase "stdinsets" from the provides_ set. 
470         //The
471         //  Provides stdinsets 1
472         //declaration simply tells us that the standard insets have been
473         //defined. (It's found in stdinsets.inc but could also be used in
474         //user-defined files.) There isn't really any such package. So we
475         //might as well go ahead and erase it.
476         //If we do not succeed, then it was not there, which means that
477         //the textclass did not provide the definitions of the standard
478         //insets. So we need to try to load them.
479         int erased = provides_.erase("stdinsets");
480         if (!erased) {
481                 FileName tmp = libFileSearch("layouts", "stdinsets.inc");
482
483                 if (tmp.empty()) {
484                         throw ExceptionMessage(WarningException, _("Missing File"),
485                                 _("Could not find stdinsets.inc! This may lead to data loss!"));
486                         error = true;
487                 } else if (read(tmp, MERGE)) {
488                         throw ExceptionMessage(WarningException, _("Corrupt File"),
489                                         _("Could not read stdinsets.inc! This may lead to data loss!"));
490                         error = true;
491                 }
492         }
493
494         min_toclevel_ = Layout::NOT_IN_TOC;
495         max_toclevel_ = Layout::NOT_IN_TOC;
496         const_iterator cit = begin();
497         const_iterator the_end = end();
498         for ( ; cit != the_end ; ++cit) {
499                 int const toclevel = (*cit)->toclevel;
500                 if (toclevel != Layout::NOT_IN_TOC) {
501                         if (min_toclevel_ == Layout::NOT_IN_TOC)
502                                 min_toclevel_ = toclevel;
503                         else
504                                 min_toclevel_ = min(min_toclevel_,
505                                                         toclevel);
506                         max_toclevel_ = max(max_toclevel_,
507                                                         toclevel);
508                 }
509         }
510         LYXERR(Debug::TCLASS, "Minimum TocLevel is " << min_toclevel_
511                 << ", maximum is " << max_toclevel_);
512
513         return error;
514 }
515
516
517 void TextClass::readTitleType(Lexer & lexrc)
518 {
519         keyword_item titleTypeTags[] = {
520                 { "commandafter", TITLE_COMMAND_AFTER },
521                 { "environment", TITLE_ENVIRONMENT }
522         };
523
524         PushPopHelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
525
526         int le = lexrc.lex();
527         switch (le) {
528         case Lexer::LEX_UNDEF:
529                 lexrc.printError("Unknown output type `$$Token'");
530                 return;
531         case TITLE_COMMAND_AFTER:
532         case TITLE_ENVIRONMENT:
533                 titletype_ = static_cast<TitleLatexType>(le);
534                 break;
535         default:
536                 lyxerr << "Unhandled value " << le
537                        << " in TextClass::readTitleType." << endl;
538
539                 break;
540         }
541 }
542
543
544 void TextClass::readOutputType(Lexer & lexrc)
545 {
546         keyword_item outputTypeTags[] = {
547                 { "docbook", DOCBOOK },
548                 { "latex", LATEX },
549                 { "literate", LITERATE }
550         };
551
552         PushPopHelper pph(lexrc, outputTypeTags, LITERATE);
553
554         int le = lexrc.lex();
555         switch (le) {
556         case Lexer::LEX_UNDEF:
557                 lexrc.printError("Unknown output type `$$Token'");
558                 return;
559         case LATEX:
560         case DOCBOOK:
561         case LITERATE:
562                 outputType_ = static_cast<OutputType>(le);
563                 break;
564         default:
565                 lyxerr << "Unhandled value " << le
566                        << " in TextClass::readOutputType." << endl;
567
568                 break;
569         }
570 }
571
572
573 enum ClassOptionsTags {
574         CO_FONTSIZE = 1,
575         CO_PAGESTYLE,
576         CO_OTHER,
577         CO_HEADER,
578         CO_END
579 };
580
581
582 void TextClass::readClassOptions(Lexer & lexrc)
583 {
584         keyword_item classOptionsTags[] = {
585                 {"end", CO_END },
586                 {"fontsize", CO_FONTSIZE },
587                 {"header", CO_HEADER },
588                 {"other", CO_OTHER },
589                 {"pagestyle", CO_PAGESTYLE }
590         };
591
592         lexrc.pushTable(classOptionsTags, CO_END);
593         bool getout = false;
594         while (!getout && lexrc.isOK()) {
595                 int le = lexrc.lex();
596                 switch (le) {
597                 case Lexer::LEX_UNDEF:
598                         lexrc.printError("Unknown ClassOption tag `$$Token'");
599                         continue;
600                 default: break;
601                 }
602                 switch (static_cast<ClassOptionsTags>(le)) {
603                 case CO_FONTSIZE:
604                         lexrc.next();
605                         opt_fontsize_ = rtrim(lexrc.getString());
606                         break;
607                 case CO_PAGESTYLE:
608                         lexrc.next();
609                         opt_pagestyle_ = rtrim(lexrc.getString());
610                         break;
611                 case CO_OTHER:
612                         lexrc.next();
613                         options_ = lexrc.getString();
614                         break;
615                 case CO_HEADER:
616                         lexrc.next();
617                         class_header_ = subst(lexrc.getString(), "&quot;", "\"");
618                         break;
619                 case CO_END:
620                         getout = true;
621                         break;
622                 }
623         }
624         lexrc.popTable();
625 }
626
627
628 enum InsetLayoutTags {
629         IL_FONT = 1,
630         IL_BGCOLOR,
631         IL_DECORATION,
632         IL_FREESPACING,
633         IL_FORCELTR,
634         IL_LABELFONT,
635         IL_LABELSTRING,
636         IL_LATEXNAME,
637         IL_LATEXPARAM,
638         IL_LATEXTYPE,
639         IL_LYXTYPE,
640         IL_KEEPEMPTY,
641         IL_MULTIPAR,
642         IL_NEEDPROTECT,
643         IL_PASSTHRU,
644         IL_PREAMBLE,
645         IL_REQUIRES,
646         IL_END
647 };
648
649
650 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
651 {
652         keyword_item elementTags[] = {
653                 { "bgcolor", IL_BGCOLOR },
654                 { "decoration", IL_DECORATION },
655                 { "end", IL_END },
656                 { "font", IL_FONT },
657                 { "forceltr", IL_FORCELTR },
658                 { "freespacing", IL_FREESPACING },
659                 { "keepempty", IL_KEEPEMPTY },
660                 { "labelfont", IL_LABELFONT },
661                 { "labelstring", IL_LABELSTRING },
662                 { "latexname", IL_LATEXNAME },
663                 { "latexparam", IL_LATEXPARAM },
664                 { "latextype", IL_LATEXTYPE },
665                 { "lyxtype", IL_LYXTYPE },
666                 { "multipar", IL_MULTIPAR },
667                 { "needprotect", IL_NEEDPROTECT },
668                 { "passthru", IL_PASSTHRU },
669                 { "preamble", IL_PREAMBLE },
670                 { "requires", IL_REQUIRES }
671         };
672
673         lexrc.pushTable(elementTags, IL_END);
674
675         string lyxtype;
676         docstring labelstring;
677         string latextype;
678         string decoration;
679         string latexname;
680         string latexparam;
681         FontInfo font = inherit_font;
682         FontInfo labelfont = inherit_font;
683         ColorCode bgcolor(Color_background);
684         string preamble;
685         set<string> requires;
686         bool multipar = false;
687         bool passthru = false;
688         bool needprotect = false;
689         bool keepempty = false;
690         bool freespacing = false;
691         bool forceltr = false;
692
693         bool getout = false;
694         while (!getout && lexrc.isOK()) {
695                 int le = lexrc.lex();
696                 switch (le) {
697                 case Lexer::LEX_UNDEF:
698                         lexrc.printError("Unknown InsetLayout tag `$$Token'");
699                         continue;
700                 default: break;
701                 }
702                 switch (static_cast<InsetLayoutTags>(le)) {
703                 case IL_LYXTYPE:
704                         lexrc.next();
705                         lyxtype = lexrc.getString();
706                         break;
707                 case IL_LATEXTYPE:
708                         lexrc.next();
709                         latextype = lexrc.getString();
710                         break;
711                 case IL_LABELSTRING:
712                         lexrc.next();
713                         labelstring = lexrc.getDocString();
714                         break;
715                 case IL_DECORATION:
716                         lexrc.next();
717                         decoration = lexrc.getString();
718                         break;
719                 case IL_LATEXNAME:
720                         lexrc.next();
721                         latexname = lexrc.getString();
722                         break;
723                 case IL_LATEXPARAM:
724                         lexrc.next();
725                         latexparam = subst(lexrc.getString(), "&quot;", "\"");
726                         break;
727                 case IL_LABELFONT:
728                         labelfont = lyxRead(lexrc, inherit_font);
729                         break;
730                 case IL_FORCELTR:
731                         lexrc.next();
732                         forceltr = lexrc.getBool();
733                         break;
734                 case IL_MULTIPAR:
735                         lexrc.next();
736                         multipar = lexrc.getBool();
737                         break;
738                 case IL_PASSTHRU:
739                         lexrc.next();
740                         passthru = lexrc.getBool();
741                         break;
742                 case IL_KEEPEMPTY:
743                         lexrc.next();
744                         keepempty = lexrc.getBool();
745                         break;
746                 case IL_FREESPACING:
747                         lexrc.next();
748                         freespacing = lexrc.getBool();
749                         break;
750                 case IL_NEEDPROTECT:
751                         lexrc.next();
752                         needprotect = lexrc.getBool();
753                         break;
754                 case IL_FONT:
755                         font = lyxRead(lexrc, inherit_font);
756                         // So: define font before labelfont
757                         labelfont = font;
758                         break;
759                 case IL_BGCOLOR: {
760                         lexrc.next();
761                         string const token = lexrc.getString();
762                         bgcolor = lcolor.getFromLyXName(token);
763                         break;
764                 }
765                 case IL_PREAMBLE:
766                         preamble = lexrc.getLongString("EndPreamble");
767                         break;
768                 case IL_REQUIRES: {
769                         lexrc.eatLine();
770                         vector<string> const req 
771                                 = getVectorFromString(lexrc.getString());
772                         requires.insert(req.begin(), req.end());
773                         break;
774                 }
775                 case IL_END:
776                         getout = true;
777                         break;
778                 }
779         }
780
781         // Here add element to list if getout == true
782         if (getout) {
783                 InsetLayout il;
784                 il.name = to_ascii(name);
785                 il.lyxtype = lyxtype;
786                 il.labelstring = labelstring;
787                 il.decoration = decoration;
788                 il.latextype = latextype;
789                 il.latexname = latexname;
790                 il.latexparam = latexparam;
791                 il.multipar = multipar;
792                 il.passthru = passthru;
793                 il.needprotect = needprotect;
794                 il.freespacing = freespacing;
795                 il.forceltr = forceltr;
796                 il.keepempty = keepempty;
797                 il.font = font;
798                 // The label font is generally used as-is without
799                 // any realization against a given context.
800                 labelfont.realize(sane_font);
801                 il.labelfont = labelfont;
802                 il.bgcolor = bgcolor;
803                 il.preamble = preamble;
804                 il.requires = requires;
805                 insetlayoutlist_[name] = il;
806         }
807
808         lexrc.popTable();
809 }
810
811
812
813 enum FloatTags {
814         FT_TYPE = 1,
815         FT_NAME,
816         FT_PLACEMENT,
817         FT_EXT,
818         FT_WITHIN,
819         FT_STYLE,
820         FT_LISTNAME,
821         FT_BUILTIN,
822         FT_END
823 };
824
825
826 void TextClass::readFloat(Lexer & lexrc)
827 {
828         keyword_item floatTags[] = {
829                 { "end", FT_END },
830                 { "extension", FT_EXT },
831                 { "guiname", FT_NAME },
832                 { "latexbuiltin", FT_BUILTIN },
833                 { "listname", FT_LISTNAME },
834                 { "numberwithin", FT_WITHIN },
835                 { "placement", FT_PLACEMENT },
836                 { "style", FT_STYLE },
837                 { "type", FT_TYPE }
838         };
839
840         lexrc.pushTable(floatTags, FT_END);
841
842         string type;
843         string placement;
844         string ext;
845         string within;
846         string style;
847         string name;
848         string listName;
849         bool builtin = false;
850
851         bool getout = false;
852         while (!getout && lexrc.isOK()) {
853                 int le = lexrc.lex();
854                 switch (le) {
855                 case Lexer::LEX_UNDEF:
856                         lexrc.printError("Unknown float tag `$$Token'");
857                         continue;
858                 default: break;
859                 }
860                 switch (static_cast<FloatTags>(le)) {
861                 case FT_TYPE:
862                         lexrc.next();
863                         type = lexrc.getString();
864                         if (floatlist_->typeExist(type)) {
865                                 Floating const & fl = floatlist_->getType(type);
866                                 placement = fl.placement();
867                                 ext = fl.ext();
868                                 within = fl.within();
869                                 style = fl.style();
870                                 name = fl.name();
871                                 listName = fl.listName();
872                                 builtin = fl.builtin();
873                         } 
874                         break;
875                 case FT_NAME:
876                         lexrc.next();
877                         name = lexrc.getString();
878                         break;
879                 case FT_PLACEMENT:
880                         lexrc.next();
881                         placement = lexrc.getString();
882                         break;
883                 case FT_EXT:
884                         lexrc.next();
885                         ext = lexrc.getString();
886                         break;
887                 case FT_WITHIN:
888                         lexrc.next();
889                         within = lexrc.getString();
890                         if (within == "none")
891                                 within.erase();
892                         break;
893                 case FT_STYLE:
894                         lexrc.next();
895                         style = lexrc.getString();
896                         break;
897                 case FT_LISTNAME:
898                         lexrc.next();
899                         listName = lexrc.getString();
900                         break;
901                 case FT_BUILTIN:
902                         lexrc.next();
903                         builtin = lexrc.getBool();
904                         break;
905                 case FT_END:
906                         getout = true;
907                         break;
908                 }
909         }
910
911         // Here if have a full float if getout == true
912         if (getout) {
913                 Floating fl(type, placement, ext, within,
914                             style, name, listName, builtin);
915                 floatlist_->newFloat(fl);
916                 // each float has its own counter
917                 counters_->newCounter(from_ascii(type), from_ascii(within), 
918                                       docstring(), docstring());
919         }
920
921         lexrc.popTable();
922 }
923
924
925 enum CounterTags {
926         CT_NAME = 1,
927         CT_WITHIN,
928         CT_LABELSTRING,
929         CT_LABELSTRING_APPENDIX,
930         CT_END
931 };
932
933
934 void TextClass::readCounter(Lexer & lexrc)
935 {
936         keyword_item counterTags[] = {
937                 { "end", CT_END },
938                 { "labelstring", CT_LABELSTRING },
939                 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
940                 { "name", CT_NAME },
941                 { "within", CT_WITHIN }
942         };
943
944         lexrc.pushTable(counterTags, CT_END);
945
946         docstring name;
947         docstring within;
948         docstring labelstring;
949         docstring labelstring_appendix;
950
951         bool getout = false;
952         while (!getout && lexrc.isOK()) {
953                 int le = lexrc.lex();
954                 switch (le) {
955                 case Lexer::LEX_UNDEF:
956                         lexrc.printError("Unknown counter tag `$$Token'");
957                         continue;
958                 default: break;
959                 }
960                 switch (static_cast<CounterTags>(le)) {
961                 case CT_NAME:
962                         lexrc.next();
963                         name = lexrc.getDocString();
964                         if (counters_->hasCounter(name))
965                                 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
966                         else
967                                 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
968                         break;
969                 case CT_WITHIN:
970                         lexrc.next();
971                         within = lexrc.getDocString();
972                         if (within == "none")
973                                 within.erase();
974                         break;
975                 case CT_LABELSTRING:
976                         lexrc.next();
977                         labelstring = lexrc.getDocString();
978                         labelstring_appendix = labelstring;
979                         break;
980                 case CT_LABELSTRING_APPENDIX:
981                         lexrc.next();
982                         labelstring_appendix = lexrc.getDocString();
983                         break;
984                 case CT_END:
985                         getout = true;
986                         break;
987                 }
988         }
989
990         // Here if have a full counter if getout == true
991         if (getout)
992                 counters_->newCounter(name, within, 
993                                       labelstring, labelstring_appendix);
994
995         lexrc.popTable();
996 }
997
998
999 FontInfo const & TextClass::defaultfont() const
1000 {
1001         return defaultfont_;
1002 }
1003
1004
1005 docstring const & TextClass::leftmargin() const
1006 {
1007         return leftmargin_;
1008 }
1009
1010
1011 docstring const & TextClass::rightmargin() const
1012 {
1013         return rightmargin_;
1014 }
1015
1016
1017 bool TextClass::hasLayout(docstring const & n) const
1018 {
1019         docstring const name = n.empty() ? defaultLayoutName() : n;
1020
1021         return find_if(layoutlist_.begin(), layoutlist_.end(),
1022                        LayoutNamesEqual(name))
1023                 != layoutlist_.end();
1024 }
1025
1026
1027
1028 LayoutPtr const & TextClass::operator[](docstring const & name) const
1029 {
1030         BOOST_ASSERT(!name.empty());
1031
1032         LayoutList::const_iterator cit =
1033                 find_if(layoutlist_.begin(),
1034                         layoutlist_.end(),
1035                         LayoutNamesEqual(name));
1036
1037         if (cit == layoutlist_.end()) {
1038                 lyxerr << "We failed to find the layout '" << to_utf8(name)
1039                        << "' in the layout list. You MUST investigate!"
1040                        << endl;
1041                 for (LayoutList::const_iterator it = layoutlist_.begin();
1042                          it != layoutlist_.end(); ++it)
1043                         lyxerr  << " " << to_utf8(it->get()->name()) << endl;
1044
1045                 // we require the name to exist
1046                 BOOST_ASSERT(false);
1047         }
1048
1049         return *cit;
1050 }
1051
1052
1053 bool TextClass::deleteLayout(docstring const & name)
1054 {
1055         if (name == defaultLayoutName())
1056                 return false;
1057
1058         LayoutList::iterator it =
1059                 remove_if(layoutlist_.begin(), layoutlist_.end(),
1060                           LayoutNamesEqual(name));
1061
1062         LayoutList::iterator end = layoutlist_.end();
1063         bool const ret = (it != end);
1064         layoutlist_.erase(it, end);
1065         return ret;
1066 }
1067
1068
1069 // Load textclass info if not loaded yet
1070 bool TextClass::load(string const & path) const
1071 {
1072         if (loaded_)
1073                 return true;
1074
1075         // Read style-file, provided path is searched before the system ones
1076         FileName layout_file;
1077         if (!path.empty())
1078                 layout_file = FileName(addName(path, name_ + ".layout"));
1079         if (layout_file.empty() || !layout_file.exists())
1080                 layout_file = libFileSearch("layouts", name_, "layout");
1081         loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1082
1083         if (!loaded_) {
1084                 lyxerr << "Error reading `"
1085                        << to_utf8(makeDisplayPath(layout_file.absFilename()))
1086                        << "'\n(Check `" << name_
1087                        << "')\nCheck your installation and "
1088                         "try Options/Reconfigure..." << endl;
1089         }
1090
1091         return loaded_;
1092 }
1093
1094
1095 FloatList & TextClass::floats()
1096 {
1097         return *floatlist_.get();
1098 }
1099
1100
1101 FloatList const & TextClass::floats() const
1102 {
1103         return *floatlist_.get();
1104 }
1105
1106
1107 Counters & TextClass::counters() const
1108 {
1109         return *counters_.get();
1110 }
1111
1112
1113 // Return the layout object of an inset given by name. If the name
1114 // is not found as such, the part after the ':' is stripped off, and
1115 // searched again. In this way, an error fallback can be provided:
1116 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1117 // will invoke the layout object defined by name = 'CharStyle'.
1118 // If that doesn't work either, an empty object returns (shouldn't
1119 // happen).  -- Idea JMarc, comment MV
1120 InsetLayout const & TextClass::insetlayout(docstring const & name) const 
1121 {
1122         docstring n = name;
1123         while (!n.empty()) {
1124                 if (insetlayoutlist_.count(n) > 0)
1125                         return insetlayoutlist_[n];
1126                 docstring::size_type i = n.find(':');
1127                 if (i == string::npos)
1128                         break;
1129                 n = n.substr(0,i);
1130         }
1131         static InsetLayout empty;
1132         empty.labelstring = from_utf8("UNDEFINED");
1133         empty.labelfont = sane_font;
1134         empty.labelfont.setColor(Color_error);
1135         empty.bgcolor = Color_error;
1136         return empty;
1137 }
1138
1139
1140 docstring const & TextClass::defaultLayoutName() const
1141 {
1142         // This really should come from the actual layout... (Lgb)
1143         return defaultlayout_;
1144 }
1145
1146
1147 LayoutPtr const & TextClass::defaultLayout() const
1148 {
1149         return operator[](defaultLayoutName());
1150 }
1151
1152
1153 string const & TextClass::name() const
1154 {
1155         return name_;
1156 }
1157
1158
1159 string const & TextClass::latexname() const
1160 {
1161         const_cast<TextClass*>(this)->load();
1162         return latexname_;
1163 }
1164
1165
1166 string const & TextClass::description() const
1167 {
1168         return description_;
1169 }
1170
1171
1172 string const & TextClass::opt_fontsize() const
1173 {
1174         return opt_fontsize_;
1175 }
1176
1177
1178 string const & TextClass::opt_pagestyle() const
1179 {
1180         return opt_pagestyle_;
1181 }
1182
1183
1184 string const & TextClass::options() const
1185 {
1186         return options_;
1187 }
1188
1189
1190 string const & TextClass::class_header() const
1191 {
1192         return class_header_;
1193 }
1194
1195
1196 string const & TextClass::pagestyle() const
1197 {
1198         return pagestyle_;
1199 }
1200
1201
1202 docstring const & TextClass::preamble() const
1203 {
1204         return preamble_;
1205 }
1206
1207
1208 PageSides TextClass::sides() const
1209 {
1210         return sides_;
1211 }
1212
1213
1214 int TextClass::secnumdepth() const
1215 {
1216         return secnumdepth_;
1217 }
1218
1219
1220 int TextClass::tocdepth() const
1221 {
1222         return tocdepth_;
1223 }
1224
1225
1226 OutputType TextClass::outputType() const
1227 {
1228         return outputType_;
1229 }
1230
1231
1232 bool TextClass::provides(string const & p) const
1233 {
1234         return provides_.find(p) != provides_.end();
1235 }
1236
1237
1238 unsigned int TextClass::columns() const
1239 {
1240         return columns_;
1241 }
1242
1243
1244 TitleLatexType TextClass::titletype() const
1245 {
1246         return titletype_;
1247 }
1248
1249
1250 string const & TextClass::titlename() const
1251 {
1252         return titlename_;
1253 }
1254
1255
1256 int TextClass::size() const
1257 {
1258         return layoutlist_.size();
1259 }
1260
1261
1262 int TextClass::min_toclevel() const
1263 {
1264         return min_toclevel_;
1265 }
1266
1267
1268 int TextClass::max_toclevel() const
1269 {
1270         return max_toclevel_;
1271 }
1272
1273
1274 bool TextClass::hasTocLevels() const
1275 {
1276         return min_toclevel_ != Layout::NOT_IN_TOC;
1277 }
1278
1279
1280 ostream & operator<<(ostream & os, PageSides p)
1281 {
1282         switch (p) {
1283         case OneSide:
1284                 os << '1';
1285                 break;
1286         case TwoSides:
1287                 os << '2';
1288                 break;
1289         }
1290         return os;
1291 }
1292
1293
1294 } // namespace lyx