]> git.lyx.org Git - lyx.git/blob - src/paragraph.h
clean code to export between different flavours, output different code for sgml to...
[lyx.git] / src / paragraph.h
1 // -*- C++ -*-
2 /**
3  * \file paragraph.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Asger Alstrup
8  * \author Lars Gullik Bjønnes
9  * \author John Levon
10  * \author André Pönitz
11  * \author Jürgen Vigna
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #ifndef PARAGRAPH_H
17 #define PARAGRAPH_H
18
19 #include "changes.h"
20 #include "InsetList.h"
21 #include "lyxlayout_ptr_fwd.h"
22 #include "RowList_fwd.h"
23
24 #include "insets/insetbase.h" // only for InsetBase::Code
25
26 #include "support/types.h"
27
28 #include <string>
29
30 class Buffer;
31 class BufferParams;
32 class BufferView;
33 class Counters;
34 class InsetBase;
35 class InsetBibitem;
36 class InsetOld_code;
37 class Language;
38 class LaTeXFeatures;
39 class OutputParams;
40 class LyXFont;
41 class LyXFont_size;
42 class ParagraphParameters;
43 class TexRow;
44 class UpdatableInset;
45
46 /// A Paragraph holds all text, attributes and insets in a text paragraph
47 class Paragraph  {
48 public:
49         ///
50         enum {
51                 /// Note that this is 1 right now to avoid
52                 /// crashes where getChar() is called wrongly
53                 /// (returning 0) - if this was 0, then we'd
54                 /// try getInset() and crash. We should fix
55                 /// all these places.
56                 META_INSET = 1
57         };
58         ///
59         typedef char value_type;
60         ///
61         typedef lyx::depth_type depth_type;
62         ///
63         typedef std::vector<value_type> TextContainer;
64
65         ///
66         Paragraph();
67         ///
68         Paragraph(Paragraph const &);
69         ///
70         Paragraph & operator=(Paragraph const &);
71         ///
72         ~Paragraph();
73
74         ///
75         int id() const;
76
77         ///
78         Language const * getParLanguage(BufferParams const &) const;
79         ///
80         bool isRightToLeftPar(BufferParams const &) const;
81         ///
82         void changeLanguage(BufferParams const & bparams,
83                             Language const * from, Language const * to);
84         ///
85         bool isMultiLingual(BufferParams const &) const;
86
87         ///
88         std::string const asString(Buffer const &,
89                                    OutputParams const & runparams,
90                                    bool label) const;
91         ///
92         std::string const asString(Buffer const &, bool label) const;
93         ///
94         std::string const Paragraph::asString(Buffer const & buffer,
95                                               lyx::pos_type beg,
96                                               lyx::pos_type end,
97                                               bool label) const;
98         ///
99         std::string const asString(Buffer const &,
100                                    OutputParams const & runparams,
101                                    lyx::pos_type beg,
102                                    lyx::pos_type end,
103                                    bool label) const;
104
105         ///
106         void write(Buffer const &, std::ostream &, BufferParams const &,
107                    depth_type & depth) const;
108         ///
109         void validate(LaTeXFeatures &) const;
110
111         ///
112         int startTeXParParams(BufferParams const &, std::ostream &, bool) const;
113
114         ///
115         int endTeXParParams(BufferParams const &, std::ostream &, bool) const;
116
117
118         ///
119         bool simpleTeXOnePar(Buffer const &, BufferParams const &,
120                              LyXFont const & outerfont, std::ostream &,
121                              TexRow & texrow, OutputParams const &);
122
123         ///
124         void simpleLinuxDocOnePar(Buffer const & buf,
125                                   std::ostream & os,
126                                   LyXFont const & outerfont,
127                                   OutputParams const & runparams,
128                                   lyx::depth_type depth) const;
129
130         /// Get the id of the paragraph, usefull for docbook
131         std::string getDocbookId() const;
132
133         ///
134         void simpleDocBookOnePar(Buffer const & buf,
135                                  std::ostream &,
136                                  LyXFont const & outerfont,
137                                  OutputParams const & runparams,
138                                  lyx::depth_type depth) const;
139
140         ///
141         bool hasSameLayout(Paragraph const & par) const;
142
143         ///
144         void makeSameLayout(Paragraph const & par);
145
146         ///
147         void setInsetOwner(UpdatableInset * inset);
148         ///
149         UpdatableInset * inInset() const;
150         ///
151         InsetBase::Code ownerCode() const;
152         ///
153         bool autoBreakRows() const;
154         ///
155         bool forceDefaultParagraphs() const;
156
157         ///
158         lyx::pos_type size() const { return text_.size(); }
159         ///
160         bool empty() const { return text_.empty(); }
161         ///
162         void setContentsFromPar(Paragraph const & par);
163         ///
164         void clearContents();
165
166         ///
167         LyXLayout_ptr const & layout() const;
168         ///
169         void layout(LyXLayout_ptr const & new_layout);
170
171         /// This is the item depth, only used by enumerate and itemize
172         signed char itemdepth;
173
174         ///
175         InsetBibitem * bibitem() const;  // ale970302
176
177         /// initialise tracking for this par
178         void trackChanges(Change::Type = Change::UNCHANGED);
179
180         /// stop tracking
181         void untrackChanges();
182
183         /// set entire paragraph to new text for change tracking
184         void cleanChanges();
185
186         /// look up change type at given pos
187         Change::Type lookupChange(lyx::pos_type pos) const;
188
189         /// look up change at given pos
190         Change const lookupChangeFull(lyx::pos_type pos) const;
191
192         /// is there a change within the given range ?
193         bool isChanged(lyx::pos_type start, lyx::pos_type end) const;
194
195         /// is there a non-addition in this range ?
196         bool isChangeEdited(lyx::pos_type start, lyx::pos_type end) const;
197
198         /// set change at pos
199         void setChange(lyx::pos_type pos, Change::Type type);
200
201         /// accept change
202         void acceptChange(lyx::pos_type start, lyx::pos_type end);
203
204         /// reject change
205         void rejectChange(lyx::pos_type start, lyx::pos_type end);
206
207         /// mark whole par as erased
208         void markErased();
209
210         /// Paragraphs can contain "manual labels", for example, Description
211         /// environment. The text for this user-editable label is stored in
212         /// the paragraph alongside the text of the rest of the paragraph
213         /// (the body). This function returns the starting position of the
214         /// body of the text in the paragraph.
215         lyx::pos_type beginOfBody() const;
216         /// recompute this value
217         void setBeginOfBody();
218
219         ///
220         std::string const & getLabelstring() const;
221
222         /// the next two functions are for the manual labels
223         std::string const getLabelWidthString() const;
224         ///
225         void setLabelWidthString(std::string const & s);
226         ///
227         char getAlign() const;
228         /// The nesting depth of a paragraph
229         depth_type getDepth() const;
230         /// The maximal possible depth of a paragraph after this one
231         depth_type getMaxDepthAfter() const;
232         ///
233         void applyLayout(LyXLayout_ptr const & new_layout);
234
235         /// definite erase
236         void eraseIntern(lyx::pos_type pos);
237         /// erase the char at the given position
238         bool erase(lyx::pos_type pos);
239         /// erase the given range. Returns the number of chars actually erased
240         int erase(lyx::pos_type start, lyx::pos_type end);
241
242         /** Get uninstantiated font setting. Returns the difference
243             between the characters font and the layoutfont.
244             This is what is stored in the fonttable
245         */
246         LyXFont const
247         getFontSettings(BufferParams const &, lyx::pos_type pos) const;
248         ///
249         LyXFont const getFirstFontSettings() const;
250
251         /** Get fully instantiated font. If pos == -1, use the layout
252             font attached to this paragraph.
253             If pos == -2, use the label font of the layout attached here.
254             In all cases, the font is instantiated, i.e. does not have any
255             attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
256             LyXFont::TOGGLE.
257         */
258         LyXFont const getFont(BufferParams const &, lyx::pos_type pos,
259                               LyXFont const & outerfont) const;
260         LyXFont const getLayoutFont(BufferParams const &,
261                                     LyXFont const & outerfont) const;
262         LyXFont const getLabelFont(BufferParams const &,
263                                    LyXFont const & outerfont) const;
264         /**
265          * The font returned by the above functions is the same in a
266          * span of characters. This method will return the last position
267          * in the paragraph for which that font is the same.
268          * This can be used to avoid unnecessary calls to getFont.
269          */
270         lyx::pos_type getEndPosOfFontSpan(lyx::pos_type pos) const;
271         ///
272         value_type getChar(lyx::pos_type pos) const;
273         ///
274         value_type getUChar(BufferParams const &, lyx::pos_type pos) const;
275         /// The position must already exist.
276         void setChar(lyx::pos_type pos, value_type c);
277         /// pos <= size() (there is a dummy font change at the end of each par)
278         void setFont(lyx::pos_type pos, LyXFont const & font);
279         /// Returns the height of the highest font in range
280         LyXFont_size highestFontInRange(lyx::pos_type startpos,
281                                         lyx::pos_type endpos,
282                                         LyXFont_size def_size) const;
283         ///
284         void insert(lyx::pos_type pos, std::string const & str,
285                     LyXFont const & font);
286         ///
287         void insertChar(lyx::pos_type pos, value_type c);
288         ///
289         void insertChar(lyx::pos_type pos, value_type c,
290                 LyXFont const &, Change change = Change(Change::INSERTED));
291         ///
292         bool checkInsertChar(LyXFont &);
293         ///
294         void insertInset(lyx::pos_type pos, InsetBase * inset);
295         ///
296         void insertInset(lyx::pos_type pos, InsetBase * inset,
297                 LyXFont const &, Change change = Change(Change::INSERTED));
298         ///
299         bool insetAllowed(InsetOld_code code);
300         ///
301         InsetBase * getInset(lyx::pos_type pos);
302         ///
303         InsetBase const * getInset(lyx::pos_type pos) const;
304
305         ///
306         bool isHfill(lyx::pos_type pos) const;
307         /// hinted by profiler
308         bool isInset(lyx::pos_type pos) const { return getChar(pos) == META_INSET; }
309         ///
310         bool isNewline(lyx::pos_type pos) const;
311         ///
312         bool isSeparator(lyx::pos_type pos) const;
313         ///
314         bool isLineSeparator(lyx::pos_type pos) const;
315         ///
316         bool isKomma(lyx::pos_type pos) const;
317         /// Used by the spellchecker
318         bool isLetter(lyx::pos_type pos) const;
319         ///
320         bool isWord(lyx::pos_type pos) const;
321
322         /// returns -1 if inset not found
323         int getPositionOfInset(InsetBase const * inset) const;
324
325         ///
326         int stripLeadingSpaces();
327
328         /// return true if we allow multiple spaces
329         bool isFreeSpacing() const;
330
331         /// return true if we allow this par to stay empty
332         bool allowEmpty() const;
333         ////
334         unsigned char transformChar(unsigned char c, lyx::pos_type pos) const;
335         ///
336         ParagraphParameters & params();
337         ///
338         ParagraphParameters const & params() const;
339
340         ///
341         RowList::iterator getRow(lyx::pos_type pos);
342         ///
343         RowList::const_iterator getRow(lyx::pos_type pos) const;
344         ///
345         size_t row(lyx::pos_type pos) const;
346
347         ///
348         InsetList insetlist;
349
350         ///
351         mutable RowList rows;
352         /// last draw y position (baseline of top row)
353         int y;
354         /// total height of paragraph
355         unsigned int height;
356         /// total width of paragraph, may differ from workwidth
357         unsigned int width;
358
359 private:
360         ///
361         LyXLayout_ptr layout_;
362         /// keeping this here instead of in the pimpl makes LyX >10% faster
363         // for average tasks as buffer loading/switching etc.
364         TextContainer text_;
365         /// end of label
366         lyx::pos_type begin_of_body_;
367
368         struct Pimpl;
369         ///
370         friend struct Paragraph::Pimpl;
371         ///
372         Pimpl * pimpl_;
373
374 };
375
376
377 inline bool isInsertedText(Paragraph const & par, lyx::pos_type pos)
378 {
379         return par.lookupChange(pos) == Change::INSERTED;
380 }
381
382
383 inline bool isDeletedText(Paragraph const & par, lyx::pos_type pos)
384 {
385         return par.lookupChange(pos) == Change::DELETED;
386 }
387
388 #endif // PARAGRAPH_H