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