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