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