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