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"
16 #include "LayoutEnums.h"
17 #include "LayoutPtr.h"
19 #include "insets/InsetLayout.h"
21 #include "support/docstring.h"
22 #include "support/types.h"
24 #include <boost/shared_ptr.hpp>
33 namespace support { class FileName; }
42 /// A TextClass represents a collection of layout information: At the
43 /// moment, this includes Layout's and InsetLayout's.
45 /// There are two major subclasses of TextClass: LayoutFile and
46 /// DocumentClass. These subclasses are what are actually used in LyX.
47 /// Simple TextClass objects are not directly constructed in the main
48 /// LyX code---the constructor is protected. (That said, in tex2lyx
49 /// there are what amount to simple TextClass objects.)
53 virtual ~TextClass() {};
54 ///////////////////////////////////////////////////////////////////
56 ///////////////////////////////////////////////////////////////////
57 /// The individual paragraph layouts comprising the document class
58 typedef std::vector<Layout> LayoutList;
59 /// The inset layouts available to this class
60 typedef std::map<docstring, InsetLayout> InsetLayouts;
62 typedef LayoutList::const_iterator const_iterator;
64 ///////////////////////////////////////////////////////////////////
66 ///////////////////////////////////////////////////////////////////
68 const_iterator begin() const { return layoutlist_.begin(); }
70 const_iterator end() const { return layoutlist_.end(); }
73 ///////////////////////////////////////////////////////////////////
75 ///////////////////////////////////////////////////////////////////
77 Layout const & defaultLayout() const;
79 docstring const & defaultLayoutName() const;
81 bool isDefaultLayout(Layout const &) const;
83 bool isEmptyLayout(Layout const &) const;
84 /// returns a special layout for use when we don't really want one,
85 /// e.g., in table cells
86 Layout const & emptyLayout() const
87 { return operator[](emptylayout_); };
88 /// the name of the empty layout
89 docstring const & emptyLayoutName() const
90 { return emptylayout_; }
91 /// Enumerate the paragraph styles.
92 size_t layoutCount() const { return layoutlist_.size(); }
94 bool hasLayout(docstring const & name) const;
96 Layout const & operator[](docstring const & vname) const;
98 ///////////////////////////////////////////////////////////////////
100 ///////////////////////////////////////////////////////////////////
101 /// Enum used with TextClass::read
103 BASECLASS, //>This is a base class, i.e., top-level layout file
104 MERGE, //>This is a file included in a layout file
105 MODULE //>This is a layout module
107 /// Performs the read of the layout file.
108 /// \return true on success.
109 bool read(support::FileName const & filename, ReadType rt = BASECLASS);
111 ///////////////////////////////////////////////////////////////////
113 ///////////////////////////////////////////////////////////////////
114 /// Sees to it the textclass structure has been loaded
115 bool load(std::string const & path = std::string()) const;
116 /// Has this layout file been loaded yet?
117 /// Overridden by DocumentClass
118 virtual bool loaded() const { return loaded_; }
120 ///////////////////////////////////////////////////////////////////
122 ///////////////////////////////////////////////////////////////////
124 std::string const & name() const { return name_; };
126 std::string const & description() const { return description_; };
128 std::string const & latexname() const { return latexname_; }
130 /// Protect construction
133 Layout & operator[](docstring const & vname);
135 ///////////////////////////////////////////////////////////////////
136 // non-const iterators
137 ///////////////////////////////////////////////////////////////////
139 typedef LayoutList::iterator iterator;
141 iterator begin() { return layoutlist_.begin(); }
143 iterator end() { return layoutlist_.end(); }
145 ///////////////////////////////////////////////////////////////////
147 ///////////////////////////////////////////////////////////////////
148 /// Paragraph styles used in this layout
149 LayoutList layoutlist_;
152 /// document class name
153 std::string latexname_;
154 /// document class description
155 std::string description_;
156 /// available types of float, eg. figure, algorithm.
157 boost::shared_ptr<FloatList> floatlist_;
158 /// Types of counters, eg. sections, eqns, figures, avail. in document class.
159 boost::shared_ptr<Counters> counters_;
160 /// Has this layout file been loaded yet?
161 mutable bool loaded_;
162 /// Is the TeX class available?
165 std::string opt_fontsize_;
167 std::string opt_pagestyle_;
168 /// Specific class options
169 std::string options_;
171 std::string pagestyle_;
173 std::string class_header_;
175 docstring defaultlayout_;
176 /// name of empty layout
177 static const docstring emptylayout_;
178 /// preamble text to support layout styles
180 /// latex packages loaded by document class.
181 std::set<std::string> provides_;
182 /// latex packages requested by document class.
183 std::set<std::string> requires_;
185 unsigned int columns_;
188 /// header depth to have numbering
190 /// header depth to appear in table of contents
192 /// Can be LaTeX, DocBook, etc.
193 OutputType outputType_;
194 /** Base font. The paragraph and layout fonts are resolved against
195 this font. This has to be fully instantiated. Attributes
196 FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
199 FontInfo defaultfont_;
200 /// Text that dictates how wide the left margin is on the screen
201 docstring leftmargin_;
202 /// Text that dictates how wide the right margin is on the screen
203 docstring rightmargin_;
204 /// The type of command used to produce a title
205 TitleLatexType titletype_;
206 /// The name of the title command
207 std::string titlename_;
208 /// Input layouts available to this layout
209 mutable InsetLayouts insetlayoutlist_;
210 /// The minimal TocLevel of sectioning layouts
212 /// The maximal TocLevel of sectioning layouts
215 ///////////////////////////////////////////////////////////////////
216 // helper routines for reading layout files
217 ///////////////////////////////////////////////////////////////////
219 bool deleteLayout(docstring const &);
220 /// \return true for success.
221 bool readStyle(Lexer &, Layout &);
223 void readOutputType(Lexer &);
225 void readTitleType(Lexer &);
227 void readMaxCounter(Lexer &);
229 void readClassOptions(Lexer &);
231 void readCharStyle(Lexer &, std::string const &);
233 void readFloat(Lexer &);
235 void readCounter(Lexer &);
239 /// A DocumentClass represents the layout information associated with a
240 /// Buffer. It is based upon a LayoutFile, but may be modified by loading
241 /// various Modules. It is thus a dynamic object, as opposed to LayoutFile's
242 /// which are pretty much static.
244 /// In the main LyX code, DocumentClass objects are created only by
245 /// DocumentClassBundle, for which see below.
246 class DocumentClass : public TextClass {
249 virtual ~DocumentClass() {}
251 ///////////////////////////////////////////////////////////////////
253 ///////////////////////////////////////////////////////////////////
254 /// \return true if there is a Layout with latexname lay
255 bool hasLaTeXLayout(std::string const & lay) const;
256 /// A DocumentClass nevers count as loaded, since it is dynamic
257 virtual bool loaded() { return false; }
258 /// Inset layouts of this doc class
259 InsetLayouts & insetLayouts() const { return insetlayoutlist_; };
260 /// \return the layout object of an inset given by name. If the name
261 /// is not found as such, the part after the ':' is stripped off, and
262 /// searched again. In this way, an error fallback can be provided:
263 /// An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
264 /// will invoke the layout object defined by name = 'CharStyle'.
265 /// If that doesn't work either, an empty object returns (shouldn't
266 /// happen). -- Idea JMarc, comment MV
268 InsetLayout const & insetLayout(docstring const & name) const;
269 /// an empty inset layout for use as a default
270 static InsetLayout const & emptyInsetLayout() { return empty_insetlayout_; }
272 ///////////////////////////////////////////////////////////////////
274 ///////////////////////////////////////////////////////////////////
275 /// the list of floats defined in the document class
276 FloatList & floats() { return *floatlist_.get(); }
277 /// the list of floats defined in the document class
278 FloatList const & floats() const { return *floatlist_.get(); }
279 /// The Counters present in this document class.
280 Counters & counters() const { return *counters_.get(); }
282 std::string const & opt_fontsize() const { return opt_fontsize_; }
284 std::string const & opt_pagestyle() const { return opt_pagestyle_; }
286 std::string const & options() const { return options_; }
288 std::string const & class_header() const { return class_header_; }
290 std::string const & pagestyle() const { return pagestyle_; }
292 docstring const & preamble() const { return preamble_; }
293 /// is this feature already provided by the class?
294 bool provides(std::string const & p) const;
295 /// features required by the class?
296 std::set<std::string> const & requires() const { return requires_; }
298 unsigned int columns() const { return columns_; }
300 PageSides sides() const { return sides_; }
302 int secnumdepth() const { return secnumdepth_; }
304 int tocdepth() const { return tocdepth_; }
306 FontInfo const & defaultfont() const { return defaultfont_; }
307 /// Text that dictates how wide the left margin is on the screen
308 docstring const & leftmargin() const { return leftmargin_; }
309 /// Text that dictates how wide the right margin is on the screen
310 docstring const & rightmargin() const { return rightmargin_; }
311 /// The type of command used to produce a title
312 TitleLatexType titletype() const { return titletype_; };
313 /// The name of the title command
314 std::string const & titlename() const { return titlename_; };
316 int size() const { return layoutlist_.size(); }
317 /// The minimal TocLevel of sectioning layouts
318 int min_toclevel() const { return min_toclevel_; }
319 /// The maximal TocLevel of sectioning layouts
320 int max_toclevel() const { return max_toclevel_; }
321 /// returns true if the class has a ToC structure
322 bool hasTocLevels() const;
323 /// Can be LaTeX, DocBook, etc.
324 OutputType outputType() const { return outputType_; }
326 /// Constructs a DocumentClass based upon a LayoutFile.
327 DocumentClass(LayoutFile const & tc);
328 /// Needed in tex2lyx
331 /// The only class that can create a DocumentClass is
332 /// DocumentClassBundle, which calls the private constructor.
333 friend class DocumentClassBundle;
335 static InsetLayout empty_insetlayout_;
339 /// DocumentClassBundle is a container for DocumentClass objects, so that
340 /// they stay in memory for use by Insets, CutAndPaste, and the like, even
341 /// when their associated Buffers are destroyed.
342 /// FIXME Some sort of garbage collection or reference counting wouldn't
343 /// be a bad idea here. It might be enough to check when a Buffer is closed
344 /// (or makeDocumentClass is called) whether the old DocumentClass is in use
347 /// This is a singleton class. Its sole instance is accessed via
348 /// DocumentClassBundle::get().
349 class DocumentClassBundle {
351 /// \return Pointer to a new class equal to baseClass
352 DocumentClass & newClass(LayoutFile const & baseClass);
353 /// \return The sole instance of this class.
354 static DocumentClassBundle & get();
356 /// control instantiation
357 DocumentClassBundle() {}
359 DocumentClassBundle(DocumentClassBundle const &);
361 std::list<DocumentClass> tc_list_;
365 /// convert page sides option to text 1 or 2
366 std::ostream & operator<<(std::ostream & os, PageSides p);