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