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