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