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