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