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