]> git.lyx.org Git - lyx.git/blob - src/TextClass.h
lexer cosmetics
[lyx.git] / src / TextClass.h
1 // -*- C++ -*-
2 /**
3  * \file TextClass.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * Full author contact details are available in file CREDITS.
8  */
9
10 #ifndef TEXTCLASS_H
11 #define TEXTCLASS_H
12
13 #include "ColorCode.h"
14 #include "Counters.h"
15 #include "FloatList.h"
16 #include "FontInfo.h"
17 #include "Layout.h"
18 #include "LayoutEnums.h"
19
20 #include "insets/InsetLayout.h"
21
22 #include "support/docstring.h"
23 #include "support/types.h"
24
25 #include <boost/noncopyable.hpp>
26
27 #include <list>
28 #include <map>
29 #include <set>
30 #include <vector>
31
32 namespace lyx {
33
34 namespace support { class FileName; }
35
36 class Counters;
37 class FloatList;
38 class Layout;
39 class LayoutFile;
40 class Lexer;
41
42 /// Based upon ideas in boost::noncopyable, inheriting from this
43 /// class effectively makes the copy constructor protected but the
44 /// assignment constructor private.
45 class ProtectCopy
46 {
47 protected:
48         ProtectCopy() {}
49         ~ProtectCopy() {}
50         ProtectCopy(const ProtectCopy &) {};
51 private:
52         const ProtectCopy & operator=(const ProtectCopy &);
53 };
54
55
56 /// A TextClass represents a collection of layout information: At the 
57 /// moment, this includes Layout's and InsetLayout's.
58 ///
59 /// There are two major subclasses of TextClass: LayoutFile and
60 /// DocumentClass. These subclasses are what are actually used in LyX.
61 /// Simple TextClass objects are not directly constructed in the main 
62 /// LyX code---the constructor is protected. (That said, in tex2lyx
63 /// there are what amount to simple TextClass objects.)
64 class TextClass : protected ProtectCopy {
65 public:
66         ///
67         virtual ~TextClass() {};
68         ///////////////////////////////////////////////////////////////////
69         // typedefs
70         ///////////////////////////////////////////////////////////////////
71         /// The individual paragraph layouts comprising the document class
72         // NOTE Do NOT try to make this a container of Layout pointers, e.g.,
73         // std::vector<Layout *>. This will lead to problems. The reason is
74         // that DocumentClass objects are generally created by copying a 
75         // LayoutFile, which serves as a base for the DocumentClass. If the
76         // LayoutList is a container of pointers, then every DocumentClass
77         // that derives from a given LayoutFile (e.g., article) will SHARE
78         // a basic set of layouts. So if one Buffer were to modify a layout
79         // (say, Standard), that would modify that layout for EVERY Buffer
80         // that was based upon the same DocumentClass. (Of course, if you 
81         // really, REALLY want to make LayoutList a vector<Layout *>, then
82         // you can implement custom assignment and copy constructors.)
83         typedef std::vector<Layout> LayoutList;
84         /// The inset layouts available to this class
85         typedef std::map<docstring, InsetLayout> InsetLayouts;
86         ///
87         typedef LayoutList::const_iterator const_iterator;
88         
89         ///////////////////////////////////////////////////////////////////
90         // Iterators
91         ///////////////////////////////////////////////////////////////////
92         ///
93         const_iterator begin() const { return layoutlist_.begin(); }
94         ///
95         const_iterator end() const { return layoutlist_.end(); }
96
97
98         ///////////////////////////////////////////////////////////////////
99         // Layout Info
100         ///////////////////////////////////////////////////////////////////
101         ///
102         Layout const & defaultLayout() const;
103         ///
104         docstring const & defaultLayoutName() const;
105         ///
106         bool isDefaultLayout(Layout const &) const;
107         /// 
108         bool isEmptyLayout(Layout const &) const;
109         /// returns a special layout for use when we don't really want one,
110         /// e.g., in table cells
111         Layout const & emptyLayout() const 
112                         { return operator[](emptylayout_); };
113         /// the name of the empty layout
114         docstring const & emptyLayoutName() const 
115                         { return emptylayout_; }
116         /// Enumerate the paragraph styles.
117         size_t layoutCount() const { return layoutlist_.size(); }
118         ///
119         bool hasLayout(docstring const & name) const;
120         ///
121         Layout const & operator[](docstring const & vname) const;
122
123         ///////////////////////////////////////////////////////////////////
124         // reading routines
125         ///////////////////////////////////////////////////////////////////
126         /// Enum used with TextClass::read
127         enum ReadType { 
128                 BASECLASS, //>This is a base class, i.e., top-level layout file
129                 MERGE, //>This is a file included in a layout file
130                 MODULE, //>This is a layout module
131                 VALIDATION //>We're just validating
132         };
133         /// return values for read()
134         enum ReturnValues {
135                 OK,
136                 ERROR,
137                 FORMAT_MISMATCH
138         };
139
140         /// Performs the read of the layout file.
141         /// \return true on success.
142         bool read(support::FileName const & filename, ReadType rt = BASECLASS);
143         ///
144         bool read(std::string const & str, ReadType rt = BASECLASS);
145         ///
146         ReturnValues read(Lexer & lex, ReadType rt = BASECLASS);
147         /// validates the layout information passed in str
148         static bool validate(std::string const & str);
149
150         ///////////////////////////////////////////////////////////////////
151         // loading
152         ///////////////////////////////////////////////////////////////////
153         /// Sees to it the textclass structure has been loaded
154         bool load(std::string const & path = std::string()) const;
155         /// Has this layout file been loaded yet?
156         /// Overridden by DocumentClass
157         virtual bool loaded() const { return loaded_; }
158
159         ///////////////////////////////////////////////////////////////////
160         // accessors
161         ///////////////////////////////////////////////////////////////////
162         ///
163         std::string const & name() const { return name_; };
164         ///
165         std::string const & description() const {       return description_; };
166         ///
167         std::string const & latexname() const { return latexname_; }
168 protected:
169         /// Protect construction
170         TextClass();
171         ///
172         Layout & operator[](docstring const & vname);
173
174         ///////////////////////////////////////////////////////////////////
175         // non-const iterators
176         ///////////////////////////////////////////////////////////////////
177         ///
178         typedef LayoutList::iterator iterator;
179         ///
180         iterator begin() { return layoutlist_.begin(); }
181         ///
182         iterator end() { return layoutlist_.end(); }
183
184         ///////////////////////////////////////////////////////////////////
185         // members
186         ///////////////////////////////////////////////////////////////////
187         /// Paragraph styles used in this layout
188         LayoutList layoutlist_;
189         /// Layout file name
190         std::string name_;
191         /// document class name
192         std::string latexname_;
193         /// document class description
194         std::string description_;
195         /// available types of float, eg. figure, algorithm.
196         mutable FloatList floatlist_;
197         /// Types of counters, eg. sections, eqns, figures, avail. in document class.
198         mutable Counters counters_;
199         /// Has this layout file been loaded yet?
200         mutable bool loaded_;
201         /// Is the TeX class available?
202         bool texClassAvail_;
203         ///
204         std::string opt_fontsize_;
205         ///
206         std::string opt_pagestyle_;
207         /// Specific class options
208         std::string options_;
209         ///
210         std::string pagestyle_;
211         ///
212         std::string class_header_;
213         ///
214         docstring defaultlayout_;
215         /// name of empty layout
216         static const docstring emptylayout_;
217         /// preamble text to support layout styles
218         docstring preamble_;
219         /// latex packages loaded by document class.
220         std::set<std::string> provides_;
221         /// latex packages requested by document class.
222         std::set<std::string> requires_;
223         ///
224         unsigned int columns_;
225         ///
226         PageSides sides_;
227         /// header depth to have numbering
228         int secnumdepth_;
229         /// header depth to appear in table of contents
230         int tocdepth_;
231         /// Can be LaTeX, DocBook, etc.
232         OutputType outputType_;
233         /** Base font. The paragraph and layout fonts are resolved against
234             this font. This has to be fully instantiated. Attributes
235             FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
236             extremely illegal.
237         */
238         FontInfo defaultfont_;
239         /// Text that dictates how wide the left margin is on the screen
240         docstring leftmargin_;
241         /// Text that dictates how wide the right margin is on the screen
242         docstring rightmargin_;
243         /// The type of command used to produce a title
244         TitleLatexType titletype_;
245         /// The name of the title command
246         std::string titlename_;
247         /// Input layouts available to this layout
248         InsetLayouts insetlayoutlist_;
249         /// The minimal TocLevel of sectioning layouts
250         int min_toclevel_;
251         /// The maximal TocLevel of sectioning layouts
252         int max_toclevel_;
253 private:
254         ///////////////////////////////////////////////////////////////////
255         // helper routines for reading layout files
256         ///////////////////////////////////////////////////////////////////
257         ///
258         bool deleteLayout(docstring const &);
259         ///
260         bool convertLayoutFormat(support::FileName const &, ReadType);
261         /// \return true for success.
262         bool readStyle(Lexer &, Layout &);
263         ///
264         void readOutputType(Lexer &);
265         ///
266         void readTitleType(Lexer &);
267         ///
268         void readMaxCounter(Lexer &);
269         ///
270         void readClassOptions(Lexer &);
271         ///
272         void readCharStyle(Lexer &, std::string const &);
273         ///
274         void readFloat(Lexer &);
275         ///
276         void readCounter(Lexer &);
277 };
278
279
280 /// A DocumentClass represents the layout information associated with a
281 /// Buffer. It is based upon a LayoutFile, but may be modified by loading
282 /// various Modules. It is thus a dynamic object, as opposed to LayoutFile's
283 /// which are pretty much static. 
284 ///
285 /// In the main LyX code, DocumentClass objects are created only by
286 /// DocumentClassBundle, for which see below.
287 class DocumentClass : public TextClass, boost::noncopyable {
288 public:
289         ///
290         virtual ~DocumentClass() {}
291
292         ///////////////////////////////////////////////////////////////////
293         // Layout Info
294         ///////////////////////////////////////////////////////////////////
295         /// \return true if there is a Layout with latexname lay
296         bool hasLaTeXLayout(std::string const & lay) const;
297         /// A DocumentClass nevers count as loaded, since it is dynamic
298         virtual bool loaded() { return false; }
299         /// Inset layouts of this doc class
300         InsetLayouts const & insetLayouts() const { return insetlayoutlist_; };
301         /// \return the layout object of an inset given by name. If the name
302         /// is not found as such, the part after the ':' is stripped off, and
303         /// searched again. In this way, an error fallback can be provided:
304         /// An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
305         /// will invoke the layout object defined by name = 'CharStyle'.
306         /// If that doesn't work either, an empty object returns (shouldn't
307         /// happen).  -- Idea JMarc, comment MV
308         InsetLayout const & insetLayout(docstring const & name) const;
309         /// an empty inset layout for use as a default
310         static InsetLayout const & emptyInsetLayout() { return empty_insetlayout_; }
311
312         ///////////////////////////////////////////////////////////////////
313         // accessors
314         ///////////////////////////////////////////////////////////////////
315         /// the list of floats defined in the document class
316         FloatList const & floats() const { return floatlist_; }
317         ///
318         Counters & counters() const { return counters_; }
319         ///
320         std::string const & opt_fontsize() const { return opt_fontsize_; }
321         ///
322         std::string const & opt_pagestyle() const { return opt_pagestyle_; }
323         ///
324         std::string const & options() const { return options_; }
325         ///
326         std::string const & class_header() const { return class_header_; }
327         ///
328         std::string const & pagestyle() const { return pagestyle_; }
329         ///
330         docstring const & preamble() const { return preamble_; }
331         /// is this feature already provided by the class?
332         bool provides(std::string const & p) const;
333         /// features required by the class?
334         std::set<std::string> const & requires() const { return requires_; }
335         ///
336         unsigned int columns() const { return columns_; }
337         ///
338         PageSides sides() const { return sides_; }
339         ///
340         int secnumdepth() const { return secnumdepth_; }
341         ///
342         int tocdepth() const { return tocdepth_; }
343         ///
344         FontInfo const & defaultfont() const { return defaultfont_; }
345         /// Text that dictates how wide the left margin is on the screen
346         docstring const & leftmargin() const { return leftmargin_; }
347         /// Text that dictates how wide the right margin is on the screen
348         docstring const & rightmargin() const { return rightmargin_; }
349         /// The type of command used to produce a title
350         TitleLatexType titletype() const { return titletype_; };
351         /// The name of the title command
352         std::string const & titlename() const { return titlename_; };
353         ///
354         int size() const { return layoutlist_.size(); }
355         /// The minimal TocLevel of sectioning layouts
356         int min_toclevel() const { return min_toclevel_; }
357         /// The maximal TocLevel of sectioning layouts
358         int max_toclevel() const { return max_toclevel_; }
359         /// returns true if the class has a ToC structure
360         bool hasTocLevels() const;
361         /// Can be LaTeX, DocBook, etc.
362         OutputType outputType() const { return outputType_; }
363 protected:
364         /// Constructs a DocumentClass based upon a LayoutFile.
365         DocumentClass(LayoutFile const & tc);
366         /// Needed in tex2lyx
367         DocumentClass() {};
368 private:
369         /// The only class that can create a DocumentClass is
370         /// DocumentClassBundle, which calls the protected constructor.
371         friend class DocumentClassBundle;
372         ///
373         static InsetLayout empty_insetlayout_;
374 };
375
376
377 /// DocumentClassBundle is a container for DocumentClass objects, so that 
378 /// they stay in memory for use by Insets, CutAndPaste, and the like, even
379 /// when their associated Buffers are destroyed.
380 /// FIXME Some sort of garbage collection or reference counting wouldn't
381 /// be a bad idea here. It might be enough to check when a Buffer is closed
382 /// (or makeDocumentClass is called) whether the old DocumentClass is in use 
383 /// anywhere.
384 ///
385 /// This is a singleton class. Its sole instance is accessed via 
386 /// DocumentClassBundle::get().
387 class DocumentClassBundle : boost::noncopyable {
388 public:
389         /// \return Pointer to a new class equal to baseClass
390         DocumentClass & newClass(LayoutFile const & baseClass);
391         /// \return The sole instance of this class.
392         static DocumentClassBundle & get();
393 private:
394         /// control instantiation
395         DocumentClassBundle() {}
396         ///
397         std::list<DocumentClass *> tc_list_;
398 };
399
400
401 /// convert page sides option to text 1 or 2
402 std::ostream & operator<<(std::ostream & os, PageSides p);
403
404
405 } // namespace lyx
406
407 #endif