]> git.lyx.org Git - lyx.git/blob - src/layout.h
57f78a30f584cc7ed9068ea321e918f3acbe8980
[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 #ifdef __GNUG__
16 #pragma interface
17 #endif
18
19 #include "lyxlex.h"
20 #include "lyxfont.h"
21 #include "Spacing.h"
22
23 #include <vector>
24
25 using std::ostream;
26 using std::vector;
27 using std::pair;
28
29 /// Reads the style files
30 extern void LyXSetStyle();
31
32 ///
33 enum { // no good name for this
34         ///
35         LYX_ENVIRONMENT_DEFAULT = 97,
36         ///
37         LYX_LAYOUT_DEFAULT = 99
38 };
39 // Could this cause confusion that both DUMMY_LAYOUT and  LAYOUT_DEFAULT has
40 // the same value? (Lgb)
41 ///
42 #define LYX_DUMMY_LAYOUT 99
43
44 /// The different output types
45 enum OutputType {
46         ///
47         LATEX = 1,
48         ///
49         LINUXDOC,
50         ///
51         DOCBOOK,
52         ///
53         LITERATE
54 };
55
56 /// The different margin types
57 enum LYX_MARGIN_TYPE {
58         ///
59         MARGIN_MANUAL = 1,
60         ///
61         MARGIN_FIRST_DYNAMIC,
62         ///
63         MARGIN_DYNAMIC,
64         ///
65         MARGIN_STATIC,
66         ///
67         MARGIN_RIGHT_ADDRESS_BOX
68 };
69
70
71 ///
72 enum LyXAlignment {
73         ///
74         LYX_ALIGN_NONE = 0,
75         ///
76         LYX_ALIGN_BLOCK = 1,
77         ///
78         LYX_ALIGN_LEFT = 2,
79         ///
80         LYX_ALIGN_RIGHT = 4,
81         ///
82         LYX_ALIGN_CENTER = 8,
83         ///
84         LYX_ALIGN_LAYOUT = 16,
85         ///
86         LYX_ALIGN_SPECIAL = 32
87 };
88
89
90 inline
91 void operator|=(LyXAlignment & la1, LyXAlignment la2) {
92         la1 = static_cast<LyXAlignment>(la1 | la2);
93 }
94
95 /// The different LaTeX-Types
96 enum LYX_LATEX_TYPES {
97         ///
98         LATEX_PARAGRAPH = 1,
99         ///
100         LATEX_COMMAND,
101         ///
102         LATEX_ENVIRONMENT,
103         ///
104         LATEX_ITEM_ENVIRONMENT,
105         ///
106         LATEX_LIST_ENVIRONMENT
107 };
108
109 /// The different label types
110 enum LYX_LABEL_TYPES {
111         ///
112         LABEL_NO_LABEL,
113         ///
114         LABEL_MANUAL,
115         ///
116         LABEL_BIBLIO,
117         ///
118         LABEL_TOP_ENVIRONMENT,
119         ///
120         LABEL_CENTERED_TOP_ENVIRONMENT,
121
122         // the flushright labels following now must start with LABEL_STATIC
123         ///
124         LABEL_STATIC,
125         ///
126         LABEL_SENSITIVE,
127         ///
128         LABEL_COUNTER_CHAPTER,
129         ///
130         LABEL_COUNTER_SECTION,
131         ///
132         LABEL_COUNTER_SUBSECTION,
133         ///
134         LABEL_COUNTER_SUBSUBSECTION,
135         ///
136         LABEL_COUNTER_PARAGRAPH,
137         ///
138         LABEL_COUNTER_SUBPARAGRAPH,
139         ///
140         LABEL_COUNTER_ENUMI,
141         ///
142         LABEL_COUNTER_ENUMII,
143         ///
144         LABEL_COUNTER_ENUMIII,
145         ///
146         LABEL_COUNTER_ENUMIV,
147         ///
148         LABEL_FIRST_COUNTER = LABEL_COUNTER_CHAPTER
149 };
150
151 enum LYX_END_LABEL_TYPES {
152         ///
153         END_LABEL_NO_LABEL,
154         ///
155         END_LABEL_BOX,
156         ///
157         END_LABEL_FILLED_BOX,
158         ///
159         END_LABEL_ENUM_FIRST = END_LABEL_NO_LABEL,
160         ///
161         END_LABEL_ENUM_LAST = END_LABEL_FILLED_BOX
162 };
163                 
164 /* Fix labels are printed flushright, manual labels flushleft. 
165  * MARGIN_MANUAL and MARGIN_FIRST_DYNAMIC are *only* for LABEL_MANUAL,
166  * MARGIN_DYNAMIC and MARGIN_STATIC are *not* for LABEL_MANUAL. 
167  * This seems a funny restriction, but I think other combinations are
168  * not needed, so I will not change it yet. 
169  * Correction: MARGIN_FIRST_DYNAMIC also usable with LABEL_STATIC
170  */
171
172
173 /* There is a parindent and a parskip. Which one is used depends on the 
174  * paragraph_separation-flag of the text-object. 
175  * BUT: parindent is only thrown away, if a parskip is defined! So if you
176  * want a space between the paragraphs and a parindent at the same time, 
177  * you should set parskip to zero and use topsep, parsep and bottomsep.
178  * 
179  * The standard layout is an exception: its parindent is only set, if the 
180  * previous paragraph is standard too. Well, this is LateX and it is good!
181  */ 
182
183
184 /// Attributes of a layout/paragraph environment
185 class LyXTextClass;
186
187 ///
188 class LyXLayout {
189 public:
190         ///
191         LyXLayout ();
192
193         ///
194         bool Read (LyXLex &, LyXTextClass const &);
195         void readAlign(LyXLex &);
196         void readAlignPossible(LyXLex &);
197         void readLabelType(LyXLex &);
198         void readEndLabelType(LyXLex &);
199         void readMargin(LyXLex &);
200         void readLatexType(LyXLex &);
201         void readSpacing(LyXLex &);
202         string const & name() const { return name_; }
203         void name(string const & n) { name_ = n; }
204         string const & obsoleted_by() const { return obsoleted_by_; }
205         string const & latexname() const { return latexname_; }
206         string const & labelstring() const { return labelstring_; }
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         /// Label string inside appendix. "Appendix", ...
346         string labelstring_appendix_;
347
348         /// LaTeX parameter for environment
349         string latexparam_;
350
351         /// Macro definitions needed for this layout
352         string preamble_;
353 };
354
355
356 ///
357 class LyXTextClass {
358 public:
359         ///
360         typedef vector<LyXLayout> LayoutList;
361         ///
362         typedef LayoutList::const_iterator const_iterator;
363         ///
364         typedef LayoutList::size_type size_type;
365         ///
366         LyXTextClass (string const & = string(), 
367                       string const & = string(), 
368                       string const & = string());
369
370         ///
371         const_iterator begin() const { return layoutlist.begin(); }
372         ///
373         const_iterator end() const { return layoutlist.end(); }
374         
375         ///
376         bool Read(string const & filename, bool merge = false);
377         void readOutputType(LyXLex &);
378         void readMaxCounter(LyXLex &);
379         void readClassOptions(LyXLex &);
380         ///
381         bool hasLayout(string const & name) const;
382
383         ///
384         LyXLayout const & GetLayout(string const & vname) const;
385
386         ///
387         LyXLayout & GetLayout(string const & vname);
388
389         /// Sees to that the textclass structure has been loaded
390         void load();
391
392         ///
393         string const & name() const { return name_; }
394         ///
395         string const & latexname() const { return latexname_; }
396         ///
397         string const & description() const { return description_; }
398         ///
399         string const & opt_fontsize() const { return opt_fontsize_; }
400         ///
401         string const & opt_pagestyle() const { return opt_pagestyle_; }
402         ///
403         string const & options() const { return options_; }
404         ///
405         string const & pagestyle() const { return pagestyle_; }
406         ///
407         string const & preamble() const { return preamble_; }
408
409         /// Packages that are already loaded by the class
410         enum Provides {
411                 nothing = 0,
412                 amsmath = 1,
413                 makeidx = 2,
414                 url = 4
415         };
416         bool provides(Provides p) const { return provides_ & p; }
417         
418         ///
419         unsigned int columns() const { return columns_; }
420         ///
421         enum PageSides {
422                 OneSide,
423                 TwoSides
424         };
425         ///
426         PageSides sides() const { return sides_; }
427         ///
428         int secnumdepth() const { return secnumdepth_; }
429         ///
430         int tocdepth() const { return tocdepth_; }
431
432         ///
433         OutputType outputType() const { return outputType_; }
434
435         ///
436         LyXFont const & defaultfont() const { return defaultfont_; }
437
438         /// Text that dictates how wide the left margin is on the screen
439         string const & leftmargin() const { return leftmargin_; }
440
441         /// Text that dictates how wide the right margin is on the screen
442         string const & rightmargin() const { return rightmargin_; }
443         ///
444         int maxcounter() const { return maxcounter_; }
445         ///
446         size_type numLayouts() const { return layoutlist.size(); }
447         ///
448         LyXLayout const & operator[](size_type i) const {
449                 return layoutlist[i];
450         }
451 private:
452         ///
453         bool delete_layout(string const &);
454         ///
455         bool do_readStyle(LyXLex &, LyXLayout &);
456         ///
457         string name_;
458         ///
459         string latexname_;
460         ///
461         string description_;
462         /// Specific class options
463         string opt_fontsize_;
464         ///
465         string opt_pagestyle_;
466         ///
467         string options_;
468         ///
469         string pagestyle_;
470         ///
471         string preamble_;
472         ///
473         Provides provides_;
474         ///
475         unsigned int columns_;
476         ///
477         PageSides sides_;
478         ///
479         int secnumdepth_;
480         ///
481         int tocdepth_;
482         ///
483         OutputType outputType_;
484         /** Base font. The paragraph and layout fonts are resolved against
485             this font. This has to be fully instantiated. Attributes
486             LyXFont::INHERIT, LyXFont::IGNORE, and LyXFont::TOGGLE are
487             extremely illegal.
488         */
489         LyXFont defaultfont_;
490         /// Text that dictates how wide the left margin is on the screen
491         string leftmargin_;
492
493         /// Text that dictates how wide the right margin is on the screen
494         string rightmargin_;
495         ///
496         int maxcounter_; // add approp. signedness
497
498         ///
499         LayoutList layoutlist;
500
501         /// Has this layout file been loaded yet?
502         bool loaded;
503 };
504
505
506 ///
507 inline
508 void operator|=(LyXTextClass::Provides & p1, LyXTextClass::Provides p2)
509 {
510         p1 = static_cast<LyXTextClass::Provides>(p1 | p2);
511 }
512
513
514 ///
515 inline
516 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
517 {
518         switch (p) {
519         case LyXTextClass::OneSide:
520                 os << "1";
521                 break;
522         case LyXTextClass::TwoSides:
523                 os << "2";
524                 break;
525         }
526         return os;
527 }
528
529
530 ///
531 class LyXTextClassList {
532 public:
533         ///
534         typedef vector<LyXTextClass> ClassList;
535         ///
536         typedef ClassList::const_iterator const_iterator;
537         ///
538         typedef ClassList::size_type size_type;
539         ///
540         const_iterator begin() const { return classlist.begin(); }
541         ///
542         const_iterator end() const { return classlist.end(); }
543         
544         /// Gets layout structure from layout number and textclass number
545         LyXLayout const & Style(size_type textclass,
546                                 LyXTextClass::size_type layout) const;
547
548         /// Gets layout number from textclass number and layout name
549         pair<bool, LyXTextClass::size_type>
550         NumberOfLayout(size_type textclass,
551                        string const & name) const;
552
553         /// Gets a layout name from layout number and textclass number
554         string const &
555         NameOfLayout(size_type textclass,
556                      LyXTextClass::size_type layout) const;
557
558         /** Gets textclass number from name.
559             Returns -1 if textclass name does not exist
560         */
561         pair<bool, size_type>
562         NumberOfClass(string const & textclass) const;
563
564         ///
565         string const & NameOfClass(size_type number) const;
566
567         ///
568         string const & LatexnameOfClass(size_type number) const;
569
570         ///
571         string const & DescOfClass(size_type number) const;
572
573         ///
574         LyXTextClass const & TextClass(size_type textclass) const;
575
576         /** Read textclass list.
577             Returns false if this fails
578         */
579         bool Read();
580
581         /** Load textclass.
582             Returns false if this fails
583         */
584         bool Load(size_type number) const;
585 private:
586         ///
587         mutable ClassList classlist;
588         ///
589         void Add (LyXTextClass const &);
590 };
591
592 /// Should not be declared here!! (Lgb) Why not? (Asger)
593 extern LyXTextClassList textclasslist;
594  
595 #endif