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>
35 namespace support { class FileName; }
43 /// Based upon ideas in boost::noncopyable, inheriting from this
44 /// class effectively makes the copy constructor protected but the
45 /// assignment constructor private.
51 ProtectCopy(const ProtectCopy &) {}
53 const ProtectCopy & operator=(const ProtectCopy &);
57 /// A TextClass represents a collection of layout information: At the
58 /// moment, this includes Layout's and InsetLayout's.
60 /// There are two major subclasses of TextClass: LayoutFile and
61 /// DocumentClass. These subclasses are what are actually used in LyX.
62 /// Simple TextClass objects are not directly constructed in the main
63 /// LyX code---the constructor is protected. (That said, in tex2lyx
64 /// there are what amount to simple TextClass objects.)
66 /// A LayoutFile (see LayoutFile.{h,cpp}) represents a *.layout file.
67 /// These are generally static objects---though they can be reloaded
68 /// from disk via LFUN_LAYOUT_RELOAD, so one should not assume that
69 /// they will never change.
71 /// A DocumentClass (see below) represents the layout information that
72 /// is associated with a given Buffer. These are static, in the sense
73 /// that they will not themselves change, but which DocumentClass is
74 /// associated with a Buffer can change, as modules are loaded and
75 /// unloaded, for example.
77 class TextClass : protected ProtectCopy {
80 virtual ~TextClass() {}
81 ///////////////////////////////////////////////////////////////////
83 ///////////////////////////////////////////////////////////////////
84 /// The individual paragraph layouts comprising the document class
85 // NOTE Do NOT try to make this a container of Layout pointers, e.g.,
86 // std::vector<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. (Of course, if you
94 // really, REALLY want to make LayoutList a vector<Layout *>, then
95 // you can implement custom assignment and copy constructors.)
97 // NOTE: Layout pointers are directly assigned to paragraphs so a
98 // container that does not invalidate these pointers after insertion
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()
161 /// Performs the read of the layout file.
162 /// \return true on success.
163 bool read(support::FileName const & filename, ReadType rt = BASECLASS);
165 bool read(std::string const & str, ReadType rt = BASECLASS);
167 ReturnValues read(Lexer & lex, ReadType rt = BASECLASS);
168 /// validates the layout information passed in str
169 static bool validate(std::string const & str);
171 ///////////////////////////////////////////////////////////////////
173 ///////////////////////////////////////////////////////////////////
174 /// Sees to it the textclass structure has been loaded
175 /// This function will search for $classname.layout in default directories
176 /// and an optional path, but if path points to a file, it will be loaded
178 bool load(std::string const & path = std::string()) const;
179 /// Has this layout file been loaded yet?
180 /// Overridden by DocumentClass
181 virtual bool loaded() const { return loaded_; }
183 ///////////////////////////////////////////////////////////////////
185 ///////////////////////////////////////////////////////////////////
187 std::string const & name() const { return name_; }
189 std::string const & description() const { return description_; }
191 std::string const & latexname() const { return latexname_; }
193 std::string const & prerequisites() const;
194 /// Can be LaTeX, DocBook, etc.
195 OutputType outputType() const { return outputType_; }
196 /// Can be latex, docbook ... (the name of a format)
197 std::string outputFormat() const { return outputFormat_; }
199 /// Protect construction
202 Layout & operator[](docstring const & name);
203 /** Create an new, very basic layout for this textclass. This is used for
204 the Plain Layout common to all TextClass objects and also, in
205 DocumentClass, for the creation of new layouts `on the fly' when
206 previously unknown layouts are encountered.
207 \param unknown Set to true if this layout is used to represent an
210 Layout createBasicLayout(docstring const & name, bool unknown = false) const;
212 ///////////////////////////////////////////////////////////////////
213 // non-const iterators
214 ///////////////////////////////////////////////////////////////////
216 typedef LayoutList::iterator iterator;
218 iterator begin() { return layoutlist_.begin(); }
220 iterator end() { return layoutlist_.end(); }
222 ///////////////////////////////////////////////////////////////////
224 ///////////////////////////////////////////////////////////////////
225 /// Paragraph styles used in this layout
226 /// This variable is mutable because unknown layouts can be added
227 /// to const textclass.
228 mutable LayoutList layoutlist_;
231 /// document class name
232 std::string latexname_;
233 /// document class description
234 std::string description_;
235 /// available types of float, eg. figure, algorithm.
236 mutable FloatList floatlist_;
237 /// Types of counters, eg. sections, eqns, figures, avail. in document class.
238 mutable Counters counters_;
239 /// Has this layout file been loaded yet?
240 mutable bool loaded_;
241 /// Is the TeX class available?
242 bool tex_class_avail_;
243 /// document class prerequisites
244 mutable std::string prerequisites_;
246 std::string opt_fontsize_;
248 std::string opt_pagestyle_;
249 /// Specific class options
250 std::string options_;
252 std::string pagestyle_;
254 std::string class_header_;
256 docstring defaultlayout_;
257 /// name of plain layout
258 static const docstring plain_layout_;
259 /// preamble text to support layout styles
261 /// same, but for HTML output
262 /// this is output as is to the header
263 docstring htmlpreamble_;
264 /// the paragraph style to use for TOCs, Bibliography, etc
265 mutable docstring html_toc_section_;
266 /// latex packages loaded by document class.
267 std::set<std::string> provides_;
268 /// latex packages requested by document class.
269 std::set<std::string> requires_;
270 /// default modules wanted by document class
271 LayoutModuleList default_modules_;
272 /// modules provided by document class
273 LayoutModuleList provided_modules_;
274 /// modules excluded by document class
275 LayoutModuleList excluded_modules_;
277 unsigned int columns_;
280 /// header depth to have numbering
282 /// header depth to appear in table of contents
284 /// Can be LaTeX, DocBook, etc.
285 OutputType outputType_;
286 /// Can be latex, docbook ... (the name of a format)
287 std::string outputFormat_;
288 /** Base font. The paragraph and layout fonts are resolved against
289 this font. This has to be fully instantiated. Attributes
290 FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
293 FontInfo defaultfont_;
294 /// Text that dictates how wide the left margin is on the screen
295 docstring leftmargin_;
296 /// Text that dictates how wide the right margin is on the screen
297 docstring rightmargin_;
298 /// The type of command used to produce a title
299 TitleLatexType titletype_;
300 /// The name of the title command
301 std::string titlename_;
302 /// Input layouts available to this layout
303 InsetLayouts insetlayoutlist_;
304 /// The minimal TocLevel of sectioning layouts
306 /// The maximal TocLevel of sectioning layouts
309 ///////////////////////////////////////////////////////////////////
310 // helper routines for reading layout files
311 ///////////////////////////////////////////////////////////////////
313 bool deleteLayout(docstring const &);
315 bool convertLayoutFormat(support::FileName const &, ReadType);
316 /// Reads the layout file without running layout2layout.
317 ReturnValues readWithoutConv(support::FileName const & filename, ReadType rt);
318 /// \return true for success.
319 bool readStyle(Lexer &, Layout &) const;
321 void readOutputType(Lexer &);
323 void readTitleType(Lexer &);
325 void readMaxCounter(Lexer &);
327 void readClassOptions(Lexer &);
329 void readCharStyle(Lexer &, std::string const &);
331 void readFloat(Lexer &);
335 /// A DocumentClass represents the layout information associated with a
336 /// Buffer. It is based upon a LayoutFile, but may be modified by loading
339 /// In that regard, DocumentClass objects are "dynamic". But this is really
340 /// an illusion, since DocumentClass objects are not (currently) changed
341 /// when, say, a new Module is loaded. Rather, the old DocumentClass is
342 /// discarded---actually, it's kept around in case something on the cut
343 /// stack needs it---and a new one is created from scratch.
345 /// In the main LyX code, DocumentClass objects are created only by
346 /// DocumentClassBundle, for which see below.
348 class DocumentClass : public TextClass, boost::noncopyable {
351 virtual ~DocumentClass() {}
353 ///////////////////////////////////////////////////////////////////
355 ///////////////////////////////////////////////////////////////////
356 /// \return true if there is a Layout with latexname lay
357 bool hasLaTeXLayout(std::string const & lay) const;
358 /// A DocumentClass nevers count as loaded, since it is dynamic
359 virtual bool loaded() { return false; }
360 /// \return the layout object of an inset given by name. If the name
361 /// is not found as such, the part after the ':' is stripped off, and
362 /// searched again. In this way, an error fallback can be provided:
363 /// An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
364 /// will invoke the layout object defined by name = 'CharStyle'.
365 /// If that doesn't work either, an empty object returns (shouldn't
366 /// happen). -- Idea JMarc, comment MV
367 InsetLayout const & insetLayout(docstring const & name) const;
368 /// a plain inset layout for use as a default
369 static InsetLayout const & plainInsetLayout() { return plain_insetlayout_; }
370 /// add a new layout \c name if it does not exist in layoutlist_
371 void addLayoutIfNeeded(docstring const & name) const;
373 ///////////////////////////////////////////////////////////////////
375 ///////////////////////////////////////////////////////////////////
376 /// the list of floats defined in the document class
377 FloatList const & floats() const { return floatlist_; }
379 Counters & counters() const { return counters_; }
381 std::string const & opt_fontsize() const { return opt_fontsize_; }
383 std::string const & opt_pagestyle() const { return opt_pagestyle_; }
385 std::string const & options() const { return options_; }
387 std::string const & class_header() const { return class_header_; }
389 std::string const & pagestyle() const { return pagestyle_; }
391 docstring const & preamble() const { return preamble_; }
393 docstring const & htmlpreamble() const { return htmlpreamble_; }
394 /// the paragraph style to use for TOCs, Bibliography, etc
395 /// we will attempt to calculate this if it was not given
396 Layout const & htmlTOCLayout() const;
397 /// is this feature already provided by the class?
398 bool provides(std::string const & p) const;
399 /// features required by the class?
400 std::set<std::string> const & requires() const { return requires_; }
402 unsigned int columns() const { return columns_; }
404 PageSides sides() const { return sides_; }
406 int secnumdepth() const { return secnumdepth_; }
408 int tocdepth() const { return tocdepth_; }
410 FontInfo const & defaultfont() const { return defaultfont_; }
411 /// Text that dictates how wide the left margin is on the screen
412 docstring const & leftmargin() const { return leftmargin_; }
413 /// Text that dictates how wide the right margin is on the screen
414 docstring const & rightmargin() const { return rightmargin_; }
415 /// The type of command used to produce a title
416 TitleLatexType titletype() const { return titletype_; }
417 /// The name of the title command
418 std::string const & titlename() const { return titlename_; }
420 int size() const { return layoutlist_.size(); }
421 /// The minimal TocLevel of sectioning layouts
422 int min_toclevel() const { return min_toclevel_; }
423 /// The maximal TocLevel of sectioning layouts
424 int max_toclevel() const { return max_toclevel_; }
425 /// returns true if the class has a ToC structure
426 bool hasTocLevels() const;
428 /// Constructs a DocumentClass based upon a LayoutFile.
429 DocumentClass(LayoutFile const & tc);
430 /// Needed in tex2lyx
433 /// The only class that can create a DocumentClass is
434 /// DocumentClassBundle, which calls the protected constructor.
435 friend class DocumentClassBundle;
437 static InsetLayout plain_insetlayout_;
441 /// DocumentClassBundle is a container for DocumentClass objects, so that
442 /// they stay in memory for use by Insets, CutAndPaste, and the like, even
443 /// when their associated Buffers are destroyed.
444 /// FIXME Some sort of garbage collection or reference counting wouldn't
445 /// be a bad idea here. It might be enough to check when a Buffer is closed
446 /// (or makeDocumentClass is called) whether the old DocumentClass is in use
449 /// This is a singleton class. Its sole instance is accessed via
450 /// DocumentClassBundle::get().
451 class DocumentClassBundle : boost::noncopyable {
453 /// \return The sole instance of this class.
454 static DocumentClassBundle & get();
455 /// \return A new DocumentClass based on baseClass, with info added
456 /// from the modules in modlist.
457 DocumentClass & makeDocumentClass(LayoutFile const & baseClass,
458 LayoutModuleList const & modlist);
460 /// control instantiation
461 DocumentClassBundle() {}
463 ~DocumentClassBundle();
464 /// \return Reference to a new DocumentClass equal to baseClass
465 DocumentClass & newClass(LayoutFile const & baseClass);
467 std::vector<DocumentClass *> documentClasses_;
471 /// convert page sides option to text 1 or 2
472 std::ostream & operator<<(std::ostream & os, PageSides p);