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 "FloatList.h"
19 #include "LayoutEnums.h"
20 #include "LayoutModuleList.h"
22 #include "insets/InsetLayout.h"
24 #include "support/docstring.h"
25 #include "support/types.h"
27 #include <boost/noncopyable.hpp>
37 namespace support { class FileName; }
45 /// Based upon ideas in boost::noncopyable, inheriting from this
46 /// class effectively makes the copy constructor protected but the
47 /// assignment constructor private.
53 ProtectCopy(const ProtectCopy &) {}
55 const ProtectCopy & operator=(const ProtectCopy &);
59 /// A TextClass represents a collection of layout information: At the
60 /// moment, this includes Layout's and InsetLayout's.
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.)
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.
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.
79 class TextClass : protected ProtectCopy {
82 virtual ~TextClass() {}
83 ///////////////////////////////////////////////////////////////////
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.
96 // NOTE: Layout pointers are directly assigned to paragraphs so a
97 // container that does not invalidate these pointers after insertion
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;
104 typedef LayoutList::const_iterator const_iterator;
106 ///////////////////////////////////////////////////////////////////
108 ///////////////////////////////////////////////////////////////////
110 const_iterator begin() const { return layoutlist_.begin(); }
112 const_iterator end() const { return layoutlist_.end(); }
115 ///////////////////////////////////////////////////////////////////
117 ///////////////////////////////////////////////////////////////////
119 Layout const & defaultLayout() const;
121 docstring const & defaultLayoutName() const;
123 bool isDefaultLayout(Layout const &) const;
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(); }
136 bool hasLayout(docstring const & name) const;
138 bool hasInsetLayout(docstring const & name) const;
140 Layout const & operator[](docstring const & vname) const;
141 /// Inset layouts of this doc class
142 InsetLayouts const & insetLayouts() const { return insetlayoutlist_; }
144 ///////////////////////////////////////////////////////////////////
146 ///////////////////////////////////////////////////////////////////
147 /// Enum used with TextClass::read
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 VALIDATION //>We're just validating
154 /// return values for read()
162 /// Performs the read of the layout file.
163 /// \return true on success.
164 // FIXME Should return ReturnValues....
165 bool read(support::FileName const & filename, ReadType rt = BASECLASS);
167 ReturnValues read(std::string const & str, ReadType rt = MODULE);
169 ReturnValues read(Lexer & lex, ReadType rt = BASECLASS);
170 /// validates the layout information passed in str
171 static ReturnValues validate(std::string const & str);
173 static std::string convert(std::string const & str);
175 ///////////////////////////////////////////////////////////////////
177 ///////////////////////////////////////////////////////////////////
178 /// Sees to it the textclass structure has been loaded
179 /// This function will search for $classname.layout in default directories
180 /// and an optional path, but if path points to a file, it will be loaded
182 bool load(std::string const & path = std::string()) const;
183 /// Has this layout file been loaded yet?
184 /// Overridden by DocumentClass
185 virtual bool loaded() const { return loaded_; }
187 ///////////////////////////////////////////////////////////////////
189 ///////////////////////////////////////////////////////////////////
191 std::string const & name() const { return name_; }
193 std::string const & description() const { return description_; }
195 std::string const & latexname() const { return latexname_; }
197 std::string const & prerequisites() 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_; }
203 /// Protect construction
206 Layout & operator[](docstring const & name);
207 /** Create an new, very basic layout for this textclass. This is used for
208 the Plain Layout common to all TextClass objects and also, in
209 DocumentClass, for the creation of new layouts `on the fly' when
210 previously unknown layouts are encountered.
211 \param unknown Set to true if this layout is used to represent an
214 Layout createBasicLayout(docstring const & name, bool unknown = false) const;
216 ///////////////////////////////////////////////////////////////////
217 // non-const iterators
218 ///////////////////////////////////////////////////////////////////
220 typedef LayoutList::iterator iterator;
222 iterator begin() { return layoutlist_.begin(); }
224 iterator end() { return layoutlist_.end(); }
226 ///////////////////////////////////////////////////////////////////
228 ///////////////////////////////////////////////////////////////////
229 /// Paragraph styles used in this layout
230 /// This variable is mutable because unknown layouts can be added
231 /// to const textclass.
232 mutable LayoutList layoutlist_;
235 /// document class name
236 std::string latexname_;
237 /// document class description
238 std::string description_;
239 /// available types of float, eg. figure, algorithm.
240 mutable FloatList floatlist_;
241 /// Types of counters, eg. sections, eqns, figures, avail. in document class.
242 mutable Counters counters_;
243 /// Has this layout file been loaded yet?
244 mutable bool loaded_;
245 /// Is the TeX class available?
246 bool tex_class_avail_;
247 /// document class prerequisites
248 mutable std::string prerequisites_;
249 /// The possible cite engine types
250 std::string opt_enginetype_;
252 std::string opt_fontsize_;
254 std::string opt_pagestyle_;
255 /// Specific class options
256 std::string options_;
258 std::string pagestyle_;
260 std::string class_header_;
262 docstring defaultlayout_;
263 /// name of plain layout
264 static const docstring plain_layout_;
265 /// preamble text to support layout styles
267 /// same, but for HTML output
268 /// this is output as is to the header
269 docstring htmlpreamble_;
270 /// same, but specifically for CSS information
271 docstring htmlstyles_;
272 /// the paragraph style to use for TOCs, Bibliography, etc
273 mutable docstring html_toc_section_;
274 /// latex packages loaded by document class.
275 std::set<std::string> provides_;
276 /// latex packages requested by document class.
277 std::set<std::string> requires_;
278 /// default modules wanted by document class
279 LayoutModuleList default_modules_;
280 /// modules provided by document class
281 LayoutModuleList provided_modules_;
282 /// modules excluded by document class
283 LayoutModuleList excluded_modules_;
285 unsigned int columns_;
288 /// header depth to have numbering
290 /// header depth to appear in table of contents
292 /// Can be LaTeX, DocBook, etc.
293 OutputType outputType_;
294 /// Can be latex, docbook ... (the name of a format)
295 std::string outputFormat_;
296 /** Base font. The paragraph and layout fonts are resolved against
297 this font. This has to be fully instantiated. Attributes
298 FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
301 FontInfo defaultfont_;
302 /// Text that dictates how wide the left margin is on the screen
303 docstring leftmargin_;
304 /// Text that dictates how wide the right margin is on the screen
305 docstring rightmargin_;
306 /// The type of command used to produce a title
307 TitleLatexType titletype_;
308 /// The name of the title command
309 std::string titlename_;
310 /// Input layouts available to this layout
311 InsetLayouts insetlayoutlist_;
312 /// The minimal TocLevel of sectioning layouts
314 /// The maximal TocLevel of sectioning layouts
316 /// Citation formatting information
317 std::map<CiteEngineType, std::map<std::string, std::string> > cite_formats_;
319 std::map<CiteEngineType, std::map<std::string, std::string> > cite_macros_;
320 /// The default BibTeX bibliography style file
321 std::string cite_default_biblio_style_;
322 /// Whether full author lists are supported
323 bool cite_full_author_list_;
324 /// The possible citation styles
325 std::map<CiteEngineType, std::vector<CitationStyle> > cite_styles_;
327 ///////////////////////////////////////////////////////////////////
328 // helper routines for reading layout files
329 ///////////////////////////////////////////////////////////////////
331 bool deleteLayout(docstring const &);
333 bool convertLayoutFormat(support::FileName const &, ReadType);
334 /// Reads the layout file without running layout2layout.
335 ReturnValues readWithoutConv(support::FileName const & filename, ReadType rt);
336 /// \return true for success.
337 bool readStyle(Lexer &, Layout &) const;
339 void readOutputType(Lexer &);
341 void readTitleType(Lexer &);
343 void readMaxCounter(Lexer &);
345 void readClassOptions(Lexer &);
347 void readCharStyle(Lexer &, std::string const &);
349 bool readFloat(Lexer &);
351 bool readCiteEngine(Lexer &);
353 int readCiteEngineType(Lexer &) const;
355 bool readCiteFormat(Lexer &);
359 /// A DocumentClass represents the layout information associated with a
360 /// Buffer. It is based upon a LayoutFile, but may be modified by loading
363 /// In that regard, DocumentClass objects are "dynamic". But this is really
364 /// an illusion, since DocumentClass objects are not (currently) changed
365 /// when, say, a new Module is loaded. Rather, the old DocumentClass is
366 /// discarded---actually, it's kept around in case something on the cut
367 /// stack needs it---and a new one is created from scratch.
369 /// In the main LyX code, DocumentClass objects are created only by
370 /// DocumentClassBundle, for which see below.
372 class DocumentClass : public TextClass, boost::noncopyable {
375 virtual ~DocumentClass() {}
377 ///////////////////////////////////////////////////////////////////
379 ///////////////////////////////////////////////////////////////////
380 /// \return true if there is a Layout with latexname lay
381 bool hasLaTeXLayout(std::string const & lay) const;
382 /// A DocumentClass nevers count as loaded, since it is dynamic
383 virtual bool loaded() { return false; }
384 /// \return the layout object of an inset given by name. If the name
385 /// is not found as such, the part after the ':' is stripped off, and
386 /// searched again. In this way, an error fallback can be provided:
387 /// An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
388 /// will invoke the layout object defined by name = 'CharStyle'.
389 /// If that doesn't work either, an empty object returns (shouldn't
390 /// happen). -- Idea JMarc, comment MV
391 InsetLayout const & insetLayout(docstring const & name) const;
392 /// a plain inset layout for use as a default
393 static InsetLayout const & plainInsetLayout() { return plain_insetlayout_; }
394 /// add a new layout \c name if it does not exist in layoutlist_
395 /// \return whether we had to add one.
396 bool addLayoutIfNeeded(docstring const & name) const;
398 ///////////////////////////////////////////////////////////////////
400 ///////////////////////////////////////////////////////////////////
401 /// the list of floats defined in the document class
402 FloatList const & floats() const { return floatlist_; }
404 Counters & counters() const { return counters_; }
406 std::string const & opt_enginetype() const { return opt_enginetype_; }
408 std::string const & opt_fontsize() const { return opt_fontsize_; }
410 std::string const & opt_pagestyle() const { return opt_pagestyle_; }
412 std::string const & options() const { return options_; }
414 std::string const & class_header() const { return class_header_; }
416 std::string const & pagestyle() const { return pagestyle_; }
418 docstring const & preamble() const { return preamble_; }
420 docstring const & htmlpreamble() const { return htmlpreamble_; }
422 docstring const & htmlstyles() const { return htmlstyles_; }
423 /// the paragraph style to use for TOCs, Bibliography, etc
424 /// we will attempt to calculate this if it was not given
425 Layout const & htmlTOCLayout() const;
426 /// is this feature already provided by the class?
427 bool provides(std::string const & p) const;
428 /// features required by the class?
429 std::set<std::string> const & requires() const { return requires_; }
431 unsigned int columns() const { return columns_; }
433 PageSides sides() const { return sides_; }
435 int secnumdepth() const { return secnumdepth_; }
437 int tocdepth() const { return tocdepth_; }
439 FontInfo const & defaultfont() const { return defaultfont_; }
440 /// Text that dictates how wide the left margin is on the screen
441 docstring const & leftmargin() const { return leftmargin_; }
442 /// Text that dictates how wide the right margin is on the screen
443 docstring const & rightmargin() const { return rightmargin_; }
444 /// The type of command used to produce a title
445 TitleLatexType titletype() const { return titletype_; }
446 /// The name of the title command
447 std::string const & titlename() const { return titlename_; }
449 int size() const { return layoutlist_.size(); }
450 /// The minimal TocLevel of sectioning layouts
451 int min_toclevel() const { return min_toclevel_; }
452 /// The maximal TocLevel of sectioning layouts
453 int max_toclevel() const { return max_toclevel_; }
454 /// returns true if the class has a ToC structure
455 bool hasTocLevels() const;
457 std::string const & getCiteFormat(CiteEngineType const & type,
458 std::string const & entry, std::string const & fallback = "") const;
460 std::string const & getCiteMacro(CiteEngineType const & type,
461 std::string const & macro) const;
463 std::vector<std::string> const citeCommands(CiteEngineType const &) const;
465 std::vector<CitationStyle> const & citeStyles(CiteEngineType const &) const;
467 std::string const & defaultBiblioStyle() const { return cite_default_biblio_style_; }
469 bool const & fullAuthorList() const { return cite_full_author_list_; }
471 /// Constructs a DocumentClass based upon a LayoutFile.
472 DocumentClass(LayoutFile const & tc);
473 /// Needed in tex2lyx
476 /// The only class that can create a DocumentClass is
477 /// DocumentClassBundle, which calls the protected constructor.
478 friend class DocumentClassBundle;
480 static InsetLayout plain_insetlayout_;
484 /// DocumentClassBundle is a container for DocumentClass objects, so that
485 /// they stay in memory for use by Insets, CutAndPaste, and the like, even
486 /// when their associated Buffers are destroyed.
487 /// FIXME Some sort of garbage collection or reference counting wouldn't
488 /// be a bad idea here. It might be enough to check when a Buffer is closed
489 /// (or makeDocumentClass is called) whether the old DocumentClass is in use
492 /// This is a singleton class. Its sole instance is accessed via
493 /// DocumentClassBundle::get().
494 class DocumentClassBundle : boost::noncopyable {
496 /// \return The sole instance of this class.
497 static DocumentClassBundle & get();
498 /// \return A new DocumentClass based on baseClass, with info added
499 /// from the modules in modlist.
500 DocumentClass & makeDocumentClass(LayoutFile const & baseClass,
501 LayoutModuleList const & modlist);
503 /// control instantiation
504 DocumentClassBundle() {}
506 ~DocumentClassBundle();
507 /// \return Reference to a new DocumentClass equal to baseClass
508 DocumentClass & newClass(LayoutFile const & baseClass);
510 std::vector<DocumentClass *> documentClasses_;
514 /// convert page sides option to text 1 or 2
515 std::ostream & operator<<(std::ostream & os, PageSides p);
517 /// current format of layout files
518 extern int const LAYOUT_FORMAT;