]> git.lyx.org Git - lyx.git/blob - src/TextClass.h
Remove TextClassPtr without losing the type safety it provided.
[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 Should we have a modular_ variable, set to true when
115         // we load a module, that would force false to be returned here?
116         bool loaded() const { return loaded_; }
117
118         /// the list of floats defined in the document class
119         FloatList & floats();
120         /// the list of floats defined in the document class
121         FloatList const & floats() const;
122         /// The Counters present in this document class.
123         Counters & counters() const;
124         /// Inset layouts of this doc class
125         InsetLayouts & insetLayouts() const { return insetlayoutlist_; };
126         ///
127         InsetLayout const & insetLayout(docstring const & name) const;
128         ///
129         docstring const & defaultLayoutName() const;
130         ///
131         LayoutPtr const & defaultLayout() const;
132         /// returns a special layout for use when we don't really want one,
133         /// e.g., in table cells
134         LayoutPtr const & emptyLayout() const 
135                         { return operator[](emptylayout_); };
136         ///
137         docstring const & emptyLayoutName() const 
138                         { return emptylayout_; }
139         ///
140         std::string const & name() const;
141         ///
142         docstring const & labelstring() const;
143         ///
144         std::string const & latexname() const;
145         ///
146         std::string const & description() const;
147         ///
148         bool isModular() const { return modular_; }
149         /// Sets the layout as a modular one. There is never any
150         /// need to reset this.
151         void markAsModular() { modular_ = true; }
152         ///
153         std::string const & opt_fontsize() const;
154         ///
155         std::string const & opt_pagestyle() const;
156         ///
157         std::string const & options() const;
158         ///
159         std::string const & class_header() const;
160         ///
161         std::string const & pagestyle() const;
162         ///
163         docstring const & preamble() const;
164
165         /// is this feature already provided by the class?
166         bool provides(std::string const & p) const;
167         /// features required by the class?
168         std::set<std::string> const & requires() const { return requires_; }
169
170         ///
171         unsigned int columns() const;
172         ///
173         PageSides sides() const;
174         ///
175         int secnumdepth() const;
176         ///
177         int tocdepth() const;
178
179         /// Can be LaTeX, DocBook, etc.
180         OutputType outputType() const;
181
182         ///
183         FontInfo const & defaultfont() const;
184
185         /// Text that dictates how wide the left margin is on the screen
186         docstring const & leftmargin() const;
187
188         /// Text that dictates how wide the right margin is on the screen
189         docstring const & rightmargin() const;
190
191         /// The type of command used to produce a title
192         TitleLatexType titletype() const;
193         /// The name of the title command
194         std::string const & titlename() const;
195
196         ///
197         int size() const;
198         /// The minimal TocLevel of sectioning layouts
199         int min_toclevel() const;
200         /// The maximal TocLevel of sectioning layouts
201         int max_toclevel() const;
202         /// returns true if the class has a ToC structure
203         bool hasTocLevels() const;
204         ///
205         static InsetLayout const & emptyInsetLayout() { return empty_insetlayout_; }
206 private:
207         ///
208         bool deleteLayout(docstring const &);
209         /// \return true for success.
210         bool readStyle(Lexer &, Layout &);
211         /// Layout file name
212         std::string name_;
213         /// document class name
214         std::string latexname_;
215         /// document class description
216         std::string description_;
217         /// whether this is a modular layout, i.e., whether it has been
218         /// modified by loading of layout modules.
219         bool modular_;
220         ///
221         std::string opt_fontsize_;
222         ///
223         std::string opt_pagestyle_;
224         /// Specific class options
225         std::string options_;
226         ///
227         std::string pagestyle_;
228         ///
229         std::string class_header_;
230         ///
231         docstring defaultlayout_;
232         /// name of empty layout
233         static const docstring emptylayout_;
234         /// preamble text to support layout styles
235         docstring preamble_;
236         /// latex packages loaded by document class.
237         std::set<std::string> provides_;
238         /// latex packages requested by document class.
239         std::set<std::string> requires_;
240         ///
241         unsigned int columns_;
242         ///
243         PageSides sides_;
244         /// header depth to have numbering
245         int secnumdepth_;
246         /// header depth to appear in table of contents
247         int tocdepth_;
248         /// Can be LaTeX, DocBook, etc.
249         OutputType outputType_;
250         /** Base font. The paragraph and layout fonts are resolved against
251             this font. This has to be fully instantiated. Attributes
252             FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
253             extremely illegal.
254         */
255         FontInfo defaultfont_;
256         /// Text that dictates how wide the left margin is on the screen
257         docstring leftmargin_;
258
259         /// Text that dictates how wide the right margin is on the screen
260         docstring rightmargin_;
261
262         /// The type of command used to produce a title
263         TitleLatexType titletype_;
264         /// The name of the title command
265         std::string titlename_;
266
267         /// Paragraph styles used in this layout
268         LayoutList layoutlist_;
269
270         /// Input layouts available to this layout
271         mutable InsetLayouts insetlayoutlist_;
272
273         /// available types of float, eg. figure, algorithm.
274         boost::shared_ptr<FloatList> floatlist_;
275
276         /// Types of counters, eg. sections, eqns, figures, avail. in document class.
277         boost::shared_ptr<Counters> counters_;
278
279         /// Has this layout file been loaded yet?
280         mutable bool loaded_;
281
282         /// Is the TeX class available?
283         bool texClassAvail_;
284
285         /// The minimal TocLevel of sectioning layouts
286         int min_toclevel_;
287         /// The maximal TocLevel of sectioning layouts
288         int max_toclevel_;
289         ///
290         static InsetLayout empty_insetlayout_;
291 };
292
293
294 /// This class amounts to little more than a `strong typedef'.
295 /// Its purpose is to control the creation of TextClass objects
296 /// within the DocumentClassBundle. 
297 /// These TextClasses represent the layout information that is 
298 /// associated with a given buffer.
299 class DocumentClass : public TextClass {
300 private:
301         /// Constructs a DocumentClass based upon a TextClass.
302         DocumentClass(TextClass const & tc);
303         /// The only class that can create a DocumentClass is
304         /// DocumentClassBundle, which calls the private constructor.
305         friend class DocumentClassBundle;
306 };
307
308
309 /// This is simply a container for the text classes generated when modules
310 /// are read, so that they stay in memory for use by Insets, CutAndPaste,
311 /// and the like. 
312 /// FIXME Some sort of garbage collection or reference counting wouldn't
313 /// be a bad idea here. It might be enough to check when a Buffer is closed
314 /// (or makeDocumentClass is called) whether the old DocumentClass is in use 
315 /// anywhere.
316 ///
317 /// This is a singleton class. Its sole instance is accessed via 
318 /// DocumentClassBundle::get().
319 class DocumentClassBundle {
320 public:
321         /// \return Pointer to a new class equal to baseClass
322         DocumentClass & newClass(TextClass const & baseClass);
323         /// \return The sole instance of this class.
324         static DocumentClassBundle & get();
325 private:
326         /// control instantiation
327         DocumentClassBundle() {}
328         /// noncopyable
329         DocumentClassBundle(DocumentClassBundle const &);
330         ///
331         std::list<DocumentClass> tc_list_;
332 };
333
334
335 /// convert page sides option to text 1 or 2
336 std::ostream & operator<<(std::ostream & os, PageSides p);
337
338
339 } // namespace lyx
340
341 #endif