]> git.lyx.org Git - lyx.git/blob - src/Paragraph.h
Fixed some lines that were too long. It compiled afterwards.
[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 "Changes.h"
20 #include "Dimension.h"
21 #include "InsetList.h"
22 #include "lyxlayout_ptr_fwd.h"
23 #include "Row.h"
24
25 #include "insets/Inset.h" // only for Inset::Code
26
27
28 namespace lyx {
29
30
31 class Buffer;
32 class BufferParams;
33 class Counters;
34 class Inset;
35 class InsetBibitem;
36 class LaTeXFeatures;
37 class Inset_code;
38 class Language;
39 class Font;
40 class Font_size;
41 class MetricsInfo;
42 class OutputParams;
43 class PainterInfo;
44 class ParagraphParameters;
45 class TexRow;
46
47
48 class FontSpan {
49 public:
50         /// Invalid font span containing no character
51         FontSpan() : first(0), last(-1) {}
52         /// Span including first and last
53         FontSpan(pos_type f, pos_type l) : first(f), last(l) {}
54
55 public:
56         /// Range including first and last.
57         pos_type first, last;
58 };
59
60
61 /// A Paragraph holds all text, attributes and insets in a text paragraph
62 /// \todo FIXME: any reference to ParagraphMetrics (including inheritance)
63 /// should go in order to complete the Model/View separation of this class.
64 class Paragraph  {
65 public:
66         ///
67         enum {
68                 /// Note that this is 1 right now to avoid
69                 /// crashes where getChar() is called wrongly
70                 /// (returning 0) - if this was 0, then we'd
71                 /// try getInset() and crash. We should fix
72                 /// all these places.
73                 //META_INSET = 1 // as in trunk
74                 META_INSET = 0x200001  // above 0x10ffff, for ucs-4
75         };
76         ///
77         typedef char_type value_type;
78         ///
79         typedef std::vector<value_type> TextContainer;
80
81         ///
82         Paragraph();
83         ///
84         Paragraph(Paragraph const &);
85         ///
86         Paragraph & operator=(Paragraph const &);
87         ///
88         ~Paragraph();
89         ///
90         int id() const;
91
92
93         ///
94         Language const * getParLanguage(BufferParams const &) const;
95         ///
96         bool isRightToLeftPar(BufferParams const &) const;
97         ///
98         void changeLanguage(BufferParams const & bparams,
99                             Language const * from, Language const * to);
100         ///
101         bool isMultiLingual(BufferParams const &) const;
102
103         ///
104         docstring const asString(Buffer const &, bool label) const;
105         ///
106         docstring const asString(Buffer const & buffer,
107                                    pos_type beg,
108                                    pos_type end,
109                                    bool label) const;
110
111         ///
112         void write(Buffer const &, std::ostream &, BufferParams const &,
113                    depth_type & depth) const;
114         ///
115         void validate(LaTeXFeatures &) const;
116
117         ///
118         int startTeXParParams(BufferParams const &, odocstream &, TexRow &,
119                               bool) const;
120
121         ///
122         int endTeXParParams(BufferParams const &, odocstream &, TexRow &,
123                             bool) const;
124
125
126         ///
127         bool simpleTeXOnePar(Buffer const &, BufferParams const &,
128                              Font const & outerfont, odocstream &,
129                              TexRow & texrow, OutputParams const &) const;
130
131         /// Can we drop the standard paragraph wrapper?
132         bool emptyTag() const;
133
134         /// Get the id of the paragraph, usefull for docbook
135         std::string getID(Buffer const & buf,
136                           OutputParams const & runparams) const;
137
138         /// Get the first word of a paragraph, return the position where it left
139         pos_type getFirstWord(Buffer const & buf,
140                                    odocstream & os,
141                                    OutputParams const & runparams) const;
142
143         /// Checks if the paragraph contains only text and no inset or font change.
144         bool onlyText(Buffer const & buf, Font const & outerfont,
145                       pos_type initial) 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 * inset);
162         ///
163         Inset * inInset() const;
164         ///
165         Inset::Code ownerCode() const;
166         ///
167         bool forceDefaultParagraphs() const;
168
169         ///
170         pos_type size() const { return text_.size(); }
171         ///
172         bool empty() const { return text_.empty(); }
173
174         ///
175         Layout_ptr const & layout() const;
176         ///
177         void layout(Layout_ptr const & new_layout);
178
179         /// This is the item depth, only used by enumerate and itemize
180         signed char itemdepth;
181
182         ///
183         InsetBibitem * bibitem() const;  // ale970302
184
185         /// look up change at given pos
186         Change const & lookupChange(pos_type pos) const;
187
188         /// is there a change within the given range ?
189         bool isChanged(pos_type start, pos_type end) const;
190         /// is there an unchanged char at the given pos ?
191         bool isUnchanged(pos_type pos) const {
192                 return lookupChange(pos).type == Change::UNCHANGED;
193         }
194         /// is there an insertion at the given pos ?
195         bool isInserted(pos_type pos) const {
196                 return lookupChange(pos).type == Change::INSERTED;
197         }
198         /// is there a deletion at the given pos ?
199         bool isDeleted(pos_type pos) const {
200                 return lookupChange(pos).type == Change::DELETED;
201         }
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 & getLabelstring() 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_ptr 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_ptr 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         /** Get uninstantiated font setting. Returns the difference
256             between the characters font and the layoutfont.
257             This is what is stored in the fonttable
258         */
259         Font const
260         getFontSettings(BufferParams const &, pos_type pos) const;
261         ///
262         Font const getFirstFontSettings(BufferParams const &) const;
263
264         /** Get fully instantiated font. If pos == -1, use the layout
265             font attached to this paragraph.
266             If pos == -2, use the label font of the layout attached here.
267             In all cases, the font is instantiated, i.e. does not have any
268             attributes with values Font::INHERIT, Font::IGNORE or
269             Font::TOGGLE.
270         */
271         Font const getFont(BufferParams const &, pos_type pos,
272                               Font const & outerfont) const;
273         Font const getLayoutFont(BufferParams const &,
274                                     Font const & outerfont) const;
275         Font const getLabelFont(BufferParams const &,
276                                    Font const & outerfont) const;
277         /**
278          * The font returned by the above functions is the same in a
279          * span of characters. This method will return the first and
280          * the last positions in the paragraph for which that font is
281          * the same. This can be used to avoid unnecessary calls to getFont.
282          */
283         FontSpan fontSpan(pos_type pos) const;
284         ///
285         /// this is a bottleneck.
286         value_type getChar(pos_type pos) const { return text_[pos]; }
287         /// Get the char, but mirror all bracket characters if it is right-to-left
288         value_type getUChar(BufferParams const &, pos_type pos) const;
289         /// pos <= size() (there is a dummy font change at the end of each par)
290         void setFont(pos_type pos, Font const & font);
291         /// Returns the height of the highest font in range
292         Font_size highestFontInRange(pos_type startpos,
293                                         pos_type endpos, Font_size def_size) const;
294         ///
295         void insert(pos_type pos, docstring const & str,
296                     Font const & font, Change const & change);
297         ///
298         void insertChar(pos_type pos, value_type c, bool trackChanges);
299         ///
300         void insertChar(pos_type pos, value_type c,
301                         Font const &, bool trackChanges);
302         ///
303         void insertChar(pos_type pos, value_type c,
304                         Font const &, Change const & change);
305         ///
306         void insertInset(pos_type pos, Inset * inset,
307                          Change const & change);
308         ///
309         void insertInset(pos_type pos, Inset * inset,
310                          Font const &, Change const & change);
311         ///
312         bool insetAllowed(Inset_code code);
313         ///
314         Inset * getInset(pos_type pos) {
315                 return insetlist.get(pos);
316         }
317         ///
318         Inset const * getInset(pos_type pos) const {
319                 return insetlist.get(pos);
320         }
321
322         ///
323         bool isHfill(pos_type pos) const {
324                 return isInset(pos)
325                        && getInset(pos)->lyxCode() == Inset::HFILL_CODE;
326         }
327         /// hinted by profiler
328         bool isInset(pos_type pos) const {
329                 return getChar(pos) == static_cast<value_type>(META_INSET);
330         }
331         ///
332         bool isNewline(pos_type pos) const;
333         /// return true if the char is a word separator
334         bool isSeparator(pos_type pos) const { return getChar(pos) == ' '; }
335         ///
336         bool isLineSeparator(pos_type pos) const;
337         /// True if the character/inset at this point can be part of a word.
338         /// Note that digits in particular are considered as letters
339         bool isLetter(pos_type pos) const;
340
341         /// returns -1 if inset not found
342         int getPositionOfInset(Inset const * inset) 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         bool hfillExpansion(Row const & row, pos_type pos) const;
361
362         /// Check if we are in a Biblio environment and insert or
363         /// delete InsetBibitems as necessary.
364         /// \retval int 1, if we had to add an inset, in which case
365         /// the cursor will need to move cursor forward; -pos, if we deleted
366         /// an inset, in which case pos is the position from which the inset
367         /// was deleted, and the cursor will need to be moved back one if it
368         /// was previously past that position. Return 0 otherwise.
369         int checkBiblio(bool track_changes);
370
371         /// For each author, set 'used' to true if there is a change
372         /// by this author in the paragraph.
373         void checkAuthors(AuthorList const & authorList);
374
375 public:
376         ///
377         InsetList insetlist;
378
379 private:
380
381         ///
382         Layout_ptr layout_;
383         /**
384          * Keeping this here instead of in the pimpl makes LyX >10% faster
385          * for average tasks as buffer loading/switching etc.
386          */
387         TextContainer text_;
388         /// end of label
389         pos_type begin_of_body_;
390
391         /// Pimpl away stuff
392         class Pimpl;
393         ///
394         friend class Paragraph::Pimpl;
395         ///
396         Pimpl * pimpl_;
397 };
398
399 } // namespace lyx
400
401 #endif // PARAGRAPH_H