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