]> git.lyx.org Git - lyx.git/blob - src/paragraph.h
52621056824fb03c45535e6a9c7a86975edef1ad
[lyx.git] / src / paragraph.h
1 // -*- C++ -*-
2 /**
3  *  \file paragraph.h
4  *  Copyright 1995 Matthias Ettrich
5  *  Copyright 2002 the LyX Team
6  *  Read the file COPYING
7  */
8
9 #ifndef PARAGRAPH_H
10 #define PARAGRAPH_H
11
12 #include "lyxlayout_ptr_fwd.h"
13 #include "lyxfont.h" // Just for LyXFont::FONT_SIZE
14 #include "InsetList.h"
15
16 #include "insets/inset.h" // Just for Inset::Code
17
18 #include "support/types.h"
19 #include "changes.h"
20
21 #include "LString.h"
22
23 #include <boost/optional.hpp>
24
25 class Buffer;
26 class BufferParams;
27 class BufferView;
28 class Counters;
29 class InsetBibitem;
30 class Language;
31 class LaTeXFeatures;
32 class LatexRunParams;
33 class ParagraphParameters;
34 class TexRow;
35 class ParagraphList;
36
37 // Define this if you want to try out the new storage container for
38 // paragraphs. (Lgb)
39 // This is non working and far from finished.
40 //#define NO_STD_LIST 1
41
42 /// A Paragraph holds all text, attributes and insets in a text paragraph
43 class Paragraph  {
44 public:
45 #ifdef NO_STD_LIST
46         // Remove this whan ParagraphList transition is over. (Lgb)
47         friend class ParagraphList;
48         friend class ParagraphList::iterator;
49         friend class ParagraphList::const_iterator;
50 #endif
51         ///
52         enum META_KIND {
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         /// The same as ParameterStruct::depth_type
63         typedef unsigned int depth_type;
64
65         ///
66         Paragraph();
67
68         ///
69         Paragraph(Paragraph const &);
70         ///
71         //void operator=(Paragraph const &);
72         /// the destructor removes the new paragraph from the list
73         ~Paragraph();
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         string const asString(Buffer const *, bool label) const;
87         ///
88         string const asString(Buffer const *, lyx::pos_type beg, lyx::pos_type end,
89                               bool label) const;
90
91         ///
92         void write(Buffer const *, std::ostream &, BufferParams const &,
93                        depth_type & depth) const;
94         ///
95         void validate(LaTeXFeatures &) const;
96
97         /// return the unique ID of this paragraph
98         int id() const;
99         /// Set the Id of this paragraph.
100         void id(int);
101
102         ///
103         int startTeXParParams(BufferParams const &, std::ostream &, bool) const;
104
105         ///
106         int endTeXParParams(BufferParams const &, std::ostream &, bool) const;
107
108
109         ///
110         bool simpleTeXOnePar(Buffer const *, BufferParams const &,
111                              LyXFont const & outerfont, std::ostream &,
112                              TexRow & texrow, LatexRunParams const &);
113
114         ///
115         bool hasSameLayout(Paragraph const & par) const;
116
117         ///
118         void makeSameLayout(Paragraph const & par);
119
120         ///
121         Inset * inInset() const;
122         ///
123         void setInsetOwner(Inset * i);
124         ///
125         void deleteInsetsLyXText(BufferView *);
126         ///
127         void resizeInsetsLyXText(BufferView *);
128
129         ///
130         lyx::pos_type size() const;
131         ///
132         bool empty() const;
133         ///
134         void setContentsFromPar(Paragraph const & par);
135         ///
136         void clearContents();
137
138         ///
139         LyXLayout_ptr const & layout() const;
140         ///
141         void layout(LyXLayout_ptr const & new_layout);
142
143         ///
144         char enumdepth;
145
146         ///
147         char itemdepth;
148
149         ///
150         InsetBibitem * bibitem() const;  // ale970302
151
152         /// initialise tracking for this par
153         void trackChanges(Change::Type = Change::UNCHANGED);
154
155         /// stop tracking
156         void untrackChanges();
157
158         /// set entire paragraph to new text for change tracking
159         void cleanChanges();
160
161         /// look up change type at given pos
162         Change::Type lookupChange(lyx::pos_type pos) const;
163
164         /// look up change at given pos
165         Change const lookupChangeFull(lyx::pos_type pos) const;
166
167         /// is there a change within the given range ?
168         bool isChanged(lyx::pos_type start, lyx::pos_type end) const;
169
170         /// is there a non-addition in this range ?
171         bool isChangeEdited(lyx::pos_type start, lyx::pos_type end) const;
172
173         /// set change at pos
174         void setChange(lyx::pos_type pos, Change::Type type);
175
176         /// accept change
177         void acceptChange(lyx::pos_type start, lyx::pos_type end);
178
179         /// reject change
180         void rejectChange(lyx::pos_type start, lyx::pos_type end);
181
182         /// mark whole par as erased
183         void markErased();
184
185         /// Paragraphs can contain "manual labels", for example, Description environment.
186         /// The text for this user-editable label is stored in the paragraph alongside
187         /// the text of the rest of the paragraph (the body). This function returns
188         /// the starting position of the body of the text in the paragraph.
189         int beginningOfBody() const;
190
191         ///
192         string const & getLabelstring() const;
193
194         /// the next two functions are for the manual labels
195         string const getLabelWidthString() const;
196         ///
197         void setLabelWidthString(string const & s);
198         ///
199         char getAlign() const;
200         /// The nesting depth of a paragraph
201         depth_type getDepth() const;
202         /// The maximal possible depth of a paragraph after this one
203         depth_type getMaxDepthAfter() const;
204         ///
205         void applyLayout(LyXLayout_ptr const & new_layout);
206
207         /// definite erase
208         void eraseIntern(lyx::pos_type pos);
209         /// erase the char at the given position
210         bool erase(lyx::pos_type pos);
211         /// erase the given range. Returns the number of chars actually erased
212         int erase(lyx::pos_type start, lyx::pos_type end);
213
214         /** Get uninstantiated font setting. Returns the difference
215             between the characters font and the layoutfont.
216             This is what is stored in the fonttable
217         */
218         LyXFont const
219         getFontSettings(BufferParams const &, lyx::pos_type pos) const;
220         ///
221         LyXFont const getFirstFontSettings() const;
222
223         /** Get fully instantiated font. If pos == -1, use the layout
224             font attached to this paragraph.
225             If pos == -2, use the label font of the layout attached here.
226             In all cases, the font is instantiated, i.e. does not have any
227             attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
228             LyXFont::TOGGLE.
229         */
230         LyXFont const getFont(BufferParams const &, lyx::pos_type pos,
231                               LyXFont const & outerfont) const;
232         LyXFont const getLayoutFont(BufferParams const &,
233                                     LyXFont const & outerfont) const;
234         LyXFont const getLabelFont(BufferParams const &,
235                                    LyXFont const & outerfont) const;
236         ///
237         value_type getChar(lyx::pos_type pos) const;
238         ///
239         value_type getUChar(BufferParams const &, lyx::pos_type pos) const;
240         /// The position must already exist.
241         void setChar(lyx::pos_type pos, value_type c);
242         /// pos <= size() (there is a dummy font change at the end of each par)
243         void setFont(lyx::pos_type pos, LyXFont const & font);
244         /// Returns the height of the highest font in range
245         LyXFont::FONT_SIZE
246         highestFontInRange(lyx::pos_type startpos,
247                            lyx::pos_type endpos,
248                            LyXFont::FONT_SIZE const def_size) const;
249         ///
250         void insertChar(lyx::pos_type pos, value_type c);
251         ///
252         void insertChar(lyx::pos_type pos, value_type c, LyXFont const &, Change change = Change(Change::INSERTED));
253         ///
254         bool checkInsertChar(LyXFont &);
255         ///
256         void insertInset(lyx::pos_type pos, Inset * inset);
257         ///
258         void insertInset(lyx::pos_type pos, Inset * inset, LyXFont const &, Change change = Change(Change::INSERTED));
259         ///
260         bool insetAllowed(Inset::Code code);
261         ///
262         Inset * getInset(lyx::pos_type pos);
263         ///
264         Inset const * getInset(lyx::pos_type pos) const;
265         /** important for cut and paste
266             Temporary change from BufferParams to Buffer. Will revert when we
267             get rid of the argument to Inset::clone(Buffer const &) */
268         void copyIntoMinibuffer(Buffer const &, lyx::pos_type pos) const;
269         ///
270         void cutIntoMinibuffer(BufferParams const &, lyx::pos_type pos);
271         ///
272         bool insertFromMinibuffer(lyx::pos_type pos);
273
274         ///
275         bool isHfill(lyx::pos_type pos) const;
276         ///
277         bool isInset(lyx::pos_type pos) const;
278         ///
279         bool isNewline(lyx::pos_type pos) const;
280         ///
281         bool isSeparator(lyx::pos_type pos) const;
282         ///
283         bool isLineSeparator(lyx::pos_type pos) const;
284         ///
285         bool isKomma(lyx::pos_type pos) const;
286         /// Used by the spellchecker
287         bool isLetter(lyx::pos_type pos) const;
288         ///
289         bool isWord(lyx::pos_type pos) const;
290
291         /// returns -1 if inset not found
292         int getPositionOfInset(Inset const * inset) const;
293
294         ///
295         int stripLeadingSpaces();
296
297         ///
298         bool isFreeSpacing() const;
299
300         ///
301         ParagraphParameters & params();
302         ///
303         ParagraphParameters const & params() const;
304         ///
305         InsetList insetlist;
306         ///
307         //Counters & counters();
308         ///
309         void owningBuffer(Buffer const & b) {
310                 buffer_.reset(&b);
311         }
312 private:
313         ///
314         LyXLayout_ptr layout_;
315 #ifdef NO_STD_LIST
316         Paragraph * next_par_;
317         Paragraph * prev_par_;
318 #endif
319         ///
320         boost::optional<Buffer const *> buffer_;
321
322         struct Pimpl;
323         ///
324         friend struct Paragraph::Pimpl;
325         ///
326         Pimpl * pimpl_;
327
328 };
329
330
331 inline bool isInsertedText(Paragraph const & par, lyx::pos_type pos)
332 {
333         return par.lookupChange(pos) == Change::INSERTED;
334 }
335
336
337 inline bool isDeletedText(Paragraph const & par, lyx::pos_type pos)
338 {
339         return par.lookupChange(pos) == Change::DELETED;
340 }
341
342
343 bool operator==(Paragraph const & lhs, Paragraph const & rhs);
344
345 #endif // PARAGRAPH_H