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