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