]> git.lyx.org Git - lyx.git/blob - src/TextClass.h
Force a Buffer * argument to math insets constructor
[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 "LayoutEnums.h"
19 #include "LayoutModuleList.h"
20
21 #include "insets/InsetLayout.h"
22
23 #include "support/docstring.h"
24 #include "support/types.h"
25
26 #include <list>
27 #include <map>
28 #include <set>
29 #include <string>
30 #include <vector>
31
32 #ifdef ERROR
33 #undef ERROR
34 #endif
35
36 namespace lyx {
37
38 namespace support { class FileName; }
39
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                 CITE_ENGINE, //>This is a cite engine
153                 VALIDATION //>We're just validating
154         };
155         /// return values for read()
156         enum ReturnValues {
157                 OK,
158                 OK_OLDFORMAT,
159                 ERROR,
160                 FORMAT_MISMATCH
161         };
162
163         /// Performs the read of the layout file.
164         /// \return true on success.
165         // FIXME Should return ReturnValues....
166         bool read(support::FileName const & filename, ReadType rt = BASECLASS);
167         ///
168         ReturnValues read(std::string const & str, ReadType rt = MODULE);
169         ///
170         ReturnValues read(Lexer & lex, ReadType rt = BASECLASS);
171         /// validates the layout information passed in str
172         static ReturnValues validate(std::string const & str);
173         /// \return the conversion of \param str to the latest layout format
174         /// compatible with the lyx format.
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 & path() const { return path_; }
196         ///
197         std::string const & category() const { return category_; }
198         ///
199         std::string const & description() const { return description_; }
200         ///
201         std::string const & latexname() const { return latexname_; }
202         ///
203         std::string const & prerequisites(std::string const & sep = "\n\t") const;
204         /// Can be LaTeX, DocBook, etc.
205         OutputType outputType() const { return outputType_; }
206         /// Can be latex, docbook ... (the name of a format)
207         std::string outputFormat() const { return outputFormat_; }
208         /// Does this class redefine the output format?
209         bool hasOutputFormat() const { return has_output_format_; }
210         /// Return the non-localised names for the toc types.
211         std::map<std::string, docstring> const &
212                 outlinerNames() const { return outliner_names_; }
213         /// \returns Layout named \p name if it exists, otherwise 0
214         Layout const * getLayout(docstring const & name) const;
215         /// \returns Layout named \p name if it exists, otherwise 0
216         Layout * getLayout(docstring const & name);
217
218 protected:
219         /// Protect construction
220         TextClass();
221         ///
222         Layout & operator[](docstring const & name);
223         /** Create an new, very basic layout for this textclass. This is used for
224             the Plain Layout common to all TextClass objects and also, in
225             DocumentClass, for the creation of new layouts `on the fly' when
226             previously unknown layouts are encountered.
227             \param unknown Set to true if this layout is used to represent an
228             unknown layout
229          */
230         Layout createBasicLayout(docstring const & name, bool unknown = false) const;
231
232         ///////////////////////////////////////////////////////////////////
233         // non-const iterators
234         ///////////////////////////////////////////////////////////////////
235         ///
236         typedef LayoutList::iterator iterator;
237         ///
238         iterator begin() { return layoutlist_.begin(); }
239         ///
240         iterator end() { return layoutlist_.end(); }
241
242         ///////////////////////////////////////////////////////////////////
243         // members
244         ///////////////////////////////////////////////////////////////////
245         /// Paragraph styles used in this layout
246         /// This variable is mutable because unknown layouts can be added
247         /// to const textclass.
248         mutable LayoutList layoutlist_;
249         /// Layout file name
250         std::string name_;
251         /// Layout file path (empty for system layout files)
252         std::string path_;
253         /// Class category
254         std::string category_;
255         /// document class name
256         std::string latexname_;
257         /// document class description
258         std::string description_;
259         /// available types of float, eg. figure, algorithm.
260         mutable FloatList floatlist_;
261         /// Types of counters, eg. sections, eqns, figures, avail. in document class.
262         mutable Counters counters_;
263         /// Has this layout file been loaded yet?
264         mutable bool loaded_;
265         /// Is the TeX class available?
266         bool tex_class_avail_;
267         /// document class prerequisites
268         mutable std::string prerequisites_;
269         /// The possible cite engine types
270         std::string opt_enginetype_;
271         /// The cite framework (bibtex, biblatex)
272         std::string citeframework_;
273         ///
274         std::string opt_fontsize_;
275         ///
276         std::string opt_pagesize_;
277         ///
278         std::string opt_pagestyle_;
279         /// Specific class options
280         std::string options_;
281         /// Format of the fontsize option
282         std::string fontsize_format_;
283         /// Default page size
284         std::string pagesize_;
285         /// Format of the papersize option
286         std::string pagesize_format_;
287         ///
288         std::string pagestyle_;
289         ///
290         std::string tablestyle_;
291         ///
292         std::string class_header_;
293         ///
294         docstring defaultlayout_;
295         /// name of plain layout
296         static const docstring plain_layout_;
297         /// preamble text to support layout styles
298         docstring preamble_;
299         /// same, but for HTML output
300         /// this is output as is to the header
301         docstring htmlpreamble_;
302         /// same, but specifically for CSS information
303         docstring htmlstyles_;
304         /// the paragraph style to use for TOCs, bibliography, etc.
305         mutable docstring html_toc_section_;
306         /// root element when exporting as DocBook
307         std::string docbookroot_;
308         /// whether this root element does not accept text without a section (i.e. the first text that is met in LyX must
309         /// be considered as the abstract if this is true); this text must be output within <info> and <abstract>
310         bool docbookforceabstract_;
311         /// latex packages loaded by document class.
312         std::set<std::string> provides_;
313         /// latex packages requested by document class.
314         std::set<std::string> required_;
315         ///
316         std::map<std::string, std::string> package_options_;
317         /// default modules wanted by document class
318         LayoutModuleList default_modules_;
319         /// modules provided by document class
320         LayoutModuleList provided_modules_;
321         /// modules excluded by document class
322         LayoutModuleList excluded_modules_;
323         ///
324         unsigned int columns_;
325         ///
326         PageSides sides_;
327         /// header depth to have numbering
328         int secnumdepth_;
329         /// header depth to appear in table of contents
330         int tocdepth_;
331         /// Can be LaTeX, DocBook, etc.
332         OutputType outputType_;
333         /// Can be latex, docbook ... (the name of a format)
334         std::string outputFormat_;
335         /// Does this class redefine the output format?
336         bool has_output_format_;
337         /** Base font. The paragraph and layout fonts are resolved against
338             this font. This has to be fully instantiated. Attributes
339             FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
340             extremely illegal.
341         */
342         FontInfo defaultfont_;
343         /// Text that dictates how wide the left margin is on the screen
344         docstring leftmargin_;
345         /// Text that dictates how wide the right margin is on the screen
346         docstring rightmargin_;
347         /// The type of command used to produce a title
348         TitleLatexType titletype_;
349         /// The name of the title command
350         std::string titlename_;
351         /// Input layouts available to this layout
352         InsetLayouts insetlayoutlist_;
353         /// The minimal TocLevel of sectioning layouts
354         int min_toclevel_;
355         /// The maximal TocLevel of sectioning layouts
356         int max_toclevel_;
357         /// Citation formatting information
358         std::map<CiteEngineType, std::map<std::string, std::string> > cite_formats_;
359         /// Citation macros
360         std::map<CiteEngineType, std::map<std::string, std::string> > cite_macros_;
361         /// The default BibTeX bibliography style file
362         std::map<std::string, std::string> cite_default_biblio_style_;
363         /// Citation command aliases
364         std::map<std::string, std::string> cite_command_aliases_;
365         /// The maximum number of citations before "et al."
366         size_t maxcitenames_;
367         /// Whether full author lists are supported
368         bool cite_full_author_list_;
369         /// The possible citation styles
370         std::map<CiteEngineType, std::vector<CitationStyle> > cite_styles_;
371         /// Class-added citation styles
372         std::map<CiteEngineType, std::vector<CitationStyle> > class_cite_styles_;
373         ///
374         std::map<std::string, docstring> outliner_names_;
375         /// Does this class put the bibliography to toc by itself?
376         bool bibintoc_;
377 private:
378         ///////////////////////////////////////////////////////////////////
379         // helper routines for reading layout files
380         ///////////////////////////////////////////////////////////////////
381         ///
382         bool deleteLayout(docstring const &);
383         ///
384         bool deleteInsetLayout(docstring const &);
385         ///
386         bool convertLayoutFormat(support::FileName const &, ReadType);
387         /// Reads the layout file without running layout2layout.
388         ReturnValues readWithoutConv(support::FileName const & filename, ReadType rt);
389         /// \return true for success.
390         bool readStyle(Lexer &, Layout &, ReadType) const;
391         ///
392         void readOutputType(Lexer &);
393         ///
394         void readTitleType(Lexer &);
395         ///
396         void readMaxCounter(Lexer &);
397         ///
398         void readClassOptions(Lexer &);
399         ///
400         void readCharStyle(Lexer &, std::string const &);
401         ///
402         bool readFloat(Lexer &);
403         ///
404         std::vector<CitationStyle> const & getCiteStyles(CiteEngineType const &) const;
405         ///
406         bool readCiteEngine(Lexer &, ReadType, bool const add = false);
407         ///
408         int readCiteEngineType(Lexer &) const;
409         ///
410         bool readCiteFormat(Lexer &, ReadType);
411         ///
412         bool readOutlinerName(Lexer &);
413 };
414
415
416 /// A DocumentClass represents the layout information associated with a
417 /// Buffer. It is based upon a LayoutFile, but may be modified by loading
418 /// various Modules.
419 ///
420 /// In that regard, DocumentClass objects are "dynamic". But this is really
421 /// an illusion, since DocumentClass objects are not (currently) changed
422 /// when, say, a new Module is loaded. Rather, the old DocumentClass is
423 /// discarded---actually, it will be kept around if something on the cut
424 /// stack needs it---and a new one is created from scratch.
425 class DocumentClass : public TextClass {
426 public:
427         ///
428         virtual ~DocumentClass() {}
429
430         ///////////////////////////////////////////////////////////////////
431         // Layout Info
432         ///////////////////////////////////////////////////////////////////
433         /// \return true if there is a Layout with latexname lay
434         bool hasLaTeXLayout(std::string const & lay) const;
435         /// A DocumentClass nevers count as loaded, since it is dynamic
436         bool loaded() const override { return false; }
437         /// \return the layout object of an inset given by name. If the name
438         /// is not found as such, the part after the ':' is stripped off, and
439         /// searched again. In this way, an error fallback can be provided:
440         /// An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
441         /// will invoke the layout object defined by name = 'CharStyle'.
442         /// If that doesn't work either, an empty object returns (shouldn't
443         /// happen).  -- Idea JMarc, comment MV
444         InsetLayout const & insetLayout(docstring const & name) const;
445         /// a plain inset layout for use as a default
446         static InsetLayout const & plainInsetLayout();
447         /// add a new layout \c name if it does not exist in layoutlist_
448         /// \return whether we had to add one.
449         bool addLayoutIfNeeded(docstring const & name) const;
450         /// Forced layouts in layout file syntax
451         std::string forcedLayouts() const;
452
453         ///////////////////////////////////////////////////////////////////
454         // accessors
455         ///////////////////////////////////////////////////////////////////
456         /// the list of floats defined in the document class
457         FloatList const & floats() const { return floatlist_; }
458         ///
459         Counters & counters() const { return counters_; }
460         ///
461         std::string const & opt_enginetype() const { return opt_enginetype_; }
462         ///
463         std::string const & citeFramework() const { return citeframework_; }
464         ///
465         std::string const & opt_fontsize() const { return opt_fontsize_; }
466         ///
467         std::string const & opt_pagesize() const { return opt_pagesize_; }
468         ///
469         std::string const & opt_pagestyle() const { return opt_pagestyle_; }
470         ///
471         std::string const & options() const { return options_; }
472         ///
473         std::string const & class_header() const { return class_header_; }
474         ///
475         std::string const & fontsizeformat() const { return fontsize_format_; }
476         ///
477         std::string const & pagesize() const { return pagesize_; }
478         ///
479         std::string const & pagesizeformat() const { return pagesize_format_; }
480         ///
481         std::string const & pagestyle() const { return pagestyle_; }
482         ///
483         std::string const & tablestyle() const { return tablestyle_; }
484         ///
485         docstring const & preamble() const { return preamble_; }
486         ///
487         docstring const & htmlpreamble() const { return htmlpreamble_; }
488         ///
489         docstring const & htmlstyles() const { return htmlstyles_; }
490         ///
491         bool docbookforceabstract() const { return docbookforceabstract_; }
492         ///
493         std::string const & docbookroot() const { return docbookroot_; }
494         /// Looks for the layout of "highest level", other than Part (or other
495         /// layouts with a negative toc number), for use in constructing TOCs and
496         /// similar information.
497         Layout const & getTOCLayout() const;
498         /// the paragraph style to use for TOCs, Bibliography, etc
499         /// we will attempt to calculate this if it was not given
500         Layout const & htmlTOCLayout() const;
501         /// is this feature already provided by the class?
502         bool provides(std::string const & p) const;
503         /// features required by the class?
504         std::set<std::string> const & required() const { return required_; }
505         /// package options to write to LaTeX file
506         std::map<std::string, std::string> const & packageOptions() const
507                 { return package_options_; }
508         ///
509         unsigned int columns() const { return columns_; }
510         ///
511         PageSides sides() const { return sides_; }
512         ///
513         int secnumdepth() const { return secnumdepth_; }
514         ///
515         int tocdepth() const { return tocdepth_; }
516         ///
517         FontInfo const & defaultfont() const { return defaultfont_; }
518         /// Text that dictates how wide the left margin is on the screen
519         docstring const & leftmargin() const { return leftmargin_; }
520         /// Text that dictates how wide the right margin is on the screen
521         docstring const & rightmargin() const { return rightmargin_; }
522         /// The type of command used to produce a title
523         TitleLatexType titletype() const { return titletype_; }
524         /// The name of the title command
525         std::string const & titlename() const { return titlename_; }
526         ///
527         int size() const { return layoutlist_.size(); }
528         /// The minimal TocLevel of sectioning layouts
529         int min_toclevel() const { return min_toclevel_; }
530         /// The maximal TocLevel of sectioning layouts
531         int max_toclevel() const { return max_toclevel_; }
532         /// returns true if the class has a ToC structure
533         bool hasTocLevels() const;
534         ///
535         std::string const getCiteFormat(CiteEngineType const & type,
536                 std::string const & entry, bool const punct = true,
537                 std::string const & fallback = "") const;
538         ///
539         std::string const & getCiteMacro(CiteEngineType const & type,
540                 std::string const & macro) const;
541         ///
542         std::vector<std::string> const citeCommands(CiteEngineType const &) const;
543         ///
544         std::vector<CitationStyle> const & citeStyles(CiteEngineType const &) const;
545         ///
546         std::map<std::string, std::string> const & defaultBiblioStyle() const
547         { return cite_default_biblio_style_; }
548         ///
549         std::map<std::string, std::string> const & citeCommandAliases() const
550         { return cite_command_aliases_; }
551         /// The maximum number of citations before "et al."
552         size_t max_citenames() const { return maxcitenames_; }
553         ///
554         bool fullAuthorList() const { return cite_full_author_list_; }
555         ///
556         bool bibInToc() const { return bibintoc_; }
557 protected:
558         /// Constructs a DocumentClass based upon a LayoutFile.
559         DocumentClass(LayoutFile const & tc);
560         /// Needed in tex2lyx
561         DocumentClass() {}
562 private:
563         /// The only way to make a DocumentClass is to call this function.
564         friend DocumentClassPtr
565                 getDocumentClass(LayoutFile const &, LayoutModuleList const &,
566                                  std::string const &,
567                                  bool clone, bool internal);
568 };
569
570
571 /// The only way to make a DocumentClass is to call this function.
572 /// The shared_ptr is needed because DocumentClass objects can be kept
573 /// in memory long after their associated Buffer is destroyed, mostly
574 /// on the CutStack.
575 DocumentClassPtr getDocumentClass(LayoutFile const & baseClass,
576                         LayoutModuleList const & modlist,
577                         std::string const & cengine = std::string(),
578                         bool clone = false, bool internal = false);
579
580 /// convert page sides option to text 1 or 2
581 std::ostream & operator<<(std::ostream & os, PageSides p);
582
583 /// current format of layout files
584 extern int const LAYOUT_FORMAT;
585 /// layout format for the current lyx file format (usually equal to
586 /// LAYOUT_FORMAT)
587 extern int const LYXFILE_LAYOUT_FORMAT;
588
589
590 } // namespace lyx
591
592 #endif