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