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