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