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