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 typedef std::vector<Layout> LayoutList;
73 /// The inset layouts available to this class
74 typedef std::map<docstring, InsetLayout> InsetLayouts;
76 typedef LayoutList::const_iterator const_iterator;
78 ///////////////////////////////////////////////////////////////////
80 ///////////////////////////////////////////////////////////////////
82 const_iterator begin() const { return layoutlist_.begin(); }
84 const_iterator end() const { return layoutlist_.end(); }
87 ///////////////////////////////////////////////////////////////////
89 ///////////////////////////////////////////////////////////////////
91 Layout const & defaultLayout() const;
93 docstring const & defaultLayoutName() const;
95 bool isDefaultLayout(Layout const &) const;
97 bool isEmptyLayout(Layout const &) const;
98 /// returns a special layout for use when we don't really want one,
99 /// e.g., in table cells
100 Layout const & emptyLayout() const
101 { return operator[](emptylayout_); };
102 /// the name of the empty layout
103 docstring const & emptyLayoutName() const
104 { return emptylayout_; }
105 /// Enumerate the paragraph styles.
106 size_t layoutCount() const { return layoutlist_.size(); }
108 bool hasLayout(docstring const & name) const;
110 Layout const & operator[](docstring const & vname) const;
112 ///////////////////////////////////////////////////////////////////
114 ///////////////////////////////////////////////////////////////////
115 /// Enum used with TextClass::read
117 BASECLASS, //>This is a base class, i.e., top-level layout file
118 MERGE, //>This is a file included in a layout file
119 MODULE //>This is a layout module
121 /// Performs the read of the layout file.
122 /// \return true on success.
123 bool read(support::FileName const & filename, ReadType rt = BASECLASS);
125 ///////////////////////////////////////////////////////////////////
127 ///////////////////////////////////////////////////////////////////
128 /// Sees to it the textclass structure has been loaded
129 bool load(std::string const & path = std::string()) const;
130 /// Has this layout file been loaded yet?
131 /// Overridden by DocumentClass
132 virtual bool loaded() const { return loaded_; }
134 ///////////////////////////////////////////////////////////////////
136 ///////////////////////////////////////////////////////////////////
138 std::string const & name() const { return name_; };
140 std::string const & description() const { return description_; };
142 std::string const & latexname() const { return latexname_; }
144 /// Protect construction
147 Layout & operator[](docstring const & vname);
149 ///////////////////////////////////////////////////////////////////
150 // non-const iterators
151 ///////////////////////////////////////////////////////////////////
153 typedef LayoutList::iterator iterator;
155 iterator begin() { return layoutlist_.begin(); }
157 iterator end() { return layoutlist_.end(); }
159 ///////////////////////////////////////////////////////////////////
161 ///////////////////////////////////////////////////////////////////
162 /// Paragraph styles used in this layout
163 LayoutList layoutlist_;
166 /// document class name
167 std::string latexname_;
168 /// document class description
169 std::string description_;
170 /// available types of float, eg. figure, algorithm.
171 mutable FloatList floatlist_;
172 /// Types of counters, eg. sections, eqns, figures, avail. in document class.
173 mutable Counters counters_;
174 /// Has this layout file been loaded yet?
175 mutable bool loaded_;
176 /// Is the TeX class available?
179 std::string opt_fontsize_;
181 std::string opt_pagestyle_;
182 /// Specific class options
183 std::string options_;
185 std::string pagestyle_;
187 std::string class_header_;
189 docstring defaultlayout_;
190 /// name of empty layout
191 static const docstring emptylayout_;
192 /// preamble text to support layout styles
194 /// latex packages loaded by document class.
195 std::set<std::string> provides_;
196 /// latex packages requested by document class.
197 std::set<std::string> requires_;
199 unsigned int columns_;
202 /// header depth to have numbering
204 /// header depth to appear in table of contents
206 /// Can be LaTeX, DocBook, etc.
207 OutputType outputType_;
208 /** Base font. The paragraph and layout fonts are resolved against
209 this font. This has to be fully instantiated. Attributes
210 FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
213 FontInfo defaultfont_;
214 /// Text that dictates how wide the left margin is on the screen
215 docstring leftmargin_;
216 /// Text that dictates how wide the right margin is on the screen
217 docstring rightmargin_;
218 /// The type of command used to produce a title
219 TitleLatexType titletype_;
220 /// The name of the title command
221 std::string titlename_;
222 /// Input layouts available to this layout
223 InsetLayouts insetlayoutlist_;
224 /// The minimal TocLevel of sectioning layouts
226 /// The maximal TocLevel of sectioning layouts
229 ///////////////////////////////////////////////////////////////////
230 // helper routines for reading layout files
231 ///////////////////////////////////////////////////////////////////
233 bool deleteLayout(docstring const &);
234 /// \return true for success.
235 bool readStyle(Lexer &, Layout &);
237 void readOutputType(Lexer &);
239 void readTitleType(Lexer &);
241 void readMaxCounter(Lexer &);
243 void readClassOptions(Lexer &);
245 void readCharStyle(Lexer &, std::string const &);
247 void readFloat(Lexer &);
249 void readCounter(Lexer &);
253 /// A DocumentClass represents the layout information associated with a
254 /// Buffer. It is based upon a LayoutFile, but may be modified by loading
255 /// various Modules. It is thus a dynamic object, as opposed to LayoutFile's
256 /// which are pretty much static.
258 /// In the main LyX code, DocumentClass objects are created only by
259 /// DocumentClassBundle, for which see below.
260 class DocumentClass : public TextClass, boost::noncopyable {
263 virtual ~DocumentClass() {}
265 ///////////////////////////////////////////////////////////////////
267 ///////////////////////////////////////////////////////////////////
268 /// \return true if there is a Layout with latexname lay
269 bool hasLaTeXLayout(std::string const & lay) const;
270 /// A DocumentClass nevers count as loaded, since it is dynamic
271 virtual bool loaded() { return false; }
272 /// Inset layouts of this doc class
273 InsetLayouts const & insetLayouts() const { return insetlayoutlist_; };
274 /// \return the layout object of an inset given by name. If the name
275 /// is not found as such, the part after the ':' is stripped off, and
276 /// searched again. In this way, an error fallback can be provided:
277 /// An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
278 /// will invoke the layout object defined by name = 'CharStyle'.
279 /// If that doesn't work either, an empty object returns (shouldn't
280 /// happen). -- Idea JMarc, comment MV
282 InsetLayout const & insetLayout(docstring const & name) const;
283 /// an empty inset layout for use as a default
284 static InsetLayout const & emptyInsetLayout() { return empty_insetlayout_; }
286 ///////////////////////////////////////////////////////////////////
288 ///////////////////////////////////////////////////////////////////
289 /// the list of floats defined in the document class
290 /// the list of floats defined in the document class
291 FloatList const & floats() const { return floatlist_; }
293 Counters & counters() const { return counters_; }
295 std::string const & opt_fontsize() const { return opt_fontsize_; }
297 std::string const & opt_pagestyle() const { return opt_pagestyle_; }
299 std::string const & options() const { return options_; }
301 std::string const & class_header() const { return class_header_; }
303 std::string const & pagestyle() const { return pagestyle_; }
305 docstring const & preamble() const { return preamble_; }
306 /// is this feature already provided by the class?
307 bool provides(std::string const & p) const;
308 /// features required by the class?
309 std::set<std::string> const & requires() const { return requires_; }
311 unsigned int columns() const { return columns_; }
313 PageSides sides() const { return sides_; }
315 int secnumdepth() const { return secnumdepth_; }
317 int tocdepth() const { return tocdepth_; }
319 FontInfo const & defaultfont() const { return defaultfont_; }
320 /// Text that dictates how wide the left margin is on the screen
321 docstring const & leftmargin() const { return leftmargin_; }
322 /// Text that dictates how wide the right margin is on the screen
323 docstring const & rightmargin() const { return rightmargin_; }
324 /// The type of command used to produce a title
325 TitleLatexType titletype() const { return titletype_; };
326 /// The name of the title command
327 std::string const & titlename() const { return titlename_; };
329 int size() const { return layoutlist_.size(); }
330 /// The minimal TocLevel of sectioning layouts
331 int min_toclevel() const { return min_toclevel_; }
332 /// The maximal TocLevel of sectioning layouts
333 int max_toclevel() const { return max_toclevel_; }
334 /// returns true if the class has a ToC structure
335 bool hasTocLevels() const;
336 /// Can be LaTeX, DocBook, etc.
337 OutputType outputType() const { return outputType_; }
339 /// Constructs a DocumentClass based upon a LayoutFile.
340 DocumentClass(LayoutFile const & tc);
341 /// Needed in tex2lyx
344 /// The only class that can create a DocumentClass is
345 /// DocumentClassBundle, which calls the protected constructor.
346 friend class DocumentClassBundle;
348 static InsetLayout empty_insetlayout_;
352 /// DocumentClassBundle is a container for DocumentClass objects, so that
353 /// they stay in memory for use by Insets, CutAndPaste, and the like, even
354 /// when their associated Buffers are destroyed.
355 /// FIXME Some sort of garbage collection or reference counting wouldn't
356 /// be a bad idea here. It might be enough to check when a Buffer is closed
357 /// (or makeDocumentClass is called) whether the old DocumentClass is in use
360 /// This is a singleton class. Its sole instance is accessed via
361 /// DocumentClassBundle::get().
362 class DocumentClassBundle : boost::noncopyable {
364 /// \return Pointer to a new class equal to baseClass
365 DocumentClass & newClass(LayoutFile const & baseClass);
366 /// \return The sole instance of this class.
367 static DocumentClassBundle & get();
369 /// control instantiation
370 DocumentClassBundle() {}
372 std::list<DocumentClass *> tc_list_;
376 /// convert page sides option to text 1 or 2
377 std::ostream & operator<<(std::ostream & os, PageSides p);