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