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