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