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