]> git.lyx.org Git - lyx.git/blob - src/lyxtextclass.C
Fix bug 886 and others not reported related with the document paper size.
[lyx.git] / src / lyxtextclass.C
1 /**
2  * \file lyxtextclass.C
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 "lyxtextclass.h"
18 #include "debug.h"
19 #include "lyxlex.h"
20 #include "counters.h"
21 #include "Floating.h"
22 #include "FloatList.h"
23
24 #include "support/lstrings.h"
25 #include "support/filetools.h"
26
27 using lyx::support::LibFileSearch;
28 using lyx::support::MakeDisplayPath;
29 using lyx::support::rtrim;
30 using lyx::support::subst;
31
32 using std::endl;
33 using std::find_if;
34 using std::remove_if;
35 using std::string;
36 using std::ostream;
37
38
39 namespace {
40
41 class LayoutNamesEqual : public std::unary_function<LyXLayout_ptr, bool> {
42 public:
43         LayoutNamesEqual(string const & name)
44                 : name_(name)
45         {}
46         bool operator()(LyXLayout_ptr const & c) const
47         {
48                 return c->name() == name_;
49         }
50 private:
51         string name_;
52 };
53
54 } // namespace anon
55
56
57 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
58                            string const & desc, bool texClassAvail )
59         : name_(fn), latexname_(cln), description_(desc),
60           floatlist_(new FloatList), ctrs_(new Counters),
61           texClassAvail_(texClassAvail)
62 {
63         outputType_ = LATEX;
64         columns_ = 1;
65         sides_ = OneSide;
66         secnumdepth_ = 3;
67         tocdepth_ = 3;
68         pagestyle_ = "default";
69         defaultfont_ = LyXFont(LyXFont::ALL_SANE);
70         opt_fontsize_ = "10|11|12";
71         opt_pagestyle_ = "empty|plain|headings|fancy";
72         provides_ = nothing;
73         titletype_ = TITLE_COMMAND_AFTER;
74         titlename_ = "maketitle";
75         loaded_ = false;
76 }
77
78
79 bool LyXTextClass::isTeXClassAvailable() const
80 {
81         return texClassAvail_;
82 }
83
84
85 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
86 {
87         lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
88         if (!lay.Read(lexrc, *this)) {
89                 // Resolve fonts
90                 lay.resfont = lay.font;
91                 lay.resfont.realize(defaultfont());
92                 lay.reslabelfont = lay.labelfont;
93                 lay.reslabelfont.realize(defaultfont());
94                 return false; // no errors
95         }
96         lyxerr << "Error parsing style `" << lay.name() << '\'' << endl;
97         return true;
98 }
99
100
101 enum TextClassTags {
102         TC_OUTPUTTYPE = 1,
103         TC_INPUT,
104         TC_STYLE,
105         TC_DEFAULTSTYLE,
106         TC_CHARSTYLE,
107         TC_ENVIRONMENT,
108         TC_NOSTYLE,
109         TC_COLUMNS,
110         TC_SIDES,
111         TC_PAGESTYLE,
112         TC_DEFAULTFONT,
113         TC_SECNUMDEPTH,
114         TC_TOCDEPTH,
115         TC_CLASSOPTIONS,
116         TC_PREAMBLE,
117         TC_PROVIDESAMSMATH,
118         TC_PROVIDESNATBIB,
119         TC_PROVIDESMAKEIDX,
120         TC_PROVIDESURL,
121         TC_LEFTMARGIN,
122         TC_RIGHTMARGIN,
123         TC_FLOAT,
124         TC_COUNTER,
125         TC_NOFLOAT,
126         TC_TITLELATEXNAME,
127         TC_TITLELATEXTYPE
128 };
129
130 // Reads a textclass structure from file.
131 bool LyXTextClass::Read(string const & filename, bool merge)
132 {
133         keyword_item textClassTags[] = {
134                 { "charstyle",       TC_CHARSTYLE },
135                 { "classoptions",    TC_CLASSOPTIONS },
136                 { "columns",         TC_COLUMNS },
137                 { "counter",         TC_COUNTER },
138                 { "defaultfont",     TC_DEFAULTFONT },
139                 { "defaultstyle",    TC_DEFAULTSTYLE },
140                 { "environment",     TC_ENVIRONMENT },
141                 { "float",           TC_FLOAT },
142                 { "input",           TC_INPUT },
143                 { "leftmargin",      TC_LEFTMARGIN },
144                 { "nofloat",         TC_NOFLOAT },
145                 { "nostyle",         TC_NOSTYLE },
146                 { "outputtype",      TC_OUTPUTTYPE },
147                 { "pagestyle",       TC_PAGESTYLE },
148                 { "preamble",        TC_PREAMBLE },
149                 { "providesamsmath", TC_PROVIDESAMSMATH },
150                 { "providesmakeidx", TC_PROVIDESMAKEIDX },
151                 { "providesnatbib",  TC_PROVIDESNATBIB },
152                 { "providesurl",     TC_PROVIDESURL },
153                 { "rightmargin",     TC_RIGHTMARGIN },
154                 { "secnumdepth",     TC_SECNUMDEPTH },
155                 { "sides",           TC_SIDES },
156                 { "style",           TC_STYLE },
157                 { "titlelatexname",  TC_TITLELATEXNAME },
158                 { "titlelatextype",  TC_TITLELATEXTYPE },
159                 { "tocdepth",        TC_TOCDEPTH }
160         };
161
162         if (!merge)
163                 lyxerr[Debug::TCLASS] << "Reading textclass "
164                                       << MakeDisplayPath(filename)
165                                       << endl;
166         else
167                 lyxerr[Debug::TCLASS] << "Reading input file "
168                                      << MakeDisplayPath(filename)
169                                      << endl;
170
171         LyXLex lexrc(textClassTags,
172                 sizeof(textClassTags) / sizeof(textClassTags[0]));
173         bool error = false;
174
175         lexrc.setFile(filename);
176         if (!lexrc.isOK()) error = true;
177
178         // parsing
179         while (lexrc.isOK() && !error) {
180                 int le = lexrc.lex();
181
182                 switch (le) {
183                 case LyXLex::LEX_FEOF:
184                         continue;
185
186                 case LyXLex::LEX_UNDEF:
187                         lexrc.printError("Unknown TextClass tag `$$Token'");
188                         error = true;
189                         continue;
190
191                 default:
192                         break;
193                 }
194
195                 switch (static_cast<TextClassTags>(le)) {
196
197                 case TC_OUTPUTTYPE:   // output type definition
198                         readOutputType(lexrc);
199                         break;
200
201                 case TC_INPUT: // Include file
202                         if (lexrc.next()) {
203                                 string tmp = LibFileSearch("layouts",
204                                                             lexrc.getString(),
205                                                             "layout");
206
207                                 if (Read(tmp, true)) {
208                                         lexrc.printError("Error reading input"
209                                                          "file: "+tmp);
210                                         error = true;
211                                 }
212                         }
213                         break;
214
215                 case TC_DEFAULTSTYLE:
216                         if (lexrc.next()) {
217                                 string const name = subst(lexrc.getString(),
218                                                           '_', ' ');
219                                 defaultlayout_ = name;
220                         }
221                         break;
222
223                 case TC_ENVIRONMENT:
224                 case TC_STYLE:
225                         if (lexrc.next()) {
226                                 string const name = subst(lexrc.getString(),
227                                                     '_', ' ');
228                                 if (hasLayout(name)) {
229                                         LyXLayout * lay = operator[](name).get();
230                                         error = do_readStyle(lexrc, *lay);
231                                 } else {
232                                         LyXLayout lay;
233                                         lay.setName(name);
234                                         if (le == TC_ENVIRONMENT)
235                                                 lay.is_environment = true;
236                                         if (!(error = do_readStyle(lexrc, lay)))
237                                                 layoutlist_.push_back(
238                                                         boost::shared_ptr<LyXLayout>(new LyXLayout(lay))
239                                                         );
240
241                                         if (defaultlayout_.empty()) {
242                                                 // We do not have a default
243                                                 // layout yet, so we choose
244                                                 // the first layout we
245                                                 // encounter.
246                                                 defaultlayout_ = name;
247                                         }
248                                 }
249                         }
250                         else {
251                                 lexrc.printError("No name given for style: `$$Token'.");
252                                 error = true;
253                         }
254                         break;
255
256                 case TC_NOSTYLE:
257                         if (lexrc.next()) {
258                                 string const style = subst(lexrc.getString(),
259                                                      '_', ' ');
260                                 if (!delete_layout(style))
261                                         lyxerr << "Cannot delete style `"
262                                                << style << '\'' << endl;
263 //                                      lexrc.printError("Cannot delete style"
264 //                                                       " `$$Token'");
265                         }
266                         break;
267
268                 case TC_COLUMNS:
269                         if (lexrc.next())
270                                 columns_ = lexrc.getInteger();
271                         break;
272
273                 case TC_SIDES:
274                         if (lexrc.next()) {
275                                 switch (lexrc.getInteger()) {
276                                 case 1: sides_ = OneSide; break;
277                                 case 2: sides_ = TwoSides; break;
278                                 default:
279                                         lyxerr << "Impossible number of page"
280                                                 " sides, setting to one."
281                                                << endl;
282                                         sides_ = OneSide;
283                                         break;
284                                 }
285                         }
286                         break;
287
288                 case TC_PAGESTYLE:
289                         lexrc.next();
290                         pagestyle_ = rtrim(lexrc.getString());
291                         break;
292
293                 case TC_DEFAULTFONT:
294                         defaultfont_.lyxRead(lexrc);
295                         if (!defaultfont_.resolved()) {
296                                 lexrc.printError("Warning: defaultfont should "
297                                                  "be fully instantiated!");
298                                 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
299                         }
300                         break;
301
302                 case TC_SECNUMDEPTH:
303                         lexrc.next();
304                         secnumdepth_ = lexrc.getInteger();
305                         break;
306
307                 case TC_TOCDEPTH:
308                         lexrc.next();
309                         tocdepth_ = lexrc.getInteger();
310                         break;
311
312                         // First step to support options
313                 case TC_CLASSOPTIONS:
314                         readClassOptions(lexrc);
315                         break;
316
317                 case TC_PREAMBLE:
318                         preamble_ = lexrc.getLongString("EndPreamble");
319                         break;
320
321                 case TC_PROVIDESAMSMATH:
322                         if (lexrc.next() && lexrc.getInteger())
323                                 provides_ |= amsmath;
324                         break;
325
326                 case TC_PROVIDESNATBIB:
327                         if (lexrc.next() && lexrc.getInteger())
328                                 provides_ |= natbib;
329                         break;
330
331                 case TC_PROVIDESMAKEIDX:
332                         if (lexrc.next() && lexrc.getInteger())
333                                 provides_ |= makeidx;
334                         break;
335
336                 case TC_PROVIDESURL:
337                         if (lexrc.next() && lexrc.getInteger())
338                                 provides_ |= url;
339                         break;
340
341                 case TC_LEFTMARGIN:     // left margin type
342                         if (lexrc.next())
343                                 leftmargin_ = lexrc.getString();
344                         break;
345
346                 case TC_RIGHTMARGIN:    // right margin type
347                         if (lexrc.next())
348                                 rightmargin_ = lexrc.getString();
349                         break;
350                 case TC_CHARSTYLE:
351                         if (lexrc.next()) {
352                                 string const name = subst(lexrc.getString(), '_', ' ');
353                                 readCharStyle(lexrc, name);
354                         }
355                         break;
356                 case TC_FLOAT:
357                         readFloat(lexrc);
358                         break;
359                 case TC_COUNTER:
360                         readCounter(lexrc);
361                         break;
362                 case TC_TITLELATEXTYPE:
363                         readTitleType(lexrc);
364                         break;
365                 case TC_TITLELATEXNAME:
366                         if (lexrc.next())
367                                 titlename_ = lexrc.getString();
368                         break;
369                 case TC_NOFLOAT:
370                         if (lexrc.next()) {
371                                 string const nofloat = lexrc.getString();
372                                 floatlist_->erase(nofloat);
373                         }
374                         break;
375                 }
376         }
377
378         if (!merge) { // we are at top level here.
379                 lyxerr[Debug::TCLASS] << "Finished reading textclass "
380                                       << MakeDisplayPath(filename)
381                                       << endl;
382                 if (defaultlayout_.empty()) {
383                         lyxerr << "Error: Textclass '" << name_
384                                << "' is missing a defaultstyle." << endl;
385                         error = true;
386                 }
387
388                 min_toclevel_ = LyXLayout::NOT_IN_TOC;
389                 max_toclevel_ = LyXLayout::NOT_IN_TOC;
390                 const_iterator cit = begin();
391                 const_iterator the_end = end();
392                 for ( ; cit != the_end ; ++cit) {
393                         int const toclevel = (*cit)->toclevel;
394                         if (toclevel != LyXLayout::NOT_IN_TOC) {
395                                 if (min_toclevel_ == LyXLayout::NOT_IN_TOC)
396                                         min_toclevel_ = toclevel;
397                                 else
398                                         min_toclevel_ = std::min(min_toclevel_,
399                                                          toclevel);
400                                 max_toclevel_ = std::max(max_toclevel_,
401                                                          toclevel);
402                         }
403                 }
404                 lyxerr[Debug::TCLASS]
405                         << "Minimum TocLevel is " << min_toclevel_
406                         << ", maximum is " << max_toclevel_ <<endl;
407
408         } else
409                 lyxerr[Debug::TCLASS] << "Finished reading input file "
410                                       << MakeDisplayPath(filename)
411                                       << endl;
412
413         return error;
414 }
415
416
417 void LyXTextClass::readTitleType(LyXLex & lexrc)
418 {
419         keyword_item titleTypeTags[] = {
420                 { "commandafter", TITLE_COMMAND_AFTER },
421                 { "environment", TITLE_ENVIRONMENT }
422         };
423
424         pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
425
426         int le = lexrc.lex();
427         switch (le) {
428         case LyXLex::LEX_UNDEF:
429                 lexrc.printError("Unknown output type `$$Token'");
430                 return;
431         case TITLE_COMMAND_AFTER:
432         case TITLE_ENVIRONMENT:
433                 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
434                 break;
435         default:
436                 lyxerr << "Unhandled value " << le
437                        << " in LyXTextClass::readTitleType." << endl;
438
439                 break;
440         }
441 }
442
443
444 void LyXTextClass::readOutputType(LyXLex & lexrc)
445 {
446         keyword_item outputTypeTags[] = {
447                 { "docbook", DOCBOOK },
448                 { "latex", LATEX },
449                 { "linuxdoc", LINUXDOC },
450                 { "literate", LITERATE }
451         };
452
453         pushpophelper pph(lexrc, outputTypeTags, LITERATE);
454
455         int le = lexrc.lex();
456         switch (le) {
457         case LyXLex::LEX_UNDEF:
458                 lexrc.printError("Unknown output type `$$Token'");
459                 return;
460         case LATEX:
461         case LINUXDOC:
462         case DOCBOOK:
463         case LITERATE:
464                 outputType_ = static_cast<OutputType>(le);
465                 break;
466         default:
467                 lyxerr << "Unhandled value " << le
468                        << " in LyXTextClass::readOutputType." << endl;
469
470                 break;
471         }
472 }
473
474
475 enum ClassOptionsTags {
476         CO_FONTSIZE = 1,
477         CO_PAGESTYLE,
478         CO_OTHER,
479         CO_HEADER,
480         CO_END
481 };
482
483
484 void LyXTextClass::readClassOptions(LyXLex & lexrc)
485 {
486         keyword_item classOptionsTags[] = {
487                 {"end", CO_END },
488                 {"fontsize", CO_FONTSIZE },
489                 {"header", CO_HEADER },
490                 {"other", CO_OTHER },
491                 {"pagestyle", CO_PAGESTYLE }
492         };
493
494         lexrc.pushTable(classOptionsTags, CO_END);
495         bool getout = false;
496         while (!getout && lexrc.isOK()) {
497                 int le = lexrc.lex();
498                 switch (le) {
499                 case LyXLex::LEX_UNDEF:
500                         lexrc.printError("Unknown ClassOption tag `$$Token'");
501                         continue;
502                 default: break;
503                 }
504                 switch (static_cast<ClassOptionsTags>(le)) {
505                 case CO_FONTSIZE:
506                         lexrc.next();
507                         opt_fontsize_ = rtrim(lexrc.getString());
508                         break;
509                 case CO_PAGESTYLE:
510                         lexrc.next();
511                         opt_pagestyle_ = rtrim(lexrc.getString());
512                         break;
513                 case CO_OTHER:
514                         lexrc.next();
515                         options_ = lexrc.getString();
516                         break;
517                 case CO_HEADER:
518                         lexrc.next();
519                         class_header_ = subst(lexrc.getString(), "&quot;", "\"");
520                         break;
521                 case CO_END:
522                         getout = true;
523                         break;
524                 }
525         }
526         lexrc.popTable();
527 }
528
529 enum CharStyleTags {
530         CS_FONT = 1,
531         CS_LABELFONT,
532         CS_LATEXTYPE,
533         CS_LATEXNAME,
534         CS_LATEXPARAM,
535         CS_PREAMBLE,
536         CS_END
537 };
538
539
540 void LyXTextClass::readCharStyle(LyXLex & lexrc, string const & name)
541 {
542         keyword_item elementTags[] = {
543                 { "end", CS_END },
544                 { "font", CS_FONT },
545                 { "labelfont", CS_LABELFONT },
546                 { "latexname", CS_LATEXNAME },
547                 { "latexparam", CS_LATEXPARAM },
548                 { "latextype", CS_LATEXTYPE },
549                 { "preamble", CS_PREAMBLE}
550         };
551
552         lexrc.pushTable(elementTags, CS_END);
553
554         string latextype;
555         string latexname;
556         string latexparam;
557         LyXFont font(LyXFont::ALL_INHERIT);
558         LyXFont labelfont(LyXFont::ALL_INHERIT);
559         string preamble;
560
561         bool getout = false;
562         while (!getout && lexrc.isOK()) {
563                 int le = lexrc.lex();
564                 switch (le) {
565                 case LyXLex::LEX_UNDEF:
566                         lexrc.printError("Unknown ClassOption tag `$$Token'");
567                         continue;
568                 default: break;
569                 }
570                 switch (static_cast<CharStyleTags>(le)) {
571                 case CS_LATEXTYPE:
572                         lexrc.next();
573                         latextype = lexrc.getString();
574                         break;
575                 case CS_LATEXNAME:
576                         lexrc.next();
577                         latexname = lexrc.getString();
578                         break;
579                 case CS_LATEXPARAM:
580                         lexrc.next();
581                         latexparam = subst(lexrc.getString(), "&quot;", "\"");
582                         break;
583                 case CS_LABELFONT:
584                         labelfont.lyxRead(lexrc);
585                         break;
586                 case CS_FONT:
587                         font.lyxRead(lexrc);
588                         labelfont = font;
589                         break;
590                 case CS_PREAMBLE:
591                         preamble = lexrc.getLongString("EndPreamble");
592                         break;
593                 case CS_END:
594                         getout = true;
595                         break;
596                 }
597         }
598
599         //
600         // Here add element to list if getout == true
601         if (getout) {
602                 CharStyle cs;
603                 cs.name = name;
604                 cs.latextype = latextype;
605                 cs.latexname = latexname;
606                 cs.latexparam = latexparam;
607                 cs.font = font;
608                 cs.labelfont = labelfont;
609                 cs.preamble = preamble;
610                 charstyles().push_back(cs);
611         }
612
613         lexrc.popTable();
614 }
615
616
617 enum FloatTags {
618         FT_TYPE = 1,
619         FT_NAME,
620         FT_PLACEMENT,
621         FT_EXT,
622         FT_WITHIN,
623         FT_STYLE,
624         FT_LISTNAME,
625         FT_BUILTIN,
626         FT_END
627 };
628
629
630 void LyXTextClass::readFloat(LyXLex & lexrc)
631 {
632         keyword_item floatTags[] = {
633                 { "end", FT_END },
634                 { "extension", FT_EXT },
635                 { "guiname", FT_NAME },
636                 { "latexbuiltin", FT_BUILTIN },
637                 { "listname", FT_LISTNAME },
638                 { "numberwithin", FT_WITHIN },
639                 { "placement", FT_PLACEMENT },
640                 { "style", FT_STYLE },
641                 { "type", FT_TYPE }
642         };
643
644         lexrc.pushTable(floatTags, FT_END);
645
646         string type;
647         string placement;
648         string ext;
649         string within;
650         string style;
651         string name;
652         string listname;
653         bool builtin = false;
654
655         bool getout = false;
656         while (!getout && lexrc.isOK()) {
657                 int le = lexrc.lex();
658                 switch (le) {
659                 case LyXLex::LEX_UNDEF:
660                         lexrc.printError("Unknown ClassOption tag `$$Token'");
661                         continue;
662                 default: break;
663                 }
664                 switch (static_cast<FloatTags>(le)) {
665                 case FT_TYPE:
666                         lexrc.next();
667                         type = lexrc.getString();
668                         // Here we could check if this type is already defined
669                         // and modify it with the rest of the vars instead.
670                         break;
671                 case FT_NAME:
672                         lexrc.next();
673                         name = lexrc.getString();
674                         break;
675                 case FT_PLACEMENT:
676                         lexrc.next();
677                         placement = lexrc.getString();
678                         break;
679                 case FT_EXT:
680                         lexrc.next();
681                         ext = lexrc.getString();
682                         break;
683                 case FT_WITHIN:
684                         lexrc.next();
685                         within = lexrc.getString();
686                         if (within == "none")
687                                 within.erase();
688                         break;
689                 case FT_STYLE:
690                         lexrc.next();
691                         style = lexrc.getString();
692                         break;
693                 case FT_LISTNAME:
694                         lexrc.next();
695                         listname = lexrc.getString();
696                         break;
697                 case FT_BUILTIN:
698                         lexrc.next();
699                         builtin = lexrc.getBool();
700                         break;
701                 case FT_END:
702                         getout = true;
703                         break;
704                 }
705         }
706
707         // Here if have a full float if getout == true
708         if (getout) {
709                 Floating newfloat(type, placement, ext, within,
710                                   style, name, listname, builtin);
711                 floatlist_->newFloat(newfloat);
712         }
713
714         lexrc.popTable();
715 }
716
717
718 enum CounterTags {
719         CT_NAME = 1,
720         CT_WITHIN,
721         CT_END
722 };
723
724 void LyXTextClass::readCounter(LyXLex & lexrc)
725 {
726         keyword_item counterTags[] = {
727                 { "end", CT_END },
728                 { "name", CT_NAME },
729                 { "within", CT_WITHIN }
730         };
731
732         lexrc.pushTable(counterTags, CT_END);
733
734         string name;
735         string within;
736
737         bool getout = false;
738         while (!getout && lexrc.isOK()) {
739                 int le = lexrc.lex();
740                 switch (le) {
741                 case LyXLex::LEX_UNDEF:
742                         lexrc.printError("Unknown ClassOption tag `$$Token'");
743                         continue;
744                 default: break;
745                 }
746                 switch (static_cast<CounterTags>(le)) {
747                 case CT_NAME:
748                         lexrc.next();
749                         name = lexrc.getString();
750                         break;
751                 case CT_WITHIN:
752                         lexrc.next();
753                         within = lexrc.getString();
754                         if (within == "none")
755                                 within.erase();
756                         break;
757                 case CT_END:
758                         getout = true;
759                         break;
760                 }
761         }
762
763         // Here if have a full counter if getout == true
764         if (getout) {
765                 if (within.empty()) {
766                         ctrs_->newCounter(name);
767                 } else {
768                         ctrs_->newCounter(name, within);
769                 }
770         }
771
772         lexrc.popTable();
773 }
774
775
776 LyXFont const & LyXTextClass::defaultfont() const
777 {
778         return defaultfont_;
779 }
780
781
782 string const & LyXTextClass::leftmargin() const
783 {
784         return leftmargin_;
785 }
786
787
788 string const & LyXTextClass::rightmargin() const
789 {
790         return rightmargin_;
791 }
792
793
794 bool LyXTextClass::hasLayout(string const & n) const
795 {
796         string const name = (n.empty() ? defaultLayoutName() : n);
797
798         return find_if(layoutlist_.begin(), layoutlist_.end(),
799                        LayoutNamesEqual(name))
800                 != layoutlist_.end();
801 }
802
803
804
805 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
806 {
807         BOOST_ASSERT(!name.empty());
808
809         LayoutList::const_iterator cit =
810                 find_if(layoutlist_.begin(),
811                         layoutlist_.end(),
812                         LayoutNamesEqual(name));
813
814         if (cit == layoutlist_.end()) {
815                 lyxerr << "We failed to find the layout '" << name
816                        << "' in the layout list. You MUST investigate!"
817                        << endl;
818                 for (LayoutList::const_iterator it = layoutlist_.begin();
819                          it != layoutlist_.end(); ++it)
820                         lyxerr  << " " << it->get()->name() << endl;
821
822                 // we require the name to exist
823                 BOOST_ASSERT(false);
824         }
825
826         return (*cit);
827 }
828
829
830
831 bool LyXTextClass::delete_layout(string const & name)
832 {
833         if (name == defaultLayoutName())
834                 return false;
835
836         LayoutList::iterator it =
837                 remove_if(layoutlist_.begin(), layoutlist_.end(),
838                           LayoutNamesEqual(name));
839
840         LayoutList::iterator end = layoutlist_.end();
841         bool const ret = (it != end);
842         layoutlist_.erase(it, end);
843         return ret;
844 }
845
846
847 // Load textclass info if not loaded yet
848 bool LyXTextClass::load() const
849 {
850         if (loaded_)
851                 return true;
852
853         // Read style-file
854         string const real_file = LibFileSearch("layouts", name_, "layout");
855         loaded_ = const_cast<LyXTextClass*>(this)->Read(real_file) == 0;
856
857         if (!loaded_) {
858                 lyxerr << "Error reading `"
859                        << MakeDisplayPath(real_file)
860                        << "'\n(Check `" << name_
861                        << "')\nCheck your installation and "
862                         "try Options/Reconfigure..." << endl;
863         }
864
865         return loaded_;
866 }
867
868
869 FloatList & LyXTextClass::floats()
870 {
871         return *floatlist_.get();
872 }
873
874
875 FloatList const & LyXTextClass::floats() const
876 {
877         return *floatlist_.get();
878 }
879
880
881 Counters & LyXTextClass::counters() const
882 {
883         return *ctrs_.get();
884 }
885
886
887 CharStyles::iterator LyXTextClass::charstyle(string const & s) const
888 {
889         CharStyles::iterator cs = charstyles().begin();
890         CharStyles::iterator csend = charstyles().end();
891         for (; cs != csend; ++cs) {
892                 if (cs->name == s)
893                         return cs;
894         }
895         return csend;
896 }
897
898
899 string const & LyXTextClass::defaultLayoutName() const
900 {
901         // This really should come from the actual layout... (Lgb)
902         return defaultlayout_;
903 }
904
905
906 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
907 {
908         return operator[](defaultLayoutName());
909 }
910
911
912 string const & LyXTextClass::name() const
913 {
914         return name_;
915 }
916
917
918 string const & LyXTextClass::latexname() const
919 {
920         const_cast<LyXTextClass*>(this)->load();
921         return latexname_;
922 }
923
924
925 string const & LyXTextClass::description() const
926 {
927         return description_;
928 }
929
930
931 string const & LyXTextClass::opt_fontsize() const
932 {
933         return opt_fontsize_;
934 }
935
936
937 string const & LyXTextClass::opt_pagestyle() const
938 {
939         return opt_pagestyle_;
940 }
941
942
943 string const & LyXTextClass::options() const
944 {
945         return options_;
946 }
947
948
949 string const & LyXTextClass::class_header() const
950 {
951         return class_header_;
952 }
953
954
955 string const & LyXTextClass::pagestyle() const
956 {
957         return pagestyle_;
958 }
959
960
961 string const & LyXTextClass::preamble() const
962 {
963         return preamble_;
964 }
965
966
967 LyXTextClass::PageSides LyXTextClass::sides() const
968 {
969         return sides_;
970 }
971
972
973 int LyXTextClass::secnumdepth() const
974 {
975         return secnumdepth_;
976 }
977
978
979 int LyXTextClass::tocdepth() const
980 {
981         return tocdepth_;
982 }
983
984
985 OutputType LyXTextClass::outputType() const
986 {
987         return outputType_;
988 }
989
990
991 bool LyXTextClass::provides(LyXTextClass::Provides p) const
992 {
993         return provides_ & p;
994 }
995
996
997 unsigned int LyXTextClass::columns() const
998 {
999         return columns_;
1000 }
1001
1002
1003 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
1004 {
1005         return titletype_;
1006 }
1007
1008
1009 string const & LyXTextClass::titlename() const
1010 {
1011         return titlename_;
1012 }
1013
1014
1015 int LyXTextClass::size() const
1016 {
1017         return layoutlist_.size();
1018 }
1019
1020
1021 int LyXTextClass::min_toclevel() const
1022 {
1023         return min_toclevel_;
1024 }
1025
1026
1027 int LyXTextClass::max_toclevel() const
1028 {
1029         return max_toclevel_;
1030 }
1031
1032
1033 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
1034 {
1035         switch (p) {
1036         case LyXTextClass::OneSide:
1037                 os << '1';
1038                 break;
1039         case LyXTextClass::TwoSides:
1040                 os << '2';
1041                 break;
1042         }
1043         return os;
1044 }