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