]> git.lyx.org Git - lyx.git/blob - src/Text.h
Update of de/Math
[lyx.git] / src / Text.h
1 // -*- C++ -*-
2 /**
3  * \file Text.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author unknown
8  * \author Lars Gullik Bjønnes
9  * \author John Levon
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef TEXT_H
15 #define TEXT_H
16
17 #include "DocIterator.h"
18 #include "ParagraphList.h"
19
20 namespace lyx {
21
22 class BufferParams;
23 class BufferView;
24 class Change;
25 class CompletionList;
26 class Cursor;
27 class CursorData;
28 class CursorSlice;
29 class ErrorList;
30 class Font;
31 class FontInfo;
32 class FuncRequest;
33 class FuncStatus;
34 class Inset;
35 class InsetText;
36 class Lexer;
37 class Paragraph;
38 class ParagraphParameters;
39
40 /// This class encapsulates the main text data and operations in LyX.
41 /// This is more or less the private implementation of InsetText.
42 class Text {
43 private:
44         /// Default constructor.
45         Text(InsetText * owner, bool use_default_layout);
46
47         /// Copy constructor.
48         Text(InsetText * owner, Text const & text);
49
50 public:
51         /// \return true if there's no content at all.
52         /// \warning a non standard layout on an empty paragraph doesn't
53         /// count as empty.
54         bool empty() const;
55         /// Access to owner InsetText.
56         InsetText const & inset() const;
57
58         ///
59         FontInfo layoutFont(pit_type pit) const;
60         ///
61         FontInfo labelFont(Paragraph const & par) const;
62         /** Set font of character at position \p pos in paragraph \p pit.
63          *  Must not be called if \p pos denotes an inset with text contents,
64          *  and the inset is not allowed inside a font change (see below).
65          */
66         void setCharFont(pit_type pit, pos_type pos,
67                 Font const & font, Font const & display_font);
68
69         /** Needed to propagate font changes to all text cells of insets
70          *  that are not allowed inside a font change (bugs 1973, 6919).
71          *  Must not be called if \p pos denotes an ordinary character or an
72          *  inset that is alowed inside a font change.
73          */
74         void setInsetFont(BufferView const & bv, pit_type pit, pos_type pos,
75                 Font const & font);
76
77         /// what you expect when pressing \<enter\> at cursor position
78         /// \param inverse_logic if false, the same layout is set for the
79         /// new paragraph if the layout is an environment; if true, the
80         /// same layout is set if it is not an environment
81         void breakParagraph(Cursor & cur, bool inverse_logic = false);
82
83         /// set layout over selection
84         void setLayout(pit_type start, pit_type end,
85                 docstring const & layout);
86         /// Set given layout to current cursor position or selection.
87         /// Handles undo.
88         void setLayout(Cursor & cur, docstring const & layout);
89
90         /// what type of depth change to make
91         enum DEPTH_CHANGE {
92                 INC_DEPTH,
93                 DEC_DEPTH
94         };
95         /// Increase or decrease the nesting depth of the selected paragraph(s)
96         /// FIXME: replace Cursor with DocIterator.
97         void changeDepth(Cursor & cur, DEPTH_CHANGE type);
98
99         /// Returns whether something would be changed by changeDepth
100         /// FIXME: replace Cursor with DocIterator.
101         bool changeDepthAllowed(Cursor const & cur, DEPTH_CHANGE type) const;
102
103         /// Set font over selection paragraphs and rebreak.
104         /// FIXME: replace Cursor with DocIterator.
105         void setFont(Cursor & cur, Font const &, bool toggleall = false);
106         /// Set font from \p begin to \p end and rebreak.
107         void setFont(BufferView const & bv, CursorSlice const & begin,
108                 CursorSlice const & end, Font const &);
109
110         ///
111         void toggleFree(Cursor & cur, Font const &, bool toggleall = false);
112         /// Stack to save recent text propterty applications
113         std::vector<docstring> getFreeFonts() const;
114
115         /// ???
116         /// FIXME: replace Cursor with DocIterator.
117         docstring getStringForDialog(Cursor & cur);
118
119         /// Convert the paragraphs to a string.
120         /// \param AsStringParameter options. This can contain any combination of
121         /// asStringParameter values. Valid examples:
122         ///             asString(AS_STR_LABEL)
123         ///             asString(AS_STR_LABEL | AS_STR_INSETS)
124         ///             asString(AS_STR_INSETS)
125         docstring asString(int options = AS_STR_NONE) const;
126         ///
127         docstring asString(pit_type beg, pit_type end,
128                 int options = AS_STR_NONE) const;
129
130         /// truncates str to maxlenwith an ellipsis and replaces the characters '\n'
131         /// and '\t' with spaces
132         static void shortenForOutliner(docstring & str, size_t const maxlen);
133
134         /// Appends a possibly abbreviated representation of our text to \param os,
135         /// where \param maxlen defines the maximum size of \param os. If \param
136         /// shorten is true, then os is shortened as above.
137         void forOutliner(docstring & os, size_t maxlen, bool shorten = true) const;
138         /// Appends a possibly abbreviated representation of our text, from
139         /// start to end, to \param os, where \param maxlen defines the
140         /// maximum size of \param os. Omits the label of the first paragraph.
141         void forOutliner(docstring & os, size_t maxlen, pit_type start, pit_type end,
142                          bool shorten = true) const;
143
144         /// insert a character at cursor position
145         /// FIXME: replace Cursor with DocIterator.
146         void insertChar(Cursor & cur, char_type c);
147         /// insert an inset at cursor position
148         /// FIXME: replace Cursor with DocIterator.
149         void insertInset(Cursor & cur, Inset * inset);
150
151         /// try to handle that request
152         /// FIXME: replace Cursor with DocIterator.
153         void dispatch(Cursor & cur, FuncRequest & cmd);
154         /// do we want to handle this event?
155         bool getStatus(Cursor & cur, FuncRequest const & cmd,
156                 FuncStatus & status) const;
157
158         /// read-only access to individual paragraph
159         Paragraph const & getPar(pit_type pit) const { return pars_[pit]; }
160         /// read-write access to individual paragraph
161         Paragraph & getPar(pit_type pit) { return pars_[pit]; }
162         // Returns the current font and depth as a message.
163         // When \param devel_mode is true, add more precise information
164         docstring currentState(CursorData const & cur, bool devel_mode) const;
165
166         /** Find the word under \c from in the relative location
167          *  defined by \c word_location.
168          *  @param from return here the start of the word
169          *  @param to return here the end of the word
170          */
171         void getWord(CursorSlice & from, CursorSlice & to, word_location const) const;
172         /// just selects the word the cursor is in
173         void selectWord(Cursor & cur, word_location loc);
174         /// expands the selection to the word the cursor is in
175         void expandWordSel(Cursor & cur);
176         /// select all text
177         void selectAll(Cursor & cur);
178
179         /// what type of change operation to make
180         enum ChangeOp {
181                 ACCEPT,
182                 REJECT
183         };
184         /// accept or reject the selected change
185         void acceptOrRejectChanges(Cursor & cur, ChangeOp op);
186         /// accept the changes within the complete Text
187         void acceptChanges();
188         /// reject the changes within the complete Text
189         void rejectChanges();
190
191         /// returns true if par was empty and was removed
192         bool setCursor(Cursor & cur, pit_type pit, pos_type pos,
193                        bool setfont = true, bool boundary = false);
194         ///
195         void setCursorIntern(Cursor & cur, pit_type pit,
196                  pos_type pos, bool setfont = true, bool boundary = false);
197
198         /// Move cursor one position backwards
199         /**
200          * Returns true if an update is needed after the move.
201          */
202         bool cursorBackward(Cursor & cur);
203         /// Move cursor visually one position to the left
204         /**
205          * \param skip_inset if true, don't enter insets
206          * Returns true if an update is needed after the move.
207          */
208         bool cursorVisLeft(Cursor & cur, bool skip_inset = false);
209         /// Move cursor one position forward
210         /**
211          * Returns true if an update is needed after the move.
212          */
213         bool cursorForward(Cursor & cur);
214         /// Move cursor visually one position to the right
215         /**
216          * \param skip_inset if true, don't enter insets
217          * Returns true if an update is needed after the move.
218          */
219         bool cursorVisRight(Cursor & cur, bool skip_inset = false);
220         ///
221         bool cursorBackwardOneWord(Cursor & cur);
222         ///
223         bool cursorForwardOneWord(Cursor & cur);
224         ///
225         bool cursorVisLeftOneWord(Cursor & cur);
226         ///
227         bool cursorVisRightOneWord(Cursor & cur);
228         /// Delete from cursor up to the end of the current or next word.
229         /// Use force to skip the confirmDeletion check.
230         void deleteWordForward(Cursor & cur, bool force = false);
231         /// Delete from cursor to start of current or prior word.
232         void deleteWordBackward(Cursor & cur, bool force = false);
233         ///
234         bool cursorUpParagraph(Cursor & cur);
235         ///
236         bool cursorDownParagraph(Cursor & cur);
237         ///
238         bool cursorTop(Cursor & cur);
239         ///
240         bool cursorBottom(Cursor & cur);
241         /// Erase character at cursor. Honour change tracking
242         /// FIXME: replace Cursor with DocIterator.
243         bool erase(Cursor & cur);
244         /// Delete character before cursor. Honour CT
245         /// FIXME: replace Cursor with DocIterator.
246         bool backspace(Cursor & cur);
247         // Dissolve the inset under cursor
248         /// FIXME: replace Cursor with DocIterator.
249         bool dissolveInset(Cursor & cur);
250         /// FIXME: replace Cursor with DocIterator.
251         bool splitInset(Cursor & cur);
252         ///
253         bool selectWordWhenUnderCursor(Cursor & cur, word_location);
254         /// Change the case of the word at cursor position.
255         void changeCase(Cursor & cur, TextCase action, bool partial);
256         /// Transposes the character at the cursor with the one before it
257         void charsTranspose(Cursor & cur);
258
259         /** the DTP switches for paragraphs. LyX will store the top settings
260          always in the first physical paragraph, the bottom settings in the
261          last. When a paragraph is broken, the top settings rest, the bottom
262          settings are given to the new one.
263          This function will handle a multi-paragraph selection.
264          */
265         void setParagraphs(Cursor const & cur, docstring const & arg, bool merge = false);
266         /// Sets parameters for current or selected paragraphs
267         void setParagraphs(Cursor const & cur, ParagraphParameters const & p);
268
269         /* these things are for search and replace */
270
271         /// needed to insert the selection
272         void insertStringAsLines(Cursor & cur, docstring const & str,
273                 Font const & font);
274         /// needed to insert the selection
275         void insertStringAsParagraphs(Cursor & cur, docstring const & str,
276                 Font const & font);
277
278         /// access to our paragraphs
279         ParagraphList const & paragraphs() const { return pars_; }
280         ParagraphList & paragraphs() { return pars_; }
281         /// return true if this is the main text
282         bool isMainText() const;
283
284         ///
285         double spacing(Paragraph const & par) const;
286         /// make a suggestion for a label
287         /// FIXME: replace Cursor with DocIterator.
288         docstring getPossibleLabel(DocIterator const & cur) const;
289         /// is this paragraph right-to-left?
290         bool isRTL(pit_type pit) const;
291
292         ///
293         bool checkAndActivateInset(Cursor & cur, bool front);
294         ///
295         bool checkAndActivateInsetVisual(Cursor & cur, bool movingForward, bool movingLeft);
296
297         ///
298         void write(std::ostream & os) const;
299         /// returns true if \end_document has not been read
300         /// insetPtr is the containing Inset
301         bool read(Lexer & lex, ErrorList & errorList,
302                   InsetText * insetPtr);
303
304         /// delete double spaces, leading spaces, and empty paragraphs around old cursor.
305         /// \retval true if a change has happened and we need a redraw.
306         /// Handles undo.
307         static bool deleteEmptyParagraphMechanism(Cursor & cur,
308                 Cursor & old, bool & need_anchor_change);
309
310         /// delete double spaces, leading spaces, and empty paragraphs
311         /// from \first to \last paragraph
312         /// Does NOT handle undo (responsibility of the caller)
313         void deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges);
314
315         /// delete double spaces, leading spaces, and empty paragraphs
316         /// from \first to \last paragraph and \first_pos to \last_pos
317         /// Does NOT handle undo (responsibility of the caller)
318         void deleteEmptyParagraphMechanism(pit_type first, pit_type last,
319                                            pos_type first_pos, pos_type last_pos,
320                                            bool trackChanges);
321
322         /// To resolve macros properly the texts get their DocIterator.
323         /// Every macro definition is stored with its DocIterator
324         /// as well. Only those macros with a smaller iterator become
325         /// visible in a paragraph.
326         DocIterator macrocontextPosition() const;
327         ///
328         void setMacrocontextPosition(DocIterator const & pos);
329
330         ///
331         bool completionSupported(Cursor const & cur) const;
332         ///
333         CompletionList const * createCompletionList(Cursor const & cur) const;
334         /// Do a completion at the cursor position. Return true on success.
335         /// The completion does not contain the prefix. Handles undo.
336         bool insertCompletion(Cursor & cur, docstring const & s);
337         ///
338         docstring completionPrefix(Cursor const & cur) const;
339         /// find a paragraph before \p par with the given \p depth, if such
340         /// a paragraph cannot be found, \p par is returned
341         pit_type depthHook(pit_type pit, depth_type depth) const;
342         /// find a paragraph before \p par with depth less than the
343         /// depth of \p par. If such paragraph cannot be found because
344         /// \p par already has depth 0, lastpar + 1 is returned. If
345         /// such paragraph cannot be found because there isn't a par
346         /// with less depth before this one, \p par is returned.
347         pit_type outerHook(pit_type pit) const;
348         /// Is it the first par with same depth and layout?
349         bool isFirstInSequence(pit_type pit) const;
350         /// Return the last paragraph with same depth and layout, or a strictly
351         /// greater depth
352         pit_type lastInSequence(pit_type pit) const;
353         /// Is this paragraph in the table of contents?
354         int getTocLevel(pit_type pit) const;
355         /// Get the font of the "environment" of paragraph \p par_offset in \p pars.
356         /// All font changes of the paragraph are relative to this font.
357         Font const outerFont(pit_type pit_offset) const;
358         /// Return the label type at the end of paragraph \c pit.
359         int getEndLabel(pit_type pit) const;
360
361 private:
362         /// The InsetText owner shall have access to everything.
363         friend class InsetText;
364
365         // At cursor position 0, try to merge the paragraph with the one before it.
366         // Ignore change tracking, i.e., physically remove the end-of-par character
367         bool backspacePos0(Cursor & cur);
368         /// handle the case where bibitems were deleted
369         bool handleBibitems(Cursor & cur);
370         /// are we in a list item (description etc.)?
371         bool inDescriptionItem(Cursor const & cur) const;
372         ///
373         void charInserted(Cursor & cur);
374         /// set 'number' font property
375         void number(Cursor & cur);
376
377         /// paste plain text at current cursor.
378         /// \param str string to paste
379         /// \param asParagraphs whether to paste as paragraphs or as lines
380         void pasteString(Cursor & cur, docstring const & str,
381                         bool asParagraphs);
382         ///
383         void readParToken(Paragraph & par, Lexer & lex, std::string const & token,
384                 Font & font, Change & change, ErrorList & errorList);
385         ///
386         void readParagraph(Paragraph & par, Lexer & lex, ErrorList & errorList);
387         /// Set Label Width string to all paragraphs of the same layout
388         /// and depth in a sequence.
389         void setLabelWidthStringToSequence(Cursor const & cur, docstring const & s);
390
391         /// Owner Inset.
392         InsetText * owner_;
393         ///
394         ParagraphList pars_;
395         /// position of the text in the buffer.
396         DocIterator macrocontext_position_;
397 };
398
399
400 ///
401 void breakParagraphConservative(BufferParams const & bparams,
402                                 ParagraphList & paragraphs,
403                                 pit_type pit,
404                                 pos_type pos);
405
406 /**
407  * Append the next paragraph onto the tail of this one.
408  * Be careful, this doesent make any check at all.
409  */
410 void mergeParagraph(BufferParams const & bparams,
411         ParagraphList & paragraphs, pit_type pit);
412
413 /// accept the changes within the complete ParagraphList
414 void acceptChanges(ParagraphList & pars, BufferParams const & bparams);
415
416 /// reject the changes within the complete ParagraphList
417 void rejectChanges(ParagraphList & pars, BufferParams const & bparams);
418
419 } // namespace lyx
420
421 #endif // TEXT_H