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