]> git.lyx.org Git - lyx.git/blob - src/lyxtextclasslist.C
*** empty log message ***
[lyx.git] / src / lyxtextclasslist.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-2001 The LyX Team.
8  *
9  * ======================================================
10  */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "lyxtextclasslist.h"
19 #include "lyxtextclass.h"
20 #include "debug.h"
21 #include "lyxlex.h"
22 #include "gettext.h"
23
24 #include "frontends/Alert.h"
25
26 #include "support/lyxfunctional.h"
27 #include "support/LAssert.h"
28 #include "support/filetools.h"
29
30 #include <utility>
31
32 using lyx::textclass_type;
33 using std::pair;
34 using std::make_pair;
35 using std::endl;
36 using std::find_if;
37 using std::sort;
38
39
40 // Gets textclass number from name
41 pair<bool, textclass_type> const
42 LyXTextClassList::NumberOfClass(string const & textclass) const
43 {
44         ClassList::const_iterator cit =
45                 find_if(classlist.begin(), classlist.end(),
46                         lyx::compare_memfun(&LyXTextClass::name, textclass));
47         return cit != classlist.end() ?
48                 make_pair(true, textclass_type(cit - classlist.begin())) :
49                 make_pair(false, textclass_type(0));
50 }
51
52
53 // Gets a textclass structure from number
54 LyXTextClass const &
55 LyXTextClassList::operator[](textclass_type textclass) const
56 {
57         classlist[textclass].load();
58         if (textclass < classlist.size())
59                 return classlist[textclass];
60         else
61                 return classlist[0];
62 }
63
64
65 void LyXTextClassList::Add(LyXTextClass const & t)
66 {
67         classlist.push_back(t);
68 }
69
70
71 // used when sorting the textclass list.
72 class less_textclass_desc {
73 public:
74         int operator()(LyXTextClass const & tc1, LyXTextClass const & tc2) {
75                 return tc1.description() < tc2.description();
76         }
77 };
78
79
80 // Reads LyX textclass definitions according to textclass config file
81 bool LyXTextClassList::Read ()
82 {
83         LyXLex lex(0, 0);
84         string real_file = LibFileSearch("", "textclass.lst");
85         lyxerr[Debug::TCLASS] << "Reading textclasses from `"
86                               << real_file << "'" << endl;
87
88         if (real_file.empty()) {
89                 lyxerr << "LyXTextClassList::Read: unable to find "
90                         "textclass file  `" << MakeDisplayPath(real_file, 1000)
91                        << "'. Exiting." << endl;
92
93                 Alert::alert(_("LyX wasn't able to find its layout descriptions!"),
94                            _("Check that the file \"textclass.lst\""),
95                            _("is installed correctly. Sorry, has to exit :-("));
96                 return false;
97                 // This causes LyX to end... Not a desirable behaviour. Lgb
98                 // What do you propose? That the user gets a file dialog
99                 // and is allowed to hunt for the file? (Asger)
100                 // more that we have a layout for minimal.cls statically
101                 // compiled in... (Lgb)
102         }
103
104         if (!lex.setFile(real_file)) {
105                 lyxerr << "LyXTextClassList::Read: "
106                         "lyxlex was not able to set file: "
107                        << real_file << endl;
108         }
109
110         if (!lex.isOK()) {
111                 lyxerr << "LyXTextClassList::Read: unable to open "
112                         "textclass file  `" << MakeDisplayPath(real_file, 1000)
113                        << "'\nCheck your installation. LyX can't continue."
114                        << endl;
115                 return false;
116         }
117
118         bool finished = false;
119         // Parse config-file
120         lyxerr[Debug::TCLASS] << "Starting parsing of textclass.lst" << endl;
121         while (lex.isOK() && !finished) {
122                 lyxerr[Debug::TCLASS] << "\tline by line" << endl;
123                 switch (lex.lex()) {
124                 case LyXLex::LEX_FEOF:
125                         finished = true;
126                         break;
127                 default:
128                         string const fname = lex.getString();
129                         lyxerr[Debug::TCLASS] << "Fname: " << fname << endl;
130                         if (lex.next()) {
131                                 string const clname = lex.getString();
132                                 lyxerr[Debug::TCLASS] << "Clname: " << clname << endl;
133                                 if (lex.next()) {
134                                         string const desc = lex.getString();
135                                         lyxerr[Debug::TCLASS] << "Desc: " << desc << endl;
136                                         // This code is run when we have
137                                         // fname, clname and desc
138                                         LyXTextClass tmpl(fname, clname, desc);
139                                         if (lyxerr.debugging(Debug::TCLASS)) {
140                                                 tmpl.load();
141                                         }
142                                         Add(tmpl);
143                                 }
144                         }
145                 }
146         }
147         lyxerr[Debug::TCLASS] << "End of parsing of textclass.lst" << endl;
148
149         if (classlist.empty()) {
150                 lyxerr << "LyXTextClassList::Read: no textclasses found!"
151                        << endl;
152                 Alert::alert(_("LyX wasn't able to find any layout description!"),
153                            _("Check the contents of the file \"textclass.lst\""),
154                            _("Sorry, has to exit :-("));
155                 return false;
156         }
157         // Ok everything loaded ok, now sort the list.
158         sort(classlist.begin(), classlist.end(), less_textclass_desc());
159         return true;
160 }
161
162
163 // Global variable: textclass table.
164 LyXTextClassList textclasslist;
165
166 // Reads the style files
167 void LyXSetStyle()
168 {
169         lyxerr[Debug::TCLASS] << "LyXSetStyle: parsing configuration...\n";
170
171         if (!textclasslist.Read()) {
172                 lyxerr[Debug::TCLASS] << "LyXSetStyle: an error occured "
173                         "during parsing.\n             Exiting." << endl;
174                 exit(1);
175         }
176
177         lyxerr[Debug::TCLASS] << "LyXSetStyle: configuration parsed." << endl;
178 }