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