]> git.lyx.org Git - lyx.git/blob - src/Paragraph.h
cosmetics
[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 "LayoutPtr.h"
20
21 #include "insets/InsetCode.h"
22
23 #include "support/docstring.h"
24 // FIXME: would be nice to forward declare odocstream instead of
25 // including this:
26 #include "support/docstream.h"
27
28 #include <vector>
29
30 namespace lyx {
31
32 class AuthorList;
33 class Buffer;
34 class BufferParams;
35 class Change;
36 class Counters;
37 class Inset;
38 class InsetBibitem;
39 class LaTeXFeatures;
40 class Inset_code;
41 class InsetList;
42 class Language;
43 class Font;
44 class Font_size;
45 class MetricsInfo;
46 class OutputParams;
47 class PainterInfo;
48 class ParagraphParameters;
49 class TexRow;
50
51
52 class FontSpan {
53 public:
54         /// Invalid font span containing no character
55         FontSpan() : first(0), last(-1) {}
56         /// Span including first and last
57         FontSpan(pos_type f, pos_type l) : first(f), last(l) {}
58
59 public:
60         /// Range including first and last.
61         pos_type first, last;
62 };
63
64
65 /// A Paragraph holds all text, attributes and insets in a text paragraph
66 /// \todo FIXME: any reference to ParagraphMetrics (including inheritance)
67 /// should go in order to complete the Model/View separation of this class.
68 class Paragraph  {
69 public:
70         ///
71         enum {
72                 /// Note that this is 1 right now to avoid
73                 /// crashes where getChar() is called wrongly
74                 /// (returning 0) - if this was 0, then we'd
75                 /// try getInset() and crash. We should fix
76                 /// all these places.
77                 //META_INSET = 1 // as in trunk
78                 META_INSET = 0x200001  // above 0x10ffff, for ucs-4
79         };
80         ///
81         typedef char_type value_type;
82
83         ///
84         Paragraph();
85         ///
86         Paragraph(Paragraph const &);
87         ///
88         Paragraph & operator=(Paragraph const &);
89         ///
90         ~Paragraph();
91         ///
92         int id() const;
93
94         ///
95         Language const * getParLanguage(BufferParams const &) const;
96         ///
97         bool isRTL(BufferParams const &) const;
98         ///
99         void changeLanguage(BufferParams const & bparams,
100                             Language const * from, Language const * to);
101         ///
102         bool isMultiLingual(BufferParams const &) const;
103
104         ///
105         docstring const asString(Buffer const &, bool label) const;
106         ///
107         docstring const asString(Buffer const & buffer,
108                                    pos_type beg,
109                                    pos_type end,
110                                    bool label) const;
111
112         ///
113         void write(Buffer const &, std::ostream &, BufferParams const &,
114                    depth_type & depth) const;
115         ///
116         void validate(LaTeXFeatures &) const;
117
118         ///
119         bool latex(Buffer const &, BufferParams const &,
120                              Font const & outerfont, odocstream &,
121                              TexRow & texrow, OutputParams const &) const;
122
123         /// Can we drop the standard paragraph wrapper?
124         bool emptyTag() const;
125
126         /// Get the id of the paragraph, usefull for docbook
127         std::string getID(Buffer const & buf,
128                           OutputParams const & runparams) const;
129
130         /// Get the first word of a paragraph, return the position where it left
131         pos_type getFirstWord(Buffer const & buf,
132                                    odocstream & os,
133                                    OutputParams const & runparams) const;
134
135         /// Checks if the paragraph contains only text and no inset or font change.
136         bool onlyText(Buffer const & buf, Font const & outerfont,
137                       pos_type initial) const;
138
139         /// Writes to stream the docbook representation
140         void simpleDocBookOnePar(Buffer const & buf,
141                                  odocstream &,
142                                  OutputParams const & runparams,
143                                  Font const & outerfont,
144                                  pos_type initial = 0) const;
145
146         ///
147         bool hasSameLayout(Paragraph const & par) const;
148
149         ///
150         void makeSameLayout(Paragraph const & par);
151
152         ///
153         void setInsetOwner(Inset * inset);
154         ///
155         Inset * inInset() const;
156         ///
157         InsetCode ownerCode() const;
158         ///
159         bool forceDefaultParagraphs() const;
160
161         ///
162         pos_type size() const { return text_.size(); }
163         ///
164         bool empty() const { return text_.empty(); }
165
166         ///
167         LayoutPtr const & layout() const;
168         ///
169         void layout(LayoutPtr 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         /// look up change at given pos
178         Change const & lookupChange(pos_type pos) const;
179
180         /// is there a change within the given range ?
181         bool isChanged(pos_type start, pos_type end) const;
182         /// is there an unchanged char at the given pos ?
183         bool isUnchanged(pos_type pos) const;
184         /// is there an insertion at the given pos ?
185         bool isInserted(pos_type pos) const;
186         /// is there a deletion at the given pos ?
187         bool isDeleted(pos_type pos) const;
188
189         /// will the paragraph be physically merged with the next
190         /// one if the imaginary end-of-par character is logically deleted?
191         bool isMergedOnEndOfParDeletion(bool trackChanges) const;
192
193         /// set change for the entire par
194         void setChange(Change const & change);
195
196         /// set change at given pos
197         void setChange(pos_type pos, Change const & change);
198
199         /// accept changes within the given range
200         void acceptChanges(BufferParams const & bparams, pos_type start, pos_type end);
201
202         /// reject changes within the given range
203         void rejectChanges(BufferParams const & bparams, pos_type start, pos_type end);
204
205         /// Paragraphs can contain "manual labels", for example, Description
206         /// environment. The text for this user-editable label is stored in
207         /// the paragraph alongside the text of the rest of the paragraph
208         /// (the body). This function returns the starting position of the
209         /// body of the text in the paragraph.
210         pos_type beginOfBody() const;
211         /// recompute this value
212         void setBeginOfBody();
213
214         ///
215         docstring const & getLabelstring() const;
216
217         /// the next two functions are for the manual labels
218         docstring const getLabelWidthString() const;
219         /// Set label width string.
220         void setLabelWidthString(docstring const & s);
221         /// translate \p label to the paragraph language if possible.
222         docstring const translateIfPossible(docstring const & label,
223                 BufferParams const & bparams) const;
224         /// Expand the counters for the labelstring of \c layout
225         docstring expandLabel(LayoutPtr const &, BufferParams const &,
226                 bool process_appendix = true) const;
227         /// Actual paragraph alignment used
228         char getAlign() const;
229         /// The nesting depth of a paragraph
230         depth_type getDepth() const;
231         /// The maximal possible depth of a paragraph after this one
232         depth_type getMaxDepthAfter() const;
233         ///
234         void applyLayout(LayoutPtr const & new_layout);
235
236         /// (logically) erase the char at pos; return true if it was actually erased
237         bool eraseChar(pos_type pos, bool trackChanges);
238         /// (logically) erase the given range; return the number of chars actually erased
239         int eraseChars(pos_type start, pos_type end, bool trackChanges);
240
241         /** Get uninstantiated font setting. Returns the difference
242             between the characters font and the layoutfont.
243             This is what is stored in the fonttable
244         */
245         Font const
246         getFontSettings(BufferParams const &, pos_type pos) const;
247         ///
248         Font const getFirstFontSettings(BufferParams const &) const;
249
250         /** Get fully instantiated font. If pos == -1, use the layout
251             font attached to this paragraph.
252             If pos == -2, use the label font of the layout attached here.
253             In all cases, the font is instantiated, i.e. does not have any
254             attributes with values Font::INHERIT, Font::IGNORE or
255             Font::TOGGLE.
256         */
257         Font const getFont(BufferParams const &, pos_type pos,
258                               Font const & outerfont) const;
259         Font const getLayoutFont(BufferParams const &,
260                                     Font const & outerfont) const;
261         Font const getLabelFont(BufferParams const &,
262                                    Font const & outerfont) const;
263         /**
264          * The font returned by the above functions is the same in a
265          * span of characters. This method will return the first and
266          * the last positions in the paragraph for which that font is
267          * the same. This can be used to avoid unnecessary calls to getFont.
268          */
269         FontSpan fontSpan(pos_type pos) const;
270         ///
271         /// this is a bottleneck.
272         value_type getChar(pos_type pos) const { return text_[pos]; }
273         /// Get the char, but mirror all bracket characters if it is right-to-left
274         value_type getUChar(BufferParams const &, pos_type pos) const;
275         /// pos <= size() (there is a dummy font change at the end of each par)
276         void setFont(pos_type pos, Font const & font);
277         /// Returns the height of the highest font in range
278         Font_size highestFontInRange(pos_type startpos,
279                                         pos_type endpos, Font_size def_size) const;
280         ///
281         void insert(pos_type pos, docstring const & str,
282                     Font const & font, Change const & change);
283
284         ///
285         void appendString(docstring const & s, Font const & font,
286                 Change const & change);
287         ///
288         void appendChar(value_type c, Font const & font, Change const & change);
289         ///
290         void insertChar(pos_type pos, value_type c, bool trackChanges);
291         ///
292         void insertChar(pos_type pos, value_type c,
293                         Font const &, bool trackChanges);
294         ///
295         void insertChar(pos_type pos, value_type c,
296                         Font const &, Change const & change);
297         ///
298         void insertInset(pos_type pos, Inset * inset,
299                          Change const & change);
300         ///
301         void insertInset(pos_type pos, Inset * inset,
302                          Font const &, Change const & change);
303         ///
304         bool insetAllowed(InsetCode code);
305         ///
306         Inset * getInset(pos_type pos);
307         ///
308         Inset const * getInset(pos_type pos) const;
309
310         /// Release inset at given position.
311         /// \warning does not honour change tracking!
312         /// Therefore, it should only be used for breaking and merging
313         /// paragraphs
314         Inset * releaseInset(pos_type pos);
315
316         ///
317         InsetList const & insetList() const;
318
319         ///
320         bool isHfill(pos_type pos) const;
321
322         /// hinted by profiler
323         bool isInset(pos_type pos) const {
324                 return getChar(pos) == static_cast<value_type>(META_INSET);
325         }
326         ///
327         bool isNewline(pos_type pos) const;
328         /// return true if the char is a word separator
329         bool isSeparator(pos_type pos) const { return getChar(pos) == ' '; }
330         ///
331         bool isLineSeparator(pos_type pos) const;
332         /// True if the character/inset at this point can be part of a word.
333         /// Note that digits in particular are considered as letters
334         bool isLetter(pos_type pos) const;
335
336         /// returns true if at least one line break or line separator has been deleted
337         /// at the beginning of the paragraph (either physically or logically)
338         bool stripLeadingSpaces(bool trackChanges);
339
340         /// return true if we allow multiple spaces
341         bool isFreeSpacing() const;
342
343         /// return true if we allow this par to stay empty
344         bool allowEmpty() const;
345         ///
346         char_type transformChar(char_type c, pos_type pos) const;
347         ///
348         ParagraphParameters & params();
349         ///
350         ParagraphParameters const & params() const;
351
352         /// Check if we are in a Biblio environment and insert or
353         /// delete InsetBibitems as necessary.
354         /// \retval int 1, if we had to add an inset, in which case
355         /// the cursor will need to move cursor forward; -pos, if we deleted
356         /// an inset, in which case pos is the position from which the inset
357         /// was deleted, and the cursor will need to be moved back one if it
358         /// was previously past that position. Return 0 otherwise.
359         int checkBiblio(bool track_changes);
360
361         /// For each author, set 'used' to true if there is a change
362         /// by this author in the paragraph.
363         void checkAuthors(AuthorList const & authorList);
364
365         /// return the number of InsetOptArg in a paragraph
366         int numberOfOptArgs() const;
367
368 private:
369         /**
370          * Keeping this here instead of in the pimpl makes LyX >10% faster
371          * for average tasks as buffer loading/switching etc.
372          */
373         typedef std::vector<value_type> TextContainer;
374         ///
375         TextContainer text_;
376
377         /// Pimpl away stuff
378         class Private;
379         ///
380         friend class Paragraph::Private;
381         ///
382         Private * d;
383 };
384
385 } // namespace lyx
386
387 #endif // PARAGRAPH_H