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; }
40 /// A TextClass represents a collection of layout information: At the
41 /// moment, this includes Layout's and InsetLayout's.
43 /// The main function of TextClass objecs is to provide layout information
44 /// to a Buffer, by being the TextClass associated with the BufferParams for
45 /// a given Buffer. This is the object returned by BufferParams::textClass().
46 /// These instances of TextClass do not necessarily correspond just to a
47 /// *.layout file---that is, to a LyX "document class" or *.layout file---
48 /// since a Buffer's TextClass, though always based upon a "document class"
49 /// may be modified by loading modules.
51 /// That said, some TextClass instances do correspond strictly to document
52 /// classes, that is, to *.layout files. These instances are known in the code
53 /// as "base classes". These are cached in BaseClassList.
55 /// Though it does not presently exist, one can imagine an extension of this
56 /// mechanism that would lead to caching of *.module or *.inc files. In that
57 /// case, some TextClass's would just correspond to *.module or *.inc files,
58 /// just as some now correspond to *.layout files.
61 /// The individual paragraph layouts comprising the document class
62 typedef std::vector<LayoutPtr> LayoutList;
63 /// The inset layouts available to this class
64 typedef std::map<docstring, InsetLayout> InsetLayouts;
65 /// Construct a layout with default values. Actual values loaded later.
66 explicit TextClass(std::string const & = std::string(),
67 std::string const & = std::string(),
68 std::string const & = std::string(),
69 bool texClassAvail = false);
71 /// check whether the TeX class is available
72 bool isTeXClassAvailable() const;
74 /// Enumerate the paragraph styles.
75 size_t layoutCount() const { return layoutlist_.size(); }
76 /// Access the paragraph styles.
77 LayoutPtr const & layout(size_t index) const { return layoutlist_[index]; }
79 /// Enum used with TextClass::read
81 BASECLASS, //>This is a base class, i.e., top-level layout file
82 MERGE, //>This is a file included in a layout file
83 MODULE //>This is a layout module
85 /// Performs the read of the layout file.
86 /// \return true on success.
87 bool read(support::FileName const & filename, ReadType rt = BASECLASS);
89 void readOutputType(Lexer &);
91 void readTitleType(Lexer &);
93 void readMaxCounter(Lexer &);
95 void readClassOptions(Lexer &);
97 void readCharStyle(Lexer &, std::string const &);
99 void readFloat(Lexer &);
101 void readCounter(Lexer &);
103 bool hasLayout(docstring const & name) const;
106 LayoutPtr const & operator[](docstring const & vname) const;
108 /// Sees to that the textclass structure has been loaded
109 bool load(std::string const & path = std::string()) const;
110 /// Has this layout file been loaded yet?
111 /// NOTE This only makes sense when used with "static" TextClass
112 /// objects, e.g., ones that represent files on disk, as opposed
113 /// to ones that can be modified by modules.
114 // FIXME Therefore it should return true only for BaseClass objects,
115 // and false for DocumentClass objects.
116 // Indeed, quite generally, those two sorts of objects should now be
117 // disentangled a bit.
118 bool loaded() const { return loaded_; }
120 /// the list of floats defined in the document class
121 FloatList & floats();
122 /// the list of floats defined in the document class
123 FloatList const & floats() const;
124 /// The Counters present in this document class.
125 Counters & counters() const;
126 /// Inset layouts of this doc class
127 InsetLayouts & insetLayouts() const { return insetlayoutlist_; };
129 InsetLayout const & insetLayout(docstring const & name) const;
131 docstring const & defaultLayoutName() const;
133 LayoutPtr const & defaultLayout() const;
134 /// returns a special layout for use when we don't really want one,
135 /// e.g., in table cells
136 LayoutPtr const & emptyLayout() const
137 { return operator[](emptylayout_); };
139 docstring const & emptyLayoutName() const
140 { return emptylayout_; }
142 std::string const & name() const;
144 docstring const & labelstring() const;
146 std::string const & latexname() const;
148 std::string const & description() const;
150 bool isModular() const { return modular_; }
151 /// Sets the layout as a modular one. There is never any
152 /// need to reset this.
153 void markAsModular() { modular_ = true; }
155 std::string const & opt_fontsize() const;
157 std::string const & opt_pagestyle() const;
159 std::string const & options() const;
161 std::string const & class_header() const;
163 std::string const & pagestyle() const;
165 docstring const & preamble() const;
167 /// is this feature already provided by the class?
168 bool provides(std::string const & p) const;
169 /// features required by the class?
170 std::set<std::string> const & requires() const { return requires_; }
173 unsigned int columns() const;
175 PageSides sides() const;
177 int secnumdepth() const;
179 int tocdepth() const;
181 /// Can be LaTeX, DocBook, etc.
182 OutputType outputType() const;
185 FontInfo const & defaultfont() const;
187 /// Text that dictates how wide the left margin is on the screen
188 docstring const & leftmargin() const;
190 /// Text that dictates how wide the right margin is on the screen
191 docstring const & rightmargin() const;
193 /// The type of command used to produce a title
194 TitleLatexType titletype() const;
195 /// The name of the title command
196 std::string const & titlename() const;
200 /// The minimal TocLevel of sectioning layouts
201 int min_toclevel() const;
202 /// The maximal TocLevel of sectioning layouts
203 int max_toclevel() const;
204 /// returns true if the class has a ToC structure
205 bool hasTocLevels() const;
207 static InsetLayout const & emptyInsetLayout() { return empty_insetlayout_; }
209 /// Paragraph styles used in this layout
210 LayoutList layoutlist_;
213 bool deleteLayout(docstring const &);
214 /// \return true for success.
215 bool readStyle(Lexer &, Layout &);
218 /// document class name
219 std::string latexname_;
220 /// document class description
221 std::string description_;
222 /// whether this is a modular layout, i.e., whether it has been
223 /// modified by loading of layout modules.
226 std::string opt_fontsize_;
228 std::string opt_pagestyle_;
229 /// Specific class options
230 std::string options_;
232 std::string pagestyle_;
234 std::string class_header_;
236 docstring defaultlayout_;
237 /// name of empty layout
238 static const docstring emptylayout_;
239 /// preamble text to support layout styles
241 /// latex packages loaded by document class.
242 std::set<std::string> provides_;
243 /// latex packages requested by document class.
244 std::set<std::string> requires_;
246 unsigned int columns_;
249 /// header depth to have numbering
251 /// header depth to appear in table of contents
253 /// Can be LaTeX, DocBook, etc.
254 OutputType outputType_;
255 /** Base font. The paragraph and layout fonts are resolved against
256 this font. This has to be fully instantiated. Attributes
257 FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
260 FontInfo defaultfont_;
261 /// Text that dictates how wide the left margin is on the screen
262 docstring leftmargin_;
264 /// Text that dictates how wide the right margin is on the screen
265 docstring rightmargin_;
267 /// The type of command used to produce a title
268 TitleLatexType titletype_;
269 /// The name of the title command
270 std::string titlename_;
271 /// Input layouts available to this layout
272 mutable InsetLayouts insetlayoutlist_;
274 /// available types of float, eg. figure, algorithm.
275 boost::shared_ptr<FloatList> floatlist_;
277 /// Types of counters, eg. sections, eqns, figures, avail. in document class.
278 boost::shared_ptr<Counters> counters_;
280 /// Has this layout file been loaded yet?
281 mutable bool loaded_;
283 /// Is the TeX class available?
286 /// The minimal TocLevel of sectioning layouts
288 /// The maximal TocLevel of sectioning layouts
291 static InsetLayout empty_insetlayout_;
295 /// This class amounts to little more than a `strong typedef'.
296 /// Its purpose is to control the creation of TextClass objects
297 /// within the DocumentClassBundle.
298 /// These TextClasses represent the layout information that is
299 /// associated with a given buffer.
300 class DocumentClass : public TextClass {
302 bool hasLaTeXLayout(std::string const & lay) const;
304 /// Constructs a DocumentClass based upon a TextClass.
305 DocumentClass(TextClass const & tc);
306 /// The only class that can create a DocumentClass is
307 /// DocumentClassBundle, which calls the private constructor.
308 friend class DocumentClassBundle;
312 /// This is simply a container for the text classes generated when modules
313 /// are read, so that they stay in memory for use by Insets, CutAndPaste,
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(TextClass 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);