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