]> git.lyx.org Git - lyx.git/blob - src/TextClass.h
f35eb5ee1baf97dd8bb74679ca0d376debfc4e4e
[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 <map>
28 #include <set>
29 #include <vector>
30 #include <list>
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 ///
65 /// A LayoutFile (see LayoutFile.{h,cpp}) represents a *.layout file.
66 /// These are generally static objects---though they can be reloaded 
67 /// from disk via LFUN_LAYOUT_RELOAD, so one should not assume that 
68 /// they will never change.
69 ///
70 /// A DocumentClass (see below) represents the layout information that
71 /// is associated with a given Buffer. These are static, in the sense
72 /// that they will not themselves change, but which DocumentClass is
73 /// associated with a Buffer can change, as modules are loaded and 
74 /// unloaded, for example.
75 ///
76 class TextClass : protected ProtectCopy {
77 public:
78         ///
79         virtual ~TextClass() {};
80         ///////////////////////////////////////////////////////////////////
81         // typedefs
82         ///////////////////////////////////////////////////////////////////
83         /// The individual paragraph layouts comprising the document class
84         // NOTE Do NOT try to make this a container of Layout pointers, e.g.,
85         // std::vector<Layout *>. This will lead to problems. The reason is
86         // that DocumentClass objects are generally created by copying a 
87         // LayoutFile, which serves as a base for the DocumentClass. If the
88         // LayoutList is a container of pointers, then every DocumentClass
89         // that derives from a given LayoutFile (e.g., article) will SHARE
90         // a basic set of layouts. So if one Buffer were to modify a layout
91         // (say, Standard), that would modify that layout for EVERY Buffer
92         // that was based upon the same DocumentClass. (Of course, if you 
93         // really, REALLY want to make LayoutList a vector<Layout *>, then
94         // you can implement custom assignment and copy constructors.)
95         //
96         // NOTE: Layout pointers are directly assigned to paragraphs so a
97         // container that does not invalidate these pointers after insertion
98         // is needed.
99         typedef std::list<Layout> LayoutList;
100         /// The inset layouts available to this class
101         typedef std::map<docstring, InsetLayout> InsetLayouts;
102         ///
103         typedef LayoutList::const_iterator const_iterator;
104
105         ///////////////////////////////////////////////////////////////////
106         // Iterators
107         ///////////////////////////////////////////////////////////////////
108         ///
109         const_iterator begin() const { return layoutlist_.begin(); }
110         ///
111         const_iterator end() const { return layoutlist_.end(); }
112
113
114         ///////////////////////////////////////////////////////////////////
115         // Layout Info
116         ///////////////////////////////////////////////////////////////////
117         ///
118         Layout const & defaultLayout() const;
119         ///
120         docstring const & defaultLayoutName() const;
121         ///
122         bool isDefaultLayout(Layout const &) const;
123         /// 
124         bool isPlainLayout(Layout const &) const;
125         /// returns a special layout for use when we don't really want one,
126         /// e.g., in table cells
127         Layout const & plainLayout() const 
128                         { return operator[](plain_layout_); };
129         /// the name of the plain layout
130         docstring const & plainLayoutName() const 
131                         { return plain_layout_; }
132         /// Enumerate the paragraph styles.
133         size_t layoutCount() const { return layoutlist_.size(); }
134         ///
135         bool hasLayout(docstring const & name) const;
136         ///
137         bool hasInsetLayout(docstring const & name) const;
138         ///
139         Layout const & operator[](docstring const & vname) const;
140         /// Inset layouts of this doc class
141         InsetLayouts const & insetLayouts() const { return insetlayoutlist_; };
142
143         ///////////////////////////////////////////////////////////////////
144         // reading routines
145         ///////////////////////////////////////////////////////////////////
146         /// Enum used with TextClass::read
147         enum ReadType { 
148                 BASECLASS, //>This is a base class, i.e., top-level layout file
149                 MERGE, //>This is a file included in a layout file
150                 MODULE, //>This is a layout module
151                 VALIDATION //>We're just validating
152         };
153         /// return values for read()
154         enum ReturnValues {
155                 OK,
156                 ERROR,
157                 FORMAT_MISMATCH
158         };
159
160         /// Performs the read of the layout file.
161         /// \return true on success.
162         bool read(support::FileName const & filename, ReadType rt = BASECLASS);
163         ///
164         bool read(std::string const & str, ReadType rt = BASECLASS);
165         ///
166         ReturnValues read(Lexer & lex, ReadType rt = BASECLASS);
167         /// validates the layout information passed in str
168         static bool validate(std::string const & str);
169
170         ///////////////////////////////////////////////////////////////////
171         // loading
172         ///////////////////////////////////////////////////////////////////
173         /// Sees to it the textclass structure has been loaded
174         /// This function will search for $classname.layout in default directories
175         /// and an optional path, but if path points to a file, it will be loaded
176         /// directly.
177         bool load(std::string const & path = std::string()) const;
178         /// Has this layout file been loaded yet?
179         /// Overridden by DocumentClass
180         virtual bool loaded() const { return loaded_; }
181
182         ///////////////////////////////////////////////////////////////////
183         // accessors
184         ///////////////////////////////////////////////////////////////////
185         ///
186         std::string const & name() const { return name_; };
187         ///
188         std::string const & description() const {       return description_; };
189         ///
190         std::string const & latexname() const { return latexname_; }
191 protected:
192         /// Protect construction
193         TextClass();
194         ///
195         Layout & operator[](docstring const & name);
196         /** Create an new, very basic layout for this textclass. This is used for
197             the Plain Layout common to all TextClass objects and also, in 
198             DocumentClass, for the creation of new layouts `on the fly' when
199             previously unknown layouts are encountered.
200             \param unknown Set to true if this layout is used to represent an 
201             unknown layout
202          */
203         Layout createBasicLayout(docstring const & name, bool unknown = false) const;
204         
205         ///////////////////////////////////////////////////////////////////
206         // non-const iterators
207         ///////////////////////////////////////////////////////////////////
208         ///
209         typedef LayoutList::iterator iterator;
210         ///
211         iterator begin() { return layoutlist_.begin(); }
212         ///
213         iterator end() { return layoutlist_.end(); }
214
215         ///////////////////////////////////////////////////////////////////
216         // members
217         ///////////////////////////////////////////////////////////////////
218         /// Paragraph styles used in this layout
219         /// This variable is mutable because unknown layouts can be added
220         /// to const textclass.
221         mutable LayoutList layoutlist_;
222         /// Layout file name
223         std::string name_;
224         /// document class name
225         std::string latexname_;
226         /// document class description
227         std::string description_;
228         /// available types of float, eg. figure, algorithm.
229         mutable FloatList floatlist_;
230         /// Types of counters, eg. sections, eqns, figures, avail. in document class.
231         mutable Counters counters_;
232         /// Has this layout file been loaded yet?
233         mutable bool loaded_;
234         /// Is the TeX class available?
235         bool texClassAvail_;
236         ///
237         std::string opt_fontsize_;
238         ///
239         std::string opt_pagestyle_;
240         /// Specific class options
241         std::string options_;
242         ///
243         std::string pagestyle_;
244         ///
245         std::string class_header_;
246         ///
247         docstring defaultlayout_;
248         /// name of plain layout
249         static const docstring plain_layout_;
250         /// preamble text to support layout styles
251         docstring preamble_;
252         /// latex packages loaded by document class.
253         std::set<std::string> provides_;
254         /// latex packages requested by document class.
255         std::set<std::string> requires_;
256         /// default modules wanted by document class
257         std::list<std::string> default_modules_;
258         /// modules provided by document class
259         std::list<std::string> provided_modules_;
260         /// modules excluded by document class
261         std::list<std::string> excluded_modules_;
262         ///
263         unsigned int columns_;
264         ///
265         PageSides sides_;
266         /// header depth to have numbering
267         int secnumdepth_;
268         /// header depth to appear in table of contents
269         int tocdepth_;
270         /// Can be LaTeX, DocBook, etc.
271         OutputType outputType_;
272         /** Base font. The paragraph and layout fonts are resolved against
273             this font. This has to be fully instantiated. Attributes
274             FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
275             extremely illegal.
276         */
277         FontInfo defaultfont_;
278         /// Text that dictates how wide the left margin is on the screen
279         docstring leftmargin_;
280         /// Text that dictates how wide the right margin is on the screen
281         docstring rightmargin_;
282         /// The type of command used to produce a title
283         TitleLatexType titletype_;
284         /// The name of the title command
285         std::string titlename_;
286         /// Input layouts available to this layout
287         InsetLayouts insetlayoutlist_;
288         /// The minimal TocLevel of sectioning layouts
289         int min_toclevel_;
290         /// The maximal TocLevel of sectioning layouts
291         int max_toclevel_;
292 private:
293         ///////////////////////////////////////////////////////////////////
294         // helper routines for reading layout files
295         ///////////////////////////////////////////////////////////////////
296         ///
297         bool deleteLayout(docstring const &);
298         ///
299         bool convertLayoutFormat(support::FileName const &, ReadType);
300         /// \return true for success.
301         bool readStyle(Lexer &, Layout &) const;
302         ///
303         void readOutputType(Lexer &);
304         ///
305         void readTitleType(Lexer &);
306         ///
307         void readMaxCounter(Lexer &);
308         ///
309         void readClassOptions(Lexer &);
310         ///
311         void readCharStyle(Lexer &, std::string const &);
312         ///
313         void readFloat(Lexer &);
314 };
315
316
317 /// A DocumentClass represents the layout information associated with a
318 /// Buffer. It is based upon a LayoutFile, but may be modified by loading
319 /// various Modules. 
320 /// 
321 /// In that regard, DocumentClass objects are "dynamic". But this is really
322 /// an illusion, since DocumentClass objects are not (currently) changed
323 /// when, say, a new Module is loaded. Rather, the old DocumentClass is
324 /// discarded---actually, it's kept around in case something on the cut
325 /// stack needs it---and a new one is created from scratch. 
326 /// 
327 /// In the main LyX code, DocumentClass objects are created only by
328 /// DocumentClassBundle, for which see below.
329 /// 
330 class DocumentClass : public TextClass, boost::noncopyable {
331 public:
332         ///
333         virtual ~DocumentClass() {}
334
335         ///////////////////////////////////////////////////////////////////
336         // Layout Info
337         ///////////////////////////////////////////////////////////////////
338         /// \return true if there is a Layout with latexname lay
339         bool hasLaTeXLayout(std::string const & lay) const;
340         /// A DocumentClass nevers count as loaded, since it is dynamic
341         virtual bool loaded() { return false; }
342         /// \return the layout object of an inset given by name. If the name
343         /// is not found as such, the part after the ':' is stripped off, and
344         /// searched again. In this way, an error fallback can be provided:
345         /// An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
346         /// will invoke the layout object defined by name = 'CharStyle'.
347         /// If that doesn't work either, an empty object returns (shouldn't
348         /// happen).  -- Idea JMarc, comment MV
349         InsetLayout const & insetLayout(docstring const & name) const;
350         /// a plain inset layout for use as a default
351         static InsetLayout const & plainInsetLayout() { return plain_insetlayout_; }
352         /// add a new layout \c name if it does not exist in layoutlist_
353         void addLayoutIfNeeded(docstring const & name) const;
354
355         ///////////////////////////////////////////////////////////////////
356         // accessors
357         ///////////////////////////////////////////////////////////////////
358         /// the list of floats defined in the document class
359         FloatList const & floats() const { return floatlist_; }
360         ///
361         Counters & counters() const { return counters_; }
362         ///
363         std::string const & opt_fontsize() const { return opt_fontsize_; }
364         ///
365         std::string const & opt_pagestyle() const { return opt_pagestyle_; }
366         ///
367         std::string const & options() const { return options_; }
368         ///
369         std::string const & class_header() const { return class_header_; }
370         ///
371         std::string const & pagestyle() const { return pagestyle_; }
372         ///
373         docstring const & preamble() const { return preamble_; }
374         /// is this feature already provided by the class?
375         bool provides(std::string const & p) const;
376         /// features required by the class?
377         std::set<std::string> const & requires() const { return requires_; }
378         ///
379         unsigned int columns() const { return columns_; }
380         ///
381         PageSides sides() const { return sides_; }
382         ///
383         int secnumdepth() const { return secnumdepth_; }
384         ///
385         int tocdepth() const { return tocdepth_; }
386         ///
387         FontInfo const & defaultfont() const { return defaultfont_; }
388         /// Text that dictates how wide the left margin is on the screen
389         docstring const & leftmargin() const { return leftmargin_; }
390         /// Text that dictates how wide the right margin is on the screen
391         docstring const & rightmargin() const { return rightmargin_; }
392         /// The type of command used to produce a title
393         TitleLatexType titletype() const { return titletype_; };
394         /// The name of the title command
395         std::string const & titlename() const { return titlename_; };
396         ///
397         int size() const { return layoutlist_.size(); }
398         /// The minimal TocLevel of sectioning layouts
399         int min_toclevel() const { return min_toclevel_; }
400         /// The maximal TocLevel of sectioning layouts
401         int max_toclevel() const { return max_toclevel_; }
402         /// returns true if the class has a ToC structure
403         bool hasTocLevels() const;
404         /// Can be LaTeX, DocBook, etc.
405         OutputType outputType() const { return outputType_; }
406 protected:
407         /// Constructs a DocumentClass based upon a LayoutFile.
408         DocumentClass(LayoutFile const & tc);
409         /// Needed in tex2lyx
410         DocumentClass() {}
411 private:
412         /// The only class that can create a DocumentClass is
413         /// DocumentClassBundle, which calls the protected constructor.
414         friend class DocumentClassBundle;
415         ///
416         static InsetLayout plain_insetlayout_;
417 };
418
419
420 /// DocumentClassBundle is a container for DocumentClass objects, so that 
421 /// they stay in memory for use by Insets, CutAndPaste, and the like, even
422 /// when their associated Buffers are destroyed.
423 /// FIXME Some sort of garbage collection or reference counting wouldn't
424 /// be a bad idea here. It might be enough to check when a Buffer is closed
425 /// (or makeDocumentClass is called) whether the old DocumentClass is in use 
426 /// anywhere.
427 ///
428 /// This is a singleton class. Its sole instance is accessed via 
429 /// DocumentClassBundle::get().
430 class DocumentClassBundle : boost::noncopyable {
431 public:
432         /// \return Pointer to a new class equal to baseClass
433         DocumentClass & newClass(LayoutFile const & baseClass);
434         /// \return The sole instance of this class.
435         static DocumentClassBundle & get();
436 private:
437         /// control instantiation
438         DocumentClassBundle() {}
439         /// clean up
440         ~DocumentClassBundle();
441         ///
442         std::vector<DocumentClass *> documentClasses_;
443 };
444
445
446 /// convert page sides option to text 1 or 2
447 std::ostream & operator<<(std::ostream & os, PageSides p);
448
449
450 } // namespace lyx
451
452 #endif