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