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