]> git.lyx.org Git - lyx.git/blob - src/lyxparagraph.h
faster compile of LColor.C
[lyx.git] / src / lyxparagraph.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 LYXPARAGRAPH_H
13 #define LYXPARAGRAPH_H
14
15 #ifdef __GNUG__
16 #pragma interface
17 #endif
18
19 #include <vector>
20 #include <list>
21
22 #include "insets/lyxinset.h"
23 #include "table.h"
24 #include "vspace.h"
25 #include "layout.h"
26 #include "support/block.h"
27 #include "language.h"
28
29 class BufferParams;
30 class LyXBuffer;
31 class TexRow;
32 struct LaTeXFeatures;
33 class InsetBibKey;
34
35 /// A LyXParagraph holds all text, attributes and insets in a text paragraph
36 class LyXParagraph  {
37 public:
38         ///
39         enum PEXTRA_TYPE {
40                 ///
41                 PEXTRA_NONE,
42                 ///
43                 PEXTRA_INDENT,
44                 ///
45                 PEXTRA_MINIPAGE,
46                 ///
47                 PEXTRA_FLOATFLT
48         };
49         ///
50         enum MINIPAGE_ALIGNMENT {
51                 ///
52                 MINIPAGE_ALIGN_TOP,
53                 ///
54                 MINIPAGE_ALIGN_MIDDLE,
55                 ///
56                 MINIPAGE_ALIGN_BOTTOM
57         };
58         ///
59         enum META_KIND {
60                 ///
61                 META_FOOTNOTE = 1,
62                 ///
63                 META_MARGIN,
64                 ///
65                 META_FIG,
66                 ///
67                 META_TAB,
68                 ///
69                 META_ALGORITHM,
70                 ///
71                 META_WIDE_FIG,
72                 ///
73                 META_WIDE_TAB,
74                 ///
75                 META_HFILL,
76                 ///
77                 META_NEWLINE,
78                 ///
79                 //META_PROTECTED_SEPARATOR,
80                 ///
81                 META_INSET
82         };
83
84         /// The footnoteflag
85         enum footnote_flag {
86                 ///
87                 NO_FOOTNOTE,
88                 ///
89                 OPEN_FOOTNOTE,
90                 ///
91                 CLOSED_FOOTNOTE
92         };
93
94         /// The footnotekinds
95         enum footnote_kind {
96                 ///
97                 FOOTNOTE,
98                 ///
99                 MARGIN,
100                 ///
101                 FIG,
102                 ///
103                 TAB,
104                 ///
105                 ALGORITHM,  // Bernhard, 970807
106                 ///
107                 WIDE_FIG,   // CFO-G, 971106
108                 ///
109                 WIDE_TAB    // CFO-G, 971106
110         };
111         
112         ///
113         typedef char value_type;
114         ///
115         typedef std::vector<value_type> TextContainer;
116         ///
117         typedef int size_type;
118
119         ///
120         LyXParagraph();
121         /// this konstruktor inserts the new paragraph in a list
122         explicit
123         LyXParagraph(LyXParagraph * par);
124         /// the destruktors removes the new paragraph from the list
125         ~LyXParagraph();
126
127         ///
128         Language const * getParLanguage() const;
129         ///
130         bool isRightToLeftPar() const;
131         ///
132         void ChangeLanguage(Language const * from, Language const * to);
133         ///
134         bool isMultiLingual();
135         ///
136         string String(bool label);
137         ///
138         string String(size_type beg, size_type end);
139         
140         ///
141         void writeFile(std::ostream &, BufferParams const &, char, char) const;
142         ///
143         void validate(LaTeXFeatures &) const;
144         
145         ///
146         int id() const {
147                 return id_;
148         }
149         ///
150         void id(int id_arg) {
151                 id_ = id_arg;
152         }
153
154         ///
155         void read();
156
157         ///
158         LyXParagraph * TeXOnePar(std::ostream &, TexRow & texrow,
159                                  bool moving_arg,
160                                  std::ostream & foot, TexRow & foot_texrow,
161                                  int & foot_count);
162         ///
163         bool SimpleTeXOnePar(std::ostream &, TexRow & texrow, bool moving_arg);
164
165         ///
166         LyXParagraph * TeXEnvironment(std::ostream &, TexRow & texrow,
167                                       std::ostream & foot, TexRow & foot_texrow,
168                                       int & foot_count);
169         ///
170         LyXParagraph * Clone() const;
171         
172         ///
173         bool HasSameLayout(LyXParagraph const * par) const;
174         
175         ///
176         void MakeSameLayout(LyXParagraph const * par);
177
178         /// Is it the first par with same depth and layout?
179         bool IsFirstInSequence() const {
180                 LyXParagraph const * dhook = DepthHook(GetDepth());
181                 return (dhook == this
182                         || dhook->GetLayout() != GetLayout()
183                         || dhook->GetDepth() != GetDepth());
184         }
185
186         /** Check if the current paragraph is the last paragraph in a
187             proof environment */
188         int GetEndLabel() const;
189         ///
190         Inset * InInset() { return inset_owner; }
191         ///
192         void SetInsetOwner(Inset * i) { inset_owner = i; }
193 private:
194         ///
195         TextContainer text;
196         ///
197         Inset * inset_owner;
198
199 public:
200         ///
201         size_type size() const { return text.size(); }
202         ///
203         void fitToSize() {
204                 text.resize(text.size());
205         }
206         void setContentsFromPar(LyXParagraph * par) {
207                 text = par->text;
208         }
209         void clearContents() {
210                 text.clear();
211         }
212         
213         /// 
214         VSpace added_space_top;
215         
216         /// 
217         VSpace added_space_bottom;
218
219         ///
220         Spacing spacing;
221         
222         ///
223         LyXTextClass::LayoutList::size_type layout;
224         
225         /**
226           \begin{itemize}
227           \item no footnote, closed footnote, 
228           \item open footnote, where footnote
229           \item means footnote-environment
230           \end{itemize}
231          */
232         footnote_flag footnoteflag;
233
234         /// footnote, margin, fig, tab
235         footnote_kind footnotekind;
236    
237         //@Man: the LyX- DTP-switches
238         //@{
239         ///
240         bool line_top;
241         
242         ///
243         bool line_bottom;
244         
245         ///
246         bool pagebreak_top;
247         
248         ///
249         bool pagebreak_bottom;
250         
251         ///
252         LyXAlignment align;
253         
254         ///
255         char depth;
256         
257         ///
258         bool noindent;
259         
260 private:
261         block<int, 10> counter_;
262 public:
263         ///
264         void setCounter(int i, int v) { counter_[i] = v; }
265         int getCounter(int i) const { return counter_[i]; }
266         void incCounter(int i) { counter_[i]++; }
267         ///
268         bool start_of_appendix;
269
270         ///
271         bool appendix;
272
273         ///
274         char enumdepth;
275         
276         ///
277         char itemdepth;
278
279         /* This is for the paragraph extra stuff */
280         ///
281         int pextra_type;
282         ///
283         string pextra_width;
284         ///
285         string pextra_widthp;
286         ///
287         int pextra_alignment;
288         ///
289         bool pextra_hfill;
290         ///
291         bool pextra_start_minipage;
292         
293         ///
294         string labelstring;
295         
296         ///
297         string labelwidthstring;
298         //@}
299         
300         ///
301         LyXParagraph * next;
302         ///
303         LyXParagraph * previous;
304
305         /* table stuff -- begin*/
306         ///
307         LyXTable * table;
308         /* table stuff -- end*/
309
310         /// 
311         InsetBibKey * bibkey;  // ale970302
312
313         /** these function are able to hide closed footnotes
314          */
315         LyXParagraph * Next();
316         
317         ///
318         LyXParagraph * Previous();
319         ///
320         LyXParagraph const * Previous() const;
321
322         /** these function are able to hide open and closed footnotes
323          */ 
324         LyXParagraph * NextAfterFootnote();
325         ///
326         LyXParagraph const * NextAfterFootnote() const;
327         
328         ///
329         LyXParagraph * PreviousBeforeFootnote();
330         ///
331         LyXParagraph * LastPhysicalPar();
332         ///
333         LyXParagraph const * LastPhysicalPar() const;
334         
335         ///
336         LyXParagraph * FirstPhysicalPar();
337         ///
338         LyXParagraph const * FirstPhysicalPar() const;
339
340         /// returns the physical paragraph
341         LyXParagraph * ParFromPos(size_type pos);
342         /// returns the position in the physical par
343         int PositionInParFromPos(size_type pos) const;
344
345         /// for the environments
346         LyXParagraph * DepthHook(int depth);
347         /// for the environments
348         LyXParagraph const * DepthHook(int depth) const;
349         ///
350         int BeginningOfMainBody() const;
351         ///
352         string GetLabelstring() const;
353         
354         /// the next two functions are for the manual labels
355         string GetLabelWidthString() const;
356         ///
357         void SetLabelWidthString(string const & s);
358         ///
359         LyXTextClass::LayoutList::size_type GetLayout() const;
360         ///
361         char GetAlign() const;
362         ///
363         char GetDepth() const;
364         ///
365         void SetLayout(LyXTextClass::LayoutList::size_type new_layout);
366         ///
367         void SetOnlyLayout(LyXTextClass::LayoutList::size_type new_layout);
368         ///
369         int GetFirstCounter(int i) const;
370         ///
371         size_type Last() const;
372         ///
373         void Erase(size_type pos);
374         /** the flag determines wether the layout should be copied
375          */ 
376         void BreakParagraph(size_type pos, int flag);
377         ///
378         void BreakParagraphConservative(size_type pos);
379         /** Get unistantiated font setting. Returns the difference
380           between the characters font and the layoutfont.
381           This is what is stored in the fonttable
382          */
383         LyXFont GetFontSettings(size_type pos) const;
384         ///
385         LyXFont GetFirstFontSettings() const;
386
387         /** Get fully instantiated font. If pos == -1, use the layout
388           font attached to this paragraph.
389           If pos == -2, use the label font of the layout attached here.
390           In all cases, the font is instantiated, i.e. does not have any
391           attributes with values LyXFont::INHERIT, LyXFont::IGNORE or 
392           LyXFont::TOGGLE.
393           */
394         LyXFont getFont(size_type pos) const;
395         ///
396         char GetChar(size_type pos);
397         ///
398         char GetChar(size_type pos) const;
399         /// The position must already exist.
400         void SetChar(size_type pos, char c) {
401                 text[pos] = c;
402         }
403         
404         ///
405         void SetFont(size_type pos, LyXFont const & font);
406         ///
407         string GetWord(size_type &) const;
408         /// Returns the height of the highest font in range
409         LyXFont::FONT_SIZE HighestFontInRange(size_type startpos,
410                                               size_type endpos) const;
411         ///
412         void InsertChar(size_type pos, char c);
413         ///
414         void InsertInset(size_type pos, Inset * inset);
415         ///
416         bool InsertInsetAllowed(Inset * inset);
417         ///
418         Inset * GetInset(size_type pos);
419         ///
420         Inset const * GetInset(size_type pos) const;
421         ///
422         void OpenFootnote(size_type pos);
423         ///
424         void CloseFootnote(size_type pos);
425         /// important for cut and paste
426         void CopyIntoMinibuffer(size_type pos) const;
427         ///
428         void CutIntoMinibuffer(size_type pos);
429         ///
430         bool InsertFromMinibuffer(size_type pos);
431
432         ///
433         bool IsHfill(size_type pos) const;
434         ///
435         bool IsInset(size_type pos) const;
436         ///
437         bool IsFloat(size_type pos) const;
438         ///
439         bool IsNewline(size_type pos) const;
440         ///
441         bool IsSeparator(size_type pos) const;
442         ///
443         bool IsLineSeparator(size_type pos) const;
444         ///
445         bool IsKomma(size_type pos) const;
446         /// Used by the spellchecker
447         bool IsLetter(size_type pos) const;
448         /// 
449         bool IsWord(size_type pos) const;
450
451         /** This one resets all layout and dtp switches but not the font
452          of the single characters
453          */ 
454         void Clear();
455
456         /** paste this paragraph with the next one
457           be carefull, this doesent make any check at all
458           */ 
459         void PasteParagraph();
460
461         /// used to remove the error messages
462         int AutoDeleteInsets();
463
464         /// returns -1 if inset not found
465         int GetPositionOfInset(Inset * inset) const;
466         
467         /// ok and now some footnote functions
468         void OpenFootnotes();
469
470         ///
471         void CloseFootnotes();
472    
473         ///
474         LyXParagraph * FirstSelfrowPar();
475
476         ///
477         int ClearParagraph() {
478                 int i = 0;
479                 if (!IsDummy() && !table){
480                         while (Last()
481                                && (IsNewline(0) 
482                                    || IsLineSeparator(0))){
483                                 Erase(0);
484                                 ++i;
485                         }
486                 }
487                 return i;
488         }
489         
490         /** A paragraph following a footnote is a "dummy". A paragraph
491           with a footnote in it is stored as three paragraphs:
492           First a paragraph with the text up to the footnote, then
493           one (or more) paragraphs with the footnote, and finally
494           the a paragraph with the text after the footnote. Only the
495           first paragraph keeps information  about layoutparameters, */
496         bool IsDummy() const;
497
498         /* If I set a PExtra Indent on one paragraph of a ENV_LIST-TYPE
499            I have to set it on each of it's elements */
500         ///
501         void SetPExtraType(int type, char const * width, char const * widthp);
502         ///
503         void UnsetPExtraType();
504 #if 0
505         ///
506         bool RoffContTableRows(std::ostream &, size_type i, int actcell);
507 #endif
508         ///
509         bool linuxDocConvertChar(char c, string & sgml_string);
510         ///
511         void DocBookContTableRows(std::ostream &, string & extra,
512                                   int & desc_on, size_type i,
513                                   int current_cell_number, int & column);
514         ///
515         void SimpleDocBookOneTablePar(std::ostream &, string & extra,
516                                       int & desc_on, int depth);
517 private:
518         ///
519         struct InsetTable {
520                 ///
521                 size_type pos;
522                 ///
523                 Inset * inset;
524                 ///
525                 InsetTable(size_type p, Inset * i) { pos = p; inset = i;}
526         };
527         ///
528         friend struct matchIT;
529         ///
530         struct matchIT {
531                 /// used by lower_bound
532                 inline
533                 int operator()(LyXParagraph::InsetTable const & a,
534                                LyXParagraph::size_type pos) const {
535                         return a.pos < pos;
536                 }
537                 /// used by upper_bound
538                 inline
539                 int operator()(LyXParagraph::size_type pos,
540                                LyXParagraph::InsetTable const & a) const {
541                         return pos < a.pos;
542                 }
543         };
544         /** A font entry covers a range of positions. Notice that the
545           entries in the list are inserted in random order.
546           I don't think it's worth the effort to implement a more effective
547           datastructure, because the number of different fonts in a paragraph
548           is limited. (Asger)
549         */
550         struct FontTable  {
551                 /// Start position of paragraph this font attribute covers
552                 size_type pos;
553                 /// Ending position of paragraph this font attribute covers
554                 size_type pos_end;
555                 /** Font. Interpretation of the font values:
556                 If a value is LyXFont::INHERIT_*, it means that the font 
557                 attribute is inherited from either the layout of this
558                 paragraph or, in the case of nested paragraphs, from the 
559                 layout in the environment one level up until completely 
560                 resolved.
561                 The values LyXFont::IGNORE_* and LyXFont::TOGGLE are NOT 
562                 allowed in these font tables.
563                 */
564                 LyXFont font;
565         };
566         ///
567         typedef std::list<FontTable> FontList;
568         ///
569         FontList fontlist;
570         ///
571         typedef std::vector<InsetTable> InsetList;
572         ///
573         InsetList insetlist;
574         ///
575         LyXParagraph * TeXDeeper(std::ostream &, TexRow & texrow,
576                                  std::ostream & foot, TexRow & foot_texrow,
577                                  int & foot_count);
578         ///
579         LyXParagraph * TeXFootnote(std::ostream &, TexRow & texrow,
580                                    std::ostream & foot, TexRow & foot_texrow,
581                                    int & foot_count,
582                                    bool parent_is_rtl);
583         ///
584         bool SimpleTeXOneTablePar(std::ostream &, TexRow & texrow);
585         ///
586         bool TeXContTableRows(std::ostream &, size_type i,
587                               int current_cell_number,
588                               int & column, TexRow & texrow);
589         ///
590         void SimpleTeXBlanks(std::ostream &, TexRow & texrow,
591                              size_type const i,
592                              int & column, LyXFont const & font,
593                              LyXLayout const & style);
594         ///
595         void SimpleTeXSpecialChars(std::ostream &, TexRow & texrow,
596                                    bool moving_arg,
597                                    LyXFont & font, LyXFont & running_font,
598                                    LyXFont & basefont, bool & open_font,
599                                    LyXLayout const & style,
600                                    size_type & i,
601                                    int & column, char const c);
602         ///
603         unsigned int id_;
604         ///
605         static unsigned int paragraph_id;
606 public:
607         class inset_iterator {
608         public:
609                 inset_iterator() {}
610                 inset_iterator(InsetList::iterator const & iter) : it(iter) {};
611                 inset_iterator & operator++() {
612                         ++it;
613                         return *this;
614                 }
615                 Inset * operator*() { return (*it).inset; }
616                 size_type getPos() {return (*it).pos; }
617                 bool operator==(inset_iterator const & iter) const {
618                         return it == iter.it;
619                 }
620                 bool operator!=(inset_iterator const & iter) const {
621                         return it != iter.it;
622                 }
623         private:
624                 InsetList::iterator it;
625         };
626         ///
627         inset_iterator inset_iterator_begin() {
628                 return inset_iterator(insetlist.begin());
629         }
630         ///
631         inset_iterator inset_iterator_end() {
632                 return inset_iterator(insetlist.end());
633         }
634         ///
635         inset_iterator InsetIterator(size_type pos);
636
637 };
638
639 #endif