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