4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * Full author contact details are available in file CREDITS.
14 #include "ColorCode.h"
16 #include "DocumentClassPtr.h"
17 #include "FloatList.h"
20 #include "LayoutEnums.h"
21 #include "LayoutModuleList.h"
23 #include "insets/InsetLayout.h"
25 #include "support/docstring.h"
26 #include "support/types.h"
36 namespace support { class FileName; }
44 /// Based upon ideas in boost::noncopyable, inheriting from this
45 /// class effectively makes the copy constructor protected but the
46 /// assignment constructor private.
52 ProtectCopy(const ProtectCopy &) {}
54 const ProtectCopy & operator=(const ProtectCopy &);
58 /// A TextClass represents a collection of layout information: At the
59 /// moment, this includes Layout's and InsetLayout's.
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.)
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.
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.
78 class TextClass : protected ProtectCopy {
81 virtual ~TextClass() {}
82 ///////////////////////////////////////////////////////////////////
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.
95 // NOTE: Layout pointers are directly assigned to paragraphs so a
96 // container that does not invalidate these pointers after insertion
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;
103 typedef LayoutList::const_iterator const_iterator;
105 ///////////////////////////////////////////////////////////////////
107 ///////////////////////////////////////////////////////////////////
109 const_iterator begin() const { return layoutlist_.begin(); }
111 const_iterator end() const { return layoutlist_.end(); }
114 ///////////////////////////////////////////////////////////////////
116 ///////////////////////////////////////////////////////////////////
118 Layout const & defaultLayout() const;
120 docstring const & defaultLayoutName() const;
122 bool isDefaultLayout(Layout const &) const;
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(); }
135 bool hasLayout(docstring const & name) const;
137 bool hasInsetLayout(docstring const & name) const;
139 Layout const & operator[](docstring const & vname) const;
140 /// Inset layouts of this doc class
141 InsetLayouts const & insetLayouts() const { return insetlayoutlist_; }
143 ///////////////////////////////////////////////////////////////////
145 ///////////////////////////////////////////////////////////////////
146 /// Enum used with TextClass::read
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
153 /// return values for read()
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);
166 ReturnValues read(std::string const & str, ReadType rt = MODULE);
168 ReturnValues read(Lexer & lex, ReadType rt = BASECLASS);
169 /// validates the layout information passed in str
170 static ReturnValues validate(std::string const & str);
172 static std::string convert(std::string const & str);
174 ///////////////////////////////////////////////////////////////////
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
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_; }
186 ///////////////////////////////////////////////////////////////////
188 ///////////////////////////////////////////////////////////////////
190 std::string const & name() const { return name_; }
192 std::string const & category() const { return category_; }
194 std::string const & description() const { return description_; }
196 std::string const & latexname() const { return latexname_; }
198 std::string const & prerequisites(std::string const & sep = "\n\t") const;
199 /// Can be LaTeX, DocBook, etc.
200 OutputType outputType() const { return outputType_; }
201 /// Can be latex, docbook ... (the name of a format)
202 std::string outputFormat() const { return outputFormat_; }
204 /// Protect construction
207 Layout & operator[](docstring const & name);
208 /** Create an new, very basic layout for this textclass. This is used for
209 the Plain Layout common to all TextClass objects and also, in
210 DocumentClass, for the creation of new layouts `on the fly' when
211 previously unknown layouts are encountered.
212 \param unknown Set to true if this layout is used to represent an
215 Layout createBasicLayout(docstring const & name, bool unknown = false) const;
217 ///////////////////////////////////////////////////////////////////
218 // non-const iterators
219 ///////////////////////////////////////////////////////////////////
221 typedef LayoutList::iterator iterator;
223 iterator begin() { return layoutlist_.begin(); }
225 iterator end() { return layoutlist_.end(); }
227 ///////////////////////////////////////////////////////////////////
229 ///////////////////////////////////////////////////////////////////
230 /// Paragraph styles used in this layout
231 /// This variable is mutable because unknown layouts can be added
232 /// to const textclass.
233 mutable LayoutList layoutlist_;
237 std::string category_;
238 /// document class name
239 std::string latexname_;
240 /// document class description
241 std::string description_;
242 /// available types of float, eg. figure, algorithm.
243 mutable FloatList floatlist_;
244 /// Types of counters, eg. sections, eqns, figures, avail. in document class.
245 mutable Counters counters_;
246 /// Has this layout file been loaded yet?
247 mutable bool loaded_;
248 /// Is the TeX class available?
249 bool tex_class_avail_;
250 /// document class prerequisites
251 mutable std::string prerequisites_;
252 /// The possible cite engine types
253 std::string opt_enginetype_;
255 std::string opt_fontsize_;
257 std::string opt_pagestyle_;
258 /// Specific class options
259 std::string options_;
261 std::string pagestyle_;
263 std::string class_header_;
265 docstring defaultlayout_;
266 /// name of plain layout
267 static const docstring plain_layout_;
268 /// preamble text to support layout styles
270 /// same, but for HTML output
271 /// this is output as is to the header
272 docstring htmlpreamble_;
273 /// same, but specifically for CSS information
274 docstring htmlstyles_;
275 /// the paragraph style to use for TOCs, Bibliography, etc
276 mutable docstring html_toc_section_;
277 /// latex packages loaded by document class.
278 std::set<std::string> provides_;
279 /// latex packages requested by document class.
280 std::set<std::string> requires_;
281 /// default modules wanted by document class
282 LayoutModuleList default_modules_;
283 /// modules provided by document class
284 LayoutModuleList provided_modules_;
285 /// modules excluded by document class
286 LayoutModuleList excluded_modules_;
288 unsigned int columns_;
291 /// header depth to have numbering
293 /// header depth to appear in table of contents
295 /// Can be LaTeX, DocBook, etc.
296 OutputType outputType_;
297 /// Can be latex, docbook ... (the name of a format)
298 std::string outputFormat_;
299 /** Base font. The paragraph and layout fonts are resolved against
300 this font. This has to be fully instantiated. Attributes
301 FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
304 FontInfo defaultfont_;
305 /// Text that dictates how wide the left margin is on the screen
306 docstring leftmargin_;
307 /// Text that dictates how wide the right margin is on the screen
308 docstring rightmargin_;
309 /// The type of command used to produce a title
310 TitleLatexType titletype_;
311 /// The name of the title command
312 std::string titlename_;
313 /// Input layouts available to this layout
314 InsetLayouts insetlayoutlist_;
315 /// The minimal TocLevel of sectioning layouts
317 /// The maximal TocLevel of sectioning layouts
319 /// Citation formatting information
320 std::map<CiteEngineType, std::map<std::string, std::string> > cite_formats_;
322 std::map<CiteEngineType, std::map<std::string, std::string> > cite_macros_;
323 /// The default BibTeX bibliography style file
324 std::string cite_default_biblio_style_;
325 /// Whether full author lists are supported
326 bool cite_full_author_list_;
327 /// The possible citation styles
328 std::map<CiteEngineType, std::vector<CitationStyle> > cite_styles_;
330 ///////////////////////////////////////////////////////////////////
331 // helper routines for reading layout files
332 ///////////////////////////////////////////////////////////////////
334 bool deleteLayout(docstring const &);
336 bool deleteInsetLayout(docstring const &);
338 bool convertLayoutFormat(support::FileName const &, ReadType);
339 /// Reads the layout file without running layout2layout.
340 ReturnValues readWithoutConv(support::FileName const & filename, ReadType rt);
341 /// \return true for success.
342 bool readStyle(Lexer &, Layout &) const;
344 void readOutputType(Lexer &);
346 void readTitleType(Lexer &);
348 void readMaxCounter(Lexer &);
350 void readClassOptions(Lexer &);
352 void readCharStyle(Lexer &, std::string const &);
354 bool readFloat(Lexer &);
356 bool readCiteEngine(Lexer &);
358 int readCiteEngineType(Lexer &) const;
360 bool readCiteFormat(Lexer &);
364 /// A DocumentClass represents the layout information associated with a
365 /// Buffer. It is based upon a LayoutFile, but may be modified by loading
368 /// In that regard, DocumentClass objects are "dynamic". But this is really
369 /// an illusion, since DocumentClass objects are not (currently) changed
370 /// when, say, a new Module is loaded. Rather, the old DocumentClass is
371 /// discarded---actually, it's kept around in case something on the cut
372 /// stack needs it---and a new one is created from scratch.
374 /// In the main LyX code, DocumentClass objects are created only by
375 /// DocumentClassBundle, for which see below.
377 class DocumentClass : public TextClass {
380 virtual ~DocumentClass() {}
382 ///////////////////////////////////////////////////////////////////
384 ///////////////////////////////////////////////////////////////////
385 /// \return true if there is a Layout with latexname lay
386 bool hasLaTeXLayout(std::string const & lay) const;
387 /// A DocumentClass nevers count as loaded, since it is dynamic
388 virtual bool loaded() const { return false; }
389 /// \return the layout object of an inset given by name. If the name
390 /// is not found as such, the part after the ':' is stripped off, and
391 /// searched again. In this way, an error fallback can be provided:
392 /// An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
393 /// will invoke the layout object defined by name = 'CharStyle'.
394 /// If that doesn't work either, an empty object returns (shouldn't
395 /// happen). -- Idea JMarc, comment MV
396 InsetLayout const & insetLayout(docstring const & name) const;
397 /// a plain inset layout for use as a default
398 static InsetLayout const & plainInsetLayout() { return plain_insetlayout_; }
399 /// add a new layout \c name if it does not exist in layoutlist_
400 /// \return whether we had to add one.
401 bool addLayoutIfNeeded(docstring const & name) const;
402 /// Forced layouts in layout file syntax
403 std::string forcedLayouts() const;
405 ///////////////////////////////////////////////////////////////////
407 ///////////////////////////////////////////////////////////////////
408 /// the list of floats defined in the document class
409 FloatList const & floats() const { return floatlist_; }
411 Counters & counters() const { return counters_; }
413 std::string const & opt_enginetype() const { return opt_enginetype_; }
415 std::string const & opt_fontsize() const { return opt_fontsize_; }
417 std::string const & opt_pagestyle() const { return opt_pagestyle_; }
419 std::string const & options() const { return options_; }
421 std::string const & class_header() const { return class_header_; }
423 std::string const & pagestyle() const { return pagestyle_; }
425 docstring const & preamble() const { return preamble_; }
427 docstring const & htmlpreamble() const { return htmlpreamble_; }
429 docstring const & htmlstyles() const { return htmlstyles_; }
431 Layout const & getTOCLayout() const;
432 /// the paragraph style to use for TOCs, Bibliography, etc
433 /// we will attempt to calculate this if it was not given
434 Layout const & htmlTOCLayout() const;
435 /// is this feature already provided by the class?
436 bool provides(std::string const & p) const;
437 /// features required by the class?
438 std::set<std::string> const & requires() const { return requires_; }
440 unsigned int columns() const { return columns_; }
442 PageSides sides() const { return sides_; }
444 int secnumdepth() const { return secnumdepth_; }
446 int tocdepth() const { return tocdepth_; }
448 FontInfo const & defaultfont() const { return defaultfont_; }
449 /// Text that dictates how wide the left margin is on the screen
450 docstring const & leftmargin() const { return leftmargin_; }
451 /// Text that dictates how wide the right margin is on the screen
452 docstring const & rightmargin() const { return rightmargin_; }
453 /// The type of command used to produce a title
454 TitleLatexType titletype() const { return titletype_; }
455 /// The name of the title command
456 std::string const & titlename() const { return titlename_; }
458 int size() const { return layoutlist_.size(); }
459 /// The minimal TocLevel of sectioning layouts
460 int min_toclevel() const { return min_toclevel_; }
461 /// The maximal TocLevel of sectioning layouts
462 int max_toclevel() const { return max_toclevel_; }
463 /// returns true if the class has a ToC structure
464 bool hasTocLevels() const;
466 std::string const & getCiteFormat(CiteEngineType const & type,
467 std::string const & entry, std::string const & fallback = "") const;
469 std::string const & getCiteMacro(CiteEngineType const & type,
470 std::string const & macro) const;
472 std::vector<std::string> const citeCommands(CiteEngineType const &) const;
474 std::vector<CitationStyle> const & citeStyles(CiteEngineType const &) const;
476 std::string const & defaultBiblioStyle() const { return cite_default_biblio_style_; }
478 bool const & fullAuthorList() const { return cite_full_author_list_; }
480 /// Constructs a DocumentClass based upon a LayoutFile.
481 DocumentClass(LayoutFile const & tc);
482 /// Needed in tex2lyx
485 /// The only class that can create a DocumentClass is
486 /// DocumentClassBundle, which calls the protected constructor.
487 friend DocumentClassPtr
488 getDocumentClass(LayoutFile const &, LayoutModuleList const &);
490 static InsetLayout plain_insetlayout_;
494 /// The only way to make a DocumentClass is to call this function.
495 /// The shared_ptr is needed because DocumentClass objects can be kept
496 /// in memory long after their associated Buffer is destroyed, mostly
498 DocumentClassPtr getDocumentClass(LayoutFile const & baseClass,
499 LayoutModuleList const & modlist);
501 /// convert page sides option to text 1 or 2
502 std::ostream & operator<<(std::ostream & os, PageSides p);
504 /// current format of layout files
505 extern int const LAYOUT_FORMAT;