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