]> git.lyx.org Git - lyx.git/blob - src/paragraph.h
Translate labels based on the paragraph language instead of the Buffer language.
[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
28 namespace lyx {
29
30
31 class Buffer;
32 class BufferParams;
33 class Counters;
34 class InsetBase;
35 class InsetBibitem;
36 class LaTeXFeatures;
37 class InsetBase_code;
38 class Language;
39 class LyXFont;
40 class LyXFont_size;
41 class MetricsInfo;
42 class OutputParams;
43 class PainterInfo;
44 class ParagraphParameters;
45 class TexRow;
46
47
48 class FontSpan {
49 public:
50         /// Invalid font span containing no character
51         FontSpan() : first(0), last(-1) {}
52         /// Span including first and last
53         FontSpan(pos_type f, pos_type l) : first(f), last(l) {}
54
55 public:
56         /// Range including first and last.
57         pos_type first, last;
58 };
59
60
61 /// A Paragraph holds all text, attributes and insets in a text paragraph
62 /// \todo FIXME: any reference to ParagraphMetrics (including inheritance)
63 /// should go in order to complete the Model/View separation of this class.
64 class Paragraph  {
65 public:
66         ///
67         enum {
68                 /// Note that this is 1 right now to avoid
69                 /// crashes where getChar() is called wrongly
70                 /// (returning 0) - if this was 0, then we'd
71                 /// try getInset() and crash. We should fix
72                 /// all these places.
73                 //META_INSET = 1 // as in trunk
74                 META_INSET = 0x200001  // above 0x10ffff, for ucs-4
75         };
76         ///
77         typedef char_type value_type;
78         ///
79         typedef std::vector<value_type> TextContainer;
80
81         ///
82         Paragraph();
83         ///
84         Paragraph(Paragraph const &);
85         ///
86         Paragraph & operator=(Paragraph const &);
87         ///
88         ~Paragraph();
89         ///
90         int id() const;
91
92
93         ///
94         Language const * getParLanguage(BufferParams const &) const;
95         ///
96         bool isRightToLeftPar(BufferParams const &) const;
97         ///
98         void changeLanguage(BufferParams const & bparams,
99                             Language const * from, Language const * to);
100         ///
101         bool isMultiLingual(BufferParams const &) const;
102
103         ///
104         docstring const asString(Buffer const &,
105                                    OutputParams const & runparams,
106                                    bool label) const;
107         ///
108         docstring const asString(Buffer const &, bool label) const;
109         ///
110         docstring const asString(Buffer const & buffer,
111                                    pos_type beg,
112                                    pos_type end,
113                                    bool label) const;
114         ///
115         docstring const asString(Buffer const &,
116                                    OutputParams const & runparams,
117                                    pos_type beg,
118                                    pos_type end,
119                                    bool label) const;
120
121         ///
122         void write(Buffer const &, std::ostream &, BufferParams const &,
123                    depth_type & depth) const;
124         ///
125         void validate(LaTeXFeatures &) const;
126
127         ///
128         int startTeXParParams(BufferParams const &, odocstream &, bool) const;
129
130         ///
131         int endTeXParParams(BufferParams const &, odocstream &, bool) const;
132
133
134         ///
135         bool simpleTeXOnePar(Buffer const &, BufferParams const &,
136                              LyXFont const & outerfont, odocstream &,
137                              TexRow & texrow, OutputParams const &) const;
138
139         /// Can we drop the standard paragraph wrapper?
140         bool emptyTag() const;
141
142         /// Get the id of the paragraph, usefull for docbook
143         std::string getID(Buffer const & buf,
144                           OutputParams const & runparams) const;
145
146         /// Get the first word of a paragraph, return the position where it left
147         pos_type getFirstWord(Buffer const & buf,
148                                    odocstream & os,
149                                    OutputParams const & runparams) const;
150
151         /// Checks if the paragraph contains only text and no inset or font change.
152         bool onlyText(Buffer const & buf, LyXFont const & outerfont,
153                       pos_type initial) const;
154
155         /// Writes to stream the docbook representation
156         void simpleDocBookOnePar(Buffer const & buf,
157                                  odocstream &,
158                                  OutputParams const & runparams,
159                                  LyXFont const & outerfont,
160                                  pos_type initial = 0) const;
161
162         ///
163         bool hasSameLayout(Paragraph const & par) const;
164
165         ///
166         void makeSameLayout(Paragraph const & par);
167
168         ///
169         void setInsetOwner(InsetBase * inset);
170         ///
171         InsetBase * inInset() const;
172         ///
173         InsetBase::Code ownerCode() const;
174         ///
175         bool forceDefaultParagraphs() const;
176
177         ///
178         pos_type size() const { return text_.size(); }
179         ///
180         bool empty() const { return text_.empty(); }
181         ///
182         void clearContents();
183
184         ///
185         LyXLayout_ptr const & layout() const;
186         ///
187         void layout(LyXLayout_ptr const & new_layout);
188
189         /// This is the item depth, only used by enumerate and itemize
190         signed char itemdepth;
191
192         ///
193         InsetBibitem * bibitem() const;  // ale970302
194
195         /// look up change at given pos
196         Change const & lookupChange(pos_type pos) const;
197
198         /// is there a change within the given range ?
199         bool isChanged(pos_type start, pos_type end) const;
200         /// is there an unchanged char at the given pos ?
201         bool isUnchanged(pos_type pos) const {
202                 return lookupChange(pos).type == Change::UNCHANGED;
203         }
204         /// is there an insertion at the given pos ?
205         bool isInserted(pos_type pos) const {
206                 return lookupChange(pos).type == Change::INSERTED;
207         }
208         /// is there a deletion at the given pos ?
209         bool isDeleted(pos_type pos) const {
210                 return lookupChange(pos).type == Change::DELETED;
211         }
212
213         /// will the paragraph be physically merged with the next
214         /// one if the imaginary end-of-par character is logically deleted?
215         bool isMergedOnEndOfParDeletion(bool trackChanges) const;
216
217         /// set change for the entire par
218         void setChange(Change const & change);
219
220         /// set change at given pos
221         void setChange(pos_type pos, Change const & change);
222
223         /// accept changes within the given range
224         void acceptChanges(pos_type start, pos_type end);
225
226         /// reject changes within the given range
227         void rejectChanges(pos_type start, pos_type end);
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         pos_type beginOfBody() const;
235         /// recompute this value
236         void setBeginOfBody();
237
238         ///
239         docstring const & getLabelstring() const;
240
241         /// the next two functions are for the manual labels
242         docstring const getLabelWidthString() const;
243         /// Set label width string.
244         void setLabelWidthString(docstring const & s);
245         /// translate \p label to the paragraph language if possible.
246         docstring const translateIfPossible(docstring const & label,
247                 BufferParams const & bparams) const;
248         /// Expand the counters for the labelstring of \c layout
249         docstring expandLabel(LyXLayout_ptr const &, BufferParams const &,
250                 bool process_appendix = true) const;
251         /// Actual paragraph alignment used
252         char getAlign() const;
253         /// The nesting depth of a paragraph
254         depth_type getDepth() const;
255         /// The maximal possible depth of a paragraph after this one
256         depth_type getMaxDepthAfter() const;
257         ///
258         void applyLayout(LyXLayout_ptr const & new_layout);
259
260         /// (logically) erase the char at pos; return true if it was actually erased
261         bool eraseChar(pos_type pos, bool trackChanges);
262         /// (logically) erase the given range; return the number of chars actually erased
263         int eraseChars(pos_type start, pos_type end, bool trackChanges);
264
265         /** Get uninstantiated font setting. Returns the difference
266             between the characters font and the layoutfont.
267             This is what is stored in the fonttable
268         */
269         LyXFont const
270         getFontSettings(BufferParams const &, pos_type pos) const;
271         ///
272         LyXFont const getFirstFontSettings(BufferParams const &) const;
273
274         /** Get fully instantiated font. If pos == -1, use the layout
275             font attached to this paragraph.
276             If pos == -2, use the label font of the layout attached here.
277             In all cases, the font is instantiated, i.e. does not have any
278             attributes with values LyXFont::INHERIT, LyXFont::IGNORE or
279             LyXFont::TOGGLE.
280         */
281         LyXFont const getFont(BufferParams const &, pos_type pos,
282                               LyXFont const & outerfont) const;
283         LyXFont const getLayoutFont(BufferParams const &,
284                                     LyXFont const & outerfont) const;
285         LyXFont const getLabelFont(BufferParams const &,
286                                    LyXFont const & outerfont) const;
287         /**
288          * The font returned by the above functions is the same in a
289          * span of characters. This method will return the first and
290          * the last positions in the paragraph for which that font is
291          * the same. This can be used to avoid unnecessary calls to getFont.
292          */
293         FontSpan fontSpan(pos_type pos) const;
294         ///
295         /// this is a bottleneck.
296         value_type getChar(pos_type pos) const { return text_[pos]; }
297         /// Get the char, but mirror all bracket characters if it is right-to-left
298         value_type getUChar(BufferParams const &, pos_type pos) const;
299         /// pos <= size() (there is a dummy font change at the end of each par)
300         void setFont(pos_type pos, LyXFont const & font);
301         /// Returns the height of the highest font in range
302         LyXFont_size highestFontInRange(pos_type startpos,
303                                         pos_type endpos, LyXFont_size def_size) const;
304         ///
305         void insert(pos_type pos, docstring const & str,
306                     LyXFont const & font, Change const & change);
307         ///
308         void insertChar(pos_type pos, value_type c, bool trackChanges);
309         ///
310         void insertChar(pos_type pos, value_type c,
311                         LyXFont const &, bool trackChanges);
312         ///
313         void insertChar(pos_type pos, value_type c,
314                         LyXFont const &, Change const & change);
315         ///
316         void insertInset(pos_type pos, InsetBase * inset,
317                          Change const & change);
318         ///
319         void insertInset(pos_type pos, InsetBase * inset,
320                          LyXFont const &, Change const & change);
321         ///
322         bool insetAllowed(InsetBase_code code);
323         ///
324         InsetBase * getInset(pos_type pos) {
325                 return insetlist.get(pos);
326         }
327         ///
328         InsetBase const * getInset(pos_type pos) const {
329                 return insetlist.get(pos);
330         }
331
332         ///
333         bool isHfill(pos_type pos) const {
334                 return isInset(pos)
335                        && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
336         }
337         /// hinted by profiler
338         bool isInset(pos_type pos) const {
339                 return getChar(pos) == static_cast<value_type>(META_INSET);
340         }
341         ///
342         bool isNewline(pos_type pos) const;
343         /// return true if the char is a word separator
344         bool isSeparator(pos_type pos) const { return getChar(pos) == ' '; }
345         ///
346         bool isLineSeparator(pos_type pos) const;
347         /// True if the character/inset at this point can be part of a word.
348         /// Note that digits in particular are considered as letters
349         bool isLetter(pos_type pos) const;
350
351         /// returns -1 if inset not found
352         int getPositionOfInset(InsetBase const * inset) const;
353
354         /// Returns the number of line breaks and white-space stripped at the start
355         int stripLeadingSpaces(bool trackChanges);
356
357         /// return true if we allow multiple spaces
358         bool isFreeSpacing() const;
359
360         /// return true if we allow this par to stay empty
361         bool allowEmpty() const;
362         ///
363         char_type transformChar(char_type c, pos_type pos) const;
364         ///
365         ParagraphParameters & params();
366         ///
367         ParagraphParameters const & params() const;
368         ///
369         bool hfillExpansion(Row const & row, pos_type pos) const;
370
371         /// Check if we are in a Biblio environment.
372         /// \retval true if the cursor needs to be moved right.
373         bool checkBiblio(bool track_changes);
374
375 public:
376         ///
377         InsetList insetlist;
378
379 private:
380
381         ///
382         LyXLayout_ptr layout_;
383         /**
384          * Keeping this here instead of in the pimpl makes LyX >10% faster
385          * for average tasks as buffer loading/switching etc.
386          */
387         TextContainer text_;
388         /// end of label
389         pos_type begin_of_body_;
390
391         /// Pimpl away stuff
392         class Pimpl;
393         ///
394         friend class Paragraph::Pimpl;
395         ///
396         Pimpl * pimpl_;
397 };
398
399 } // namespace lyx
400
401 #endif // PARAGRAPH_H