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