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