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