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