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