]> git.lyx.org Git - lyx.git/blob - src/lyxtextclasslist.C
layout as string
[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]
133                                         << "Clname: " << clname << endl;
134                                 if (lex.next()) {
135                                               string const desc = lex.getString();
136                                               lyxerr[Debug::TCLASS]
137                                                       << "Desc: " << desc << endl;
138                                               // This code is run when we have
139                                               // fname, clname and desc
140                                               LyXTextClass tmpl(fname,
141                                                                 clname,
142                                                                 desc);
143                                               if (lyxerr.
144                                                   debugging(Debug::TCLASS)) {
145                                                       tmpl.load();
146                                               }
147                                               Add (tmpl);
148                                 }
149                         }
150                 }
151         }
152         lyxerr[Debug::TCLASS] << "End of parsing of textclass.lst" << endl;
153
154         if (classlist.empty()) {
155                 lyxerr << "LyXTextClassList::Read: no textclasses found!"
156                        << endl;
157                 Alert::alert(_("LyX wasn't able to find any layout description!"),
158                            _("Check the contents of the file \"textclass.lst\""),
159                            _("Sorry, has to exit :-("));
160                 return false;
161         }
162         // Ok everything loaded ok, now sort the list.
163         sort(classlist.begin(), classlist.end(), less_textclass_desc());
164         return true;
165 }
166
167
168 // Global variable: textclass table.
169 LyXTextClassList textclasslist;
170
171 // Reads the style files
172 void LyXSetStyle()
173 {
174         lyxerr[Debug::TCLASS] << "LyXSetStyle: parsing configuration...\n";
175         
176         if (!textclasslist.Read()) {
177                 lyxerr[Debug::TCLASS] << "LyXSetStyle: an error occured "
178                         "during parsing.\n             Exiting." << endl;
179                 exit(1);
180         }
181
182         lyxerr[Debug::TCLASS] << "LyXSetStyle: configuration parsed." << endl;
183 }