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