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