]> git.lyx.org Git - lyx.git/blob - src/paragraph.h
First step towards model/view separation of Paragraph class.
[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 "RowList_fwd.h"
24
25 #include "insets/insetbase.h" // only for InsetBase::Code
26
27
28 namespace lyx {
29
30
31 class Buffer;
32 class BufferParams;
33 class BufferView;
34 class Counters;
35 class InsetBase;
36 class InsetBibitem;
37 class LaTeXFeatures;
38 class InsetBase_code;
39 class Language;
40 class LyXFont;
41 class LyXFont_size;
42 class MetricsInfo;
43 class OutputParams;
44 class PainterInfo;
45 class ParagraphParameters;
46 class TexRow;
47
48
49 class FontSpan {
50 public:
51         /// Invalid font span containing no character
52         FontSpan() : first(0), last(-1) {}
53         /// Span including first and last
54         FontSpan(pos_type f, pos_type l) : first(f), last(l) {}
55
56 public:
57         /// Range including first and last.
58         pos_type first, last;
59 };
60
61 /// Helper class for Paragraph Metrics.
62 /// \todo FIXME: this class deserves its own .[Ch] files.
63 /// Then, the storage of such object should be done in \c BufferView 
64 /// (most probably in the \c CoordCache class along \c Point objects).
65 class ParagraphMetrics  {
66 public:
67         ParagraphMetrics();
68         ParagraphMetrics(ParagraphMetrics const & pm);
69         ParagraphMetrics & operator=(ParagraphMetrics const & pm);
70         ///
71         Row & getRow(pos_type pos, bool boundary);
72         ///
73         Row const & getRow(pos_type pos, bool boundary) const;
74         ///
75         size_t pos2row(pos_type pos) const;
76
77         /// LyXText::redoParagraph updates this
78         Dimension & dim() { return dim_; }
79         /// total height of paragraph
80         unsigned int height() const { return dim_.height(); }
81         /// total width of paragraph, may differ from workwidth
82         unsigned int width() const { return dim_.width(); }
83         /// ascend of paragraph above baseline
84         unsigned int ascent() const { return dim_.ascent(); }
85         /// descend of paragraph below baseline
86         unsigned int descent() const { return dim_.descent(); }
87         /// LyXText updates the rows using this access point
88         RowList & rows() { return rows_; }
89         /// The painter and others use this
90         RowList const & rows() const { return rows_; }
91         ///
92         RowSignature & rowSignature() const { return rowSignature_; }
93
94         /// dump some information to lyxerr
95         void dump() const;
96
97 private:
98         ///
99         mutable RowList rows_;
100         ///
101         mutable RowSignature rowSignature_;
102         /// cached dimensions of paragraph
103         Dimension dim_;
104 };
105
106
107 /// A Paragraph holds all text, attributes and insets in a text paragraph
108 /// \todo FIXME: any reference to ParagraphMetrics (including inheritance)
109 /// should go in order to complete the Model/View separation of this class.
110 class Paragraph: public ParagraphMetrics  {
111 public:
112         ///
113         enum {
114                 /// Note that this is 1 right now to avoid
115                 /// crashes where getChar() is called wrongly
116                 /// (returning 0) - if this was 0, then we'd
117                 /// try getInset() and crash. We should fix
118                 /// all these places.
119                 //META_INSET = 1 // as in trunk
120                 META_INSET = 0x200001  // above 0x10ffff, for ucs-4
121         };
122         ///
123         typedef char_type value_type;
124         ///
125         typedef std::vector<value_type> TextContainer;
126
127         ///
128         Paragraph();
129         ///
130         Paragraph(Paragraph const &);
131         ///
132         Paragraph & operator=(Paragraph const &);
133         ///
134         ~Paragraph();
135         ///
136         int id() const;
137
138
139         ///
140         Language const * getParLanguage(BufferParams const &) const;
141         ///
142         bool isRightToLeftPar(BufferParams const &) const;
143         ///
144         void changeLanguage(BufferParams const & bparams,
145                             Language const * from, Language const * to);
146         ///
147         bool isMultiLingual(BufferParams const &) const;
148
149         ///
150         docstring const asString(Buffer const &,
151                                    OutputParams const & runparams,
152                                    bool label) const;
153         ///
154         docstring const asString(Buffer const &, bool label) const;
155         ///
156         docstring const asString(Buffer const & buffer,
157                                    pos_type beg,
158                                    pos_type end,
159                                    bool label) const;
160         ///
161         docstring const asString(Buffer const &,
162                                    OutputParams const & runparams,
163                                    pos_type beg,
164                                    pos_type end,
165                                    bool label) const;
166
167         ///
168         void write(Buffer const &, std::ostream &, BufferParams const &,
169                    depth_type & depth) const;
170         ///
171         void validate(LaTeXFeatures &) const;
172
173         ///
174         int startTeXParParams(BufferParams const &, odocstream &, bool) const;
175
176         ///
177         int endTeXParParams(BufferParams const &, odocstream &, bool) const;
178
179
180         ///
181         bool simpleTeXOnePar(Buffer const &, BufferParams const &,
182                              LyXFont const & outerfont, odocstream &,
183                              TexRow & texrow, OutputParams const &) const;
184
185         /// Can we drop the standard paragraph wrapper?
186         bool emptyTag() const;
187
188         /// Get the id of the paragraph, usefull for docbook
189         std::string getID(Buffer const & buf,
190                           OutputParams const & runparams) const;
191
192         // Get the first word of a paragraph, return the position where it left
193         pos_type getFirstWord(Buffer const & buf,
194                                    odocstream & os,
195                                    OutputParams const & runparams) const;
196
197         /// Checks if the paragraph contains only text and no inset or font change.
198         bool onlyText(Buffer const & buf, LyXFont const & outerfont,
199                       pos_type initial) const;
200
201         /// Writes to stream the docbook representation
202         void simpleDocBookOnePar(Buffer const & buf,
203                                  odocstream &,
204                                  OutputParams const & runparams,
205                                  LyXFont const & outerfont,
206                                  pos_type initial = 0) const;
207
208         ///
209         bool hasSameLayout(Paragraph const & par) const;
210
211         ///
212         void makeSameLayout(Paragraph const & par);
213
214         ///
215         void setInsetOwner(InsetBase * inset);
216         ///
217         InsetBase * inInset() const;
218         ///
219         InsetBase::Code ownerCode() const;
220         ///
221         bool forceDefaultParagraphs() const;
222
223         ///
224         pos_type size() const { return text_.size(); }
225         ///
226         bool empty() const { return text_.empty(); }
227         ///
228         void clearContents();
229
230         ///
231         LyXLayout_ptr const & layout() const;
232         ///
233         void layout(LyXLayout_ptr const & new_layout);
234
235         /// This is the item depth, only used by enumerate and itemize
236         signed char itemdepth;
237
238         ///
239         InsetBibitem * bibitem() const;  // ale970302
240
241         /// look up change at given pos
242         Change const lookupChange(pos_type pos) const;
243
244         /// is there a change within the given range ?
245         bool isChanged(pos_type start, pos_type end) const;
246         /// is there an unchanged char at the given pos ?
247         bool isUnchanged(pos_type pos) const {
248                 return lookupChange(pos).type == Change::UNCHANGED;
249         }
250         /// is there an insertion at the given pos ?
251         bool isInserted(pos_type pos) const {
252                 return lookupChange(pos).type == Change::INSERTED;
253         }
254         /// is there a deletion at the given pos ?
255         bool isDeleted(pos_type pos) const {
256                 return lookupChange(pos).type == Change::DELETED;
257         }
258
259         /// will the paragraph be physically merged with the next
260         /// one if the imaginary end-of-par character is logically deleted?
261         bool isMergedOnEndOfParDeletion(bool trackChanges) const;
262
263         /// set change for the entire par
264         void setChange(Change const & change);
265
266         /// set change at given pos
267         void setChange(pos_type pos, Change const & change);
268
269         /// accept changes within the given range
270         void acceptChanges(pos_type start, pos_type end);
271
272         /// reject changes within the given range
273         void rejectChanges(pos_type start, pos_type end);
274
275         /// Paragraphs can contain "manual labels", for example, Description
276         /// environment. The text for this user-editable label is stored in
277         /// the paragraph alongside the text of the rest of the paragraph
278         /// (the body). This function returns the starting position of the
279         /// body of the text in the paragraph.
280         pos_type beginOfBody() const;
281         /// recompute this value
282         void setBeginOfBody();
283
284         ///
285         docstring const & getLabelstring() const;
286
287         /// the next two functions are for the manual labels
288         docstring const getLabelWidthString() const;
289         ///
290         void setLabelWidthString(docstring const & s);
291         /// Actual paragraph alignment used
292         char getAlign() const;
293         /// The nesting depth of a paragraph
294         depth_type getDepth() const;
295         /// The maximal possible depth of a paragraph after this one
296         depth_type getMaxDepthAfter() const;
297         ///
298         void applyLayout(LyXLayout_ptr const & new_layout);
299
300         /// (logically) erase the char at pos; return true if it was actually erased
301         bool eraseChar(pos_type pos, bool trackChanges);
302         /// (logically) erase the given range; return the number of chars actually erased
303         int eraseChars(pos_type start, pos_type end, bool trackChanges);
304
305         /** Get uninstantiated font setting. Returns the difference
306             between the characters font and the layoutfont.
307             This is what is stored in the fonttable
308         */
309         LyXFont const
310         getFontSettings(BufferParams const &, pos_type pos) const;
311         ///
312         LyXFont const getFirstFontSettings(BufferParams const &) const;
313
314         /** Get fully instantiated font. If pos == -1, use the layout
315             font attached to this paragraph.
316             If pos == -2, use the label font of the layout attached here.
317             In all cases, the font is instantiated, i.e. does not have any
318             attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
319             LyXFont::TOGGLE.
320         */
321         LyXFont const getFont(BufferParams const &, pos_type pos,
322                               LyXFont const & outerfont) const;
323         LyXFont const getLayoutFont(BufferParams const &,
324                                     LyXFont const & outerfont) const;
325         LyXFont const getLabelFont(BufferParams const &,
326                                    LyXFont const & outerfont) const;
327         /**
328          * The font returned by the above functions is the same in a
329          * span of characters. This method will return the first and
330          * the last positions in the paragraph for which that font is
331          * the same. This can be used to avoid unnecessary calls to
332    * getFont.
333          */
334         FontSpan fontSpan(pos_type pos) const;
335         ///
336         /// this is a bottleneck.
337         value_type getChar(pos_type pos) const { return text_[pos]; }
338         /// Get the char, but mirror all bracket characters if it is right-to-left
339         value_type getUChar(BufferParams const &, pos_type pos) const;
340         /// pos <= size() (there is a dummy font change at the end of each par)
341         void setFont(pos_type pos, LyXFont const & font);
342         /// Returns the height of the highest font in range
343         LyXFont_size highestFontInRange(pos_type startpos,
344                                         pos_type endpos, LyXFont_size def_size) const;
345         ///
346         void insert(pos_type pos, docstring const & str,
347                     LyXFont const & font, Change const & change);
348         ///
349         void insertChar(pos_type pos, value_type c, bool trackChanges);
350         ///
351         void insertChar(pos_type pos, value_type c,
352                         LyXFont const &, bool trackChanges);
353         ///
354         void insertChar(pos_type pos, value_type c,
355                         LyXFont const &, Change const & change);
356         ///
357         void insertInset(pos_type pos, InsetBase * inset,
358                          Change const & change);
359         ///
360         void insertInset(pos_type pos, InsetBase * inset,
361                          LyXFont const &, Change const & change);
362         ///
363         bool insetAllowed(InsetBase_code code);
364         ///
365         InsetBase * getInset(pos_type pos) {
366                 return insetlist.get(pos);
367         }
368         ///
369         InsetBase const * getInset(pos_type pos) const {
370                 return insetlist.get(pos);
371         }
372
373         ///
374         bool isHfill(pos_type pos) const {
375                 return isInset(pos)
376                        && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
377         }
378         /// hinted by profiler
379         bool isInset(pos_type pos) const {
380                 return getChar(pos) == static_cast<value_type>(META_INSET);
381         }
382         ///
383         bool isNewline(pos_type pos) const;
384         /// return true if the char is a word separator
385         bool isSeparator(pos_type pos) const { return getChar(pos) == ' '; }
386         ///
387         bool isLineSeparator(pos_type pos) const;
388         /// True if the character/inset at this point can be part of a word
389         // Note that digits in particular are considered as letters
390         bool isLetter(pos_type pos) const;
391
392         /// returns -1 if inset not found
393         int getPositionOfInset(InsetBase const * inset) const;
394
395         /// Returns the number of line breaks and white-space stripped at the start
396         int stripLeadingSpaces();
397
398         /// return true if we allow multiple spaces
399         bool isFreeSpacing() const;
400
401         /// return true if we allow this par to stay empty
402         bool allowEmpty() const;
403         ///
404         char_type transformChar(char_type c, pos_type pos) const;
405         ///
406         ParagraphParameters & params();
407         ///
408         ParagraphParameters const & params() const;
409         ///
410         bool hfillExpansion(Row const & row, pos_type pos) const;
411
412 public:
413         ///
414         InsetList insetlist;
415
416 private:
417
418         ///
419         LyXLayout_ptr layout_;
420         /**
421          * Keeping this here instead of in the pimpl makes LyX >10% faster
422          * for average tasks as buffer loading/switching etc.
423          */
424         TextContainer text_;
425         /// end of label
426         pos_type begin_of_body_;
427
428         /// Pimpl away stuff
429         class Pimpl;
430         ///
431         friend class Paragraph::Pimpl;
432         ///
433         Pimpl * pimpl_;
434 };
435
436 } // namespace lyx
437
438 #endif // PARAGRAPH_H