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