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