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