1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-1999 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
21 #include "definitions.h"
24 #include "support/filetools.h"
25 #include "lyx_gui_misc.h"
28 #include "support/LAssert.h"
30 // Global variable: textclass table.
31 LyXTextClassList textclasslist;
33 // Reads the style files
36 lyxerr[Debug::TCLASS] << "LyXSetStyle: parsing configuration...\n";
38 if (!textclasslist.Read()) {
39 lyxerr[Debug::TCLASS] << "LyXSetStyle: an error occured "
40 "during parsing.\n Exiting." << endl;
44 lyxerr[Debug::TCLASS] << "LyXSetStyle: configuration parsed." << endl;
48 // The order of the LayoutTags enum is no more important. [asierra300396]
51 LT_ALIGN, LT_ALIGNPOSSIBLE,
53 LT_BOTTOMSEP, LT_CENTER, LT_CENTERED_TOP_ENVIRONMENT, LT_COLUMNS,
54 LT_COPYSTYLE, LT_OBSOLETEDBY,
55 LT_COMMAND, LT_COUNTER_CHAPTER, LT_COUNTER_ENUMI, LT_COUNTER_ENUMII,
56 LT_COUNTER_ENUMIII, LT_COUNTER_ENUMIV, LT_COUNTER_PARAGRAPH,
57 LT_COUNTER_SECTION, LT_COUNTER_SUBPARAGRAPH, LT_COUNTER_SUBSECTION,
58 LT_COUNTER_SUBSUBSECTION, LT_DEFAULTFONT, LT_DYNAMIC, LT_EMPTY,
59 LT_END, LT_ENVIRONMENT, LT_ENVIRONMENT_DEFAULT,
60 LT_FANCYHDR, LT_FILL_BOTTOM, LT_FILL_TOP, LT_FIRST_COUNTER,
61 LT_FIRST_DYNAMIC, LT_FONT, LT_FREE_SPACING, LT_HEADINGS, LT_INPUT,
62 LT_ITEM_ENVIRONMENT, LT_ITEMSEP, LT_KEEPEMPTY,
63 LT_LABEL_BOTTOMSEP, LT_LABELFONT, LT_TEXTFONT,
64 LT_LABELINDENT, LT_LABELSEP, LT_LABELSTRING,
65 LT_LABELSTRING_APPENDIX, LT_LABELTYPE,
66 LT_LATEXNAME, LT_LATEXPARAM, LT_LATEXTYPE, LT_LAYOUT, LT_LEFT,
68 LT_LIST_ENVIRONMENT , LT_MANUAL, LT_MAXCOUNTER,
69 LT_NEED_PROTECT, LT_NEWLINE,
70 LT_NEXTNOINDENT, LT_NO_LABEL, LT_NOSTYLE,
71 LT_PAGESTYLE, LT_PARAGRAPH,
72 LT_PARINDENT, LT_PARSEP, LT_PARSKIP, LT_PLAIN, LT_PREAMBLE,
73 LT_PROVIDESAMSMATH, LT_PROVIDESMAKEIDX, LT_PROVIDESURL, LT_RIGHT,
74 LT_RIGHT_ADDRESS_BOX, LT_RIGHTMARGIN, LT_SENSITIVE, LT_SIDES,
75 LT_SPACING, LT_SPACING_SINGLE, LT_SPACING_ONEHALF,
76 LT_SPACING_DOUBLE, LT_OTHER, LT_CLASSOPTIONS, LT_FONTSIZE,
77 LT_STATIC, LT_STYLE, LT_TOP_ENVIRONMENT, LT_TOPSEP, LT_BIBLIO,
78 LT_INTITLE, LT_SECNUMDEPTH, LT_TOCDEPTH,
79 LT_OUTPUTTYPE, LT_OTLATEX, LT_OTLINUXDOC, LT_OTDOCBOOK, LT_OTLITERATE
83 // This table is sorted alphabetically [asierra 30March96]
84 static keyword_item layoutTags[] = {
85 { "align", LT_ALIGN },
86 { "alignpossible", LT_ALIGNPOSSIBLE },
87 { "bibliography", LT_BIBLIO },
88 { "block", LT_BLOCK },
89 { "bottomsep", LT_BOTTOMSEP },
90 { "center", LT_CENTER },
91 { "centered_top_environment", LT_CENTERED_TOP_ENVIRONMENT },
92 { "classoptions", LT_CLASSOPTIONS },
93 { "columns", LT_COLUMNS },
94 { "command", LT_COMMAND },
95 { "copystyle", LT_COPYSTYLE },
96 { "counter_chapter", LT_COUNTER_CHAPTER },
97 { "counter_enumi", LT_COUNTER_ENUMI },
98 { "counter_enumii", LT_COUNTER_ENUMII },
99 { "counter_enumiii", LT_COUNTER_ENUMIII },
100 { "counter_enumiv", LT_COUNTER_ENUMIV },
101 { "counter_paragraph", LT_COUNTER_PARAGRAPH },
102 { "counter_section", LT_COUNTER_SECTION },
103 { "counter_subparagraph", LT_COUNTER_SUBPARAGRAPH },
104 { "counter_subsection", LT_COUNTER_SUBSECTION },
105 { "counter_subsubsection", LT_COUNTER_SUBSUBSECTION },
106 { "defaultfont", LT_DEFAULTFONT },
107 { "docbook", LT_OTDOCBOOK },
108 { "double", LT_SPACING_DOUBLE },
109 { "dynamic", LT_DYNAMIC },
110 { "empty", LT_EMPTY },
112 { "environment", LT_ENVIRONMENT },
113 { "environment_default", LT_ENVIRONMENT_DEFAULT },
114 { "fancyhdr", LT_FANCYHDR },
115 { "fill_bottom", LT_FILL_BOTTOM },
116 { "fill_top", LT_FILL_TOP },
117 { "first_counter", LT_FIRST_COUNTER },
118 { "first_dynamic", LT_FIRST_DYNAMIC },
120 { "fontsize", LT_FONTSIZE },
121 { "freespacing", LT_FREE_SPACING },
122 { "headings", LT_HEADINGS },
123 { "input", LT_INPUT },
124 { "intitle", LT_INTITLE },
125 { "item_environment", LT_ITEM_ENVIRONMENT },
126 { "itemsep", LT_ITEMSEP },
127 { "keepempty", LT_KEEPEMPTY },
128 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
129 { "labelfont", LT_LABELFONT },
130 { "labelindent", LT_LABELINDENT },
131 { "labelsep", LT_LABELSEP },
132 { "labelstring", LT_LABELSTRING },
133 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
134 { "labeltype", LT_LABELTYPE },
135 { "latex", LT_OTLATEX },
136 { "latexname", LT_LATEXNAME },
137 { "latexparam", LT_LATEXPARAM }, //arrae970411
138 { "latextype", LT_LATEXTYPE },
139 { "layout", LT_LAYOUT },
141 { "leftmargin", LT_LEFTMARGIN },
142 { "linuxdoc", LT_OTLINUXDOC },
143 { "list_environment", LT_LIST_ENVIRONMENT },
144 { "literate", LT_OTLITERATE },
145 { "manual", LT_MANUAL },
146 { "margin", LT_MARGIN },
147 { "maxcounter", LT_MAXCOUNTER },
148 { "needprotect", LT_NEED_PROTECT },
149 { "newline", LT_NEWLINE },
150 { "nextnoindent", LT_NEXTNOINDENT },
151 { "no_label", LT_NO_LABEL },
152 { "nostyle", LT_NOSTYLE },
153 { "obsoletedby", LT_OBSOLETEDBY },
154 { "onehalf", LT_SPACING_ONEHALF },
155 { "other", LT_OTHER },
156 { "outputtype", LT_OUTPUTTYPE },
157 { "pagestyle", LT_PAGESTYLE },
158 { "paragraph", LT_PARAGRAPH },
159 { "parindent", LT_PARINDENT },
160 { "parsep", LT_PARSEP },
161 { "parskip", LT_PARSKIP },
162 { "plain", LT_PLAIN },
163 { "preamble", LT_PREAMBLE },
164 { "providesamsmath", LT_PROVIDESAMSMATH },
165 { "providesmakeidx", LT_PROVIDESMAKEIDX },
166 { "providesurl", LT_PROVIDESURL },
167 { "right", LT_RIGHT },
168 { "right_address_box", LT_RIGHT_ADDRESS_BOX },
169 { "rightmargin", LT_RIGHTMARGIN },
170 { "secnumdepth", LT_SECNUMDEPTH },
171 { "sensitive", LT_SENSITIVE },
172 { "sides", LT_SIDES },
173 { "single", LT_SPACING_SINGLE },
174 { "spacing", LT_SPACING },
175 { "static", LT_STATIC },
176 { "style", LT_STYLE },
177 { "textfont", LT_TEXTFONT },
178 { "tocdepth", LT_TOCDEPTH },
179 { "top_environment", LT_TOP_ENVIRONMENT },
180 { "topsep", LT_TOPSEP }
184 /////////////////////
186 // Constructor for layout
187 LyXLayout::LyXLayout ()
189 margintype = MARGIN_STATIC;
190 latextype = LATEX_PARAGRAPH;
194 font = LyXFont(LyXFont::ALL_INHERIT);
195 labelfont = LyXFont(LyXFont::ALL_INHERIT);
196 resfont = LyXFont(LyXFont::ALL_SANE);
197 reslabelfont = LyXFont(LyXFont::ALL_SANE);
198 nextnoindent = false;
203 labelbottomsep = 0.0;
205 align = LYX_ALIGN_BLOCK;
206 alignpossible = LYX_ALIGN_BLOCK;
207 labeltype = LABEL_NO_LABEL;
208 // Should or should not. That is the question.
209 // spacing.set(Spacing::OneHalf);
212 newline_allowed = true;
213 free_spacing = false;
217 // Reads a layout definition from file
218 bool LyXLayout::Read (LyXLex & lexrc, LyXTextClass const & tclass)
221 bool finished = false;
223 // parse style section
224 while (!finished && lexrc.IsOK() && !error) {
225 switch(lexrc.lex()) {
230 case -1: // parse error
231 lexrc.printError("Unknown tag `$$Token'");
235 case LT_END: // end of structure
239 case LT_COPYSTYLE: // initialize with a known style
241 if (tclass.hasLayout(lexrc.GetString())) {
242 string tmpname = name_;
243 this->operator=(tclass.GetLayout(lexrc.GetString()));
246 lexrc.printError("Cannot copy known "
252 case LT_OBSOLETEDBY: // replace with a known style
254 if (tclass.hasLayout(lexrc.GetString())) {
255 string tmpname = name_;
256 this->operator=(tclass.GetLayout(lexrc.GetString()));
258 if (obsoleted_by().empty())
259 obsoleted_by_ = lexrc.GetString();
261 lexrc.printError("Cannot replace with"
268 case LT_MARGIN: // Margin style definition.
270 switch(lexrc.lex()) {
272 margintype = MARGIN_STATIC;
275 margintype = MARGIN_MANUAL;
278 margintype = MARGIN_DYNAMIC;
280 case LT_FIRST_DYNAMIC:
281 margintype = MARGIN_FIRST_DYNAMIC;
283 case LT_RIGHT_ADDRESS_BOX:
284 margintype = MARGIN_RIGHT_ADDRESS_BOX;
287 lexrc.printError("Unknown margin type `$$Token'");
292 case LT_LATEXTYPE: // Latex style definition.
293 switch (lexrc.lex()) {
295 latextype=LATEX_PARAGRAPH;
298 latextype=LATEX_COMMAND;
301 latextype=LATEX_ENVIRONMENT;
303 case LT_ITEM_ENVIRONMENT:
304 latextype=LATEX_ITEM_ENVIRONMENT;
306 case LT_LIST_ENVIRONMENT:
307 latextype=LATEX_LIST_ENVIRONMENT;
310 lexrc.printError("Unknown latextype `$$Token'");
316 intitle = lexrc.next() && lexrc.GetInteger();
319 case LT_NEED_PROTECT:
320 needprotect = lexrc.next() && lexrc.GetInteger();
324 keepempty = lexrc.next() && lexrc.GetInteger();
337 labelfont.lyxRead(lexrc);
340 case LT_NEXTNOINDENT: // Indent next paragraph?
341 if (lexrc.next() && lexrc.GetInteger())
344 nextnoindent = false;
349 latexname_ = lexrc.GetString();
354 latexparam_ = lexrc.GetString();
358 preamble_ = lexrc.getLongString("EndPreamble");
362 switch (lexrc.lex()) {
364 labeltype = LABEL_NO_LABEL;
367 labeltype = LABEL_MANUAL;
369 case LT_TOP_ENVIRONMENT:
370 labeltype = LABEL_TOP_ENVIRONMENT;
372 case LT_CENTERED_TOP_ENVIRONMENT:
373 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
376 labeltype = LABEL_STATIC;
379 labeltype = LABEL_SENSITIVE;
381 case LT_COUNTER_CHAPTER:
382 labeltype = LABEL_COUNTER_CHAPTER;
384 case LT_COUNTER_SECTION:
385 labeltype = LABEL_COUNTER_SECTION;
387 case LT_COUNTER_SUBSECTION:
388 labeltype = LABEL_COUNTER_SUBSECTION;
390 case LT_COUNTER_SUBSUBSECTION:
391 labeltype = LABEL_COUNTER_SUBSUBSECTION;
393 case LT_COUNTER_PARAGRAPH:
394 labeltype = LABEL_COUNTER_PARAGRAPH;
396 case LT_COUNTER_SUBPARAGRAPH:
397 labeltype = LABEL_COUNTER_SUBPARAGRAPH;
399 case LT_COUNTER_ENUMI:
400 labeltype = LABEL_COUNTER_ENUMI;
402 case LT_COUNTER_ENUMII:
403 labeltype = LABEL_COUNTER_ENUMII;
405 case LT_COUNTER_ENUMIII:
406 labeltype = LABEL_COUNTER_ENUMIII;
408 case LT_COUNTER_ENUMIV:
409 labeltype = LABEL_COUNTER_ENUMIV;
412 labeltype = LABEL_BIBLIO;
415 lexrc.printError("Unknown labeltype `$$Token'");
419 case LT_LEFTMARGIN: // left margin type
421 leftmargin = lexrc.GetString();
424 case LT_RIGHTMARGIN: // right margin type
426 rightmargin = lexrc.GetString();
429 case LT_LABELINDENT: // label indenting flag
431 labelindent = lexrc.GetString();
434 case LT_PARINDENT: // paragraph indent. flag
436 parindent = lexrc.GetString();
439 case LT_PARSKIP: // paragraph skip size
441 parskip = lexrc.GetFloat();
444 case LT_ITEMSEP: // item separation size
446 itemsep = lexrc.GetFloat();
449 case LT_TOPSEP: // top separation size
451 topsep = lexrc.GetFloat();
454 case LT_BOTTOMSEP: // bottom separation size
456 bottomsep = lexrc.GetFloat();
459 case LT_LABEL_BOTTOMSEP: // label bottom separation size
461 labelbottomsep = lexrc.GetFloat();
464 case LT_LABELSEP: // label separator
466 labelsep = subst(lexrc.GetString(), 'x', ' ');
470 case LT_PARSEP: // par. separation size
472 parsep = lexrc.GetFloat();
475 case LT_FILL_TOP: // fill top flag
477 fill_top = lexrc.GetInteger();
480 case LT_FILL_BOTTOM: // fill bottom flag
482 fill_bottom = lexrc.GetInteger();
485 case LT_NEWLINE: // newlines allowed?
487 newline_allowed = lexrc.GetInteger();
490 case LT_ALIGN: // paragraph align
491 switch (lexrc.lex()) {
493 align = LYX_ALIGN_BLOCK;
496 align = LYX_ALIGN_LEFT;
499 align = LYX_ALIGN_RIGHT;
502 align = LYX_ALIGN_CENTER;
505 align = LYX_ALIGN_LAYOUT;
508 lexrc.printError("Unknown alignment `$$Token'");
512 case LT_ALIGNPOSSIBLE: // paragraph allowed align
513 { alignpossible = LYX_ALIGN_NONE;
515 int lineno = lexrc.GetLineNo();
517 switch (lexrc.lex()) {
519 alignpossible |= LYX_ALIGN_BLOCK;
522 alignpossible |= LYX_ALIGN_LEFT;
525 alignpossible |= LYX_ALIGN_RIGHT;
528 alignpossible |= LYX_ALIGN_CENTER;
531 alignpossible |= LYX_ALIGN_LAYOUT;
534 lexrc.printError("Unknown alignment `$$Token'");
537 } while (lineno == lexrc.GetLineNo());
541 case LT_LABELSTRING: // label string definition
543 labelstring_ = lexrc.GetString();
546 case LT_LABELSTRING_APPENDIX: // label string appendix definition
548 labelstring_appendix_ = lexrc.GetString();
551 case LT_FREE_SPACING: // Allow for free spacing.
553 free_spacing = lexrc.GetInteger();
556 case LT_SPACING: // setspace.sty
557 switch(lexrc.lex()) {
558 case LT_SPACING_SINGLE:
559 spacing.set(Spacing::Single);
561 case LT_SPACING_ONEHALF:
562 spacing.set(Spacing::Onehalf);
564 case LT_SPACING_DOUBLE:
565 spacing.set(Spacing::Double);
569 spacing.set(Spacing::Other, lexrc.GetFloat());
572 lexrc.printError("Unknown spacing `$$Token'");
575 default: /* context error */
576 lexrc.printError("Tag `$$Token' is not "
577 "allowed in layout");
587 /* ******************************************************************* */
589 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
591 : name_(fn), latexname_(cln), description_(desc)
598 pagestyle_ = "default";
599 maxcounter_ = LABEL_COUNTER_CHAPTER;
600 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
601 opt_fontsize_ = "10|11|12";
602 opt_pagestyle_ = "empty|plain|headings|fancy";
608 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
610 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
611 if (!lay.Read(lexrc, *this)) {
613 lay.resfont = lay.font;
614 lay.resfont.realize(defaultfont());
615 lay.reslabelfont = lay.labelfont;
616 lay.reslabelfont.realize(defaultfont());
617 return false; // no errors
619 lyxerr << "Error parsing style `" << lay.name() << "'" << endl;
624 // Reads a textclass structure from file.
625 bool LyXTextClass::Read(string const & filename, bool merge)
628 lyxerr[Debug::TCLASS] << "Reading textclass "
629 << MakeDisplayPath(filename)
632 lyxerr[Debug::TCLASS] << "Reading input file "
633 << MakeDisplayPath(filename)
636 LyXLex lexrc(layoutTags, sizeof(layoutTags)/sizeof(keyword_item));
639 lexrc.setFile(filename);
640 if (!lexrc.IsOK()) error = true;
643 while (lexrc.IsOK() && !error) {
644 switch(lexrc.lex()) {
649 lexrc.printError("Unknown tag `$$Token'");
653 case LT_OUTPUTTYPE: // output type definition
654 switch(lexrc.lex()) {
659 outputType_ = LINUXDOC;
662 outputType_ = DOCBOOK;
665 outputType_ = LITERATE;
668 lexrc.printError("Unknown output type `$$Token'");
673 case LT_INPUT: // Include file
675 string tmp = LibFileSearch("layouts",
679 if (Read(tmp, true)) {
680 lexrc.printError("Error reading input"
689 string name = subst(lexrc.GetString(),
691 if (hasLayout(name)) {
692 LyXLayout & lay = GetLayout(name);
693 error = do_readStyle(lexrc, lay);
697 if (!(error =do_readStyle(lexrc, lay)))
698 layoutlist.push_back(lay);
702 lexrc.printError("No name given for style: `$$Token'.");
709 string style = subst(lexrc.GetString(),
711 if (!delete_layout(style))
712 lexrc.printError("Cannot delete style"
719 columns_ = lexrc.GetInteger();
724 switch(lexrc.GetInteger()) {
725 case 1: sides_ = OneSide; break;
726 case 2: sides_ = TwoSides; break;
728 lyxerr << "Impossible number of page"
729 " sides, setting to one."
739 pagestyle_ = strip(lexrc.GetString());
743 defaultfont_.lyxRead(lexrc);
744 if (!defaultfont_.resolved()) {
745 lexrc.printError("Warning: defaultfont should "
746 "be fully instantiated!");
747 defaultfont_.realize(LyXFont::ALL_SANE);
752 switch (lexrc.lex()) {
753 case LT_COUNTER_CHAPTER:
754 maxcounter_ = LABEL_COUNTER_CHAPTER;
756 case LT_COUNTER_SECTION:
757 maxcounter_ = LABEL_COUNTER_SECTION;
759 case LT_COUNTER_SUBSECTION:
760 maxcounter_ = LABEL_COUNTER_SUBSECTION;
762 case LT_COUNTER_SUBSUBSECTION:
763 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
765 case LT_COUNTER_PARAGRAPH:
766 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
768 case LT_COUNTER_SUBPARAGRAPH:
769 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
771 case LT_COUNTER_ENUMI:
772 maxcounter_ = LABEL_COUNTER_ENUMI;
774 case LT_COUNTER_ENUMII:
775 maxcounter_ = LABEL_COUNTER_ENUMII;
777 case LT_COUNTER_ENUMIII:
778 maxcounter_ = LABEL_COUNTER_ENUMIII;
780 case LT_COUNTER_ENUMIV:
781 maxcounter_ = LABEL_COUNTER_ENUMIV;
788 secnumdepth_ = lexrc.GetInteger();
793 tocdepth_ = lexrc.GetInteger();
796 // First step to support options
797 case LT_CLASSOPTIONS:
800 while (getout && lexrc.IsOK()) {
801 switch (lexrc.lex()) {
804 opt_fontsize_ = strip(lexrc.GetString());
808 opt_pagestyle_ = strip(lexrc.GetString());
812 options_ = lexrc.GetString();
814 case LT_END: getout = false; break;
816 lexrc.printError("Out of context tag `$$Token'");
824 preamble_ = lexrc.getLongString("EndPreamble");
827 case LT_PROVIDESAMSMATH:
828 if (lexrc.next() && lexrc.GetInteger())
829 provides_ |= amsmath;
832 case LT_PROVIDESMAKEIDX:
833 if (lexrc.next() && lexrc.GetInteger())
834 provides_ |= makeidx;
838 if (lexrc.next() && lexrc.GetInteger())
842 case LT_LEFTMARGIN: // left margin type
844 leftmargin_ = lexrc.GetString();
847 case LT_RIGHTMARGIN: // right margin type
849 rightmargin_ = lexrc.GetString();
853 lexrc.printError("Out of context tag `$$Token'");
858 if (!merge) { // we are at top level here.
859 lyxerr[Debug::TCLASS] << "Finished reading textclass "
860 << MakeDisplayPath(filename)
863 lyxerr[Debug::TCLASS] << "Finished reading input file "
864 << MakeDisplayPath(filename)
871 bool LyXTextClass::hasLayout(string const & name) const
873 for (LayoutList::const_iterator cit = layoutlist.begin();
874 cit != layoutlist.end(); ++cit) {
875 if ((*cit).name() == name)
882 LyXLayout const & LyXTextClass::GetLayout (string const & name) const
884 for (LayoutList::const_iterator cit = layoutlist.begin();
885 cit != layoutlist.end(); ++cit) {
886 if ((*cit).name() == name)
889 Assert(false); // we actually require the name to exist.
890 return layoutlist.front();
894 LyXLayout & LyXTextClass::GetLayout(string const & name)
896 for (LayoutList::iterator it = layoutlist.begin();
897 it != layoutlist.end(); ++it) {
898 if ((*it).name() == name)
901 Assert(false); // we actually require the name to exist.
902 return layoutlist.front();
906 bool LyXTextClass::delete_layout (string const & name)
908 for(LayoutList::iterator it = layoutlist.begin();
909 it != layoutlist.end(); ++it) {
910 if ((*it).name() == name) {
911 layoutlist.erase(it);
919 // Load textclass info if not loaded yet
920 void LyXTextClass::load()
925 string real_file = LibFileSearch("layouts", name_, "layout");
927 if (Read(real_file)) {
928 lyxerr << "Error reading `"
929 << MakeDisplayPath(real_file)
930 << "'\n(Check `" << name_
931 << "')\nCheck your installation and "
932 "try Options/Reconfigure..." << endl;
938 //////////////////////////////////////////
940 // Gets textclass number from name
941 pair<bool, LyXTextClassList::ClassList::size_type>
942 LyXTextClassList::NumberOfClass(string const & textclass) const
944 for (ClassList::const_iterator cit = classlist.begin();
945 cit != classlist.end(); ++cit) {
946 if ((*cit).name() == textclass)
947 return make_pair(true, cit - classlist.begin());
949 return make_pair(false, 0);
953 // Gets layout structure from style number and textclass number
955 LyXTextClassList::Style(LyXTextClassList::ClassList::size_type textclass,
956 LyXTextClass::LayoutList::size_type layout) const
958 classlist[textclass].load();
959 if (layout < classlist[textclass].numLayouts())
960 return classlist[textclass][layout];
961 return classlist[textclass][0];
965 // Gets layout number from name and textclass number
966 pair<bool, LyXTextClass::LayoutList::size_type>
967 LyXTextClassList::NumberOfLayout(LyXTextClassList::ClassList::size_type textclass, string const & name) const
969 classlist[textclass].load();
970 for(unsigned int i = 0; i < classlist[textclass].numLayouts(); ++i) {
971 if (classlist[textclass][i].name() == name)
972 return make_pair(true, i);
975 return make_pair(true, LYX_DUMMY_LAYOUT);
976 return make_pair(false, 0); // not found
980 // Gets a layout (style) name from layout number and textclass number
982 LyXTextClassList::NameOfLayout(LyXTextClassList::ClassList::size_type textclass,
983 LyXTextClass::LayoutList::size_type layout) const
985 static string dummy("dummy");
986 static string end("@@end@@");
987 classlist[textclass].load();
988 if (layout < classlist[textclass].numLayouts())
989 return classlist[textclass][layout].name();
990 else if (layout == LYX_DUMMY_LAYOUT)
997 // Gets a textclass name from number
999 LyXTextClassList::NameOfClass(LyXTextClassList::ClassList::size_type number) const
1001 static string dummy("dummy");
1002 static string end("@@end@@");
1003 if (classlist.size() == 0) {
1004 if (number == 0) return dummy;
1007 if (number < classlist.size())
1008 return classlist[number].name();
1014 // Gets a textclass latexname from number
1016 LyXTextClassList::LatexnameOfClass(LyXTextClassList::ClassList::size_type number) const
1018 static string dummy("dummy");
1019 static string end("@@end@@");
1020 classlist[number].load();
1021 if (classlist.size() == 0) {
1022 if (number == 0) return dummy;
1025 if (number < classlist.size())
1026 return classlist[number].latexname();
1032 // Gets a textclass description from number
1034 LyXTextClassList::DescOfClass(LyXTextClassList::ClassList::size_type number) const
1036 static string dummy("dummy");
1037 static string end("@@end@@");
1038 if (classlist.size() == 0) {
1039 if (number == 0) return dummy;
1042 if (number < classlist.size())
1043 return classlist[number].description();
1049 // Gets a textclass structure from number
1050 LyXTextClass const &
1051 LyXTextClassList::TextClass(LyXTextClassList::ClassList::size_type textclass) const
1053 classlist[textclass].load();
1054 if (textclass < classlist.size())
1055 return classlist[textclass];
1057 return classlist[0];
1061 void LyXTextClassList::Add(LyXTextClass const & t)
1063 classlist.push_back(t);
1067 // used when sorting the textclass list.
1068 class less_textclass_desc {
1070 int operator()(LyXTextClass const & tc1, LyXTextClass const & tc2) {
1071 return tc1.description() < tc2.description();
1076 // Reads LyX textclass definitions according to textclass config file
1077 bool LyXTextClassList::Read ()
1080 string real_file = LibFileSearch("", "textclass.lst");
1081 lyxerr[Debug::TCLASS] << "Reading textclasses from "
1082 << real_file << endl;
1084 if (real_file.empty()) {
1085 lyxerr << "LyXTextClassList::Read: unable to find "
1086 "textclass file `" << MakeDisplayPath(real_file, 1000)
1087 << "'. Exiting." << endl;
1089 WriteAlert(_("LyX wasn't able to find its layout descriptions!"),
1090 _("Check that the file \"textclass.lst\""),
1091 _("is installed correctly. Sorry, has to exit :-("));
1093 // This causes LyX to end... Not a desirable behaviour. Lgb
1094 // What do you propose? That the user gets a file dialog
1095 // and is allowed to hunt for the file? (Asger)
1098 lex.setFile(real_file);
1101 lyxerr << "LyXTextClassList::Read: unable to open "
1102 "textclass file `" << MakeDisplayPath(real_file, 1000)
1103 << "\'\nCheck your installation. LyX can't continue."
1107 bool finished = false;
1108 string fname, clname, desc;
1109 // Parse config-file
1110 while (lex.IsOK() && !finished) {
1111 switch (lex.lex()) {
1112 case LyXLex::LEX_FEOF:
1116 fname = lex.GetString();
1117 lyxerr[Debug::TCLASS] << "Fname: " << fname << endl;
1119 clname = lex.GetString();
1120 lyxerr[Debug::TCLASS]
1121 << "Clname: " << clname << endl;
1123 desc = lex.GetString();
1124 lyxerr[Debug::TCLASS]
1125 << "Desc: " << desc << endl;
1126 // This code is run when we have
1127 // fname, clname and desc
1128 LyXTextClass tmpl(fname,
1132 debugging(Debug::TCLASS)) {
1141 if (classlist.size() == 0) {
1142 lyxerr << "LyXTextClassList::Read: no textclass found!"
1144 WriteAlert(_("LyX wasn't able to find any layout description!"),
1145 _("Check the contents of the file \"textclass.lst\""),
1146 _("Sorry, has to exit :-("));
1149 // Ok everything loaded ok, now sort the list.
1150 sort(classlist.begin(), classlist.end(), less_textclass_desc());
1156 Returns false if this fails
1159 LyXTextClassList::Load (LyXTextClassList::ClassList::size_type number) const
1162 if (number < classlist.size()) {
1163 classlist[number].load();
1164 if (classlist[number].numLayouts() == 0) {