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