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