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