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