]> git.lyx.org Git - lyx.git/blob - src/lyxtextclasslist.C
redraw fix 1.
[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 void LyXTextClassList::Add(LyXTextClass const & t)
70 {
71         classlist.push_back(t);
72 }
73
74
75 // used when sorting the textclass list.
76 class less_textclass_desc {
77 public:
78         int operator()(LyXTextClass const & tc1, LyXTextClass const & tc2) {
79                 return tc1.description() < tc2.description();
80         }
81 };
82
83
84 // Reads LyX textclass definitions according to textclass config file
85 bool LyXTextClassList::Read ()
86 {
87         LyXLex lex(0, 0);
88         string real_file = LibFileSearch("", "textclass.lst");
89         lyxerr[Debug::TCLASS] << "Reading textclasses from `"
90                               << real_file << "'" << endl;
91
92         if (real_file.empty()) {
93                 lyxerr << "LyXTextClassList::Read: unable to find "
94                         "textclass file  `" << MakeDisplayPath(real_file, 1000)
95                        << "'. Exiting." << endl;
96
97                 Alert::alert(_("LyX wasn't able to find its layout descriptions!"),
98                            _("Check that the file \"textclass.lst\""),
99                            _("is installed correctly. Sorry, has to exit :-("));
100                 return false;
101                 // This causes LyX to end... Not a desirable behaviour. Lgb
102                 // What do you propose? That the user gets a file dialog
103                 // and is allowed to hunt for the file? (Asger)
104                 // more that we have a layout for minimal.cls statically
105                 // compiled in... (Lgb)
106         }
107
108         if (!lex.setFile(real_file)) {
109                 lyxerr << "LyXTextClassList::Read: "
110                         "lyxlex was not able to set file: "
111                        << real_file << endl;
112         }
113
114         if (!lex.isOK()) {
115                 lyxerr << "LyXTextClassList::Read: unable to open "
116                         "textclass file  `" << MakeDisplayPath(real_file, 1000)
117                        << "'\nCheck your installation. LyX can't continue."
118                        << endl;
119                 return false;
120         }
121
122         bool finished = false;
123         // Parse config-file
124         lyxerr[Debug::TCLASS] << "Starting parsing of textclass.lst" << endl;
125         while (lex.isOK() && !finished) {
126                 lyxerr[Debug::TCLASS] << "\tline by line" << endl;
127                 switch (lex.lex()) {
128                 case LyXLex::LEX_FEOF:
129                         finished = true;
130                         break;
131                 default:
132                         string const fname = lex.getString();
133                         lyxerr[Debug::TCLASS] << "Fname: " << fname << endl;
134                         if (lex.next()) {
135                                 string const clname = lex.getString();
136                                 lyxerr[Debug::TCLASS] << "Clname: " << clname << endl;
137                                 if (lex.next()) {
138                                         string const desc = lex.getString();
139                                         lyxerr[Debug::TCLASS] << "Desc: " << desc << endl;
140                                         // This code is run when we have
141                                         // fname, clname and desc
142                                         LyXTextClass tmpl(fname, clname, desc);
143                                         if (lyxerr.debugging(Debug::TCLASS)) {
144                                                 tmpl.load();
145                                         }
146                                         Add(tmpl);
147                                 }
148                         }
149                 }
150         }
151         lyxerr[Debug::TCLASS] << "End of parsing of textclass.lst" << endl;
152
153         if (classlist.empty()) {
154                 lyxerr << "LyXTextClassList::Read: no textclasses found!"
155                        << endl;
156                 Alert::alert(_("LyX wasn't able to find any layout description!"),
157                            _("Check the contents of the file \"textclass.lst\""),
158                            _("Sorry, has to exit :-("));
159                 return false;
160         }
161         // Ok everything loaded ok, now sort the list.
162         sort(classlist.begin(), classlist.end(), less_textclass_desc());
163         return true;
164 }
165
166
167 // Global variable: textclass table.
168 LyXTextClassList textclasslist;
169
170 // Reads the style files
171 void LyXSetStyle()
172 {
173         lyxerr[Debug::TCLASS] << "LyXSetStyle: parsing configuration...\n";
174
175         if (!textclasslist.Read()) {
176                 lyxerr[Debug::TCLASS] << "LyXSetStyle: an error occured "
177                         "during parsing.\n             Exiting." << endl;
178                 exit(1);
179         }
180
181         lyxerr[Debug::TCLASS] << "LyXSetStyle: configuration parsed." << endl;
182 }