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