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