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"
20 #include "insets/InsetLayout.h"
22 #include "support/docstring.h"
23 #include "support/types.h"
25 #include <boost/noncopyable.hpp>
34 namespace support { class FileName; }
42 /// Based upon ideas in boost::noncopyable, inheriting from this
43 /// class effectively makes the copy constructor protected but the
44 /// assignment constructor private.
50 ProtectCopy(const ProtectCopy &) {};
52 const ProtectCopy & operator=(const ProtectCopy &);
56 /// A TextClass represents a collection of layout information: At the
57 /// moment, this includes Layout's and InsetLayout's.
59 /// There are two major subclasses of TextClass: LayoutFile and
60 /// DocumentClass. These subclasses are what are actually used in LyX.
61 /// Simple TextClass objects are not directly constructed in the main
62 /// LyX code---the constructor is protected. (That said, in tex2lyx
63 /// there are what amount to simple TextClass objects.)
64 class TextClass : protected ProtectCopy {
67 virtual ~TextClass() {};
68 ///////////////////////////////////////////////////////////////////
70 ///////////////////////////////////////////////////////////////////
71 /// The individual paragraph layouts comprising the document class
72 // NOTE Do NOT try to make this a container of Layout pointers, e.g.,
73 // std::vector<Layout *>. This will lead to problems. The reason is
74 // that DocumentClass objects are generally created by copying a
75 // LayoutFile, which serves as a base for the DocumentClass. If the
76 // LayoutList is a container of pointers, then every DocumentClass
77 // that derives from a given LayoutFile (e.g., article) will SHARE
78 // a basic set of layouts. So if one Buffer were to modify a layout
79 // (say, Standard), that would modify that layout for EVERY Buffer
80 // that was based upon the same DocumentClass. (Of course, if you
81 // really, REALLY want to make LayoutList a vector<Layout *>, then
82 // you can implement custom assignment and copy constructors.)
83 typedef std::vector<Layout> LayoutList;
84 /// The inset layouts available to this class
85 typedef std::map<docstring, InsetLayout> InsetLayouts;
87 typedef LayoutList::const_iterator const_iterator;
89 ///////////////////////////////////////////////////////////////////
91 ///////////////////////////////////////////////////////////////////
93 const_iterator begin() const { return layoutlist_.begin(); }
95 const_iterator end() const { return layoutlist_.end(); }
98 ///////////////////////////////////////////////////////////////////
100 ///////////////////////////////////////////////////////////////////
102 Layout const & defaultLayout() const;
104 docstring const & defaultLayoutName() const;
106 bool isDefaultLayout(Layout const &) const;
108 bool isEmptyLayout(Layout const &) const;
109 /// returns a special layout for use when we don't really want one,
110 /// e.g., in table cells
111 Layout const & emptyLayout() const
112 { return operator[](emptylayout_); };
113 /// the name of the empty layout
114 docstring const & emptyLayoutName() const
115 { return emptylayout_; }
116 /// Enumerate the paragraph styles.
117 size_t layoutCount() const { return layoutlist_.size(); }
119 bool hasLayout(docstring const & name) const;
121 Layout const & operator[](docstring const & vname) const;
123 ///////////////////////////////////////////////////////////////////
125 ///////////////////////////////////////////////////////////////////
126 /// Enum used with TextClass::read
128 BASECLASS, //>This is a base class, i.e., top-level layout file
129 MERGE, //>This is a file included in a layout file
130 MODULE, //>This is a layout module
131 VALIDATION //>We're just validating
133 /// return values for read()
140 /// Performs the read of the layout file.
141 /// \return true on success.
142 bool read(support::FileName const & filename, ReadType rt = BASECLASS);
144 bool read(std::string const & str, ReadType rt = BASECLASS);
146 ReturnValues read(Lexer & lex, ReadType rt = BASECLASS);
147 /// validates the layout information passed in str
148 static bool validate(std::string const & str);
150 ///////////////////////////////////////////////////////////////////
152 ///////////////////////////////////////////////////////////////////
153 /// Sees to it the textclass structure has been loaded
154 bool load(std::string const & path = std::string()) const;
155 /// Has this layout file been loaded yet?
156 /// Overridden by DocumentClass
157 virtual bool loaded() const { return loaded_; }
159 ///////////////////////////////////////////////////////////////////
161 ///////////////////////////////////////////////////////////////////
163 std::string const & name() const { return name_; };
165 std::string const & description() const { return description_; };
167 std::string const & latexname() const { return latexname_; }
169 /// Protect construction
172 Layout & operator[](docstring const & vname);
174 ///////////////////////////////////////////////////////////////////
175 // non-const iterators
176 ///////////////////////////////////////////////////////////////////
178 typedef LayoutList::iterator iterator;
180 iterator begin() { return layoutlist_.begin(); }
182 iterator end() { return layoutlist_.end(); }
184 ///////////////////////////////////////////////////////////////////
186 ///////////////////////////////////////////////////////////////////
187 /// Paragraph styles used in this layout
188 LayoutList layoutlist_;
191 /// document class name
192 std::string latexname_;
193 /// document class description
194 std::string description_;
195 /// available types of float, eg. figure, algorithm.
196 mutable FloatList floatlist_;
197 /// Types of counters, eg. sections, eqns, figures, avail. in document class.
198 mutable Counters counters_;
199 /// Has this layout file been loaded yet?
200 mutable bool loaded_;
201 /// Is the TeX class available?
204 std::string opt_fontsize_;
206 std::string opt_pagestyle_;
207 /// Specific class options
208 std::string options_;
210 std::string pagestyle_;
212 std::string class_header_;
214 docstring defaultlayout_;
215 /// name of empty layout
216 static const docstring emptylayout_;
217 /// preamble text to support layout styles
219 /// latex packages loaded by document class.
220 std::set<std::string> provides_;
221 /// latex packages requested by document class.
222 std::set<std::string> requires_;
224 unsigned int columns_;
227 /// header depth to have numbering
229 /// header depth to appear in table of contents
231 /// Can be LaTeX, DocBook, etc.
232 OutputType outputType_;
233 /** Base font. The paragraph and layout fonts are resolved against
234 this font. This has to be fully instantiated. Attributes
235 FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
238 FontInfo defaultfont_;
239 /// Text that dictates how wide the left margin is on the screen
240 docstring leftmargin_;
241 /// Text that dictates how wide the right margin is on the screen
242 docstring rightmargin_;
243 /// The type of command used to produce a title
244 TitleLatexType titletype_;
245 /// The name of the title command
246 std::string titlename_;
247 /// Input layouts available to this layout
248 InsetLayouts insetlayoutlist_;
249 /// The minimal TocLevel of sectioning layouts
251 /// The maximal TocLevel of sectioning layouts
254 ///////////////////////////////////////////////////////////////////
255 // helper routines for reading layout files
256 ///////////////////////////////////////////////////////////////////
258 bool deleteLayout(docstring const &);
260 bool convertLayoutFormat(support::FileName const &, ReadType);
261 /// \return true for success.
262 bool readStyle(Lexer &, Layout &);
264 void readOutputType(Lexer &);
266 void readTitleType(Lexer &);
268 void readMaxCounter(Lexer &);
270 void readClassOptions(Lexer &);
272 void readCharStyle(Lexer &, std::string const &);
274 void readFloat(Lexer &);
276 void readCounter(Lexer &);
280 /// A DocumentClass represents the layout information associated with a
281 /// Buffer. It is based upon a LayoutFile, but may be modified by loading
282 /// various Modules. It is thus a dynamic object, as opposed to LayoutFile's
283 /// which are pretty much static.
285 /// In the main LyX code, DocumentClass objects are created only by
286 /// DocumentClassBundle, for which see below.
287 class DocumentClass : public TextClass, boost::noncopyable {
290 virtual ~DocumentClass() {}
292 ///////////////////////////////////////////////////////////////////
294 ///////////////////////////////////////////////////////////////////
295 /// \return true if there is a Layout with latexname lay
296 bool hasLaTeXLayout(std::string const & lay) const;
297 /// A DocumentClass nevers count as loaded, since it is dynamic
298 virtual bool loaded() { return false; }
299 /// Inset layouts of this doc class
300 InsetLayouts const & insetLayouts() const { return insetlayoutlist_; };
301 /// \return the layout object of an inset given by name. If the name
302 /// is not found as such, the part after the ':' is stripped off, and
303 /// searched again. In this way, an error fallback can be provided:
304 /// An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
305 /// will invoke the layout object defined by name = 'CharStyle'.
306 /// If that doesn't work either, an empty object returns (shouldn't
307 /// happen). -- Idea JMarc, comment MV
308 InsetLayout const & insetLayout(docstring const & name) const;
309 /// an empty inset layout for use as a default
310 static InsetLayout const & emptyInsetLayout() { return empty_insetlayout_; }
312 ///////////////////////////////////////////////////////////////////
314 ///////////////////////////////////////////////////////////////////
315 /// the list of floats defined in the document class
316 FloatList const & floats() const { return floatlist_; }
318 Counters & counters() const { return counters_; }
320 std::string const & opt_fontsize() const { return opt_fontsize_; }
322 std::string const & opt_pagestyle() const { return opt_pagestyle_; }
324 std::string const & options() const { return options_; }
326 std::string const & class_header() const { return class_header_; }
328 std::string const & pagestyle() const { return pagestyle_; }
330 docstring const & preamble() const { return preamble_; }
331 /// is this feature already provided by the class?
332 bool provides(std::string const & p) const;
333 /// features required by the class?
334 std::set<std::string> const & requires() const { return requires_; }
336 unsigned int columns() const { return columns_; }
338 PageSides sides() const { return sides_; }
340 int secnumdepth() const { return secnumdepth_; }
342 int tocdepth() const { return tocdepth_; }
344 FontInfo const & defaultfont() const { return defaultfont_; }
345 /// Text that dictates how wide the left margin is on the screen
346 docstring const & leftmargin() const { return leftmargin_; }
347 /// Text that dictates how wide the right margin is on the screen
348 docstring const & rightmargin() const { return rightmargin_; }
349 /// The type of command used to produce a title
350 TitleLatexType titletype() const { return titletype_; };
351 /// The name of the title command
352 std::string const & titlename() const { return titlename_; };
354 int size() const { return layoutlist_.size(); }
355 /// The minimal TocLevel of sectioning layouts
356 int min_toclevel() const { return min_toclevel_; }
357 /// The maximal TocLevel of sectioning layouts
358 int max_toclevel() const { return max_toclevel_; }
359 /// returns true if the class has a ToC structure
360 bool hasTocLevels() const;
361 /// Can be LaTeX, DocBook, etc.
362 OutputType outputType() const { return outputType_; }
364 /// Constructs a DocumentClass based upon a LayoutFile.
365 DocumentClass(LayoutFile const & tc);
366 /// Needed in tex2lyx
369 /// The only class that can create a DocumentClass is
370 /// DocumentClassBundle, which calls the protected constructor.
371 friend class DocumentClassBundle;
373 static InsetLayout empty_insetlayout_;
377 /// DocumentClassBundle is a container for DocumentClass objects, so that
378 /// they stay in memory for use by Insets, CutAndPaste, and the like, even
379 /// when their associated Buffers are destroyed.
380 /// FIXME Some sort of garbage collection or reference counting wouldn't
381 /// be a bad idea here. It might be enough to check when a Buffer is closed
382 /// (or makeDocumentClass is called) whether the old DocumentClass is in use
385 /// This is a singleton class. Its sole instance is accessed via
386 /// DocumentClassBundle::get().
387 class DocumentClassBundle : boost::noncopyable {
389 /// \return Pointer to a new class equal to baseClass
390 DocumentClass & newClass(LayoutFile const & baseClass);
391 /// \return The sole instance of this class.
392 static DocumentClassBundle & get();
394 /// control instantiation
395 DocumentClassBundle() {}
397 std::list<DocumentClass *> tc_list_;
401 /// convert page sides option to text 1 or 2
402 std::ostream & operator<<(std::ostream & os, PageSides p);