]> git.lyx.org Git - lyx.git/blob - src/layout.h
Angus insetindex patch + protect patch from Dekel
[lyx.git] / src / layout.h
1 // -*- C++ -*-
2 /* This file is part of
3  * ======================================================
4  * 
5  *           LyX, The Document Processor
6  *       
7  *           Copyright 1995 Matthias Ettrich
8  *           Copyright 1995-2000 The LyX Team.
9  *
10  * ====================================================== */
11
12 #ifndef LAYOUT_H
13 #define LAYOUT_H
14
15 #include <vector>
16
17 #ifdef __GNUG__
18 #pragma interface
19 #endif
20
21 #include "lyxlex.h"
22 #include "lyxfont.h"
23 #include "Spacing.h"
24
25 /// Reads the style files
26 extern void LyXSetStyle();
27
28 ///
29 enum { // no good name for this
30         ///
31         LYX_ENVIRONMENT_DEFAULT = 97,
32         ///
33         LYX_LAYOUT_DEFAULT = 99
34 };
35
36
37 // Could this cause confusion that both DUMMY_LAYOUT and  LAYOUT_DEFAULT has
38 // the same value? (Lgb)
39 ///
40 #define LYX_DUMMY_LAYOUT 99
41
42
43 /// The different output types
44 enum OutputType {
45         ///
46         LATEX = 1,
47         ///
48         LINUXDOC,
49         ///
50         DOCBOOK,
51         ///
52         LITERATE
53 };
54
55 /// The different margin types
56 enum LYX_MARGIN_TYPE {
57         ///
58         MARGIN_MANUAL = 1,
59         ///
60         MARGIN_FIRST_DYNAMIC,
61         ///
62         MARGIN_DYNAMIC,
63         ///
64         MARGIN_STATIC,
65         ///
66         MARGIN_RIGHT_ADDRESS_BOX
67 };
68
69
70 ///
71 enum LyXAlignment {
72         ///
73         LYX_ALIGN_NONE = 0,
74         ///
75         LYX_ALIGN_BLOCK = 1,
76         ///
77         LYX_ALIGN_LEFT = 2,
78         ///
79         LYX_ALIGN_RIGHT = 4,
80         ///
81         LYX_ALIGN_CENTER = 8,
82         ///
83         LYX_ALIGN_LAYOUT = 16,
84         ///
85         LYX_ALIGN_SPECIAL = 32
86 };
87
88
89 inline
90 void operator|=(LyXAlignment & la1, LyXAlignment la2) {
91         la1 = static_cast<LyXAlignment>(la1 | la2);
92 }
93
94 /// The different LaTeX-Types
95 enum LYX_LATEX_TYPES {
96         ///
97         LATEX_PARAGRAPH = 1,
98         ///
99         LATEX_COMMAND,
100         ///
101         LATEX_ENVIRONMENT,
102         ///
103         LATEX_ITEM_ENVIRONMENT,
104         ///
105         LATEX_LIST_ENVIRONMENT
106 };
107
108 /// The different label types
109 enum LYX_LABEL_TYPES {
110         ///
111         LABEL_NO_LABEL,
112         ///
113         LABEL_MANUAL,
114         ///
115         LABEL_BIBLIO,
116         ///
117         LABEL_TOP_ENVIRONMENT,
118         ///
119         LABEL_CENTERED_TOP_ENVIRONMENT,
120
121         // the flushright labels following now must start with LABEL_STATIC
122         ///
123         LABEL_STATIC,
124         ///
125         LABEL_SENSITIVE,
126         ///
127         LABEL_COUNTER_CHAPTER,
128         ///
129         LABEL_COUNTER_SECTION,
130         ///
131         LABEL_COUNTER_SUBSECTION,
132         ///
133         LABEL_COUNTER_SUBSUBSECTION,
134         ///
135         LABEL_COUNTER_PARAGRAPH,
136         ///
137         LABEL_COUNTER_SUBPARAGRAPH,
138         ///
139         LABEL_COUNTER_ENUMI,
140         ///
141         LABEL_COUNTER_ENUMII,
142         ///
143         LABEL_COUNTER_ENUMIII,
144         ///
145         LABEL_COUNTER_ENUMIV
146 };
147
148 enum LYX_END_LABEL_TYPES {
149         ///
150         END_LABEL_NO_LABEL,
151         ///
152         END_LABEL_BOX,
153         ///
154         END_LABEL_FILLED_BOX,
155         ///
156         END_LABEL_STATIC,
157         ///
158         END_LABEL_ENUM_FIRST = END_LABEL_NO_LABEL,
159         ///
160         END_LABEL_ENUM_LAST = END_LABEL_STATIC
161 };
162                 
163 /* Fix labels are printed flushright, manual labels flushleft. 
164  * MARGIN_MANUAL and MARGIN_FIRST_DYNAMIC are *only* for LABEL_MANUAL,
165  * MARGIN_DYNAMIC and MARGIN_STATIC are *not* for LABEL_MANUAL. 
166  * This seems a funny restriction, but I think other combinations are
167  * not needed, so I will not change it yet. 
168  * Correction: MARGIN_FIRST_DYNAMIC also usable with LABEL_STATIC
169  */
170
171
172 /* There is a parindent and a parskip. Which one is used depends on the 
173  * paragraph_separation-flag of the text-object. 
174  * BUT: parindent is only thrown away, if a parskip is defined! So if you
175  * want a space between the paragraphs and a parindent at the same time, 
176  * you should set parskip to zero and use topsep, parsep and bottomsep.
177  * 
178  * The standard layout is an exception: its parindent is only set, if the 
179  * previous paragraph is standard too. Well, this is LateX and it is good!
180  */ 
181
182
183 /// Attributes of a layout/paragraph environment
184 class LyXTextClass;
185
186 ///
187 class LyXLayout {
188 public:
189         ///
190         LyXLayout ();
191
192         ///
193         bool Read (LyXLex &, LyXTextClass const &);
194         void readAlign(LyXLex &);
195         void readAlignPossible(LyXLex &);
196         void readLabelType(LyXLex &);
197         void readEndLabelType(LyXLex &);
198         void readMargin(LyXLex &);
199         void readLatexType(LyXLex &);
200         void readSpacing(LyXLex &);
201         string const & name() const { return name_; }
202         void name(string const & n) { name_ = n; }
203         string const & obsoleted_by() const { return obsoleted_by_; }
204         string const & latexname() const { return latexname_; }
205         string const & labelstring() const { return labelstring_; }
206         string const & endlabelstring() const { return endlabelstring_; }
207         string const & preamble() const { return preamble_; }
208         string const & latexparam() const { return latexparam_; }
209         string const & labelstring_appendix() const {
210                 return labelstring_appendix_;
211         }
212         /** Default font for this layout/environment.
213             The main font for this kind of environment. If an attribute has
214             LyXFont::INHERITED_*, it means that the value is specified by
215             the defaultfont for the entire layout. If we are nested, the
216             font is inherited from the font in the environment one level
217             up until the font is resolved. The values LyXFont::IGNORE_*
218             and LyXFont::TOGGLE are illegal here.
219         */
220         LyXFont font;
221
222         /** Default font for labels.
223             Interpretation the same as for font above
224         */
225         LyXFont labelfont;
226
227         /** Resolved version of the font for this layout/environment.
228             This is a resolved version the default font. The font is resolved
229             against the defaultfont of the entire layout.
230         */
231         LyXFont resfont;
232
233         /** Resolved version of the font used for labels.
234             This is a resolved version the label font. The font is resolved
235             against the defaultfont of the entire layout.
236         */
237         LyXFont reslabelfont;
238
239         /// Text that dictates how wide the left margin is on the screen
240         string leftmargin;
241
242         /// Text that dictates how wide the right margin is on the screen
243         string rightmargin;
244
245         /// Text that dictates how much space to leave after a potential label
246         string labelsep;
247
248         /// Text that dictates how much space to leave before a potential label
249         string labelindent;
250
251         /** Text that dictates the width of the indentation of
252             indented paragraphs.
253         */
254         string parindent;
255
256         ///
257         float parskip;
258
259         ///
260         float itemsep;
261
262         ///
263         float topsep;
264
265         ///
266         float bottomsep;
267
268         ///
269         float labelbottomsep;
270
271         ///
272         float parsep;
273
274         ///
275         Spacing spacing;
276
277         ///
278         LyXAlignment align;
279
280         ///
281         LyXAlignment alignpossible;
282
283         ///
284         char labeltype; // add approp. type
285
286         ///
287         char endlabeltype;
288
289         ///
290         LYX_MARGIN_TYPE margintype;
291
292         ///
293         bool fill_top;
294
295         ///
296         bool fill_bottom;
297
298         ///
299         bool newline_allowed;
300
301         ///
302         bool nextnoindent;
303
304         ///
305         bool free_spacing;
306         /// true when the fragile commands in the paragraph need to be
307         /// \protect'ed.
308         bool needprotect;
309         /// true when empty paragraphs should be kept.
310         bool keepempty;
311         ///
312         bool isParagraph() const {
313                 return latextype == LATEX_PARAGRAPH;
314         }
315         ///
316         bool isCommand() const { 
317                 return latextype == LATEX_COMMAND;
318         }
319         ///
320         bool isEnvironment() const {
321                 return (latextype == LATEX_ENVIRONMENT
322                         || latextype == LATEX_ITEM_ENVIRONMENT
323                         || latextype == LATEX_LIST_ENVIRONMENT);
324         }
325         /// Type of LaTeX object
326         LYX_LATEX_TYPES latextype;
327         /// Does this object belong in the title part of the document?
328         bool intitle;
329 private:
330         /// Name of the layout/paragraph environment
331         string name_;
332
333         /** Name of an layout that has replaced this layout.
334             This is used to rename a layout, while keeping backward
335             compatibility 
336         */
337         string obsoleted_by_;
338
339         /// LaTeX name for environment
340         string latexname_;
341
342         /// Label string. "Abstract", "Reference", "Caption"...
343         string labelstring_;
344
345         ///
346         string endlabelstring_;
347
348         /// Label string inside appendix. "Appendix", ...
349         string labelstring_appendix_;
350
351         /// LaTeX parameter for environment
352         string latexparam_;
353
354         /// Macro definitions needed for this layout
355         string preamble_;
356 };
357
358
359 ///
360 class LyXTextClass {
361 public:
362         ///
363         typedef std::vector<LyXLayout> LayoutList;
364         ///
365         typedef LayoutList::const_iterator const_iterator;
366         ///
367         typedef LayoutList::size_type size_type;
368         ///
369         explicit
370         LyXTextClass (string const & = string(), 
371                       string const & = string(), 
372                       string const & = string());
373
374         ///
375         const_iterator begin() const { return layoutlist.begin(); }
376         ///
377         const_iterator end() const { return layoutlist.end(); }
378         
379         ///
380         bool Read(string const & filename, bool merge = false);
381         void readOutputType(LyXLex &);
382         void readMaxCounter(LyXLex &);
383         void readClassOptions(LyXLex &);
384         ///
385         bool hasLayout(string const & name) const;
386
387         ///
388         LyXLayout const & GetLayout(string const & vname) const;
389
390         ///
391         LyXLayout & GetLayout(string const & vname);
392
393         /// Sees to that the textclass structure has been loaded
394         void load();
395
396         ///
397         string const & name() const { return name_; }
398         ///
399         string const & latexname() const { return latexname_; }
400         ///
401         string const & description() const { return description_; }
402         ///
403         string const & opt_fontsize() const { return opt_fontsize_; }
404         ///
405         string const & opt_pagestyle() const { return opt_pagestyle_; }
406         ///
407         string const & options() const { return options_; }
408         ///
409         string const & pagestyle() const { return pagestyle_; }
410         ///
411         string const & preamble() const { return preamble_; }
412
413         /// Packages that are already loaded by the class
414         enum Provides {
415                 nothing = 0,
416                 amsmath = 1,
417                 makeidx = 2,
418                 url = 4
419         };
420         bool provides(Provides p) const { return provides_ & p; }
421         
422         ///
423         unsigned int columns() const { return columns_; }
424         ///
425         enum PageSides {
426                 OneSide,
427                 TwoSides
428         };
429         ///
430         PageSides sides() const { return sides_; }
431         ///
432         int secnumdepth() const { return secnumdepth_; }
433         ///
434         int tocdepth() const { return tocdepth_; }
435
436         ///
437         OutputType outputType() const { return outputType_; }
438
439         ///
440         LyXFont const & defaultfont() const { return defaultfont_; }
441
442         /// Text that dictates how wide the left margin is on the screen
443         string const & leftmargin() const { return leftmargin_; }
444
445         /// Text that dictates how wide the right margin is on the screen
446         string const & rightmargin() const { return rightmargin_; }
447         ///
448         int maxcounter() const { return maxcounter_; }
449         ///
450         size_type numLayouts() const { return layoutlist.size(); }
451         ///
452         LyXLayout const & operator[](size_type i) const {
453                 return layoutlist[i];
454         }
455 private:
456         ///
457         bool delete_layout(string const &);
458         ///
459         bool do_readStyle(LyXLex &, LyXLayout &);
460         ///
461         string name_;
462         ///
463         string latexname_;
464         ///
465         string description_;
466         /// Specific class options
467         string opt_fontsize_;
468         ///
469         string opt_pagestyle_;
470         ///
471         string options_;
472         ///
473         string pagestyle_;
474         ///
475         string preamble_;
476         ///
477         Provides provides_;
478         ///
479         unsigned int columns_;
480         ///
481         PageSides sides_;
482         ///
483         int secnumdepth_;
484         ///
485         int tocdepth_;
486         ///
487         OutputType outputType_;
488         /** Base font. The paragraph and layout fonts are resolved against
489             this font. This has to be fully instantiated. Attributes
490             LyXFont::INHERIT, LyXFont::IGNORE, and LyXFont::TOGGLE are
491             extremely illegal.
492         */
493         LyXFont defaultfont_;
494         /// Text that dictates how wide the left margin is on the screen
495         string leftmargin_;
496
497         /// Text that dictates how wide the right margin is on the screen
498         string rightmargin_;
499         ///
500         int maxcounter_; // add approp. signedness
501
502         ///
503         LayoutList layoutlist;
504
505         /// Has this layout file been loaded yet?
506         bool loaded;
507 };
508
509
510 inline
511 void operator|=(LyXTextClass::Provides & p1, LyXTextClass::Provides p2)
512 {
513         p1 = static_cast<LyXTextClass::Provides>(p1 | p2);
514 }
515
516
517 ///
518 std::ostream & operator<<(std::ostream & os, LyXTextClass::PageSides p);
519
520
521 ///
522 class LyXTextClassList {
523 public:
524         ///
525         typedef std::vector<LyXTextClass> ClassList;
526         ///
527         typedef ClassList::const_iterator const_iterator;
528         ///
529         typedef ClassList::size_type size_type;
530         ///
531         const_iterator begin() const { return classlist.begin(); }
532         ///
533         const_iterator end() const { return classlist.end(); }
534         
535         /// Gets layout structure from layout number and textclass number
536         LyXLayout const & Style(size_type textclass,
537                                 LyXTextClass::size_type layout) const;
538
539         /// Gets layout number from textclass number and layout name
540         std::pair<bool, LyXTextClass::size_type>
541         NumberOfLayout(size_type textclass,
542                        string const & name) const;
543
544         /// Gets a layout name from layout number and textclass number
545         string const &
546         NameOfLayout(size_type textclass,
547                      LyXTextClass::size_type layout) const;
548
549         /** Gets textclass number from name.
550             Returns -1 if textclass name does not exist
551         */
552         std::pair<bool, size_type>
553         NumberOfClass(string const & textclass) const;
554
555         ///
556         string const & NameOfClass(size_type number) const;
557
558         ///
559         string const & LatexnameOfClass(size_type number) const;
560
561         ///
562         string const & DescOfClass(size_type number) const;
563
564         ///
565         LyXTextClass const & TextClass(size_type textclass) const;
566
567         /** Read textclass list.
568             Returns false if this fails
569         */
570         bool Read();
571
572         /** Load textclass.
573             Returns false if this fails
574         */
575         bool Load(size_type number) const;
576 private:
577         ///
578         mutable ClassList classlist;
579         ///
580         void Add (LyXTextClass const &);
581 };
582
583 /// Should not be declared here!! (Lgb) Why not? (Asger)
584 extern LyXTextClassList textclasslist;
585  
586 #endif