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