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