]> git.lyx.org Git - lyx.git/blob - src/TextClass.h
67293610ce67e44ed89c98dee94e7df06c9bf616
[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         };
132         /// Performs the read of the layout file.
133         /// \return true on success.
134         bool read(support::FileName const & filename, ReadType rt = BASECLASS);
135
136         ///////////////////////////////////////////////////////////////////
137         // loading
138         ///////////////////////////////////////////////////////////////////
139         /// Sees to it the textclass structure has been loaded
140         bool load(std::string const & path = std::string()) const;
141         /// Has this layout file been loaded yet?
142         /// Overridden by DocumentClass
143         virtual bool loaded() const { return loaded_; }
144
145         ///////////////////////////////////////////////////////////////////
146         // accessors
147         ///////////////////////////////////////////////////////////////////
148         ///
149         std::string const & name() const { return name_; };
150         ///
151         std::string const & description() const {       return description_; };
152         ///
153         std::string const & latexname() const { return latexname_; }
154 protected:
155         /// Protect construction
156         TextClass();
157         ///
158         Layout & operator[](docstring const & vname);
159
160         ///////////////////////////////////////////////////////////////////
161         // non-const iterators
162         ///////////////////////////////////////////////////////////////////
163         ///
164         typedef LayoutList::iterator iterator;
165         ///
166         iterator begin() { return layoutlist_.begin(); }
167         ///
168         iterator end() { return layoutlist_.end(); }
169
170         ///////////////////////////////////////////////////////////////////
171         // members
172         ///////////////////////////////////////////////////////////////////
173         /// Paragraph styles used in this layout
174         LayoutList layoutlist_;
175         /// Layout file name
176         std::string name_;
177         /// document class name
178         std::string latexname_;
179         /// document class description
180         std::string description_;
181         /// available types of float, eg. figure, algorithm.
182         mutable FloatList floatlist_;
183         /// Types of counters, eg. sections, eqns, figures, avail. in document class.
184         mutable Counters counters_;
185         /// Has this layout file been loaded yet?
186         mutable bool loaded_;
187         /// Is the TeX class available?
188         bool texClassAvail_;
189         ///
190         std::string opt_fontsize_;
191         ///
192         std::string opt_pagestyle_;
193         /// Specific class options
194         std::string options_;
195         ///
196         std::string pagestyle_;
197         ///
198         std::string class_header_;
199         ///
200         docstring defaultlayout_;
201         /// name of empty layout
202         static const docstring emptylayout_;
203         /// preamble text to support layout styles
204         docstring preamble_;
205         /// latex packages loaded by document class.
206         std::set<std::string> provides_;
207         /// latex packages requested by document class.
208         std::set<std::string> requires_;
209         ///
210         unsigned int columns_;
211         ///
212         PageSides sides_;
213         /// header depth to have numbering
214         int secnumdepth_;
215         /// header depth to appear in table of contents
216         int tocdepth_;
217         /// Can be LaTeX, DocBook, etc.
218         OutputType outputType_;
219         /** Base font. The paragraph and layout fonts are resolved against
220             this font. This has to be fully instantiated. Attributes
221             FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
222             extremely illegal.
223         */
224         FontInfo defaultfont_;
225         /// Text that dictates how wide the left margin is on the screen
226         docstring leftmargin_;
227         /// Text that dictates how wide the right margin is on the screen
228         docstring rightmargin_;
229         /// The type of command used to produce a title
230         TitleLatexType titletype_;
231         /// The name of the title command
232         std::string titlename_;
233         /// Input layouts available to this layout
234         InsetLayouts insetlayoutlist_;
235         /// The minimal TocLevel of sectioning layouts
236         int min_toclevel_;
237         /// The maximal TocLevel of sectioning layouts
238         int max_toclevel_;
239 private:
240         ///////////////////////////////////////////////////////////////////
241         // helper routines for reading layout files
242         ///////////////////////////////////////////////////////////////////
243         ///
244         bool deleteLayout(docstring const &);
245         /// \return true for success.
246         bool readStyle(Lexer &, Layout &);
247         ///
248         void readOutputType(Lexer &);
249         ///
250         void readTitleType(Lexer &);
251         ///
252         void readMaxCounter(Lexer &);
253         ///
254         void readClassOptions(Lexer &);
255         ///
256         void readCharStyle(Lexer &, std::string const &);
257         ///
258         void readFloat(Lexer &);
259         ///
260         void readCounter(Lexer &);
261 };
262
263
264 /// A DocumentClass represents the layout information associated with a
265 /// Buffer. It is based upon a LayoutFile, but may be modified by loading
266 /// various Modules. It is thus a dynamic object, as opposed to LayoutFile's
267 /// which are pretty much static. 
268 ///
269 /// In the main LyX code, DocumentClass objects are created only by
270 /// DocumentClassBundle, for which see below.
271 class DocumentClass : public TextClass, boost::noncopyable {
272 public:
273         ///
274         virtual ~DocumentClass() {}
275
276         ///////////////////////////////////////////////////////////////////
277         // Layout Info
278         ///////////////////////////////////////////////////////////////////
279         /// \return true if there is a Layout with latexname lay
280         bool hasLaTeXLayout(std::string const & lay) const;
281         /// A DocumentClass nevers count as loaded, since it is dynamic
282         virtual bool loaded() { return false; }
283         /// Inset layouts of this doc class
284         InsetLayouts const & insetLayouts() const { return insetlayoutlist_; };
285         /// \return the layout object of an inset given by name. If the name
286         /// is not found as such, the part after the ':' is stripped off, and
287         /// searched again. In this way, an error fallback can be provided:
288         /// An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
289         /// will invoke the layout object defined by name = 'CharStyle'.
290         /// If that doesn't work either, an empty object returns (shouldn't
291         /// happen).  -- Idea JMarc, comment MV
292         InsetLayout const & insetLayout(docstring const & name) const;
293         /// an empty inset layout for use as a default
294         static InsetLayout const & emptyInsetLayout() { return empty_insetlayout_; }
295
296         ///////////////////////////////////////////////////////////////////
297         // accessors
298         ///////////////////////////////////////////////////////////////////
299         /// the list of floats defined in the document class
300         FloatList const & floats() const { return floatlist_; }
301         ///
302         Counters & counters() const { return counters_; }
303         ///
304         std::string const & opt_fontsize() const { return opt_fontsize_; }
305         ///
306         std::string const & opt_pagestyle() const { return opt_pagestyle_; }
307         ///
308         std::string const & options() const { return options_; }
309         ///
310         std::string const & class_header() const { return class_header_; }
311         ///
312         std::string const & pagestyle() const { return pagestyle_; }
313         ///
314         docstring const & preamble() const { return preamble_; }
315         /// is this feature already provided by the class?
316         bool provides(std::string const & p) const;
317         /// features required by the class?
318         std::set<std::string> const & requires() const { return requires_; }
319         ///
320         unsigned int columns() const { return columns_; }
321         ///
322         PageSides sides() const { return sides_; }
323         ///
324         int secnumdepth() const { return secnumdepth_; }
325         ///
326         int tocdepth() const { return tocdepth_; }
327         ///
328         FontInfo const & defaultfont() const { return defaultfont_; }
329         /// Text that dictates how wide the left margin is on the screen
330         docstring const & leftmargin() const { return leftmargin_; }
331         /// Text that dictates how wide the right margin is on the screen
332         docstring const & rightmargin() const { return rightmargin_; }
333         /// The type of command used to produce a title
334         TitleLatexType titletype() const { return titletype_; };
335         /// The name of the title command
336         std::string const & titlename() const { return titlename_; };
337         ///
338         int size() const { return layoutlist_.size(); }
339         /// The minimal TocLevel of sectioning layouts
340         int min_toclevel() const { return min_toclevel_; }
341         /// The maximal TocLevel of sectioning layouts
342         int max_toclevel() const { return max_toclevel_; }
343         /// returns true if the class has a ToC structure
344         bool hasTocLevels() const;
345         /// Can be LaTeX, DocBook, etc.
346         OutputType outputType() const { return outputType_; }
347 protected:
348         /// Constructs a DocumentClass based upon a LayoutFile.
349         DocumentClass(LayoutFile const & tc);
350         /// Needed in tex2lyx
351         DocumentClass() {};
352 private:
353         /// The only class that can create a DocumentClass is
354         /// DocumentClassBundle, which calls the protected constructor.
355         friend class DocumentClassBundle;
356         ///
357         static InsetLayout empty_insetlayout_;
358 };
359
360
361 /// DocumentClassBundle is a container for DocumentClass objects, so that 
362 /// they stay in memory for use by Insets, CutAndPaste, and the like, even
363 /// when their associated Buffers are destroyed.
364 /// FIXME Some sort of garbage collection or reference counting wouldn't
365 /// be a bad idea here. It might be enough to check when a Buffer is closed
366 /// (or makeDocumentClass is called) whether the old DocumentClass is in use 
367 /// anywhere.
368 ///
369 /// This is a singleton class. Its sole instance is accessed via 
370 /// DocumentClassBundle::get().
371 class DocumentClassBundle : boost::noncopyable {
372 public:
373         /// \return Pointer to a new class equal to baseClass
374         DocumentClass & newClass(LayoutFile const & baseClass);
375         /// \return The sole instance of this class.
376         static DocumentClassBundle & get();
377 private:
378         /// control instantiation
379         DocumentClassBundle() {}
380         ///
381         std::list<DocumentClass *> tc_list_;
382 };
383
384
385 /// convert page sides option to text 1 or 2
386 std::ostream & operator<<(std::ostream & os, PageSides p);
387
388
389 } // namespace lyx
390
391 #endif