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 "LayoutEnums.h"
16 #include "LayoutPtr.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<LayoutPtr> LayoutList;
58 /// The inset layouts available to this class
59 typedef std::map<docstring, InsetLayout> InsetLayouts;
61 ///////////////////////////////////////////////////////////////////
63 ///////////////////////////////////////////////////////////////////
65 LayoutPtr const & defaultLayout() const;
67 docstring const & defaultLayoutName() const;
68 /// returns a special layout for use when we don't really want one,
69 /// e.g., in table cells
70 LayoutPtr const & emptyLayout() const
71 { return operator[](emptylayout_); };
72 /// the name of the empty layout
73 docstring const & emptyLayoutName() const
74 { return emptylayout_; }
75 /// Enumerate the paragraph styles.
76 size_t layoutCount() const { return layoutlist_.size(); }
77 /// Access the paragraph styles.
78 LayoutPtr const & layout(size_t index) const { return layoutlist_[index]; }
80 bool hasLayout(docstring const & name) const;
82 LayoutPtr const & operator[](docstring const & vname) const;
84 ///////////////////////////////////////////////////////////////////
86 ///////////////////////////////////////////////////////////////////
87 /// Enum used with TextClass::read
89 BASECLASS, //>This is a base class, i.e., top-level layout file
90 MERGE, //>This is a file included in a layout file
91 MODULE //>This is a layout module
93 /// Performs the read of the layout file.
94 /// \return true on success.
95 bool read(support::FileName const & filename, ReadType rt = BASECLASS);
97 ///////////////////////////////////////////////////////////////////
99 ///////////////////////////////////////////////////////////////////
100 /// Sees to it the textclass structure has been loaded
101 bool load(std::string const & path = std::string()) const;
102 /// Has this layout file been loaded yet?
103 /// Overridden by DocumentClass
104 virtual bool loaded() const { return loaded_; }
106 ///////////////////////////////////////////////////////////////////
108 ///////////////////////////////////////////////////////////////////
110 std::string const & name() const { return name_; };
112 std::string const & description() const { return description_; };
114 std::string const & latexname() const { return latexname_; }
116 /// Protect construction
118 ///////////////////////////////////////////////////////////////////
120 ///////////////////////////////////////////////////////////////////
121 /// Paragraph styles used in this layout
122 LayoutList layoutlist_;
125 /// document class name
126 std::string latexname_;
127 /// document class description
128 std::string description_;
129 /// available types of float, eg. figure, algorithm.
130 boost::shared_ptr<FloatList> floatlist_;
131 /// Types of counters, eg. sections, eqns, figures, avail. in document class.
132 boost::shared_ptr<Counters> counters_;
133 /// Has this layout file been loaded yet?
134 mutable bool loaded_;
135 /// Is the TeX class available?
138 std::string opt_fontsize_;
140 std::string opt_pagestyle_;
141 /// Specific class options
142 std::string options_;
144 std::string pagestyle_;
146 std::string class_header_;
148 docstring defaultlayout_;
149 /// name of empty layout
150 static const docstring emptylayout_;
151 /// preamble text to support layout styles
153 /// latex packages loaded by document class.
154 std::set<std::string> provides_;
155 /// latex packages requested by document class.
156 std::set<std::string> requires_;
158 unsigned int columns_;
161 /// header depth to have numbering
163 /// header depth to appear in table of contents
165 /// Can be LaTeX, DocBook, etc.
166 OutputType outputType_;
167 /** Base font. The paragraph and layout fonts are resolved against
168 this font. This has to be fully instantiated. Attributes
169 FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
172 FontInfo defaultfont_;
173 /// Text that dictates how wide the left margin is on the screen
174 docstring leftmargin_;
175 /// Text that dictates how wide the right margin is on the screen
176 docstring rightmargin_;
177 /// The type of command used to produce a title
178 TitleLatexType titletype_;
179 /// The name of the title command
180 std::string titlename_;
181 /// Input layouts available to this layout
182 mutable InsetLayouts insetlayoutlist_;
183 /// The minimal TocLevel of sectioning layouts
185 /// The maximal TocLevel of sectioning layouts
188 ///////////////////////////////////////////////////////////////////
189 // helper routines for reading layout files
190 ///////////////////////////////////////////////////////////////////
192 bool deleteLayout(docstring const &);
193 /// \return true for success.
194 bool readStyle(Lexer &, Layout &);
196 void readOutputType(Lexer &);
198 void readTitleType(Lexer &);
200 void readMaxCounter(Lexer &);
202 void readClassOptions(Lexer &);
204 void readCharStyle(Lexer &, std::string const &);
206 void readFloat(Lexer &);
208 void readCounter(Lexer &);
212 /// A DocumentClass represents the layout information associated with a
213 /// Buffer. It is based upon a LayoutFile, but may be modified by loading
214 /// various Modules. It is thus a dynamic object, as opposed to LayoutFile's
215 /// which are pretty much static.
217 /// In the main LyX code, DocumentClass objects are created only by
218 /// DocumentClassBundle, for which see below.
219 class DocumentClass : public TextClass {
222 virtual ~DocumentClass() {}
224 ///////////////////////////////////////////////////////////////////
226 ///////////////////////////////////////////////////////////////////
227 /// \return true if there is a Layout with latexname lay
228 bool hasLaTeXLayout(std::string const & lay) const;
229 /// A DocumentClass nevers count as loaded, since it is dynamic
230 virtual bool loaded() { return false; }
231 /// Inset layouts of this doc class
232 InsetLayouts & insetLayouts() const { return insetlayoutlist_; };
233 /// \return the layout object of an inset given by name. If the name
234 /// is not found as such, the part after the ':' is stripped off, and
235 /// searched again. In this way, an error fallback can be provided:
236 /// An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
237 /// will invoke the layout object defined by name = 'CharStyle'.
238 /// If that doesn't work either, an empty object returns (shouldn't
239 /// happen). -- Idea JMarc, comment MV
241 InsetLayout const & insetLayout(docstring const & name) const;
242 /// an empty inset layout for use as a default
243 static InsetLayout const & emptyInsetLayout() { return empty_insetlayout_; }
245 ///////////////////////////////////////////////////////////////////
247 ///////////////////////////////////////////////////////////////////
248 /// the list of floats defined in the document class
249 FloatList & floats() { return *floatlist_.get(); }
250 /// the list of floats defined in the document class
251 FloatList const & floats() const { return *floatlist_.get(); }
252 /// The Counters present in this document class.
253 Counters & counters() const { return *counters_.get(); }
255 std::string const & opt_fontsize() const { return opt_fontsize_; }
257 std::string const & opt_pagestyle() const { return opt_pagestyle_; }
259 std::string const & options() const { return options_; }
261 std::string const & class_header() const { return class_header_; }
263 std::string const & pagestyle() const { return pagestyle_; }
265 docstring const & preamble() const { return preamble_; }
266 /// is this feature already provided by the class?
267 bool provides(std::string const & p) const;
268 /// features required by the class?
269 std::set<std::string> const & requires() const { return requires_; }
271 unsigned int columns() const { return columns_; }
273 PageSides sides() const { return sides_; }
275 int secnumdepth() const { return secnumdepth_; }
277 int tocdepth() const { return tocdepth_; }
279 FontInfo const & defaultfont() const { return defaultfont_; }
280 /// Text that dictates how wide the left margin is on the screen
281 docstring const & leftmargin() const { return leftmargin_; }
282 /// Text that dictates how wide the right margin is on the screen
283 docstring const & rightmargin() const { return rightmargin_; }
284 /// The type of command used to produce a title
285 TitleLatexType titletype() const { return titletype_; };
286 /// The name of the title command
287 std::string const & titlename() const { return titlename_; };
289 int size() const { return layoutlist_.size(); }
290 /// The minimal TocLevel of sectioning layouts
291 int min_toclevel() const { return min_toclevel_; }
292 /// The maximal TocLevel of sectioning layouts
293 int max_toclevel() const { return max_toclevel_; }
294 /// returns true if the class has a ToC structure
295 bool hasTocLevels() const;
296 /// Can be LaTeX, DocBook, etc.
297 OutputType outputType() const { return outputType_; }
299 /// Constructs a DocumentClass based upon a LayoutFile.
300 DocumentClass(LayoutFile const & tc);
301 /// Needed in tex2lyx
304 /// The only class that can create a DocumentClass is
305 /// DocumentClassBundle, which calls the private constructor.
306 friend class DocumentClassBundle;
308 static InsetLayout empty_insetlayout_;
312 /// DocumentClassBundle is a container for DocumentClass objects, so that
313 /// they stay in memory for use by Insets, CutAndPaste, and the like, even
314 /// when their associated Buffers are destroyed.
315 /// FIXME Some sort of garbage collection or reference counting wouldn't
316 /// be a bad idea here. It might be enough to check when a Buffer is closed
317 /// (or makeDocumentClass is called) whether the old DocumentClass is in use
320 /// This is a singleton class. Its sole instance is accessed via
321 /// DocumentClassBundle::get().
322 class DocumentClassBundle {
324 /// \return Pointer to a new class equal to baseClass
325 DocumentClass & newClass(LayoutFile const & baseClass);
326 /// \return The sole instance of this class.
327 static DocumentClassBundle & get();
329 /// control instantiation
330 DocumentClassBundle() {}
332 DocumentClassBundle(DocumentClassBundle const &);
334 std::list<DocumentClass> tc_list_;
338 /// convert page sides option to text 1 or 2
339 std::ostream & operator<<(std::ostream & os, PageSides p);