]> git.lyx.org Git - lyx.git/blob - src/lyxparagraph.h
75d49444ee94c8a9c0f2d67da7777bbf08bb63d6
[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 "insets/insetbib.h"
24 #include "table.h"
25 #include "vspace.h"
26 #include "layout.h"
27 #include "support/block.h"
28 #include "direction.h"
29
30 class BufferParams;
31 class LyXBuffer;
32 class TexRow;
33 struct LaTeXFeatures;
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 &, char, char);
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         ///
154         LyXParagraph * TeXOnePar(string & file, TexRow & texrow,
155                                 string & foot, TexRow & foot_texrow,
156                                 int & foot_count);
157
158         ///
159         LyXParagraph * TeXEnvironment(string & file, TexRow & texrow,
160                                      string & foot, TexRow & foot_texrow,
161                                      int & foot_count);
162         
163         ///
164         LyXParagraph * Clone() const;
165         
166         ///
167         bool HasSameLayout(LyXParagraph const * par) const;
168         
169         ///
170         void MakeSameLayout(LyXParagraph const * par);
171
172         /// Is it the first par with same depth and layout?
173         bool IsFirstInSequence() const {
174                 LyXParagraph const * dhook = DepthHook(GetDepth());
175                 return (dhook == this
176                         || dhook->GetLayout() != GetLayout()
177                         || dhook->GetDepth() != GetDepth());
178         }
179
180
181         ///
182         TextContainer text;
183         ///
184         size_type size() const { return text.size(); }
185         /// 
186         VSpace added_space_top;
187         
188         /// 
189         VSpace added_space_bottom;
190         
191         ///
192         LyXTextClass::LayoutList::size_type layout;
193         
194         /**
195           \begin{itemize}
196           \item no footnote, closed footnote, 
197           \item open footnote, where footnote
198           \item means footnote-environment
199           \end{itemize}
200          */
201         footnote_flag footnoteflag;
202
203         /// footnote, margin, fig, tab
204         footnote_kind footnotekind;
205    
206         //@Man: the LyX- DTP-switches
207         //@{
208         ///
209         bool line_top;
210         
211         ///
212         bool line_bottom;
213         
214         ///
215         bool pagebreak_top;
216         
217         ///
218         bool pagebreak_bottom;
219         
220         ///
221         LyXAlignment align;
222         
223         ///
224         char depth;
225         
226         ///
227         bool noindent;
228         
229 private:
230         block<int, 10> counter_;
231 public:
232         ///
233         void setCounter(int i, int v) { counter_[i] = v; }
234         int getCounter(int i) const { return counter_[i]; }
235         void incCounter(int i) { counter_[i]++; }
236         ///
237         bool start_of_appendix;
238
239         ///
240         bool appendix;
241
242         ///
243         char enumdepth;
244         
245         ///
246         char itemdepth;
247
248         /* This is for the paragraph extra stuff */
249         ///
250         int pextra_type;
251         ///
252         string pextra_width;
253         ///
254         string pextra_widthp;
255         ///
256         int pextra_alignment;
257         ///
258         bool pextra_hfill;
259         ///
260         bool pextra_start_minipage;
261         
262         ///
263         string labelstring;
264         
265         ///
266         string labelwidthstring;
267         //@}
268         
269         ///
270         LyXParagraph * next;
271         ///
272         LyXParagraph * previous;
273
274         /* table stuff -- begin*/
275         ///
276         LyXTable * table;
277         /* table stuff -- end*/
278
279         /// 
280         InsetBibKey * bibkey;  // ale970302
281
282         /** these function are able to hide closed footnotes
283          */
284         LyXParagraph * Next();
285         
286         ///
287         LyXParagraph * Previous();
288         ///
289         LyXParagraph const * Previous() const;
290
291         /** these function are able to hide open and closed footnotes
292          */ 
293         LyXParagraph * NextAfterFootnote();
294         ///
295         LyXParagraph const * NextAfterFootnote() const;
296         
297         ///
298         LyXParagraph * PreviousBeforeFootnote();
299         ///
300         LyXParagraph * LastPhysicalPar();
301         
302         ///
303         LyXParagraph * FirstPhysicalPar();
304         ///
305         LyXParagraph const * FirstPhysicalPar() const;
306
307         /// returns the physical paragraph
308         LyXParagraph * ParFromPos(size_type pos);
309         /// returns the position in the physical par
310         int PositionInParFromPos(size_type pos) const;
311
312         /// for the environments
313         LyXParagraph * DepthHook(int depth);
314         /// for the environments
315         LyXParagraph const * DepthHook(int depth) const;
316         ///
317         int BeginningOfMainBody() const;
318         ///
319         string GetLabestring() const;
320         
321         /// the next two functions are for the manual labels
322         string GetLabelWidthString() const;
323         ///
324         void SetLabelWidthString(string const & s);
325         ///
326         LyXTextClass::LayoutList::size_type GetLayout() const;
327         ///
328         char GetAlign() const;
329         ///
330         char GetDepth() const;
331         ///
332         void SetLayout(LyXTextClass::LayoutList::size_type new_layout);
333         ///
334         void SetOnlyLayout(LyXTextClass::LayoutList::size_type new_layout);
335         ///
336         int GetFirstCounter(int i) const;
337         ///
338         size_type Last() const;
339         ///
340         void Erase(size_type pos);
341         /** the flag determines wether the layout should be copied
342          */ 
343         void BreakParagraph(size_type pos, int flag);
344         ///
345         void BreakParagraphConservative(size_type pos);
346         /** Get unistantiated font setting. Returns the difference
347           between the characters font and the layoutfont.
348           This is what is stored in the fonttable
349          */
350         LyXFont GetFontSettings(size_type pos) const;
351         /** Get fully instantiated font. If pos == -1, use the layout
352           font attached to this paragraph.
353           If pos == -2, use the label font of the layout attached here.
354           In all cases, the font is instantiated, i.e. does not have any
355           attributes with values LyXFont::INHERIT, LyXFont::IGNORE or 
356           LyXFont::TOGGLE.
357           */
358         LyXFont getFont(size_type pos) const;
359         ///
360         char GetChar(size_type pos);
361         ///
362         char GetChar(size_type pos) const;
363         ///
364         void SetFont(size_type pos, LyXFont const & font);
365         ///
366         string GetWord(size_type &) const;
367         /// Returns the height of the highest font in range
368         LyXFont::FONT_SIZE HighestFontInRange(size_type startpos,
369                                               size_type endpos) const;
370         ///
371         void InsertChar(size_type pos, char c);
372         ///
373         void InsertInset(size_type pos, Inset * inset);
374         ///
375         Inset * GetInset(size_type pos);
376         ///
377         Inset const * GetInset(size_type pos) const;
378         ///
379         Inset * ReturnNextInsetPointer(size_type & pos);
380         ///
381         void OpenFootnote(size_type pos);
382         ///
383         void CloseFootnote(size_type pos);
384         /// important for cut and paste
385         void CopyIntoMinibuffer(size_type pos) const;
386         ///
387         void CutIntoMinibuffer(size_type pos);
388         ///
389         void InsertFromMinibuffer(size_type pos);
390
391         ///
392         bool IsHfill(size_type pos) const;
393         ///
394         bool IsInset(size_type pos) const;
395         ///
396         bool IsFloat(size_type pos) const;
397         ///
398         bool IsNewline(size_type pos) const;
399         ///
400         bool IsSeparator(size_type pos) const;
401         ///
402         bool IsLineSeparator(size_type pos) const;
403         ///
404         bool IsKomma(size_type pos) const;
405         /// Used by the spellchecker
406         bool IsLetter(size_type pos) const;
407         /// 
408         bool IsWord(size_type pos) const;
409
410         /** This one resets all layout and dtp switches but not the font
411          of the single characters
412          */ 
413         void Clear();
414
415         /** paste this paragraph with the next one
416           be carefull, this doesent make any check at all
417           */ 
418         void PasteParagraph();
419
420         /// used to remove the error messages
421         int AutoDeleteInsets();
422
423         /// returns -1 if inset not found
424         int GetPositionOfInset(Inset * inset) const;
425         
426         /// ok and now some footnote functions
427         void OpenFootnotes();
428
429         ///
430         void CloseFootnotes();
431    
432         ///
433         LyXParagraph * FirstSelfrowPar();
434
435         ///
436         int ClearParagraph() {
437                 int i = 0;
438                 if (!IsDummy() && !table){
439                         while (Last()
440                                && (IsNewline(0) 
441                                    || IsLineSeparator(0))){
442                                 Erase(0);
443                                 ++i;
444                         }
445                 }
446                 return i;
447         }
448         
449         /** A paragraph following a footnote is a "dummy". A paragraph
450           with a footnote in it is stored as three paragraphs:
451           First a paragraph with the text up to the footnote, then
452           one (or more) paragraphs with the footnote, and finally
453           the a paragraph with the text after the footnote. Only the
454           first paragraph keeps information  about layoutparameters, */
455         bool IsDummy() const {
456                 return (footnoteflag == LyXParagraph::NO_FOOTNOTE && previous
457                         && previous->footnoteflag != LyXParagraph::NO_FOOTNOTE);
458         }
459
460         /* If I set a PExtra Indent on one paragraph of a ENV_LIST-TYPE
461            I have to set it on each of it's elements */
462         ///
463         void SetPExtraType(int type, char const * width, char const * widthp);
464         ///
465         void UnsetPExtraType();
466         ///
467         bool RoffContTableRows(ostream &, size_type i, int actcell);
468         ///
469         void DocBookContTableRows(string & file, string & extra, int & desc_on,
470                                   size_type i,
471                                   int current_cell_number, int & column);
472         ///
473         bool linuxDocConvertChar(char c, string & sgml_string);
474         ///
475         void SimpleDocBookOneTablePar(string & file, string & extra,
476                                       int & desc_on, int depth);
477 private:
478         /** A font entry covers a range of positions. Notice that the
479           entries in the list are inserted in random order.
480           I don't think it's worth the effort to implement a more effective
481           datastructure, because the number of different fonts in a paragraph
482           is limited. (Asger)
483         */
484         struct FontTable  {
485                 /// Start position of paragraph this font attribute covers
486                 size_type pos;
487                 /// Ending position of paragraph this font attribute covers
488                 size_type pos_end;
489                 /** Font. Interpretation of the font values:
490                 If a value is LyXFont::INHERIT_*, it means that the font 
491                 attribute is inherited from either the layout of this
492                 paragraph or, in the case of nested paragraphs, from the 
493                 layout in the environment one level up until completely 
494                 resolved.
495                 The values LyXFont::IGNORE_* and LyXFont::TOGGLE are NOT 
496                 allowed in these font tables.
497                 */
498                 LyXFont font;
499         };
500         ///
501         struct InsetTable {
502                 ///
503                 size_type pos;
504                 ///
505                 Inset * inset;
506         };
507         ///
508         typedef list<FontTable> FontList;
509         ///
510         FontList fontlist;
511         ///
512         typedef list<InsetTable> InsetList;
513         ///
514         InsetList insetlist;
515         ///
516         LyXParagraph * TeXDeeper(string & file, TexRow & texrow,
517                                    string & foot, TexRow & foot_texrow,
518                                    int & foot_count);
519         ///
520         LyXParagraph * TeXFootnote(string & file, TexRow & texrow,
521                                    string & foot, TexRow & foot_texrow,
522                                    int & foot_count,
523                                    LyXDirection par_direction);
524         ///
525         bool SimpleTeXOnePar(string & file, TexRow & texrow);
526         ///
527         bool SimpleTeXOneTablePar(string & file, TexRow & texrow);
528         ///
529         bool TeXContTableRows(string & file, size_type i,
530                               int current_cell_number,
531                               int & column, TexRow & texrow);
532         ///
533         void SimpleTeXBlanks(string & file, TexRow & texrow,
534                              size_type const i,
535                              int & column, LyXFont const & font,
536                              LyXLayout const & style);
537         ///
538         void SimpleTeXSpecialChars(string & file, TexRow & texrow,
539                                    LyXFont & font, LyXFont & running_font,
540                                    LyXFont & basefont, bool & open_font,
541                                    LyXLayout const & style,
542                                    size_type & i,
543                                    int & column, char const c);
544         ///
545         unsigned int id_;
546         ///
547         static unsigned int paragraph_id;
548 };
549 #endif