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