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"
18 #include "insets/InsetLayout.h"
20 #include "support/docstring.h"
21 #include "support/types.h"
23 #include <boost/shared_ptr.hpp>
32 namespace support { class FileName; }
41 /// A TextClass represents a collection of layout information: At the
42 /// moment, this includes Layout's and InsetLayout's.
44 /// There are two major subclasses of TextClass: LayoutFile and
45 /// DocumentClass. These subclasses are what are actually used in LyX.
46 /// Simple TextClass objects are not directly constructed in the main
47 /// LyX code---the constructor is protected. (That said, in tex2lyx
48 /// there are what amount to simple TextClass objects.)
52 virtual ~TextClass() {};
53 ///////////////////////////////////////////////////////////////////
55 ///////////////////////////////////////////////////////////////////
56 /// The individual paragraph layouts comprising the document class
57 typedef std::vector<Layout> LayoutList;
58 /// The inset layouts available to this class
59 typedef std::map<docstring, InsetLayout> InsetLayouts;
61 typedef LayoutList::const_iterator const_iterator;
63 ///////////////////////////////////////////////////////////////////
65 ///////////////////////////////////////////////////////////////////
67 const_iterator begin() const { return layoutlist_.begin(); }
69 const_iterator end() const { return layoutlist_.end(); }
72 ///////////////////////////////////////////////////////////////////
74 ///////////////////////////////////////////////////////////////////
76 Layout const & defaultLayout() const;
78 docstring const & defaultLayoutName() const;
80 bool isDefaultLayout(Layout const &) const;
82 bool isEmptyLayout(Layout const &) const;
83 /// returns a special layout for use when we don't really want one,
84 /// e.g., in table cells
85 Layout const & emptyLayout() const
86 { return operator[](emptylayout_); };
87 /// the name of the empty layout
88 docstring const & emptyLayoutName() const
89 { return emptylayout_; }
90 /// Enumerate the paragraph styles.
91 size_t layoutCount() const { return layoutlist_.size(); }
93 bool hasLayout(docstring const & name) const;
95 Layout const & operator[](docstring const & vname) const;
97 ///////////////////////////////////////////////////////////////////
99 ///////////////////////////////////////////////////////////////////
100 /// Enum used with TextClass::read
102 BASECLASS, //>This is a base class, i.e., top-level layout file
103 MERGE, //>This is a file included in a layout file
104 MODULE //>This is a layout module
106 /// Performs the read of the layout file.
107 /// \return true on success.
108 bool read(support::FileName const & filename, ReadType rt = BASECLASS);
110 ///////////////////////////////////////////////////////////////////
112 ///////////////////////////////////////////////////////////////////
113 /// Sees to it the textclass structure has been loaded
114 bool load(std::string const & path = std::string()) const;
115 /// Has this layout file been loaded yet?
116 /// Overridden by DocumentClass
117 virtual bool loaded() const { return loaded_; }
119 ///////////////////////////////////////////////////////////////////
121 ///////////////////////////////////////////////////////////////////
123 std::string const & name() const { return name_; };
125 std::string const & description() const { return description_; };
127 std::string const & latexname() const { return latexname_; }
129 /// Protect construction
132 Layout & operator[](docstring const & vname);
134 ///////////////////////////////////////////////////////////////////
135 // non-const iterators
136 ///////////////////////////////////////////////////////////////////
138 typedef LayoutList::iterator iterator;
140 iterator begin() { return layoutlist_.begin(); }
142 iterator end() { return layoutlist_.end(); }
144 ///////////////////////////////////////////////////////////////////
146 ///////////////////////////////////////////////////////////////////
147 /// Paragraph styles used in this layout
148 LayoutList layoutlist_;
151 /// document class name
152 std::string latexname_;
153 /// document class description
154 std::string description_;
155 /// available types of float, eg. figure, algorithm.
156 boost::shared_ptr<FloatList> floatlist_;
157 /// Types of counters, eg. sections, eqns, figures, avail. in document class.
158 boost::shared_ptr<Counters> counters_;
159 /// Has this layout file been loaded yet?
160 mutable bool loaded_;
161 /// Is the TeX class available?
164 std::string opt_fontsize_;
166 std::string opt_pagestyle_;
167 /// Specific class options
168 std::string options_;
170 std::string pagestyle_;
172 std::string class_header_;
174 docstring defaultlayout_;
175 /// name of empty layout
176 static const docstring emptylayout_;
177 /// preamble text to support layout styles
179 /// latex packages loaded by document class.
180 std::set<std::string> provides_;
181 /// latex packages requested by document class.
182 std::set<std::string> requires_;
184 unsigned int columns_;
187 /// header depth to have numbering
189 /// header depth to appear in table of contents
191 /// Can be LaTeX, DocBook, etc.
192 OutputType outputType_;
193 /** Base font. The paragraph and layout fonts are resolved against
194 this font. This has to be fully instantiated. Attributes
195 FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
198 FontInfo defaultfont_;
199 /// Text that dictates how wide the left margin is on the screen
200 docstring leftmargin_;
201 /// Text that dictates how wide the right margin is on the screen
202 docstring rightmargin_;
203 /// The type of command used to produce a title
204 TitleLatexType titletype_;
205 /// The name of the title command
206 std::string titlename_;
207 /// Input layouts available to this layout
208 mutable InsetLayouts insetlayoutlist_;
209 /// The minimal TocLevel of sectioning layouts
211 /// The maximal TocLevel of sectioning layouts
214 ///////////////////////////////////////////////////////////////////
215 // helper routines for reading layout files
216 ///////////////////////////////////////////////////////////////////
218 bool deleteLayout(docstring const &);
219 /// \return true for success.
220 bool readStyle(Lexer &, Layout &);
222 void readOutputType(Lexer &);
224 void readTitleType(Lexer &);
226 void readMaxCounter(Lexer &);
228 void readClassOptions(Lexer &);
230 void readCharStyle(Lexer &, std::string const &);
232 void readFloat(Lexer &);
234 void readCounter(Lexer &);
238 /// A DocumentClass represents the layout information associated with a
239 /// Buffer. It is based upon a LayoutFile, but may be modified by loading
240 /// various Modules. It is thus a dynamic object, as opposed to LayoutFile's
241 /// which are pretty much static.
243 /// In the main LyX code, DocumentClass objects are created only by
244 /// DocumentClassBundle, for which see below.
245 class DocumentClass : public TextClass {
248 virtual ~DocumentClass() {}
250 ///////////////////////////////////////////////////////////////////
252 ///////////////////////////////////////////////////////////////////
253 /// \return true if there is a Layout with latexname lay
254 bool hasLaTeXLayout(std::string const & lay) const;
255 /// A DocumentClass nevers count as loaded, since it is dynamic
256 virtual bool loaded() { return false; }
257 /// Inset layouts of this doc class
258 InsetLayouts & insetLayouts() const { return insetlayoutlist_; };
259 /// \return the layout object of an inset given by name. If the name
260 /// is not found as such, the part after the ':' is stripped off, and
261 /// searched again. In this way, an error fallback can be provided:
262 /// An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
263 /// will invoke the layout object defined by name = 'CharStyle'.
264 /// If that doesn't work either, an empty object returns (shouldn't
265 /// happen). -- Idea JMarc, comment MV
267 InsetLayout const & insetLayout(docstring const & name) const;
268 /// an empty inset layout for use as a default
269 static InsetLayout const & emptyInsetLayout() { return empty_insetlayout_; }
271 ///////////////////////////////////////////////////////////////////
273 ///////////////////////////////////////////////////////////////////
274 /// the list of floats defined in the document class
275 FloatList & floats() { return *floatlist_.get(); }
276 /// the list of floats defined in the document class
277 FloatList const & floats() const { return *floatlist_.get(); }
278 /// The Counters present in this document class.
279 Counters & counters() const { return *counters_.get(); }
281 std::string const & opt_fontsize() const { return opt_fontsize_; }
283 std::string const & opt_pagestyle() const { return opt_pagestyle_; }
285 std::string const & options() const { return options_; }
287 std::string const & class_header() const { return class_header_; }
289 std::string const & pagestyle() const { return pagestyle_; }
291 docstring const & preamble() const { return preamble_; }
292 /// is this feature already provided by the class?
293 bool provides(std::string const & p) const;
294 /// features required by the class?
295 std::set<std::string> const & requires() const { return requires_; }
297 unsigned int columns() const { return columns_; }
299 PageSides sides() const { return sides_; }
301 int secnumdepth() const { return secnumdepth_; }
303 int tocdepth() const { return tocdepth_; }
305 FontInfo const & defaultfont() const { return defaultfont_; }
306 /// Text that dictates how wide the left margin is on the screen
307 docstring const & leftmargin() const { return leftmargin_; }
308 /// Text that dictates how wide the right margin is on the screen
309 docstring const & rightmargin() const { return rightmargin_; }
310 /// The type of command used to produce a title
311 TitleLatexType titletype() const { return titletype_; };
312 /// The name of the title command
313 std::string const & titlename() const { return titlename_; };
315 int size() const { return layoutlist_.size(); }
316 /// The minimal TocLevel of sectioning layouts
317 int min_toclevel() const { return min_toclevel_; }
318 /// The maximal TocLevel of sectioning layouts
319 int max_toclevel() const { return max_toclevel_; }
320 /// returns true if the class has a ToC structure
321 bool hasTocLevels() const;
322 /// Can be LaTeX, DocBook, etc.
323 OutputType outputType() const { return outputType_; }
325 /// Constructs a DocumentClass based upon a LayoutFile.
326 DocumentClass(LayoutFile const & tc);
327 /// Needed in tex2lyx
330 /// The only class that can create a DocumentClass is
331 /// DocumentClassBundle, which calls the private constructor.
332 friend class DocumentClassBundle;
334 static InsetLayout empty_insetlayout_;
338 /// DocumentClassBundle is a container for DocumentClass objects, so that
339 /// they stay in memory for use by Insets, CutAndPaste, and the like, even
340 /// when their associated Buffers are destroyed.
341 /// FIXME Some sort of garbage collection or reference counting wouldn't
342 /// be a bad idea here. It might be enough to check when a Buffer is closed
343 /// (or makeDocumentClass is called) whether the old DocumentClass is in use
346 /// This is a singleton class. Its sole instance is accessed via
347 /// DocumentClassBundle::get().
348 class DocumentClassBundle {
350 /// \return Pointer to a new class equal to baseClass
351 DocumentClass & newClass(LayoutFile const & baseClass);
352 /// \return The sole instance of this class.
353 static DocumentClassBundle & get();
355 /// control instantiation
356 DocumentClassBundle() {}
358 DocumentClassBundle(DocumentClassBundle const &);
360 std::list<DocumentClass> tc_list_;
364 /// convert page sides option to text 1 or 2
365 std::ostream & operator<<(std::ostream & os, PageSides p);