]> git.lyx.org Git - lyx.git/blob - src/Layout.h
Introduce NeedCProtect -1 layout option
[lyx.git] / src / Layout.h
1 // -*- C++ -*-
2 /**
3  * \file Layout.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author André Pönitz
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef LAYOUT_H
15 #define LAYOUT_H
16
17 #include "FontInfo.h"
18 #include "LayoutEnums.h"
19 #include "Spacing.h"
20 #include "support/debug.h"
21 #include "support/docstring.h"
22
23 #include <map>
24 #include <set>
25 #include <string>
26
27 namespace lyx {
28
29 namespace support { class Lexer; }
30
31 class TextClass;
32
33 /* Fixed labels are printed flushright, manual labels flushleft.
34  * MARGIN_MANUAL and MARGIN_FIRST_DYNAMIC are *only* for LABEL_MANUAL,
35  * MARGIN_DYNAMIC and MARGIN_STATIC are *not* for LABEL_MANUAL.
36  * This seems a funny restriction, but I think other combinations are
37  * not needed, so I will not change it yet.
38  * Correction: MARGIN_FIRST_DYNAMIC also usable with LABEL_STATIC.
39  */
40
41
42 /* There is a parindent and a parskip. Which one is used depends on the
43  * paragraph_separation-flag of the text-object.
44  * BUT: parindent is only thrown away, if a parskip is defined! So if you
45  * want a space between the paragraphs and a parindent at the same time,
46  * you should set parskip to zero and use topsep, parsep and bottomsep.
47  *
48  * The standard layout is an exception: its parindent is only set, if the
49  * previous paragraph is standard too. Well, this is LateX and it is good!
50  */
51
52 ///
53 class Layout {
54 public:
55         ///
56         Layout();
57         /// is this layout a default layout created for an unknown layout
58         bool isUnknown() const { return unknown_; }
59         void setUnknown(bool unknown) { unknown_ = unknown; }
60         /// Reads a layout definition from file
61         /// \return true on success.
62         bool read(support::Lexer &, TextClass const &, bool validating = false);
63         ///
64         void readAlign(support::Lexer &);
65         ///
66         void readAlignPossible(support::Lexer &);
67         ///
68         void readLabelType(support::Lexer &);
69         ///
70         void readEndLabelType(support::Lexer &);
71         ///
72         void readMargin(support::Lexer &);
73         ///
74         void readLatexType(support::Lexer &);
75         ///
76         void readSpacing(support::Lexer &);
77         ///
78         void readArgument(support::Lexer &, bool);
79         /// Write a layout definition in utf8 encoding
80         void write(std::ostream &) const;
81         ///
82         docstring const & name() const { return name_; }
83         ///
84         void setName(docstring const & n) { name_ = n; }
85         ///
86         docstring const & obsoleted_by() const { return obsoleted_by_; }
87         ///
88         docstring const & depends_on() const { return depends_on_; }
89         ///
90         std::string const & latexname() const { return latexname_; }
91         ///
92         std::string const & itemcommand() const { return itemcommand_; }
93         /// One argument of this layout
94         struct latexarg {
95                 docstring labelstring;
96                 docstring menustring;
97                 bool mandatory = false;
98                 bool nodelims = false;
99                 docstring ldelim;
100                 docstring rdelim;
101                 docstring defaultarg;
102                 docstring presetarg;
103                 docstring tooltip;
104                 std::string required;
105                 std::string decoration;
106                 FontInfo font = inherit_font;
107                 FontInfo labelfont = inherit_font;
108                 bool autoinsert = false;
109                 bool insertcotext = false;
110                 bool insertonnewline = false;
111                 ArgPassThru passthru = PT_INHERITED;
112                 docstring pass_thru_chars;
113                 bool is_toc_caption = false;
114                 bool free_spacing = false;
115                 std::string newlinecmd;
116                 /// The DocBook tag corresponding to this argument.
117                 docstring docbooktag;
118                 docstring docbooktagtype;
119                 docstring docbookattr;
120                 /// Whether this argument should be output after the main tag (default: inside). The result if the argument
121                 /// should be output both before and after the main tag is undefined.
122                 bool docbookargumentbeforemaintag = false;
123                 /// Whether this argument should be output before the main tag (default: inside). The result if the argument
124                 /// should be output both before and after the main tag is undefined.
125                 bool docbookargumentaftermaintag = false;
126         };
127         ///
128         typedef std::map<std::string, latexarg> LaTeXArgMap;
129         ///
130         LaTeXArgMap const & latexargs() const { return latexargs_; }
131         ///
132         LaTeXArgMap const & postcommandargs() const { return postcommandargs_; }
133         ///
134         LaTeXArgMap const & listpreamble() const { return listpreamble_; }
135         ///
136         LaTeXArgMap const & itemargs() const { return itemargs_; }
137         /// Returns true is the layout has arguments. If false, then an
138         /// InsetArgument in this layout stands for the parent InsetText.
139         bool hasArgs() const;
140         /// Returns latexargs() + postcommandargs() + itemargs().
141         /// But note that it returns a *copy*, not a reference, so do not do
142         /// anything like:
143         ///   Layout::LaTeXArgMap::iterator it = args().begin();
144         ///   Layout::LaTeXArgMap::iterator en = args().end();
145         /// Those are iterators for different containers.
146         LaTeXArgMap args() const;
147         ///
148         int optArgs() const;
149         ///
150         int requiredArgs() const;
151         ///
152         docstring const & labelstring(bool in_appendix) const
153         { return in_appendix ? labelstring_appendix_ : labelstring_; }
154         ///
155         docstring const & endlabelstring() const { return endlabelstring_; }
156         ///
157         docstring const & category() const { return category_; }
158         ///
159         docstring const & preamble() const { return preamble_; }
160         /// Get language dependent macro definitions needed for this layout
161         /// for language \p lang
162         docstring const langpreamble() const { return langpreamble_; }
163         /// Get language and babel dependent macro definitions needed for
164         /// this layout for language \p lang
165         docstring const babelpreamble() const { return babelpreamble_; }
166         ///
167         std::set<std::string> const & required() const { return required_; }
168         ///
169         std::set<docstring> const & autonests() const { return autonests_; }
170         ///
171         std::set<docstring> const & isAutonestedBy() const { return autonested_by_; }
172         ///
173         std::string const & latexparam() const { return latexparam_; }
174         ///
175         docstring leftdelim() const { return leftdelim_; }
176         ///
177         docstring rightdelim() const { return rightdelim_; }
178         ///
179         std::string const & innertag() const { return innertag_; }
180         ///
181         std::string const & labeltag() const { return labeltag_; }
182         ///
183         std::string const & itemtag() const { return itemtag_; }
184         ///
185         std::string const & htmltag() const;
186         ///
187         std::string const & htmlattr() const { return htmlattr_; }
188         ///
189         std::string const & htmlclass() const;
190         /// Returns a complete attribute string, including class, etc.
191         std::string const & htmlGetAttrString() const;
192         ///
193         std::string const & htmlitemtag() const;
194         ///
195         std::string const & htmlitemattr() const;
196         ///
197         std::string const & htmllabeltag() const;
198         ///
199         std::string const & htmllabelattr() const;
200         ///
201         bool htmlintoc() const { return htmlintoc_; }
202         ///
203         std::string defaultCSSClass() const;
204         ///
205         bool htmllabelfirst() const { return htmllabelfirst_; }
206         ///
207         docstring htmlstyle() const;
208         ///
209         docstring const & htmlpreamble() const { return htmlpreamble_; }
210         ///
211         bool htmltitle() const { return htmltitle_; }
212         ///
213         std::string const & docbooktag() const;
214         ///
215         std::string const & docbookattr() const;
216         ///
217         std::string const & docbooktagtype() const;
218         ///
219         std::string const & docbookinnertag() const;
220         ///
221         std::string const & docbookinnerattr() const;
222         ///
223         std::string const & docbookinnertagtype() const;
224         ///
225         std::string const & docbookininfo() const;
226         ///
227         bool docbookabstract() const { return docbookabstract_; }
228         ///
229         std::string const & docbookwrappertag() const;
230         ///
231         std::string const & docbookwrapperattr() const;
232         ///
233         std::string const & docbookwrappertagtype() const;
234         ///
235         bool docbookwrappermergewithprevious() const { return docbookwrappermergewithprevious_; }
236         ///
237         std::string const & docbooksectiontag() const;
238         ///
239         bool docbooksection() const { return docbooksection_; }
240         ///
241         std::string const & docbookitemwrappertag() const;
242         ///
243         std::string const & docbookitemwrapperattr() const;
244         ///
245         std::string const & docbookitemwrappertagtype() const;
246         ///
247         std::string const & docbookitemlabeltag() const;
248         ///
249         std::string const & docbookitemlabelattr() const;
250         ///
251         std::string const & docbookitemlabeltagtype() const;
252         ///
253         std::string const & docbookiteminnertag() const;
254         ///
255         std::string const & docbookiteminnerattr() const;
256         ///
257         std::string const & docbookiteminnertagtype() const;
258         ///
259         std::string const & docbookitemtag() const;
260         ///
261         std::string const & docbookitemattr() const;
262         ///
263         std::string const & docbookitemtagtype() const;
264         ///
265         std::string const & docbookforceabstracttag() const;
266         ///
267         bool docbooknofontinside() const { return docbooknofontinside_; }
268         ///
269         bool docbookgeneratetitle() const { return docbookgeneratetitle_; }
270         ///
271         bool isParagraph() const { return latextype == LATEX_PARAGRAPH; }
272         ///
273         bool isCommand() const { return latextype == LATEX_COMMAND; }
274         ///
275         bool isEnvironment() const {
276                 return latextype == LATEX_ENVIRONMENT
277                         || latextype == LATEX_BIB_ENVIRONMENT
278                         || latextype == LATEX_ITEM_ENVIRONMENT
279                         || latextype == LATEX_LIST_ENVIRONMENT;
280         }
281         /// Is this the kind of layout in which adjacent paragraphs
282         /// are handled as one group?
283         bool isParagraphGroup() const { return par_group_; }
284         ///
285         bool labelIsInline() const {
286                 return labeltype == LABEL_STATIC
287                         || labeltype == LABEL_SENSITIVE
288                         || labeltype == LABEL_ENUMERATE
289                         || labeltype == LABEL_ITEMIZE;
290         }
291         bool labelIsAbove() const {
292                 return labeltype == LABEL_ABOVE
293                         || labeltype == LABEL_CENTERED
294                         || labeltype == LABEL_BIBLIO;
295         }
296         bool isNumHeadingLabelType() const {
297                 return labeltype == LABEL_ABOVE
298                         || labeltype == LABEL_CENTERED
299                         || labeltype == LABEL_STATIC;
300         }
301         ///
302         bool addToToc() const { return add_to_toc_; }
303         ///
304         std::string tocType() const { return toc_type_; }
305         ///
306         bool isTocCaption() const { return is_toc_caption_; }
307
308         ///
309         bool operator==(Layout const &) const;
310         ///
311         bool operator!=(Layout const & rhs) const
312                 { return !(*this == rhs); }
313
314         ////////////////////////////////////////////////////////////////
315         // members
316         ////////////////////////////////////////////////////////////////
317         /** Default font for this layout/environment.
318             The main font for this kind of environment. If an attribute has
319             INHERITED_*, it means that the value is specified by
320             the defaultfont for the entire layout. If we are nested, the
321             font is inherited from the font in the environment one level
322             up until the font is resolved. The values :IGNORE_*
323             and FONT_TOGGLE are illegal here.
324         */
325         FontInfo font;
326
327         /** Default font for labels.
328             Interpretation the same as for font above
329         */
330         FontInfo labelfont;
331
332         /** Resolved version of the font for this layout/environment.
333             This is a resolved version the default font. The font is resolved
334             against the defaultfont of the entire layout.
335         */
336         FontInfo resfont;
337
338         /** Resolved version of the font used for labels.
339             This is a resolved version the label font. The font is resolved
340             against the defaultfont of the entire layout.
341         */
342         FontInfo reslabelfont;
343
344         /// Text that dictates how wide the left margin is on the screen
345         docstring leftmargin;
346         /// Text that dictates how wide the right margin is on the screen
347         docstring rightmargin;
348         /// Text that dictates how much space to leave after a potential label
349         docstring labelsep;
350         /// Text that dictates how much space to leave before a potential label
351         docstring labelindent;
352         /// Text that dictates the width of the indentation of indented pars
353         docstring parindent;
354         ///
355         double parskip;
356         ///
357         double itemsep;
358         ///
359         double topsep;
360         ///
361         double bottomsep;
362         ///
363         double labelbottomsep;
364         ///
365         double parsep;
366         ///
367         Spacing spacing;
368         ///
369         LyXAlignment align;
370         ///
371         LyXAlignment alignpossible;
372         ///
373         LabelType labeltype;
374         ///
375         EndLabelType endlabeltype;
376         ///
377         MarginType margintype;
378         ///
379         bool newline_allowed;
380         ///
381         bool nextnoindent;
382         ///
383         ToggleIndentation toggle_indent;
384         ///
385         bool free_spacing;
386         ///
387         bool pass_thru;
388         /// Individual chars to be passed verbatim
389         docstring pass_thru_chars;
390         ///
391         bool parbreak_is_newline;
392         /// show this in toc
393         int toclevel;
394         /// special value of toclevel for non-section layouts
395         static const int NOT_IN_TOC;
396
397         /** true when the fragile commands in the paragraph need to be
398             \protect'ed. */
399         bool needprotect;
400         /** true when the verbatim stuff of this layout needs to be
401             \cprotect'ed. */
402         bool needcprotect;
403         /** true when the verbatim stuff of this layout never should be
404             \cprotect'ed. */
405         bool nocprotect;
406         /** true when specific commands in this paragraph need to be
407             protected in an \mbox. */
408         bool needmboxprotect;
409         /// true when empty paragraphs should be kept.
410         bool keepempty;
411         /// Type of LaTeX object
412         LatexType latextype;
413         /// Does this object belong in the title part of the document?
414         bool intitle;
415         /// Is the content to go in the preamble rather than the body?
416         bool inpreamble;
417         /// Which counter to step
418         docstring counter;
419         /// Resume counter?
420         bool resumecounter;
421         /// Step parent counter?
422         bool stepparentcounter;
423         /// Prefix to use when creating labels
424         docstring refprefix;
425         /// Depth of XML command
426         int commanddepth;
427
428         /// Is this spellchecked?
429         bool spellcheck;
430         /**
431          * Should this layout definition always be written to the document preamble?
432          * Possible values are:
433          *   0: Do not enforce local layout
434          * >=1: Enforce local layout with version forcelocal
435          *  -1: Enforce local layout with infinite version
436          * On reading, the forced local layout is only used if its version
437          * number is greater than the version number of the same layout in the
438          * document class. Otherwise, it is ignored.
439          */
440         int forcelocal;
441
442
443 private:
444         /// Reads a layout definition from file
445         /// \return true on success.
446         bool readIgnoreForcelocal(support::Lexer &, TextClass const &, bool validating);
447         /// generates the default CSS for this layout
448         void makeDefaultCSS() const;
449         ///
450         std::string defaultCSSItemClass() const { return defaultCSSClass() + "_item"; }
451         ///
452         std::string defaultCSSLabelClass() const { return defaultCSSClass() + "_label"; }
453
454         /// Name of the layout/paragraph environment
455         docstring name_;
456
457         /// LaTeX name for environment
458         std::string latexname_;
459
460         /** Is this layout the default layout for an unknown layout? If
461          * so, its name will be displayed as xxx (unknown).
462          */
463         bool unknown_;
464
465         /** Name of an layout that has replaced this layout.
466             This is used to rename a layout, while keeping backward
467             compatibility
468         */
469         docstring obsoleted_by_;
470
471         /** Name of an layout which preamble must come before this one
472             This is used when the preamble snippet uses macros defined in
473             another preamble
474          */
475         docstring depends_on_;
476
477         /// Label string. "Abstract", "Reference", "Caption"...
478         docstring labelstring_;
479         ///
480         docstring endlabelstring_;
481         /// Label string inside appendix. "Appendix", ...
482         docstring labelstring_appendix_;
483         /// LaTeX parameter for environment
484         std::string latexparam_;
485         /// Item command in lists
486         std::string itemcommand_;
487         /// Left delimiter of the content
488         docstring leftdelim_;
489         /// Right delimiter of the content
490         docstring rightdelim_;
491         /// Internal tag to use (e.g., <title></title> for sect header)
492         std::string innertag_;
493         /// Internal tag to use (e.g. to surround varentrylist label)
494         std::string labeltag_;
495         /// Internal tag to surround the item text in a list.
496         std::string itemtag_;
497         /// The interpretation of this tag varies depending upon the latextype.
498         /// In an environment, it is the tag enclosing all content for this set of
499         /// paragraphs. So for quote, e.g,. it would be: blockquote. For itemize,
500         /// it would be: ul. (You get the idea.)
501         ///
502         /// For a command, it is the tag enclosing the content of the command.
503         /// So, for section, it might be: h2.
504         ///
505         /// For the paragraph type, it is the tag that will enclose each paragraph.
506         ///
507         /// Defaults to "div".
508         mutable std::string htmltag_;
509         /// Additional attributes for inclusion with the start tag.
510         /// Note that the CSS class is handled separately.
511         std::string htmlattr_;
512         /// The CSS class to use. Calculated from the layout name if not given.
513         mutable std::string htmlclass_;
514         /// cached
515         mutable std::string htmlfullattrs_;
516         /// Tag for individual paragraphs in an environment. In lists, this
517         /// would be something like "li". But it also needs to be set for
518         /// quotation, e.g., since the paragraphs in a quote need to be
519         /// in "p" tags. Default is "div".
520         /// Note that when I said "environment", I meant it: This has no
521         /// effect for LATEX_PARAGRAPH type layouts.
522         mutable std::string htmlitemtag_;
523         /// Attributes for htmlitemtag_. Default is: class="layoutname_item".
524         mutable std::string htmlitemattr_;
525         /// Tag for labels, of whatever sort. One use for this is in setting
526         /// descriptions, in which case it would be: dt. Another use is to
527         /// customize the display of, say, the auto-generated label for
528         /// sections. Defaults to "span".
529         /// If set to "NONE", this suppresses the printing of the label.
530         mutable std::string htmllabeltag_;
531         /// Attributes for the label. Defaults to: class="layoutname_label".
532         mutable std::string htmllabelattr_;
533         /// Whether to put the label before the item, or within the item.
534         /// I.e., do we have (true):
535         ///    <label>...</label><item>...</item>
536         /// or instead (false):
537         ///    <item><label>...</label>...</item>
538         /// The latter is the default.
539         bool htmllabelfirst_;
540         /// Is this to be output with the toc?
541         bool htmlintoc_;
542         /// CSS information needed by this layout.
543         docstring htmlstyle_;
544         /// DocBook tag corresponding to this layout.
545         mutable std::string docbooktag_;
546         /// Roles to add to docbooktag_, if any (default: none).
547         mutable std::string docbookattr_;
548         /// DocBook tag type corresponding to this layout (block, paragraph, or inline; default: block).
549         mutable std::string docbooktagtype_;
550         /// DocBook inner tag corresponding to this layout.
551         mutable std::string docbookinnertag_;
552         /// Roles to add to docbookinnertag_, if any (default: none).
553         mutable std::string docbookinnerattr_;
554         /// DocBook inner-tag type corresponding to this layout (block, paragraph, or inline; default: block).
555         mutable std::string docbookinnertagtype_;
556         /// DocBook tag corresponding to this item (mainly for lists).
557         mutable std::string docbookitemtag_;
558         /// Roles to add to docbookitemtag_, if any (default: none).
559         mutable std::string docbookitemattr_;
560         /// DocBook tag type corresponding to this item (block, paragraph, or inline; default: block).
561         mutable std::string docbookitemtagtype_;
562         /// DocBook tag corresponding to the wrapper around an item (mainly for lists).
563         mutable std::string docbookitemwrappertag_;
564         /// Roles to add to docbookitemwrappertag_, if any (default: none).
565         mutable std::string docbookitemwrapperattr_;
566         /// DocBook tag type corresponding to the wrapper around an item (block, paragraph, or inline; default: block).
567         mutable std::string docbookitemwrappertagtype_;
568         /// DocBook tag corresponding to this label (mostly for description lists;
569         /// labels in the common sense do not exist with DocBook).
570         mutable std::string docbookitemlabeltag_;
571         /// Roles to add to docbooklabeltag_, if any (default: none).
572         mutable std::string docbookitemlabelattr_;
573         /// DocBook tag corresponding to this label (block, paragraph, or inline; default: block).
574         mutable std::string docbookitemlabeltagtype_;
575         /// DocBook tag to add within the item, around its direct content (mainly for lists).
576         mutable std::string docbookiteminnertag_;
577         /// Roles to add to docbookiteminnertag_, if any (default: none).
578         mutable std::string docbookiteminnerattr_;
579         /// DocBook tag to add within the item, around its direct content (block, paragraph, or inline; default: block).
580         mutable std::string docbookiteminnertagtype_;
581         /// DocBook tag corresponding to this wrapper around the main tag.
582         mutable std::string docbookwrappertag_;
583         /// Roles to add to docbookwrappertag_, if any (default: none).
584         mutable std::string docbookwrapperattr_;
585         /// DocBook tag corresponding to this wrapper around the main tag (block, paragraph, or inline; default: block).
586         mutable std::string docbookwrappertagtype_;
587         /// Whether this wrapper tag may be merged with the previously opened wrapper tag.
588         bool docbookwrappermergewithprevious_;
589         /// Outer tag for this section, only if this layout represent a sectionning item, including chapters
590         /// (default: section).
591         mutable std::string docbooksectiontag_;
592         /// Whether this element should be considered as a section-level element in DocBook.
593         bool docbooksection_;
594         /// Whether this tag must/can/can't go into an <info> tag (default: never, as it only makes sense for metadata).
595         mutable std::string docbookininfo_;
596         /// Whether this paragraph should be considered as abstract. Such paragraphs are output as a part of the document
597         /// abstract element (corresponding to the root tag).
598         bool docbookabstract_;
599         /// Whether this element (root or not) does not accept text without a section (i.e. the first text that is met
600         /// in LyX must be considered as the abstract if this is true); this text must be output with the specific tag
601         /// held by this attribute.
602         mutable std::string docbookforceabstracttag_;
603         /// Whether font tags are allowed inside this tag.
604         bool docbooknofontinside_ = false;
605         /// Whether LyX should create a title on its own, just after the wrapper tag. Typically, this parameter is required
606         /// because the wrapper tag requires a title (like a figure). The generated title will be similar to a LyXHTML label
607         /// (environment type and a number).
608         bool docbookgeneratetitle_ = false;
609         /// Should we generate the default CSS for this layout, even if HTMLStyle
610         /// has been given? Default is false.
611         /// Note that the default CSS is output first, then the user CSS, so it is
612         /// possible to override what one does not want.
613         bool htmlforcecss_;
614         /// A cache for the default style info so generated.
615         mutable docstring htmldefaultstyle_;
616         /// Any other info for the HTML header.
617         docstring htmlpreamble_;
618         /// Whether this is the <title> paragraph.
619         bool htmltitle_;
620         /// calculating this is expensive, so we cache it.
621         mutable std::string defaultcssclass_;
622         /// This is the `category' for this layout. The following are
623         /// recommended basic categories: FrontMatter, BackMatter, MainText,
624         /// Sectioning, Starred, List, Reasoning.
625         docstring category_;
626         /// Macro definitions needed for this layout
627         docstring preamble_;
628         /// Language dependent macro definitions needed for this layout
629         docstring langpreamble_;
630         /// Language and babel dependent macro definitions needed for this layout
631         docstring babelpreamble_;
632         /// Are adjacent paragraphs handled as one group?
633         bool par_group_;
634         /// Packages needed for this layout
635         std::set<std::string> required_;
636         /// Layouts that are by default nested after this one
637         std::set<docstring> autonests_;
638         /// Layouts that by auto-nest this one
639         std::set<docstring> autonested_by_;
640         ///
641         LaTeXArgMap latexargs_;
642         ///
643         LaTeXArgMap postcommandargs_;
644         ///
645         LaTeXArgMap listpreamble_;
646         ///
647         LaTeXArgMap itemargs_;
648         ///
649         bool add_to_toc_;
650         ///
651         std::string toc_type_;
652         ///
653         bool is_toc_caption_;
654 };
655
656
657 } // namespace lyx
658
659 #endif