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