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.
13 #include "ColorCode.h"
15 #include "FloatList.h"
18 #include "LayoutEnums.h"
19 #include "LayoutModuleList.h"
21 #include "insets/InsetLayout.h"
23 #include "support/docstring.h"
24 #include "support/types.h"
26 #include <boost/noncopyable.hpp>
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()
160 /// Performs the read of the layout file.
161 /// \return true on success.
162 bool read(support::FileName const & filename, ReadType rt = BASECLASS);
164 bool read(std::string const & str, ReadType rt = BASECLASS);
166 ReturnValues read(Lexer & lex, ReadType rt = BASECLASS);
167 /// validates the layout information passed in str
168 static bool validate(std::string const & str);
170 ///////////////////////////////////////////////////////////////////
172 ///////////////////////////////////////////////////////////////////
173 /// Sees to it the textclass structure has been loaded
174 /// This function will search for $classname.layout in default directories
175 /// and an optional path, but if path points to a file, it will be loaded
177 bool load(std::string const & path = std::string()) const;
178 /// Has this layout file been loaded yet?
179 /// Overridden by DocumentClass
180 virtual bool loaded() const { return loaded_; }
182 ///////////////////////////////////////////////////////////////////
184 ///////////////////////////////////////////////////////////////////
186 std::string const & name() const { return name_; }
188 std::string const & description() const { return description_; }
190 std::string const & latexname() const { return latexname_; }
192 std::string const & prerequisites() const;
193 /// Can be LaTeX, DocBook, etc.
194 OutputType outputType() const { return outputType_; }
195 /// Can be latex, docbook ... (the name of a format)
196 std::string outputFormat() const { return outputFormat_; }
198 /// Protect construction
201 Layout & operator[](docstring const & name);
202 /** Create an new, very basic layout for this textclass. This is used for
203 the Plain Layout common to all TextClass objects and also, in
204 DocumentClass, for the creation of new layouts `on the fly' when
205 previously unknown layouts are encountered.
206 \param unknown Set to true if this layout is used to represent an
209 Layout createBasicLayout(docstring const & name, bool unknown = false) const;
211 ///////////////////////////////////////////////////////////////////
212 // non-const iterators
213 ///////////////////////////////////////////////////////////////////
215 typedef LayoutList::iterator iterator;
217 iterator begin() { return layoutlist_.begin(); }
219 iterator end() { return layoutlist_.end(); }
221 ///////////////////////////////////////////////////////////////////
223 ///////////////////////////////////////////////////////////////////
224 /// Paragraph styles used in this layout
225 /// This variable is mutable because unknown layouts can be added
226 /// to const textclass.
227 mutable LayoutList layoutlist_;
230 /// document class name
231 std::string latexname_;
232 /// document class description
233 std::string description_;
234 /// available types of float, eg. figure, algorithm.
235 mutable FloatList floatlist_;
236 /// Types of counters, eg. sections, eqns, figures, avail. in document class.
237 mutable Counters counters_;
238 /// Has this layout file been loaded yet?
239 mutable bool loaded_;
240 /// Is the TeX class available?
241 bool tex_class_avail_;
242 /// document class prerequisites
243 mutable std::string prerequisites_;
245 std::string opt_fontsize_;
247 std::string opt_pagestyle_;
248 /// Specific class options
249 std::string options_;
251 std::string pagestyle_;
253 std::string class_header_;
255 docstring defaultlayout_;
256 /// name of plain layout
257 static const docstring plain_layout_;
258 /// preamble text to support layout styles
260 /// same, but for HTML output
261 /// this is output as is to the header
262 docstring htmlpreamble_;
263 /// the paragraph style to use for TOCs, Bibliography, etc
264 mutable docstring html_toc_section_;
265 /// latex packages loaded by document class.
266 std::set<std::string> provides_;
267 /// latex packages requested by document class.
268 std::set<std::string> requires_;
269 /// default modules wanted by document class
270 LayoutModuleList default_modules_;
271 /// modules provided by document class
272 LayoutModuleList provided_modules_;
273 /// modules excluded by document class
274 LayoutModuleList excluded_modules_;
276 unsigned int columns_;
279 /// header depth to have numbering
281 /// header depth to appear in table of contents
283 /// Can be LaTeX, DocBook, etc.
284 OutputType outputType_;
285 /// Can be latex, docbook ... (the name of a format)
286 std::string outputFormat_;
287 /** Base font. The paragraph and layout fonts are resolved against
288 this font. This has to be fully instantiated. Attributes
289 FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
292 FontInfo defaultfont_;
293 /// Text that dictates how wide the left margin is on the screen
294 docstring leftmargin_;
295 /// Text that dictates how wide the right margin is on the screen
296 docstring rightmargin_;
297 /// The type of command used to produce a title
298 TitleLatexType titletype_;
299 /// The name of the title command
300 std::string titlename_;
301 /// Input layouts available to this layout
302 InsetLayouts insetlayoutlist_;
303 /// The minimal TocLevel of sectioning layouts
305 /// The maximal TocLevel of sectioning layouts
307 /// Citation formatting information
308 std::map<std::string, std::string> cite_formats_;
310 ///////////////////////////////////////////////////////////////////
311 // helper routines for reading layout files
312 ///////////////////////////////////////////////////////////////////
314 bool deleteLayout(docstring const &);
316 bool convertLayoutFormat(support::FileName const &, ReadType);
317 /// Reads the layout file without running layout2layout.
318 ReturnValues readWithoutConv(support::FileName const & filename, ReadType rt);
319 /// \return true for success.
320 bool readStyle(Lexer &, Layout &) const;
322 void readOutputType(Lexer &);
324 void readTitleType(Lexer &);
326 void readMaxCounter(Lexer &);
328 void readClassOptions(Lexer &);
330 void readCharStyle(Lexer &, std::string const &);
332 void readFloat(Lexer &);
334 void readCiteFormat(Lexer &);
338 /// A DocumentClass represents the layout information associated with a
339 /// Buffer. It is based upon a LayoutFile, but may be modified by loading
342 /// In that regard, DocumentClass objects are "dynamic". But this is really
343 /// an illusion, since DocumentClass objects are not (currently) changed
344 /// when, say, a new Module is loaded. Rather, the old DocumentClass is
345 /// discarded---actually, it's kept around in case something on the cut
346 /// stack needs it---and a new one is created from scratch.
348 /// In the main LyX code, DocumentClass objects are created only by
349 /// DocumentClassBundle, for which see below.
351 class DocumentClass : public TextClass, boost::noncopyable {
354 virtual ~DocumentClass() {}
356 ///////////////////////////////////////////////////////////////////
358 ///////////////////////////////////////////////////////////////////
359 /// \return true if there is a Layout with latexname lay
360 bool hasLaTeXLayout(std::string const & lay) const;
361 /// A DocumentClass nevers count as loaded, since it is dynamic
362 virtual bool loaded() { return false; }
363 /// \return the layout object of an inset given by name. If the name
364 /// is not found as such, the part after the ':' is stripped off, and
365 /// searched again. In this way, an error fallback can be provided:
366 /// An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
367 /// will invoke the layout object defined by name = 'CharStyle'.
368 /// If that doesn't work either, an empty object returns (shouldn't
369 /// happen). -- Idea JMarc, comment MV
370 InsetLayout const & insetLayout(docstring const & name) const;
371 /// a plain inset layout for use as a default
372 static InsetLayout const & plainInsetLayout() { return plain_insetlayout_; }
373 /// add a new layout \c name if it does not exist in layoutlist_
374 void addLayoutIfNeeded(docstring const & name) const;
376 ///////////////////////////////////////////////////////////////////
378 ///////////////////////////////////////////////////////////////////
379 /// the list of floats defined in the document class
380 FloatList const & floats() const { return floatlist_; }
382 Counters & counters() const { return counters_; }
384 std::string const & opt_fontsize() const { return opt_fontsize_; }
386 std::string const & opt_pagestyle() const { return opt_pagestyle_; }
388 std::string const & options() const { return options_; }
390 std::string const & class_header() const { return class_header_; }
392 std::string const & pagestyle() const { return pagestyle_; }
394 docstring const & preamble() const { return preamble_; }
396 docstring const & htmlpreamble() const { return htmlpreamble_; }
397 /// the paragraph style to use for TOCs, Bibliography, etc
398 /// we will attempt to calculate this if it was not given
399 Layout const & htmlTOCLayout() const;
400 /// is this feature already provided by the class?
401 bool provides(std::string const & p) const;
402 /// features required by the class?
403 std::set<std::string> const & requires() const { return requires_; }
405 unsigned int columns() const { return columns_; }
407 PageSides sides() const { return sides_; }
409 int secnumdepth() const { return secnumdepth_; }
411 int tocdepth() const { return tocdepth_; }
413 FontInfo const & defaultfont() const { return defaultfont_; }
414 /// Text that dictates how wide the left margin is on the screen
415 docstring const & leftmargin() const { return leftmargin_; }
416 /// Text that dictates how wide the right margin is on the screen
417 docstring const & rightmargin() const { return rightmargin_; }
418 /// The type of command used to produce a title
419 TitleLatexType titletype() const { return titletype_; }
420 /// The name of the title command
421 std::string const & titlename() const { return titlename_; }
423 int size() const { return layoutlist_.size(); }
424 /// The minimal TocLevel of sectioning layouts
425 int min_toclevel() const { return min_toclevel_; }
426 /// The maximal TocLevel of sectioning layouts
427 int max_toclevel() const { return max_toclevel_; }
428 /// returns true if the class has a ToC structure
429 bool hasTocLevels() const;
431 std::string const & getCiteFormat(std::string const & entry_type) const;
433 /// Constructs a DocumentClass based upon a LayoutFile.
434 DocumentClass(LayoutFile const & tc);
435 /// Needed in tex2lyx
438 /// The only class that can create a DocumentClass is
439 /// DocumentClassBundle, which calls the protected constructor.
440 friend class DocumentClassBundle;
442 static InsetLayout plain_insetlayout_;
446 /// DocumentClassBundle is a container for DocumentClass objects, so that
447 /// they stay in memory for use by Insets, CutAndPaste, and the like, even
448 /// when their associated Buffers are destroyed.
449 /// FIXME Some sort of garbage collection or reference counting wouldn't
450 /// be a bad idea here. It might be enough to check when a Buffer is closed
451 /// (or makeDocumentClass is called) whether the old DocumentClass is in use
454 /// This is a singleton class. Its sole instance is accessed via
455 /// DocumentClassBundle::get().
456 class DocumentClassBundle : boost::noncopyable {
458 /// \return The sole instance of this class.
459 static DocumentClassBundle & get();
460 /// \return A new DocumentClass based on baseClass, with info added
461 /// from the modules in modlist.
462 DocumentClass & makeDocumentClass(LayoutFile const & baseClass,
463 LayoutModuleList const & modlist);
465 /// control instantiation
466 DocumentClassBundle() {}
468 ~DocumentClassBundle();
469 /// \return Reference to a new DocumentClass equal to baseClass
470 DocumentClass & newClass(LayoutFile const & baseClass);
472 std::vector<DocumentClass *> documentClasses_;
476 /// convert page sides option to text 1 or 2
477 std::ostream & operator<<(std::ostream & os, PageSides p);