]> git.lyx.org Git - lyx.git/blob - src/TextClass.h
Fix bug #5131: Remember last file active.
[lyx.git] / src / TextClass.h
1 // -*- C++ -*-
2 /**
3  * \file TextClass.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * Full author contact details are available in file CREDITS.
8  */
9
10 #ifndef TEXTCLASS_H
11 #define TEXTCLASS_H
12
13 #include "ColorCode.h"
14 #include "Counters.h"
15 #include "FloatList.h"
16 #include "FontInfo.h"
17 #include "Layout.h"
18 #include "LayoutEnums.h"
19 #include "LayoutModuleList.h"
20
21 #include "insets/InsetLayout.h"
22
23 #include "support/docstring.h"
24 #include "support/types.h"
25
26 #include <boost/noncopyable.hpp>
27
28 #include <map>
29 #include <set>
30 #include <vector>
31 #include <list>
32
33 namespace lyx {
34
35 namespace support { class FileName; }
36
37 class Counters;
38 class FloatList;
39 class Layout;
40 class LayoutFile;
41 class Lexer;
42
43 /// Based upon ideas in boost::noncopyable, inheriting from this
44 /// class effectively makes the copy constructor protected but the
45 /// assignment constructor private.
46 class ProtectCopy
47 {
48 protected:
49         ProtectCopy() {}
50         ~ProtectCopy() {}
51         ProtectCopy(const ProtectCopy &) {};
52 private:
53         const ProtectCopy & operator=(const ProtectCopy &);
54 };
55
56
57 /// A TextClass represents a collection of layout information: At the 
58 /// moment, this includes Layout's and InsetLayout's.
59 ///
60 /// There are two major subclasses of TextClass: LayoutFile and
61 /// DocumentClass. These subclasses are what are actually used in LyX.
62 /// Simple TextClass objects are not directly constructed in the main 
63 /// LyX code---the constructor is protected. (That said, in tex2lyx
64 /// there are what amount to simple TextClass objects.)
65 ///
66 /// A LayoutFile (see LayoutFile.{h,cpp}) represents a *.layout file.
67 /// These are generally static objects---though they can be reloaded 
68 /// from disk via LFUN_LAYOUT_RELOAD, so one should not assume that 
69 /// they will never change.
70 ///
71 /// A DocumentClass (see below) represents the layout information that
72 /// is associated with a given Buffer. These are static, in the sense
73 /// that they will not themselves change, but which DocumentClass is
74 /// associated with a Buffer can change, as modules are loaded and 
75 /// unloaded, for example.
76 ///
77 class TextClass : protected ProtectCopy {
78 public:
79         ///
80         virtual ~TextClass() {};
81         ///////////////////////////////////////////////////////////////////
82         // typedefs
83         ///////////////////////////////////////////////////////////////////
84         /// The individual paragraph layouts comprising the document class
85         // NOTE Do NOT try to make this a container of Layout pointers, e.g.,
86         // std::vector<Layout *>. This will lead to problems. The reason is
87         // that DocumentClass objects are generally created by copying a 
88         // LayoutFile, which serves as a base for the DocumentClass. If the
89         // LayoutList is a container of pointers, then every DocumentClass
90         // that derives from a given LayoutFile (e.g., article) will SHARE
91         // a basic set of layouts. So if one Buffer were to modify a layout
92         // (say, Standard), that would modify that layout for EVERY Buffer
93         // that was based upon the same DocumentClass. (Of course, if you 
94         // really, REALLY want to make LayoutList a vector<Layout *>, then
95         // you can implement custom assignment and copy constructors.)
96         //
97         // NOTE: Layout pointers are directly assigned to paragraphs so a
98         // container that does not invalidate these pointers after insertion
99         // is needed.
100         typedef std::list<Layout> LayoutList;
101         /// The inset layouts available to this class
102         typedef std::map<docstring, InsetLayout> InsetLayouts;
103         ///
104         typedef LayoutList::const_iterator const_iterator;
105
106         ///////////////////////////////////////////////////////////////////
107         // Iterators
108         ///////////////////////////////////////////////////////////////////
109         ///
110         const_iterator begin() const { return layoutlist_.begin(); }
111         ///
112         const_iterator end() const { return layoutlist_.end(); }
113
114
115         ///////////////////////////////////////////////////////////////////
116         // Layout Info
117         ///////////////////////////////////////////////////////////////////
118         ///
119         Layout const & defaultLayout() const;
120         ///
121         docstring const & defaultLayoutName() const;
122         ///
123         bool isDefaultLayout(Layout const &) const;
124         /// 
125         bool isPlainLayout(Layout const &) const;
126         /// returns a special layout for use when we don't really want one,
127         /// e.g., in table cells
128         Layout const & plainLayout() const 
129                         { return operator[](plain_layout_); };
130         /// the name of the plain layout
131         docstring const & plainLayoutName() const 
132                         { return plain_layout_; }
133         /// Enumerate the paragraph styles.
134         size_t layoutCount() const { return layoutlist_.size(); }
135         ///
136         bool hasLayout(docstring const & name) const;
137         ///
138         bool hasInsetLayout(docstring const & name) const;
139         ///
140         Layout const & operator[](docstring const & vname) const;
141         /// Inset layouts of this doc class
142         InsetLayouts const & insetLayouts() const { return insetlayoutlist_; };
143
144         ///////////////////////////////////////////////////////////////////
145         // reading routines
146         ///////////////////////////////////////////////////////////////////
147         /// Enum used with TextClass::read
148         enum ReadType { 
149                 BASECLASS, //>This is a base class, i.e., top-level layout file
150                 MERGE, //>This is a file included in a layout file
151                 MODULE, //>This is a layout module
152                 VALIDATION //>We're just validating
153         };
154         /// return values for read()
155         enum ReturnValues {
156                 OK,
157                 ERROR,
158                 FORMAT_MISMATCH
159         };
160
161         /// Performs the read of the layout file.
162         /// \return true on success.
163         bool read(support::FileName const & filename, ReadType rt = BASECLASS);
164         ///
165         bool read(std::string const & str, ReadType rt = BASECLASS);
166         ///
167         ReturnValues read(Lexer & lex, ReadType rt = BASECLASS);
168         /// validates the layout information passed in str
169         static bool validate(std::string const & str);
170
171         ///////////////////////////////////////////////////////////////////
172         // loading
173         ///////////////////////////////////////////////////////////////////
174         /// Sees to it the textclass structure has been loaded
175         /// This function will search for $classname.layout in default directories
176         /// and an optional path, but if path points to a file, it will be loaded
177         /// directly.
178         bool load(std::string const & path = std::string()) const;
179         /// Has this layout file been loaded yet?
180         /// Overridden by DocumentClass
181         virtual bool loaded() const { return loaded_; }
182
183         ///////////////////////////////////////////////////////////////////
184         // accessors
185         ///////////////////////////////////////////////////////////////////
186         ///
187         std::string const & name() const { return name_; }
188         ///
189         std::string const & description() const { return description_; }
190         ///
191         std::string const & latexname() const { return latexname_; }
192         /// Can be LaTeX, DocBook, etc.
193         OutputType outputType() const { return outputType_; }
194 protected:
195         /// Protect construction
196         TextClass();
197         ///
198         Layout & operator[](docstring const & name);
199         /** Create an new, very basic layout for this textclass. This is used for
200             the Plain Layout common to all TextClass objects and also, in 
201             DocumentClass, for the creation of new layouts `on the fly' when
202             previously unknown layouts are encountered.
203             \param unknown Set to true if this layout is used to represent an 
204             unknown layout
205          */
206         Layout createBasicLayout(docstring const & name, bool unknown = false) const;
207         
208         ///////////////////////////////////////////////////////////////////
209         // non-const iterators
210         ///////////////////////////////////////////////////////////////////
211         ///
212         typedef LayoutList::iterator iterator;
213         ///
214         iterator begin() { return layoutlist_.begin(); }
215         ///
216         iterator end() { return layoutlist_.end(); }
217
218         ///////////////////////////////////////////////////////////////////
219         // members
220         ///////////////////////////////////////////////////////////////////
221         /// Paragraph styles used in this layout
222         /// This variable is mutable because unknown layouts can be added
223         /// to const textclass.
224         mutable LayoutList layoutlist_;
225         /// Layout file name
226         std::string name_;
227         /// document class name
228         std::string latexname_;
229         /// document class description
230         std::string description_;
231         /// available types of float, eg. figure, algorithm.
232         mutable FloatList floatlist_;
233         /// Types of counters, eg. sections, eqns, figures, avail. in document class.
234         mutable Counters counters_;
235         /// Has this layout file been loaded yet?
236         mutable bool loaded_;
237         /// Is the TeX class available?
238         bool texClassAvail_;
239         ///
240         std::string opt_fontsize_;
241         ///
242         std::string opt_pagestyle_;
243         /// Specific class options
244         std::string options_;
245         ///
246         std::string pagestyle_;
247         ///
248         std::string class_header_;
249         ///
250         docstring defaultlayout_;
251         /// name of plain layout
252         static const docstring plain_layout_;
253         /// preamble text to support layout styles
254         docstring preamble_;
255         /// latex packages loaded by document class.
256         std::set<std::string> provides_;
257         /// latex packages requested by document class.
258         std::set<std::string> requires_;
259         /// default modules wanted by document class
260         LayoutModuleList default_modules_;
261         /// modules provided by document class
262         LayoutModuleList provided_modules_;
263         /// modules excluded by document class
264         LayoutModuleList excluded_modules_;
265         ///
266         unsigned int columns_;
267         ///
268         PageSides sides_;
269         /// header depth to have numbering
270         int secnumdepth_;
271         /// header depth to appear in table of contents
272         int tocdepth_;
273         /// Can be LaTeX, DocBook, etc.
274         OutputType outputType_;
275         /** Base font. The paragraph and layout fonts are resolved against
276             this font. This has to be fully instantiated. Attributes
277             FONT_INHERIT, FONT_IGNORE, and FONT_TOGGLE are
278             extremely illegal.
279         */
280         FontInfo defaultfont_;
281         /// Text that dictates how wide the left margin is on the screen
282         docstring leftmargin_;
283         /// Text that dictates how wide the right margin is on the screen
284         docstring rightmargin_;
285         /// The type of command used to produce a title
286         TitleLatexType titletype_;
287         /// The name of the title command
288         std::string titlename_;
289         /// Input layouts available to this layout
290         InsetLayouts insetlayoutlist_;
291         /// The minimal TocLevel of sectioning layouts
292         int min_toclevel_;
293         /// The maximal TocLevel of sectioning layouts
294         int max_toclevel_;
295 private:
296         ///////////////////////////////////////////////////////////////////
297         // helper routines for reading layout files
298         ///////////////////////////////////////////////////////////////////
299         ///
300         bool deleteLayout(docstring const &);
301         ///
302         bool convertLayoutFormat(support::FileName const &, ReadType);
303         /// Reads the layout file without running layout2layout.
304         ReturnValues readWithoutConv(support::FileName const & filename, ReadType rt);
305         /// \return true for success.
306         bool readStyle(Lexer &, Layout &) const;
307         ///
308         void readOutputType(Lexer &);
309         ///
310         void readTitleType(Lexer &);
311         ///
312         void readMaxCounter(Lexer &);
313         ///
314         void readClassOptions(Lexer &);
315         ///
316         void readCharStyle(Lexer &, std::string const &);
317         ///
318         void readFloat(Lexer &);
319 };
320
321
322 /// A DocumentClass represents the layout information associated with a
323 /// Buffer. It is based upon a LayoutFile, but may be modified by loading
324 /// various Modules. 
325 /// 
326 /// In that regard, DocumentClass objects are "dynamic". But this is really
327 /// an illusion, since DocumentClass objects are not (currently) changed
328 /// when, say, a new Module is loaded. Rather, the old DocumentClass is
329 /// discarded---actually, it's kept around in case something on the cut
330 /// stack needs it---and a new one is created from scratch. 
331 /// 
332 /// In the main LyX code, DocumentClass objects are created only by
333 /// DocumentClassBundle, for which see below.
334 /// 
335 class DocumentClass : public TextClass, boost::noncopyable {
336 public:
337         ///
338         virtual ~DocumentClass() {}
339
340         ///////////////////////////////////////////////////////////////////
341         // Layout Info
342         ///////////////////////////////////////////////////////////////////
343         /// \return true if there is a Layout with latexname lay
344         bool hasLaTeXLayout(std::string const & lay) const;
345         /// A DocumentClass nevers count as loaded, since it is dynamic
346         virtual bool loaded() { return false; }
347         /// \return the layout object of an inset given by name. If the name
348         /// is not found as such, the part after the ':' is stripped off, and
349         /// searched again. In this way, an error fallback can be provided:
350         /// An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
351         /// will invoke the layout object defined by name = 'CharStyle'.
352         /// If that doesn't work either, an empty object returns (shouldn't
353         /// happen).  -- Idea JMarc, comment MV
354         InsetLayout const & insetLayout(docstring const & name) const;
355         /// a plain inset layout for use as a default
356         static InsetLayout const & plainInsetLayout() { return plain_insetlayout_; }
357         /// add a new layout \c name if it does not exist in layoutlist_
358         void addLayoutIfNeeded(docstring const & name) const;
359
360         ///////////////////////////////////////////////////////////////////
361         // accessors
362         ///////////////////////////////////////////////////////////////////
363         /// the list of floats defined in the document class
364         FloatList const & floats() const { return floatlist_; }
365         ///
366         Counters & counters() const { return counters_; }
367         ///
368         std::string const & opt_fontsize() const { return opt_fontsize_; }
369         ///
370         std::string const & opt_pagestyle() const { return opt_pagestyle_; }
371         ///
372         std::string const & options() const { return options_; }
373         ///
374         std::string const & class_header() const { return class_header_; }
375         ///
376         std::string const & pagestyle() const { return pagestyle_; }
377         ///
378         docstring const & preamble() const { return preamble_; }
379         /// is this feature already provided by the class?
380         bool provides(std::string const & p) const;
381         /// features required by the class?
382         std::set<std::string> const & requires() const { return requires_; }
383         ///
384         unsigned int columns() const { return columns_; }
385         ///
386         PageSides sides() const { return sides_; }
387         ///
388         int secnumdepth() const { return secnumdepth_; }
389         ///
390         int tocdepth() const { return tocdepth_; }
391         ///
392         FontInfo const & defaultfont() const { return defaultfont_; }
393         /// Text that dictates how wide the left margin is on the screen
394         docstring const & leftmargin() const { return leftmargin_; }
395         /// Text that dictates how wide the right margin is on the screen
396         docstring const & rightmargin() const { return rightmargin_; }
397         /// The type of command used to produce a title
398         TitleLatexType titletype() const { return titletype_; };
399         /// The name of the title command
400         std::string const & titlename() const { return titlename_; };
401         ///
402         int size() const { return layoutlist_.size(); }
403         /// The minimal TocLevel of sectioning layouts
404         int min_toclevel() const { return min_toclevel_; }
405         /// The maximal TocLevel of sectioning layouts
406         int max_toclevel() const { return max_toclevel_; }
407         /// returns true if the class has a ToC structure
408         bool hasTocLevels() const;
409         /// Can be LaTeX, DocBook, etc.
410         OutputType outputType() const { return outputType_; }
411 protected:
412         /// Constructs a DocumentClass based upon a LayoutFile.
413         DocumentClass(LayoutFile const & tc);
414         /// Needed in tex2lyx
415         DocumentClass() {}
416 private:
417         /// The only class that can create a DocumentClass is
418         /// DocumentClassBundle, which calls the protected constructor.
419         friend class DocumentClassBundle;
420         ///
421         static InsetLayout plain_insetlayout_;
422 };
423
424
425 /// DocumentClassBundle is a container for DocumentClass objects, so that 
426 /// they stay in memory for use by Insets, CutAndPaste, and the like, even
427 /// when their associated Buffers are destroyed.
428 /// FIXME Some sort of garbage collection or reference counting wouldn't
429 /// be a bad idea here. It might be enough to check when a Buffer is closed
430 /// (or makeDocumentClass is called) whether the old DocumentClass is in use 
431 /// anywhere.
432 ///
433 /// This is a singleton class. Its sole instance is accessed via 
434 /// DocumentClassBundle::get().
435 class DocumentClassBundle : boost::noncopyable {
436 public:
437         /// \return The sole instance of this class.
438         static DocumentClassBundle & get();
439         /// \return A new DocumentClass based on baseClass, with info added
440         /// from the modules in modlist.
441         DocumentClass & makeDocumentClass(LayoutFile const & baseClass, 
442                         LayoutModuleList const & modlist);
443 private:
444         /// control instantiation
445         DocumentClassBundle() {}
446         /// clean up
447         ~DocumentClassBundle();
448         /// \return Reference to a new DocumentClass equal to baseClass
449         DocumentClass & newClass(LayoutFile const & baseClass);
450         ///
451         std::vector<DocumentClass *> documentClasses_;
452 };
453
454
455 /// convert page sides option to text 1 or 2
456 std::ostream & operator<<(std::ostream & os, PageSides p);
457
458
459 } // namespace lyx
460
461 #endif