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