1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ======================================================
15 #pragma implementation
18 #include "lyxtextclasslist.h"
19 #include "lyxtextclass.h"
24 #include "frontends/Alert.h"
26 #include "support/lyxfunctional.h"
27 #include "support/LAssert.h"
28 #include "support/filetools.h"
32 using lyx::layout_type;
33 using lyx::textclass_type;
41 // Gets textclass number from name
42 pair<bool, textclass_type> const
43 LyXTextClassList::NumberOfClass(string const & textclass) const
45 ClassList::const_iterator cit =
46 find_if(classlist.begin(), classlist.end(),
47 lyx::compare_memfun(&LyXTextClass::name, textclass));
48 return cit != classlist.end() ?
49 make_pair(true, textclass_type(cit - classlist.begin())) :
50 make_pair(false, textclass_type(0));
54 // Gets layout structure from style number and textclass number
56 LyXTextClassList::Style(textclass_type textclass,
57 layout_type layout) const
59 classlist[textclass].load();
60 if (layout < classlist[textclass].numLayouts())
61 return classlist[textclass][layout];
62 return classlist[textclass][0];
66 // Gets layout number from name and textclass number
67 pair<bool, layout_type> const
68 LyXTextClassList::NumberOfLayout(textclass_type textclass,
69 string const & name) const
71 classlist[textclass].load();
72 for (unsigned int i = 0; i < classlist[textclass].numLayouts(); ++i) {
73 if (classlist[textclass][i].name() == name)
74 return make_pair(true, i);
76 return make_pair(false, layout_type(0)); // not found
80 // Gets a layout (style) name from layout number and textclass number
82 LyXTextClassList::NameOfLayout(textclass_type textclass,
83 layout_type layout) const
85 static string const dummy("dummy");
86 classlist[textclass].load();
87 if (layout < classlist[textclass].numLayouts())
88 return classlist[textclass][layout].name();
93 // Gets a textclass name from number
95 LyXTextClassList::NameOfClass(textclass_type number) const
97 static string const dummy("dummy");
98 if (classlist.empty()) {
101 lyx::Assert(number < classlist.size());
102 return classlist[number].name();
106 // Gets a textclass latexname from number
108 LyXTextClassList::LatexnameOfClass(textclass_type number) const
110 static string const dummy("dummy");
111 classlist[number].load();
112 if (classlist.empty()) {
115 lyx::Assert(number < classlist.size());
116 return classlist[number].latexname();
120 // Gets a textclass description from number
122 LyXTextClassList::DescOfClass(textclass_type number) const
124 static string const dummy("dummy");
125 if (classlist.empty()) {
128 lyx::Assert(number < classlist.size());
129 return classlist[number].description();
133 // Gets a textclass structure from number
135 LyXTextClassList::TextClass(textclass_type textclass) const
137 classlist[textclass].load();
138 if (textclass < classlist.size())
139 return classlist[textclass];
145 void LyXTextClassList::Add(LyXTextClass const & t)
147 classlist.push_back(t);
151 // used when sorting the textclass list.
152 class less_textclass_desc {
154 int operator()(LyXTextClass const & tc1, LyXTextClass const & tc2) {
155 return tc1.description() < tc2.description();
160 // Reads LyX textclass definitions according to textclass config file
161 bool LyXTextClassList::Read ()
164 string real_file = LibFileSearch("", "textclass.lst");
165 lyxerr[Debug::TCLASS] << "Reading textclasses from `"
166 << real_file << "'" << endl;
168 if (real_file.empty()) {
169 lyxerr << "LyXTextClassList::Read: unable to find "
170 "textclass file `" << MakeDisplayPath(real_file, 1000)
171 << "'. Exiting." << endl;
173 Alert::alert(_("LyX wasn't able to find its layout descriptions!"),
174 _("Check that the file \"textclass.lst\""),
175 _("is installed correctly. Sorry, has to exit :-("));
177 // This causes LyX to end... Not a desirable behaviour. Lgb
178 // What do you propose? That the user gets a file dialog
179 // and is allowed to hunt for the file? (Asger)
180 // more that we have a layout for minimal.cls statically
181 // compiled in... (Lgb)
184 if (!lex.setFile(real_file)) {
185 lyxerr << "LyXTextClassList::Read: "
186 "lyxlex was not able to set file: "
187 << real_file << endl;
191 lyxerr << "LyXTextClassList::Read: unable to open "
192 "textclass file `" << MakeDisplayPath(real_file, 1000)
193 << "'\nCheck your installation. LyX can't continue."
198 bool finished = false;
200 lyxerr[Debug::TCLASS] << "Starting parsing of textclass.lst" << endl;
201 while (lex.isOK() && !finished) {
202 lyxerr[Debug::TCLASS] << "\tline by line" << endl;
204 case LyXLex::LEX_FEOF:
208 string const fname = lex.getString();
209 lyxerr[Debug::TCLASS] << "Fname: " << fname << endl;
211 string const clname = lex.getString();
212 lyxerr[Debug::TCLASS]
213 << "Clname: " << clname << endl;
215 string const desc = lex.getString();
216 lyxerr[Debug::TCLASS]
217 << "Desc: " << desc << endl;
218 // This code is run when we have
219 // fname, clname and desc
220 LyXTextClass tmpl(fname,
224 debugging(Debug::TCLASS)) {
232 lyxerr[Debug::TCLASS] << "End of parsing of textclass.lst" << endl;
234 if (classlist.empty()) {
235 lyxerr << "LyXTextClassList::Read: no textclasses found!"
237 Alert::alert(_("LyX wasn't able to find any layout description!"),
238 _("Check the contents of the file \"textclass.lst\""),
239 _("Sorry, has to exit :-("));
242 // Ok everything loaded ok, now sort the list.
243 sort(classlist.begin(), classlist.end(), less_textclass_desc());
249 Returns false if this fails
251 bool LyXTextClassList::Load(textclass_type number) const
254 if (number < classlist.size()) {
255 classlist[number].load();
256 if (classlist[number].numLayouts() == 0) {
266 // Global variable: textclass table.
267 LyXTextClassList textclasslist;
269 // Reads the style files
272 lyxerr[Debug::TCLASS] << "LyXSetStyle: parsing configuration...\n";
274 if (!textclasslist.Read()) {
275 lyxerr[Debug::TCLASS] << "LyXSetStyle: an error occured "
276 "during parsing.\n Exiting." << endl;
280 lyxerr[Debug::TCLASS] << "LyXSetStyle: configuration parsed." << endl;