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