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