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