]> git.lyx.org Git - lyx.git/blob - src/TextClass.h
2c656cd3b0a838f0881d0a9afa963c09def97ae7
[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 "FontInfo.h"
15 #include "LayoutEnums.h"
16 #include "LayoutPtr.h"
17
18 #include "insets/InsetLayout.h"
19
20 #include "support/docstring.h"
21 #include "support/types.h"
22
23 #include <boost/shared_ptr.hpp>
24
25 #include <list>
26 #include <map>
27 #include <set>
28 #include <vector>
29
30 namespace lyx {
31
32 namespace support { class FileName; }
33
34 class Counters;
35 class FloatList;
36 class Layout;
37 class Lexer;
38
39
40 /// A TextClass represents a collection of layout information: At the 
41 /// moment, this includes Layout's and InsetLayout's.
42 ///
43 /// The main function of TextClass objecs is to provide layout information
44 /// to a Buffer, by being the TextClass associated with the BufferParams for
45 /// a given Buffer. This is the object returned by BufferParams::textClass().
46 /// These instances of TextClass do not necessarily correspond just to a 
47 /// *.layout file---that is, to a LyX "document class" or *.layout file---
48 /// since a Buffer's TextClass, though always based upon a "document class" 
49 /// may be modified by loading modules.
50
51 /// That said, some TextClass instances do correspond strictly to document
52 /// classes, that is, to *.layout files. These instances are known in the code
53 /// as "base classes". These are cached in BaseClassList.
54 ///
55 /// Though it does not presently exist, one can imagine an extension of this
56 /// mechanism that would lead to caching of *.module or *.inc files. In that
57 /// case, some TextClass's would just correspond to *.module or *.inc files,
58 /// just as some now correspond to *.layout files.
59 class TextClass {
60 public:
61         /// The individual paragraph layouts comprising the document class
62         typedef std::vector<LayoutPtr> LayoutList;
63         /// The inset layouts available to this class
64         typedef std::map<docstring, InsetLayout> InsetLayouts;
65         /// Construct a layout with default values. Actual values loaded later.
66         explicit TextClass(std::string const & = std::string(),
67                                  std::string const & = std::string(),
68                                  std::string const & = std::string(),
69                                  bool texClassAvail = false);
70         
71         /// check whether the TeX class is available
72         bool isTeXClassAvailable() const;
73
74         /// Enumerate the paragraph styles.
75         size_t layoutCount() const { return layoutlist_.size(); }
76         /// Access the paragraph styles.
77         LayoutPtr const & layout(size_t index) const { return layoutlist_[index]; }
78
79         /// Enum used with TextClass::read
80         enum ReadType { 
81                 BASECLASS, //>This is a base class, i.e., top-level layout file
82                 MERGE, //>This is a file included in a layout file
83                 MODULE //>This is a layout module
84         };
85         /// Performs the read of the layout file.
86         /// \return true on success.
87         bool read(support::FileName const & filename, ReadType rt = BASECLASS);
88         ///
89         void readOutputType(Lexer &);
90         ///
91         void readTitleType(Lexer &);
92         ///
93         void readMaxCounter(Lexer &);
94         ///
95         void readClassOptions(Lexer &);
96         ///
97         void readCharStyle(Lexer &, std::string const &);
98         ///
99         void readFloat(Lexer &);
100         ///
101         void readCounter(Lexer &);
102         ///
103         bool hasLayout(docstring const & name) const;
104
105         ///
106         LayoutPtr const & operator[](docstring const & vname) const;
107
108         /// Sees to that the textclass structure has been loaded
109         bool load(std::string const & path = std::string()) const;
110         /// Has this layout file been loaded yet?
111         /// NOTE This only makes sense when used with "static" TextClass
112         /// objects, e.g., ones that represent files on disk, as opposed
113         /// to ones that can be modified by modules.
114         // FIXME Therefore it should return true only for BaseClass objects,
115         // and false for DocumentClass objects.
116         // Indeed, quite generally, those two sorts of objects should now be
117         // disentangled a bit.
118         bool loaded() const { return loaded_; }
119
120         /// the list of floats defined in the document class
121         FloatList & floats();
122         /// the list of floats defined in the document class
123         FloatList const & floats() const;
124         /// The Counters present in this document class.
125         Counters & counters() const;
126         /// Inset layouts of this doc class
127         InsetLayouts & insetLayouts() const { return insetlayoutlist_; };
128         ///
129         InsetLayout const & insetLayout(docstring const & name) const;
130         ///
131         docstring const & defaultLayoutName() const;
132         ///
133         LayoutPtr const & defaultLayout() const;
134         /// returns a special layout for use when we don't really want one,
135         /// e.g., in table cells
136         LayoutPtr const & emptyLayout() const 
137                         { return operator[](emptylayout_); };
138         ///
139         docstring const & emptyLayoutName() const 
140                         { return emptylayout_; }
141         ///
142         std::string const & name() const;
143         ///
144         docstring const & labelstring() const;
145         ///
146         std::string const & latexname() const;
147         ///
148         std::string const & description() const;
149         ///
150         bool isModular() const { return modular_; }
151         /// Sets the layout as a modular one. There is never any
152         /// need to reset this.
153         void markAsModular() { modular_ = true; }
154         ///
155         std::string const & opt_fontsize() const;
156         ///
157         std::string const & opt_pagestyle() const;
158         ///
159         std::string const & options() const;
160         ///
161         std::string const & class_header() const;
162         ///
163         std::string const & pagestyle() const;
164         ///
165         docstring const & preamble() const;
166
167         /// is this feature already provided by the class?
168         bool provides(std::string const & p) const;
169         /// features required by the class?
170         std::set<std::string> const & requires() const { return requires_; }
171
172         ///
173         unsigned int columns() const;
174         ///
175         PageSides sides() const;
176         ///
177         int secnumdepth() const;
178         ///
179         int tocdepth() const;
180
181         /// Can be LaTeX, DocBook, etc.
182         OutputType outputType() const;
183
184         ///
185         FontInfo const & defaultfont() const;
186
187         /// Text that dictates how wide the left margin is on the screen
188         docstring const & leftmargin() const;
189
190         /// Text that dictates how wide the right margin is on the screen
191         docstring const & rightmargin() const;
192
193         /// The type of command used to produce a title
194         TitleLatexType titletype() const;
195         /// The name of the title command
196         std::string const & titlename() const;
197
198         ///
199         int size() const;
200         /// The minimal TocLevel of sectioning layouts
201         int min_toclevel() const;
202         /// The maximal TocLevel of sectioning layouts
203         int max_toclevel() const;
204         /// returns true if the class has a ToC structure
205         bool hasTocLevels() const;
206         ///
207         static InsetLayout const & emptyInsetLayout() { return empty_insetlayout_; }
208 private:
209         ///
210         bool deleteLayout(docstring const &);
211         /// \return true for success.
212         bool readStyle(Lexer &, Layout &);
213         /// Layout file name
214         std::string name_;
215         /// document class name
216         std::string latexname_;
217         /// document class description
218         std::string description_;
219         /// whether this is a modular layout, i.e., whether it has been
220         /// modified by loading of layout modules.
221         bool modular_;
222         ///
223         std::string opt_fontsize_;
224         ///
225         std::string opt_pagestyle_;
226         /// Specific class options
227         std::string options_;
228         ///
229         std::string pagestyle_;
230         ///
231         std::string class_header_;
232         ///
233         docstring defaultlayout_;
234         /// name of empty layout
235         static const docstring emptylayout_;
236         /// preamble text to support layout styles
237         docstring preamble_;
238         /// latex packages loaded by document class.
239         std::set<std::string> provides_;
240         /// latex packages requested by document class.
241         std::set<std::string> requires_;
242         ///
243         unsigned int columns_;
244         ///
245         PageSides sides_;
246         /// header depth to have numbering
247         int secnumdepth_;
248         /// header depth to appear in table of contents
249         int tocdepth_;
250         /// Can be LaTeX, DocBook, etc.
251         OutputType outputType_;
252         /** Base font. The paragraph and layout fonts are resolved against
253             this font. This has to be fully instantiated. Attributes
254             FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
255             extremely illegal.
256         */
257         FontInfo defaultfont_;
258         /// Text that dictates how wide the left margin is on the screen
259         docstring leftmargin_;
260
261         /// Text that dictates how wide the right margin is on the screen
262         docstring rightmargin_;
263
264         /// The type of command used to produce a title
265         TitleLatexType titletype_;
266         /// The name of the title command
267         std::string titlename_;
268
269         /// Paragraph styles used in this layout
270         LayoutList layoutlist_;
271
272         /// Input layouts available to this layout
273         mutable InsetLayouts insetlayoutlist_;
274
275         /// available types of float, eg. figure, algorithm.
276         boost::shared_ptr<FloatList> floatlist_;
277
278         /// Types of counters, eg. sections, eqns, figures, avail. in document class.
279         boost::shared_ptr<Counters> counters_;
280
281         /// Has this layout file been loaded yet?
282         mutable bool loaded_;
283
284         /// Is the TeX class available?
285         bool texClassAvail_;
286
287         /// The minimal TocLevel of sectioning layouts
288         int min_toclevel_;
289         /// The maximal TocLevel of sectioning layouts
290         int max_toclevel_;
291         ///
292         static InsetLayout empty_insetlayout_;
293 };
294
295
296 /// This class amounts to little more than a `strong typedef'.
297 /// Its purpose is to control the creation of TextClass objects
298 /// within the DocumentClassBundle. 
299 /// These TextClasses represent the layout information that is 
300 /// associated with a given buffer.
301 class DocumentClass : public TextClass {
302 private:
303         /// Constructs a DocumentClass based upon a TextClass.
304         DocumentClass(TextClass const & tc);
305         /// The only class that can create a DocumentClass is
306         /// DocumentClassBundle, which calls the private constructor.
307         friend class DocumentClassBundle;
308 };
309
310
311 /// This is simply a container for the text classes generated when modules
312 /// are read, so that they stay in memory for use by Insets, CutAndPaste,
313 /// and the like. 
314 /// FIXME Some sort of garbage collection or reference counting wouldn't
315 /// be a bad idea here. It might be enough to check when a Buffer is closed
316 /// (or makeDocumentClass is called) whether the old DocumentClass is in use 
317 /// anywhere.
318 ///
319 /// This is a singleton class. Its sole instance is accessed via 
320 /// DocumentClassBundle::get().
321 class DocumentClassBundle {
322 public:
323         /// \return Pointer to a new class equal to baseClass
324         DocumentClass & newClass(TextClass const & baseClass);
325         /// \return The sole instance of this class.
326         static DocumentClassBundle & get();
327 private:
328         /// control instantiation
329         DocumentClassBundle() {}
330         /// noncopyable
331         DocumentClassBundle(DocumentClassBundle const &);
332         ///
333         std::list<DocumentClass> tc_list_;
334 };
335
336
337 /// convert page sides option to text 1 or 2
338 std::ostream & operator<<(std::ostream & os, PageSides p);
339
340
341 } // namespace lyx
342
343 #endif