]> git.lyx.org Git - lyx.git/blob - src/Text.h
Cleanup: Replace a bunch of Cursor arguments with DocIterators.
[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 BufferView;
23 class CompletionList;
24 class CursorSlice;
25 class DocIterator;
26 class ErrorList;
27 class Font;
28 class FontInfo;
29 class FuncRequest;
30 class FuncStatus;
31 class Inset;
32 class Cursor;
33 class Lexer;
34 class PainterInfo;
35 class Spacing;
36
37
38 /// This class encapsulates the main text data and operations in LyX
39 class Text {
40 public:
41         /// constructor
42         explicit Text(InsetText * owner)
43                 : autoBreakRows_(false), owner_(owner)
44         {}
45
46         /// \return true if there's no content at all.
47         /// \warning a non standard layout on an empty paragraph doesn't
48         // count as empty.
49         bool empty() const;
50         /// Access to owner InsetText.
51         InsetText const & inset() const;
52
53         ///
54         FontInfo layoutFont(pit_type pit) const;
55         ///
56         FontInfo labelFont(Paragraph const & par) const;
57         /** Set font of character at position \p pos in paragraph \p pit.
58          *  Must not be called if \p pos denotes an inset with text contents,
59          *  and the inset is not allowed inside a font change (see below).
60          */
61         void setCharFont(pit_type pit, pos_type pos,
62                 Font const & font, Font const & display_font);
63
64         /** Needed to propagate font changes to all text cells of insets
65          *  that are not allowed inside a font change (bug 1973).
66          *  Must not be called if \p pos denotes an ordinary character or an
67          *  inset that is alowed inside a font change.
68          *  FIXME: This should be removed, see documentation of noFontChange
69          *  in insetbase.h
70          */
71         void setInsetFont(BufferView const & bv, pit_type pit, pos_type pos,
72                 Font const & font, bool toggleall = false);
73
74         /// what you expect when pressing \<enter\> at cursor position
75         void breakParagraph(Cursor & cur, bool inverse_logic = false);
76
77         /// set layout over selection
78         void setLayout(pit_type start, pit_type end,
79                 docstring const & layout);
80         /// Set given layout to current cursor position.
81         /// FIXME: replace Cursor with DocIterator.
82         void setLayout(Cursor & cur, docstring const & layout);
83
84         /// what type of depth change to make
85         enum DEPTH_CHANGE {
86                 INC_DEPTH,
87                 DEC_DEPTH
88         };
89         /// Increase or decrease the nesting depth of the selected paragraph(s)
90         /// FIXME: replace Cursor with DocIterator.
91         void changeDepth(Cursor & cur, DEPTH_CHANGE type);
92
93         /// Returns whether something would be changed by changeDepth
94         /// FIXME: replace Cursor with DocIterator.
95         bool changeDepthAllowed(Cursor & cur, DEPTH_CHANGE type) const;
96
97         /// Set font over selection paragraphs and rebreak.
98         /// FIXME: replace Cursor with DocIterator.
99         void setFont(Cursor & cur, Font const &, bool toggleall = false);
100         /// Set font from \p begin to \p end and rebreak.
101         void setFont(BufferView const & bv, CursorSlice const & begin,
102                 CursorSlice const & end, Font const &,
103                 bool toggleall = false);
104
105         ///
106         void toggleFree(Cursor & cur, Font const &, bool toggleall = false);
107
108         /// ???
109         /// FIXME: replace Cursor with DocIterator.
110         docstring getStringToIndex(Cursor const & cur);
111
112         /// Convert the paragraphs to a string.
113         /// \param AsStringParameter options. This can contain any combination of
114         /// asStringParameter values. Valid examples:
115         ///             asString(AS_STR_LABEL)
116         ///             asString(AS_STR_LABEL | AS_STR_INSETS)
117         ///             asString(AS_STR_INSETS)
118         docstring asString(int options = AS_STR_NONE) const;
119         ///
120         docstring asString(pit_type beg, pit_type end,
121                 int options = AS_STR_NONE) const;
122
123         /// insert a character at cursor position
124         /// FIXME: replace Cursor with DocIterator.
125         void insertChar(Cursor & cur, char_type c);
126         /// insert an inset at cursor position
127         /// FIXME: replace Cursor with DocIterator.
128         void insertInset(Cursor & cur, Inset * inset);
129
130         /// try to handle that request
131         /// FIXME: replace Cursor with DocIterator.
132         void dispatch(Cursor & cur, FuncRequest & cmd);
133         /// do we want to handle this event?
134         bool getStatus(Cursor & cur, FuncRequest const & cmd,
135                 FuncStatus & status) const;
136
137         /// read-only access to individual paragraph
138         Paragraph const & getPar(pit_type pit) const { return pars_[pit]; }
139         /// read-write access to individual paragraph
140         Paragraph & getPar(pit_type pit) { return pars_[pit]; }
141         // Returns the current font and depth as a message.
142         /// FIXME: replace Cursor with DocIterator.
143         docstring currentState(Cursor const & cur) const;
144
145         /** Find the word under \c from in the relative location
146          *  defined by \c word_location.
147          *  @param from return here the start of the word
148          *  @param to return here the end of the word
149          */
150         void getWord(CursorSlice & from, CursorSlice & to, word_location const) const;
151         /// just selects the word the cursor is in
152         void selectWord(Cursor & cur, word_location loc);
153         /// select all text
154         void selectAll(Cursor & cur);
155         /// convenience function get the previous word or an empty string
156         docstring previousWord(CursorSlice const & sl) const;
157         
158         /// what type of change operation to make
159         enum ChangeOp {
160                 ACCEPT,
161                 REJECT
162         };
163         /// accept or reject the selected change
164         void acceptOrRejectChanges(Cursor & cur, ChangeOp op);
165         /// accept the changes within the complete Text
166         void acceptChanges();
167         /// reject the changes within the complete Text
168         void rejectChanges();
169
170         /// returns true if par was empty and was removed
171         bool setCursor(Cursor & cur, pit_type par, pos_type pos,
172                        bool setfont = true, bool boundary = false);
173         ///
174         void setCursor(CursorSlice &, pit_type par, pos_type pos);
175         ///
176         void setCursorIntern(Cursor & cur, pit_type par,
177                  pos_type pos, bool setfont = true, bool boundary = false);
178
179         ///
180         void recUndo(Cursor & cur, pit_type first, pit_type last) const;
181         ///
182         void recUndo(Cursor & cur, pit_type first) const;
183
184         /// Move cursor one position backwards
185         /**
186          * Returns true if an update is needed after the move.
187          */
188         bool cursorBackward(Cursor & cur);
189         /// Move cursor visually one position to the left
190         /**
191          * \param skip_inset if true, don't enter insets
192          * Returns true if an update is needed after the move.
193          */
194         bool cursorVisLeft(Cursor & cur, bool skip_inset = false);
195         /// Move cursor one position forward
196         /**
197          * Returns true if an update is needed after the move.
198          */
199         bool cursorForward(Cursor & cur);
200         /// Move cursor visually one position to the right
201         /**
202          * \param skip_inset if true, don't enter insets
203          * Returns true if an update is needed after the move.
204          */
205         bool cursorVisRight(Cursor & cur, bool skip_inset = false);
206         ///
207         bool cursorBackwardOneWord(Cursor & cur);
208         ///
209         bool cursorForwardOneWord(Cursor & cur);
210         ///
211         bool cursorVisLeftOneWord(Cursor & cur);
212         ///
213         bool cursorVisRightOneWord(Cursor & cur);
214         /// Delete from cursor up to the end of the current or next word.
215         void deleteWordForward(Cursor & cur);
216         /// Delete from cursor to start of current or prior word.
217         void deleteWordBackward(Cursor & cur);
218         ///
219         bool cursorUpParagraph(Cursor & cur);
220         ///
221         bool cursorDownParagraph(Cursor & cur);
222         ///
223         bool cursorTop(Cursor & cur);
224         ///
225         bool cursorBottom(Cursor & cur);
226         /// Erase character at cursor. Honour change tracking
227         /// FIXME: replace Cursor with DocIterator.
228         bool erase(Cursor & cur);
229         /// Delete character before cursor. Honour CT
230         /// FIXME: replace Cursor with DocIterator.
231         bool backspace(Cursor & cur);
232         // Dissolve the inset under cursor
233         /// FIXME: replace Cursor with DocIterator.
234         bool dissolveInset(Cursor & cur);
235         ///
236         bool selectWordWhenUnderCursor(Cursor & cur, word_location);
237         /// Change the case of the word at cursor position.
238         void changeCase(Cursor & cur, TextCase action);
239         /// Transposes the character at the cursor with the one before it
240         void charsTranspose(Cursor & cur);
241
242         /** the DTP switches for paragraphs. LyX will store the top settings
243          always in the first physical paragraph, the bottom settings in the
244          last. When a paragraph is broken, the top settings rest, the bottom
245          settings are given to the new one.
246          This function will handle a multi-paragraph selection.
247          */
248         void setParagraphs(Cursor & cur, docstring arg, bool modify = false);
249         /// Sets parameters for current or selected paragraphs
250         void setParagraphs(Cursor & cur, ParagraphParameters const & p);
251
252         /* these things are for search and replace */
253
254         /// needed to insert the selection
255         void insertStringAsLines(DocIterator const & dit, docstring const & str,
256                 Font const & font);
257         /// needed to insert the selection
258         void insertStringAsParagraphs(DocIterator const & dit, docstring const & str,
259                 Font const & font);
260
261         /// access to our paragraphs
262         ParagraphList const & paragraphs() const { return pars_; }
263         ParagraphList & paragraphs() { return pars_; }
264         /// return true if this is the main text
265         bool isMainText() const;
266
267         ///
268         double spacing(Paragraph const & par) const;
269         /// make a suggestion for a label
270         /// FIXME: replace Cursor with DocIterator.
271         docstring getPossibleLabel(Cursor const & cur) const;
272         /// is this paragraph right-to-left?
273         bool isRTL(Paragraph const & par) const;
274
275         ///
276         bool checkAndActivateInset(Cursor & cur, bool front);
277         ///
278         bool checkAndActivateInsetVisual(Cursor & cur, bool movingForward, bool movingLeft);
279
280         ///
281         void write(std::ostream & os) const;
282         /// returns true if \end_document has not been read
283         /// insetPtr is the containing Inset
284         bool read(Lexer & lex, ErrorList & errorList, 
285                   InsetText * insetPtr);
286
287         /// delete double spaces, leading spaces, and empty paragraphs around old cursor.
288         /// \retval true if a change has happened and we need a redraw.
289         /// FIXME: replace Cursor with DocIterator. This is not possible right
290         /// now because recordUndo() is called which needs a Cursor.
291         static bool deleteEmptyParagraphMechanism(Cursor & cur,
292                 Cursor & old, bool & need_anchor_change);
293
294         /// delete double spaces, leading spaces, and empty paragraphs
295         /// from \first to \last paragraph
296         void deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges);
297
298         /// To resolve macros properly the texts get their DocIterator.
299         /// Every macro definition is stored with its DocIterator
300         /// as well. Only those macros with a smaller iterator become 
301         /// visible in a paragraph.
302         DocIterator macrocontextPosition() const;
303         ///
304         void setMacrocontextPosition(DocIterator const & pos);
305
306         ///
307         bool completionSupported(Cursor const & cur) const;
308         ///
309         CompletionList const * createCompletionList(Cursor const & cur) const;
310         ///
311         bool insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/);
312         ///
313         docstring completionPrefix(Cursor const & cur) const;
314
315 private:
316         /// The InsetText owner shall have access to everything.
317         friend class InsetText;
318
319         /// return past-the-last paragraph influenced by a layout
320         /// change on pit
321         pit_type undoSpan(pit_type pit);
322
323         // fix the cursor `cur' after a characters has been deleted at `where'
324         // position. Called by deleteEmptyParagraphMechanism
325         static void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where);
326
327         // At cursor position 0, try to merge the paragraph with the one before it.
328         // Ignore change tracking, i.e., physically remove the end-of-par character
329         bool backspacePos0(Cursor & cur);
330         /// handle the case where bibitems were deleted
331         bool handleBibitems(Cursor & cur);
332         /// are we in a list item (description etc.)?
333         bool inDescriptionItem(Cursor & cur) const;
334         ///
335         void charInserted(Cursor & cur);
336         /// set 'number' font property
337         void number(Cursor & cur);
338
339         /// paste plain text at current cursor.
340         /// \param str string to paste
341         /// \param asParagraphs whether to paste as paragraphs or as lines
342         void pasteString(Cursor & cur, docstring const & str,
343                         bool asParagraphs);
344         ///
345         void readParToken(Paragraph & par, Lexer & lex, std::string const & token,
346                 Font & font, Change & change, ErrorList & errorList);
347         ///
348         void readParagraph(Paragraph & par, Lexer & lex, ErrorList & errorList);
349
350         /// Owner Inset.
351         InsetText * owner_;
352         ///
353         ParagraphList pars_;
354         ///
355         bool autoBreakRows_;
356         /// position of the text in the buffer.
357         DocIterator macrocontext_position_;
358 };
359
360 } // namespace lyx
361
362 #endif // TEXT_H